forked from CellProfiler/CellProfiler-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidentifyyeastcells.py
1148 lines (953 loc) · 50.8 KB
/
identifyyeastcells.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
import threading
import cellprofiler.icons
try:
# version 3.0.0
from cellprofiler.modules._help import PROTIP_RECOMEND_ICON, PROTIP_AVOID_ICON, TECH_NOTE_ICON
except:
from cellprofiler.modules._help import PROTIP_RECOMMEND_ICON as PROTIP_RECOMEND_ICON
from cellprofiler.modules._help import PROTIP_AVOID_ICON, TECH_NOTE_ICON
__doc__ = """<b>IdentifyYeastCells</b> identifies yeast (or other round) objects in the image. This module was designed
to work on brightfield images. However in some cases it can also be efficient with fluorescent imagery.
The important part of the module is parameter fitting mechanism which allows to adapt it to many different types of imagery.
There is a number of various usage example available on website
<a href="http://www.cellstar-algorithm.org/">CellStar algorithm</a> which includes ready to use pipelines.
<hr>
<h4>What do I need as input?</h4>
To use this module, you will need to make sure that your input image has the following qualities:
<ul>
<li>The image should be grayscale.</li>
</ul>
<h4>What do the settings mean?</h4>
See below for help on the individual settings. The following icons are used to call attention to
key items:
<ul>
<li><img src="memory:{PROTIP_RECOMEND_ICON}"> Our recommendation or example use case
for which a particular setting is best used.</li>
<li><img src="memory:{PROTIP_AVOID_ICON}"> Indicates a condition under which
a particular setting may not work well.</li>
<li><img src="memory:{TECH_NOTE_ICON}"> Technical note. Provides more
detailed information on the setting.</li>
</ul>
<h4>What do I get as output?</h4>
A set of primary objects are produced by this module, which can be used in downstream modules
for measurement purposes or other operations.
See the section <a href="#Available_measurements">"Available measurements"</a> below for
the measurements that are produced by this module.
Once the module has finished processing, the module display window
will show the following panels:
<ul>
<li><i>Left:</i> The raw, original image.</li>
<li><i>Right:</i> The identified objects shown as a color
image where connected pixels that belong to the same object are assigned the same
color (<i>label image</i>). It is important to note that assigned colors are
arbitrary; they are used simply to help you distingush the various objects. </li>
</ul>
<a name="Available_measurements">
<h4>Available measurements</h4>
<b>Image measurements:</b>
<ul>
<li><i>Count:</i> The number of primary objects identified.</li>
</ul>
<b>Object measurements:</b>
<ul>
<li><i>Location_X, Location_Y:</i> The pixel (X,Y) coordinates of the primary
object centroids. The centroid is calculated as the center of mass of the binary
representation of the object.</li>
<li><i>Features_Quality:</i> The module internal quality measure describing how well objects fits the model.
It can be used to filter out the weakest objects.</li>
</ul>
<h3>Publications</h3>
This module was prepared by Filip Mroz, Adam Kaczmarek and Szymon Stoma. The method uses CellStar approach
developed by Versari et al. It is also available with a dedicated GUI for semi-automatic segmentation.
"Long-term Tracking of Budding Yeast Cells in Brightfield Microscopy: CellStar and the Evaluation Platform."
Journal of The Royal Society Interface 14.127 (2017)
Please reach us at <a href="http://www.cellstar-algorithm.org/">CellStar algorithm</a> website for inquires and manuals.
<h3>Credits</h3>
Filip Mroz, Adam Kaczmarek, Szymon Stoma,
Artemis Llamosi,
Kirill Batmanov,
Cristian Versari,
Cedric Lhoussaine,
Gabor Csucs,
Simon F. Noerrelykke,
Gregory Batt,
Pawel Rychlikowski,
Pascal Hersen.
""".format(PROTIP_RECOMEND_ICON=PROTIP_RECOMEND_ICON, PROTIP_AVOID_ICON=PROTIP_AVOID_ICON,
TECH_NOTE_ICON=TECH_NOTE_ICON)
# Module documentation variables:
__authors__ = """Filip Mroz,
Adam Kaczmarek,
Szymon Stoma
"""
__contact__ = "fafafft@gmail.com"
__date__ = "2017.05.24"
__version__ = "1.3.0"
__docformat__ = "restructuredtext en"
__revision__ = "$Id$"
#################################
#
# Imports from useful Python libraries
#
#################################
from os.path import join as pj
from os.path import isfile
import logging
logger = logging.getLogger(__name__)
import math
import numpy as np
import scipy.ndimage
#################################
#
# Imports from CellProfiler
#
##################################
import bioformats
import cellprofiler.modules
import cellprofiler.measurement as cpmeas
import cellprofiler.setting as cps
import cellprofiler.preferences as pref
#################################
#
# Specific imports
#
##################################
from cellstar.utils.params_util import default_parameters, create_size_weights
from cellstar.segmentation import Segmentation
from cellstar.parameter_fitting.pf_runner import run_pf, run_rank_pf
from cellstar.utils.debug_util import memory_profile, explorer_expected
###################################
#
# Constants
#
###################################
DEBUG = 1
F_BACKGROUND = "Background"
BKG_CURRENT = 'Actual image'
BKG_FIRST = 'First image'
BKG_FILE = 'File'
C_OBJECT_FEATURES = "Features"
FTR_OBJECT_QUALITY = "Quality"
'''The object quality - floating number the higher the better'''
M_OBJECT_FEATURES_OBJECT_QUALITY = '%s_%s' % (C_OBJECT_FEATURES, FTR_OBJECT_QUALITY)
def hack_add_from_file_into_EditObjects(dialog_box):
import wx
def on_load(event):
with wx.FileDialog(None,
message="Select image with labels",
wildcard="Image file (*.tif,*.tiff,*.jpg,*.jpeg,*.png,*.gif,*.bmp)|*.tif;*.tiff;*.jpg;*.jpeg;*.png;*.gif;*.bmp|*.* (all files)|*.*",
style=wx.FD_OPEN) as dlg:
if dlg.ShowModal() != wx.ID_OK:
return
path = dlg.Path
labels_loaded = (bioformats.load_image(path) * 255).astype(int)
if labels_loaded.ndim == 3:
labels_loaded = np.sum(labels_loaded, 2) / labels_loaded.shape[2]
for l in np.unique(labels_loaded):
if l != 0:
dialog_box.add_label(l == labels_loaded)
# no shape check, no float check
dialog_box.init_labels()
dialog_box.display()
dialog_box.record_undo()
ID_ACTION_LOAD_FROM_FILE = wx.NewId()
sizer = dialog_box.toolbar.ContainingSizer
load_file_button = wx.Button(dialog_box, ID_ACTION_LOAD_FROM_FILE, "Add from file")
list(sizer.Children)[-1].Sizer.Add(load_file_button, 0, wx.ALIGN_CENTER)
dialog_box.Bind(wx.EVT_BUTTON, on_load, load_file_button)
###################################
#
# The module class
#
###################################
class IdentifyYeastCells(cellprofiler.module.ImageSegmentation):
module_name = "IdentifyYeastCells"
category = "Yeast Toolbox"
variable_revision_number = 9
current_workspace = ''
fitting_image_set = None
param_fit_progress = 0
param_fit_progress_partial = 0
def create_settings(self):
super(IdentifyYeastCells, self).create_settings()
self.input_image_name = self.x_name
self.input_image_name.doc = "How do you call the images you want to use to identify objects?"
self.object_name = self.y_name
self.object_name.doc = "How do you want to call the objects identified by this module?"
self.background_image_name = cps.ImageNameSubscriber(
"Select the empty field image", doc="""
<i>(Used only when you select "loaded from file" background calculation strategy)</i><br>
How do you call the image you want to use as background
image (same image will be used for every image in the workflow)?
""")
self.ignore_mask_image_name = cps.ImageNameSubscriber(
"Select ignore mask image", doc="""
You can provide a ignore mark with regions in the image which are to be ignored by the algorithm in segmentation.
""", can_be_blank=True)
self.background_elimination_strategy = cps.Choice(
'Select the background calculation mode',
[BKG_CURRENT, BKG_FILE], doc="""
You can choose from the following options:
<ul>
<li><i>loaded from file</i>: Use this option if your background does not change at all for all images in the series. </li>
<li><i>computed from actual image</i>: This is default option. The algorithm will try to automatically compute the background for
each individual image. In some specific cases it is better to use manually precomputed background loaded from file.</li>
</ul>""")
self.average_cell_diameter = cps.Float(
"Average cell diameter in pixels",
30.0, minval=10, doc='''\
The average cell diameter is used to scale many algorithm parameters.
Please use e.g. ImageJ to measure the average cell size in pixels.
'''
)
self.advanced_cell_filtering = cps.Binary(
'Do you want to filter cells by area?', False, doc="""
This parameter is used for cell filtering. The algorithm creates many more cell candidates than finally accepted cells (these
cells overlap with each other). At the final phase of algorithm the cells are selected from the ensemble of cell candidates based on the
"quality" measure. Use this option if you want to absolutely prohibit too small (or too big) cells to be chosen (regardless of "quality").
""")
self.min_cell_area = cps.Integer(
"Minimal area of accepted cell in pixels",
900, minval=10, doc='''\
<i>(Used only when you want to filter cells based on area)</i><br>
The minimum cell area is used while final filtering of cells.
Please use e.g. ImageJ to measure the average cell size in pixels.
'''
)
self.max_cell_area = cps.Integer(
"Maximum area of accepted cell in pixels",
5 * 900, minval=10, doc='''\
<i>(Used only when you want to filter cells based on area)</i><br>
The maximum cell area is used while final filtering of cells.
Please use e.g. ImageJ to measure the average cell size in pixels.
'''
)
self.background_brighter_then_cell_inside = cps.Binary(
'Is the area without cells (background) brighter then cell interiors?', True, doc="""
Please check if the area inside of the cells is <b>darker</b> than the area without the cells (background). Use e.g. ImageJ to measure
average intensity.
"""
)
self.bright_field_image = cps.Binary(
'Do you want to segment brightfield images?', True, doc="""
Choose this option if you want to segment a brightfield image. For segmentation of fluorescent images please answer "No".
"""
)
self.advanced_parameters = cps.Binary(
'Use advanced configuration parameters?', False, doc="""
Do you want to use advanced parameters to configure plugin? They allow for more flexibility, and require
understanding of the algorithm to configure well.
"""
)
self.segmentation_precision = cps.Integer(
"Segmentation precision",
2, minval=1, maxval=5, doc='''\
<i>(Used only when you want to specify advanced parameters)</i><br>
Describes how thoroughly the algorithm searches for cells. Higher values should
make it easier to find smaller cells because the more parameters sets are searched.
The cost is longer runtime.
'''
)
self.specify_precision_details = cps.Binary(
"Do you want to edit details of segmentation precision?", False,
doc='''<i>(Used only when you want to specify advanced parameters)</i><br>
Do you want to edit details of segmentation precision?<br>
<img src="memory:{TECH_NOTE_ICON}"> These are low level parameters that can be modified
to further tweak result.
'''.format(TECH_NOTE_ICON=TECH_NOTE_ICON))
self.iterations = cps.Integer(
"Iterations",
6, minval=1, maxval=15, doc='''\
<i>(Used only when you want to specify precision details)</i><br>
Number of segmentation iterations done by CellStar.''')
self.seeds_border = cps.Float(
"Seeds from border",
1.2, minval=0, maxval=5, doc='''\
<i>(Used only when you want to specify precision details)</i><br>
How many seeds are to be extracted from this source:<br><br>
val = 0 - no seeds from this source<br>
val in (0.0, 0.5] - seeds only from second iteration<br>
val in (0.5, 1) - seeds only in first iteration<br>
val = 1 - seeds in every iteration<br>
val in (1, 5.0) - additional random seeding: val*seeds_num total<br>
''')
self.seeds_content = cps.Float(
"Seeds from content",
1.2, minval=0, maxval=5, doc=self.seeds_border.doc)
self.seeds_centroid = cps.Float(
"Seeds from centroid",
1.2, minval=0, maxval=5, doc=self.seeds_border.doc)
self.contour_points = cps.Integer(
"Contour points",
44, minval=36, maxval=70, doc='''\
<i>(Used only when you want to specify precision details)</i><br>
Number of points in every contour.''')
self.contour_precision = cps.Float(
"Contour precision",
49.75, minval=25, maxval=200, doc='''\
<i>(Used only when you want to specify precision details)</i><br>
Number of space points covering average cell diameter.''')
self.weights_number = cps.Integer(
"Cell size variants",
2, minval=1, maxval=4, doc='''\
<i>(Used only when you want to specify precision details)</i><br>
Number of tested difference size weights in every cell grow.''')
self.maximal_cell_overlap = cps.Float(
"Maximal overlap allowed while final filtering of cells",
0.2, minval=0, maxval=1, doc='''\
<i>(Used only when you want to specify advanced parameters)</i><br>
This parameter is used for cell filtering. The algorithm creates many more cell candidates than finally accepted cells (these
cells overlap with each other). At the final phase of algorithm the cells are selected from the ensamble of cell candidates based on the
"quality" measure. One of the conditions checked while filtering is the overlap of the current candidate cell with already chosen cells.
Use this parameter if you want to allow to choose cells even if they overlap. Important: at the end cells do not overlap - they are
trimmed in such a way that the cell of higher "quality" will "borrow" the area of lower "quality" cells.
'''
)
self.autoadaptation_steps = cps.Integer(
"Number of steps in the autoadaptation procedure",
1, minval=1, maxval=1000, doc='''
Describes how thoroughly we want to adapt the algorithm to current image sets. Higher values should
make it easier to correctly discover cells, however you will have to wait longer for the autoadaptation
procedure to finish. Remember that you do it once for all images, and you can copy the values from other
pipelines, if you have already found the parameters before.
'''
)
self.use_ground_truth_to_set_params = cps.DoSomething("", "Autoadapt parameters",
self.ground_truth_editor, doc="""
Use this option to autoadapt parameters required for correct contour identification. This procedure should be run once
for one image in the series. Using your input the algorithm "learns" to recognize cells. When you click this button the window will open.
Please select one of the images which you would like to segment. If you use background or ignore mask images you will have to provide them as well.
On the input image you should draw few cells (3-6) and click "Done". Then the
"learning" procedure will start. Please be patient. Usually single iteration lasts 1-3 min. The more iterations you will choose, the more likely
it is that the algorithm will work better on your images.
<br><br>
There is an experimental alternative to selecting existing image files which may be useful when images are "produced" by the pipeline:
<ol>
<li>Start Test Run and step down to IdentifyYeastCells module</li>
<li>Step through IdentifyYeastCells - it will run segmentation and remember input images.</li>
<li>Now you can adapt without specifing any images.</li>
</ol>
<br><br>
<dl>
<dd><img src="memory:{PROTIP_RECOMEND_ICON}"> Recommendations:
<ul>
<li>Start with single iteration, then check how well the algorithm is able to recognize your cells </li>
<li>If you have more time, use more iterations. You can always cancel (it will last 1-5 min). The algorithm will
then remember the best parameters learned during the session. </li>
<li>Parameters are stored as a text in "Autoadapted parameters". If you found good parameters for your datasets
you can copy them to another pipeline.<\li>
</ul></dd>
</dl>
""".format(PROTIP_RECOMEND_ICON=PROTIP_RECOMEND_ICON))
self.show_autoadapted_params = cps.Binary(
'Do you want to see autoadapted parameters?', False, doc="""
<i>(Used only when you want to specify advanced parameters)</i><br>
Use this option to display autoadapted parameters.""")
self.autoadapted_params = cps.Text(text="Autoadapted parameters: ",
value="[[0.0442, 304.45, 15.482, 189.40820000000002], [300, 10, 0, 18, 10]]",
doc="""
<i>(Used only when you want to specify advanced and autoadapted parameters)</i><br>
Autoadapted parameters are pasted here from the "learning" preocedure. These parameters are used to characterize cell borders.
Edit them only if you know what you are doing. If you found good parameters for your datasets
you can copy them to another pipeline.
""")
PRECISION_PARAMS_START = 18
PRECISION_PARAMS_END = 24
def settings(self):
return [self.input_image_name,
self.object_name,
self.average_cell_diameter,
self.segmentation_precision,
self.maximal_cell_overlap,
self.background_image_name,
self.advanced_parameters,
self.background_brighter_then_cell_inside,
self.bright_field_image,
self.min_cell_area,
self.max_cell_area,
self.advanced_cell_filtering,
self.background_elimination_strategy,
self.show_autoadapted_params,
self.autoadapted_params,
self.autoadaptation_steps,
self.ignore_mask_image_name,
self.specify_precision_details,
self.iterations,
self.seeds_border,
self.seeds_content,
self.seeds_centroid,
self.contour_points,
self.contour_precision,
self.weights_number,
]
def visible_settings(self):
visible_settings = [self.input_image_name,
self.object_name,
self.average_cell_diameter,
]
visible_settings += [self.bright_field_image,
self.background_brighter_then_cell_inside,
self.autoadaptation_steps,
self.use_ground_truth_to_set_params,
self.show_autoadapted_params]
if self.show_autoadapted_params:
visible_settings.append(self.autoadapted_params)
visible_settings.append(self.advanced_parameters)
#
# Show the user the background only if self.provide_background is checked
#
if self.advanced_parameters:
visible_settings.append(self.segmentation_precision)
visible_settings.append(self.specify_precision_details)
if self.specify_precision_details:
visible_settings.append(self.iterations)
visible_settings.append(self.seeds_border)
visible_settings.append(self.seeds_content)
visible_settings.append(self.seeds_centroid)
visible_settings.append(self.contour_points)
visible_settings.append(self.contour_precision)
visible_settings.append(self.weights_number)
visible_settings.append(self.maximal_cell_overlap)
visible_settings.append(self.advanced_cell_filtering)
if self.advanced_cell_filtering:
visible_settings.append(self.min_cell_area)
visible_settings.append(self.max_cell_area)
# Show the user the background only if self.provide_background is checked
visible_settings.append(self.background_elimination_strategy) #
if self.background_elimination_strategy == BKG_FILE:
visible_settings.append(self.background_image_name)
visible_settings.append(self.ignore_mask_image_name)
return visible_settings
def on_setting_changed(self, setting, pipeline):
'''If precision is changed then update all the related settings'''
if setting == self.segmentation_precision or \
setting == self.specify_precision_details and not self.specify_precision_details.value:
self.set_ui_from_precision(self.segmentation_precision.value)
def is_interactive(self):
return False
def prepare_group(self, workspace, grouping, image_numbers):
'''Erase module information at the start of a run'''
d = self.get_dictionary(workspace.image_set_list)
d.clear()
return True
def get_ws_dictionary(self, workspace):
return self.get_dictionary(workspace.image_set_list)
def __get(self, field, workspace, default):
if self.get_ws_dictionary(workspace).has_key(field):
return self.get_ws_dictionary(workspace)[field]
return default
def __set(self, field, workspace, value):
self.get_ws_dictionary(workspace)[field] = value
def upgrade_settings(self, setting_values, variable_revision_number, module_name, from_matlab):
'''Adjust setting values if they came from a previous revision
setting_values - a sequence of strings representing the settings
for the module as stored in the pipeline
variable_revision_number - the variable revision number of the
module at the time the pipeline was saved. Use this
to determine how the incoming setting values map
to those of the current module version.
module_name - the name of the module that did the saving. This can be
used to import the settings from another module if
that module was merged into the current module
from_matlab - True if the settings came from a Matlab pipeline, False
if the settings are from a CellProfiler 2.0 pipeline.
Overriding modules should return a tuple of setting_values,
variable_revision_number and True if upgraded to CP 2.0, otherwise
they should leave things as-is so that the caller can report
an error.
'''
if variable_revision_number == 2:
setting_values = setting_values + [False]
variable_revision_number = 3
if variable_revision_number == 3:
setting_values = setting_values + [True]
variable_revision_number = 4
if variable_revision_number < 7:
setting_values = setting_values + ['Leave blank'] # ignore mask
# decode precision index 3
setting_values[3] = str(self.precision_to_ui_map[int(setting_values[3])])
variable_revision_number = 7
if variable_revision_number == 7:
# fill new ones based on precision
setting_values = setting_values + [False]
params_from_precision = self.get_ui_params_from_precision(int(setting_values[3]))
setting_values[20:26 + 1] = params_from_precision
variable_revision_number = 8
if variable_revision_number == 8:
# two settings were removed
setting_values = setting_values[:6] + setting_values[8:]
variable_revision_number = 9
return setting_values, variable_revision_number, from_matlab
def display(self, workspace, figure=None):
if self.show_window:
figure.set_subplots((2, 1))
title = "Input image, cycle #%d" % (workspace.measurements.image_number,)
image = workspace.display_data.input_pixels
labeled_image = workspace.display_data.segmentation_pixels
ax = figure.subplot_imshow_grayscale(0, 0, image, title)
figure.subplot_imshow_labels(1, 0, labeled_image,
self.object_name.value,
sharexy=ax)
#
# Measuremets:
# - objects count
# - objects location
def get_measurement_columns(self, pipeline):
'''Column definitions for measurements made by IdentifyPrimAutomatic'''
columns = super(IdentifyYeastCells, self).get_measurement_columns(pipeline)
columns += [(self.object_name.value, M_OBJECT_FEATURES_OBJECT_QUALITY, cpmeas.COLTYPE_FLOAT)]
return columns
def get_categories(self, pipeline, object_name):
"""Return the categories of measurements that this module produces
object_name - return measurements made on this object (or 'Image' for image measurements)
"""
result = super(IdentifyYeastCells, self).get_categories(pipeline, object_name)
result += [C_OBJECT_FEATURES]
return result
def get_measurements(self, pipeline, object_name, category):
"""Return the measurements that this module produces
object_name - return measurements made on this object (or 'Image' for image measurements)
category - return measurements made in this category
"""
result = super(IdentifyYeastCells, self).get_measurements(pipeline, object_name, category)
if category == C_OBJECT_FEATURES:
result += [FTR_OBJECT_QUALITY]
return result
ui_to_precision_map = dict([(1, 9), (2, 11), (3, 12), (4, 13), (5, 15)])
precision_to_ui_map = dict([(v, k) for k, v in ui_to_precision_map.items()] + [(10, 2)])
@property
def decoded_segmentation_precision_value(self):
return self.ui_to_precision_map[self.segmentation_precision.value]
@memory_profile
def run(self, workspace):
input_image_name = self.input_image_name.value
self.current_workspace = workspace
image_set = workspace.image_set
# same image_set for fitting
self.fitting_image_set = image_set
#
# Load images from workspace
#
input_pixels, background_pixels, ignore_mask_pixels = self.get_input_images_from_image_set(image_set)
input_image = image_set.get_image(input_image_name, must_be_grayscale=True)
self.input_image_file_name = input_image.file_name
self.current_workspace.display_data.input_pixels = input_pixels
#
# Preprocessing
#
input_pixels, background_pixels, ignore_mask_pixels = self.preprocess_images(input_pixels, background_pixels,
ignore_mask_pixels)
#
# Segmentation
#
objects, objects_qualities, background_pixels = self.segmentation(input_pixels, background_pixels,
ignore_mask_pixels)
objects.parent_image = input_image
if self.__get(F_BACKGROUND, workspace, None) is None and self.background_elimination_strategy == BKG_FIRST:
self.__set(F_BACKGROUND, workspace, background_pixels)
workspace.object_set.add_objects(objects, self.object_name.value)
# Save measurements
workspace.measurements.add_measurement(self.object_name.value, M_OBJECT_FEATURES_OBJECT_QUALITY,
objects_qualities)
super(IdentifyYeastCells, self).add_measurements(workspace)
def set_params_from_ui(self, params):
def update_params(next_name, first_name, random_name, ui_value):
params["segmentation"]["seeding"]["from"][next_name] = ui_value >= 1 or ui_value <= 0.5
params["segmentation"]["seeding"]["from"][first_name] = ui_value > 0.5
params["segmentation"]["seeding"]["from"][random_name] = max(0, ui_value - 1)
params_seeding = params["segmentation"]["seeding"]["from"]
params["segmentation"]["steps"] = self.iterations.value
update_params("cellBorderRemovingCurrSegments", "cellBorder", "cellBorderRandom", self.seeds_border.value)
update_params("cellContentRemovingCurrSegments", "cellContent", "cellContentRandom", self.seeds_content.value)
params_seeding["cellBorderRemovingCurrSegmentsRandom"] = params_seeding["cellBorderRandom"]
params_seeding["cellContentRemovingCurrSegmentsRandom"] = params_seeding["cellContentRandom"]
params["segmentation"]["seeding"]["from"]["snakesCentroids"] = self.seeds_centroid.value > 0.0
params["segmentation"]["seeding"]["from"]["snakesCentroidsRandom"] = max(0, self.seeds_centroid.value - 1)
params["segmentation"]["stars"]["points"] = self.contour_points.value
params["segmentation"]["stars"]["step"] = 1.0 / self.contour_precision.value
default_size_weight_average = np.average(params["segmentation"]["stars"]["sizeWeight"])
params["segmentation"]["stars"]["sizeWeight"] = list(
create_size_weights(default_size_weight_average, self.weights_number.value)
)
def get_ui_params_from_precision(self, ui_precision):
def params_to_ui(params_seeding, next_name, first_name, random_name):
random = params_seeding[random_name]
next = params_seeding[next_name]
first = params_seeding[first_name]
if random != 0:
return random + 1
elif next and first:
return 1
elif first:
return 0.7
elif next:
return 0.5
return 0.0
def override_random_seeds_number(precision, value):
if precision == 2:
return max(value, 1.2)
elif precision >= 3:
return max(value, 1.5)
return value
params = default_parameters(self.ui_to_precision_map[ui_precision], 30)
ui_params = [params["segmentation"]["steps"]]
params_seeding = params["segmentation"]["seeding"]["from"]
ui_params.append(override_random_seeds_number(ui_precision,
params_to_ui(params_seeding, "cellBorderRemovingCurrSegments", "cellBorder", "cellBorderRandom")))
ui_params.append(override_random_seeds_number(ui_precision,
params_to_ui(params_seeding, "cellContentRemovingCurrSegments", "cellContent", "cellContentRandom")))
ui_params.append(override_random_seeds_number(ui_precision,
params_to_ui(params_seeding, "snakesCentroids", "snakesCentroids", "snakesCentroidsRandom")))
ui_params.append(params["segmentation"]["stars"]["points"])
ui_params.append(1.0 / params["segmentation"]["stars"]["step"])
ui_params.append(len(params["segmentation"]["stars"]["sizeWeight"]))
return ui_params
def set_ui_from_precision(self, ui_precision):
ui_precision_values = self.get_ui_params_from_precision(ui_precision)
ui_precision_settings = self.settings()[self.PRECISION_PARAMS_START:self.PRECISION_PARAMS_END + 1]
for setting, value in zip(ui_precision_settings, ui_precision_values):
setting.value = value
def prepare_cell_star_object(self, segmentation_precision):
cellstar = Segmentation(segmentation_precision, self.average_cell_diameter.value)
cellstar.parameters["segmentation"]["maxOverlap"] = self.maximal_cell_overlap.value
self.set_params_from_ui(cellstar.parameters)
if self.advanced_cell_filtering.value:
def calculate_area_multiplier(area):
return 4.0 * area / self.average_cell_diameter.value ** 2 / math.pi
# def calculate_size_multiplier(area):
# return calculate_area_multiplier(area) ** 0.5
areas_range = self.min_cell_area.value, self.max_cell_area.value
cellstar.parameters["segmentation"]["minArea"] = max(cellstar.parameters["segmentation"]["minArea"],
calculate_area_multiplier(areas_range[0]))
cellstar.parameters["segmentation"]["maxArea"] = calculate_area_multiplier(areas_range[1])
# to some extent change length of rays
# cellstar.parameters["segmentation"]["stars"]["maxSize"] = max(cellstar.parameters["segmentation"]["stars"]["maxSize"], min(2.5, calculate_size_multiplier(areas_range[1])))
success = cellstar.decode_auto_params(self.autoadapted_params.value)
if not success: # if current value is invalid overwrite it with current settings
self.autoadapted_params.value = cellstar.encode_auto_params()
return cellstar
@staticmethod
def normalized_log(image, sigma):
'''Perform the Laplacian of Gaussian transform on the image
image - 2-d image array
mask - binary mask of significant pixels
size - length of side of square kernel to use
sigma - standard deviation of the Gaussian
'''
size = int(sigma * 4) + 1
half_size = size / 2
i, j = np.mgrid[-half_size:half_size + 1,
-half_size:half_size + 1].astype(float) / float(sigma)
distance = (i ** 2 + j ** 2) / 2
gaussian = np.exp(-distance)
#
# Normalize the Gaussian
#
gaussian = gaussian / np.sum(gaussian)
log = (distance - 1) * gaussian
#
# Normalize the kernel to have a sum of zero
#
log_norm = log - np.mean(log)
output = scipy.ndimage.convolve(image, log_norm, mode='nearest')
return output
def preprocess_images(self, input_image, background_image, ignore_mask):
# Invert images if required.
if not self.background_brighter_then_cell_inside:
input_image = 1 - input_image
if background_image is not None:
background_image = 1 - background_image
# support for fluorescent images
# here it is design question: we assume that the user *should* say
# truth about background and inside of cells: insides are brighter
# than background (so the bkg and image for fluorescent will be
# inverted at this stage)
if not self.bright_field_image:
sigma = 4
factor = 10
edge_pixels = IdentifyYeastCells.normalized_log(input_image, sigma)
input_image = np.subtract(input_image, factor * edge_pixels)
return input_image, background_image, ignore_mask
#
# Segmentation of the image into yeast cells.
# Returns: yeast cells, yeast cells qualities, background
#
def segmentation(self, normalized_image, background_pixels, ignore_mask_pixels=None):
cellstar = self.prepare_cell_star_object(self.decoded_segmentation_precision_value)
if self.input_image_file_name is not None:
dedicated_image_folder = pj(pref.get_default_output_directory(), self.input_image_file_name)
if dedicated_image_folder is not None:
cellstar.debug_output_image_path = dedicated_image_folder
cellstar.set_frame(normalized_image)
cellstar.set_background(background_pixels)
cellstar.set_mask(ignore_mask_pixels)
segmented_image, snakes = cellstar.run_segmentation()
objects = cellprofiler.object.Objects()
objects.segmented = segmented_image
objects.unedited_segmented = segmented_image
objects.small_removed_segmented = np.zeros(normalized_image.shape)
self.current_workspace.display_data.segmentation_pixels = objects.segmented
raw_qualities = [-s.rank for s in snakes]
if not raw_qualities == []:
raw_interval = min(raw_qualities), max(raw_qualities)
logger.info("Qualities are in interval [%.3f,%.3f]." % raw_interval)
return objects, np.array(raw_qualities), cellstar.images.background
def fit_parameters(self, input_image, background_image, ignore_mask_image, ground_truth_labels, number_of_steps,
update_callback, wait_callback):
"""
:param wait_callback: function that wait and potentially updates UI
:param update_callback: function that take number of steps completed and return if fitting should be continued
"""
keep_going = True
self.param_fit_progress = 0
self.best_snake_score = 10
self.best_rank_score = 1000000000
aft_active = []
adaptations_stopped = False
cellstar = self.prepare_cell_star_object(min(11, self.decoded_segmentation_precision_value))
self.best_parameters = cellstar.parameters
self.autoadapted_params.value = cellstar.encode_auto_params()
try:
while (keep_going or not adaptations_stopped) and self.param_fit_progress < number_of_steps:
# here put one it. of fitting instead
wait_callback(0.5)
# Thread ended with exception so optimisation have to be stopped.
if any(aft_active) and aft_active[0].exception is not None:
break
# Clean aft_active from dead threads.
while any(aft_active) and (not aft_active[0].is_alive() and aft_active[0].started):
aft_active = aft_active[1:]
# If thread in line start first of them.
if any(aft_active) and not aft_active[0].started:
# update parameters which may already be changed
aft_active[0].update_params(self.best_parameters)
aft_active[0].start()
adaptations_stopped = aft_active == []
if adaptations_stopped and keep_going and self.param_fit_progress < number_of_steps:
aft_active.append(
AutoFitterThread(run_pf, self.update_snake_params,
input_image, background_image, ignore_mask_image, ground_truth_labels,
self.best_parameters,
self.decoded_segmentation_precision_value, self.average_cell_diameter.value,
self.update_partial_iteration_progress))
aft_active.append(
AutoFitterThread(run_rank_pf, self.update_rank_params,
input_image, background_image, ignore_mask_image, ground_truth_labels,
self.best_parameters,
self.update_partial_iteration_progress))
# here update params. in the GUI
keep_going_update = update_callback(self.param_fit_progress + self.param_fit_progress_partial)
keep_going = keep_going and keep_going_update
finally:
update_callback(number_of_steps)
def fit_parameters_with_ui(self, input_image, background_image, ignore_mask_image, ground_truth_labels):
import wx
# reading GT from dialog_box.labels[0] and image from self.pixel
progress_max = self.autoadaptation_steps.value * 2 # every step consists of: snake params and ranking params fitting
with wx.ProgressDialog("Fitting parameters..", "Iterations remaining", progress_max * 100,
# show percents of change
style=wx.PD_CAN_ABORT | wx.PD_ELAPSED_TIME | wx.PD_REMAINING_TIME) as dialog:
def update(steps):
return dialog.Update(steps * 100)[0]
def wait(time):
return wx.Sleep(int(time + 0.5))
self.fit_parameters(input_image, background_image, ignore_mask_image, ground_truth_labels, progress_max,
update, wait)
def get_param_fitting_input_images_from_image_set(self):
"""
Try to load images from current workspace. Can be used when fitting is called in test run after segmentation has been
run at least once.
"""
if self.fitting_image_set is None:
return None
images = self.get_input_images_from_image_set(self.fitting_image_set)
if images is None:
return None
return images + (None,)
def get_input_images_from_image_set(self, image_set):
try:
background_needed = self.background_elimination_strategy == BKG_FILE
ignore_mask_needed = self.ignore_mask_image_name.value != cps.LEAVE_BLANK
background_image = None
ignore_mask = None
# load images from workspace
input_image = image_set.get_image(self.input_image_name.value, must_be_grayscale=True).pixel_data
if background_needed:
background_image = image_set.get_image(self.background_image_name.value,
must_be_grayscale=True).pixel_data
if ignore_mask_needed:
ignore_mask_image = image_set.get_image(self.ignore_mask_image_name)
ignore_mask = ignore_mask_image.pixel_data > 0
return input_image, background_image, ignore_mask
except Exception as ex:
logger.info("Could not use image from workspace.image_set because: " + str(ex))
return None
@staticmethod
def load_image_grayscale(path):
image = bioformats.load_image(path)
if image.ndim == 3:
image = np.sum(image, 2) / image.shape[2]
return image
def get_param_fitting_input_images_from_user(self):
import wx
def get_file_path(message):
with wx.FileDialog(None,
message=message,
wildcard="Image file (*.tif,*.tiff,*.jpg,*.jpeg,*.png,*.gif,*.bmp)|*.tif;*.tiff;*.jpg;*.jpeg;*.png;*.gif;*.bmp|*.* (all files)|*.*",
style=wx.FD_OPEN) as dlg:
if dlg.ShowModal() == wx.ID_OK:
return dlg.Path
else:
return None
input_image = None
background_image = None
ignore_mask = None
labels = None
background_needed = self.background_elimination_strategy == BKG_FILE
ignore_mask_needed = self.ignore_mask_image_name.value != cps.LEAVE_BLANK
message = "In order to autoadapt parameters you need to provide:\n- input image"
if background_needed:
message += "\n- background image"
if ignore_mask_needed:
message += "\n- ignore mask image"
with wx.MessageDialog(None, message, "Select images for autoadapt", wx.OK | wx.ICON_INFORMATION) as dlg:
dlg.ShowModal()
input_image_path = get_file_path("Select sample input image")
if input_image_path is None:
return None
else:
input_image = self.load_image_grayscale(input_image_path)
label_path = input_image_path + ".lab.png" # if file attached load labels from file
if isfile(label_path):
labels = (self.load_image_grayscale(label_path) * 255).astype(int)
if background_needed:
background_image_path = get_file_path("Select background image")
if background_image_path is None:
return None
else: