diff --git a/livecellx/core/io_sc.py b/livecellx/core/io_sc.py index f705840..87c0d03 100644 --- a/livecellx/core/io_sc.py +++ b/livecellx/core/io_sc.py @@ -8,27 +8,9 @@ from livecellx.core.sc_key_manager import SingleCellMetaKeyManager -# TODO: fix the function below -def process_scs_from_label_mask(label_mask_dataset, dic_dataset, time, bg_val=0, min_contour_len=10): - """process single cells from one label mask. Store labels of single cells in their meta data. - - Parameters - ---------- - label_mask_dataset : _type_ - _description_ - dic_dataset : _type_ - _description_ - time : _type_ - _description_ - bg_val : int, optional - _description_, by default 0 - - Returns - ------- - _type_ - _description_ - """ - label_mask = label_mask_dataset.get_img_by_time(time) +def process_scs_from_single_label_mask( + label_mask, img_dataset, time, bg_val=0, min_contour_len=10, label_mask_dataset=None +): labels = set(np.unique(label_mask)) if bg_val in labels: labels.remove(bg_val) @@ -75,7 +57,7 @@ def process_scs_from_label_mask(label_mask_dataset, dic_dataset, time, bg_val=0, ) # int important here to get rid of numpy.int64 or numpy.int8, etc, to avoid json dump error sc = SingleCellStatic( timeframe=time, - img_dataset=dic_dataset, + img_dataset=img_dataset, mask_dataset=label_mask_dataset, contour=contour, ) @@ -84,6 +66,37 @@ def process_scs_from_label_mask(label_mask_dataset, dic_dataset, time, bg_val=0, return _scs +# TODO: fix the function below +def process_scs_from_label_mask(label_mask_dataset, img_dataset, time, bg_val=0, min_contour_len=10): + """process single cells from one label mask. Store labels of single cells in their meta data. + + Parameters + ---------- + label_mask_dataset : _type_ + _description_ + dic_dataset : _type_ + _description_ + time : _type_ + _description_ + bg_val : int, optional + _description_, by default 0 + + Returns + ------- + _type_ + _description_ + """ + + return process_scs_from_single_label_mask( + label_mask_dataset.get_mask(time), + img_dataset, + time, + bg_val=bg_val, + min_contour_len=min_contour_len, + label_mask_dataset=label_mask_dataset, + ) + + def process_mask_wrapper(args): return process_scs_from_label_mask(*args) diff --git a/livecellx/core/single_cell.py b/livecellx/core/single_cell.py index c8df086..ca6770a 100644 --- a/livecellx/core/single_cell.py +++ b/livecellx/core/single_cell.py @@ -49,6 +49,7 @@ def __init__( cache: Optional[Dict[str, object]] = None, # TODO: now only image crop is cached update_mask_dataset_by_contour=False, empty_cell=False, + tmp=None, ) -> None: """_summary_ @@ -125,6 +126,11 @@ def __init__( # self.id = SingleCellStatic.id_generator.__next__() self.id = uuid.uuid4() + if tmp is not None: + self.tmp = tmp + else: + self.tmp = dict() + def __repr__(self) -> str: return f"SingleCellStatic(id={self.id}, timeframe={self.timeframe}, bbox={self.bbox})" @@ -1734,3 +1740,12 @@ def filter_boundary_cells(scs: List[SingleCellStatic], dist_to_boundary=30): ): not_boundary_scs.append(sc) return not_boundary_scs + + +def create_label_mask_from_scs(scs: List[SingleCellStatic], labels=None, dtype=np.int32): + label_mask = np.zeros(scs[0].get_mask().shape, dtype=dtype) + if labels is None: + labels = list(range(1, len(scs) + 1)) # Bg label is 0 + for idx, sc in enumerate(scs): + label_mask[sc.get_mask()] = labels[idx] + return label_mask