Skip to content

Commit 2ccc490

Browse files
authored
Merge pull request #211 from effigies/fix/itk-load
FIX: Load ITK fields from H5 correctly
2 parents d5c8a25 + 1c74669 commit 2ccc490

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

nitransforms/io/itk.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -403,22 +403,27 @@ def from_h5obj(cls, fileobj, check=True, only_linear=False):
403403
if xfm["TransformType"][0].startswith(b"DisplacementFieldTransform"):
404404
if only_linear:
405405
continue
406-
_fixed = np.asanyarray(xfm[f"{typo_fallback}FixedParameters"])
407-
shape = _fixed[:3].astype("uint16").tolist()
408-
offset = _fixed[3:6].astype("float")
409-
zooms = _fixed[6:9].astype("float")
410-
directions = _fixed[9:].astype("float").reshape((3, 3))
406+
_fixed = xfm[f"{typo_fallback}FixedParameters"]
407+
shape = _fixed[:3]
408+
offset = _fixed[3:6]
409+
zooms = _fixed[6:9]
410+
directions = np.reshape(_fixed[9:], (3, 3))
411411
affine = from_matvec(directions * zooms, offset)
412-
field = np.asanyarray(xfm[f"{typo_fallback}Parameters"]).reshape(
413-
(*shape, 1, -1)
412+
# ITK uses Fortran ordering, like NIfTI, but with the vector dimension first
413+
field = np.moveaxis(
414+
np.reshape(
415+
xfm[f"{typo_fallback}Parameters"], (3, *shape.astype(int)), order='F'
416+
),
417+
0,
418+
-1,
414419
)
415420
field[..., (0, 1)] *= -1.0
416421
hdr = Nifti1Header()
417422
hdr.set_intent("vector")
418423
hdr.set_data_dtype("float")
419424

420425
xfm_list.append(
421-
Nifti1Image(field.astype("float"), LPS @ affine @ LPS, hdr)
426+
Nifti1Image(field.astype("float"), LPS @ affine, hdr)
422427
)
423428
continue
424429

0 commit comments

Comments
 (0)