@@ -135,10 +135,10 @@ def to_ras(self, moving=None, reference=None):
135135 if reference is not None and _is_oblique (ref_aff := _ensure_image (reference ).affine ):
136136 pre_rotation = _cardinal_rotation (ref_aff , True )
137137 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 )
139139
140140 return np .stack ([
141- post_rotation @ xfm .to_ras () @ pre_rotation
141+ post_rotation @ ( xfm .to_ras () @ pre_rotation )
142142 for xfm in self .xforms
143143 ])
144144
@@ -152,14 +152,22 @@ def to_string(self):
152152 if line .strip ()
153153 ]
154154 strings += lines
155- return "\n " .join (strings )
155+ return "\n " .join (strings + [ "" ] )
156156
157157 @classmethod
158158 def from_ras (cls , ras , moving = None , reference = None ):
159159 """Create an ITK affine from a nitransform's RAS+ matrix."""
160160 _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+
161169 _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 )
163171 for i in range (ras .shape [0 ])
164172 ]
165173 return _self
0 commit comments