Skip to content

Commit c86b6c0

Browse files
committed
Only reindex after deleting all the ROIs in multiple removal
1 parent 849ca11 commit c86b6c0

File tree

3 files changed

+1474
-296
lines changed

3 files changed

+1474
-296
lines changed

cellpose/gui/delete_utils.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""
2+
Utilities for deleting and relabeling GUI masks.
3+
"""
4+
5+
import numpy as np
6+
7+
8+
def normalize_remove_ids(remove_ids, ncells):
9+
"""Return unique valid label IDs in descending order."""
10+
remove_ids = np.asarray(remove_ids, dtype=np.int64).reshape(-1)
11+
if remove_ids.size == 0 or ncells <= 0:
12+
return np.zeros(0, dtype=np.int64)
13+
valid = (remove_ids > 0) & (remove_ids <= int(ncells))
14+
if not np.any(valid):
15+
return np.zeros(0, dtype=np.int64)
16+
remove_ids = np.unique(remove_ids[valid])
17+
return np.sort(remove_ids)[::-1]
18+
19+
20+
def batch_delete_reindex(cellpix, outpix, ismanual, cellcolors, zdraw, remove_ids):
21+
"""Delete labels and reindex all state in one pass.
22+
23+
Returns updated `(cellpix, outpix, ismanual, cellcolors, zdraw, remove_ids, remove_mask)`.
24+
"""
25+
if cellpix.shape != outpix.shape:
26+
raise ValueError("cellpix and outpix must have the same shape")
27+
28+
ncells = int(len(cellcolors) - 1)
29+
remove_ids = normalize_remove_ids(remove_ids, ncells)
30+
if remove_ids.size == 0:
31+
remove_mask = np.zeros(ncells + 1, dtype=bool)
32+
return (
33+
cellpix,
34+
outpix,
35+
ismanual,
36+
cellcolors,
37+
list(zdraw),
38+
remove_ids,
39+
remove_mask,
40+
)
41+
42+
remove_mask = np.zeros(ncells + 1, dtype=bool)
43+
remove_mask[remove_ids] = True
44+
keep_mask = ~remove_mask
45+
46+
lut_dtype = cellpix.dtype if np.issubdtype(cellpix.dtype, np.integer) else np.int64
47+
relabel_map = np.cumsum(keep_mask, dtype=lut_dtype) - 1
48+
relabel_map[remove_mask] = 0
49+
50+
cellpix = relabel_map[cellpix]
51+
outpix = relabel_map[outpix]
52+
ismanual = ismanual[keep_mask[1:]]
53+
cellcolors = cellcolors[keep_mask]
54+
zdraw = [z for z, keep in zip(zdraw, keep_mask[1:]) if keep]
55+
56+
return cellpix, outpix, ismanual, cellcolors, zdraw, remove_ids, remove_mask

0 commit comments

Comments
 (0)