Skip to content

Commit

Permalink
fix: inefficient iterative reloading of reference and moving images
Browse files Browse the repository at this point in the history
The list comprehension had ``nb.loads`` and other method calls
continously happening when converting the affine array into RAS.
  • Loading branch information
oesteban committed Nov 16, 2023
1 parent 1674e86 commit 755e8d1
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions nitransforms/io/afni.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,24 @@ def from_string(cls, string):
sa["parameters"] = parameters
return tf

def to_ras(self, moving=None, reference=None):
def to_ras(self, moving=None, reference=None, pre_rotation=None, post_rotation=None):
"""Return a nitransforms internal RAS+ matrix."""
# swapaxes is necessary, as axis 0 encodes series of transforms
retval = LPS @ np.swapaxes(self.structarr["parameters"].T, 0, 1) @ LPS
reference = _ensure_image(reference)
if reference is not None and _is_oblique(reference.affine):
retval = retval @ _cardinal_rotation(reference.affine, True)

moving = _ensure_image(moving)
if moving is not None and _is_oblique(moving.affine):
retval = _cardinal_rotation(moving.affine, False) @ retval
if pre_rotation is None and reference is not None:
ref_aff = _ensure_image(reference).affine
pre_rotation = _cardinal_rotation(ref_aff, True) if _is_oblique(ref_aff) else None

if pre_rotation is not None:
retval = retval @ pre_rotation

if post_rotation is None and reference is not None:
mov_aff = _ensure_image(moving).affine
post_rotation = _cardinal_rotation(mov_aff, True) if _is_oblique(mov_aff) else None

if post_rotation is not None:
retval = post_rotation @ retval

return retval

Expand All @@ -130,9 +137,21 @@ class AFNILinearTransformArray(BaseLinearTransformList):

def to_ras(self, moving=None, reference=None):
"""Return a nitransforms' internal RAS matrix."""
return np.stack(
[xfm.to_ras(moving=moving, reference=reference) for xfm in self.xforms]
)

pre_rotation = None
if reference is not None:
ref_aff = _ensure_image(reference).affine
pre_rotation = _cardinal_rotation(ref_aff, True) if _is_oblique(ref_aff) else None

post_rotation = None
if moving is not None:
mov_aff = _ensure_image(moving).affine
post_rotation = _cardinal_rotation(mov_aff, True) if _is_oblique(mov_aff) else None

return np.stack([
xfm.to_ras(pre_rotation=pre_rotation, post_rotation=post_rotation)
for xfm in self.xforms
])

def to_string(self):
"""Convert to a string directly writeable to file."""
Expand Down

0 comments on commit 755e8d1

Please sign in to comment.