-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspatialFilter.py
224 lines (181 loc) · 6.92 KB
/
spatialFilter.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
import numpy as np
import scipy.linalg as la
from mne.preprocessing.xdawn import _fit_xdawn
from mne.decoding import CSP as BaseCSP
class TRCA():
def __init__(self,n_components=2):
self.n_components = n_components
def fit(self,X,y):
"""
Parameters
----------
X : ndarray, shape (n_epochs, n_channels, n_times)
The data on which to estimate the CSP.
y : array, shape (n_epochs,)
The class for each epoch.
Returns
-------
self : instance of TRCA
Returns the modified instance.
"""
self._classes = np.unique(y)
n_classes = len(self._classes)
filter = []
evokeds = []
for classINX in range(n_classes):
this_class_data = X[y==classINX]
evoked = np.mean(this_class_data,axis=0)
evokeds.append(evoked)
weight = self.computer_trca_weight(this_class_data)
filter.append(weight)
self.filter = filter
self.evokeds = evokeds
return self
def transform(self,X):
"""
Parameters
----------
X : array, shape (n_epochs, n_channels, n_times)
The data.
Returns
-------
X : ndarray
shape is (n_epochs, n_sources, n_times).
"""
enhanced = []
for classINX in range(len(self._classes)):
X_filtered = np.dot(self.filter[classINX][:,:self.n_components].T, X)
X_filtered = X_filtered.transpose((1, 0, 2))
X_filtered = np.stack(X_filtered[i].ravel() for i in range(X.shape[0]))
enhanced.append(X_filtered)
enhanced = np.concatenate(enhanced,axis=-1)
return enhanced
def computer_trca_weight(self,eeg):
"""
Input:
eeg : Input eeg data (# of targets, # of channels, Data length [sample])
Output:
W : Weight coefficients for electrodes which can be used as a spatial filter.
"""
epochNUM,self.channelNUM,_ = eeg.shape
S = np.zeros((self.channelNUM,self.channelNUM))
for epoch_i in range(epochNUM):
x1 = np.squeeze(eeg[epoch_i,:,:])
x1 = x1 - np.mean(x1,axis=1,keepdims=True)
for epoch_j in range(epoch_i+1,epochNUM):
x2 = np.squeeze(eeg[epoch_j,:,:])
x2 = x2 - np.mean(x2,axis=1,keepdims=True)
S = S + np.dot(x1,x2.T)+ np.dot(x2,x1.T)
UX = np.stack(eeg[:,i,:].ravel() for i in range(self.channelNUM))
UX = UX - np.mean(UX,axis=1,keepdims=True)
Q = np.dot(UX,UX.T)
_,W = la.eig(S,Q)
return W
class Xdawn():
def __init__(self,n_components=2):
self.n_components = n_components
def fit(self,X,y):
"""
Parameters
----------
X : ndarray, shape (n_epochs, n_channels, n_times)
The data on which to estimate the CSP.
y : array, shape (n_epochs,)
The class for each epoch.
Returns
-------
self : instance of TRCA
Returns the modified instance.
"""
self.channelNUM = X.shape[1]
self._classes = np.unique(y)
filters,_,evokeds = _fit_xdawn(X,y,n_components=self.channelNUM)
filters = filters.T
self.filter = np.split(filters,indices_or_sections=2,axis=1)
self.evokeds = evokeds
return self
def transform(self,X):
"""
Parameters
----------
X : ndarray, shape (n_epochs, n_channels, n_times)
The data on which to estimate the CSP.
Returns
-------
self : instance of TRCA
Returns the modified instance.
"""
enhanced = []
for classINX in range(len(self._classes)):
X_filtered = np.dot(self.filter[classINX][:,:self.n_components].T, X)
X_filtered = X_filtered.transpose((1, 0, 2))
X_filtered = np.stack(X_filtered[i].ravel() for i in range(X.shape[0]))
enhanced.append(X_filtered)
enhanced = np.concatenate(enhanced,axis=-1)
return enhanced
class CSP(BaseCSP):
def transform(self, X):
self.filters_ = self.filters_.T
enhanced = []
for classINX in range(len(self._classes)):
X_filtered = np.dot(self.filter[classINX][:,:self.n_components].T, X)
X_filtered = X_filtered.transpose((1, 0, 2))
X_filtered = np.stack(X_filtered[i].ravel() for i in range(X.shape[0]))
enhanced.append(X_filtered)
enhanced = np.concatenate(enhanced,axis=-1)
return enhanced
def fit(self, X, y):
"""Estimate the CSP decomposition on epochs.
Parameters
----------
X : ndarray, shape (n_epochs, n_channels, n_times)
The data on which to estimate the CSP.
y : array, shape (n_epochs,)
The class for each epoch.
Returns
-------
self : instance of CSP
Returns the modified instance.
"""
self._check_Xy(X, y)
self._classes = np.unique(y)
n_classes = len(self._classes)
covs, sample_weights = self._compute_covariance_matrices(X, y)
eigen_vectors, eigen_values = self._decompose_covs(covs,
sample_weights)
filters = []
for classINX in range(n_classes):
ix = self._order_components(covs[classINX], sample_weights[classINX], eigen_vectors[classINX],
eigen_values[classINX], self.component_order)
eigen_vector = eigen_vectors[classINX][:,ix]
filters.append(eigen_vector)
self.filters_ = filters
return self
def transform(self,X):
"""
Parameters
----------
X : ndarray, shape (n_epochs, n_channels, n_times)
The data on which to estimate the CSP.
Returns
-------
self : instance of TRCA
Returns the modified instance.
"""
enhanced = []
for classINX in range(len(self._classes)):
X_filtered = np.dot(self.filters_[classINX][:,:self.n_components].T, X)
X_filtered = X_filtered.transpose((1, 0, 2))
X_filtered = np.stack(X_filtered[i].ravel() for i in range(X.shape[0]))
enhanced.append(X_filtered)
enhanced = np.concatenate(enhanced,axis=-1)
return enhanced
def _decompose_covs(self, covs, sample_weights):
n_classes = len(covs)
eigen_vectors = []
eigen_values = []
for classINX in range(n_classes):
eigen_value, eigen_vector = la.eigh(covs[classINX], covs.sum(0))
eigen_vectors.append(eigen_vector)
eigen_values.append(eigen_value)
return eigen_vectors, eigen_values