Skip to content

Commit

Permalink
update io_sc.py and single_cell.py: add tmp to sc; refactor prep from…
Browse files Browse the repository at this point in the history
… single label mask case
  • Loading branch information
dummyindex committed Apr 11, 2024
1 parent 1d1cdb1 commit 5aace66
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 22 deletions.
57 changes: 35 additions & 22 deletions livecellx/core/io_sc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
)
Expand All @@ -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)

Expand Down
15 changes: 15 additions & 0 deletions livecellx/core/single_cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_
Expand Down Expand Up @@ -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})"

Expand Down Expand Up @@ -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

0 comments on commit 5aace66

Please sign in to comment.