celldetective 1.0.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. celldetective/__init__.py +2 -0
  2. celldetective/__main__.py +432 -0
  3. celldetective/datasets/segmentation_annotations/blank +0 -0
  4. celldetective/datasets/signal_annotations/blank +0 -0
  5. celldetective/events.py +149 -0
  6. celldetective/extra_properties.py +100 -0
  7. celldetective/filters.py +89 -0
  8. celldetective/gui/__init__.py +20 -0
  9. celldetective/gui/about.py +44 -0
  10. celldetective/gui/analyze_block.py +563 -0
  11. celldetective/gui/btrack_options.py +898 -0
  12. celldetective/gui/classifier_widget.py +386 -0
  13. celldetective/gui/configure_new_exp.py +532 -0
  14. celldetective/gui/control_panel.py +438 -0
  15. celldetective/gui/gui_utils.py +495 -0
  16. celldetective/gui/json_readers.py +113 -0
  17. celldetective/gui/measurement_options.py +1425 -0
  18. celldetective/gui/neighborhood_options.py +452 -0
  19. celldetective/gui/plot_signals_ui.py +1042 -0
  20. celldetective/gui/process_block.py +1055 -0
  21. celldetective/gui/retrain_segmentation_model_options.py +706 -0
  22. celldetective/gui/retrain_signal_model_options.py +643 -0
  23. celldetective/gui/seg_model_loader.py +460 -0
  24. celldetective/gui/signal_annotator.py +2388 -0
  25. celldetective/gui/signal_annotator_options.py +340 -0
  26. celldetective/gui/styles.py +217 -0
  27. celldetective/gui/survival_ui.py +903 -0
  28. celldetective/gui/tableUI.py +608 -0
  29. celldetective/gui/thresholds_gui.py +1300 -0
  30. celldetective/icons/logo-large.png +0 -0
  31. celldetective/icons/logo.png +0 -0
  32. celldetective/icons/signals_icon.png +0 -0
  33. celldetective/icons/splash-test.png +0 -0
  34. celldetective/icons/splash.png +0 -0
  35. celldetective/icons/splash0.png +0 -0
  36. celldetective/icons/survival2.png +0 -0
  37. celldetective/icons/vignette_signals2.png +0 -0
  38. celldetective/icons/vignette_signals2.svg +114 -0
  39. celldetective/io.py +2050 -0
  40. celldetective/links/zenodo.json +561 -0
  41. celldetective/measure.py +1258 -0
  42. celldetective/models/segmentation_effectors/blank +0 -0
  43. celldetective/models/segmentation_generic/blank +0 -0
  44. celldetective/models/segmentation_targets/blank +0 -0
  45. celldetective/models/signal_detection/blank +0 -0
  46. celldetective/models/tracking_configs/mcf7.json +68 -0
  47. celldetective/models/tracking_configs/ricm.json +203 -0
  48. celldetective/models/tracking_configs/ricm2.json +203 -0
  49. celldetective/neighborhood.py +717 -0
  50. celldetective/scripts/analyze_signals.py +51 -0
  51. celldetective/scripts/measure_cells.py +275 -0
  52. celldetective/scripts/segment_cells.py +212 -0
  53. celldetective/scripts/segment_cells_thresholds.py +140 -0
  54. celldetective/scripts/track_cells.py +206 -0
  55. celldetective/scripts/train_segmentation_model.py +246 -0
  56. celldetective/scripts/train_signal_model.py +49 -0
  57. celldetective/segmentation.py +712 -0
  58. celldetective/signals.py +2826 -0
  59. celldetective/tracking.py +974 -0
  60. celldetective/utils.py +1681 -0
  61. celldetective-1.0.2.dist-info/LICENSE +674 -0
  62. celldetective-1.0.2.dist-info/METADATA +192 -0
  63. celldetective-1.0.2.dist-info/RECORD +66 -0
  64. celldetective-1.0.2.dist-info/WHEEL +5 -0
  65. celldetective-1.0.2.dist-info/entry_points.txt +2 -0
  66. celldetective-1.0.2.dist-info/top_level.txt +1 -0
