Skip to content

Commit

Permalink
DBG: Better reporting for ANTs h5 parsing failures
Browse files Browse the repository at this point in the history
  • Loading branch information
effigies committed Oct 26, 2023
1 parent 38646f6 commit 1c3599a
Showing 1 changed file with 21 additions and 28 deletions.
49 changes: 21 additions & 28 deletions fmriprep/utils/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,41 +43,34 @@ def load_ants_h5(filename: Path) -> nt.TransformChain:
return nt.TransformChain([warp_transform, nt.Affine(affine)])


FIXED_PARAMS = np.array([
193.0, 229.0, 193.0,
96.0, 132.0, -78.0,
1.0, 1.0, 1.0,
-1.0, 0.0, 0.0,
0.0, -1.0, 0.0,
0.0, 0.0, 1.0,
]) # fmt:skip


def parse_combined_hdf5(h5_fn, to_ras=True):
# Borrowed from https://github.com/feilong/process
# process.resample.parse_combined_hdf5()
h = h5py.File(h5_fn)
xform = ITKCompositeH5.from_h5obj(h)
affine = xform[0].to_ras()
transform2 = h['TransformGroup']['2']

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

View check run for this annotation

Codecov / codecov/patch

fmriprep/utils/transforms.py#L62

Added line #L62 was not covered by tests
# Confirm these transformations are applicable
assert (
h['TransformGroup']['2']['TransformType'][:][0] == b'DisplacementFieldTransform_float_3_3'
)
assert np.array_equal(
h['TransformGroup']['2']['TransformFixedParameters'][:],
np.array(
[
193.0,
229.0,
193.0,
96.0,
132.0,
-78.0,
1.0,
1.0,
1.0,
-1.0,
0.0,
0.0,
0.0,
-1.0,
0.0,
0.0,
0.0,
1.0,
]
),
)
if transform2['TransformType'][:][0] != b'DisplacementFieldTransform_float_3_3':
msg = 'Unknown transform type [2]\n'
for i in h['TransformGroup'].keys():
msg += f'[{i}]: {h["TransformGroup"][i]["TransformType"][:][0]}\n'
raise ValueError(msg)
if not np.array_equal(transform2['TransformFixedParameters'], FIXED_PARAMS):
msg = 'Unexpected fixed parameters\n'
msg += f'Expected: {FIXED_PARAMS}\n'
msg += f'Found: {transform2["TransformFixedParameters"][:]}'
raise ValueError(msg)

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

View check run for this annotation

Codecov / codecov/patch

fmriprep/utils/transforms.py#L64-L73

Added lines #L64 - L73 were not covered by tests
warp = h['TransformGroup']['2']['TransformParameters'][:]
warp = warp.reshape((193, 229, 193, 3)).transpose(2, 1, 0, 3)
warp *= np.array([-1, -1, 1])
Expand Down

0 comments on commit 1c3599a

Please sign in to comment.