1
1
"""Read/write AFNI's transforms."""
2
2
from math import pi
3
+ import warnings
3
4
import numpy as np
4
5
from nibabel .affines import obliquity , voxel_sizes
5
6
@@ -18,6 +19,39 @@ def __str__(self):
18
19
param = self .structarr ['parameters' ]
19
20
return '\t ' .join (['%g' % p for p in param [:3 , :].reshape (- 1 )])
20
21
22
+ def to_ras (self , moving = None , reference = None ):
23
+ """Convert to RAS+ coordinate system."""
24
+ pre = LPS .copy ()
25
+ post = LPS .copy ()
26
+
27
+ if moving is None :
28
+ moving = reference
29
+
30
+ if reference is None :
31
+ warnings .warn ('At least, the reference image should be set.' )
32
+ else :
33
+ if _is_oblique (reference .affine ):
34
+ warnings .warn ('Reference affine axes are oblique.' )
35
+ M = reference .affine
36
+ A = shape_zoom_affine (reference .shape ,
37
+ voxel_sizes (M ), x_flip = False , y_flip = False )
38
+ pre = M .dot (np .linalg .inv (A )).dot (LPS )
39
+
40
+ if _is_oblique (moving .affine ):
41
+ warnings .warn ('Moving affine axes are oblique.' )
42
+ M2 = moving .affine
43
+ A2 = shape_zoom_affine (moving .shape ,
44
+ voxel_sizes (M2 ), x_flip = True , y_flip = True )
45
+ post = A2 .dot (np .linalg .inv (M2 ))
46
+
47
+ pre = np .linalg .inv (pre )
48
+ post = np .linalg .inv (post )
49
+
50
+ # swapaxes is necessary, as axis 0 encodes series of transforms
51
+ ras = self .structarr ['parameters' ].copy ()
52
+ parameters = np .swapaxes (post .dot (ras .dot (pre )), 0 , 1 ).T
53
+ return parameters
54
+
21
55
def to_string (self , banner = True ):
22
56
"""Convert to a string directly writeable to file."""
23
57
string = '%s\n ' % self
@@ -33,14 +67,14 @@ def from_ras(cls, ras, moving=None, reference=None):
33
67
pre = LPS .copy ()
34
68
post = LPS .copy ()
35
69
if _is_oblique (reference .affine ):
36
- print ('Reference affine axes are oblique.' )
70
+ warnings . warn ('Reference affine axes are oblique.' )
37
71
M = reference .affine
38
72
A = shape_zoom_affine (reference .shape ,
39
73
voxel_sizes (M ), x_flip = False , y_flip = False )
40
74
pre = M .dot (np .linalg .inv (A )).dot (LPS )
41
75
42
76
if _is_oblique (moving .affine ):
43
- print ('Moving affine axes are oblique.' )
77
+ warnings . warn ('Moving affine axes are oblique.' )
44
78
M2 = moving .affine
45
79
A2 = shape_zoom_affine (moving .shape ,
46
80
voxel_sizes (M2 ), x_flip = True , y_flip = True )
0 commit comments