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: Load ITK fields from H5 correctly #211

Merged
merged 2 commits into from
Jul 11, 2024
Merged
Changes from 1 commit
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
21 changes: 13 additions & 8 deletions nitransforms/io/itk.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,22 +403,27 @@ def from_h5obj(cls, fileobj, check=True, only_linear=False):
if xfm["TransformType"][0].startswith(b"DisplacementFieldTransform"):
if only_linear:
continue
_fixed = np.asanyarray(xfm[f"{typo_fallback}FixedParameters"])
shape = _fixed[:3].astype("uint16").tolist()
offset = _fixed[3:6].astype("float")
zooms = _fixed[6:9].astype("float")
directions = _fixed[9:].astype("float").reshape((3, 3))
_fixed = xfm[f"{typo_fallback}FixedParameters"]
shape = _fixed[:3]
offset = _fixed[3:6]
zooms = _fixed[6:9]
directions = np.reshape(_fixed[9:], (3, 3))
affine = from_matvec(directions * zooms, offset)
field = np.asanyarray(xfm[f"{typo_fallback}Parameters"]).reshape(
(*shape, 1, -1)
# ITK uses Fortran ordering, like NIfTI, but with the vector dimension first
field = np.moveaxis(
np.reshape(
xfm[f"{typo_fallback}Parameters"], (3, *shape.astype(int)), order='F'
),
0,
-1,
)
field[..., (0, 1)] *= -1.0
hdr = Nifti1Header()
hdr.set_intent("vector")
hdr.set_data_dtype("float")

xfm_list.append(
Nifti1Image(field.astype("float"), LPS @ affine @ LPS, hdr)
Nifti1Image(field.astype("float"), affine @ LPS, hdr)
effigies marked this conversation as resolved.
Show resolved Hide resolved
)
continue

Expand Down
Loading