@@ -135,10 +135,10 @@ def to_ras(self, moving=None, reference=None):
135
135
if reference is not None and _is_oblique (ref_aff := _ensure_image (reference ).affine ):
136
136
pre_rotation = _cardinal_rotation (ref_aff , True )
137
137
if moving is not None and _is_oblique (mov_aff := _ensure_image (moving ).affine ):
138
- post_rotation = _cardinal_rotation (mov_aff , True )
138
+ post_rotation = _cardinal_rotation (mov_aff , False )
139
139
140
140
return np .stack ([
141
- post_rotation @ xfm .to_ras () @ pre_rotation
141
+ post_rotation @ ( xfm .to_ras () @ pre_rotation )
142
142
for xfm in self .xforms
143
143
])
144
144
@@ -152,14 +152,22 @@ def to_string(self):
152
152
if line .strip ()
153
153
]
154
154
strings += lines
155
- return "\n " .join (strings )
155
+ return "\n " .join (strings + [ "" ] )
156
156
157
157
@classmethod
158
158
def from_ras (cls , ras , moving = None , reference = None ):
159
159
"""Create an ITK affine from a nitransform's RAS+ matrix."""
160
160
_self = cls ()
161
+
162
+ pre_rotation = post_rotation = np .eye (4 )
163
+
164
+ if reference is not None and _is_oblique (ref_aff := _ensure_image (reference ).affine ):
165
+ pre_rotation = _cardinal_rotation (ref_aff , False )
166
+ if moving is not None and _is_oblique (mov_aff := _ensure_image (moving ).affine ):
167
+ post_rotation = _cardinal_rotation (mov_aff , True )
168
+
161
169
_self .xforms = [
162
- cls ._inner_type .from_ras (ras [i , ...], moving = moving , reference = reference )
170
+ cls ._inner_type .from_ras (post_rotation @ ras [i , ...] @ pre_rotation )
163
171
for i in range (ras .shape [0 ])
164
172
]
165
173
return _self
0 commit comments