@@ -37,13 +37,6 @@ def load_transforms(xfm_paths: list[Path], inverse: list[bool]) -> nt.base.Trans
3737 return chain
3838
3939
40- def load_ants_h5 (filename : Path ) -> nt .TransformChain :
41- """Load ANTs H5 files as a nitransforms TransformChain"""
42- affine , warp , warp_affine = parse_combined_hdf5 (filename )
43- warp_transform = nt .DenseFieldTransform (nb .Nifti1Image (warp , warp_affine ))
44- return nt .TransformChain ([warp_transform , nt .Affine (affine )])
45-
46-
4740FIXED_PARAMS = np .array ([
4841 193.0 , 229.0 , 193.0 , # Size
4942 96.0 , 132.0 , - 78.0 , # Origin
@@ -54,12 +47,26 @@ def load_ants_h5(filename: Path) -> nt.TransformChain:
5447]) # fmt:skip
5548
5649
57- def parse_combined_hdf5 (h5_fn ):
50+ def load_ants_h5 (filename : Path ) -> nt .TransformChain :
51+ """Load ANTs H5 files as a nitransforms TransformChain"""
5852 # Borrowed from https://github.com/feilong/process
5953 # process.resample.parse_combined_hdf5()
60- h = h5py .File (h5_fn )
54+ #
55+ # Changes:
56+ # * Tolerate a missing displacement field
57+ # * Return the original affine without a round-trip
58+ # * Always return a nitransforms TransformChain
59+ #
60+ # This should be upstreamed into nitransforms
61+ h = h5py .File (filename )
6162 xform = ITKCompositeH5 .from_h5obj (h )
62- affine = xform [0 ].to_ras ()
63+
64+ # nt.Affine
65+ transforms = [nt .Affine (xform [0 ].to_ras ())]
66+
67+ if '2' not in h ['TransformGroup' ]:
68+ return transforms [0 ]
69+
6370 transform2 = h ['TransformGroup' ]['2' ]
6471
6572 # Confirm these transformations are applicable
@@ -68,6 +75,7 @@ def parse_combined_hdf5(h5_fn):
6875 for i in h ['TransformGroup' ].keys ():
6976 msg += f'[{ i } ]: { h ["TransformGroup" ][i ]["TransformType" ][:][0 ]} \n '
7077 raise ValueError (msg )
78+
7179 fixed_params = transform2 ['TransformFixedParameters' ][:]
7280 if not np .array_equal (fixed_params , FIXED_PARAMS ):
7381 msg = 'Unexpected fixed parameters\n '
@@ -100,4 +108,5 @@ def parse_combined_hdf5(h5_fn):
100108 ]
101109 ),
102110 )
103- return affine , warp , warp_affine
111+ transforms .insert (0 , nt .DenseFieldTransform (nb .Nifti1Image (warp , warp_affine )))
112+ return nt .TransformChain (transforms )
0 commit comments