@@ -37,13 +37,6 @@ def load_transforms(xfm_paths: list[Path], inverse: list[bool]) -> nt.base.Trans
37
37
return chain
38
38
39
39
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
-
47
40
FIXED_PARAMS = np .array ([
48
41
193.0 , 229.0 , 193.0 , # Size
49
42
96.0 , 132.0 , - 78.0 , # Origin
@@ -54,12 +47,26 @@ def load_ants_h5(filename: Path) -> nt.TransformChain:
54
47
]) # fmt:skip
55
48
56
49
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"""
58
52
# Borrowed from https://github.com/feilong/process
59
53
# 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 )
61
62
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
+
63
70
transform2 = h ['TransformGroup' ]['2' ]
64
71
65
72
# Confirm these transformations are applicable
@@ -68,6 +75,7 @@ def parse_combined_hdf5(h5_fn):
68
75
for i in h ['TransformGroup' ].keys ():
69
76
msg += f'[{ i } ]: { h ["TransformGroup" ][i ]["TransformType" ][:][0 ]} \n '
70
77
raise ValueError (msg )
78
+
71
79
fixed_params = transform2 ['TransformFixedParameters' ][:]
72
80
if not np .array_equal (fixed_params , FIXED_PARAMS ):
73
81
msg = 'Unexpected fixed parameters\n '
@@ -100,4 +108,5 @@ def parse_combined_hdf5(h5_fn):
100
108
]
101
109
),
102
110
)
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