forked from alexandrebarachant/muse-lsl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
lsl-viewer-V2.py
254 lines (208 loc) · 8.15 KB
/
lsl-viewer-V2.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vispy: gallery 2
# Copyright (c) 2015, Vispy Development Team.
# Distributed under the (new) BSD License. See LICENSE.txt for more info.
"""
Multiple real-time digital signals with GLSL-based clipping.
"""
from vispy import gloo, app, visuals
import numpy as np
import math
from seaborn import color_palette
from pylsl import StreamInlet, resolve_byprop
from scipy.signal import lfilter, lfilter_zi
from mne.filter import create_filter
print("looking for an EEG stream...")
streams = resolve_byprop('type', 'EEG', timeout=2)
if len(streams) == 0:
raise(RuntimeError("Cant find EEG stream"))
print("Start aquiring data")
inlet = StreamInlet(streams[0], max_chunklen=12)
info = inlet.info()
description = info.desc()
window = 10
sfreq = info.nominal_srate()
n_samples = int(sfreq * window)
n_chan = info.channel_count()
ch = description.child('channels').first_child()
ch_names = [ch.child_value('label')]
for i in range(n_chan):
ch = ch.next_sibling()
ch_names.append(ch.child_value('label'))
# Number of cols and rows in the table.
nrows = n_chan
ncols = 1
# Number of signals.
m = nrows*ncols
# Number of samples per signal.
n = n_samples
# Various signal amplitudes.
amplitudes = np.zeros((m, n)).astype(np.float32)
gamma = np.ones((m, n)).astype(np.float32)
# Generate the signals as a (m, n) array.
y = amplitudes
color = color_palette("RdBu_r", nrows)
color = np.repeat(color, n, axis=0).astype(np.float32)
# Signal 2D index of each vertex (row and col) and x-index (sample index
# within each signal).
index = np.c_[np.repeat(np.repeat(np.arange(ncols), nrows), n),
np.repeat(np.tile(np.arange(nrows), ncols), n),
np.tile(np.arange(n), m)].astype(np.float32)
VERT_SHADER = """
#version 120
// y coordinate of the position.
attribute float a_position;
// row, col, and time index.
attribute vec3 a_index;
varying vec3 v_index;
// 2D scaling factor (zooming).
uniform vec2 u_scale;
// Size of the table.
uniform vec2 u_size;
// Number of samples per signal.
uniform float u_n;
// Color.
attribute vec3 a_color;
varying vec4 v_color;
// Varying variables used for clipping in the fragment shader.
varying vec2 v_position;
varying vec4 v_ab;
void main() {
float nrows = u_size.x;
float ncols = u_size.y;
// Compute the x coordinate from the time index.
float x = -1 + 2*a_index.z / (u_n-1);
vec2 position = vec2(x - (1 - 1 / u_scale.x), a_position);
// Find the affine transformation for the subplots.
vec2 a = vec2(1./ncols, 1./nrows)*.9;
vec2 b = vec2(-1 + 2*(a_index.x+.5) / ncols,
-1 + 2*(a_index.y+.5) / nrows);
// Apply the static subplot transformation + scaling.
gl_Position = vec4(a*u_scale*position+b, 0.0, 1.0);
v_color = vec4(a_color, 1.);
v_index = a_index;
// For clipping test in the fragment shader.
v_position = gl_Position.xy;
v_ab = vec4(a, b);
}
"""
FRAG_SHADER = """
#version 120
varying vec4 v_color;
varying vec3 v_index;
varying vec2 v_position;
varying vec4 v_ab;
void main() {
gl_FragColor = v_color;
// Discard the fragments between the signals (emulate glMultiDrawArrays).
if ((fract(v_index.x) > 0.) || (fract(v_index.y) > 0.))
discard;
// Clipping test.
vec2 test = abs((v_position.xy-v_ab.zw)/v_ab.xy);
if ((test.x > 1))
discard;
}
"""
class Canvas(app.Canvas):
def __init__(self, scale=500, filt=True):
app.Canvas.__init__(self, title='EEG - Use your wheel to zoom!',
keys='interactive')
self.program = gloo.Program(VERT_SHADER, FRAG_SHADER)
self.program['a_position'] = y.reshape(-1, 1)
self.program['a_color'] = color
self.program['a_index'] = index
self.program['u_scale'] = (1., 1.)
self.program['u_size'] = (nrows, ncols)
self.program['u_n'] = n
# text
self.font_size = 48.
self.names = []
self.quality = []
for ii in range(n_chan):
text = visuals.TextVisual(ch_names[ii], bold=True, color='white')
self.names.append(text)
text = visuals.TextVisual('', bold=True, color='white')
self.quality.append(text)
self.quality_colors = color_palette("RdYlGn", 11)[::-1]
self.scale = scale
self.n_samples = n_samples
self.filt = filt
self.af = [1.0]
self.data_f = np.zeros((n_samples, n_chan))
self.data = np.zeros((n_samples, n_chan))
self.bf = create_filter(self.data_f.T, sfreq, 3, 40.,
method='fir', fir_design='firwin')
zi = lfilter_zi(self.bf, self.af)
self.filt_state = np.tile(zi, (n_chan, 1)).transpose()
self._timer = app.Timer('auto', connect=self.on_timer, start=True)
gloo.set_viewport(0, 0, *self.physical_size)
gloo.set_state(clear_color='black', blend=True,
blend_func=('src_alpha', 'one_minus_src_alpha'))
self.show()
def on_key_press(self, event):
# toggle filtering
if event.key.name == 'D':
self.filt = not self.filt
# increase time scale
if event.key.name in ['+', '-']:
if event.key.name == '+':
dx = -0.05
else:
dx = 0.05
scale_x, scale_y = self.program['u_scale']
scale_x_new, scale_y_new = (scale_x * math.exp(1.0*dx),
scale_y * math.exp(0.0*dx))
self.program['u_scale'] = (max(1, scale_x_new), max(1, scale_y_new))
self.update()
def on_mouse_wheel(self, event):
dx = np.sign(event.delta[1]) * .05
scale_x, scale_y = self.program['u_scale']
scale_x_new, scale_y_new = (scale_x * math.exp(0.0*dx),
scale_y * math.exp(2.0*dx))
self.program['u_scale'] = (max(1, scale_x_new), max(0.01, scale_y_new))
self.update()
def on_timer(self, event):
"""Add some data at the end of each signal (real-time signals)."""
samples, timestamps = inlet.pull_chunk(timeout=0.0,
max_samples=100)
if timestamps:
samples = np.array(samples)[:, ::-1]
self.data = np.vstack([self.data, samples])
self.data = self.data[-self.n_samples:]
filt_samples, self.filt_state = lfilter(self.bf, self.af, samples,
axis=0, zi=self.filt_state)
self.data_f = np.vstack([self.data_f, filt_samples])
self.data_f = self.data_f[-self.n_samples:]
if self.filt:
plot_data = self.data_f / self.scale
elif not self.filt:
plot_data = (self.data - self.data.mean(axis=0)) / self.scale
sd = np.std(plot_data[-int(sfreq):], axis=0)[::-1] * self.scale
co = np.int32(np.tanh((sd - 30) / 15)*5 + 5)
for ii in range(n_chan):
self.quality[ii].text = '%.2f' % (sd[ii])
self.quality[ii].color = self.quality_colors[co[ii]]
self.quality[ii].font_size = 12 + co[ii]
self.names[ii].font_size = 12 + co[ii]
self.names[ii].color = self.quality_colors[co[ii]]
self.program['a_position'].set_data(plot_data.T.ravel().astype(np.float32))
self.update()
def on_resize(self, event):
# Set canvas viewport and reconfigure visual transforms to match.
vp = (0, 0, self.physical_size[0], self.physical_size[1])
self.context.set_viewport(*vp)
for ii, t in enumerate(self.names):
t.transforms.configure(canvas=self, viewport=vp)
t.pos = (self.size[0] * 0.025, ((ii + 0.5)/n_chan) * self.size[1])
for ii, t in enumerate(self.quality):
t.transforms.configure(canvas=self, viewport=vp)
t.pos = (self.size[0] * 0.975, ((ii + 0.5)/n_chan) * self.size[1])
def on_draw(self, event):
gloo.clear()
gloo.set_viewport(0, 0, *self.physical_size)
self.program.draw('line_strip')
[t.draw() for t in self.names + self.quality]
if __name__ == '__main__':
c = Canvas()
app.run()