-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ENH] add voxel_dimensions [TEST] add test for voxel_dimensions
- Loading branch information
Showing
5 changed files
with
82 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.