-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathbackend_qt.py
More file actions
512 lines (397 loc) · 18.6 KB
/
backend_qt.py
File metadata and controls
512 lines (397 loc) · 18.6 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
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
from .myqt import QT
import pyqtgraph as pg
import markdown
import numpy as np
from copy import copy
import weakref
from .viewlist import possible_class_views
from .layout_presets import get_layout_description
from .utils_global import fill_unnecessary_space, get_present_zones_in_half_of_layout
from .utils_qt import qt_style, add_stretch_to_qtoolbar
# Used by views to emit/trigger signals
class SignalNotifier(QT.QObject):
spike_selection_changed = QT.pyqtSignal()
unit_visibility_changed = QT.pyqtSignal()
channel_visibility_changed = QT.pyqtSignal()
manual_curation_updated = QT.pyqtSignal()
time_info_updated = QT.pyqtSignal()
use_times_updated = QT.pyqtSignal()
unit_color_changed = QT.pyqtSignal()
def __init__(self, parent=None, view=None):
QT.QObject.__init__(self, parent=parent)
self.view = view
def notify_spike_selection_changed(self):
self.spike_selection_changed.emit()
def notify_unit_visibility_changed(self):
self.unit_visibility_changed.emit()
def notify_channel_visibility_changed(self):
self.channel_visibility_changed.emit()
def notify_manual_curation_updated(self):
self.manual_curation_updated.emit()
def notify_time_info_updated(self):
self.time_info_updated.emit()
def notify_use_times_updated(self):
self.use_times_updated.emit()
def notify_unit_color_changed(self):
self.unit_color_changed.emit()
# Used by controler to handle/callback signals
class SignalHandler(QT.QObject):
def __init__(self, controller, parent=None):
QT.QObject.__init__(self, parent=parent)
self.controller = controller
self._active = True
def activate(self):
self._active = True
def deactivate(self):
self._active = False
def connect_view(self, view):
view.notifier.spike_selection_changed.connect(self.on_spike_selection_changed)
view.notifier.unit_visibility_changed.connect(self.on_unit_visibility_changed)
view.notifier.channel_visibility_changed.connect(self.on_channel_visibility_changed)
view.notifier.manual_curation_updated.connect(self.on_manual_curation_updated)
view.notifier.time_info_updated.connect(self.on_time_info_updated)
view.notifier.use_times_updated.connect(self.on_use_times_updated)
view.notifier.unit_color_changed.connect(self.on_unit_color_changed)
def on_spike_selection_changed(self):
if not self._active:
return
for view in self.controller.views:
if view.qt_widget == self.sender().parent():
# do not refresh it self
continue
view.on_spike_selection_changed()
def on_unit_visibility_changed(self):
if not self._active:
return
for view in self.controller.views:
if view.qt_widget == self.sender().parent():
# do not refresh it self
continue
view.on_unit_visibility_changed()
def on_channel_visibility_changed(self):
if not self._active:
return
for view in self.controller.views:
if view.qt_widget == self.sender().parent():
# do not refresh it self
continue
view.on_channel_visibility_changed()
def on_manual_curation_updated(self):
if not self._active:
return
for view in self.controller.views:
if view.qt_widget == self.sender().parent():
# do not refresh it self
continue
view.on_manual_curation_updated()
def on_time_info_updated(self):
if not self._active:
return
for view in self.controller.views:
if view.qt_widget == self.sender().parent():
# do not refresh it self
continue
view.on_time_info_updated()
def on_use_times_updated(self):
if not self._active:
return
for view in self.controller.views:
if view.qt_widget == self.sender().parent():
# do not refresh it self
continue
view.on_use_times_updated()
def on_unit_color_changed(self):
if not self._active:
return
for view in self.controller.views:
if view.qt_widget == self.sender().parent():
# do not refresh it self
continue
view.on_unit_color_changed()
def create_settings(view, parent):
view.settings = pg.parametertree.Parameter.create(name="settings", type='group', children=view._settings)
# not that the parent is not the view (not Qt anymore) itself but the widget
view.tree_settings = pg.parametertree.ParameterTree(parent=parent)
view.tree_settings.header().hide()
view.tree_settings.setParameters(view.settings, showTop=True)
view.tree_settings.setWindowTitle(u'View options')
# view.tree_settings.setWindowFlags(QT.Qt.Window)
def listen_setting_changes(view):
view.settings.sigTreeStateChanged.connect(view.on_settings_changed)
def stop_listen_setting_changes(view):
view.settings.sigTreeStateChanged.disconnect(view.on_settings_changed)
class QtMainWindow(QT.QMainWindow):
main_window_closed = QT.pyqtSignal(object)
def __init__(self, controller, parent=None, layout_dict=None, user_settings=None):
QT.QMainWindow.__init__(self, parent)
self.controller = controller
self.verbose = controller.verbose
self.layout_dict = layout_dict
self.make_views(user_settings)
self.create_main_layout()
# refresh all views wihtout notiying
self.controller.signal_handler.deactivate()
for view in self.views.values():
# refresh do not work because view are not yet visible at init
view._refresh()
self.controller.signal_handler.activate()
# TODO sam : all veiws are always refreshed at the moment so this is useless.
# uncommen this when ViewBase.is_view_visible() work correctly
# for view_name, dock in self.docks.items():
# dock.visibilityChanged.connect(self.views[view_name].refresh)
def make_views(self, user_settings):
self.views = {}
self.docks = {}
for view_name, view_class in possible_class_views.items():
if 'qt' not in view_class._supported_backend:
continue
if not self.controller.check_is_view_possible(view_name):
continue
if view_name == 'curation' and not self.controller.curation:
continue
if view_name in ("trace", "tracemap") and not self.controller.with_traces:
continue
widget = ViewWidget(view_class)
view = view_class(controller=self.controller, parent=widget, backend='qt')
if user_settings is not None and user_settings.get(view_name) is not None:
for setting_name, user_setting in user_settings.get(view_name).items():
if setting_name not in view.settings.keys().keys():
raise KeyError(f"Setting {setting_name} is not a valid setting for View {view_name}. Check your settings file.")
stop_listen_setting_changes(view)
view.settings[setting_name] = user_setting
listen_setting_changes(view)
widget.set_view(view)
dock = QT.QDockWidget(view_name)
dock.setWidget(widget)
# dock.visibilityChanged.connect(view.refresh)
self.views[view_name] = view
self.docks[view_name] = dock
def create_main_layout(self):
import warnings
warnings.filterwarnings("ignore", category=RuntimeWarning, module="pyqtgraph")
self.setDockNestingEnabled(True)
preset = self.layout_dict
widgets_zone = {}
for zone, view_names in preset.items():
# keep only instantiated views
view_names = [view_name for view_name in view_names if view_name in self.views.keys()]
widgets_zone[zone] = view_names
self.make_half_layout(widgets_zone, "left")
self.make_half_layout(widgets_zone, "right")
# make tabs
for zone, view_names in widgets_zone.items():
n = len(widgets_zone[zone])
if n < 2:
# no tab here
continue
view_name0 = widgets_zone[zone][0]
for i in range(1, n):
view_name = widgets_zone[zone][i]
dock = self.docks[view_name]
self.tabifyDockWidget(self.docks[view_name0], dock)
# make visible the first of each zone
self.docks[view_name0].raise_()
def make_half_layout(self, widgets_zone, left_or_right):
"""
Function contains the logic for the greedy layout. Given the 2x2 box of zones
1 2 3 4
5 6 or 7 8
Then depending on which zones are non-zero, a different layout is generated using splits.
The zone indices in the second box (34,78) are equal to the zone indices first box (12,56)
shifted by 2. We take advantage of this fact.
"""
shift = 0 if left_or_right == "left" else 2
widgets_zone = fill_unnecessary_space(widgets_zone, shift)
present_zones = get_present_zones_in_half_of_layout(widgets_zone, shift)
if len(present_zones) == 0:
return
# The movements from earlier guarantee that the top-left zone is non-zero. Make this the initial zone
view_name = widgets_zone[f"zone{1+shift}"][0]
dock = self.docks[view_name]
self.addDockWidget(areas[left_or_right], dock)
# The main logic: apply splittings between different zones and in
# different orders, depending on which zones are present.
# Layouts with two non-zero zones
if present_zones == set([f'zone{1+shift}', f'zone{2+shift}']):
self.make_split(1,2,"horizontal", widgets_zone, shift)
elif present_zones == set([f'zone{1+shift}', f'zone{5+shift}']):
self.make_split(1,5,"vertical", widgets_zone, shift)
elif present_zones == set([f'zone{1+shift}', f'zone{6+shift}']):
self.make_split(1,6,"horizontal", widgets_zone, shift)
# Layouts with three non-zero zones
elif present_zones == set([f'zone{1+shift}', f'zone{2+shift}', f'zone{5+shift}']):
self.make_split(1,2,"horizontal", widgets_zone, shift)
self.make_split(1,5,"vertical", widgets_zone, shift)
elif present_zones == set([f'zone{1+shift}', f'zone{2+shift}', f'zone{6+shift}']):
self.make_split(1,2,"horizontal", widgets_zone, shift)
self.make_split(2,6,"vertical", widgets_zone, shift)
elif present_zones == set([f'zone{1+shift}', f'zone{5+shift}', f'zone{6+shift}']):
self.make_split(1,5,"vertical", widgets_zone, shift)
self.make_split(5,6,"horizontal", widgets_zone, shift)
# Layout with four non-zero zones
elif present_zones == set([f'zone{1+shift}', f'zone{2+shift}', f'zone{5+shift}', f'zone{6+shift}']):
self.make_split(1,5,"vertical", widgets_zone, shift)
self.make_split(1,2,"horizontal", widgets_zone, shift)
self.make_split(5,6,"horizontal", widgets_zone, shift)
def make_split(self, zone_index_1, zone_index_2, orientation, widgets_zone, shift):
"""
Splits the zone at `zone_{zone_index_1+shift}` into two zones
(`zone_{zone_index_1+shift}` and `zone_{zone_index_2+shift}`)
with an `orientation` split.
"""
widget_1 = widgets_zone[f"zone{zone_index_1+shift}"][0]
widget_2 = widgets_zone[f"zone{zone_index_2+shift}"][0]
self.splitDockWidget(self.docks[widget_1], self.docks[widget_2], orientations[orientation])
# used by to tell the launcher this is closed
def closeEvent(self, event):
if not self.controller.current_curation_saved:
reply = QT.QMessageBox.question(self, 'Confirmation',
"You are daydreaming: your curation has not been saved. You can save it at the top of the 'CurationView'.\nDo you still want to quit?", QT.QMessageBox.Yes |
QT.QMessageBox.No, QT.QMessageBox.No)
if reply == QT.QMessageBox.Yes:
# 2. Accept the event to allow closing
pass
else:
# 3. Ignore the event to prevent closing
event.ignore()
return
self.main_window_closed.emit(self)
event.accept()
class ViewWidget(QT.QWidget):
def __init__(self, view_class, parent=None):
QT.QWidget.__init__(self, parent=parent)
self.layout = QT.QVBoxLayout()
self.setLayout(self.layout)
self.layout.setContentsMargins(4,4,4,4)
self.layout.setSpacing(4)
tb = self.view_toolbar = QT.QToolBar()
self.layout.addWidget(self.view_toolbar)
tb.setStyleSheet(qt_style)
if view_class._settings is not None:
but = QT.QPushButton('⚙ settings')
tb.addWidget(but)
but.clicked.connect(self.open_settings)
# but.setStyleSheet(qt_style)
if view_class._need_compute:
but = QT.QPushButton('compute')
tb.addWidget(but)
but.clicked.connect(self.compute)
but = QT.QPushButton('↻ refresh')
tb.addWidget(but)
but.clicked.connect(self.refresh)
but = QT.QPushButton('?')
tb.addWidget(but)
but.clicked.connect(self.open_help)
tooltip_html = markdown.markdown(view_class._gui_help_txt)
but.setToolTip(tooltip_html)
add_stretch_to_qtoolbar(tb)
# TODO: make _qt method for all existing methods that don't start with _qt or _panel
# skip = ['__init__', 'set_view', 'open_settings', 'compute', 'refresh', 'open_help',
# 'on_spike_selection_changed', 'on_unit_visibility_changed',
# 'on_channel_visibility_changed', 'on_manual_curation_updated']
# for name in dir(view_class):
# if name.startswith('_qt_') or name.startswith('_panel_') or name in skip:
# continue
# if hasattr(view_class, name):
# method = getattr(view_class, name)
# if callable(method):
# if name == "save_in_analyzer":
# print(f'creating _qt_save_in_analyzer for {view_class}')
# setattr(view_class, '_qt_' + name, method)
def set_view(self, view):
self._view = weakref.ref(view)
if view._settings is not None:
self.layout.addWidget(view.tree_settings)
view.tree_settings.hide()
self.layout.addLayout(view.layout)
def open_settings(self):
view = self._view()
if not view.tree_settings.isVisible():
view.tree_settings.show()
else:
view.tree_settings.hide()
def compute(self):
view = self._view()
if view._need_compute:
view.compute()
def open_help(self):
view = self._view()
but = self.sender()
txt = view._gui_help_txt
txt = markdown.markdown(txt)
QT.QToolTip.showText(but.mapToGlobal(QT.QPoint()), txt, but)
def refresh(self):
view = self._view()
view.refresh()
areas = {
'right' : QT.Qt.RightDockWidgetArea,
'left' : QT.Qt.LeftDockWidgetArea,
}
orientations = {
'horizontal' : QT.Qt.Horizontal,
'vertical' : QT.Qt.Vertical,
}
class ControllerSynchronizer(QT.QWidget):
def __init__(self, sorting_comparison, controllers, windows, parent=None):
QT.QWidget.__init__(self, parent=parent)
self.comp = sorting_comparison
self.controllers = controllers
self.windows = windows
self.layout = QT.QVBoxLayout()
self.setLayout(self.layout)
self.label = QT.QLabel('')
self.layout.addWidget(self.label)
for i, window in enumerate(windows):
# this is not working ???!!!!!
# callback = lambda: self.on_unit_visibility_changed(win_ind=i)
# so uggly solution
callback = [self.on_unit_visibility_changed_0, self.on_unit_visibility_changed_1][i]
for view in window.views.values():
view.notifier.unit_visibility_changed.connect(callback)
settings = [
{'name': 'mode', 'type': 'list', 'limits' : ['all', 'best', ] },
{'name': 'thresh', 'type': 'float', 'value' : 0.05, 'step': 0.01, 'limits': (0, 1.)},
]
self.settings = pg.parametertree.Parameter.create(name="settings", type='group', children=settings)
# not that the parent is not the view (not Qt anymore) itself but the widget
self.tree_settings = pg.parametertree.ParameterTree(parent=self)
self.tree_settings.header().hide()
self.tree_settings.setParameters(self.settings, showTop=True)
self.tree_settings.setWindowTitle('Settings')
self.layout.addWidget(self.tree_settings)
def on_unit_visibility_changed_0(self):
self.on_unit_visibility_changed(0)
def on_unit_visibility_changed_1(self):
self.on_unit_visibility_changed(1)
def on_unit_visibility_changed(self, win_ind):
changed_controller = self.controllers[win_ind]
visible_unit_inds = changed_controller.get_visible_unit_indices()
visible_unit_ids = changed_controller.get_visible_unit_ids()
if len(visible_unit_inds) != 1:
# TODO handle several units at once
return
unit_ind = visible_unit_inds[0]
agreement = self.comp.agreement_scores.values
if win_ind == 1:
agreement = agreement.T
thresh = self.settings['thresh']
mode = self.settings['mode']
other_ind = (win_ind + 1) % 2
other_controller = self.controllers[other_ind]
other_window = self.windows[other_ind]
if mode == 'all':
other_visible_inds = agreement[unit_ind, :] > thresh
elif mode == 'best':
best_ind = np.argmax(agreement[unit_ind, :])
if agreement[unit_ind, best_ind] > thresh:
other_visible_inds = [best_ind]
else:
other_visible_inds = []
other_visible_ids = other_controller.unit_ids[other_visible_inds]
other_controller.set_visible_unit_ids(other_visible_ids)
for view in other_window.views.values():
view.refresh()
self.label.setText(
f'Analyzer {win_ind} : {visible_unit_ids}\n'
f'Analyzer {other_ind} : {other_visible_ids}\n'
)