Skip to content

Commit 7dcc27f

Browse files
author
ga84mun
committed
remove type hint and update nifti_to_ants to ax-flip assumption not qform.
1 parent a7190e2 commit 7dcc27f

File tree

1 file changed

+38
-11
lines changed

1 file changed

+38
-11
lines changed

ants/utils/nifti_to_ants.py

+38-11
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
from typing import TYPE_CHECKING
44

5-
import ants
65
import numpy as np
76

7+
import ants
8+
89
if TYPE_CHECKING:
910
from nibabel.nifti1 import Nifti1Image
1011

1112

12-
def nifti_to_ants(nib_image: "Nifti1Image") -> ants.ANTsImage:
13+
def nifti_to_ants(nib_image: "Nifti1Image"):
1314
"""
1415
Convert a Nifti image to an ANTsPy image.
1516
@@ -26,26 +27,33 @@ def nifti_to_ants(nib_image: "Nifti1Image") -> ants.ANTsImage:
2627
ndim = nib_image.ndim
2728

2829
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+
)
3133
q_form = nib_image.get_qform()
3234
spacing = nib_image.header["pixdim"][1 : ndim + 1]
3335

3436
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])
3638

3739
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]
3941

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)"
4149

4250
return ants_img
4351

4452

45-
def get_ras_affine_from_ants(ants_img: ants.ANTsImage) -> np.ndarray:
53+
def get_ras_affine_from_ants(ants_img) -> np.ndarray:
4654
"""
4755
Convert ANTs image affine to RAS coordinate system.
48-
56+
Source: https://github.com/fepegar/torchio/blob/main/src/torchio/data/io.py
4957
Parameters
5058
----------
5159
ants_img : ants.ANTsImage
@@ -86,7 +94,7 @@ def get_ras_affine_from_ants(ants_img: ants.ANTsImage) -> np.ndarray:
8694
return affine
8795

8896

89-
def ants_to_nifti(img: ants.ANTsImage, header=None) -> "Nifti1Image":
97+
def ants_to_nifti(img, header=None) -> "Nifti1Image":
9098
"""
9199
Convert an ANTs image to a Nifti image.
92100
@@ -125,11 +133,30 @@ def ants_to_nifti(img: ants.ANTsImage, header=None) -> "Nifti1Image":
125133
nii_mni: "Nifti1Image" = nib.load(fn)
126134
ants_mni = to_nibabel(ants_img)
127135
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+
129139
assert ants.image_physical_space_consistency(ants_img, temp)
130140

131141
fn = ants.get_data("ch2")
132142
ants_mni = ants.image_read(fn)
133143
nii_mni = nib.load(fn)
134144
ants_mni = to_nibabel(ants_mni)
135145
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

Comments
 (0)