From 3923b66df2081f2dddd0756098e9d3ac26748efe Mon Sep 17 00:00:00 2001 From: iback Date: Thu, 17 Apr 2025 13:53:02 +0000 Subject: [PATCH 1/2] fixed poi crop bug and connected components keep label argument --- TPTBox/core/nii_wrapper.py | 15 ++++++++------- TPTBox/core/poi.py | 2 +- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/TPTBox/core/nii_wrapper.py b/TPTBox/core/nii_wrapper.py index f5e3e8d..383a3b1 100755 --- a/TPTBox/core/nii_wrapper.py +++ b/TPTBox/core/nii_wrapper.py @@ -1254,13 +1254,14 @@ def filter_connected_components(self, labels: int |list[int]|None=None,min_volum """ assert self.seg, "This only works on segmentations" arr = np_filter_connected_components(self.get_seg_array(), largest_k_components=max_count_component,label_ref=labels,connectivity=connectivity,return_original_labels=keep_label,min_volume=min_volume,max_volume=max_volume,removed_to_label=removed_to_label,) - #if keep_label and labels is not None: - # if isinstance(labels,int): - # labels = [labels] - # old_labels = [i for i in self.unique() if i not in labels] - # if len(old_labels) != 0: - # s = self.extract_label(old_labels,keep_label=True) - # nii[s != 0] = s[s!=0] + assert arr.shape == self.shape, f"Shape mismatch: {arr.shape} != {self.shape}" + if keep_label and labels is not None: + if isinstance(labels,int): + labels = [labels] + old_labels = [i for i in self.unique() if i not in labels] + if len(old_labels) != 0: + s = self.extract_label(old_labels,keep_label=True).get_array() + arr[s != 0] = s[s!=0] #print("filter",nii.unique()) #assert max_count_component is None or nii.max() <= max_count_component, nii.unique() return self.set_array(arr, inplace=inplace) diff --git a/TPTBox/core/poi.py b/TPTBox/core/poi.py index a581665..77ef207 100755 --- a/TPTBox/core/poi.py +++ b/TPTBox/core/poi.py @@ -1073,7 +1073,7 @@ def calc_centroids_from_two_masks( org_shape = subreg_msk.shape # crop to mask to speed up the segmentation crop = vert_msk.compute_crop() - crop = subreg_msk.compute_crop(maximum_size=crop) + # crop = subreg_msk.compute_crop(maximum_size=crop) # crop = (slice(0, subreg_msk.shape[0]), slice(0, subreg_msk.shape[1]), slice(0, subreg_msk.shape[2])) vert_msk = vert_msk.apply_crop(crop) From 46f78468b3cb328e68fc5e375a842c5bfacdb9ea Mon Sep 17 00:00:00 2001 From: iback Date: Wed, 11 Jun 2025 07:46:45 +0000 Subject: [PATCH 2/2] quick fixes --- TPTBox/core/nii_wrapper.py | 17 +++++++++++++++++ TPTBox/core/np_utils.py | 2 +- TPTBox/core/poi.py | 1 + 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/TPTBox/core/nii_wrapper.py b/TPTBox/core/nii_wrapper.py index cb2ba9f..48b6e7f 100755 --- a/TPTBox/core/nii_wrapper.py +++ b/TPTBox/core/nii_wrapper.py @@ -952,6 +952,23 @@ def normalize_to_range_(self, min_value: int = 0, max_value: int = 1500, verbose self.set_dtype_(self_dtype) log.print(f"Shifted from range {mi, ma} to range {self.min(), self.max()}", verbose=verbose) + def get_histogram(self, bins=256, hrange=None, density=False, c_val:float|None=None): + """Returns the histogram of the image array. + + Args: + bins (int, optional): Number of bins for the histogram. Defaults to 256. + range (tuple, optional): Range of values to consider for the histogram. Defaults to None. + density (bool, optional): If True, the result is the probability density function at the bin, normalized such that the integral over the range is 1. Defaults to False. + c_val (float|None, optional): The value below which all values are set to c_val. Defaults to None. + + Returns: + tuple: A tuple containing the histogram values and the bin edges. + """ + arr = self.get_array() + if c_val is not None: + arr[arr <= c_val] = c_val + return np.histogram(arr, bins=bins, range=hrange, density=density) + def match_histograms(self, reference:Image_Reference,c_val = 0,inplace=False): assert not self.seg ref_nii = to_nii(reference) diff --git a/TPTBox/core/np_utils.py b/TPTBox/core/np_utils.py index 92b5403..191a61e 100755 --- a/TPTBox/core/np_utils.py +++ b/TPTBox/core/np_utils.py @@ -341,7 +341,7 @@ def np_dilate_msk( data = out.copy() data[i != data] = 0 if use_crop: - lcrop = np_bbox_binary(data, px_dist=2, raise_error=False) + lcrop = np_bbox_binary(data, px_dist=2 + n_pixel, raise_error=False) data = data[lcrop] msk_ibe_data = _binary_dilation(data, struct=struct) diff --git a/TPTBox/core/poi.py b/TPTBox/core/poi.py index a3733b0..71cf2b3 100755 --- a/TPTBox/core/poi.py +++ b/TPTBox/core/poi.py @@ -314,6 +314,7 @@ def apply_crop(self: Self, o_shift: tuple[slice, slice, slice] | Sequence[slice] """ origin: COORDINATE = None # type: ignore shape = None # type: ignore + o_shift = tuple(o if o.start is not None else slice(0, None) for o in o_shift) try: def shift(x, y, z):