@@ -0,0 +1,706 @@
1
+ from PyQt5.QtWidgets import QMainWindow, QApplication,QRadioButton, QMessageBox, QScrollArea, QComboBox, QFrame, QCheckBox, QFileDialog, QGridLayout, QTextEdit, QLineEdit, QVBoxLayout, QWidget, QLabel, QHBoxLayout, QPushButton
2
+ from PyQt5.QtCore import Qt, QSize
3
+ from PyQt5.QtGui import QDoubleValidator, QIntValidator, QIcon
4
+ from celldetective.gui.gui_utils import center_window, FeatureChoice, ListWidget, QHSeperationLine, FigureCanvas, GeometryChoice, OperationChoice
5
+ from superqt import QLabeledDoubleRangeSlider, QLabeledDoubleSlider,QLabeledSlider
6
+ from superqt.fonticon import icon
7
+ from fonticon_mdi6 import MDI6
8
+ from celldetective.utils import extract_experiment_channels, get_software_location
9
+ from celldetective.io import interpret_tracking_configuration, load_frames, get_segmentation_datasets_list, locate_segmentation_dataset
10
+ from celldetective.measure import compute_haralick_features, contour_of_instance_segmentation
11
+ from celldetective.segmentation import train_segmentation_model
12
+ import numpy as np
13
+ import json
14
+ from shutil import copyfile
15
+ import os
16
+ import matplotlib.pyplot as plt
17
+ from mpl_toolkits.axes_grid1 import make_axes_locatable
18
+ from glob import glob
19
+ from natsort import natsorted
20
+ from tifffile import imread
21
+ from pathlib import Path, PurePath
22
+ from datetime import datetime
23
+ from functools import partial
24
+
25
+ class ConfigSegmentationModelTraining(QMainWindow):
26
+
27
+ """
28
+ UI to set segmentation model training instructions.
29
+
30
+ """
31
+
32
+ def __init__(self, parent=None):
33
+
34
+ super().__init__()
35
+ self.parent = parent
36
+ self.setWindowTitle("Train segmentation model")
37
+ self.setWindowIcon(QIcon(os.sep.join(['celldetective','icons','mexican-hat.png'])))
38
+ self.mode = self.parent.mode
39
+ self.exp_dir = self.parent.exp_dir
40
+ self.soft_path = get_software_location()
41
+ self.pretrained_model = None
42
+ self.dataset_folder = None
43
+ self.software_models_dir = os.sep.join([self.soft_path, 'celldetective', 'models', f'segmentation_{self.mode}'])
44
+
45
+ self.onlyFloat = QDoubleValidator()
46
+ self.onlyInt = QIntValidator()
47
+
48
+ self.screen_height = self.parent.parent.parent.screen_height
49
+ center_window(self)
50
+
51
+ self.setMinimumWidth(500)
52
+ self.setMinimumHeight(int(0.3*self.screen_height))
53
+ self.setMaximumHeight(int(0.8*self.screen_height))
54
+ self.populate_widget()
55
+ #self.load_previous_measurement_instructions()
56
+
57
+ def populate_widget(self):
58
+
59
+ """
60
+ Create the multibox design.
61
+
62
+ """
63
+
64
+ # Create button widget and layout
65
+ self.scroll_area = QScrollArea(self)
66
+ self.button_widget = QWidget()
67
+ main_layout = QVBoxLayout()
68
+ self.button_widget.setLayout(main_layout)
69
+ main_layout.setContentsMargins(30,30,30,30)
70
+
71
+ # first frame for FEATURES
72
+ self.model_frame = QFrame()
73
+ self.model_frame.setFrameStyle(QFrame.StyledPanel | QFrame.Raised)
74
+ self.populate_model_frame()
75
+ main_layout.addWidget(self.model_frame)
76
+
77
+ self.data_frame = QFrame()
78
+ self.data_frame.setFrameStyle(QFrame.StyledPanel | QFrame.Raised)
79
+ self.populate_data_frame()
80
+ main_layout.addWidget(self.data_frame)
81
+
82
+ self.hyper_frame = QFrame()
83
+ self.hyper_frame.setFrameStyle(QFrame.StyledPanel | QFrame.Raised)
84
+ self.populate_hyper_frame()
85
+ main_layout.addWidget(self.hyper_frame)
86
+
87
+ self.submit_btn = QPushButton('Train')
88
+ self.submit_btn.setStyleSheet(self.parent.parent.parent.button_style_sheet)
89
+ self.submit_btn.clicked.connect(self.prep_model)
90
+ main_layout.addWidget(self.submit_btn)
91
+ self.submit_btn.setEnabled(False)
92
+
93
+ #self.populate_left_panel()
94
+ #grid.addLayout(self.left_side, 0, 0, 1, 1)
95
+ self.button_widget.adjustSize()
96
+
97
+ self.scroll_area.setAlignment(Qt.AlignCenter)
98
+ self.scroll_area.setWidget(self.button_widget)
99
+ self.scroll_area.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
100
+ self.scroll_area.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
101
+ self.scroll_area.setWidgetResizable(True)
102
+ self.setCentralWidget(self.scroll_area)
103
+ self.show()
104
+
105
+ QApplication.processEvents()
106
+ self.adjustScrollArea()
107
+
108
+ def populate_hyper_frame(self):
109
+
110
+ """
111
+ Add widgets and layout in the POST-PROCESSING frame.
112
+ """
113
+
114
+ grid = QGridLayout(self.hyper_frame)
115
+ grid.setContentsMargins(30,30,30,30)
116
+ grid.setSpacing(30)
117
+
118
+ self.hyper_lbl = QLabel("HYPERPARAMETERS")
119
+ self.hyper_lbl.setStyleSheet("""
120
+ font-weight: bold;
121
+ padding: 0px;
122
+ """)
123
+ grid.addWidget(self.hyper_lbl, 0, 0, 1, 4, alignment=Qt.AlignCenter)
124
+ self.generate_hyper_contents()
125
+ grid.addWidget(self.ContentsHyper, 1, 0, 1, 4, alignment=Qt.AlignTop)
126
+
127
+ def generate_hyper_contents(self):
128
+
129
+ self.ContentsHyper = QFrame()
130
+ layout = QVBoxLayout(self.ContentsHyper)
131
+ layout.setContentsMargins(0,0,0,0)
132
+
133
+ lr_layout = QHBoxLayout()
134
+ lr_layout.addWidget(QLabel('learning rate: '),30)
135
+ self.lr_le = QLineEdit('0,01')
136
+ self.lr_le.setValidator(self.onlyFloat)
137
+ lr_layout.addWidget(self.lr_le, 70)
138
+ layout.addLayout(lr_layout)
139
+
140
+ bs_layout = QHBoxLayout()
141
+ bs_layout.addWidget(QLabel('batch size: '),30)
142
+ self.bs_le = QLineEdit('4')
143
+ self.bs_le.setValidator(self.onlyInt)
144
+ bs_layout.addWidget(self.bs_le, 70)
145
+ layout.addLayout(bs_layout)
146
+
147
+ epochs_layout = QHBoxLayout()
148
+ epochs_layout.addWidget(QLabel('# epochs: '), 30)
149
+ self.epochs_slider = QLabeledSlider()
150
+ self.epochs_slider.setRange(1,300)
151
+ self.epochs_slider.setSingleStep(1)
152
+ self.epochs_slider.setTickInterval(1)
153
+ self.epochs_slider.setOrientation(1)
154
+ self.epochs_slider.setValue(100)
155
+ epochs_layout.addWidget(self.epochs_slider, 70)
156
+ layout.addLayout(epochs_layout)
157
+
158
+ self.stardist_model.clicked.connect(self.rescale_slider)
159
+ self.cellpose_model.clicked.connect(self.rescale_slider)
160
+
161
+ def populate_data_frame(self):
162
+
163
+ """
164
+ Add widgets and layout in the POST-PROCESSING frame.
165
+ """
166
+
167
+ grid = QGridLayout(self.data_frame)
168
+ grid.setContentsMargins(30,30,30,30)
169
+ grid.setSpacing(30)
170
+
171
+ self.data_lbl = QLabel("DATA")
172
+ self.data_lbl.setStyleSheet("""
173
+ font-weight: bold;
174
+ padding: 0px;
175
+ """)
176
+ grid.addWidget(self.data_lbl, 0, 0, 1, 4, alignment=Qt.AlignCenter)
177
+ self.generate_data_contents()
178
+ grid.addWidget(self.ContentsData, 1, 0, 1, 4, alignment=Qt.AlignTop)
179
+
180
+ def populate_model_frame(self):
181
+
182
+ """
183
+ Add widgets and layout in the FEATURES frame.
184
+ """
185
+
186
+ grid = QGridLayout(self.model_frame)
187
+ grid.setContentsMargins(30,30,30,30)
188
+ grid.setSpacing(30)
189
+
190
+ self.model_lbl = QLabel("MODEL")
191
+ self.model_lbl.setStyleSheet("""
192
+ font-weight: bold;
193
+ padding: 0px;
194
+ """)
195
+ grid.addWidget(self.model_lbl, 0, 0, 1, 4, alignment=Qt.AlignCenter)
196
+
197
+ self.generate_model_panel_contents()
198
+ grid.addWidget(self.ContentsModel, 1, 0, 1, 4, alignment=Qt.AlignTop)
199
+
200
+
201
+ def generate_data_contents(self):
202
+
203
+ self.ContentsData = QFrame()
204
+ layout = QVBoxLayout(self.ContentsData)
205
+ layout.setContentsMargins(0,0,0,0)
206
+
207
+ train_data_layout = QHBoxLayout()
208
+ train_data_layout.addWidget(QLabel('Training data: '), 30)
209
+ self.select_data_folder_btn = QPushButton('Choose folder')
210
+ self.select_data_folder_btn.clicked.connect(self.showDialog_dataset)
211
+ self.data_folder_label = QLabel('No folder chosen')
212
+ train_data_layout.addWidget(self.select_data_folder_btn, 35)
213
+ train_data_layout.addWidget(self.data_folder_label, 30)
214
+
215
+ self.cancel_dataset = QPushButton()
216
+ self.cancel_dataset.setIcon(icon(MDI6.close,color="black"))
217
+ self.cancel_dataset.clicked.connect(self.clear_dataset)
218
+ self.cancel_dataset.setStyleSheet(self.parent.parent.parent.button_select_all)
219
+ self.cancel_dataset.setIconSize(QSize(20, 20))
220
+ self.cancel_dataset.setVisible(False)
221
+ train_data_layout.addWidget(self.cancel_dataset, 5)
222
+
223
+
224
+ layout.addLayout(train_data_layout)
225
+
226
+ include_dataset_layout = QHBoxLayout()
227
+ include_dataset_layout.addWidget(QLabel('include dataset: '),30)
228
+ self.dataset_cb = QComboBox()
229
+ available_datasets, self.datasets_path = get_segmentation_datasets_list(return_path=True)
230
+ signal_datasets = ['--'] + available_datasets #[d.split('/')[-2] for d in available_datasets]
231
+ self.dataset_cb.addItems(signal_datasets)
232
+ include_dataset_layout.addWidget(self.dataset_cb, 70)
233
+ layout.addLayout(include_dataset_layout)
234
+
235
+ augmentation_hbox = QHBoxLayout()
236
+ augmentation_hbox.addWidget(QLabel('augmentation\nfactor: '), 30)
237
+ self.augmentation_slider = QLabeledDoubleSlider()
238
+ self.augmentation_slider.setSingleStep(0.01)
239
+ self.augmentation_slider.setTickInterval(0.01)
240
+ self.augmentation_slider.setOrientation(1)
241
+ self.augmentation_slider.setRange(1, 5)
242
+ self.augmentation_slider.setValue(1.5)
243
+
244
+ augmentation_hbox.addWidget(self.augmentation_slider, 70)
245
+ layout.addLayout(augmentation_hbox)
246
+
247
+ validation_split_layout = QHBoxLayout()
248
+ validation_split_layout.addWidget(QLabel('validation split: '),30)
249
+ self.validation_slider = QLabeledDoubleSlider()
250
+ self.validation_slider.setSingleStep(0.01)
251
+ self.validation_slider.setTickInterval(0.01)
252
+ self.validation_slider.setOrientation(1)
253
+ self.validation_slider.setRange(0,0.9)
254
+ self.validation_slider.setValue(0.25)
255
+ validation_split_layout.addWidget(self.validation_slider, 70)
256
+ layout.addLayout(validation_split_layout)
257
+
258
+
259
+ def generate_model_panel_contents(self):
260
+
261
+ self.ContentsModel = QFrame()
262
+ layout = QVBoxLayout(self.ContentsModel)
263
+ layout.setContentsMargins(0,0,0,0)
264
+
265
+ model_type_layout = QHBoxLayout()
266
+ model_type_layout.setContentsMargins(30,5,30,15)
267
+ self.cellpose_model = QRadioButton('Cellpose')
268
+ self.stardist_model = QRadioButton('StarDist')
269
+ self.stardist_model.setChecked(True)
270
+ model_type_layout.addWidget(self.stardist_model,50, alignment=Qt.AlignCenter)
271
+ model_type_layout.addWidget(self.cellpose_model,50, alignment=Qt.AlignCenter)
272
+ layout.addLayout(model_type_layout)
273
+
274
+ modelname_layout = QHBoxLayout()
275
+ modelname_layout.addWidget(QLabel('Model name: '), 30)
276
+ self.modelname_le = QLineEdit()
277
+ self.modelname_le.setText(f"Untitled_model_{datetime.today().strftime('%Y-%m-%d')}")
278
+ modelname_layout.addWidget(self.modelname_le, 70)
279
+ layout.addLayout(modelname_layout)
280
+
281
+ pretrained_layout = QHBoxLayout()
282
+ pretrained_layout.setContentsMargins(0,0,0,0)
283
+ pretrained_layout.addWidget(QLabel('Pretrained model: '), 30)
284
+
285
+ self.browse_pretrained_btn = QPushButton('Choose folder')
286
+ self.browse_pretrained_btn.clicked.connect(self.showDialog_pretrained)
287
+ pretrained_layout.addWidget(self.browse_pretrained_btn, 35)
288
+
289
+ self.pretrained_lbl = QLabel('No folder chosen')
290
+ pretrained_layout.addWidget(self.pretrained_lbl, 30)
291
+
292
+ self.cancel_pretrained = QPushButton()
293
+ self.cancel_pretrained.setIcon(icon(MDI6.close,color="black"))
294
+ self.cancel_pretrained.clicked.connect(self.clear_pretrained)
295
+ self.cancel_pretrained.setStyleSheet(self.parent.parent.parent.button_select_all)
296
+ self.cancel_pretrained.setIconSize(QSize(20, 20))
297
+ self.cancel_pretrained.setVisible(False)
298
+ pretrained_layout.addWidget(self.cancel_pretrained, 5)
299
+
300
+ layout.addLayout(pretrained_layout)
301
+
302
+ # recompile_layout = QHBoxLayout()
303
+ # recompile_layout.addWidget(QLabel('Recompile: '), 30)
304
+ # self.recompile_option = QCheckBox()
305
+ # self.recompile_option.setEnabled(False)
306
+ # recompile_layout.addWidget(self.recompile_option, 70)
307
+ # layout.addLayout(recompile_layout)
308
+
309
+ self.max_nbr_channels = 5
310
+ self.channel_cbs = [QComboBox() for i in range(self.max_nbr_channels)]
311
+ self.normalization_mode_btns = [QPushButton('') for i in range(self.max_nbr_channels)]
312
+ self.normalization_mode = [True for i in range(self.max_nbr_channels)]
313
+
314
+ self.normalization_clip_btns = [QPushButton('') for i in range(self.max_nbr_channels)]
315
+ self.clip_option = [False for i in range(self.max_nbr_channels)]
316
+
317
+ for i in range(self.max_nbr_channels):
318
+
319
+ self.normalization_mode_btns[i].setIcon(icon(MDI6.percent_circle,color="#1565c0"))
320
+ self.normalization_mode_btns[i].setIconSize(QSize(20, 20))
321
+ self.normalization_mode_btns[i].setStyleSheet(self.parent.parent.parent.button_select_all)
322
+ self.normalization_mode_btns[i].setToolTip("Switch to absolute normalization values.")
323
+ self.normalization_mode_btns[i].clicked.connect(partial(self.switch_normalization_mode, i))
324
+
325
+ self.normalization_clip_btns[i].setIcon(icon(MDI6.content_cut,color="black"))
326
+ self.normalization_clip_btns[i].setIconSize(QSize(20, 20))
327
+ self.normalization_clip_btns[i].setStyleSheet(self.parent.parent.parent.button_select_all)
328
+ self.normalization_clip_btns[i].clicked.connect(partial(self.switch_clipping_mode, i))
329
+ self.normalization_clip_btns[i].setToolTip('clip')
330
+
331
+ self.normalization_min_value_lbl = [QLabel('Min %: ') for i in range(self.max_nbr_channels)]
332
+ self.normalization_min_value_le = [QLineEdit('0.1') for i in range(self.max_nbr_channels)]
333
+
334
+ self.normalization_max_value_lbl = [QLabel('Max %: ') for i in range(self.max_nbr_channels)]
335
+ self.normalization_max_value_le = [QLineEdit('99.99') for i in range(self.max_nbr_channels)]
336
+
337
+ self.channel_items = ['--', 'brightfield_channel', 'live_nuclei_channel', 'dead_nuclei_channel',
338
+ 'effector_fluo_channel', 'adhesion_channel', 'fluo_channel_1', 'fluo_channel_2','None'
339
+ ]
340
+ exp_ch = self.parent.parent.exp_channels
341
+ for c in exp_ch:
342
+ if c not in self.channel_items:
343
+ self.channel_items.append(c)
344
+
345
+ self.channel_option_layouts = []
346
+ for i in range(len(self.channel_cbs)):
347
+ ch_layout = QHBoxLayout()
348
+ ch_layout.addWidget(QLabel(f'channel {i}: '), 30)
349
+ self.channel_cbs[i].addItems(self.channel_items)
350
+ self.channel_cbs[i].currentIndexChanged.connect(self.check_valid_channels)
351
+ ch_layout.addWidget(self.channel_cbs[i], 70)
352
+ layout.addLayout(ch_layout)
353
+
354
+ channel_norm_options_layout = QHBoxLayout()
355
+ channel_norm_options_layout.setContentsMargins(130,0,0,0)
356
+ channel_norm_options_layout.addWidget(self.normalization_min_value_lbl[i])
357
+ channel_norm_options_layout.addWidget(self.normalization_min_value_le[i])
358
+ channel_norm_options_layout.addWidget(self.normalization_max_value_lbl[i])
359
+ channel_norm_options_layout.addWidget(self.normalization_max_value_le[i])
360
+ channel_norm_options_layout.addWidget(self.normalization_clip_btns[i])
361
+ channel_norm_options_layout.addWidget(self.normalization_mode_btns[i])
362
+ layout.addLayout(channel_norm_options_layout)
363
+
364
+ # for i in range(self.max_nbr_channels):
365
+ # self.channel_cbs[i].currentIndexChanged.connect(partial(self.show_norm_options, i))
366
+
367
+ spatial_calib_layout = QHBoxLayout()
368
+ spatial_calib_layout.addWidget(QLabel('input spatial\ncalibration'), 30)
369
+ self.spatial_calib_le = QLineEdit('')
370
+ self.spatial_calib_le.setPlaceholderText('e.g. 0.1 µm per pixel')
371
+ spatial_calib_layout.addWidget(self.spatial_calib_le, 70)
372
+ layout.addLayout(spatial_calib_layout)
373
+
374
+
375
+ # model_length_layout = QHBoxLayout()
376
+ # model_length_layout.addWidget(QLabel('Max signal length: '), 30)
377
+ # self.model_length_slider = QLabeledSlider()
378
+ # self.model_length_slider.setSingleStep(1)
379
+ # self.model_length_slider.setTickInterval(1)
380
+ # self.model_length_slider.setSingleStep(1)
381
+ # self.model_length_slider.setOrientation(1)
382
+ # self.model_length_slider.setRange(0,1024)
383
+ # self.model_length_slider.setValue(128)
384
+ # model_length_layout.addWidget(self.model_length_slider, 70)
385
+ # layout.addLayout(model_length_layout)
386
+
387
+
388
+ def rescale_slider(self):
389
+ if self.stardist_model.isChecked():
390
+ self.epochs_slider.setRange(1,300)
391
+ else:
392
+ self.epochs_slider.setRange(1,3000)
393
+
394
+
395
+ def showDialog_pretrained(self):
396
+
397
+ try:
398
+ self.cancel_pretrained.click()
399
+ except:
400
+ pass
401
+
402
+ self.pretrained_model = QFileDialog.getExistingDirectory(
403
+ self, "Open Directory",
404
+ os.sep.join([self.soft_path, 'celldetective', 'models', f'segmentation_generic','']),
405
+ QFileDialog.ShowDirsOnly | QFileDialog.DontResolveSymlinks,
406
+ )
407
+
408
+ if self.pretrained_model is not None:
409
+
410
+ self.pretrained_model = self.pretrained_model.replace('\\','/')
411
+ self.pretrained_model = rf"{self.pretrained_model}"
412
+
413
+ subfiles = glob('/'.join([self.pretrained_model,"*"]))
414
+ subfiles = [s.replace('\\','/') for s in subfiles]
415
+ subfiles = [rf"{s}" for s in subfiles]
416
+
417
+ if '/'.join([self.pretrained_model,"config_input.json"]) in subfiles:
418
+ self.load_pretrained_config()
419
+ self.pretrained_lbl.setText(self.pretrained_model.split("/")[-1])
420
+ self.cancel_pretrained.setVisible(True)
421
+ #self.recompile_option.setEnabled(True)
422
+ self.modelname_le.setText(f"{self.pretrained_model.split('/')[-1]}_{datetime.today().strftime('%Y-%m-%d')}")
423
+ else:
424
+ self.pretrained_model = None
425
+ self.pretrained_lbl.setText('No folder chosen')
426
+ #self.recompile_option.setEnabled(False)
427
+ self.cancel_pretrained.setVisible(False)
428
+ print(self.pretrained_model)
429
+
430
+ self.seg_folder = self.pretrained_model.split('/')[-2]
431
+ self.model_name = self.pretrained_model.split('/')[-1]
432
+ if self.model_name.startswith('CP') and self.seg_folder=='segmentation_generic':
433
+
434
+ self.diamWidget = QWidget()
435
+ self.diamWidget.setWindowTitle('Estimate diameter')
436
+
437
+ layout = QVBoxLayout()
438
+ self.diamWidget.setLayout(layout)
439
+ self.diameter_le = QLineEdit('40')
440
+
441
+ hbox = QHBoxLayout()
442
+ hbox.addWidget(QLabel('diameter [px]: '), 33)
443
+ hbox.addWidget(self.diameter_le, 66)
444
+ layout.addLayout(hbox)
445
+
446
+ self.set_cellpose_scale_btn = QPushButton('set')
447
+ self.set_cellpose_scale_btn.clicked.connect(self.set_cellpose_scale)
448
+ layout.addWidget(self.set_cellpose_scale_btn)
449
+
450
+ self.diamWidget.show()
451
+ center_window(self.diamWidget)
452
+
453
+ def set_cellpose_scale(self):
454
+
455
+ scale = self.parent.parent.PxToUm * float(self.diameter_le.text()) / 30.0
456
+ if self.model_name=="CP_nuclei":
457
+ scale = self.parent.parent.PxToUm * float(self.diameter_le.text()) / 17.0
458
+ self.spatial_calib_le.setText(str(scale))
459
+ self.diamWidget.close()
460
+
461
+
462
+ def showDialog_dataset(self):
463
+
464
+ self.dataset_folder = QFileDialog.getExistingDirectory(
465
+ self, "Open Directory",
466
+ self.exp_dir,
467
+ QFileDialog.ShowDirsOnly | QFileDialog.DontResolveSymlinks,
468
+ )
469
+ if self.dataset_folder is not None:
470
+
471
+ subfiles = glob(self.dataset_folder+"/*.tif")
472
+ if len(subfiles)>0:
473
+ print(f'found {len(subfiles)} files in folder')
474
+ self.data_folder_label.setText(self.dataset_folder[:16]+'...')
475
+ self.data_folder_label.setToolTip(self.dataset_folder)
476
+ self.cancel_dataset.setVisible(True)
477
+ else:
478
+ self.data_folder_label.setText('No folder chosen')
479
+ self.data_folder_label.setToolTip('')
480
+ self.dataset_folder = None
481
+ self.cancel_dataset.setVisible(False)
482
+
483
+ def clear_pretrained(self):
484
+
485
+ self.pretrained_model = None
486
+ self.pretrained_lbl.setText('No folder chosen')
487
+ for i in range(len(self.channel_cbs)):
488
+ self.channel_cbs[i].setEnabled(True)
489
+ self.normalization_mode_btns[i].setEnabled(True)
490
+ self.normalization_max_value_le[i].setEnabled(True)
491
+ self.normalization_min_value_le[i].setEnabled(True)
492
+ self.normalization_clip_btns[i].setEnabled(True)
493
+ self.normalization_min_value_lbl[i].setEnabled(True)
494
+ self.normalization_max_value_lbl[i].setEnabled(True)
495
+
496
+ self.cancel_pretrained.setVisible(False)
497
+ self.modelname_le.setText(f"Untitled_model_{datetime.today().strftime('%Y-%m-%d')}")
498
+
499
+ def clear_dataset(self):
500
+
501
+ self.dataset_folder = None
502
+ self.data_folder_label.setText('No folder chosen')
503
+ self.data_folder_label.setToolTip('')
504
+ self.cancel_dataset.setVisible(False)
505
+
506
+
507
+ def load_pretrained_config(self):
508
+
509
+ f = open('/'.join([self.pretrained_model,"config_input.json"]))
510
+ data = json.load(f)
511
+ channels = data["channels"]
512
+ self.seg_folder = self.pretrained_model.split('/')[-2]
513
+ self.model_name = self.pretrained_model.split('/')[-1]
514
+ if self.model_name.startswith('CP') and self.seg_folder=='segmentation_generic':
515
+ channels = ['brightfield_channel', 'live_nuclei_channel']
516
+ if self.model_name=="CP_nuclei":
517
+ channels = ['live_nuclei_channel', 'None']
518
+ if self.model_name.startswith('SD') and self.seg_folder=='segmentation_generic':
519
+ channels = ['live_nuclei_channel']
520
+ if self.model_name=="SD_versatile_he":
521
+ channels = ["H&E_1","H&E_2","H&E_3"]
522
+
523
+ normalization_percentile = data['normalization_percentile']
524
+ normalization_clip = data['normalization_clip']
525
+ normalization_values = data['normalization_values']
526
+ spatial_calib = data['spatial_calibration']
527
+ model_type = data['model_type']
528
+ if model_type=='stardist':
529
+ self.stardist_model.setChecked(True)
530
+ self.cellpose_model.setChecked(False)
531
+ else:
532
+ self.stardist_model.setChecked(False)
533
+ self.cellpose_model.setChecked(True)
534
+
535
+ for c,cb in zip(channels, self.channel_cbs):
536
+ index = cb.findText(c)
537
+ cb.setCurrentIndex(index)
538
+
539
+ for i in range(len(channels)):
540
+
541
+ to_clip = normalization_clip[i]
542
+ if self.clip_option[i] != to_clip:
543
+ self.normalization_clip_btns[i].click()
544
+
545
+ use_percentile = normalization_percentile[i]
546
+ if self.normalization_mode[i] != use_percentile:
547
+ self.normalization_mode_btns[i].click()
548
+
549
+ self.normalization_min_value_le[i].setText(str(normalization_values[i][0]))
550
+ self.normalization_max_value_le[i].setText(str(normalization_values[i][1]))
551
+
552
+
553
+ if len(channels)<len(self.channel_cbs):
554
+ for k in range(len(self.channel_cbs)-len(channels)):
555
+ self.channel_cbs[len(channels)+k].setCurrentIndex(0)
556
+ self.channel_cbs[len(channels)+k].setEnabled(False)
557
+ self.normalization_mode_btns[len(channels)+k].setEnabled(False)
558
+ self.normalization_max_value_le[len(channels)+k].setEnabled(False)
559
+ self.normalization_min_value_le[len(channels)+k].setEnabled(False)
560
+ self.normalization_min_value_lbl[len(channels)+k].setEnabled(False)
561
+ self.normalization_max_value_lbl[len(channels)+k].setEnabled(False)
562
+ self.normalization_clip_btns[len(channels)+k].setEnabled(False)
563
+
564
+ self.spatial_calib_le.setText(str(spatial_calib))
565
+
566
+ def adjustScrollArea(self):
567
+
568
+ """
569
+ Auto-adjust scroll area to fill space
570
+ (from https://stackoverflow.com/questions/66417576/make-qscrollarea-use-all-available-space-of-qmainwindow-height-axis)
571
+ """
572
+
573
+ step = 5
574
+ while self.scroll_area.verticalScrollBar().isVisible() and self.height() < self.maximumHeight():
575
+ self.resize(self.width(), self.height() + step)
576
+
577
+ def prep_model(self):
578
+
579
+ model_name = self.modelname_le.text()
580
+ pretrained_model = self.pretrained_model
581
+
582
+ channels = []
583
+ for i in range(len(self.channel_cbs)):
584
+ channels.append(self.channel_cbs[i].currentText())
585
+
586
+ slots_to_keep = np.where(np.array(channels)!='--')[0]
587
+ while '--' in channels:
588
+ channels.remove('--')
589
+
590
+ norm_values = np.array([[float(a.replace(',','.')),float(b.replace(',','.'))] for a,b in zip([l.text() for l in self.normalization_min_value_le],
591
+ [l.text() for l in self.normalization_max_value_le])])
592
+ norm_values = norm_values[slots_to_keep]
593
+ norm_values = [list(v) for v in norm_values]
594
+
595
+ clip_values = np.array(self.clip_option)
596
+ clip_values = list(clip_values[slots_to_keep])
597
+ clip_values = [bool(c) for c in clip_values]
598
+
599
+ normalization_mode = np.array(self.normalization_mode)
600
+ normalization_mode = list(normalization_mode[slots_to_keep])
601
+ normalization_mode = [bool(m) for m in normalization_mode]
602
+
603
+ data_folders = []
604
+ if self.dataset_folder is not None:
605
+ data_folders.append(self.dataset_folder)
606
+ if self.dataset_cb.currentText()!='--':
607
+ dataset = locate_segmentation_dataset(self.dataset_cb.currentText()) #glob(self.soft_path+'/celldetective/datasets/signals/*/')[self.dataset_cb.currentIndex()-1]
608
+ data_folders.append(dataset)
609
+
610
+ aug_factor = round(self.augmentation_slider.value(),2)
611
+ val_split = round(self.validation_slider.value(),2)
612
+ if self.stardist_model.isChecked():
613
+ model_type = 'stardist'
614
+ else:
615
+ model_type = 'cellpose'
616
+
617
+ try:
618
+ lr = float(self.lr_le.text().replace(',','.'))
619
+ except:
620
+ msgBox = QMessageBox()
621
+ msgBox.setIcon(QMessageBox.Warning)
622
+ msgBox.setText("Invalid value encountered for the learning rate.")
623
+ msgBox.setWindowTitle("Warning")
624
+ msgBox.setStandardButtons(QMessageBox.Ok)
625
+ returnValue = msgBox.exec()
626
+ if returnValue == QMessageBox.Ok:
627
+ return None
628
+
629
+ bs = int(self.bs_le.text())
630
+ epochs = self.epochs_slider.value()
631
+ spatial_calib = float(self.spatial_calib_le.text().replace(',','.'))
632
+
633
+ training_instructions = {'model_name': model_name,'model_type': model_type, 'pretrained': pretrained_model, 'spatial_calibration': spatial_calib, 'channel_option': channels, 'normalization_percentile': normalization_mode,
634
+ 'normalization_clip': clip_values,'normalization_values': norm_values, 'ds': data_folders, 'augmentation_factor': aug_factor, 'validation_split': val_split,
635
+ 'learning_rate': lr, 'batch_size': bs, 'epochs': epochs}
636
+
637
+ print(training_instructions)
638
+
639
+ model_folder = '/'.join([self.software_models_dir,model_name, ''])
640
+ print(model_folder)
641
+ if not os.path.exists(model_folder):
642
+ os.mkdir(model_folder)
643
+
644
+ training_instructions.update({'target_directory': self.software_models_dir})
645
+
646
+ print(f"Set of instructions: {training_instructions}")
647
+ with open(model_folder+"training_instructions.json", 'w') as f:
648
+ json.dump(training_instructions, f, indent=4)
649
+
650
+ train_segmentation_model(model_folder+"training_instructions.json", use_gpu=self.parent.parent.parent.use_gpu)
651
+
652
+ # self.parent.refresh_signal_models()
653
+
654
+
655
+ def check_valid_channels(self):
656
+
657
+ if np.all([cb.currentText()=='--' for cb in self.channel_cbs]):
658
+ self.submit_btn.setEnabled(False)
659
+ else:
660
+ self.submit_btn.setEnabled(True)
661
+
662
+
663
+ def switch_normalization_mode(self, index):
664
+
665
+ """
666
+ Use absolute or percentile values for the normalization of each individual channel.
667
+
668
+ """
669
+
670
+ currentNormMode = self.normalization_mode[index]
671
+ self.normalization_mode[index] = not currentNormMode
672
+
673
+ if self.normalization_mode[index]:
674
+ self.normalization_mode_btns[index].setIcon(icon(MDI6.percent_circle,color="#1565c0"))
675
+ self.normalization_mode_btns[index].setIconSize(QSize(20, 20))
676
+ self.normalization_mode_btns[index].setStyleSheet(self.parent.parent.parent.button_select_all)
677
+ self.normalization_mode_btns[index].setToolTip("Switch to absolute normalization values.")
678
+ self.normalization_min_value_lbl[index].setText('Min %: ')
679
+ self.normalization_max_value_lbl[index].setText('Max %: ')
680
+ self.normalization_min_value_le[index].setText('0.1')
681
+ self.normalization_max_value_le[index].setText('99.99')
682
+
683
+ else:
684
+ self.normalization_mode_btns[index].setIcon(icon(MDI6.percent_circle_outline,color="black"))
685
+ self.normalization_mode_btns[index].setIconSize(QSize(20, 20))
686
+ self.normalization_mode_btns[index].setStyleSheet(self.parent.parent.parent.button_select_all)
687
+ self.normalization_mode_btns[index].setToolTip("Switch to percentile normalization values.")
688
+ self.normalization_min_value_lbl[index].setText('Min: ')
689
+ self.normalization_min_value_le[index].setText('0')
690
+ self.normalization_max_value_lbl[index].setText('Max: ')
691
+ self.normalization_max_value_le[index].setText('1000')
692
+
693
+ def switch_clipping_mode(self, index):
694
+
695
+ currentClipMode = self.clip_option[index]
696
+ self.clip_option[index] = not currentClipMode
697
+
698
+ if self.clip_option[index]:
699
+ self.normalization_clip_btns[index].setIcon(icon(MDI6.content_cut,color="#1565c0"))
700
+ self.normalization_clip_btns[index].setIconSize(QSize(20, 20))
701
+ self.normalization_clip_btns[index].setStyleSheet(self.parent.parent.parent.button_select_all)
702
+
703
+ else:
704
+ self.normalization_clip_btns[index].setIcon(icon(MDI6.content_cut,color="black"))
705
+ self.normalization_clip_btns[index].setIconSize(QSize(20, 20))
706
+ self.normalization_clip_btns[index].setStyleSheet(self.parent.parent.parent.button_select_all)