forked from DidsheMa/memory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
p3_classify.py
executable file
·197 lines (163 loc) · 7.23 KB
/
p3_classify.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
import sys
from StimulusPresentation import StimulusPresentation, PresentationMode, PresentationStatus
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QCoreApplication, QEventLoop, QDateTime
from lslrecorder import Lslrecorder
import numpy as np
import time
from auxiliary import *
from termcolor import colored
import mne.filter as filter
class P3_classify():
def __init__(self):
self.prefix = "data/"
self.images_prefix = 'speller_images/'
self.samples = None
self.hasPCA = False
self.isi = 120 # speller stim interval in ms
self.stimnum = 12
self.trials = 3
self.do_channel_mask = False
self.mode = None
self.loadData()
if self.mode == PresentationMode.SPELLER_MODE:
self.images = int((self.stimnum / 2) ** 2)
else:
self.images = self.stimnum
self.lslrec = Lslrecorder()
if(self.lslrec.findStream() == -1):
return
if self.do_channel_mask is True:
self.channels = 9
else:
self.channels = self.lslrec.channelCount
self.lslrec.startRec()
app = QApplication(sys.argv)
self.presentation = StimulusPresentation(quit_with_escape=True)
# init speller-mode with image path and inter-stimulus-interval,
self.presentation.initialize(path=self.images_prefix, inter_stimulus_interval=120,
n_images=self.images, mode=self.mode)
# show window
self.presentation.show()
# detect targets
self.detectTargets()
sys.exit(app.exec_())
def detectTargets(self):
for i in range(self.trials):
s = 1
target = -1
if self.mode == PresentationMode.SPELLER_MODE:
TP = np.zeros((int(self.stimnum/2),int(self.stimnum/2)))
else:
TP = np.zeros((self.stimnum))
print(colored("TP has length: %s " % len(TP), 'green'))
#Show only blank matrix to allow user to choose symbol
self.presentation.show_target(-2, '', show_matrix=True)
# % Subtrial loop. Executed until a target is detected or max. number
# % of subtrials is reached.
while target == -1 :
flash =self.presentation.execute_sub_trial()
on = np.array([int(i[1]) for i in flash])
fseq = np.array([int(i[0]) for i in flash])
# wait for all data to be collected
time.sleep(1)
(target,TP) = self.processSubtrial(on ,fseq, s, TP)
print(target)
print(TP)
s=s+1
if self.mode == PresentationMode.SPELLER_MODE:
# % Expand row/col to linear index [0...(stimnum/2)^2-1]
target = (target[1])*(self.stimnum/2)+target[0]
print(colored("The current target has index %s." % target, 'magenta'))
self.presentation.show_target(target, 'Detected Target')
def processSubtrial(self, on, fseq, s, TP):
self.dataset = np.zeros((self.stimnum,self.samples,self.channels))
for (stim, onset) in zip(list(range(self.stimnum)),on):
ind = (np.abs(self.lslrec.timeStamps - onset)).argmin()
# print("closest timestamp: ",int(self.lslrec.timeStamps[ind]))
thresh = (1000/self.lslrec.srate)*2
diff = abs(int(self.lslrec.timeStamps[ind]) - onset)
#print(diff, thresh)
if diff > thresh:
print('difference between timestamps is %d ms. Something is wrong' % diff)
return
self.dataset[stim] = self.getDataAtIndex(ind)
#print(self.dataset.shape)
if (not self.hasPCA):
self.dataset = downsample(self.dataset, 10, avg="pick")
#print("downsampled dataset:")
#print(self.dataset.shape)
self.dataset = self.dataset.transpose((0, 2, 1))
self.dataset = self.dataset.reshape(self.stimnum, -1)
# apply pca
if self.hasPCA:
self.dataset = np.matmul(self.dataset,self.pcamat)
# apply classification
ys = np.matmul(self.dataset,self.fda_w) + self.fda_b
return self.subtrialDynamic(ys, fseq, TP, s)
def subtrialDynamic(self, ys, flashseq, TPprev, subtrcount, maxRep = 10):
# % function [target,TPcurr] = class_subtrial_dynamic(ys,flashseq,TPprev,sbtrcount,Mthresh,isSpeller,maxrep)
# %
# % THIS IS EXECUTED ONCE PER SUBTRIAL !
# %
# % INPUT
# % flashseq [stimnum x 1]
# % ys [stimnum x 1]
# % TPprev Current score matrix
# % sbtrcount Current repetion
# % Mthresh Matrix brightness threshold
# % maxrep Maximal number of subtrials
# %
# % OUTPUT
# % target index of decoded stimulus
dummy = np.array(list(zip(flashseq, ys))).transpose()
dummy = dummy[:, np.argsort(dummy[0])]
if self.mode == PresentationMode.SPELLER_MODE:
rc = int(len(flashseq)/2)
TPcurr = TPprev + np.tile(dummy[1][0:rc],(rc,1))+np.tile(dummy[1][rc:],(rc,1)).transpose()
M = sum(Scale(TPcurr.flatten(),0,1))
#print("sum over scaled TP")
#print(M)
max_idx = np.argmax(TPcurr)
max_col = int(np.floor(max_idx / rc))
max_row = int(max_idx-(max_col*rc))
target = [max_col,max_row]
#print(TPcurr[max_col,max_row])
# TODO NOT speller case
else:
TPcurr = TPprev + dummy[1, :]
M = sum(Scale(TPcurr.flatten(), 0, 1))
print(colored("M = %s " % M, 'magenta'))
print(colored("M_thr is %s " % self.M_thr, 'red'))
print(colored("TPcurr has shape: %s: " % len(TPcurr), 'magenta'))
print(TPcurr)
target = np.argmax(TPcurr)
# else
#Check if we haven't met criteria yet and need to issue another subtrial
#print("TPcurr")
#print(TPcurr)
if(M > self.M_thr and subtrcount<maxRep):
target = -1
print("M is currently %s" % M)
return target, TPcurr
def getDataAtIndex(self,i):
returnData = self.lslrec.data[i:i+self.samples][:]
if self.do_channel_mask is True:
mask = [2, 3, 4, 5, 6, 7, 14, 15, 16]
returnData = filter.filter_data(returnData[:, mask].T, self.srate, l_freq=0.1, h_freq=None, method='iir', n_jobs=6)
returnData = returnData.T
return returnData
def loadData(self):
if self.hasPCA:
self.pcamat = np.load(self.prefix + "pcamat.npy")
self.fda_w = np.load(self.prefix + "fda_w.npy")
self.fda_b = float(np.load(self.prefix + "fda_b.npy"))
self.Tp_thr = float(np.load(self.prefix + "Tp_thr.npy"))
self.M_thr = float(np.load(self.prefix + "M_thr.npy"))
print("Threshold for dyn lim is %s" % self.M_thr)
self.srate = int(np.load(self.prefix + "srate.npy"))
self.samples = int(0.8 * self.srate)
self.mode = np.load(self.prefix + "presentation_mode.npy")
print("loaded data from " + self.prefix)
if __name__ == '__main__':
P3_classify()