-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtest_transform.py
136 lines (112 loc) · 4.5 KB
/
test_transform.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# 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 pytest
import numpy as np
from subprocess import check_call
import nibabel as nb
from nibabel.eulerangles import euler2mat
from nibabel.affines import from_matvec
from nibabel.tmpdirs import InTemporaryDirectory
from .. import linear as nbl
from .utils import assert_affines_by_filename
TESTS_BORDER_TOLERANCE = 0.05
APPLY_LINEAR_CMD = {
'fsl': """\
flirt -setbackground 0 -interp nearestneighbour -in {moving} -ref {reference} \
-applyxfm -init {transform} -out resampled.nii.gz\
""".format,
'itk': """\
antsApplyTransforms -d 3 -r {reference} -i {moving} \
-o resampled.nii.gz -n NearestNeighbor -t {transform} --float\
""".format,
'afni': """\
3dAllineate -base {reference} -input {moving} \
-prefix resampled.nii.gz -1Dmatrix_apply {transform} -final NN\
""".format,
}
@pytest.mark.parametrize('image_orientation', [
'RAS', 'LAS', 'LPS', # 'oblique',
])
@pytest.mark.parametrize('sw_tool', ['itk', 'fsl', 'afni'])
def test_linear_load(tmpdir, data_path, get_data, image_orientation, sw_tool):
"""Check implementation of loading affines from formats."""
tmpdir.chdir()
img = get_data[image_orientation]
img.to_filename('img.nii.gz')
# Generate test transform
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
ext = ''
if sw_tool == 'itk':
ext = '.tfm'
fname = 'affine-%s.%s%s' % (image_orientation, sw_tool, ext)
xfm_fname = os.path.join(data_path, fname)
if sw_tool == 'fsl':
with pytest.raises("ValueError"):
loaded = nbl.load(xfm_fname, fmt=fname.split('.')[-1])
with pytest.raises("ValueError"):
loaded = nbl.load(xfm_fname, fmt=fname.split('.')[-1],
reference='img.nii.gz')
with pytest.raises("ValueError"):
loaded = nbl.load(xfm_fname, fmt=fname.split('.')[-1],
moving='img.nii.gz')
loaded = nbl.load(xfm_fname, fmt=fname.split('.')[-1],
moving='img.nii.gz', reference='img.nii.gz')
if sw_tool == 'afni':
with pytest.raises("ValueError"):
loaded = nbl.load(xfm_fname, fmt=fname.split('.')[-1])
loaded = nbl.load(xfm_fname, fmt=fname.split('.')[-1],
reference='img.nii.gz')
assert loaded == xfm
@pytest.mark.parametrize('image_orientation', [
'RAS', 'LAS', 'LPS', # 'oblique',
])
@pytest.mark.parametrize('sw_tool', ['itk', 'fsl', 'afni'])
def test_linear_save(data_path, get_data, image_orientation, sw_tool):
"""Check implementation of exporting affines to formats."""
img = get_data[image_orientation]
# Generate test transform
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
ext = ''
if sw_tool == 'itk':
ext = '.tfm'
with InTemporaryDirectory():
xfm_fname1 = 'M.%s%s' % (sw_tool, ext)
xfm.to_filename(xfm_fname1, fmt=sw_tool)
xfm_fname2 = os.path.join(
data_path, 'affine-%s.%s%s' % (image_orientation, sw_tool, ext))
assert_affines_by_filename(xfm_fname1, xfm_fname2)
@pytest.mark.parametrize('image_orientation', [
'RAS', 'LAS', 'LPS', # 'oblique',
])
@pytest.mark.parametrize('sw_tool', ['itk', 'fsl', 'afni'])
def test_apply_linear_transform(tmpdir, data_path, get_data, image_orientation, sw_tool):
"""Check implementation of exporting affines to formats."""
tmpdir.chdir()
img = get_data[image_orientation]
# Generate test transform
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
ext = ''
if sw_tool == 'itk':
ext = '.tfm'
img.to_filename('img.nii.gz')
xfm_fname = 'M.%s%s' % (sw_tool, ext)
xfm.to_filename(xfm_fname, fmt=sw_tool)
cmd = APPLY_LINEAR_CMD[sw_tool](
transform=os.path.abspath(xfm_fname),
reference=os.path.abspath('img.nii.gz'),
moving=os.path.abspath('img.nii.gz'))
exit_code = check_call([cmd], shell=True)
assert exit_code == 0
sw_moved = nb.load('resampled.nii.gz')
nt_moved = xfm.resample(img, order=0)
diff = sw_moved.get_fdata() - nt_moved.get_fdata()
# A certain tolerance is necessary because of resampling at borders
assert (np.abs(diff) > 1e-3).sum() / diff.size < TESTS_BORDER_TOLERANCE