Skip to content

Commit b68e0d0

Browse files
authored
Enable getting probability map when running from Docker (#250)
* Get probabilities in RunCellpose from docker include a conditional statement to get the right flow for probabilities when running RunCellpose from docker image * add probability map rescaling option when running from Python
1 parent 23fabd8 commit b68e0d0

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

active_plugins/runcellpose.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class RunCellpose(ImageSegmentation):
9797

9898
module_name = "RunCellpose"
9999

100-
variable_revision_number = 5
100+
variable_revision_number = 6
101101

102102
doi = {
103103
"Please cite the following when using RunCellPose:": "https://doi.org/10.1038/s41592-020-01018-x",
@@ -111,8 +111,8 @@ def create_settings(self):
111111
text="Rescale images before running Cellpose",
112112
value=True,
113113
doc="""\
114-
Reminds the user that the normalization step will be performed to ensure suimilar segmentation behaviour inthe RunCellpose
115-
modul and the Cellpose app.
114+
Reminds the user that the normalization step will be performed to ensure suimilar segmentation behaviour in the RunCellpose
115+
module and the Cellpose app.
116116
"""
117117
)
118118

@@ -346,6 +346,14 @@ def set_directory_fn(path):
346346
doc="""
347347
If you do not want to include any object masks that are not in full view in the image, you can have the masks that have pixels touching the the edges removed.
348348
The default is set to "Yes".
349+
""",
350+
)
351+
352+
self.probability_rescale_setting = Binary(
353+
text="Rescale probability map?",
354+
value=True,
355+
doc="""
356+
Activate to rescale probability map to 0-255 (which matches the scale used when running this module from Docker)
349357
""",
350358
)
351359

@@ -375,6 +383,7 @@ def settings(self):
375383
self.omni,
376384
self.invert,
377385
self.remove_edge_masks,
386+
self.probability_rescale_setting,
378387
]
379388

380389
def visible_settings(self):
@@ -416,6 +425,8 @@ def visible_settings(self):
416425

417426
if self.save_probabilities.value:
418427
vis_settings += [self.probabilities_name]
428+
if self.docker_or_python.value == 'Python':
429+
vis_settings += [self.probability_rescale_setting]
419430

420431
vis_settings += [self.use_averaging, self.use_gpu]
421432

@@ -630,9 +641,18 @@ def run(self, workspace):
630641
objects.add_objects(y, y_name)
631642

632643
if self.save_probabilities.value:
644+
if self.docker_or_python.value == "Docker":
645+
# get rid of extra dimension
646+
prob_map = numpy.squeeze(flows[1], axis=0) # ranges 0-255
647+
else:
648+
prob_map = flows[2]
649+
rescale_prob_map = prob_map.copy()
650+
prob_map01 = numpy.percentile(rescale_prob_map, 1)
651+
prob_map99 = numpy.percentile(rescale_prob_map, 99)
652+
prob_map = numpy.clip((rescale_prob_map - prob_map01) / (prob_map99 - prob_map01), a_min=0, a_max=1)
633653
# Flows come out sized relative to CellPose's inbuilt model size.
634654
# We need to slightly resize to match the original image.
635-
size_corrected = skimage.transform.resize(flows[2], y_data.shape)
655+
size_corrected = skimage.transform.resize(prob_map, y_data.shape)
636656
prob_image = Image(
637657
size_corrected,
638658
parent_image=x.parent_image,
@@ -723,6 +743,9 @@ def upgrade_settings(self, setting_values, variable_revision_number, module_name
723743
if variable_revision_number == 4:
724744
setting_values = [setting_values[0]] + ['No'] + setting_values[1:]
725745
variable_revision_number = 5
746+
if variable_revision_number == 5:
747+
setting_values = setting_values + [False]
748+
variable_revision_number = 6
726749
return setting_values, variable_revision_number
727750

728751

0 commit comments

Comments
 (0)