Skip to content

Commit 3af053f

Browse files
committed
Update cp5 cellpose
1 parent 73f711d commit 3af053f

File tree

1 file changed

+50
-3
lines changed

1 file changed

+50
-3
lines changed

CP5/active_plugins/runcellpose.py

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,11 @@
8383
8484
"""
8585

86+
# Select Cellpose Docker Image
8687
CELLPOSE_DOCKER_NO_PRETRAINED = "cellprofiler/runcellpose_no_pretrained:0.1"
8788
CELLPOSE_DOCKER_IMAGE_WITH_PRETRAINED = "cellprofiler/runcellpose_with_pretrained:0.1"
8889

90+
# Detection mode
8991
MODEL_NAMES = ['cyto','nuclei','tissuenet','livecell', 'cyto2', 'general',
9092
'CP', 'CPx', 'TN1', 'TN2', 'TN3', 'LC1', 'LC2', 'LC3', 'LC4', 'custom']
9193

@@ -95,7 +97,7 @@ class RunCellpose(ImageSegmentation):
9597

9698
module_name = "RunCellpose"
9799

98-
variable_revision_number = 4
100+
variable_revision_number = 6
99101

100102
doi = {
101103
"Please cite the following when using RunCellPose:": "https://doi.org/10.1038/s41592-020-01018-x",
@@ -105,6 +107,16 @@ class RunCellpose(ImageSegmentation):
105107
def create_settings(self):
106108
super(RunCellpose, self).create_settings()
107109

110+
self.rescale = Binary(
111+
text="Rescale images before running Cellpose",
112+
value=True,
113+
doc="""\
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.
116+
"""
117+
)
118+
119+
108120
self.docker_or_python = Choice(
109121
text="Run CellPose in docker or local python environment",
110122
choices=["Docker", "Python"],
@@ -334,12 +346,21 @@ def set_directory_fn(path):
334346
doc="""
335347
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.
336348
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)
337357
""",
338358
)
339359

340360
def settings(self):
341361
return [
342362
self.x_name,
363+
self.rescale,
343364
self.docker_or_python,
344365
self.docker_image,
345366
self.expected_diameter,
@@ -362,10 +383,12 @@ def settings(self):
362383
self.omni,
363384
self.invert,
364385
self.remove_edge_masks,
386+
self.probability_rescale_setting,
365387
]
366388

367389
def visible_settings(self):
368-
vis_settings = [self.docker_or_python]
390+
vis_settings = [self.rescale, self.docker_or_python]
391+
369392

370393
if self.docker_or_python.value == "Docker":
371394
vis_settings += [self.docker_image]
@@ -402,6 +425,8 @@ def visible_settings(self):
402425

403426
if self.save_probabilities.value:
404427
vis_settings += [self.probabilities_name]
428+
if self.docker_or_python.value == 'Python':
429+
vis_settings += [self.probability_rescale_setting]
405430

406431
vis_settings += [self.use_averaging, self.use_gpu]
407432

@@ -440,6 +465,13 @@ def run(self, workspace):
440465
x = images.get_image(x_name)
441466
dimensions = x.dimensions
442467
x_data = x.pixel_data
468+
469+
if self.rescale.value:
470+
rescale_x = x_data.copy()
471+
x01 = numpy.percentile(rescale_x, 1)
472+
x99 = numpy.percentile(rescale_x, 99)
473+
x_data = numpy.clip((rescale_x - x01) / (x99 - x01), a_min=0, a_max=1)
474+
443475
anisotropy = 0.0
444476
if self.do_3D.value:
445477
anisotropy = x.spacing[0] / x.spacing[1]
@@ -609,9 +641,18 @@ def run(self, workspace):
609641
objects.add_objects(y, y_name)
610642

611643
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)
612653
# Flows come out sized relative to CellPose's inbuilt model size.
613654
# We need to slightly resize to match the original image.
614-
size_corrected = skimage.transform.resize(flows[2], y_data.shape)
655+
size_corrected = skimage.transform.resize(prob_map, y_data.shape)
615656
prob_image = Image(
616657
size_corrected,
617658
parent_image=x.parent_image,
@@ -699,4 +740,10 @@ def upgrade_settings(self, setting_values, variable_revision_number, module_name
699740
if variable_revision_number == 3:
700741
setting_values = [setting_values[0]] + ["Python",CELLPOSE_DOCKER_IMAGE_WITH_PRETRAINED] + setting_values[1:]
701742
variable_revision_number = 4
743+
if variable_revision_number == 4:
744+
setting_values = [setting_values[0]] + ['No'] + setting_values[1:]
745+
variable_revision_number = 5
746+
if variable_revision_number == 5:
747+
setting_values = setting_values + [False]
748+
variable_revision_number = 6
702749
return setting_values, variable_revision_number

0 commit comments

Comments
 (0)