9
9
DisplacementsField ,
10
10
LinearParameters ,
11
11
TransformFileError ,
12
+ _ensure_image ,
12
13
)
13
14
14
15
LPS = np .diag ([- 1 , - 1 , 1 , 1 ])
@@ -38,15 +39,22 @@ def from_ras(cls, ras, moving=None, reference=None):
38
39
ras = ras .copy ()
39
40
pre = LPS .copy ()
40
41
post = LPS .copy ()
41
- if _is_oblique (reference .affine ):
42
+
43
+ if reference is not None :
44
+ reference = _ensure_image (reference )
45
+
46
+ if reference is not None and _is_oblique (reference .affine ):
42
47
print ("Reference affine axes are oblique." )
43
48
M = reference .affine
44
49
A = shape_zoom_affine (
45
50
reference .shape , voxel_sizes (M ), x_flip = False , y_flip = False
46
51
)
47
52
pre = M .dot (np .linalg .inv (A )).dot (LPS )
48
53
49
- if _is_oblique (moving .affine ):
54
+ if moving is not None :
55
+ moving = _ensure_image (moving )
56
+
57
+ if moving is not None and _is_oblique (moving .affine ):
50
58
print ("Moving affine axes are oblique." )
51
59
M2 = moving .affine
52
60
A2 = shape_zoom_affine (
@@ -55,7 +63,7 @@ def from_ras(cls, ras, moving=None, reference=None):
55
63
post = A2 .dot (np .linalg .inv (M2 ))
56
64
57
65
# swapaxes is necessary, as axis 0 encodes series of transforms
58
- parameters = np .swapaxes (post . dot ( ras . dot ( pre )) , 0 , 1 )
66
+ parameters = np .swapaxes (post @ ras @ pre , 0 , 1 )
59
67
60
68
tf = cls ()
61
69
tf .structarr ["parameters" ] = parameters .T
@@ -84,6 +92,26 @@ def from_string(cls, string):
84
92
sa ["parameters" ] = parameters
85
93
return tf
86
94
95
+ def to_ras (self , moving = None , reference = None ):
96
+ """Return a nitransforms internal RAS+ matrix."""
97
+ pre = LPS .copy ()
98
+ post = LPS .copy ()
99
+
100
+ if reference is not None :
101
+ reference = _ensure_image (reference )
102
+
103
+ if reference is not None and _is_oblique (reference .affine ):
104
+ raise NotImplementedError
105
+
106
+ if moving is not None :
107
+ moving = _ensure_image (moving )
108
+
109
+ if moving is not None and _is_oblique (moving .affine ):
110
+ raise NotImplementedError
111
+
112
+ # swapaxes is necessary, as axis 0 encodes series of transforms
113
+ return post @ np .swapaxes (self .structarr ["parameters" ].T , 0 , 1 ) @ pre
114
+
87
115
88
116
class AFNILinearTransformArray (BaseLinearTransformList ):
89
117
"""A string-based structure for series of AFNI linear transforms."""
0 commit comments