Skip to content

Commit ba170d8

Browse files
committed
enh: add parametric test of from/to_ras
1 parent ec8487b commit ba170d8

File tree

6 files changed

+27
-19
lines changed

6 files changed

+27
-19
lines changed

nitransforms/io/afni.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def to_string(self, banner=True):
2727
return string % self
2828

2929
@classmethod
30-
def from_ras(cls, ras, moving, reference):
30+
def from_ras(cls, ras, moving=None, reference=None):
3131
"""Create an ITK affine from a nitransform's RAS+ matrix."""
3232
ras = ras.copy()
3333
pre = LPS.copy()
@@ -77,7 +77,7 @@ class AFNILinearTransformArray(BaseLinearTransformList):
7777

7878
_inner_type = AFNILinearTransform
7979

80-
def to_ras(self, moving, reference):
80+
def to_ras(self, moving=None, reference=None):
8181
"""Return a nitransforms' internal RAS matrix."""
8282
return np.stack([xfm.to_ras(moving=moving, reference=reference)
8383
for xfm in self.xforms])
@@ -90,7 +90,7 @@ def to_string(self):
9090
return '\n'.join(strings)
9191

9292
@classmethod
93-
def from_ras(cls, ras, moving, reference):
93+
def from_ras(cls, ras, moving=None, reference=None):
9494
"""Create an ITK affine from a nitransform's RAS+ matrix."""
9595
_self = cls()
9696
_self.xforms = [cls._inner_type.from_ras(

nitransforms/io/base.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def to_filename(self, filename):
6464
with open(str(filename), 'w') as f:
6565
f.write(self.to_string())
6666

67-
def to_ras(self):
67+
def to_ras(self, moving=None, reference=None):
6868
"""Return a nitransforms internal RAS+ matrix."""
6969
raise NotImplementedError
7070

@@ -126,7 +126,7 @@ def to_filename(self, filename):
126126
with open(str(filename), 'w') as f:
127127
f.write(self.to_string())
128128

129-
def to_ras(self, moving, reference):
129+
def to_ras(self, moving=None, reference=None):
130130
"""Return a nitransforms' internal RAS matrix."""
131131
raise NotImplementedError
132132

@@ -147,7 +147,7 @@ def from_fileobj(cls, fileobj, check=True):
147147
return cls.from_string(fileobj.read())
148148

149149
@classmethod
150-
def from_ras(cls, ras, moving, reference):
150+
def from_ras(cls, ras, moving=None, reference=None):
151151
"""Create an ITK affine from a nitransform's RAS+ matrix."""
152152
raise NotImplementedError
153153

nitransforms/io/fsl.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def to_string(self):
1919
return self.__str__()
2020

2121
@classmethod
22-
def from_ras(cls, ras, moving, reference):
22+
def from_ras(cls, ras, moving=None, reference=None):
2323
"""Create an ITK affine from a nitransform's RAS+ matrix."""
2424
# Adjust for reference image offset and orientation
2525
refswp, refspc = _fsl_aff_adapt(reference)
@@ -64,7 +64,7 @@ def to_filename(self, filename):
6464
with open('%s.%03d' % (filename, i), 'w') as f:
6565
f.write(xfm.to_string())
6666

67-
def to_ras(self, moving, reference):
67+
def to_ras(self, moving=None, reference=None):
6868
"""Return a nitransforms' internal RAS matrix."""
6969
return np.stack([xfm.to_ras(moving=moving, reference=reference)
7070
for xfm in self.xforms])
@@ -79,7 +79,7 @@ def from_fileobj(cls, fileobj, check=True):
7979
return cls.from_string(fileobj.read())
8080

8181
@classmethod
82-
def from_ras(cls, ras, moving, reference):
82+
def from_ras(cls, ras, moving=None, reference=None):
8383
"""Create an ITK affine from a nitransform's RAS+ matrix."""
8484
_self = cls()
8585
_self.xforms = [cls._inner_type.from_ras(

nitransforms/io/itk.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def to_filename(self, filename):
6060
with open(str(filename), 'w') as f:
6161
f.write(self.to_string())
6262

63-
def to_ras(self):
63+
def to_ras(self, moving=None, reference=None):
6464
"""Return a nitransforms internal RAS+ matrix."""
6565
sa = self.structarr
6666
matrix = sa['parameters']
@@ -108,7 +108,7 @@ def from_matlab_dict(cls, mdict, index=0):
108108
return tf
109109

110110
@classmethod
111-
def from_ras(cls, ras, index=0):
111+
def from_ras(cls, ras, index=0, moving=None, reference=None):
112112
"""Create an ITK affine from a nitransform's RAS+ matrix."""
113113
tf = cls()
114114
sa = tf.structarr
@@ -163,7 +163,7 @@ def to_filename(self, filename):
163163
with open(str(filename), 'w') as f:
164164
f.write(self.to_string())
165165

166-
def to_ras(self):
166+
def to_ras(self, moving=None, reference=None):
167167
"""Return a nitransforms' internal RAS matrix."""
168168
return np.stack([xfm.to_ras() for xfm in self.xforms])
169169

@@ -199,10 +199,10 @@ def from_fileobj(cls, fileobj, check=True):
199199
return cls.from_string(fileobj.read())
200200

201201
@classmethod
202-
def from_ras(cls, ras):
202+
def from_ras(cls, ras, moving=None, reference=None):
203203
"""Create an ITK affine from a nitransform's RAS+ matrix."""
204204
_self = cls()
205-
_self.xforms = [cls._inner_type.from_ras(ras[i, ...], i)
205+
_self.xforms = [cls._inner_type.from_ras(ras[i, ...], index=i)
206206
for i in range(ras.shape[0])]
207207
return _self
208208

nitransforms/io/lta.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ def set_type(self, new_type):
159159
)
160160
)
161161

162-
def to_ras(self):
162+
def to_ras(self, moving=None, reference=None):
163163
"""Return a nitransforms internal RAS+ matrix."""
164164
self.set_type(1)
165165
return self.structarr['m_L']

nitransforms/tests/test_io.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,20 +94,26 @@ def test_LT_conversions(data_path):
9494
assert np.allclose(r2r_m, v2v_m, atol=1e-05)
9595

9696

97-
@pytest.mark.xfail(raises=FileNotFoundError)
97+
@pytest.mark.xfail(raises=(FileNotFoundError, NotImplementedError))
9898
@pytest.mark.parametrize('image_orientation', [
9999
'RAS', 'LAS', 'LPS', 'oblique',
100100
])
101101
@pytest.mark.parametrize('sw', ['afni', 'fsl', 'fs', 'itk'])
102-
def test_Linear_common(tmpdir, data_path, sw, image_orientation):
102+
def test_Linear_common(tmpdir, data_path, sw, image_orientation,
103+
get_testdata):
103104
tmpdir.chdir()
104105

106+
moving = get_testdata[image_orientation]
107+
reference = get_testdata[image_orientation]
108+
105109
ext = ''
106110
if sw == 'afni':
107111
factory = afni.AFNILinearTransform
108112
elif sw == 'fsl':
109113
factory = fsl.FSLLinearTransform
110114
elif sw == 'itk':
115+
reference = None
116+
moving = None
111117
ext = '.tfm'
112118
factory = itk.ITKLinearTransform
113119
elif sw == 'fs':
@@ -125,9 +131,10 @@ def test_Linear_common(tmpdir, data_path, sw, image_orientation):
125131
xfm.to_filename(fname)
126132
assert filecmp.cmp(fname, str((data_path / fname).resolve()))
127133

128-
# Generate test transform
129-
# T = from_matvec(euler2mat(x=0.9, y=0.001, z=0.001), [4.0, 2.0, -1.0])
130134
# Test from_ras
135+
RAS = from_matvec(euler2mat(x=0.9, y=0.001, z=0.001), [4.0, 2.0, -1.0])
136+
xfm = factory.from_ras(RAS, reference=reference, moving=moving)
137+
assert np.allclose(xfm.to_ras(reference=reference, moving=moving), RAS)
131138

132139

133140
def test_ITKLinearTransform(tmpdir, data_path):
@@ -156,6 +163,7 @@ def test_ITKLinearTransform(tmpdir, data_path):
156163
rasmat = from_matvec(euler2mat(x=0.9, y=0.001, z=0.001), [4.0, 2.0, -1.0])
157164
itkxfm = itk.ITKLinearTransform.from_ras(rasmat)
158165
assert np.allclose(itkxfm['parameters'], ITK_MAT * rasmat)
166+
assert np.allclose(itkxfm.to_ras(), rasmat)
159167

160168

161169
def test_ITKLinearTransformArray(tmpdir, data_path):

0 commit comments

Comments
 (0)