-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.py
422 lines (308 loc) · 12.2 KB
/
utils.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
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
# -*- coding: utf-8 -*-
"""
BCI Workshop Auxiliary Tools
Created on Fri May 08 15:34:59 2015
@author: Cassani
"""
import os
import sys
from tempfile import gettempdir
from subprocess import call
import matplotlib.pyplot as plt
import numpy as np
from sklearn import svm
from scipy.signal import butter, lfilter, lfilter_zi
NOTCH_B, NOTCH_A = butter(4, np.array([55, 65])/(256/2), btype='bandstop')
def plot_multichannel(data, params=None):
"""Create a plot to present multichannel data.
Args:
data (numpy.ndarray): Multichannel Data [n_samples, n_channels]
params (dict): information about the data acquisition device
TODO Receive labels as arguments
"""
fig, ax = plt.subplots()
n_samples = data.shape[0]
n_channels = data.shape[1]
if params is not None:
fs = params['sampling frequency']
names = params['names of channels']
else:
fs = 1
names = [''] * n_channels
time_vec = np.arange(n_samples) / float(fs)
data = np.fliplr(data)
offset = 0
for i_channel in range(n_channels):
data_ac = data[:, i_channel] - np.mean(data[:, i_channel])
offset = offset + 2 * np.max(np.abs(data_ac))
ax.plot(time_vec, data_ac + offset, label=names[i_channel])
ax.set_xlabel('Time [s]')
ax.set_ylabel('Amplitude')
plt.legend()
plt.draw()
def epoch(data, samples_epoch, samples_overlap=0):
"""Extract epochs from a time series.
Given a 2D array of the shape [n_samples, n_channels]
Creates a 3D array of the shape [wlength_samples, n_channels, n_epochs]
Args:
data (numpy.ndarray or list of lists): data [n_samples, n_channels]
samples_epoch (int): window length in samples
samples_overlap (int): Overlap between windows in samples
Returns:
(numpy.ndarray): epoched data of shape
"""
if isinstance(data, list):
data = np.array(data)
n_samples, n_channels = data.shape
samples_shift = samples_epoch - samples_overlap
n_epochs = int(np.floor((n_samples - samples_epoch) / float(samples_shift)) + 1)
# Markers indicate where the epoch starts, and the epoch contains samples_epoch rows
markers = np.asarray(range(0, n_epochs + 1)) * samples_shift
markers = markers.astype(int)
# Divide data in epochs
epochs = np.zeros((samples_epoch, n_channels, n_epochs))
for i in range(0, n_epochs):
epochs[:, :, i] = data[markers[i]:markers[i] + samples_epoch, :]
return epochs
def compute_PSD(eegdata, fs):
"""Extract the features from the EEG.
Args:
eegdata (numpy.ndarray): array of dimension [number of samples,
number of channels]
fs (float): sampling frequency of eegdata
Returns:
(numpy.ndarray): feature matrix of shape [number of feature points,
number of different features]
"""
# 1. Compute the PSD
winSampleLength, nbCh = eegdata.shape
# Apply Hamming window
w = np.hamming(winSampleLength)
dataWinCentered = eegdata - np.mean(eegdata, axis=0) # Remove offset
dataWinCenteredHam = (dataWinCentered.T*w).T
NFFT = nextpow2(winSampleLength)
Y = np.fft.fft(dataWinCenteredHam, n=NFFT, axis=0)/winSampleLength
PSD = 2*np.abs(Y[0:int(NFFT/2), :])
f = fs/2*np.linspace(0, 1, int(NFFT/2))
# SPECTRAL FEATURES
# Average of band powers
# Delta <4
ind_delta, = np.where(f < 4)
meanDelta = np.mean(PSD[ind_delta, :], axis=0)
# Theta 4-8
ind_theta, = np.where((f >= 4) & (f <= 8))
meanTheta = np.mean(PSD[ind_theta, :], axis=0)
# Alpha 8-12
ind_alpha, = np.where((f >= 8) & (f <= 12))
meanAlpha = np.mean(PSD[ind_alpha, :], axis=0)
# Beta 12-30
ind_beta, = np.where((f >= 12) & (f < 30))
meanBeta = np.mean(PSD[ind_beta, :], axis=0)
feature_vector = np.concatenate((meanDelta, meanTheta, meanAlpha,
meanBeta), axis=0)
feature_vector = np.log10(feature_vector)
return feature_vector
def nextpow2(i):
"""
Find the next power of 2 for number i
"""
n = 1
while n < i:
n *= 2
return n
def compute_feature_matrix(epochs, fs):
"""
Call compute_feature_vector for each EEG epoch
"""
n_epochs = epochs.shape[2]
for i_epoch in range(n_epochs):
if i_epoch == 0:
feat = compute_feature_vector(epochs[:, :, i_epoch], fs).T
feature_matrix = np.zeros((n_epochs, feat.shape[0])) # Initialize feature_matrix
feature_matrix[i_epoch, :] = compute_feature_vector(
epochs[:, :, i_epoch], fs).T
return feature_matrix
def train_classifier(feature_matrix_0, feature_matrix_1, algorithm='SVM'):
"""Train a binary classifier.
Train a binary classifier. First perform Z-score normalization, then
fit
Args:
feature_matrix_0 (numpy.ndarray): array of shape (n_samples,
n_features) with examples for Class 0
feature_matrix_0 (numpy.ndarray): array of shape (n_samples,
n_features) with examples for Class 1
alg (str): Type of classifer to use. Currently only SVM is
supported.
Returns:
(sklearn object): trained classifier (scikit object)
(numpy.ndarray): normalization mean
(numpy.ndarray): normalization standard deviation
"""
# Create vector Y (class labels)
class0 = np.zeros((feature_matrix_0.shape[0], 1))
class1 = np.ones((feature_matrix_1.shape[0], 1))
# Concatenate feature matrices and their respective labels
y = np.concatenate((class0, class1), axis=0)
features_all = np.concatenate((feature_matrix_0, feature_matrix_1),
axis=0)
# Normalize features columnwise
mu_ft = np.mean(features_all, axis=0)
std_ft = np.std(features_all, axis=0)
X = (features_all - mu_ft) / std_ft
# Train SVM using default parameters
clf = svm.SVC()
clf.fit(X, y)
score = clf.score(X, y.ravel())
# Visualize decision boundary
# plot_classifier_training(clf, X, y, features_to_plot=[0, 1])
return clf, mu_ft, std_ft, score
def test_classifier(clf, feature_vector, mu_ft, std_ft):
"""Test the classifier on new data points.
Args:
clf (sklearn object): trained classifier
feature_vector (numpy.ndarray): array of shape (n_samples,
n_features)
mu_ft (numpy.ndarray): normalization mean
std_ft (numpy.ndarray): normalization standard deviation
Returns:
(numpy.ndarray): decision of the classifier on the data points
"""
# Normalize feature_vector
x = (feature_vector - mu_ft) / std_ft
y_hat = clf.predict(x)
return y_hat
def beep(waveform=(79, 45, 32, 50, 99, 113, 126, 127)):
"""Play a beep sound.
Cross-platform sound playing with standard library only, no sound
file required.
From https://gist.github.com/juancarlospaco/c295f6965ed056dd08da
"""
wavefile = os.path.join(gettempdir(), "beep.wav")
if not os.path.isfile(wavefile) or not os.access(wavefile, os.R_OK):
with open(wavefile, "w+") as wave_file:
for sample in range(0, 300, 1):
for wav in range(0, 8, 1):
wave_file.write(chr(waveform[wav]))
if sys.platform.startswith("linux"):
return call("chrt -i 0 aplay '{fyle}'".format(fyle=wavefile),
shell=1)
if sys.platform.startswith("darwin"):
return call("afplay '{fyle}'".format(fyle=wavefile), shell=True)
if sys.platform.startswith("win"): # FIXME: This is Ugly.
return call("start /low /min '{fyle}'".format(fyle=wavefile),
shell=1)
def get_feature_names(ch_names):
"""Generate the name of the features.
Args:
ch_names (list): electrode names
Returns:
(list): feature names
"""
bands = ['delta', 'theta', 'alpha', 'beta']
feat_names = []
for band in bands:
for ch in range(len(ch_names)):
feat_names.append(band + '-' + ch_names[ch])
return feat_names
def update_buffer(data_buffer, new_data, notch=False, filter_state=None):
"""
Concatenates "new_data" into "data_buffer", and returns an array with
the same size as "data_buffer"
"""
if new_data.ndim == 1:
new_data = new_data.reshape(-1, data_buffer.shape[1])
if notch:
if filter_state is None:
filter_state = np.tile(lfilter_zi(NOTCH_B, NOTCH_A),
(data_buffer.shape[1], 1)).T
new_data, filter_state = lfilter(NOTCH_B, NOTCH_A, new_data, axis=0,
zi=filter_state)
new_buffer = np.concatenate((data_buffer, new_data), axis=0)
new_buffer = new_buffer[new_data.shape[0]:, :]
return new_buffer, filter_state
def get_last_data(data_buffer, newest_samples):
"""
Obtains from "buffer_array" the "newest samples" (N rows from the
bottom of the buffer)
"""
new_buffer = data_buffer[(data_buffer.shape[0] - newest_samples):, :]
return new_buffer
class DataPlotter():
"""
Class for creating and updating a line plot.
"""
def __init__(self, nbPoints, chNames, fs=None, title=None):
"""Initialize the figure."""
self.nbPoints = nbPoints
self.chNames = chNames
self.nbCh = len(self.chNames)
self.fs = 1 if fs is None else fs
self.figTitle = '' if title is None else title
data = np.empty((self.nbPoints, 1))*np.nan
self.t = np.arange(data.shape[0])/float(self.fs)
# Create offset parameters for plotting multiple signals
self.yAxisRange = 100
self.chRange = self.yAxisRange/float(self.nbCh)
self.offsets = np.round((np.arange(self.nbCh)+0.5)*(self.chRange))
# Create the figure and axis
plt.ion()
self.fig, self.ax = plt.subplots()
self.ax.set_yticks(self.offsets)
self.ax.set_yticklabels(self.chNames)
# Initialize the figure
self.ax.set_title(self.figTitle)
self.chLinesDict = {}
for i, chName in enumerate(self.chNames):
self.chLinesDict[chName], = self.ax.plot(
self.t, data+self.offsets[i], label=chName)
self.ax.set_xlabel('Time')
self.ax.set_ylim([0, self.yAxisRange])
self.ax.set_xlim([np.min(self.t), np.max(self.t)])
plt.show()
def update_plot(self, data):
""" Update the plot """
data = data - np.mean(data, axis=0)
std_data = np.std(data, axis=0)
std_data[np.where(std_data == 0)] = 1
data = data/std_data*self.chRange/5.0
for i, chName in enumerate(self.chNames):
self.chLinesDict[chName].set_ydata(data[:, i] + self.offsets[i])
self.fig.canvas.draw()
def clear(self):
""" Clear the figure """
blankData = np.empty((self.nbPoints, 1))*np.nan
for i, chName in enumerate(self.chNames):
self.chLinesDict[chName].set_ydata(blankData)
self.fig.canvas.draw()
def close(self):
""" Close the figure """
plt.close(self.fig)
def plot_classifier_training(clf, X, y, features_to_plot=[0, 1]):
"""Visualize the decision boundary of a classifier.
Args:
clf (sklearn object): trained classifier
X (numpy.ndarray): data to visualize the decision boundary for
y (numpy.ndarray): labels for X
Keyword Args:
features_to_plot (list): indices of the two features to use for
plotting
Inspired from: http://scikit-learn.org/stable/auto_examples/tree/plot_iris.html
"""
plot_colors = "bry"
plot_step = 0.02
n_classes = len(np.unique(y))
x_min = np.min(X[:, 1])-1
x_max = np.max(X[:, 1])+1
y_min = np.min(X[:, 0])-1
y_max = np.max(X[:, 0])+1
xx, yy = np.meshgrid(np.arange(x_min, x_max, plot_step),
np.arange(y_min, y_max, plot_step))
Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
fig, ax = plt.subplots()
ax.contourf(xx, yy, Z, cmap=plt.cm.Paired, alpha=0.5)
# Plot the training points
for i, color in zip(range(n_classes), plot_colors):
idx = np.where(y == i)
ax.scatter(X[idx, 0], X[idx, 1], c=color, cmap=plt.cm.Paired)
plt.axis('tight')