-
Notifications
You must be signed in to change notification settings - Fork 1
/
gui.py
448 lines (329 loc) · 13.4 KB
/
gui.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
import log
logger = log.get_logger(__name__)
import threading
import cleaviz
import mock
import grinder
import pyqtgraph as pg
import socket
import sthread
import multiprocessing
import analysis
import sys
import os
import queue
import logging
import time
import meamer
import demo_receiver
import multiprocessing.connection
from multiprocessing import Process
from PyQt5.QtWidgets import QApplication, QWidget, QToolTip, QPushButton, \
QDesktopWidget, QLineEdit, QFormLayout, QMainWindow, QLabel, QTextEdit, \
QAbstractScrollArea, QFileDialog
from PyQt5.QtGui import QIcon, QFont, QTextCharFormat, QBrush, QColor, QTextCursor, \
QTextFormat, QCursor
from PyQt5.QtMultimedia import QSound
from PyQt5.QtCore import QCoreApplication, QPoint, Qt, QThread, pyqtSignal, QObject, \
pyqtSlot
from PyQt5 import uic
MAINWINDOW_UI_FILE = 'style/interface.ui'
DEMOWINDOW_UI_FILE = 'style/demo.ui'
MAINWINDOW_CSS_FILE = 'style/stylesheet.css'
grinderStarted = False
grinderStarted = False
# We _need_ a synchronized queue to handle outputting to the status
# area in a separate thread so we don't block.
class LogStream(object):
def __init__(self, q):
self.q = q
def write(self, text):
self.q.put(text)
def flush(self):
self.q.queue.clear()
class QLogOutputter(QObject):
stdout_signal = pyqtSignal(str)
running = True
def __init__(self, q, *args, **kwargs):
QObject.__init__(self, *args, **kwargs)
self.q = q
@pyqtSlot()
def run(self):
while self.running:
try:
text = self.q.get(timeout=1)
self.stdout_signal.emit(text)
except queue.Empty:
pass
try:
text = self.mp_reader.readline()
self.stdout_signal.emit(text)
except AttributeError:
pass
class QLoggingHandler(logging.Handler):
logging_format = '%(asctime)s - %(levelname)s - %(module)s - %(message)s'
def __init__(self):
super().__init__()
self.setFormatter(logging.Formatter(self.logging_format))
def emit(self, record):
print(self.format(record))
def fork_cleaviz():
# os.dup2(w.fileno(), 1)
# print('fork cleaviz')
cleaviz_window = cleaviz.CleavizWindow(sample_rate=10000, segment_length=1000)
cleaviz_window.run()
def analysis_loading(func):
def wrapper(self, example):
QApplication.setOverrideCursor(Qt.WaitCursor)
example()
QApplication.restoreOverrideCursor()
return wrapper
class DemoWidget(QWidget):
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
uic.loadUi(DEMOWINDOW_UI_FILE, self)
self.black_mustache = 'style/img/bs19_black_icon.png'
self.yellow_mustache = 'style/img/bs19_yellow_icon.png'
self.toggle_mustache(True)
self.checkSensorButton.clicked.connect(self.set_sensor_status)
self.initDemoButton.clicked.connect(self.run_demo)
# Events.
self.closeEvent = self.close_event
self.demo_running = False
self.show()
self.checkSensorButton.setAutoFillBackground(True)
self.initDemoButton.setAutoFillBackground(True)
def close_event(self, event):
try:
self.demo_running = False
self.demo_thread.join()
except AttributeError as e:
pass
def change_button_colors(self, btn, background, color):
btn.setStyleSheet('background-color: {};'
'color: {};'.format(background, color))
btn.setAutoFillBackground(True)
def toggle_mustache(self, b):
mustache = self.black_mustache if b else self.yellow_mustache
self.mustacheLabel.setText(
'<html><head/><body><p><img src="{}"/></p></body></html>'
.format(mustache))
def set_sensor_status(self):
if demo_receiver.check_sensor_active():
self.sensorStatus.setStyleSheet('background-color: "#21c226"')
else:
self.sensorStatus.setStyleSheet('background-color: "red"')
# Needed as an inner thread to keep the GUI responsive during
# demo. This is the cleanest way we found to make sure that we can
# exit the demo window and make sure that the demo ends.
def _run_demo(self):
self.demo_running = True
self._demo_thread = sthread.StoppableThread(
target=demo_receiver.connect_to_grinder, args=(False,))
self._demo_thread.start()
while self.demo_running:
if not demo_receiver.prediction_event.wait(timeout=1):
continue
else:
self.toggle_mustache(not demo_receiver.stimuli_state)
demo_receiver.prediction_event.clear()
self._demo_thread.stop()
self._demo_thread.join()
def run_demo(self):
if self.initDemoButton.text() == 'START DEMO':
self.demo_thread = sthread.StoppableThread(target=self._run_demo)
self.demo_thread.start()
self.initDemoButton.setText('STOP DEMO')
self.change_button_colors(self.initDemoButton, 'red', 'white')
else:
self.close_event(None)
self.initDemoButton.setText('START DEMO')
self.change_button_colors(self.initDemoButton, '#21c226', 'white')
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
# This is required as Linux uses fork instead of spawn to
# create the new window. This will crash with an X error if
# the spawn method is not set.
multiprocessing.set_start_method('spawn')
uic.loadUi(MAINWINDOW_UI_FILE, self)
self.setWindowTitle('SiNRI')
self.setObjectName("SiNRI")
# self.setWindowIcon(QIcon('style/SiNRI_logo.svg'))
with open(MAINWINDOW_CSS_FILE) as style_file:
self.setStyleSheet(style_file.read())
self.startMockButton.clicked.connect(self.toggle_mock)
self.startGrinderButton.clicked.connect(self.toggle_grinder)
self.reflectButton.clicked.connect(self.toggle_reflect)
self.startCleavizButton.clicked.connect(self.start_cleaviz)
self.startDemoButton.clicked.connect(self.init_demo_widget)
self.stimuliSetupButton.clicked.connect(self.setup_stimuli)
self.stimuliStartButton.clicked.connect(self.start_stimuli)
self.stimuliStopButton.clicked.connect(self.stop_stimuli)
self.selectExperimentButton.clicked.connect(self.select_experiment)
self.bucketingButton.clicked.connect(
lambda: self.run_analysis_example(analysis.bucketing_example))
self.plottingButton.clicked.connect(
lambda: self.run_analysis_example(analysis.plotting_example))
self.peakDetectionButton.clicked.connect(
lambda: self.run_analysis_example(analysis.peak_detection_example))
self.summaryButton.clicked.connect(
lambda: self.run_analysis_example(analysis.peak_detection_summary_example))
self.smaButton.clicked.connect(
lambda: self.run_analysis_example(analysis.simple_moving_average_example))
self.spectralAnalysisButton.clicked.connect(
lambda: self.run_analysis_example(analysis.spectral_analysis_example))
# Events.
self.closeEvent = self.close_event
self.setWindowIcon(QIcon('style/img/bs19_yellow_icon.png'))
self.show()
# I have no idea why this is needed, the designer is not of
# much help here. Jeez.
self.startGrinderButton.setAutoFillBackground(True)
self.reflectButton.setAutoFillBackground(True)
self.startMockButton.setAutoFillBackground(True)
self.startCleavizButton.setAutoFillBackground(True)
self.stimuliSetupButton.setAutoFillBackground(True)
self.stimuliStartButton.setAutoFillBackground(True)
self.stimuliStopButton.setAutoFillBackground(True)
# Connect stdout to the status area, as this needs to be
# synchronized with a blocking queue.
self.add_custom_log_handler()
self.log_q = queue.Queue()
self.log_stream = LogStream(self.log_q)
sys.stdout = self.log_stream
self.log_t = QThread()
self.log_signal_obj = QLogOutputter(self.log_q)
self.log_signal_obj.stdout_signal.connect(self.output_log)
self.log_signal_obj.moveToThread(self.log_t)
self.log_t.started.connect(self.log_signal_obj.run)
self.log_t.start()
self.mock_running = False
self.grinder_running = False
self.reflect = True
self.meamer = meamer.MEAMEr('10.20.92.130')
def close_event(self, event):
self.log_signal_obj.running = False
if self.mock_running:
self.stop_mock()
if self.grinder_running:
self.stop_grinder()
# We need to force re-evaluation of logging thread with print
# to exit.
print('Logging thread exited')
self.log_t.terminate()
self.log_t.wait()
def add_custom_log_handler(self):
self.logging_handler = QLoggingHandler()
for logger_str in logging.root.manager.loggerDict:
logging.getLogger(logger_str).addHandler(self.logging_handler)
def output_log(self, text):
self.logArea.moveCursor(QTextCursor.End)
self.logArea.insertPlainText(text)
def start_cleaviz(self):
# self.r, self.w = multiprocessing.Pipe()
# self.mp_reader = os.fdopen(self.r.fileno(), 'r')
# self.log_signal_obj.mp_reader = self.mp_reader
p = Process(target=fork_cleaviz)
p.start()
def init_demo_widget(self):
self.demo_widget = DemoWidget()
def change_button_colors(self, btn, background, color):
btn.setStyleSheet('background-color: {};'
'color: {};'.format(background, color))
btn.setAutoFillBackground(True)
def set_button_style(self, btn, background, color, text):
self.change_button_colors(btn, background, color)
btn.setText(text)
def start_mock(self):
try:
if not self.mockThread.stopped():
return
except AttributeError:
pass
meame_mock = mock.MEAMEMock(12340)
self.mockThread = sthread.StoppableThread(target=meame_mock.run)
self.mockThread.start()
def stop_mock(self):
self.mockThread.stop()
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
try:
s.connect(('localhost', 12340))
except ConnectionRefusedError:
pass
self.mockThread.join()
def toggle_mock(self):
self.mock_running = self.mock_running ^ True
if self.mock_running:
self.start_mock()
self.set_button_style(self.startMockButton, 'red', 'white', 'STOP MOCK')
else:
self.stop_mock()
self.set_button_style(self.startMockButton, '#21c226', 'white', 'START MOCK')
def _start_grinder(self):
try:
self.server = grinder.Server(8080,
reflect=self.reflect,
# sawtooth=True,
meame_addr='10.20.92.130')
self.server.listen()
except Exception as e:
logger.info('Shutting down gracefully')
self.server.socket.shutdown(socket.SHUT_RDWR)
def start_grinder(self):
self.grndThread = sthread.StoppableThread(target=self._start_grinder, args=(), kwargs={})
self.grndThread.start()
def stop_grinder(self):
self.grndThread.stop()
try:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect(('localhost', 8080))
except ConnectionRefusedError as e:
pass
self.grndThread.join()
def toggle_grinder(self):
time.sleep(0.01)
self.grinder_running = self.grinder_running ^ True
if self.grinder_running:
self.start_grinder()
self.set_button_style(self.startGrinderButton, 'red', 'white', 'STOP GRINDER')
else:
self.stop_grinder()
self.set_button_style(self.startGrinderButton, '#21c226', 'white', 'START GRINDER')
def toggle_reflect(self):
self.reflect ^= True
if self.reflect:
self.set_button_style(self.reflectButton, '#21c226', 'white', '')
else:
self.set_button_style(self.reflectButton, 'red', 'white', '')
try:
self.server.reflect = self.reflect
except AttributeError:
pass
def setup_stimuli(self):
self.meamer.setup_stim()
def start_stimuli(self):
self.meamer.start_stim()
self.meamer.setup_stim()
def stop_stimuli(self):
self.meamer.stop_stim()
def select_experiment(self):
logger.info("Browsing Experiment Files...")
file_name, _ = QFileDialog.getOpenFileName(self, str("Open File"), './mea_data')
if file_name:
logger.info("Setting file name: " + file_name)
analysis.experiment_fp = file_name
# Not strictly needed anymore, was used for threading before.
@analysis_loading
def run_analysis_example(self, example):
example()
def main():
app = pg.QtGui.QApplication([])
main_window = MainWindow()
app.exec_()
if __name__ == '__main__':
main()