Skip to content

Commit

Permalink
Merge pull request #102 from poldracklab/fix/reading-ANTS-mat-files
Browse files Browse the repository at this point in the history
FIX: Accept double precision when reading ANTs' ``.mat`` affines
  • Loading branch information
oesteban authored Apr 13, 2021
2 parents e728b57 + 9583dd6 commit c4d86eb
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions nitransforms/io/itk.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ITKLinearTransform(LinearParameters):
[
("type", "i4"),
("index", "i4"),
("parameters", "f4", (4, 4)),
("parameters", "f8", (4, 4)),
("offset", "f4", 3), # Center of rotation
]
)
Expand Down Expand Up @@ -67,11 +67,11 @@ def to_filename(self, filename):
np.hstack(
(sa["parameters"][:3, :3].reshape(-1), sa["parameters"][:3, 3])
)[..., np.newaxis],
dtype="f4",
dtype="f8",
)
fixed = np.array(sa["offset"][..., np.newaxis], dtype="f4")
mdict = {
"AffineTransform_float_3_3": affine,
"AffineTransform_double_3_3": affine,
"fixed": fixed,
}
_save_mat(str(filename), mdict, format="4")
Expand Down Expand Up @@ -129,10 +129,18 @@ def from_matlab_dict(cls, mdict, index=0):
tf = cls()
sa = tf.structarr

affine = mdict.get(
"AffineTransform_double_3_3",
mdict.get("AffineTransform_float_3_3")
)

if affine is None:
raise NotImplementedError("Unsupported transform type")

sa["index"] = index
parameters = np.eye(4, dtype="f4")
parameters[:3, :3] = mdict["AffineTransform_float_3_3"][:-3].reshape((3, 3))
parameters[:3, 3] = mdict["AffineTransform_float_3_3"][-3:].flatten()
parameters = np.eye(4, dtype=affine.dtype)
parameters[:3, :3] = affine[:-3].reshape((3, 3))
parameters[:3, 3] = affine[-3:].flatten()
sa["parameters"] = parameters
sa["offset"] = mdict["fixed"].flatten()
return tf
Expand Down

0 comments on commit c4d86eb

Please sign in to comment.