-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
444 lines (383 loc) · 17.1 KB
/
Copy pathmain.py
File metadata and controls
444 lines (383 loc) · 17.1 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
import subprocess
import tkinter as tk
from tkinter import messagebox
import tkinter.simpledialog
from tkinter import ttk
from brain import Brain
from utils import *
from gen_data import SignalGen
import threading
import socket
import argparse
import os
import time
import sync_post_process
class MainWindow(tk.Tk):
def __init__(self, use_lab: bool, use_sim: bool):
super().__init__()
self.title("BrainSync Recorder")
self.geometry("600x650")
style = ttk.Style()
style.theme_use('clam')
# --- Data & State ---
self.use_lab = use_lab
self.use_sim = use_sim
self.lab_recorder = None
self.name = None
self.surname = None
self.age = None
self.parts = 0
self.run_no = 0
self.sel_pat = False
self.exper: Brain = None
self.data = []
self.running = False
self.COM_port = None
# Ensure the data folder exists
if not os.path.exists('data'):
os.mkdir('data')
if self.use_lab:
try:
self.lab_recorder = socket.create_connection(("localhost", 22345))
self.lab_recorder.sendall(b"select all\n")
except ConnectionRefusedError:
messagebox.showerror("Error", "Could not connect to LabRecorder (localhost:22345)")
if self.use_sim:
self.exper = SignalGen(1, 256, self.lab_recorder)
# GUI layout
main_frame = ttk.Frame(self, padding="20")
main_frame.pack(fill=tk.BOTH, expand=True)
# Section 1: Patient Management
patient_frame = ttk.LabelFrame(main_frame, text="Patient Management", padding="10")
patient_frame.grid(row=0, column=0, sticky="ew", pady=(0, 15))
main_frame.columnconfigure(0, weight=1)
self.info_label = ttk.Label(patient_frame, text="No Patient Selected", font=("Helvetica", 12, "bold"))
self.info_label.grid(row=0, column=0, columnspan=3, pady=(0, 10))
self.select_button = ttk.Button(patient_frame, text="Select / New Patient", command=self.select_patient)
self.select_button.grid(row=1, column=0, padx=5, sticky="ew")
self.unset_button = ttk.Button(patient_frame, text="Unset Patient", command=self.unset_pat)
self.unset_button.grid(row=1, column=1, padx=5, sticky="ew")
patient_frame.columnconfigure(0, weight=1)
patient_frame.columnconfigure(1, weight=1)
# Section 2: Configuration
config_frame = ttk.LabelFrame(main_frame, text="Configuration", padding="10")
config_frame.grid(row=1, column=0, sticky="ew", pady=(0, 15))
config_frame.columnconfigure(1, weight=1)
ttk.Label(config_frame, text="COM Port:").grid(row=0, column=0, sticky="w", pady=5)
self.port_combo = ttk.Combobox(config_frame, state="readonly", values=[i for i in range(1, 11)])
self.port_combo.grid(row=0, column=1, padx=10, sticky="ew")
self.confirm_port_button = ttk.Button(config_frame, text="Set Port", command=self.set_port)
self.confirm_port_button.grid(row=0, column=2, padx=5)
ttk.Label(config_frame, text="Session Part:").grid(row=1, column=0, sticky="w", pady=5)
self.parts_combo = ttk.Combobox(config_frame, state="readonly", values=[i for i in range(1, 8)])
self.parts_combo.grid(row=1, column=1, padx=10, sticky="ew")
self.confirm_parts_button = ttk.Button(config_frame, text="Set Part", command=self.set_selected_part)
self.confirm_parts_button.grid(row=1, column=2, padx=5)
self.rem_part_button = ttk.Button(config_frame, text="Remove Last Part", command=self.rem_part)
self.rem_part_button.grid(row=2, column=0, columnspan=3, pady=(10, 0), sticky="ew")
# Section 3: Recording Controls
control_frame = ttk.LabelFrame(main_frame, text="Recording Control", padding="10")
control_frame.grid(row=2, column=0, sticky="ew", pady=(0, 15))
control_frame.columnconfigure(0, weight=1)
control_frame.columnconfigure(1, weight=1)
self.start_button = ttk.Button(control_frame, text="START RECORDING", command=self.start_record)
self.start_button.grid(row=0, column=0, ipady=15, padx=5, sticky="ew")
self.stop_button = ttk.Button(control_frame, text="STOP", command=self.stop_record)
self.stop_button.grid(row=0, column=1, ipady=15, padx=5, sticky="ew")
# Section 4: Status Monitor
status_frame = ttk.LabelFrame(main_frame, text="System Status", padding="10")
status_frame.grid(row=3, column=0, sticky="nsew")
main_frame.rowconfigure(3, weight=1)
self.lbl_part_status = ttk.Label(status_frame, text="Current Part: 0")
self.lbl_part_status.grid(row=0, column=0, sticky="w", padx=10, pady=2)
self.lbl_run_status = ttk.Label(status_frame, text="Current Run: 0")
self.lbl_run_status.grid(row=1, column=0, sticky="w", padx=10, pady=2)
self.lbl_active_status = ttk.Label(status_frame, text="Is Running: False", foreground="red")
self.lbl_active_status.grid(row=2, column=0, sticky="w", padx=10, pady=2)
self.port_info = ttk.Label(status_frame, text="COM Port: Not Set")
self.port_info.grid(row=3, column=0, sticky="w", padx=10, pady=2)
self.lbl_sync_status = ttk.Label(status_frame, text="Sync Status: Idle")
self.lbl_sync_status.grid(row=4, column=0, sticky="w", padx=10, pady=2)
def toggle_ui_state(self, is_recording):
"""
Disables or enables UI components based on recording state.
If is_recording is True, everything except STOP is disabled.
"""
state = "disabled" if is_recording else "normal"
# Disable/Enable Patient Controls
self.select_button.config(state=state)
self.unset_button.config(state=state)
# Disable/Enable Configuration
self.port_combo.config(state=state)
self.confirm_port_button.config(state=state)
self.parts_combo.config(state=state)
self.confirm_parts_button.config(state=state)
self.rem_part_button.config(state=state)
# Disable/Enable Start Button
self.start_button.config(state=state)
def send_msg(self, msg: str):
"""
send message to LabRecorder instance
"""
if self.use_lab and self.lab_recorder:
try:
self.lab_recorder.sendall(msg)
except Exception as e:
print(f"Error sending to LabRecorder: {e}")
def send_start(self):
if self.use_sim:
self.exper.sendData("1_Hz", "256_Hz")
else:
try:
self.exper = Brain(connect2headset(f"COM{self.COM_port}"))
self.thread = threading.Thread(target=self.exper.read_serial_data, daemon=True)
self.thread.start()
self.send_msg(b"start\n")
except:
self._wrong_port_()
def send_stop(self):
if self.use_sim:
self.exper.running = False
self.exper.stopData()
else:
self.exper.continue_running = False
self.exper.stop_serial_data()
self.send_msg(b"stop\n")
self.thread.join(timeout=2)
self.running = False
def set_selected_part(self):
val = self.parts_combo.get()
if val:
self.parts = int(val)
self.set_lab_dir()
self.update_parts_label()
messagebox.showinfo("Part Set", f"Selected Part: {self.parts}")
else:
messagebox.showwarning("No Selection", "Please select a part from the list.")
def get_participant_str(self):
return f"{self.name}_{self.surname}_{self.age}"
def set_lab_dir(self, run=1):
if not self.name:
return
participant = self.get_participant_str()
session = self.parts
# Template: %p = participant, %s = session, %r = run
param_str = f"{{run:{run}}} {{participant:{participant}}} {{session:{session}}} {{task:{run}}} {{modality:eeg}} {{block:{run}}}\n"
# LabRecorder stores in: Root / Participant / Session / LabRecorder / run.xdf
send_msg = b"filename {template:%p/%s/labrecorder/%b.xdf} " + param_str.encode()
self.send_msg(send_msg)
def update_parts_label(self):
self.lbl_part_status.config(text=f"Current Part: {self.parts}")
self.lbl_run_status.config(text=f"Current Run: {self.run_no}")
if self.name:
pat_text = f"{self.name} {self.surname} ({self.age})"
else:
pat_text = "No Patient Selected"
self.info_label.config(text=pat_text)
self.lbl_active_status.config(text=f"Is Running: {self.running}", foreground="green" if self.running else "red")
def update_COM_label(self):
self.port_info.config(text=f"COM Port: COM{self.COM_port}")
# Messages
def _patient_selection_(self, text=''):
messagebox.showinfo("Selection Info", f"Patient {text} selected.")
def _type_data_(self):
dialog = tk.Toplevel()
dialog.title("New Patient Entry")
dialog.geometry("350x250")
tk.Label(dialog, text="Name:").grid(row=0, column=0, padx=10, pady=10, sticky="e")
entry_name = tk.Entry(dialog)
entry_name.grid(row=0, column=1, padx=10, pady=10)
tk.Label(dialog, text="Surname:").grid(row=1, column=0, padx=10, pady=10, sticky="e")
entry_surname = tk.Entry(dialog)
entry_surname.grid(row=1, column=1, padx=10, pady=10)
tk.Label(dialog, text="Age:").grid(row=2, column=0, padx=10, pady=10, sticky="e")
entry_age = tk.Entry(dialog)
entry_age.grid(row=2, column=1, padx=10, pady=10)
def validate_and_close():
self.name = entry_name.get().strip()
self.surname = entry_surname.get().strip()
self.age = entry_age.get().strip()
if self.name and self.surname and self.age:
try:
int(self.age)
# Note: assuming add_patient is in utils
try:
exists = not add_patient(self.name, self.surname, self.age)
except NameError:
exists = False
if exists:
self._sel_existing_()
else:
self.sel_pat = True
self.parts = 1
self.set_lab_dir()
self.update_parts_label()
dialog.destroy()
except ValueError:
messagebox.showwarning("Invalid Input", "Age must be a number.")
else:
messagebox.showwarning("Missing Fields", "Please fill in all fields.")
ok_button = ttk.Button(dialog, text="Create Patient", command=validate_and_close)
ok_button.grid(row=3, column=0, columnspan=2, pady=20, sticky="ew", padx=20)
dialog.transient(self)
dialog.grab_set()
self.wait_window(dialog)
def _sel_existing_(self):
if messagebox.askyesno("Patient Exists", "Patient already exists. Select them?"):
self.parts = 1
self.set_lab_dir()
self.sel_pat = True
self.update_parts_label()
else:
self.name = None
self.surname = None
self.age = None
def _type_run_(self):
run_input = tk.simpledialog.askinteger("Run Number", f"Current Part: {self.parts}\nEnter Run Number:", parent=self, minvalue=1)
if run_input is not None:
self.run_no = run_input
self.set_lab_dir(self.run_no)
self.update_parts_label()
return True
return False
def _wrong_port_(self):
messagebox.showerror("Connection Error", f"Could not connect to COM{self.COM_port}")
def set_port(self):
val = self.port_combo.get()
if val:
self.COM_port = int(val)
self.update_COM_label()
else:
messagebox.showwarning("No Selection", "Please select a COM port from the list.")
def res_pat(self):
if not self.sel_pat:
self._patient_selection_("not")
return
try:
reset_patient(self.name, self.surname, self.age)
except NameError: pass
self.parts = 0
self.update_parts_label()
def rem_part(self):
if not self.sel_pat:
self._patient_selection_("not")
return
try:
remove_part(self.name, self.surname, self.age, self.parts)
except NameError: pass
if self.parts > 0:
self.parts -= 1
else:
self.parts += 1
self.update_parts_label()
def unset_pat(self):
self.name = None
self.surname = None
self.age = None
self.parts = 0
self.sel_pat = False
self.update_parts_label()
# --- Recording & Processing Logic ---
def select_patient(self):
if self.sel_pat:
messagebox.showinfo("Info", "Patient already selected. Unset first to change.")
return
self.parts = 0
self.name = None
self.surname = None
self.age = None
self.update_parts_label()
self._type_data_()
def start_record(self):
if not self.sel_pat:
messagebox.showwarning("Required", "Please select a patient first.")
return
if not self.running:
if self._type_run_():
print("Connecting...")
self.send_start()
self.running = True
# --- Lock the UI ---
self.toggle_ui_state(True)
self.update_parts_label()
self.lbl_sync_status.config(text="Sync Status: Recording...")
def stop_record(self):
if not self.sel_pat:
return
if self.running:
print("Shutting down connection")
self.send_stop()
self.update_parts_label()
self.toggle_ui_state(False)
# Trigger Post-Processing
if self.use_lab:
threading.Thread(target=self._wait_and_process, daemon=True).start()
def _wait_and_process(self):
"""
Calculates expected file path in the DATA folder, waits for it, runs sync,
and updates patients.json.
"""
self.lbl_sync_status.config(text="Sync Status: Waiting for file...", foreground="orange")
part_str = self.get_participant_str()
expected_path = os.path.join(
os.getcwd(),
"data",
part_str,
str(self.parts),
"labrecorder",
f"{self.run_no}.xdf"
)
print(f"Looking for: {expected_path}")
# wait loop
found = False
for _ in range(20):
if os.path.exists(expected_path):
found = True
break
time.sleep(0.5)
if found:
self.lbl_sync_status.config(text="Sync Status: Processing...", foreground="blue")
try:
csv_out = sync_post_process.process_and_save(expected_path)
if csv_out:
self.lbl_sync_status.config(text="Sync Status: Saved CSV", foreground="green")
success = add_run_path(self.name, self.surname, self.age, self.parts, self.run_no, csv_out)
if success:
print(f"[System] Successfully logged Run {self.run_no} to patients.json")
else:
print("[System] Failed to update patients.json")
else:
self.lbl_sync_status.config(text="Sync Status: Error in Sync", foreground="red")
except Exception as e:
print(f"Sync error: {e}")
self.lbl_sync_status.config(text="Sync Status: Exception", foreground="red")
else:
print("File not found (LabRecorder path mismatch?)")
self.lbl_sync_status.config(text="Sync Status: File Not Found", foreground="red")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='App args')
parser.add_argument('use_lab', type=int, help='use labrecorder (1/0)')
parser.add_argument('use_sim', type=int, help='use simulator (1/0)')
args = parser.parse_args()
use_lab = bool(args.use_lab)
use_sim = bool(args.use_sim)
lab_proc = None
# LabRecorder process is needed for LSL integration
if use_lab:
try:
lab_proc = subprocess.Popen(["LabRecorder"])
time.sleep(2)
except FileNotFoundError:
print("Warning: 'LabRecorder' command not found. Please start it manually.")
except Exception as e:
print(f"Error starting LabRecorder: {e}")
app = MainWindow(use_lab, use_sim)
app.mainloop()
if lab_proc:
print("Terminating LabRecorder...")
lab_proc.terminate()
try:
lab_proc.wait(timeout=3)
except subprocess.TimeoutExpired:
print("Force killing LabRecorder...")
lab_proc.kill()