-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathheadset.py
More file actions
299 lines (251 loc) · 10.7 KB
/
headset.py
File metadata and controls
299 lines (251 loc) · 10.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
#### IMEC EEG HEADSET FOR PYTHON #####################################################################
# Authors: Tom De Smedt <tom@organisms.be>
# License: GNU General Public License v3, see LICENSE.txt
# Copyright (c) 2012 University of Antwerp, Belgium
# All rights reserved.
__author__ = "Tom De Smedt"
__credits__ = "Tom De Smedt"
__version__ = "1.0"
__copyright__ = "Copyright (c) 2012 University of Antwerp (BE)"
__license__ = "GPL"
import socket
import struct
import collections
######################################################################################################
#-----------------------------------------------------------------------------------------------------
RAW, ALPHA, VALENCE = 1, 2, 3
class BufferError(Exception):
pass
class Channel(collections.deque):
def __init__(self, iterable=[]):
""" A list of measurements from an electrode on the headset.
For alpha and valence channels, the list contains (value, long-term average) tuples.
"""
collections.deque.__init__(self, iterable)
self._total = 0 # Sum of all measurements.
self._length = 0 # Amount of measurements.
self._min = +2000 # All-time lowest measurement.
self._max = -2000 # All-time highest measurement.
def push(self, v):
collections.deque.append(self, v)
if isinstance(v, tuple):
v = v[0]
self._total += v
self._length += 1
# Progressively update min and max.
self._min = min(self._min, v)
self._max = min(self._max, v)
def pop(self):
collections.deque.popleft(self)
@property
def current(self):
""" Returns the most recent value, or None.
"""
try:
return self[-1][0]
except:
return None
@property
def min(self):
""" Yields the lowest of all measurements.
"""
return self._min
@property
def max(self):
""" Yields the highest of all measurements.
"""
return self._max
@property
def avg(self):
""" Yields the average of all measurements.
"""
return self._total / (self._length or 1)
@property
def lta(self):
""" Yields the long-term average (for alpha and valence).
"""
try:
return self[-1][1]
except:
return None
@property
def slope(self, d=50):
""" Yields a value between -1.0 and 1.0 indicating if the curve rises or drops.
"""
d = min(d, len(self))
if d > 0:
x = self.relative(self[-d][0])
y = self.relative(self[-1][0])
return (x-y) / -1
else:
return 0.0
@property
def angle(self):
return self.slope * 90
def relative(self, v):
""" Returns the given value as relative between 0.0 and 1.0.
"""
return ((v or 0) - self.min) / (self.max or 1)
#-----------------------------------------------------------------------------------------------------
class Headset:
def __init__(self, host="127.0.0.1", port=12002, history=250):
""" Interface to IMEC's EEG wireless headset.
The headset application will stream data over a UPD socket.
Headset.channels stores raw values from the headset's electrodes.
Headset.alpha stores alpha wave values (e.g., relaxation).
Headset.valence represents emotional state (left vs. right hemisphere).
Negative vs. positive valence is linked to negative vs. positive emotion
(or vice versa depending on the person).
Note: to measure relaxation (alpha), you can look at one channel instead of all of them.
Relaxation will be noticeable on all eight electrodes.
"""
# self.channel[0] is a list of int (maximum 250), newest-last.
# self.alpha[0] is a list of (int, int), second int is long-term average.
# self.valence is a list of (int, int), second int is long-term average.
self._socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self._socket.bind((host, port))
self._socket.setblocking(0)
self.channel = [Channel() for i in range(8)]
self.alpha = [Channel() for i in range(8)]
self.valence = Channel()
self.history = history
@property
def socket(self):
return self._socket
def update(self, buffer=1024):
""" Read data streamed from the headset application.
Raises a BufferError if the buffer is too small:
1 raw channel = 104 bytes, 1 alpha channel = 12 bytes, valence = 12 bytes,
8 * 104 + 8 * 12 + 12 = 940
"""
try:
data = self._socket.recvfrom(buffer)
data = data[0].replace("#bundle:", "", 1)
except Exception, e:
if "larger than the internal message buffer" in str(e):
raise BufferError, "need more than %s bytes" % buffer
# Non-blocking has nothing to read, ignore.
#print e
return
while len(data) > 0:
# The first 4 bytes is metadata:
# 1 byte is the type (RAW, ALPHA, VALENCE).
# 1 byte is the channel (0-7), corresponding to an electrode on the headset,
# 2 bytes is the length (number of readings for this channel),
type, channel, length = (
struct.unpack("B", data[0])[0],
struct.unpack("B", data[1])[0],
struct.unpack("<H", data[2:4])[0])
if type == RAW:
# 4 bytes for each reading (signed int), which must be divided by 100,000.
for i in range(len(data[4:4+4*length])/4):
r = struct.unpack("<i", data[4+4*i:4+4+4*i])[0]
r = float(r) / 100000
self.channel[channel].push(r)
data = data[4+4*length:]
if type == ALPHA:
# 4 bytes is the Alpha-wave value.
# 4 bytes is the Alpha-wave LTA (long-term average).
a1, a2 = (
struct.unpack("<i", data[4:8])[0],
struct.unpack("<i", data[8:12])[0])
a1 = float(a1) / 100000
a2 = float(a2) / 100000
self.alpha[channel].push((a1,a2))
data = data[12:]
if type == VALENCE:
# 4 bytes is the valence.
# 4 bytes is the valence LTA (long-term average).
v1, v2 = (
struct.unpack("<i", data[4:8])[0],
struct.unpack("<i", data[8:12])[0])
v1 = float(v1) / 100000
v2 = float(v2) / 100000
self.valence.push((v1,v2))
data = data[12:]
m = self.history
# Limit the list size of raw and alpha channels and valence.
for i in range(8):
for j in range(len(self.channel[i]) - m):
self.channel[i].pop()
for j in range(len(self.alpha[i]) - m):
self.alpha[i].pop()
for j in range(len(self.valence) - m):
self.valence.pop()
def close(self):
self._socket.close()
self._socket = None
def __delete__(self):
try:
self.close()
except:
pass
#-----------------------------------------------------------------------------------------------------
if __name__ == "__main__":
from nodebox.graphics import *
def wave(channel, x, y, width, m=1.0, key=lambda v: v):
""" Draws a channel (e.g., Headset.channel[3]).
For alpha and valence channels (which are tuple), key defines the item to use,
for example: wave(Headset.valence, 0, 0, 100, key=v: v[0])
"""
i = max(0, len(channel)-int(width))
dy0 = y
for j in range(len(channel)-i):
dy1 = key(channel[i+j])
line(x+j, y+dy0*m, x+j+1, y+dy1*m)
dy0 = dy1
def setup(canvas):
# IP connection for a setup with:
# 1) Windows computer running the headset application,
# 2) Mac computer running headset.py.
# Over cable: on Mac, close AirPort, System Preferences > Network, use a self-assigned IP as host.
# Over AirPort: on Mac, open Terminal and enter "ipconfig getifaddr en1" to obtain host IP.
# Shut down firewalls on both machines.
# Select IP adresses within the same range.
global headset
headset = Headset(host="127.0.0.1", port=12002)
def draw(canvas):
global headset
headset.update(buffer=1024) # Poll the headset.
background(1)
# Draw raw channel data (black curves):
stroke(0, 0.5)
for i in range(8):
wave(headset.channel[i], x=0, y=canvas.height/2, width=canvas.width)
# Draw alpha data (blue curves):
for i in range(8):
if i != 2: # Only process channel 3.
continue
stroke(0, 0, 1)
wave(headset.alpha[i], x=0, y=canvas.height/2, width=canvas.width, m=5.0, key=lambda a: a[0])
# The purple curve is the alpha long-term average (LTA):
stroke(0.5, 0, 1)
wave(headset.alpha[i], x=0, y=canvas.height/2, width=canvas.width, m=5.0, key=lambda a: a[1])
# The all-time average calculated in Python
# (horizontal blue line):
stroke(0, 0, 1, 0.5)
a = canvas.height / 2 + 5 * headset.alpha[i].avg
line(0, a, canvas.width, a)
# The arrow indicates alpha slope, i.e.,
# how steep it is rising or dropping:
push()
translate(240, 235)
rotate(headset.alpha[i].slope * 90)
arrow(0, 0, 15, fill=[0,0,1,0.25])
pop()
# The alpha min and max are calculated progressively:
stroke(0, 0, 1, 0.5)
y = canvas.height / 2 + 5 * headset.alpha[i].min
line(0, y, canvas.width, y)
y = canvas.height / 2 + 5 * headset.alpha[i].max
line(0, y, canvas.width, y)
# Draw valence data (red curve + orange LTA).
stroke(1, 0, 0)
wave(headset.valence, x=0, y=canvas.height/2, width=canvas.width, m=40.0, key=lambda v: v[0])
stroke(1, 0.5, 0, 0.5)
wave(headset.valence, x=0, y=canvas.height/2, width=canvas.width, m=40.0, key=lambda v: v[1])
def stop(canvas):
headset.close()
canvas.size = 250, 250
canvas.stop = stop
canvas.run(setup=setup, draw=draw)