forked from neurotech-berkeley/neurotech-course
-
Notifications
You must be signed in to change notification settings - Fork 3
/
neurofeedback.py
executable file
·204 lines (151 loc) · 5.64 KB
/
neurofeedback.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
#!/usr/bin/env python
## code by Alexandre Barachant
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import butter, filtfilt
from time import time, sleep
from pylsl import StreamInlet, resolve_byprop
import seaborn as sns
from threading import Thread
from scipy import signal
sns.set(style="whitegrid")
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-w", "--window",
dest="window", type='float', default=0.5,
help="window lenght to display in seconds.")
parser.add_option("-s", "--scale",
dest="scale", type='float', default=100,
help="scale in uV")
parser.add_option("-r", "--refresh",
dest="refresh", type='float', default=0.2,
help="refresh rate in seconds.")
parser.add_option("-f", "--figure",
dest="figure", type='string', default="15x6",
help="window size.")
filt = True
subsample = 2
buf = 12
(options, args) = parser.parse_args()
window = options.window
scale = options.scale
figsize = np.int16(options.figure.split('x'))
refresh = options.refresh
decrease_fs = [4, 8]
increase_fs = [12, 20]
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")
class LSLViewer():
def __init__(self, stream, fig, axes, window, scale, dejitter=True):
"""Init"""
self.stream = stream
self.window = window
self.scale = scale
self.dejitter = dejitter
self.inlet = StreamInlet(stream, max_chunklen=buf)
self.filt = True
info = self.inlet.info()
description = info.desc()
self.sfreq = info.nominal_srate()
self.n_samples = int(self.sfreq * self.window)
self.n_chan = info.channel_count()
ch = description.child('channels').first_child()
ch_names = [ch.child_value('label')]
for i in range(self.n_chan):
ch = ch.next_sibling()
ch_names.append(ch.child_value('label'))
self.ch_names = ch_names
fig.canvas.mpl_connect('key_press_event', self.OnKeypress)
fig.canvas.mpl_connect('button_press_event', self.onclick)
self.fig = fig
self.axes = axes
sns.despine(left=True)
self.data = np.zeros((self.n_samples, self.n_chan))
self.times = np.arange(-self.window, 0, 1./self.sfreq)
impedances = np.std(self.data, axis=0)
lines = []
self.rects = axes.bar(0, 1)
# self.text = axes.
axes.xaxis.grid(False)
axes.set_xticks([])
self.value = None
self.display_every = int(refresh / (12/self.sfreq))
self.bf1, self.af1 = butter(4, np.array(decrease_fs)/(self.sfreq/2.),
'bandpass')
self.bf2, self.af2 = butter(4, np.array(increase_fs)/(self.sfreq/2.),
'bandpass')
self.low = 10000
self.high = 0
def compute_value(self):
data_f1 = filtfilt(self.bf1, self.af1, self.data, axis=0)
data_f2 = filtfilt(self.bf2, self.af2, self.data, axis=0)
v1 = np.sqrt(np.sum(np.square(data_f1)))
v2 = np.sqrt(np.sum(np.square(data_f2)))
return v2 / v1
def update_plot(self):
value = self.compute_value()
if self.value is None:
self.value = value
self.value = 0.8 * self.value + 0.2 * value
self.low = min(self.low, self.value)
self.high = max(self.high, self.value)
rect = self.rects.get_children()[0]
rect.set_height(self.value)
self.axes.set_ylim([self.low, self.high])
self.fig.canvas.draw()
plt.pause(0.01)
def update_data_and_plot(self):
k = 0
while self.started:
samples, timestamps = self.inlet.pull_chunk(timeout=1.0,
max_samples=buf)
if timestamps:
self.data = np.vstack([self.data, samples])
if self.dejitter:
timestamps = np.float64(np.arange(len(timestamps)))
timestamps /= self.sfreq
timestamps += self.times[-1] + 1./self.sfreq
self.times = np.concatenate([self.times, timestamps])
self.n_samples = int(self.sfreq * self.window)
self.data = self.data[-self.n_samples:]
self.times = self.times[-self.n_samples:]
k += 1
if k >= self.display_every:
self.update_plot()
k = 0
else:
sleep(0.1)
def onclick(self, event):
print((event.button, event.x, event.y, event.xdata, event.ydata))
def OnKeypress(self, event):
if event.key == 'r':
self.low = 10000
self.high = 0
elif event.key == '+':
self.window += 1
elif event.key == '-':
if self.window > 1:
self.window -= 1
elif event.key == 'd':
self.filt = not(self.filt)
def start(self):
self.started = True
self.thread = Thread(target=self.update_data_and_plot)
self.thread.daemon = True
self.thread.start()
def stop(self):
self.started = False
fig, axes = plt.subplots(1, 1, figsize=figsize, sharex=True)
lslv = LSLViewer(streams[0], fig, axes, window, scale)
help_str = """
reset scale: r
increase time scale : -
decrease time scale : +
"""
print(help_str)
lslv.start()
plt.show()
lslv.stop()