2
2
3
3
from typing import TYPE_CHECKING
4
4
5
- import ants
6
5
import numpy as np
7
6
7
+ import ants
8
+
8
9
if TYPE_CHECKING :
9
10
from nibabel .nifti1 import Nifti1Image
10
11
11
12
12
- def nifti_to_ants (nib_image : "Nifti1Image" ) -> ants . ANTsImage :
13
+ def nifti_to_ants (nib_image : "Nifti1Image" ):
13
14
"""
14
15
Convert a Nifti image to an ANTsPy image.
15
16
@@ -26,26 +27,33 @@ def nifti_to_ants(nib_image: "Nifti1Image") -> ants.ANTsImage:
26
27
ndim = nib_image .ndim
27
28
28
29
if ndim < 3 :
29
- raise NotImplementedError ("Conversion is only implemented for 3D or higher images." )
30
-
30
+ raise NotImplementedError (
31
+ "Conversion is only implemented for 3D or higher images."
32
+ )
31
33
q_form = nib_image .get_qform ()
32
34
spacing = nib_image .header ["pixdim" ][1 : ndim + 1 ]
33
35
34
36
origin = np .zeros (ndim )
35
- origin [:3 ] = q_form [:3 , 3 ]
37
+ origin [:3 ] = np . dot ( np . diag ([ - 1 , - 1 , 1 ]), q_form [:3 , 3 ])
36
38
37
39
direction = np .eye (ndim )
38
- direction [:3 , :3 ] = q_form [:3 , :3 ] / spacing [:3 ]
40
+ direction [:3 , :3 ] = np . dot ( np . diag ([ - 1 , - 1 , 1 ]), q_form [:3 , :3 ]) / spacing [:3 ]
39
41
40
- ants_img = ants .from_numpy (data = nib_image .get_fdata (), origin = origin .tolist (), spacing = spacing .tolist (), direction = direction )
42
+ ants_img = ants .from_numpy (
43
+ data = nib_image .get_fdata (),
44
+ origin = origin .tolist (),
45
+ spacing = spacing .tolist (),
46
+ direction = direction ,
47
+ )
48
+ "add nibabel conversion (lacey import to prevent forced dependency)"
41
49
42
50
return ants_img
43
51
44
52
45
- def get_ras_affine_from_ants (ants_img : ants . ANTsImage ) -> np .ndarray :
53
+ def get_ras_affine_from_ants (ants_img ) -> np .ndarray :
46
54
"""
47
55
Convert ANTs image affine to RAS coordinate system.
48
-
56
+ Source: https://github.com/fepegar/torchio/blob/main/src/torchio/data/io.py
49
57
Parameters
50
58
----------
51
59
ants_img : ants.ANTsImage
@@ -86,7 +94,7 @@ def get_ras_affine_from_ants(ants_img: ants.ANTsImage) -> np.ndarray:
86
94
return affine
87
95
88
96
89
- def ants_to_nifti (img : ants . ANTsImage , header = None ) -> "Nifti1Image" :
97
+ def ants_to_nifti (img , header = None ) -> "Nifti1Image" :
90
98
"""
91
99
Convert an ANTs image to a Nifti image.
92
100
@@ -125,11 +133,30 @@ def ants_to_nifti(img: ants.ANTsImage, header=None) -> "Nifti1Image":
125
133
nii_mni : "Nifti1Image" = nib .load (fn )
126
134
ants_mni = to_nibabel (ants_img )
127
135
assert (ants_mni .get_qform () == nii_mni .get_qform ()).all ()
128
- temp = ants .from_nibabel (nii_mni )
136
+ assert (ants_mni .affine == nii_mni .affine ).all ()
137
+ temp = from_nibabel (nii_mni )
138
+
129
139
assert ants .image_physical_space_consistency (ants_img , temp )
130
140
131
141
fn = ants .get_data ("ch2" )
132
142
ants_mni = ants .image_read (fn )
133
143
nii_mni = nib .load (fn )
134
144
ants_mni = to_nibabel (ants_mni )
135
145
assert (ants_mni .get_qform () == nii_mni .get_qform ()).all ()
146
+
147
+ nii_org = nib .load (fn )
148
+ ants_org = ants .image_read (fn )
149
+ temp = ants_org
150
+ for i in range (10 ):
151
+ temp = to_nibabel (ants_org )
152
+ assert (temp .get_qform () == nii_org .get_qform ()).all ()
153
+ assert (ants_mni .affine == nii_mni .affine ).all ()
154
+ temp = from_nibabel (temp )
155
+ assert ants .image_physical_space_consistency (ants_org , temp )
156
+ for i in range (10 ):
157
+ temp = from_nibabel (nii_org )
158
+ assert ants .image_physical_space_consistency (ants_org , temp )
159
+ temp = to_nibabel (temp )
160
+
161
+ assert (temp .get_qform () == nii_org .get_qform ()).all ()
162
+ assert (ants_mni .affine == nii_mni .affine ).all ()
0 commit comments