Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion TPTBox/core/bids_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@

file_types = [
"nii.gz",
"nii",
"nrrd",
"json",
"mrk.json",
"png",
Expand Down Expand Up @@ -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",
Expand Down
5 changes: 4 additions & 1 deletion TPTBox/core/internal/__init__.py
Original file line number Diff line number Diff line change
@@ -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
10 changes: 9 additions & 1 deletion TPTBox/core/internal/ants_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion TPTBox/core/nii_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 23 additions & 12 deletions TPTBox/core/np_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand All @@ -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(
Expand Down
1 change: 0 additions & 1 deletion TPTBox/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ scikit-learn = "*"
antspyx = "0.4.2"
pynrrd = "*"
#hf-deepali = "*"
requests = "*"

[tool.poetry.group.dev.dependencies]
pytest = ">=8.1.1"
Expand Down