From 7dcc27f68405399d4ff3f09b75b0ca1e5b7010d7 Mon Sep 17 00:00:00 2001 From: ga84mun Date: Thu, 15 Aug 2024 13:17:44 +0000 Subject: [PATCH] remove type hint and update nifti_to_ants to ax-flip assumption not qform. --- ants/utils/nifti_to_ants.py | 49 ++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/ants/utils/nifti_to_ants.py b/ants/utils/nifti_to_ants.py index 5bfcdbbe..afc89908 100644 --- a/ants/utils/nifti_to_ants.py +++ b/ants/utils/nifti_to_ants.py @@ -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. @@ -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 @@ -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. @@ -125,7 +133,9 @@ 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") @@ -133,3 +143,20 @@ def ants_to_nifti(img: ants.ANTsImage, header=None) -> "Nifti1Image": 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()