Skip to content

Commit 62a83aa

Browse files
committed
enh: one more test of the itk transforms io
1 parent ba170d8 commit 62a83aa

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

nitransforms/io/itk.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,16 @@ def from_binary(cls, byte_stream, index=0):
8686
mdict = _read_mat(byte_stream)
8787
return cls.from_matlab_dict(mdict, index=index)
8888

89+
@classmethod
90+
def from_filename(cls, filename):
91+
"""Read the struct from a file given its path."""
92+
if str(filename).endswith('.mat'):
93+
with open(str(filename), 'rb') as fileobj:
94+
return cls.from_binary(fileobj)
95+
96+
with open(str(filename)) as fileobj:
97+
return cls.from_string(fileobj.read())
98+
8999
@classmethod
90100
def from_fileobj(cls, fileobj, check=True):
91101
"""Read the struct from a file object."""
@@ -184,7 +194,7 @@ def from_binary(cls, byte_stream):
184194
def from_filename(cls, filename):
185195
"""Read the struct from a file given its path."""
186196
if str(filename).endswith('.mat'):
187-
with open(str(filename), 'b') as f:
197+
with open(str(filename), 'rb') as f:
188198
return cls.from_binary(f)
189199

190200
with open(str(filename)) as f:

nitransforms/tests/test_io.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,19 @@ def test_Linear_common(tmpdir, data_path, sw, image_orientation,
140140
def test_ITKLinearTransform(tmpdir, data_path):
141141
tmpdir.chdir()
142142

143-
matlabfile = str(data_path / 'ds-005_sub-01_from-T1_to-OASIS_affine.mat')
144-
mat = loadmat(matlabfile)
145-
with open(matlabfile, 'rb') as f:
143+
matlabfile = data_path / 'ds-005_sub-01_from-T1_to-OASIS_affine.mat'
144+
mat = loadmat(str(matlabfile))
145+
with open(str(matlabfile), 'rb') as f:
146146
itkxfm = itk.ITKLinearTransform.from_fileobj(f)
147147
assert np.allclose(itkxfm['parameters'][:3, :3].flatten(),
148148
mat['AffineTransform_float_3_3'][:-3].flatten())
149149
assert np.allclose(itkxfm['offset'], mat['fixed'].reshape((3, )))
150150

151+
itkxfm = itk.ITKLinearTransform.from_filename(matlabfile)
152+
assert np.allclose(itkxfm['parameters'][:3, :3].flatten(),
153+
mat['AffineTransform_float_3_3'][:-3].flatten())
154+
assert np.allclose(itkxfm['offset'], mat['fixed'].reshape((3, )))
155+
151156
# Test to_filename(textfiles)
152157
itkxfm.to_filename('textfile.tfm')
153158
with open('textfile.tfm', 'r') as f:
@@ -174,6 +179,16 @@ def test_ITKLinearTransformArray(tmpdir, data_path):
174179
f.seek(0)
175180
itklist = itk.ITKLinearTransformArray.from_fileobj(f)
176181

182+
itklistb = itk.ITKLinearTransformArray.from_filename(
183+
data_path / 'itktflist.tfm')
184+
assert itklist['nxforms'] == itklistb['nxforms']
185+
assert all([np.allclose(x1['parameters'], x2['parameters'])
186+
for x1, x2 in zip(itklist.xforms, itklistb.xforms)])
187+
188+
tmpdir.join('empty.mat').write('')
189+
with pytest.raises(TransformFileError):
190+
itklistb.from_filename('empty.mat')
191+
177192
assert itklist['nxforms'] == 9
178193
assert text == itklist.to_string()
179194
with pytest.raises(ValueError):

0 commit comments

Comments
 (0)