-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaster.py
More file actions
executable file
·334 lines (289 loc) · 18.2 KB
/
master.py
File metadata and controls
executable file
·334 lines (289 loc) · 18.2 KB
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
#!/usr/bin/env python3
import os
import sys
import logging
import importlib
import multiprocessing as mp
from functools import partial
from PyQt5.QtCore import Qt, QSize, QTimer, QUrl
from PyQt5.QtGui import QPixmap, QIcon, QFontMetrics, QKeySequence
from PyQt5.QtWidgets import QApplication, QLabel, QPushButton, QVBoxLayout, QHBoxLayout, QWidget, QLineEdit, QTreeWidget, QAbstractItemView, QSplitter, QStackedWidget, QTreeWidgetItem, QTreeWidgetItemIterator, QShortcut
from modules.registration_module import registration
from modules.zprojection_module import zprojection
from modules.ground_truth_generator_module import ground_truth_generator
from modules.segmentation_module import segmentation
from modules.cell_tracking_module import cell_tracking
from modules.graph_filtering_module import graph_filtering
from modules.events_selection_module import events_selection
from modules.viewer_module import viewer
from modules.file_organization_module import file_organization
from modules.file_conversion_module import file_conversion
from modules.image_cropping_module import image_cropping
from modules.pipeline_module import pipeline
from general import general_functions as gf
from platform import python_version, platform
from cellpose import version as cellpose_version
from napari import __version__ as napari_version
from numpy import __version__ as numpy_version
from torch import __version__ as torch_version
from version import __version__ as vlabapp_version
from cv2 import __version__ as opencv_version
from igraph import __version__ as igraph_version
from packaging.version import Version
from pystackreg import __version__ as pystackreg_version
from skimage import __version__ as skimage_version
from scipy import __version__ as scipy_version
from roifile import __version__ as roifile_version
from ome_types import __version__ as ometypes_version
from bioio import __version__ as bioio_version
from imageio import __version__ as imageio_version
from imageio_ffmpeg import __version__ as imageioffmpeg_version
from tifffile import __version__ as tifffile_version
from matplotlib import __version__ as matplotlib_version
from nd2 import __version__ as nd2_version
from pandas import __version__ as pandas_version
from PyQt5.QtCore import PYQT_VERSION_STR as pyqt_version
try:
from micro_sam import __version__ as microsam_version
microsam_available = True
except ImportError:
microsam_available = False
class AboutVLabApp(QWidget):
def __init__(self, pipeline_layout=False):
super().__init__()
layout = QVBoxLayout()
logo = QLabel()
logo.setPixmap(QPixmap(os.path.join(os.path.dirname(__file__),'support_files/logo.png')))
logo.setAlignment(Qt.AlignCenter)
layout.addWidget(logo)
label = QLabel()
label.setTextFormat(Qt.RichText)
label.setWordWrap(True)
label.setOpenExternalLinks(True)
label.setText('<h2>VLabApp</h2>' +
'Home page:<br><a href="https://github.com/vjesticalab/VLabApp">https://github.com/vjesticalab/VLabApp</a><br><br>' +
'Documentation:<br><a href="' + QUrl.fromUserInput(os.path.join(os.path.dirname(__file__), 'doc', 'site', 'index.html')).toString() + '">' + os.path.normpath(os.path.join(os.path.dirname(__file__), 'doc', 'site', 'index.html')) + '</a>' +
'<h3>Citation</h3>' +
'If you use VLabApp in your research, please cite the VLabApp paper:' +
'<blockquote>J. Dorier, A. Ravera and A. Vjestica. In preparation</blockquote>' +
'If you use the registration module with <a href="https://bigwww.epfl.ch/thevenaz/stackreg/">stackreg</a>, please cite the following <a href="https://doi.org/10.1109/83.650848">publication</a>:' +
'<blockquote>P. Thevenaz, U. E. Ruttimann and M. Unser (1998). A pyramid approach to subpixel registration based on intensity. IEEE Transactions on Image Processing, 7(1), 27–41.</blockquote>' +
'If you use the segmentation module with <a href="https://www.cellpose.org/">Cellpose</a>, please cite the Cellpose 1.0 <a href="https://doi.org/10.1038/s41592-020-01018-x">publication</a>:' +
'<blockquote>C. Stringer, T. Wang, M. Michaelos and M. Pachitariu (2021). Cellpose: a generalist algorithm for cellular segmentation. Nature Methods 18, 100–106.</blockquote>' +
'If you fine-tune a Cellpose model, please cite the Cellpose 2.0 <a href="https://doi.org/10.1038/s41592-022-01663-4">publication</a>:' +
'<blockquote>M. Pachitariu and C. Stringer (2022). Cellpose 2.0: how to train your own model. Nature Methods 19, 1634–1641.</blockquote>' +
'If you use the segmentation module with the `cyto3` Cellpose model, please cite the Cellpose 3.0 <a href="https://doi.org/10.1038/s41592-025-02595-5">publication</a>:' +
'<blockquote>C. Stringer and M. Pachitariu (2025). Cellpose3: one-click image restoration for improved cellular segmentation. Nature Methods 22, 592-599.</blockquote>' +
('If you use the segmentation module with <a href="https://github.com/computational-cell-analytics/micro-sam">Segment Anything for Microscopy</a>, please cite the Segment Anything for Microscopy <a href="https://doi.org/10.1038/s41592-024-02580-4">publication</a>:' if microsam_available else '' ) +
('<blockquote>A. Archit, L. Freckmann, S. Nair et al. (2025). Segment Anything for Microscopy. Nature Methods 22, 579-591.</blockquote>' if microsam_available else '' ) +
('as well as the original <a href="https://segment-anything.com/">Segment Anything</a> <a href="https://doi.org/10.48550/arXiv.2304.02643">publication</a>:' if microsam_available else '' ) +
('<blockquote>A. Kirillov, E. Mintun, N. Ravi et al. (2023). Segment Anything. http://arxiv.org/abs/2304.02643</blockquote>' if microsam_available else '' ) +
'<h3>System information</h3>' +
'<b>VLabApp:</b> ' + vlabapp_version + '<br>' +
'<b>platform:</b> ' + platform() + '<br>' +
'<b>python:</b> ' + python_version() + '<br>' +
'<b>bioio:</b> ' + bioio_version + '<br>' +
'<b>cellpose:</b> ' + cellpose_version + '<br>' +
'<b>igraph:</b> ' + igraph_version + '<br>' +
'<b>imageio:</b> ' + imageio_version + '<br>' +
'<b>imageio-ffmpeg:</b> ' + imageioffmpeg_version + '<br>' +
'<b>matplotlib:</b> ' + matplotlib_version + '<br>' +
('<b>micro_sam:</b> ' + microsam_version + '<br>' if microsam_available else '' ) +
'<b>napari:</b> ' + napari_version + '<br>' +
'<b>nd2:</b> ' + nd2_version + '<br>' +
'<b>numpy:</b> ' + numpy_version + '<br>' +
'<b>ome-types:</b> ' + ometypes_version + '<br>' +
'<b>opencv:</b> ' + opencv_version + '<br>' +
'<b>pandas:</b> ' + pandas_version + '<br>' +
'<b>pyqt:</b> ' + pyqt_version + '<br>' +
'<b>pystackreg:</b> ' + pystackreg_version + '<br>' +
'<b>pytorch:</b> ' + torch_version + '<br>' +
'<b>roifile:</b> ' + roifile_version + '<br>' +
'<b>scikit-image:</b> ' + skimage_version + '<br>' +
'<b>scipy:</b> ' + scipy_version + '<br>' +
'<b>tifffile:</b> ' + tifffile_version +
'<h3>Credits</h3>' +
'Julien Dorier - Bioinformatics Competence Center, <a href="https://www.unil.ch">University of Lausanne</a>.<br>' +
'Arianna Ravera - Scientific Computing and Research Support Unit, <a href="https://www.unil.ch">University of Lausanne</a>.<br>' +
'Aleksandar Vjestica - Center for Integrative Genomics, <a href="https://www.unil.ch">University of Lausanne</a>.<br>')
layout.addWidget(label)
self.setLayout(layout)
class MainWindow(QWidget):
def __init__(self):
super().__init__()
window = QVBoxLayout(self)
title = "VLabApp"
self.setWindowTitle(title)
self.status_line = QLineEdit()
self.status_line.setEnabled(False)
self.status_line.setFrame(False)
font = self.status_line.font()
font.setItalic(True)
self.status_line.setFont(font)
self.module_list = QTreeWidget()
self.module_list.setColumnCount(1)
self.module_list.setSortingEnabled(False)
self.module_list.setSelectionMode(QAbstractItemView.SingleSelection)
self.module_list.setVerticalScrollMode(QAbstractItemView.ScrollPerPixel)
self.module_list.setEditTriggers(QAbstractItemView.NoEditTriggers)
self.module_list.setHeaderHidden(True)
self.right_panel = QStackedWidget()
splitter = QSplitter()
self.left_panel = QWidget()
self.left_panel.setLayout(QVBoxLayout())
self.left_panel.layout().setContentsMargins(0, 0, 0, 0)
self.left_panel.layout().addWidget(self.module_list)
splitter.addWidget(self.left_panel)
splitter.addWidget(self.right_panel)
splitter.setCollapsible(1, False)
splitter.setStretchFactor(0, 1)
splitter.setStretchFactor(1, 5)
window.addWidget(splitter)
item = QTreeWidgetItem(self.module_list, ["About VLabApp"])
item.setData(0, Qt.UserRole, self.right_panel.count())
self.right_panel.addWidget(gf.Page(widget=AboutVLabApp()))
item = QTreeWidgetItem(self.module_list, ["Registration"])
item.setData(0, Qt.UserRole, self.right_panel.count())
item.setFlags(item.flags() & ~Qt.ItemIsSelectable)
item.setExpanded(True)
self.right_panel.addWidget(gf.Page(widget=QWidget()))
subitem = QTreeWidgetItem(item, ["Registration"])
subitem.setData(0, Qt.UserRole, self.right_panel.count())
self.right_panel.addWidget(gf.Page(widget=registration.Perform()))
subitem = QTreeWidgetItem(item, ["Alignment"])
subitem.setData(0, Qt.UserRole, self.right_panel.count())
self.right_panel.addWidget(gf.Page(widget=registration.Align()))
subitem = QTreeWidgetItem(item, ["Editing (manual)"])
subitem.setData(0, Qt.UserRole, self.right_panel.count())
self.right_panel.addWidget(gf.Page(widget=registration.ManualEdit()))
item = QTreeWidgetItem(self.module_list, ["Z-Projection"])
item.setData(0, Qt.UserRole, self.right_panel.count())
self.right_panel.addWidget(gf.Page(widget=zprojection.zProjection()))
item = QTreeWidgetItem(self.module_list, ["Segmentation"])
item.setData(0, Qt.UserRole, self.right_panel.count())
self.right_panel.addWidget(gf.Page(widget=segmentation.Segmentation()))
item = QTreeWidgetItem(self.module_list, ["Cell tracking"])
item.setData(0, Qt.UserRole, self.right_panel.count())
self.right_panel.addWidget(gf.Page(widget=cell_tracking.CellTracking()))
item = QTreeWidgetItem(self.module_list, ["Graph filtering"])
item.setData(0, Qt.UserRole, self.right_panel.count())
self.right_panel.addWidget(gf.Page(widget=graph_filtering.GraphFiltering()))
item = QTreeWidgetItem(self.module_list, ["Events selection"])
item.setData(0, Qt.UserRole, self.right_panel.count())
self.right_panel.addWidget(gf.Page(widget=events_selection.EventsSelection()))
item = QTreeWidgetItem(self.module_list, ["Pipeline"])
item.setData(0, Qt.UserRole, self.right_panel.count())
self.right_panel.addWidget(gf.Page(widget=pipeline.Pipeline()))
item = QTreeWidgetItem(self.module_list, ["Tools"])
item.setData(0, Qt.UserRole, self.right_panel.count())
item.setFlags(item.flags() & ~Qt.ItemIsSelectable)
item.setExpanded(True)
self.right_panel.addWidget(gf.Page(widget=QWidget()))
subitem = QTreeWidgetItem(item, ["View image, mask and graph"])
subitem.setData(0, Qt.UserRole, self.right_panel.count())
self.right_panel.addWidget(gf.Page(widget=viewer.ImageMaskGraphViewer()))
subitem = QTreeWidgetItem(item, ["View registration matrix"])
subitem.setData(0, Qt.UserRole, self.right_panel.count())
self.right_panel.addWidget(gf.Page(widget=viewer.RegistrationViewer()))
subitem = QTreeWidgetItem(item, ["View metadata"])
subitem.setData(0, Qt.UserRole, self.right_panel.count())
self.right_panel.addWidget(gf.Page(widget=viewer.MetadataViewer(), add_stretch=False))
subitem = QTreeWidgetItem(item, ["File organization"])
subitem.setData(0, Qt.UserRole, self.right_panel.count())
self.right_panel.addWidget(gf.Page(widget=file_organization.FileOrganization()))
subitem = QTreeWidgetItem(item, ["File conversion (masks and graphs)"])
subitem.setData(0, Qt.UserRole, self.right_panel.count())
self.right_panel.addWidget(gf.Page(widget=file_conversion.MaskGraphConversion()))
subitem = QTreeWidgetItem(item, ["File conversion (lossy preview)"])
subitem.setData(0, Qt.UserRole, self.right_panel.count())
self.right_panel.addWidget(gf.Page(widget=file_conversion.ImageMaskConversion()))
subitem = QTreeWidgetItem(item, ["Image cropping"])
subitem.setData(0, Qt.UserRole, self.right_panel.count())
self.right_panel.addWidget(gf.Page(widget=image_cropping.ImageCropping()))
subitem = QTreeWidgetItem(item, ["Ground truth generator"])
subitem.setData(0, Qt.UserRole, self.right_panel.count())
self.right_panel.addWidget(gf.Page(widget=ground_truth_generator.GroundTruthGenerator()))
## add plugins
plugins_path = os.path.join(os.path.dirname(__file__), 'plugins')
plugins_list = []
for p in os.listdir(plugins_path):
if os.path.isfile(os.path.join(plugins_path, p, '__init__.py')):
module = importlib.import_module('plugins.' + p)
try:
name = module.NAME
except AttributeError:
name = module.__name__.replace('plugins.', '')
plugins_list.append((name, module))
if plugins_list:
item = QTreeWidgetItem(self.module_list, ["Plugins"])
item.setData(0, Qt.UserRole, self.right_panel.count())
item.setFlags(item.flags() & ~Qt.ItemIsSelectable)
item.setExpanded(True)
self.right_panel.addWidget(gf.Page(widget=QWidget()))
# add plugins (alphabetical order)
for name, plugin in sorted(plugins_list, key=lambda x: (x[0].lower())):
subitem = QTreeWidgetItem(item, [name])
subitem.setData(0, Qt.UserRole, self.right_panel.count())
self.right_panel.addWidget(gf.Page(widget=plugin.Widget()))
w = self.module_list.sizeHintForColumn(0)
h = round(1.5*self.module_list.sizeHintForRow(0))
font_metric = QFontMetrics(self.module_list.font())
iterator = QTreeWidgetItemIterator(self.module_list)
while iterator.value():
item = iterator.value()
item.setSizeHint(0, QSize(w, h))
iterator += 1
self.module_list.setMinimumWidth(font_metric.width('a')*25)
self.module_list.currentItemChanged.connect(self.module_list_current_item_changed)
self.module_list.setCurrentItem(self.module_list.topLevelItem(0))
layout = QHBoxLayout()
layout.addWidget(self.status_line)
shortcut_quit = QShortcut(QKeySequence.Quit, self)
shortcut_quit.setContext(Qt.ShortcutContext.WidgetWithChildrenShortcut)
shortcut_quit.activated.connect(QApplication.quit)
self.quit_button = QPushButton("Quit", self)
self.quit_button.clicked.connect(QApplication.quit)
self.quit_button.setStyleSheet("background: darkred;")
layout.addWidget(self.quit_button, alignment=Qt.AlignRight)
window.addLayout(layout)
# Setup logging
logging.basicConfig(level=logging.INFO, format="%(asctime)s (%(name)s) [%(levelname)s] %(message)s", handlers=[logging.StreamHandler(sys.stdout)], force=True)
# Add a handler to output messages to self.status_line
self.qlabel_handler = gf.QLineEditHandler(self.status_line)
self.qlabel_handler.setFormatter(logging.Formatter('%(message)s'))
logging.getLogger().addHandler(self.qlabel_handler)
# Add a handler to output errors to QMessageBox
self.qmessagebox_handler = gf.QMessageBoxErrorHandler(self)
self.qmessagebox_handler.setFormatter(logging.Formatter('%(message)s'))
self.qmessagebox_handler.setLevel(logging.ERROR)
self.qmessagebox_handler.name = 'messagebox_error_handler'
logging.getLogger().addHandler(self.qmessagebox_handler)
# Temporary workaround to ensure QGroupBox titles are not cropped
self.setStyleSheet("QLineEdit:disabled {background: transparent} " +
"QGroupBox::title {padding-right: 1px}")
def module_list_current_item_changed(self, current, previous):
self.right_panel.setCurrentIndex(current.data(0, Qt.UserRole))
self.setWindowTitle('VLabApp - '+current.text(0))
self.status_line.setText('')
if current.childCount():
current.setExpanded(True)
self.module_list.setCurrentItem(current.child(0))
QTimer.singleShot(0, partial(self.module_list.setCurrentItem, current.child(0)))
def __del__(self):
# Remove handler to avoid problems after self and self.status_line are destroyed
logging.getLogger().removeHandler(self.qlabel_handler)
logging.getLogger().removeHandler(self.qmessagebox_handler)
if __name__ == "__main__":
# set up some environmental variables
os.environ['OMP_NUM_THREADS'] = '1'
os.environ['MKL_NUM_THREADS'] = '1'
try:
mp.set_start_method('spawn', force=True)
except RuntimeError:
sys.exit(1)
app = QApplication(sys.argv)
app.setWindowIcon(QIcon(os.path.join(os.path.dirname(__file__),'support_files/logo.png')))
w = MainWindow()
w.show()
w.resize(1000, 800)
sys.exit(app.exec_())