Skip to content

Commit

Permalink
Merge pull request #430 from mgxd/fix/multi-step-order
Browse files Browse the repository at this point in the history
FIX: Reverse transform order for multi-step
  • Loading branch information
mgxd authored Jan 21, 2025
2 parents 6599e40 + 1e5083a commit e5ed154
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions nibabies/utils/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ def load_transforms(xfm_paths: list[Path], inverse: list[bool]) -> nt.base.Trans
if path.suffix == '.h5':
# Load as a TransformChain
xfm = nt.manip.load(path)
if len(xfm.transforms) == 4:
# MG: This behavior should be ported to nitransforms
xfm = nt.manip.TransformChain(reverse_pairs(xfm.transforms))
else:
xfm = nt.linear.load(path)
if inv:
Expand All @@ -32,3 +35,19 @@ def load_transforms(xfm_paths: list[Path], inverse: list[bool]) -> nt.base.Trans
if chain is None:
chain = nt.Affine() # Identity
return chain


def reverse_pairs(arr: list) -> list:
"""
Reverse the order of pairs in a list.
>>> reverse_pairs([1, 2, 3, 4])
[3, 4, 1, 2]
>>> reverse_pairs([1, 2, 3, 4, 5, 6])
[5, 6, 3, 4, 1, 2]
"""
rev = []
for i in range(len(arr), 0, -2):
rev.extend(arr[i - 2 : i])
return rev

0 comments on commit e5ed154

Please sign in to comment.