diff --git a/TPTBox/core/bids_constants.py b/TPTBox/core/bids_constants.py index bbeb4ea..2e0d51b 100755 --- a/TPTBox/core/bids_constants.py +++ b/TPTBox/core/bids_constants.py @@ -265,6 +265,8 @@ file_types = [ "nii.gz", + "nii", + "nrrd", "json", "mrk.json", "png", @@ -334,7 +336,7 @@ # Single class segmentation "Label": "label", # Others (never used) - "Split": "split", + "Split": "split", # Never use, legacy. Is applied inconsistently by other datasets. "Density": "den", "version": "version", "Description": "desc", diff --git a/TPTBox/core/internal/__init__.py b/TPTBox/core/internal/__init__.py index 80dab08..d41c789 100644 --- a/TPTBox/core/internal/__init__.py +++ b/TPTBox/core/internal/__init__.py @@ -1,3 +1,6 @@ from __future__ import annotations -from .ants_load import ants_to_nifti, nifti_to_ants +try: + from .ants_load import ants_to_nifti, nifti_to_ants +except ModuleNotFoundError: + pass diff --git a/TPTBox/core/internal/ants_load.py b/TPTBox/core/internal/ants_load.py index 26e1b1e..86b3a06 100644 --- a/TPTBox/core/internal/ants_load.py +++ b/TPTBox/core/internal/ants_load.py @@ -9,7 +9,7 @@ from nibabel.nifti1 import Nifti1Image -def nifti_to_ants(nib_image: Nifti1Image): +def nifti_to_ants(nib_image: Nifti1Image, **args): """ Convert a Nifti image to an ANTsPy image. @@ -23,6 +23,10 @@ def nifti_to_ants(nib_image: Nifti1Image): ants_image : ants.ANTsImage The converted ANTs image. """ + try: + return ants.utils.from_nibabel_nifti(nib_image, **args) + except Exception: + pass ndim = nib_image.ndim if ndim < 3: @@ -107,6 +111,10 @@ def ants_to_nifti(img, header=None) -> Nifti1Image: img : Nifti1Image The converted Nifti image. """ + try: + return ants.utils.to_nibabel_nifti(img, header=header) + except Exception: + pass from nibabel.nifti1 import Nifti1Image affine = get_ras_affine_from_ants(img) diff --git a/TPTBox/core/nii_wrapper.py b/TPTBox/core/nii_wrapper.py index 053ee5a..ee563a8 100755 --- a/TPTBox/core/nii_wrapper.py +++ b/TPTBox/core/nii_wrapper.py @@ -954,7 +954,10 @@ def n4_bias_field_correction( assert self.seg is False, "n4 bias field correction on a segmentation does not make any sense" # install antspyx not ants! import ants - import ants.utils.bias_correction as bc # install antspyx not ants! + try: + import ants.ops.bias_correction as bc # install antspyx not ants! + except ModuleNotFoundError: + import ants.utils.bias_correction as bc # install antspyx not ants! from ants.utils.convert_nibabel import from_nibabel from scipy.ndimage import binary_dilation, generate_binary_structure dtype = self.dtype diff --git a/TPTBox/core/np_utils.py b/TPTBox/core/np_utils.py index 6997aa0..900b054 100755 --- a/TPTBox/core/np_utils.py +++ b/TPTBox/core/np_utils.py @@ -1185,7 +1185,16 @@ def np_calc_boundary_mask( infect_list = [] def infect(x, y, z): - if any([x < 0, y < 0, z < 0, x == boundary.shape[0], y == boundary.shape[1], z == boundary.shape[2]]): + if any( + [ + x < 0, + y < 0, + z < 0, + x == boundary.shape[0], + y == boundary.shape[1], + z == boundary.shape[2], + ] + ): return if boundary[x, y, z] == 0: boundary[x, y, z] = 1 @@ -1328,7 +1337,6 @@ def np_fill_holes_global_with_majority_voting(arr: UINTARRAY, connectivity: int cc_msk, label_mask=arr_c, dilate_pixel=1, - label_ref=1, inplace=False, ) arr_c[seg_nii_new != 0] = seg_nii_new[seg_nii_new != 0] @@ -1355,26 +1363,29 @@ def np_map_labels_based_on_majority_label_mask_overlap( Returns: arr: input array with all labels in labels relabeled """ - arr_c = arr if inplace else arr.copy() + arr_cc = arr if inplace else arr.copy() labels = _to_labels(arr, label_ref) - label_list: list[int] = [l for l in np_unique(arr) if l in labels] - for l in label_list: - arr_l = np_extract_label(arr, l, inplace=False) + label_list: list[int] = [label for label in np_unique(arr) if label in labels] + + for label in label_list: + arr_l = np_extract_label(arr, label, inplace=False) arr_ld = np_dilate_msk(arr_l.copy(), n_pixel=dilate_pixel, label_ref=1, connectivity=3) if dilate_pixel > 0 else arr_l + # crop speed up by factor 6 + crop = np_bbox_binary(arr_ld, px_dist=0, raise_error=False) - mult = label_mask * arr_ld + mult = label_mask[crop] * arr_ld[crop] label_ref, count = np.unique(mult, return_counts=True) if 0 in label_ref: label_ref = label_ref[1:] count = count[1:] try: - newlabel = label_ref[np.argmax(count)] - except ValueError: - newlabel = no_match_label - arr_c[arr_l != 0] = newlabel - return arr_c + new_label = label_ref[np.argmax(count)] + except ValueError: # should never happen if called from np_fill_holes_global_with_majority_voting + new_label = no_match_label + arr_cc[arr_l != 0] = new_label + return arr_cc def _pad_to_parameters( diff --git a/TPTBox/tests/test_utils.py b/TPTBox/tests/test_utils.py index 2b64ad8..40e096d 100644 --- a/TPTBox/tests/test_utils.py +++ b/TPTBox/tests/test_utils.py @@ -14,7 +14,6 @@ import numpy as np # noqa: E402 import TPTBox.core.bids_files as bids # noqa: E402 -from TPTBox import Centroids # noqa: E402 from TPTBox.core.nii_wrapper import NII # noqa: E402 from TPTBox.core.poi import POI # noqa: E402 from TPTBox.core.vert_constants import AX_CODES # noqa: E402 diff --git a/pyproject.toml b/pyproject.toml index d01af0a..6dcf02a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -31,6 +31,7 @@ scikit-learn = "*" antspyx = "0.4.2" pynrrd = "*" #hf-deepali = "*" +requests = "*" [tool.poetry.group.dev.dependencies] pytest = ">=8.1.1"