Skip to content

Commit be2e950

Browse files
authored
Merge pull request #98 from Hendrik-code/development_robert
bug fixes
2 parents 5ac0bf5 + c1a7a04 commit be2e950

7 files changed

Lines changed: 44 additions & 17 deletions

File tree

TPTBox/core/bids_constants.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,8 @@
265265

266266
file_types = [
267267
"nii.gz",
268+
"nii",
269+
"nrrd",
268270
"json",
269271
"mrk.json",
270272
"png",
@@ -334,7 +336,7 @@
334336
# Single class segmentation
335337
"Label": "label",
336338
# Others (never used)
337-
"Split": "split",
339+
"Split": "split", # Never use, legacy. Is applied inconsistently by other datasets.
338340
"Density": "den",
339341
"version": "version",
340342
"Description": "desc",

TPTBox/core/internal/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
from __future__ import annotations
22

3-
from .ants_load import ants_to_nifti, nifti_to_ants
3+
try:
4+
from .ants_load import ants_to_nifti, nifti_to_ants
5+
except ModuleNotFoundError:
6+
pass

TPTBox/core/internal/ants_load.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from nibabel.nifti1 import Nifti1Image
1010

1111

12-
def nifti_to_ants(nib_image: Nifti1Image):
12+
def nifti_to_ants(nib_image: Nifti1Image, **args):
1313
"""
1414
Convert a Nifti image to an ANTsPy image.
1515
@@ -23,6 +23,10 @@ def nifti_to_ants(nib_image: Nifti1Image):
2323
ants_image : ants.ANTsImage
2424
The converted ANTs image.
2525
"""
26+
try:
27+
return ants.utils.from_nibabel_nifti(nib_image, **args)
28+
except Exception:
29+
pass
2630
ndim = nib_image.ndim
2731

2832
if ndim < 3:
@@ -107,6 +111,10 @@ def ants_to_nifti(img, header=None) -> Nifti1Image:
107111
img : Nifti1Image
108112
The converted Nifti image.
109113
"""
114+
try:
115+
return ants.utils.to_nibabel_nifti(img, header=header)
116+
except Exception:
117+
pass
110118
from nibabel.nifti1 import Nifti1Image
111119

112120
affine = get_ras_affine_from_ants(img)

TPTBox/core/nii_wrapper.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,10 @@ def n4_bias_field_correction(
954954
assert self.seg is False, "n4 bias field correction on a segmentation does not make any sense"
955955
# install antspyx not ants!
956956
import ants
957-
import ants.utils.bias_correction as bc # install antspyx not ants!
957+
try:
958+
import ants.ops.bias_correction as bc # install antspyx not ants!
959+
except ModuleNotFoundError:
960+
import ants.utils.bias_correction as bc # install antspyx not ants!
958961
from ants.utils.convert_nibabel import from_nibabel
959962
from scipy.ndimage import binary_dilation, generate_binary_structure
960963
dtype = self.dtype

TPTBox/core/np_utils.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,7 +1185,16 @@ def np_calc_boundary_mask(
11851185
infect_list = []
11861186

11871187
def infect(x, y, z):
1188-
if any([x < 0, y < 0, z < 0, x == boundary.shape[0], y == boundary.shape[1], z == boundary.shape[2]]):
1188+
if any(
1189+
[
1190+
x < 0,
1191+
y < 0,
1192+
z < 0,
1193+
x == boundary.shape[0],
1194+
y == boundary.shape[1],
1195+
z == boundary.shape[2],
1196+
]
1197+
):
11891198
return
11901199
if boundary[x, y, z] == 0:
11911200
boundary[x, y, z] = 1
@@ -1328,7 +1337,6 @@ def np_fill_holes_global_with_majority_voting(arr: UINTARRAY, connectivity: int
13281337
cc_msk,
13291338
label_mask=arr_c,
13301339
dilate_pixel=1,
1331-
label_ref=1,
13321340
inplace=False,
13331341
)
13341342
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(
13551363
Returns:
13561364
arr: input array with all labels in labels relabeled
13571365
"""
1358-
arr_c = arr if inplace else arr.copy()
1366+
arr_cc = arr if inplace else arr.copy()
13591367

13601368
labels = _to_labels(arr, label_ref)
13611369

1362-
label_list: list[int] = [l for l in np_unique(arr) if l in labels]
1363-
for l in label_list:
1364-
arr_l = np_extract_label(arr, l, inplace=False)
1370+
label_list: list[int] = [label for label in np_unique(arr) if label in labels]
1371+
1372+
for label in label_list:
1373+
arr_l = np_extract_label(arr, label, inplace=False)
13651374
arr_ld = np_dilate_msk(arr_l.copy(), n_pixel=dilate_pixel, label_ref=1, connectivity=3) if dilate_pixel > 0 else arr_l
1375+
# crop speed up by factor 6
1376+
crop = np_bbox_binary(arr_ld, px_dist=0, raise_error=False)
13661377

1367-
mult = label_mask * arr_ld
1378+
mult = label_mask[crop] * arr_ld[crop]
13681379
label_ref, count = np.unique(mult, return_counts=True)
13691380
if 0 in label_ref:
13701381
label_ref = label_ref[1:]
13711382
count = count[1:]
13721383
try:
1373-
newlabel = label_ref[np.argmax(count)]
1374-
except ValueError:
1375-
newlabel = no_match_label
1376-
arr_c[arr_l != 0] = newlabel
1377-
return arr_c
1384+
new_label = label_ref[np.argmax(count)]
1385+
except ValueError: # should never happen if called from np_fill_holes_global_with_majority_voting
1386+
new_label = no_match_label
1387+
arr_cc[arr_l != 0] = new_label
1388+
return arr_cc
13781389

13791390

13801391
def _pad_to_parameters(

TPTBox/tests/test_utils.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import numpy as np # noqa: E402
1515

1616
import TPTBox.core.bids_files as bids # noqa: E402
17-
from TPTBox import Centroids # noqa: E402
1817
from TPTBox.core.nii_wrapper import NII # noqa: E402
1918
from TPTBox.core.poi import POI # noqa: E402
2019
from TPTBox.core.vert_constants import AX_CODES # noqa: E402

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ scikit-learn = "*"
3131
antspyx = "0.4.2"
3232
pynrrd = "*"
3333
#hf-deepali = "*"
34+
requests = "*"
3435

3536
[tool.poetry.group.dev.dependencies]
3637
pytest = ">=8.1.1"

0 commit comments

Comments
 (0)