diff --git a/narps_open/core/common.py b/narps_open/core/common.py index 6889ed75..9a076719 100644 --- a/narps_open/core/common.py +++ b/narps_open/core/common.py @@ -3,26 +3,19 @@ """ Common functions to write pipelines """ -def remove_files(_, files): +def remove_file(_, file_name): """ Fully remove files generated by a Node, once they aren't needed anymore. This function is meant to be used in a Nipype Function Node. Parameters: - _: input only used for triggering the Node - - files: str or list, a single filename or a list of absolute filenames to remove + - file_name: str, a single absolute filename of the file to remove """ # This import must stay inside the function, as required by Nipype from os import remove - if isinstance(files, str): - files = [files] - try: - for file in files: - remove(file) + remove(file_name) except OSError as error: print(error) - else: - print('The following files were successfully deleted.') - print(files) diff --git a/narps_open/core/image.py b/narps_open/core/image.py new file mode 100644 index 00000000..35323e45 --- /dev/null +++ b/narps_open/core/image.py @@ -0,0 +1,25 @@ +#!/usr/bin/python +# coding: utf-8 + +""" Image functions to write pipelines """ + +def get_voxel_dimensions(image: str) -> list: + """ + Return the voxel dimensions of a image in millimeters. + + Arguments: + image: str, string that represent an absolute path to a Nifti image. + + Returns: + list, size of the voxels in the image in millimeters. + """ + # This import must stay inside the function, as required by Nipype + from nibabel import load + + voxel_dimensions = load(image).header.get_zooms() + + return [ + float(voxel_dimensions[0]), + float(voxel_dimensions[1]), + float(voxel_dimensions[2]) + ] diff --git a/tests/core/test_common.py b/tests/core/test_common.py index 30142fdc..a86add9f 100644 --- a/tests/core/test_common.py +++ b/tests/core/test_common.py @@ -19,7 +19,7 @@ from nipype import Node, Function from narps_open.utils.configuration import Configuration -from narps_open.core.common import remove_files +import narps_open.core.common as co TEMPORARY_DIR = join(Configuration()['directories']['test_runs'], 'test_common') @@ -38,7 +38,7 @@ class TestCoreCommon: @staticmethod @mark.unit_test def test_remove_file(remove_test_dir): - """ Test the remove_files function with only one file """ + """ Test the remove_file function """ # Create a single file test_file_path = abspath(join(TEMPORARY_DIR, 'file1.txt')) @@ -49,41 +49,13 @@ def test_remove_file(remove_test_dir): # Create a Nipype Node using remove_files test_remove_file_node = Node(Function( - function = remove_files, - input_names = ['_', 'files'], + function = co.remove_file, + input_names = ['_', 'file_name'], output_names = [] - ), 'test_remove_file_node') + ), name = 'test_remove_file_node') test_remove_file_node.inputs._ = '' - test_remove_file_node.inputs.files = test_file_path + test_remove_file_node.inputs.file_name = test_file_path test_remove_file_node.run() # Check file is removed assert not exists(test_file_path) - - @staticmethod - @mark.unit_test - def test_remove_files(remove_test_dir): - """ Test the remove_files function with a list of files """ - - # Create a list of files - test_file_paths = [ - abspath(join(TEMPORARY_DIR, 'file1.txt')), - abspath(join(TEMPORARY_DIR, 'file2.txt')) - ] - for test_file_path in test_file_paths: - Path(test_file_path).touch() - assert exists(test_file_path) - - # Create a Nipype Node using remove_files - test_remove_files_node = Node(Function( - function = remove_files, - input_names = ['_', 'files'], - output_names = [] - ), 'test_remove_files_node') - test_remove_files_node.inputs._ = '' - test_remove_files_node.inputs.files = test_file_paths - test_remove_files_node.run() - - # Check files are removed - for test_file_path in test_file_paths: - assert not exists(test_file_path) diff --git a/tests/core/test_image.py b/tests/core/test_image.py new file mode 100644 index 00000000..d3b83ac5 --- /dev/null +++ b/tests/core/test_image.py @@ -0,0 +1,48 @@ +#!/usr/bin/python +# coding: utf-8 + +""" Tests of the 'narps_open.core.image' module. + +Launch this test with PyTest + +Usage: +====== + pytest -q test_image.py + pytest -q test_image.py -k +""" + +from os.path import abspath, join +from numpy import isclose + +from pytest import mark +from nipype import Node, Function + +from narps_open.utils.configuration import Configuration +import narps_open.core.image as im + +class TestCoreImage: + """ A class that contains all the unit tests for the image module.""" + + @staticmethod + @mark.unit_test + def test_get_voxel_dimensions(): + """ Test the get_voxel_dimensions function """ + + # Path to the test image + test_file_path = abspath(join( + Configuration()['directories']['test_data'], + 'core', + 'image', + 'test_image.nii.gz')) + + # Create a Nipype Node using get_voxel_dimensions + test_get_voxel_dimensions_node = Node(Function( + function = im.get_voxel_dimensions, + input_names = ['image'], + output_names = ['voxel_dimensions'] + ), name = 'test_get_voxel_dimensions_node') + test_get_voxel_dimensions_node.inputs.image = test_file_path + outputs = test_get_voxel_dimensions_node.run().outputs + + # Check voxel sizes + assert isclose(outputs.voxel_dimensions, [8.0, 8.0, 9.6]).all() diff --git a/tests/test_data/core/image/test_image.nii.gz b/tests/test_data/core/image/test_image.nii.gz new file mode 100644 index 00000000..06fb9aa3 Binary files /dev/null and b/tests/test_data/core/image/test_image.nii.gz differ