Skip to content

Commit

Permalink
[ENH] add voxel_dimensions [TEST] add test for voxel_dimensions
Browse files Browse the repository at this point in the history
  • Loading branch information
bclenet committed Nov 16, 2023
1 parent 04976a0 commit 202fa49
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 44 deletions.
13 changes: 3 additions & 10 deletions narps_open/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
25 changes: 25 additions & 0 deletions narps_open/core/image.py
Original file line number Diff line number Diff line change
@@ -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])
]
40 changes: 6 additions & 34 deletions tests/core/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -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'))
Expand All @@ -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)
48 changes: 48 additions & 0 deletions tests/core/test_image.py
Original file line number Diff line number Diff line change
@@ -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 <selected_test>
"""

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()
Binary file added tests/test_data/core/image/test_image.nii.gz
Binary file not shown.

0 comments on commit 202fa49

Please sign in to comment.