1616from nibabel import Nifti1Header , Nifti1Image # type: ignore
1717from typing_extensions import Self
1818
19+ from TPTBox .core import bids_files
1920from TPTBox .core .compat import zip_strict
2021from TPTBox .core .internal .nii_help import _resample_from_to , secure_save
2122from TPTBox .core .nii_poi_abstract import Has_Grid
4748 np_unique_withoutzero ,
4849 np_volume ,
4950)
50- from TPTBox .logger .log_file import Log_Type
51-
52- from . import bids_files
53- from .vert_constants import (
51+ from TPTBox .core .vert_constants import (
5452 AFFINE ,
5553 AX_CODES ,
5654 COORDINATE ,
6563 logging ,
6664 v_name2idx ,
6765)
66+ from TPTBox .logger .log_file import Log_Type
6867
6968if TYPE_CHECKING :
7069 from torch import device
@@ -1065,7 +1064,7 @@ def normalize_to_range_(self, min_value: int = 0, max_value: int = 1500, verbose
10651064 mi , ma = self .min (), self .max ()
10661065 self += - mi + min_value # min = 0
10671066 self_dtype = self .dtype
1068- max_value2 = ma
1067+ max_value2 = self . max () # this is a new value if min got shifted
10691068 if max_value2 > max_value :
10701069 self *= max_value / max_value2
10711070 self .set_dtype_ (self_dtype )
@@ -1125,7 +1124,9 @@ def smooth_gaussian_labelwise(
11251124 boundary_mode : str = "nearest" ,
11261125 dilate_prior : int = 0 ,
11271126 dilate_connectivity : int = 1 ,
1127+ dilate_channelwise : bool = False ,
11281128 smooth_background : bool = True ,
1129+ background_threshold : float | None = None ,
11291130 inplace : bool = False ,
11301131 ):
11311132 """Smoothes the segmentation mask by applying a gaussian filter label-wise and then using argmax to derive the smoothed segmentation labels again.
@@ -1145,8 +1146,20 @@ def smooth_gaussian_labelwise(
11451146 NII: The smoothed NII object.
11461147 """
11471148 assert self .seg , "You cannot use this on a non-segmentation NII"
1148- smoothed = np_smooth_gaussian_labelwise (self .get_seg_array (), label_to_smooth = label_to_smooth , sigma = sigma , radius = radius , truncate = truncate , boundary_mode = boundary_mode , dilate_prior = dilate_prior , dilate_connectivity = dilate_connectivity ,smooth_background = smooth_background ,)
1149- return self .set_array (smoothed ,inplace ,verbose = False )
1149+ smoothed = np_smooth_gaussian_labelwise (
1150+ self .get_seg_array (),
1151+ label_to_smooth = label_to_smooth ,
1152+ sigma = sigma ,
1153+ radius = radius ,
1154+ truncate = truncate ,
1155+ boundary_mode = boundary_mode ,
1156+ dilate_prior = dilate_prior ,
1157+ dilate_connectivity = dilate_connectivity ,
1158+ smooth_background = smooth_background ,
1159+ background_threshold = background_threshold ,
1160+ dilate_channelwise = dilate_channelwise ,
1161+ )
1162+ return self .set_array (smoothed , inplace , verbose = False )
11501163
11511164 def smooth_gaussian_labelwise_ (
11521165 self ,
@@ -1157,9 +1170,23 @@ def smooth_gaussian_labelwise_(
11571170 boundary_mode : str = "nearest" ,
11581171 dilate_prior : int = 1 ,
11591172 dilate_connectivity : int = 1 ,
1160- smooth_background : bool = True
1173+ dilate_channelwise : bool = False ,
1174+ smooth_background : bool = True ,
1175+ background_threshold : float | None = None ,
11611176 ):
1162- return self .smooth_gaussian_labelwise (label_to_smooth = label_to_smooth , sigma = sigma , radius = radius , truncate = truncate , boundary_mode = boundary_mode , dilate_prior = dilate_prior , dilate_connectivity = dilate_connectivity , smooth_background = smooth_background , inplace = True ,)
1177+ return self .smooth_gaussian_labelwise (
1178+ label_to_smooth = label_to_smooth ,
1179+ sigma = sigma ,
1180+ radius = radius ,
1181+ truncate = truncate ,
1182+ boundary_mode = boundary_mode ,
1183+ dilate_prior = dilate_prior ,
1184+ dilate_connectivity = dilate_connectivity ,
1185+ smooth_background = smooth_background ,
1186+ inplace = True ,
1187+ background_threshold = background_threshold ,
1188+ dilate_channelwise = dilate_channelwise ,
1189+ )
11631190
11641191 def to_ants (self ):
11651192 try :
@@ -1402,7 +1429,7 @@ def filter_connected_components(self, labels: int |list[int]|None=None,min_volum
14021429 #print("filter",nii.unique())
14031430 #assert max_count_component is None or nii.max() <= max_count_component, nii.unique()
14041431 return self .set_array (arr , inplace = inplace )
1405- def filter_connected_components_ (self , labels : int | list [int ]| None ,min_volume :int = 0 ,max_volume :int | None = None , max_count_component = None , connectivity : int = 3 ,keep_label = False ):
1432+ def filter_connected_components_ (self , labels : int | list [int ]| None = None ,min_volume :int = 0 ,max_volume :int | None = None , max_count_component = None , connectivity : int = 3 ,keep_label = False ):
14061433 return self .filter_connected_components (labels ,min_volume = min_volume ,max_volume = max_volume , max_count_component = max_count_component , connectivity = connectivity ,keep_label = keep_label ,inplace = True )
14071434
14081435 def get_segmentation_connected_components_center_of_mass (self , label : int , connectivity : int = 3 , sort_by_axis : int | None = None ) -> list [COORDINATE ]:
0 commit comments