Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: Ensure shape matches template during resampling #3296

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 17 additions & 20 deletions fmriprep/utils/transforms.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Utilities for loading transforms for resampling"""

import warnings
from pathlib import Path

import h5py
Expand Down Expand Up @@ -38,16 +37,6 @@
return chain


FIXED_PARAMS = np.array([
193.0, 229.0, 193.0, # Size
96.0, 132.0, -78.0, # Origin
1.0, 1.0, 1.0, # Spacing
mgxd marked this conversation as resolved.
Show resolved Hide resolved
-1.0, 0.0, 0.0, # Directions
0.0, -1.0, 0.0,
0.0, 0.0, 1.0,
]) # fmt:skip


def load_ants_h5(filename: Path) -> nt.base.TransformBase:
"""Load ANTs H5 files as a nitransforms TransformChain"""
# Borrowed from https://github.com/feilong/process
Expand Down Expand Up @@ -81,17 +70,25 @@
raise ValueError(msg)

fixed_params = transform2['TransformFixedParameters'][:]
if not np.array_equal(fixed_params, FIXED_PARAMS):
msg = 'Unexpected fixed parameters\n'
msg += f'Expected: {FIXED_PARAMS}\n'
msg += f'Found: {fixed_params}'
if not np.array_equal(fixed_params[6:], FIXED_PARAMS[6:]):
raise ValueError(msg)
warnings.warn(msg, stacklevel=1)

spacing = fixed_params[6:9]
direction = fixed_params[9:].reshape((3, 3))

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

View check run for this annotation

Codecov / codecov/patch

fmriprep/utils/transforms.py#L74-L75

Added lines #L74 - L75 were not covered by tests

# Supported spacing
if not np.array_equal(spacing, np.array([1.0, 1.0, 1.0])):
raise ValueError(f'Unexpected spacing: {spacing}')

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

View check run for this annotation

Codecov / codecov/patch

fmriprep/utils/transforms.py#L78-L79

Added lines #L78 - L79 were not covered by tests

if not np.array_equal(direction, direction.T):
raise ValueError(f'Asymmetric direction matrix: {direction}')

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

View check run for this annotation

Codecov / codecov/patch

fmriprep/utils/transforms.py#L81-L82

Added lines #L81 - L82 were not covered by tests

shape = tuple(fixed_params[:3].astype(int))
warp = h['TransformGroup']['2']['TransformParameters'][:]
warp = warp.reshape((*shape, 3)).transpose(2, 1, 0, 3)
# ITK stores warps in Fortran-order, where the vector components change fastest
# Nitransforms expects 3 volumes, not a volume of three-vectors, so transpose
warp = np.reshape(

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

View check run for this annotation

Codecov / codecov/patch

fmriprep/utils/transforms.py#L87

Added line #L87 was not covered by tests
transform2['TransformParameters'],
(3, *shape),
order='F',
).transpose(1, 2, 3, 0)
warp *= np.array([-1, -1, 1])

warp_affine = np.eye(4)
Expand Down