forked from faturita/python-scientific
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signalfeatureclassification.py
425 lines (319 loc) · 12.2 KB
/
signalfeatureclassification.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
423
424
425
"""
==========================================
Signal Feature Classification
==========================================
# Run with ann virtual environment
# EPOC Emotiv file format https://www.researchgate.net/publication/332514530_EPOC_Emotiv_EEG_Basics
# OpemMP sometimes raises coredumps, try export KMP_DUPLICATE_LIB_OK=TRUE
"""
print(__doc__)
import numpy as np
from struct import *
import sys, select
import platform
import socket
import gevent
import time
import datetime
import os
from scipy.fftpack import fft
import math
from scipy.signal import firwin, remez, kaiser_atten, kaiser_beta
from scipy.signal import butter, filtfilt, buttord
from sklearn import svm
from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report, confusion_matrix
from scipy.signal import butter, lfilter
def isartifact(window, threshold=80):
# Window is EEG Matrix
awindow = np.asarray(window)
ameans = np.asarray( window ).mean(0)
signalaverage = ameans.tolist()
athresholds = np.asarray([threshold]*len(signalaverage))
#print awindow
#print ameans
#print athresholds
# FIXME
for t in range(0,len(window)):
asample = (ameans+athresholds)-awindow[t]
#print asample
for c in range(0,asample.shape[0]):
# while (ameans+athresholds)>(awindow)
if asample[c]<0:
return True
return False
import matplotlib.pyplot as plt
def butter_bandpass(lowcut, highcut, fs, order=5):
nyq = 0.5 * fs
low = lowcut / nyq
high = highcut / nyq
b, a = butter(order, [low, high], btype='band')
return b, a
def butter_bandpass_filter(data, lowcut, highcut, fs, order=5):
b, a = butter_bandpass(lowcut, highcut, fs, order=order)
y = lfilter(b, a, data)
return y
def psd(y):
# Number of samplepoints
N = 128
# sample spacing
T = 1.0 / 128.0
# From 0 to N, N*T, 2 points.
#x = np.linspace(0.0, 1.0, N)
#y = 1*np.sin(10.0 * 2.0*np.pi*x) + 9*np.sin(20.0 * 2.0*np.pi*x)
# Original Bandpass
fs = 128.0
fso2 = fs/2
#Nd,wn = buttord(wp=[9/fso2,11/fso2], ws=[8/fso2,12/fso2],
# gpass=3.0, gstop=40.0)
#b,a = butter(Nd,wn,'band')
#y = filtfilt(b,a,y)
y = butter_bandpass_filter(y, 8.0, 15.0, fs, order=6)
yf = fft(y)
#xf = np.linspace(0.0, 1.0/(2.0*T), N/2)
#import matplotlib.pyplot as plt
#plt.plot(xf, 2.0/N * np.abs(yf[0:N/2]))
#plt.axis((0,60,0,1))
#plt.grid()
#plt.show()
return np.sum(np.abs(yf[0:int(N/2)]))
import time
import datetime
import os
class Packet():
def init(self):
self.O1 = 0
self.O2 = 0
self.gyro_x = 0
self.gyro_y = 0
class OfflineHeadset:
def __init__(self, subject,label,paradigm='Alfa'):
# @TODO Need to parametrize this.
# @NOTE Search for datasets on current "Data" directory
self.basefilename = 'Data/%s/%s/e.%d.l.%d.dat'
self.paradigm = paradigm
self.readcounter = 0
self.running = True
self.label = label
self.subject = subject
self.fileindex = 0
self.f = None
def setup(self):
pass
def setupfile(self):
self.datasetfile = self.basefilename % (self.subject,self.paradigm,self.fileindex,self.label)
print (self.datasetfile)
if os.path.isfile(self.datasetfile):
if self.f:
self.f.close()
self.f = open(self.datasetfile,'r')
return True
else:
return False
def nextline(self):
line = None
if self.f:
line = self.f.readline()
if (not line):
self.fileindex = self.fileindex + 1
if self.setupfile():
return self.nextline()
else:
return None
else:
return line
def dequeue(self):
line = self.nextline()
if (line):
data = line.split('\r\n')[0].split(' ')
packet = Packet()
packet.O1 = [float(data[7]),0]
packet.O2 = [float(data[8]),0]
packet.gyro_x = 0
packet.gyro_y = 0
self.readcounter = self.readcounter + 1
return packet
else:
self.running = False
return None
def close(self):
if (self.f):
self.f.close()
# Segmentación de la serie de tiempo.
def process(headset):
ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d-%H-%M-%S')
log = open('data/biosensor-%s.dat' % st, 'w')
#plotter = Plotter(500,4000,5000)
print ("Starting BioProcessing Thread...")
readcounter=0
iterations=0
N = 128
window = []
fullsignal = []
awindow = None
afullsignal = None
features = []
while headset.running:
packet = headset.dequeue()
interations=iterations+1
if (packet != None):
datapoint = [packet.O1[0], packet.O2[0]]
#plotter.plotdata( [packet.gyro_x, packet.O2[0], packet.O1[0]])
log.write( str(packet.gyro_x) + "\t" + str(packet.gyro_y) + "\n" )
window.append( datapoint )
# Este punto establece cuando se hace el corte,
# como se genera el feature y
# como se hace el desplazamiento de la ventana.
# Este es el metodo de Welsh para EEG.
if len(window)>=N:
if not isartifact(window):
awindow = np.asarray( window )
fullsignal = fullsignal + window
afullsignal = np.asarray( fullsignal )
if (len(fullsignal) > 0):
awindow = awindow - afullsignal.mean(0)
o1 = psd(awindow[:,0])
o2 = psd(awindow[:,1])
print (o1, o2)
features.append( [o1, o2] )
# Slide window
window = window[int(N/2):N]
#window = window[1:N]
readcounter=readcounter+1
if (readcounter==0 and iterations>50):
headset.running = False
gevent.sleep(0.001)
log.close()
return features
def reshapefeature(feature, featuresize):
feature=feature[0:feature.shape[0]-(feature.shape[0]%featuresize)]
feature = np.reshape( feature, (int(feature.shape[0]/int(featuresize/feature.shape[1])),featuresize) )
return feature
def classify(afeatures1, afeatures2, featuresize):
print ('Feature 1 Size %d,%d' % (afeatures1.shape))
print ('Feature 2 Size %d,%d' % (afeatures2.shape))
afeatures1 = reshapefeature(afeatures1, featuresize)
afeatures2 = reshapefeature(afeatures2, featuresize)
featuredata = np.concatenate ((afeatures1,afeatures2))
featurelabels = np.concatenate( (np.zeros(afeatures1.shape[0]),(np.zeros(afeatures2.shape[0])+1) ) )
boundary = int(featuredata.shape[0]/2.0)
print ('Boundary %d:' % boundary)
# Reshape and shuffle the features
reorder = np.random.permutation(featuredata.shape[0])
trainingdata = featuredata[reorder[0:boundary]]
traininglabels = featurelabels[reorder[0:boundary]]
testdata = featuredata[reorder[boundary+1:featuredata.shape[0]]]
testlabels = featurelabels[reorder[boundary+1:featuredata.shape[0]]]
print ('Training Dataset Size %d,%d' % (trainingdata.shape))
print ('Test Dataset Size %d,%d' % (testdata.shape))
clf = svm.SVC(kernel='linear', C = 1.0)
clf.fit(trainingdata,traininglabels)
predlabels = clf.predict(testdata)
C = confusion_matrix(testlabels, predlabels)
acc = (float(C[0,0])+float(C[1,1])) / ( testdata.shape[0])
print ('SVM Feature Dim: %d Accuracy: %f' % (featuresize,acc))
print(C)
target_names = ['Open', 'Closed']
report = classification_report(testlabels, predlabels, target_names=target_names)
print(report)
from sklearn.linear_model import LogisticRegression
# all parameters not specified are set to their defaults
logisticRegr = LogisticRegression()
logisticRegr.fit(trainingdata,traininglabels)
# Returns a NumPy Array
# Predict for One Observation (image)
predlabels = logisticRegr.predict(testdata)
C = confusion_matrix(testlabels, predlabels)
acc = (float(C[0,0])+float(C[1,1])) / ( testdata.shape[0])
print ('LogReg Feature Dim: %d Accuracy: %f' % (featuresize,acc))
print(C)
target_names = ['Open', 'Closed']
report = classification_report(testlabels, predlabels, target_names=target_names)
print(report)
from keras.models import Sequential
from keras.layers import Dense
model = Sequential([
Dense(64, activation='tanh', input_shape=(trainingdata.shape[1],)),
Dense(32, activation='tanh'),
Dense(1, activation='sigmoid'),
])
model.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['accuracy'])
hist = model.fit(trainingdata, traininglabels,
batch_size=10, epochs=1000*trainingdata.shape[1],verbose=0,
validation_split=0.4)
predlabels = model.predict(testdata)
#print(predlabels)
predlabels = predlabels.round()
#print(predlabels)
C = confusion_matrix(testlabels, predlabels)
acc = (float(C[0,0])+float(C[1,1])) / ( testdata.shape[0])
print ('Keras Feature Dim: %d Accuracy: %f' % (featuresize,acc))
print(C)
print(model.evaluate(testdata,testlabels))
print ('Keras Model Accuracy: %f' % (model.evaluate(testdata,testlabels)[1]))
target_names = ['Open', 'Closed']
report = classification_report(testlabels, predlabels, target_names=target_names)
print(report)
plt.plot(hist.history['loss'])
plt.plot(hist.history['val_loss'])
plt.title('Model loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train', 'Val'], loc='upper right')
plt.show()
#plt.plot(hist.history['acc'])
#plt.plot(hist.history['val_acc'])
#plt.title('Model accuracy')
#plt.ylabel('Accuracy')
#plt.xlabel('Epoch')
#plt.legend(['Train', 'Val'], loc='lower right')
#plt.show()
# Esto es lo que hace el método principal.
# Primero toma muestras de señales de tiempo de una personas. Estas señales corresponden a dos experiemntos,
# donde la persona durante un tiempo estaba con los ojos cerrados, y luego con los ojos abiertos.
# Eso dispara un cambio en las señales occipitales, en O1 y O2 que son dos canales. Ese cambio se manifiesta
# como un aumento de la potencia de 10 Hz cuando la persona tiene los ojos cerrados.
def featureextractor():
# Get features from label 1.
headset = OfflineHeadset('Subject',1,'Alfa')
features1 = process(headset)
headset.close()
# Get features from label 2
headset = OfflineHeadset('Subject',2,'Alfa')
features2 = process(headset)
headset.close()
# En este punto se tienen una secuencia de features bidimensionales. El PSD de O1 y O2 durante una ventana de tiempo.
afeatures1 = np.asarray(features1)
afeatures2 = np.asarray(features2)
print (afeatures1.mean(0))
print (afeatures2.mean(0))
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.scatter(afeatures1[:,0], afeatures1[:,1], s=10, c='b', marker="x", label='Open')
ax1.scatter(afeatures2[:,0], afeatures2[:,1], s=10, c='r', marker="o", label='Closed')
plt.xlabel('PSD O2')
plt.ylabel('PSD O1')
plt.legend(loc='upper left')
plt.show()
# Group time features in tuples, 4-tuples and 8-tuples and classify them
classify(afeatures1, afeatures2,2)
classify(afeatures1, afeatures2,4)
classify(afeatures1, afeatures2,8)
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.scatter(afeatures1[:,0], afeatures1[:,1], s=10, c='b', marker="x", label='Open')
ax1.scatter(afeatures2[:,0], afeatures2[:,1], s=10, c='r', marker="o", label='Closed')
plt.xlabel('PSD O2')
plt.ylabel('PSD O1')
plt.legend(loc='upper left')
plt.show()
# Este If de python, sirve cuando un programa funciona como una libreria, por lo que no tiene código que se ejecute
# que no esté en el bloque global (sin indentación). En esos casos este if sirve para indicar que se tiene
# que ejecutar cuando a este .py se lo ejecuta de manera directa.
if __name__ == "__main__":
featureextractor()