-
Notifications
You must be signed in to change notification settings - Fork 0
/
eeg_emulator_interface.py
201 lines (170 loc) · 6.78 KB
/
eeg_emulator_interface.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
import multiprocessing as mp
import time
import tkinter as tk
from argparse import ArgumentParser
from tkinter import filedialog
import numpy as np
import pylsl
import pyxdf
def launch_events_stream(stream):
stream_info = stream["info"]
sample_rate = float(stream_info["nominal_srate"][0])
info = pylsl.StreamInfo(
stream_info["name"][0],
type=stream_info["type"][0],
channel_count=int(stream_info["channel_count"][0]),
nominal_srate=sample_rate,
source_id=stream_info["uid"][0],
)
outlet = pylsl.StreamOutlet(info)
wait_times = np.diff(stream["time_stamps"])
for t, sample in zip(wait_times, stream["time_series"]):
time.sleep(t)
outlet.push_sample(list(sample))
def launch_sampled_stream(stream):
stream_info = stream["info"]
sample_rate = float(stream_info["nominal_srate"][0])
info = pylsl.StreamInfo(
stream_info["name"][0],
type=stream_info["type"][0],
channel_count=int(stream_info["channel_count"][0]),
nominal_srate=sample_rate,
source_id=stream_info["uid"][0],
)
outlet = pylsl.StreamOutlet(info)
for sample in stream["time_series"]:
time.sleep(1 / sample_rate)
outlet.push_sample(list(sample))
class WaveformPlayer(tk.Frame):
def __init__(self, master, show_markers=False):
super().__init__(master)
self.master = master
self.show_markers = show_markers
self.progress = 0.0 # Progress in percentage (0.0 to 100.0)
self.progress_interval = 100 # Interval to update progress bar in milliseconds
self.loop_var = tk.BooleanVar()
self.filename = None
self.pool = None
self.pack()
self.create_widgets()
def create_widgets(self):
self.canvas = tk.Canvas(self, width=800, height=200, bg="white")
self.canvas.pack()
self.progress_bar = self.canvas.create_rectangle(0, 0, 0, 0, fill="#FFCF67")
self.canvas.bind("<Button-1>", self.load_waveform)
# Duration label
self.duration_label = tk.Label(self, text="Duration: 0.0 seconds")
self.duration_label.pack(side=tk.LEFT)
# N channels label
self.n_streams_label = tk.Label(self, text="\tN streams: 0")
self.n_streams_label.pack(side=tk.LEFT)
self.loop_checkbox = tk.Checkbutton(
self,
text="Loop",
variable=self.loop_var, # command=self.toggle_loop
)
self.loop_checkbox.pack(side=tk.RIGHT)
def stop_loop(self):
if self.pool:
self.pool.terminate()
self.pool = None
def load_waveform(self, event):
if not self.loop_var.get():
self.stop_loop()
if not self.filename or event:
self.filename = filedialog.askopenfilename(
filetypes=[("XDF files", "*.xdf")]
)
is_first_loop = True
else:
is_first_loop = False
if self.filename:
streams, _ = pyxdf.load_xdf(self.filename)
# If there are still processes running, terminate them
if self.pool:
self.pool.terminate()
self.pool = mp.Pool(processes=len(streams))
markers = None
max_sample_rate = 0
self.n_streams_label.config(text=f"N streams: {len(streams)}")
for stream in streams:
sample_rate = float(stream["info"]["nominal_srate"][0])
if sample_rate == 0:
task = launch_events_stream
else:
if sample_rate > max_sample_rate:
max_sample_rate = sample_rate
waveform_data = np.mean(stream["time_series"], axis=-1)
self.duration = len(waveform_data) / sample_rate
self.duration_label.config(
text=f"Duration: {self.duration:.1f} seconds"
)
self.draw_waveform(waveform_data, markers)
task = launch_sampled_stream
if stream["info"]["type"][0].lower() == "markers" and self.show_markers:
markers = zip(
stream["time_stamps"] - stream["time_stamps"][0],
stream["time_series"],
)
self.pool.apply_async(task, args=(stream,), callback=self.load_waveform)
if is_first_loop or self.loop_var.get():
self.start_progress()
else:
self.stop_loop()
def draw_waveform(self, waveform_data, markers=None):
self.canvas.delete("waveform")
width = self.canvas.winfo_width()
height = self.canvas.winfo_height()
x = np.linspace(0, width, len(waveform_data))
# Scale the waveform to fit the canvas
y = waveform_data / np.max(np.abs(waveform_data)) * height / 2 + height / 2
points = np.column_stack((x, y)).flatten().tolist()
self.canvas.create_line(points, fill="#91D8F0", tags="waveform")
if markers:
for marker in markers:
timestamp, label = marker
x_position = int(timestamp / self.duration * width)
self.canvas.create_line(
x_position,
0,
x_position,
height,
fill="#68293C",
width=1,
tags="waveform",
)
self.canvas.create_text(
x_position + 10,
height - 20,
anchor="n",
text=label,
fill="#4F315D",
font=("Arial", 8),
tags="waveform",
)
def start_progress(self):
self.progress = 0.0
self.after(self.progress_interval, self.update_progress)
def update_progress(self):
if self.progress < 100.0:
self.progress += self.progress_interval / (
self.duration * 10
) # Increase progress by interval
self.canvas.coords(
self.progress_bar,
0,
0,
self.canvas.winfo_width() * (self.progress / 100),
self.canvas.winfo_height(),
)
self.after(self.progress_interval, self.update_progress)
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument("-M", "--markers", action="store_true", help="Show markers")
args, _ = parser.parse_known_args()
root = tk.Tk()
root.title("EEG Emulator Interface")
photo = tk.PhotoImage(file="./assets/artboard.png")
root.wm_iconphoto(False, photo)
app = WaveformPlayer(root, show_markers=args.markers)
app.mainloop()