Skip to content

Commit

Permalink
RF: Handle no-warp H5 files
Browse files Browse the repository at this point in the history
  • Loading branch information
effigies committed Oct 27, 2023
1 parent 558dac7 commit 0b932e5
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions fmriprep/utils/transforms.py
Expand Up @@ -37,13 +37,6 @@ def load_transforms(xfm_paths: list[Path], inverse: list[bool]) -> nt.base.Trans
return chain


def load_ants_h5(filename: Path) -> nt.TransformChain:
"""Load ANTs H5 files as a nitransforms TransformChain"""
affine, warp, warp_affine = parse_combined_hdf5(filename)
warp_transform = nt.DenseFieldTransform(nb.Nifti1Image(warp, warp_affine))
return nt.TransformChain([warp_transform, nt.Affine(affine)])


FIXED_PARAMS = np.array([
193.0, 229.0, 193.0, # Size
96.0, 132.0, -78.0, # Origin
Expand All @@ -54,12 +47,26 @@ def load_ants_h5(filename: Path) -> nt.TransformChain:
]) # fmt:skip


def parse_combined_hdf5(h5_fn):
def load_ants_h5(filename: Path) -> nt.TransformChain:
"""Load ANTs H5 files as a nitransforms TransformChain"""
# Borrowed from https://github.com/feilong/process
# process.resample.parse_combined_hdf5()
h = h5py.File(h5_fn)
#
# Changes:
# * Tolerate a missing displacement field
# * Return the original affine without a round-trip
# * Always return a nitransforms TransformChain
#
# This should be upstreamed into nitransforms
h = h5py.File(filename)

Check warning on line 61 in fmriprep/utils/transforms.py

View check run for this annotation

Codecov / codecov/patch

fmriprep/utils/transforms.py#L61

Added line #L61 was not covered by tests
xform = ITKCompositeH5.from_h5obj(h)
affine = xform[0].to_ras()

# nt.Affine
transforms = [nt.Affine(xform[0].to_ras())]

Check warning on line 65 in fmriprep/utils/transforms.py

View check run for this annotation

Codecov / codecov/patch

fmriprep/utils/transforms.py#L65

Added line #L65 was not covered by tests

if '2' not in h['TransformGroup']:
return transforms[0]

Check warning on line 68 in fmriprep/utils/transforms.py

View check run for this annotation

Codecov / codecov/patch

fmriprep/utils/transforms.py#L67-L68

Added lines #L67 - L68 were not covered by tests

transform2 = h['TransformGroup']['2']

Check warning on line 70 in fmriprep/utils/transforms.py

View check run for this annotation

Codecov / codecov/patch

fmriprep/utils/transforms.py#L70

Added line #L70 was not covered by tests

# Confirm these transformations are applicable
Expand All @@ -68,6 +75,7 @@ def parse_combined_hdf5(h5_fn):
for i in h['TransformGroup'].keys():
msg += f'[{i}]: {h["TransformGroup"][i]["TransformType"][:][0]}\n'
raise ValueError(msg)

Check warning on line 77 in fmriprep/utils/transforms.py

View check run for this annotation

Codecov / codecov/patch

fmriprep/utils/transforms.py#L73-L77

Added lines #L73 - L77 were not covered by tests

fixed_params = transform2['TransformFixedParameters'][:]
if not np.array_equal(fixed_params, FIXED_PARAMS):
msg = 'Unexpected fixed parameters\n'
Expand Down Expand Up @@ -100,4 +108,5 @@ def parse_combined_hdf5(h5_fn):
]
),
)
return affine, warp, warp_affine
transforms.insert(0, nt.DenseFieldTransform(nb.Nifti1Image(warp, warp_affine)))
return nt.TransformChain(transforms)

Check warning on line 112 in fmriprep/utils/transforms.py

View check run for this annotation

Codecov / codecov/patch

fmriprep/utils/transforms.py#L111-L112

Added lines #L111 - L112 were not covered by tests

0 comments on commit 0b932e5

Please sign in to comment.