Skip to content

Commit 89d032a

Browse files
committed
enh: added test file / reduce code duplicity
1 parent a47bf1c commit 89d032a

File tree

2 files changed

+27
-22
lines changed

2 files changed

+27
-22
lines changed

nitransforms/io/itk.py

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,23 @@ def from_matlab_dict(cls, mdict, index=0):
156156
@classmethod
157157
def from_h5obj(cls, fileobj, check=True):
158158
"""Read the struct from a file object."""
159-
raise NotImplementedError
159+
160+
_xfm = ITKCompositeH5.from_h5obj(
161+
fileobj,
162+
check=check,
163+
only_linear=True,
164+
)
165+
166+
if not _xfm:
167+
raise TransformIOError(
168+
"Composite transform file does not contain at least one linear transform"
169+
)
170+
elif len(_xfm) > 1:
171+
raise TransformIOError(
172+
"Composite transform file contains more than one linear transform"
173+
)
174+
175+
return _xfm[0]
160176

161177
@classmethod
162178
def from_ras(cls, ras, index=0, moving=None, reference=None):
@@ -295,26 +311,13 @@ def from_string(cls, string):
295311
@classmethod
296312
def from_h5obj(cls, fileobj, check=True):
297313
"""Read the struct from a file object."""
298-
h5group = fileobj["TransformGroup"]
299-
typo_fallback = "Transform"
300-
try:
301-
h5group["1"][f"{typo_fallback}Parameters"]
302-
except KeyError:
303-
typo_fallback = "Tranform"
304314

305315
_self = cls()
306-
_self.xforms = []
307-
for xfm in list(h5group.values())[1:]:
308-
if xfm["TransformType"][0].startswith(b"AffineTransform"):
309-
_params = np.asanyarray(xfm[f"{typo_fallback}Parameters"])
310-
_self.xforms.append(
311-
ITKLinearTransform(
312-
parameters=from_matvec(
313-
_params[:-3].reshape(3, 3), _params[-3:]
314-
),
315-
offset=np.asanyarray(xfm[f"{typo_fallback}FixedParameters"]),
316-
)
317-
)
316+
_self.xforms = ITKCompositeH5.from_h5obj(
317+
fileobj,
318+
check=check,
319+
only_linear=True,
320+
)
318321
return _self
319322

320323

@@ -347,16 +350,16 @@ class ITKCompositeH5:
347350
"""A data structure for ITK's HDF5 files."""
348351

349352
@classmethod
350-
def from_filename(cls, filename):
353+
def from_filename(cls, filename, only_linear=False):
351354
"""Read the struct from a file given its path."""
352355
if not str(filename).endswith(".h5"):
353356
raise TransformFileError("Extension is not .h5")
354357

355358
with H5File(str(filename)) as f:
356-
return cls.from_h5obj(f)
359+
return cls.from_h5obj(f, only_linear=only_linear)
357360

358361
@classmethod
359-
def from_h5obj(cls, fileobj, check=True):
362+
def from_h5obj(cls, fileobj, check=True, only_linear=False):
360363
"""Read the struct from a file object."""
361364
xfm_list = []
362365
h5group = fileobj["TransformGroup"]
@@ -379,6 +382,8 @@ def from_h5obj(cls, fileobj, check=True):
379382
)
380383
continue
381384
if xfm["TransformType"][0].startswith(b"DisplacementFieldTransform"):
385+
if only_linear:
386+
continue
382387
_fixed = np.asanyarray(xfm[f"{typo_fallback}FixedParameters"])
383388
shape = _fixed[:3].astype("uint16").tolist()
384389
offset = _fixed[3:6].astype("float")
12.1 KB
Binary file not shown.

0 commit comments

Comments
 (0)