From 8e7c5679f92cc3866a6d7de26b7819efab021032 Mon Sep 17 00:00:00 2001 From: Chris Markiewicz Date: Sat, 22 Jun 2024 13:21:00 +0900 Subject: [PATCH 1/2] FIX: Load ITK fields from H5 correctly --- nitransforms/io/itk.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/nitransforms/io/itk.py b/nitransforms/io/itk.py index ddeb78e6..b7f59ec7 100644 --- a/nitransforms/io/itk.py +++ b/nitransforms/io/itk.py @@ -403,14 +403,19 @@ 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() @@ -418,7 +423,7 @@ def from_h5obj(cls, fileobj, check=True, only_linear=False): hdr.set_data_dtype("float") xfm_list.append( - Nifti1Image(field.astype("float"), LPS @ affine @ LPS, hdr) + Nifti1Image(field.astype("float"), affine @ LPS, hdr) ) continue From 1c7466934dadc36ed6ce2783de7670142074d81d Mon Sep 17 00:00:00 2001 From: Chris Markiewicz Date: Sat, 22 Jun 2024 19:11:47 -0400 Subject: [PATCH 2/2] Update nitransforms/io/itk.py --- nitransforms/io/itk.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nitransforms/io/itk.py b/nitransforms/io/itk.py index b7f59ec7..afabfd98 100644 --- a/nitransforms/io/itk.py +++ b/nitransforms/io/itk.py @@ -423,7 +423,7 @@ def from_h5obj(cls, fileobj, check=True, only_linear=False): hdr.set_data_dtype("float") xfm_list.append( - Nifti1Image(field.astype("float"), affine @ LPS, hdr) + Nifti1Image(field.astype("float"), LPS @ affine, hdr) ) continue