73
73
74
74
def segment_ROI (
75
75
x : np .ndarray ,
76
+ num_labels_tot : dict [str , int ],
76
77
model : models .CellposeModel = None ,
77
78
do_3D : bool = True ,
78
79
channels : list [int ] = [0 , 0 ],
79
80
diameter : float = 30.0 ,
80
81
normalize : CellposeCustomNormalizer = CellposeCustomNormalizer (),
81
82
normalize2 : Optional [CellposeCustomNormalizer ] = None ,
82
83
label_dtype : Optional [np .dtype ] = None ,
84
+ relabeling : bool = True ,
83
85
advanced_cellpose_model_params : CellposeModelParams = CellposeModelParams (), # noqa: E501
84
86
) -> np .ndarray :
85
87
"""
86
88
Internal function that runs Cellpose segmentation for a single ROI.
87
89
88
90
Args:
89
91
x: 4D numpy array.
92
+ num_labels_tot: Number of labels already in total image. Used for
93
+ relabeling purposes. Using a dict to have a mutable object that
94
+ can be edited from within the function without having to be passed
95
+ back through the masked_loading_wrapper.
90
96
model: An instance of `models.CellposeModel`.
91
97
do_3D: If `True`, cellpose runs in 3D mode: runs on xy, xz & yz planes,
92
98
then averages the flows.
@@ -107,6 +113,7 @@ def segment_ROI(
107
113
normalized with default settings, both channels need to be
108
114
normalized with default settings.
109
115
label_dtype: Label images are cast into this `np.dtype`.
116
+ relabeling: Whether relabeling based on num_labels_tot is performed.
110
117
advanced_cellpose_model_params: Advanced Cellpose model parameters
111
118
that are passed to the Cellpose `model.eval` method.
112
119
"""
@@ -163,6 +170,23 @@ def segment_ROI(
163
170
f" { advanced_cellpose_model_params .flow_threshold = } "
164
171
)
165
172
173
+ # Shift labels and update relabeling counters
174
+ if relabeling :
175
+ num_labels_roi = np .max (mask )
176
+ mask [mask > 0 ] += num_labels_tot ["num_labels_tot" ]
177
+ num_labels_tot ["num_labels_tot" ] += num_labels_roi
178
+
179
+ # Write some logs
180
+ logger .info (f"ROI had { num_labels_roi = } , { num_labels_tot = } " )
181
+
182
+ # Check that total number of labels is under control
183
+ if num_labels_tot ["num_labels_tot" ] > np .iinfo (label_dtype ).max :
184
+ raise ValueError (
185
+ "ERROR in re-labeling:"
186
+ f"Reached { num_labels_tot } labels, "
187
+ f"but dtype={ label_dtype } "
188
+ )
189
+
166
190
return mask .astype (label_dtype )
167
191
168
192
@@ -438,8 +462,7 @@ def cellpose_segmentation(
438
462
logger .info (f"{ data_zyx_c2 .chunks } " )
439
463
440
464
# Counters for relabeling
441
- if relabeling :
442
- num_labels_tot = 0
465
+ num_labels_tot = {"num_labels_tot" : 0 }
443
466
444
467
# Iterate over ROIs
445
468
num_ROIs = len (list_indices )
@@ -485,13 +508,15 @@ def cellpose_segmentation(
485
508
486
509
# Prepare keyword arguments for segment_ROI function
487
510
kwargs_segment_ROI = dict (
511
+ num_labels_tot = num_labels_tot ,
488
512
model = model ,
489
513
channels = channels ,
490
514
do_3D = do_3D ,
491
515
label_dtype = label_dtype ,
492
516
diameter = diameter_level0 / coarsening_xy ** level ,
493
517
normalize = channel .normalize ,
494
518
normalize2 = channel2 .normalize ,
519
+ relabeling = relabeling ,
495
520
advanced_cellpose_model_params = advanced_cellpose_model_params ,
496
521
)
497
522
@@ -515,23 +540,6 @@ def cellpose_segmentation(
515
540
preprocessing_kwargs = preprocessing_kwargs ,
516
541
)
517
542
518
- # Shift labels and update relabeling counters
519
- if relabeling :
520
- num_labels_roi = np .max (new_label_img )
521
- new_label_img [new_label_img > 0 ] += num_labels_tot
522
- num_labels_tot += num_labels_roi
523
-
524
- # Write some logs
525
- logger .info (f"ROI { indices } , { num_labels_roi = } , { num_labels_tot = } " )
526
-
527
- # Check that total number of labels is under control
528
- if num_labels_tot > np .iinfo (label_dtype ).max :
529
- raise ValueError (
530
- "ERROR in re-labeling:"
531
- f"Reached { num_labels_tot } labels, "
532
- f"but dtype={ label_dtype } "
533
- )
534
-
535
543
if output_ROI_table :
536
544
bbox_df = array_to_bounding_box_table (
537
545
new_label_img ,
0 commit comments