-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_menu.py
More file actions
601 lines (524 loc) · 22.7 KB
/
main_menu.py
File metadata and controls
601 lines (524 loc) · 22.7 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
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
"""
TO DO:
Main Menu:
Add in hardware/model:
unicorn
muse (2/S)
Compartmentalize the board id grab in utils (pass in hardware/model/datatype)
Impedence menu
Look for muse impedance check scripts
Confirm that the OpenBCI Impedence checks are working properly
For time sync - add back in pyLSL?
Render GA ERPs in results window from the either the baseline or the session
Choose from baseline/session
Logisitics:
Send Paul another Muse and possibly an arduino + light?
M todo
order of events lets you try and fauil tomopen graph wo selecting com port
one dropdown for hardware (<-eden no likey)
implement impedanece for all,not just cyton daisy
add support for non openbci hardware
add option to not import tensorflow
in train model, has hard coded 16 channel # (fix)
opens windows:
graph window - shows live timeseries
-potentially make it configurable
- label on garph which line is which channel by chcking hardware
impedance window
-curently hacked together, obnly cyton daiusy
-implement with other
arduino
-debug requires putting in 1
-preset for neuorstimduino
- need dosc for how to upload script to arduino using arduino ide, attach led
- currently provides a way to turn led on arduino on and off on command
baseline
- basically like the oddball window
- outputs eeg file in brainflow format
- new plan: use pylsl sender to constantly grab brainflow and events and send them together, so ww can be sure of times
saving
- sqlite prob overkill
- use numpy
- later maybe add sqlite to use if run for long time
remove unecessary windows
- we don't need a model window with tensorflow to train a thing. this isn't koalacademy
ADD SIMULATE AS HARDWARE OPTION
make board id happen in menu window so not passing raw srtrings between windows
"""
import logging
import os
import random
import sys
import time
import numpy as np
from PyQt5 import Qt, QtCore, QtGui
from PyQt5.QtOpenGL import *
from PyQt5.QtWidgets import *
from Board import (
BCI,
CONNECT,
CYTON,
CYTON_DAISY,
GANGLION,
MUSE,
MUSE_2,
MUSE_S,
SIMULATE,
PILL,
EXG_PILL,
GTEC,
UNICORN,
get_board_id,
)
from src.board.exg_pill import ExgPill
# Creates the global logger
log_file = "boiler.log"
logging.basicConfig(level=logging.INFO, filemode="a")
f = logging.Formatter(
"Logger: %(name)s: %(levelname)s at: %(asctime)s, line %(lineno)d: %(message)s"
)
stdout = logging.StreamHandler(sys.stdout)
boiler_log = logging.FileHandler(log_file)
stdout.setFormatter(f)
boiler_log.setFormatter(f)
logger = logging.getLogger("MenuWindow")
logger.addHandler(boiler_log)
logger.addHandler(stdout)
logger.info("Program started at {}".format(time.time()))
# from spectrograph import spectrograph_gui
from baseline_window import BaselineWindow
# results not implemented yet
from graph_window import graph_win
from impedance_window import impedance_win
if False: # debugging... remebeber to put the tf imports back in session_window
import tensorflow as tf
if sys.platform == "win32":
from arduino_windows import ard_wind_on as ard_turn_on
else:
from arduino_mac import ArduinoMacos as ard_turn_on
# let's make a menu window class
class MenuWindow(QMainWindow):
def __init__(self, parent=None):
"""The init function, creates the user interface for the main menu.
Args:
parent (QWindow, optional): The parent of the main window. Defaults to None.
"""
super().__init__()
logger.info("Initializing")
####################################
##### Init Main Window Globals #####
####################################
"""
-------------------INPUTS-------------------|
| TITLE |
| HARDWARE TYPE |
| MODEL PORT |
| CSV ARDUINO |
| |
|------------------ACTIONS------------------|
| arduino graph imped |
|-------------------------------------------|
"""
self.setMinimumSize(900, 950)
# self.setStyleSheet("background-color: gray;")
# setting window title and icon
self.setWindowTitle("PyQt6 Menu")
self.setWindowIcon(QtGui.QIcon("utils/logo_icon.jpg"))
# init layout
self.layout = QGridLayout()
widget = QWidget()
widget.setLayout(self.layout)
self.setCentralWidget(widget)
### DEBUG ###
self.debug = False
if self.debug == True:
self.bci_serial_port = "COM1"
self.arduino_con = "Debug"
self.arduino_serial_port = "COM2"
###################################
##### Init GUI Input Elements #####
###################################
### INIT INPUT LAYOUTS ###
# Create layouts explicitly for all GUI input fields
self.title_layout = QHBoxLayout()
self.hardware_layout = QVBoxLayout()
self.model_layout = QVBoxLayout()
self.type_layout = QVBoxLayout()
self.port_layout = QVBoxLayout()
self.csv_layout = QVBoxLayout()
self.arduino_layout = QVBoxLayout()
"""
|------------------INPUTS-------------------|
| |
| HARDWARE TYPE |
| MODEL PORT |
| CSV ARDUINO |
| |
|-------------------------------------------|
"""
self.hardware = None
self.model = None
self.data_type = None
self.board_id = None
### TITLE ###
self.title = QLabel()
self.title.setFont(QtGui.QFont("Arial", 14))
self.title.setText("Select hardware")
self.title_layout.addWidget(self.title)
### HARDWARE ###
# drop down menu to decide what hardware
self.hardware_dropdown = QComboBox()
self.hardware_dropdown.setPlaceholderText("Select hardware")
self.hardware_dropdown.addItems([BCI, MUSE, PILL, GTEC])
self.hardware_dropdown.activated.connect(self.handle_hardware_choice)
self.hardware_label = QLabel("Select hardware")
self.hardware_layout.addWidget(self.hardware_label)
self.hardware_layout.addWidget(self.hardware_dropdown)
### MODEL ###
# drop down menu for model of hardware
self.model_dropdown = QComboBox()
self.model_dropdown.setPlaceholderText("Select model")
self.model_label = QLabel("Select model")
self.model_dropdown.setEnabled(False) # starts disabled
self.model_dropdown.activated.connect(self.handle_model_choice)
self.model_layout.addWidget(self.model_label)
self.model_layout.addWidget(self.model_dropdown)
### CSV ###
self.csv_name = "eeg_" + log_file[:-4] + ".csv"
self.csv_name_edit = QLineEdit(self.csv_name)
self.csv_name_edit.returnPressed.connect(self.csv_name_changed)
self.csv_label = QLabel(
"Prefix of session's CSV file.\nHit 'Enter' to update filename."
)
self.csv_layout.addWidget(self.csv_label)
self.csv_layout.addWidget(self.csv_name_edit)
### DATATYPE ###
# drop down menu for simulate or live (previously included file step through)
self.type_dropdown = QComboBox()
self.type_dropdown.setPlaceholderText("Select data type")
self.type_dropdown.addItems([CONNECT, SIMULATE])
self.type_dropdown.activated.connect(self.handle_type_choice)
self.type_label = QLabel("Select data type")
self.type_layout.addWidget(self.type_label)
self.type_layout.addWidget(self.type_dropdown)
if self.debug == True:
self.type_dropdown.setEnabled(True) # start disabled
else:
self.type_dropdown.setEnabled(False) # start disabled
### PORT ###
self.bci_port_label = QLabel("BCI Serial Port")
self.bci_port = QLineEdit()
self.bci_port.setEnabled(False)
self.port_layout.addWidget(self.bci_port_label)
self.port_layout.addWidget(self.bci_port)
self.bci_port.setPlaceholderText("Enter Port # (Integers Only)")
self.bci_port.textEdited.connect(self.handle_bci_port)
self.bci_serial_port = None # if None gets passed to the graph window, it will look for a working port
### ARDUINO ###
self.arduino_label = QLabel("Arduino Settings")
self.arduino_dropdown = QComboBox()
self.arduino_dropdown.setPlaceholderText("Select connection to arduino")
self.arduino_dropdown.addItems(["Wired", "NeuroStimDuino", "Wireless", "Debug"])
self.arduino_dropdown.activated.connect(self.handle_arduino_dropdown)
self.arduino_port = QLineEdit()
self.arduino_port.setEnabled(False)
self.arduino_port.setPlaceholderText("Enter Port # (Integers Only)")
self.arduino_port.textEdited.connect(self.handle_arduino_port)
self.arduino_layout.addWidget(self.arduino_label)
self.arduino_layout.addWidget(self.arduino_dropdown)
self.arduino_layout.addWidget(self.arduino_port)
# self.arduino_process = None
### ADD INPUT SUBLAYOUTS TO MAIN ###
self.layout.setContentsMargins(100, 100, 100, 100)
self.hardware_layout.setContentsMargins(50, 50, 50, 50)
self.model_layout.setContentsMargins(50, 50, 50, 50)
self.csv_layout.setContentsMargins(50, 50, 50, 15)
self.type_layout.setContentsMargins(50, 50, 50, 50)
self.port_layout.setContentsMargins(50, 50, 50, 50)
self.arduino_layout.setContentsMargins(50, 50, 50, 15)
self.layout.addLayout(self.title_layout, 0, 0, 1, -1, QtCore.Qt.AlignHCenter)
self.layout.addLayout(self.hardware_layout, 1, 0)
self.layout.addLayout(self.model_layout, 2, 0)
self.layout.addLayout(self.csv_layout, 3, 0)
self.layout.addLayout(self.type_layout, 1, 1)
self.layout.addLayout(self.port_layout, 2, 1)
self.layout.addLayout(self.arduino_layout, 3, 1)
####################################
##### Init GUI Action Elements #####
####################################
"""
|------------------ACTIONS------------------|
| |
| arduino graph imped |
| baseline |
|-------------------------------------------|
"""
# here is a button to actually start a impedance window
self.impedance_window_button = QPushButton("Impedance Check")
self.impedance_window_button.setEnabled(False)
self.layout.addWidget(
self.impedance_window_button, 5, 1, 1, 1, QtCore.Qt.AlignHCenter
)
self.impedance_window_button.clicked.connect(self.open_impedance_window)
# here is a button to start the arduino window
self.arduino_window_button = QPushButton("Turn on Arduino")
self.arduino_window_button.setEnabled(False)
self.layout.addWidget(
self.arduino_window_button, 5, 0, 1, 1, QtCore.Qt.AlignHCenter
)
self.arduino_window_button.clicked.connect(self.open_arduino_window)
# here is a button to display graph
self.graph_window_button = QPushButton("Graph")
self.graph_window_button.setEnabled(False)
self.layout.addWidget(
self.graph_window_button, 5, 0, 1, -1, QtCore.Qt.AlignHCenter
)
self.graph_window_button.clicked.connect(self.open_graph_window)
# this is a variable to show whether we have a data window open
self.data_window_open = False
# this is a variable to show whether we have a impedance window open
self.impedance_window_open = False
# here is a button for the baseline window
self.baseline_window_button = QPushButton("Baseline")
self.baseline_window_button.setEnabled(False)
self.layout.addWidget(
self.baseline_window_button, 6, 0, 1, 1, QtCore.Qt.AlignHCenter
)
self.baseline_window_button.clicked.connect(self.open_baseline_window)
# targ limb
self.targ_limb = None
def closeEvent(self, event):
"""Autoruns before the window closes. Ensures that all running streams are terminated.
Args:
event (?): The close event.
"""
# this code will autorun just before the window closes
# we will check whether streams are running, if they are we will close them
logger.info("Closing")
if self.data_window_open:
self.data_window.close()
if self.impedance_window_open:
self.impedance_window.close()
event.accept()
#########################################
##### Functions for Handling Inputs #####
#########################################
def handle_hardware_choice(self):
"""Handles changes to the hardware dropdown"""
self.hardware = self.hardware_dropdown.currentText()
for btn in [
self.graph_window_button,
self.baseline_window_button,
self.impedance_window_button,
]:
btn.setEnabled(False)
# handle the choice of hardware - by opening up model selection
self.model_dropdown.setEnabled(True)
self.type_dropdown.setEnabled(False)
self.type_dropdown.setCurrentIndex(-1)
self.title.setText("Select model")
self.model_dropdown.clear()
if self.hardware_dropdown.currentText() == BCI:
self.model_dropdown.addItems([GANGLION, CYTON, CYTON_DAISY])
elif self.hardware_dropdown.currentText() == MUSE:
self.model_dropdown.addItems([MUSE_2, MUSE_S])
elif self.hardware_dropdown.currentText() == PILL:
self.model_dropdown.addItems([EXG_PILL])
elif self.hardware_dropdown.currentText() == GTEC:
self.model_dropdown.addItem(UNICORN)
def handle_model_choice(self):
"""Handles changes to the model dropdown"""
# handle the choice of model by opening up data type selection
self.model = self.model_dropdown.currentText()
for btn in [
self.graph_window_button,
self.baseline_window_button,
self.impedance_window_button,
]:
btn.setEnabled(False)
self.bci_port.setEnabled(False)
self.type_dropdown.setEnabled(True)
self.type_dropdown.setCurrentIndex(-1)
self.title.setText("Select data type")
def csv_name_changed(self):
"""Handles changes to the csv_name text field."""
# Ensures that the file is a csv file (the format to be written)
if not self.csv_name_edit.text().endswith(".csv"):
# add .csv ending if absent
self.csv_name_edit.setText(self.csv_name_edit.text() + ".csv")
# Ensures that the filename does not exist on the system
filename = self.csv_name_edit.text()
if os.path.isfile(filename):
root = os.path.splitext(filename)[0] # <name>.csv -> <name>
i = 1
while os.path.isfile(filename):
filename = f"{root}_{i}.csv"
i += 1
# Prompts the user to select a directory for file saving
save_directory = QFileDialog.getExistingDirectory()
self.csv_name = os.path.join(save_directory, filename)
logger.info("Selected save location: {}".format(self.csv_name))
def handle_type_choice(self):
"""Handles changes to the data type drop down."""
# handle the choice of data type
self.data_type = self.type_dropdown.currentText()
self.graph_window_button.setEnabled(True)
self.baseline_window_button.setEnabled(True)
self.impedance_window_button.setEnabled(True)
if self.data_type == CONNECT:
self.title.setText("Select BCI Hardware Port")
self.bci_port.setEnabled(True)
self.board_id = get_board_id(self.data_type, self.hardware, self.model)
elif self.data_type == SIMULATE:
self.title.setText("Check impedance or graph")
self.board_id = -1
def handle_bci_port(self):
"""Handles actions made within the bci_port text field"""
# check for correct value entering and enable type dropdown menu
if self.bci_port.text().isdigit():
self.type_dropdown.setEnabled(True)
self.bci_serial_port = "COM" + self.bci_port.text()
if self.data_type == CONNECT:
self.impedance_window_button.setEnabled(True)
self.title.setText("Check impedance or graph")
else:
self.bci_serial_port = None
# print("Error: OpenBCI port # must be an integer.")
self.title.setText("Select BCI Hardware Port")
def handle_arduino_dropdown(self):
"""Handles actions within the arduino dropdown"""
# check if arduino checkbox is enabled
self.arduino_con = self.arduino_dropdown.currentText()
self.arduino_port.setEnabled(True)
if self.arduino_port.text().isdigit():
self.arduino_window_button.setEnabled(True)
self.arduino_serial_port = "COM" + self.arduino_port.text()
else:
self.arduino_window_button.setEnabled(False)
def handle_arduino_port(self):
"""Handles changes to input in the arduino port field"""
# check for correct value entering and enable type dropdown menu
if self.arduino_port.text().isdigit():
self.arduino_window_button.setEnabled(True)
self.arduino_serial_port = "COM" + self.arduino_port.text()
else:
self.arduino_window_button.setEnabled(False)
#########################################
##### Functions for Opening Windows #####
#########################################
def open_arduino_window(self):
"""Opens the arduino window."""
# this actually starts the arduino testing window
# called by user pressing button, which is enabled by selecting from dropdowns
# if self.arduino_process is not None:
# self.arduino_process.terminate()
# while self.arduino_process.is_alive():
# time.sleep(0.1)
# self.arduino_process.close()
# self.arduino_process = None
logger.info("creating arduino window")
if self.arduino_port.text().isdigit() != True:
logger.warning(
"failed to create arduino window because arduino port was not an integer"
)
else:
self.data_window = ard_turn_on(
parent=self,
arduino_con=self.arduino_con,
arduino_port=self.arduino_serial_port,
)
self.data_window.show()
self.data_window_open = True
logger.info("created arduino window")
def open_impedance_window(self):
"""Opens the impedance window, moves program control over."""
if self.hardware == MUSE:
logger.error("Cannot use Muse hardware with impedances.")
elif self.checks_for_window_creation():
logger.info("creating impedance window")
self.impedance_window = impedance_win(
parent=self,
hardware=self.hardware,
model=self.model,
data_type=self.data_type,
serial_port=self.bci_serial_port,
board_id=self.board_id,
)
self.impedance_window.show()
self.impedance_window_open = True
logger.info("created impedance window")
else:
logger.info("User must fix errors before impedance window can be created.")
def open_graph_window(self):
"""Opens the graph window, moves program control over."""
if self.checks_for_window_creation():
logger.info("MenuWindow is creating graph window")
self.board = None
if self.hardware == PILL:
pill = ExgPill(self.bci_serial_port)
self.board = pill
# HOTFIX: Used to allow the pill to startup
time.sleep(4)
self.graph_window = graph_win(
parent=self,
hardware=self.hardware,
model=self.model,
data_type=self.data_type,
board_id=self.board_id,
serial_port=self.bci_serial_port,
save_file=self.csv_name,
board=self.board,
)
self.graph_window.show()
self.is_graph_window_open = True
logger.info("created graph window")
else:
logger.info("User must fix errors before graph window can be created.")
def open_baseline_window(self):
"""Opens the baseline window, moves program control over."""
if self.checks_for_window_creation():
logger.info("MenuWindow is creating baseline window")
self.baseline_window = BaselineWindow(
parent=self,
board_id=self.board_id,
csv_name=self.csv_name,
serial_port=self.bci_serial_port,
)
self.baseline_window.show()
logger.info("Created baseline window")
else:
logger.info("User must fix error before baseline window can be created.")
def checks_for_window_creation(self):
"""Checks that all attributes are properly set for both the impedance and graph window and baseline.
Logs a warning message about what must be fixed.
Returns:
bool: True if the window can be opened, False otherwise
"""
if self.hardware is None:
logger.warning(
"Hardware attribute is not set. Please fix before running graph."
)
return False
elif self.model is None:
logger.warning(
"Model attribute is not set. Please fix before running graph."
)
return False
elif self.data_type is None:
logger.warning(
"Data Type attribute is not set. Please fix before running graph."
)
return False
# TODO: Check if simulation file exists, alert if not true
elif self.data_type == SIMULATE and self.csv_name is None:
logger.warning(
"CSV file to read for simulation is not provided. Please fix before running graph."
)
return False
return True
if __name__ == "__main__":
app = QApplication(sys.argv)
win = MenuWindow()
logger.info("MenuWindow created")
win.show()
sys.exit(app.exec())