Skip to content
2 changes: 1 addition & 1 deletion TPTBox/core/nii_poi_abstract.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import annotations

import sys
import warnings
from dataclasses import dataclass
from typing import TYPE_CHECKING, Any

Expand Down Expand Up @@ -495,6 +494,7 @@ def local_to_global(self, x: COORDINATE) -> tuple:
Returns:
tuple: World-space coordinate rounded to 7 decimal places.
"""
# TODO ITK version
a = self.rotation @ (np.array(x) * np.array(self.zoom)) + self.origin
return tuple(round(float(v), 7) for v in a)

Expand Down
6 changes: 2 additions & 4 deletions TPTBox/core/nii_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -1202,8 +1202,7 @@ def resample_from_to(self, to_vox_map:Image_Reference|Has_Grid|tuple[SHAPE,AFFIN
pad = tuple((int(b), int(a)) for b, a in zip(pad_before, pad_after))
ret = s.apply_pad(pad, mode=mode,inplace=inplace,verbose=verbose)

#TODO SET raise_error=False before committing
valid = ret.assert_affine(mapping,raise_error=True,origin_tolerance=0.0001,error_tolerance=0.0001,shape_tolerance=0)
valid = ret.assert_affine(mapping,raise_error=False,origin_tolerance=0.0001,error_tolerance=0.0001,shape_tolerance=0)
if valid:
log.print(f"resample_from_to only needs padding/cropping {pad}",verbose=verbose)
ret.affine = mapping.affine #remove floating point error
Expand Down Expand Up @@ -2446,7 +2445,7 @@ def to_stls(

def to_stl(
self: NII,
label: int,
label: int|Enum|Sequence[int]|Sequence[Enum],
out_path: Path | dict[int, Path] | None = None,
bb: tuple | None = None,
to_world: bool = True,
Expand Down Expand Up @@ -2499,7 +2498,6 @@ def to_stl(
not support per-vertex attributes such as scalar values from marching cubes.
"""
from stl import mesh

seg = self.extract_label(label)
# Prepare binary mask
seg_arr = np.pad(seg.clamp(0, 1).get_array(), 1)
Expand Down
4 changes: 4 additions & 0 deletions TPTBox/core/poi_fun/poi_abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -1030,12 +1030,16 @@ def join_left(self, pois: Self, inplace=False, _right_join=False) -> Self:
Self: The combined set of centroids, either in-place or as a new set, depending on the 'inplace' parameter.
"""
ctd_list = self.centroids
if "label_name" in pois.info and "label_name" not in self.info:
self.info["label_name"] = {}
if not inplace:
ctd_list = ctd_list.copy()
for x, y, c in pois.items():
if (x, y) in self and not _right_join:
continue
ctd_list[x, y] = c
if "label_name" in pois.info and f"({x}, {y})" in pois.info["label_name"]:
self.info["label_name"][f"({x}, {y})"] = pois.info["label_name"][f"({x}, {y})"]
if inplace:
return self
return self.copy(ctd_list)
Expand Down
8 changes: 6 additions & 2 deletions TPTBox/core/poi_fun/save_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,12 @@ def convert(o):
return str(o.absolute())
raise TypeError(type(o))

with open(out_path, "w") as f:
json.dump(json_object, f, default=convert, indent=4)
try:
with open(out_path, "w") as f:
json.dump(json_object, f, default=convert, indent=4)
except TypeError:
Path(out_path).unlink(missing_ok=True)
raise
log.print("POIs saved:", out_path, print_add, ltype=Log_Type.SAVE, verbose=verbose)


Expand Down
7 changes: 7 additions & 0 deletions TPTBox/core/vert_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -1223,8 +1223,15 @@ def save_as_name(cls) -> bool:
Rib_Left = 63
Rib_Right = 64
# 66-80 Free
# Sacrum Subregions
Sacrum_Sacral_Ala_Left = 70
Sacrum_Sacral_Ala_Right = 71
Sacrum_Posterior_Sacral_Elements = 72
Sacrum_Body = 73
Sacrum_Endplate = 74
# Muscle inserts
# https://www.frontiersin.org/articles/10.3389/fbioe.2022.862804/full
Metal = 80
# 81-91
Muscle_Inserts_Spinosus_Process = 81
Muscle_Inserts_Transverse_Process_Left = 83
Expand Down
2 changes: 1 addition & 1 deletion TPTBox/registration/_deformable/multilabel_segmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ def transform_nii(self, nii_atlas: NII, allow_only_same_grid_as_moving: bool = T
nii_reg = self.reg_deform.transform_nii(nii_atlas)
if nii_reg.seg:
nii_reg.set_dtype_("smallest_uint")
out = nii_reg.resample_from_to(self.target_grid_org)
out = nii_reg.resample_from_to(self.target_grid_org, mode="constant")
if self.same_side:
return out
axis = out.get_axis("R")
Expand Down
12 changes: 12 additions & 0 deletions unit_tests/test_testsamples.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ def test_POIs_CT(self):
Location.Vertebral_Body_Endplate_Inferior,
Location.Rib_Left,
Location.Rib_Right,
Location.Sacrum_Sacral_Ala_Left,
Location.Sacrum_Sacral_Ala_Right,
Location.Sacrum_Posterior_Sacral_Elements,
Location.Sacrum_Body,
Location.Sacrum_Endplate,
Location.Metal,
]
self.make_POIs(vert_nii, subreg_nii, label, ignore_list)

Expand All @@ -134,6 +140,12 @@ def test_POIs_MR(self):
Location.Vertebral_Body_Endplate_Inferior,
Location.Rib_Left,
Location.Rib_Right,
Location.Sacrum_Sacral_Ala_Left,
Location.Sacrum_Sacral_Ala_Right,
Location.Sacrum_Posterior_Sacral_Elements,
Location.Sacrum_Body,
Location.Sacrum_Endplate,
Location.Metal,
]
self.make_POIs(vert_nii, subreg_nii, label, ignore_list)

Expand Down
Loading