-
Notifications
You must be signed in to change notification settings - Fork 258
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added some initial tests to check the writing of transforms to ITK and FSL formats.
- Loading branch information
Showing
13 changed files
with
148 additions
and
5 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 @@ | ||
affine-RAS-itk.tfm |
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 @@ | ||
affine-RAS.fsl |
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,5 @@ | ||
#Insight Transform File V1.0 | ||
#Transform 0 | ||
Transform: MatrixOffsetTransformBase_double_3_3 | ||
Parameters: 0.999999 -0.000999999 -0.001 0.00140494 0.621609 0.783327 -0.000161717 -0.783327 0.62161 -4 -2 -1 | ||
FixedParameters: 0 0 0 |
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,4 @@ | ||
0.999999 -0.00140494 0.000161717 -3.89014 | ||
0.000999999 0.621609 -0.783327 105.905 | ||
0.001 0.783327 0.62161 -34.3513 | ||
0 0 0 1 |
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,5 @@ | ||
#Insight Transform File V1.0 | ||
#Transform 0 | ||
Transform: MatrixOffsetTransformBase_double_3_3 | ||
Parameters: 0.999999 -0.000999999 -0.001 0.00140494 0.621609 0.783327 -0.000161717 -0.783327 0.62161 -4 -2 -1 | ||
FixedParameters: 0 0 0 |
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,4 @@ | ||
0.999999 -0.00140494 -0.000161717 4.14529 | ||
0.000999999 0.621609 0.783327 -37.3811 | ||
-0.001 -0.783327 0.62161 107.976 | ||
0 0 0 1 |
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,5 @@ | ||
#Insight Transform File V1.0 | ||
#Transform 0 | ||
Transform: MatrixOffsetTransformBase_double_3_3 | ||
Parameters: 0.999999 -0.000999999 -0.001 0.00140494 0.621609 0.783327 -0.000161717 -0.783327 0.62161 -4 -2 -1 | ||
FixedParameters: 0 0 0 |
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,4 @@ | ||
0.999998 -0.00181872 -0.0011965 4.26083 | ||
0.00206779 0.621609 0.783325 -25.3129 | ||
-0.000680894 -0.783326 0.621611 101.967 | ||
0 0 0 1 |
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 @@ | ||
../../../doc/source/downloads/someones_anatomy.nii.gz |
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,71 @@ | ||
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- | ||
# vi: set ft=python sts=4 ts=4 sw=4 et: | ||
"""Tests of the transform module.""" | ||
import os | ||
import numpy as np | ||
from numpy.testing import assert_array_equal, assert_almost_equal, \ | ||
assert_array_almost_equal | ||
import pytest | ||
|
||
from ..loadsave import load as loadimg | ||
from ..nifti1 import Nifti1Image | ||
from ..eulerangles import euler2mat | ||
from ..affines import from_matvec | ||
from ..volumeutils import shape_zoom_affine | ||
from ..transform import linear as nbl | ||
from ..testing import (assert_equal, assert_not_equal, assert_true, | ||
assert_false, assert_raises, data_path, | ||
suppress_warnings, assert_dt_equal) | ||
from ..tmpdirs import InTemporaryDirectory | ||
|
||
|
||
SOMEONES_ANATOMY = os.path.join(data_path, 'someones_anatomy.nii.gz') | ||
# SOMEONES_ANATOMY = os.path.join(data_path, 'someones_anatomy.nii.gz') | ||
|
||
|
||
@pytest.mark.parametrize('image_orientation', ['RAS', 'LAS', 'LPS', 'oblique']) | ||
def test_affines_save(image_orientation): | ||
"""Check implementation of exporting affines to formats.""" | ||
# Generate test transform | ||
img = loadimg(SOMEONES_ANATOMY) | ||
imgaff = img.affine | ||
|
||
if image_orientation == 'LAS': | ||
newaff = imgaff.copy() | ||
newaff[0, 0] *= -1.0 | ||
newaff[0, 3] = imgaff.dot(np.hstack((np.array(img.shape[:3]) - 1, 1.0)))[0] | ||
img = Nifti1Image(np.flip(img.get_fdata(), 0), newaff, img.header) | ||
elif image_orientation == 'LPS': | ||
newaff = imgaff.copy() | ||
newaff[0, 0] *= -1.0 | ||
newaff[1, 1] *= -1.0 | ||
newaff[:2, 3] = imgaff.dot(np.hstack((np.array(img.shape[:3]) - 1, 1.0)))[:2] | ||
img = Nifti1Image(np.flip(np.flip(img.get_fdata(), 0), 1), newaff, img.header) | ||
elif image_orientation == 'oblique': | ||
A = shape_zoom_affine(img.shape, img.header.get_zooms(), x_flip=False) | ||
R = from_matvec(euler2mat(x=0.09, y=0.001, z=0.001)) | ||
newaff = R.dot(A) | ||
img = Nifti1Image(img.get_fdata(), newaff, img.header) | ||
img.header.set_qform(newaff, 1) | ||
img.header.set_sform(newaff, 1) | ||
|
||
T = from_matvec(euler2mat(x=0.9, y=0.001, z=0.001), [4.0, 2.0, -1.0]) | ||
|
||
xfm = nbl.Affine(T) | ||
xfm.reference = img | ||
|
||
itk = nbl.load(os.path.join(data_path, 'affine-%s-itk.tfm' % image_orientation), | ||
fmt='itk') | ||
fsl = np.loadtxt(os.path.join(data_path, 'affine-%s.fsl' % image_orientation)) | ||
|
||
with InTemporaryDirectory(): | ||
xfm.to_filename('M.tfm', fmt='itk') | ||
xfm.to_filename('M.fsl', fmt='fsl') | ||
|
||
nb_itk = nbl.load('M.tfm', fmt='itk') | ||
nb_fsl = np.loadtxt('M.fsl') | ||
|
||
assert_equal(itk, nb_itk) | ||
assert_almost_equal(fsl, nb_fsl) | ||
|
||
# Create version not aligned to canonical |
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