@@ -156,7 +156,23 @@ def from_matlab_dict(cls, mdict, index=0):
156
156
@classmethod
157
157
def from_h5obj (cls , fileobj , check = True ):
158
158
"""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 ]
160
176
161
177
@classmethod
162
178
def from_ras (cls , ras , index = 0 , moving = None , reference = None ):
@@ -295,26 +311,13 @@ def from_string(cls, string):
295
311
@classmethod
296
312
def from_h5obj (cls , fileobj , check = True ):
297
313
"""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"
304
314
305
315
_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
+ )
318
321
return _self
319
322
320
323
@@ -347,16 +350,16 @@ class ITKCompositeH5:
347
350
"""A data structure for ITK's HDF5 files."""
348
351
349
352
@classmethod
350
- def from_filename (cls , filename ):
353
+ def from_filename (cls , filename , only_linear = False ):
351
354
"""Read the struct from a file given its path."""
352
355
if not str (filename ).endswith (".h5" ):
353
356
raise TransformFileError ("Extension is not .h5" )
354
357
355
358
with H5File (str (filename )) as f :
356
- return cls .from_h5obj (f )
359
+ return cls .from_h5obj (f , only_linear = only_linear )
357
360
358
361
@classmethod
359
- def from_h5obj (cls , fileobj , check = True ):
362
+ def from_h5obj (cls , fileobj , check = True , only_linear = False ):
360
363
"""Read the struct from a file object."""
361
364
xfm_list = []
362
365
h5group = fileobj ["TransformGroup" ]
@@ -379,6 +382,8 @@ def from_h5obj(cls, fileobj, check=True):
379
382
)
380
383
continue
381
384
if xfm ["TransformType" ][0 ].startswith (b"DisplacementFieldTransform" ):
385
+ if only_linear :
386
+ continue
382
387
_fixed = np .asanyarray (xfm [f"{ typo_fallback } FixedParameters" ])
383
388
shape = _fixed [:3 ].astype ("uint16" ).tolist ()
384
389
offset = _fixed [3 :6 ].astype ("float" )
0 commit comments