Skip to content

Commit

Permalink
remove type hint and update nifti_to_ants to ax-flip assumption not q…
Browse files Browse the repository at this point in the history
…form.
  • Loading branch information
ga84mun committed Aug 15, 2024
1 parent a7190e2 commit 7dcc27f
Showing 1 changed file with 38 additions and 11 deletions.
49 changes: 38 additions & 11 deletions ants/utils/nifti_to_ants.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

from typing import TYPE_CHECKING

import ants
import numpy as np

import ants

if TYPE_CHECKING:
from nibabel.nifti1 import Nifti1Image


def nifti_to_ants(nib_image: "Nifti1Image") -> ants.ANTsImage:
def nifti_to_ants(nib_image: "Nifti1Image"):
"""
Convert a Nifti image to an ANTsPy image.
Expand All @@ -26,26 +27,33 @@ def nifti_to_ants(nib_image: "Nifti1Image") -> ants.ANTsImage:
ndim = nib_image.ndim

if ndim < 3:
raise NotImplementedError("Conversion is only implemented for 3D or higher images.")

raise NotImplementedError(
"Conversion is only implemented for 3D or higher images."
)
q_form = nib_image.get_qform()
spacing = nib_image.header["pixdim"][1 : ndim + 1]

origin = np.zeros(ndim)
origin[:3] = q_form[:3, 3]
origin[:3] = np.dot(np.diag([-1, -1, 1]), q_form[:3, 3])

direction = np.eye(ndim)
direction[:3, :3] = q_form[:3, :3] / spacing[:3]
direction[:3, :3] = np.dot(np.diag([-1, -1, 1]), q_form[:3, :3]) / spacing[:3]

ants_img = ants.from_numpy(data=nib_image.get_fdata(), origin=origin.tolist(), spacing=spacing.tolist(), direction=direction)
ants_img = ants.from_numpy(
data=nib_image.get_fdata(),
origin=origin.tolist(),
spacing=spacing.tolist(),
direction=direction,
)
"add nibabel conversion (lacey import to prevent forced dependency)"

return ants_img


def get_ras_affine_from_ants(ants_img: ants.ANTsImage) -> np.ndarray:
def get_ras_affine_from_ants(ants_img) -> np.ndarray:
"""
Convert ANTs image affine to RAS coordinate system.
Source: https://github.com/fepegar/torchio/blob/main/src/torchio/data/io.py
Parameters
----------
ants_img : ants.ANTsImage
Expand Down Expand Up @@ -86,7 +94,7 @@ def get_ras_affine_from_ants(ants_img: ants.ANTsImage) -> np.ndarray:
return affine


def ants_to_nifti(img: ants.ANTsImage, header=None) -> "Nifti1Image":
def ants_to_nifti(img, header=None) -> "Nifti1Image":
"""
Convert an ANTs image to a Nifti image.
Expand Down Expand Up @@ -125,11 +133,30 @@ def ants_to_nifti(img: ants.ANTsImage, header=None) -> "Nifti1Image":
nii_mni: "Nifti1Image" = nib.load(fn)
ants_mni = to_nibabel(ants_img)
assert (ants_mni.get_qform() == nii_mni.get_qform()).all()
temp = ants.from_nibabel(nii_mni)
assert (ants_mni.affine == nii_mni.affine).all()
temp = from_nibabel(nii_mni)

assert ants.image_physical_space_consistency(ants_img, temp)

fn = ants.get_data("ch2")
ants_mni = ants.image_read(fn)
nii_mni = nib.load(fn)
ants_mni = to_nibabel(ants_mni)
assert (ants_mni.get_qform() == nii_mni.get_qform()).all()

nii_org = nib.load(fn)
ants_org = ants.image_read(fn)
temp = ants_org
for i in range(10):
temp = to_nibabel(ants_org)
assert (temp.get_qform() == nii_org.get_qform()).all()
assert (ants_mni.affine == nii_mni.affine).all()
temp = from_nibabel(temp)
assert ants.image_physical_space_consistency(ants_org, temp)
for i in range(10):
temp = from_nibabel(nii_org)
assert ants.image_physical_space_consistency(ants_org, temp)
temp = to_nibabel(temp)

assert (temp.get_qform() == nii_org.get_qform()).all()
assert (ants_mni.affine == nii_mni.affine).all()

0 comments on commit 7dcc27f

Please sign in to comment.