-
Notifications
You must be signed in to change notification settings - Fork 27
/
main_MI_online_crossSubject.m
314 lines (273 loc) · 13.4 KB
/
main_MI_online_crossSubject.m
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
%% Study the effect of transfer learning in 3 different components of online cross-subject MI classification:
% 1. Data alignment
% 2. Spatial filtering
% 3. Classification
%
%% Compare 27 different algorithms:
% 1. CSP-LDA
% 2. CSP-CLDA
% 3. CSP-wAR
% 4. CCSP-LDA
% 5. CCSP-CLDA
% 6. CCSP-wAR
% 7. RCSP-LDA
% 8. RCSP-CLDA
% 9. RCSP-wAR
% 10. EA-CSP-LDA
% 11. EA-CSP-CLDA
% 12. EA-CSP-wAR
% 13. EA-CCSP-LDA
% 14. EA-CCSP-CLDA
% 15. EA-CCSP-wAR
% 16. EA-RCSP-LDA
% 17. EA-RCSP-CLDA
% 18. EA-RCSP-wAR
% 19. PS-CSP-LDA
% 20. PS-CSP-CLDA
% 21. PS-CSP-wAR
% 22. PS-CCSP-LDA
% 23. PS-CCSP-CLDA
% 24. PS-CCSP-wAR
% 25. PS-RCSP-LDA
% 26. PS-RCSP-CLDA
% 27. PS-RCSP-wAR
%
% We study online cross-subject transfer, by combining data from all other subjects to form a single source domain
% Experiment 1: on Dataset 1, 7 subjects
% Experiment 2: on Dataset 2a, using training data of the 9 subjects only
% Experiment 3: on Dataset 2a, using evaluation data of the 9 subjects only
%
% Need the covariance toolbox: https://github.com/alexandrebarachant/covariancetoolbox
%
%% Dongrui Wu, [email protected]
clc; clearvars; close all; warning off all; rng('default');
nRepeat=30; % number of repeats to get statistically meaningful results
minN=4; % min number of target labeled samples
maxN=20; % max number of target labeled samples
nStep=4; % number of target labeled samples to add in each iteration
nAlgs=27; % Number of algorithms
for ds=1:2
switch ds
case 1
files=dir('./Data1/A*.mat');
case 2
files=dir('./Data2/A*T.mat');
case 3
files=dir('./Data2/A*E.mat');
end
XRaw=[]; XallEA=[]; XallPS=[]; yAll=[]; nSubs=length(files);
for s=1:nSubs
s
load([files(s).folder '\' files(s).name]);
XRaw=cat(3,XRaw,X); yAll=cat(1,yAll,y); nTrials=length(y);
%% EA for all subjects
% Need the covariance toolbox: https://github.com/alexandrebarachant/covariancetoolbox
refEA=mean_covariances(covariances(X),'arithmetic');
sqrtRefEA=refEA^(-1/2);
XEA=nan(size(X));
for j=1:length(y)
XEA(:,:,j)=sqrtRefEA*X(:,:,j);
end
XallEA=cat(3,XallEA,XEA);
%% PS for all subjects
% Need the covariance toolbox: https://github.com/alexandrebarachant/covariancetoolbox
refPS=mean_covariances(covariances(X),'riemann');
sqrtRefPS=refPS^(-1/2);
XPS=nan(size(X));
for j=1:length(y)
XPS(:,:,j)=sqrtRefPS*X(:,:,j);
end
XallPS=cat(3,XallPS,XPS);
end
labels=unique(y);
%% Main loop
Accs=cell(1,nSubs);
for t=1:nSubs
idsTarget=(t-1)*nTrials+1:t*nTrials;
idsSource=1:nSubs*nTrials; idsSource(idsTarget)=[];
XtRaw=XRaw(:,:,idsTarget); yt=yAll(idsTarget);
XsRaw=XRaw(:,:,idsSource); XsEA=XallEA(:,:,idsSource);
XsPS=XallPS(:,:,idsSource); ys=yAll(idsSource);
ids1=find(ys==labels(1)); ids2=find(ys==labels(2));
acc=zeros(nAlgs,nRepeat,floor((maxN-minN)/nStep)+1);
parfor r=1:nRepeat
[t,r]
tempAcc=nan(nAlgs,floor((maxN-minN)/nStep)+1);
idsTrain0=round(nTrials*rand)+(1:maxN);
idsTrain0(idsTrain0>nTrials)=idsTrain0(idsTrain0>nTrials)-nTrials;
while length(unique(yt(idsTrain0(1:minN))))==1 % make sure the first minN samples include two classes
idsTrain0=round(nTrials*rand)+(1:maxN);
idsTrain0(idsTrain0>nTrials)=idsTrain0(idsTrain0>nTrials)-nTrials;
end
idsTest0=1:nTrials; idsTest0(idsTrain0)=[];
%% Online calibration
for n=minN:nStep:maxN % select training trials from the training pool
idsTrain=idsTrain0(1:n);
idsTest=cat(2,idsTrain0(n+1:end),idsTest0);
XtTestRaw=XtRaw(:,:,idsTest);
ytTest=yt(idsTest);
XtTrainRaw=XtRaw(:,:,idsTrain);
ytTrain=yt(idsTrain);
yTrain=cat(1,ytTrain,ys);
%% %%% Case 1: CSP filters from the target subject only, no EA, no TLCSP
% Target training data only
% 1. CSP-LDA
idxAlg=1;
[fTrain,fTest]=CSPfeature(XtTrainRaw,ytTrain,XtTestRaw);
LDA = fitcdiscr(fTrain,ytTrain); yPred=predict(LDA,fTest);
tempAcc(idxAlg,(n-minN)/nStep+1)=100*mean(ytTest==yPred);
% 2. CSP-CLDA
idxAlg=2;
[fTrain,fTest]=CSPfeature(XtTrainRaw,ytTrain,cat(3,XsRaw,XtTestRaw));
fTrain=cat(1,fTrain,fTest(1:length(ys),:)); fTest(1:length(ys),:)=[];
LDA = fitcdiscr(fTrain,yTrain); yPred=predict(LDA,fTest);
tempAcc(idxAlg,(n-minN)/nStep+1)=100*mean(ytTest==yPred);
% 3. CSP-wAR
idxAlg=3;
yPredWAR3=OwAR(fTrain(n+1:end,:),ys,cat(1,fTrain(1:n,:),fTest),ytTrain);
tempAcc(idxAlg,(n-minN)/nStep+1)=100*mean(ytTest==yPredWAR3);
%% %%% Case 2: CCSP, use source data in CSP, no EA
% 4. CCSP-LDA
idxAlg=4;
[fTrain,fTest]=CSPfeature(cat(3,XtTrainRaw,XsRaw),yTrain,XtTestRaw);
LDA = fitcdiscr(fTrain(1:n,:),ytTrain); yPred=predict(LDA,fTest);
tempAcc(idxAlg,(n-minN)/nStep+1)=100*mean(ytTest==yPred);
% 5. CCSP-CLDA
idxAlg=5;
LDA = fitcdiscr(fTrain,yTrain); yPred=predict(LDA,fTest);
tempAcc(idxAlg,(n-minN)/nStep+1)=100*mean(ytTest==yPred);
% 6. CCSP+wAR
idxAlg=6;
yPredWAR6=OwAR(fTrain(n+1:end,:),ys,cat(1,fTrain(1:n,:),fTest),ytTrain);
tempAcc(idxAlg,(n-minN)/nStep+1)=100*mean(ytTest==yPredWAR6);
%% %%% Case 3: RCSP, use source data in CSP, no EA
% 7. RCSP-LDA
idxAlg=7;
[fTrain,fTest]=RCSPfeature(XsRaw,ys,cat(3,XtTrainRaw,XtTestRaw),ytTrain);
LDA = fitcdiscr(fTrain(1:n,:),ytTrain); yPred=predict(LDA,fTest);
tempAcc(idxAlg,(n-minN)/nStep+1)=100*mean(ytTest==yPred);
% 8. RCSP-CLDA
idxAlg=8;
LDA = fitcdiscr(fTrain,yTrain); yPred=predict(LDA,fTest);
tempAcc(idxAlg,(n-minN)/nStep+1)=100*mean(ytTest==yPred);
% 9. RCSP-wAR
idxAlg=9;
yPredWAR9=OwAR(fTrain(n+1:end,:),ys,cat(1,fTrain(1:n,:),fTest),ytTrain);
tempAcc(idxAlg,(n-minN)/nStep+1)=100*mean(ytTest==yPredWAR9);
%% %%%%%%%%%%%%%%% EA %%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% EA for target data
refEA=mean(covariances(XtTrainRaw),3); % EA reference matrix for new subject
sqrtRefEA=refEA^(-1/2);
XtEA=nan(size(XtRaw));
for j=1:size(XtRaw,3)
XtEA(:,:,j)=sqrtRefEA*XtRaw(:,:,j);
end
%% %%% Case 4: EA + CSP, no TL in CSP
% 10. EA-CSP-LDA
idxAlg=10;
[fTrain,fTest]=CSPfeature(XtEA(:,:,idsTrain),ytTrain,XtEA(:,:,idsTest));
LDA = fitcdiscr(fTrain,ytTrain); yPred=predict(LDA,fTest);
tempAcc(idxAlg,(n-minN)/nStep+1)=100*mean(ytTest==yPred);
% 11. EA-CSP-CLDA
idxAlg=11;
[fTrain,fTest]=CSPfeature(XtEA(:,:,idsTrain),ytTrain,cat(3,XsEA,XtEA(:,:,idsTest)));
fTrain=cat(1,fTrain,fTest(1:length(ys),:)); fTest(1:length(ys),:)=[];
LDA = fitcdiscr(fTrain,yTrain); yPred=predict(LDA,fTest);
tempAcc(idxAlg,(n-minN)/nStep+1)=100*mean(ytTest==yPred);
% 12. EA-CSP-wAR
idxAlg=12;
yPredWAR12=OwAR(fTrain(n+1:end,:),ys,cat(1,fTrain(1:n,:),fTest),ytTrain);
tempAcc(idxAlg,(n-minN)/nStep+1)=100*mean(ytTest==yPredWAR12);
%% %%% Case 5: EA + CCSP
% 13. EA-CCSP-LDA
idxAlg=13;
[fTrain,fTest]=CSPfeature(cat(3,XtEA(:,:,idsTrain),XsEA),yTrain,XtEA(:,:,idsTest));
LDA = fitcdiscr(fTrain(1:n,:),ytTrain); yPred=predict(LDA,fTest);
tempAcc(idxAlg,(n-minN)/nStep+1)=100*mean(ytTest==yPred);
% 14. EA-CCSP-CLDA
idxAlg=14;
LDA = fitcdiscr(fTrain,yTrain); yPred=predict(LDA,fTest);
tempAcc(idxAlg,(n-minN)/nStep+1)=100*mean(ytTest==yPred);
% 15. EA-CCSP-wAR
idxAlg=15;
yPredWAR15=OwAR(fTrain(n+1:end,:),ys,cat(1,fTrain(1:n,:),fTest),ytTrain);
tempAcc(idxAlg,(n-minN)/nStep+1)=100*mean(ytTest==yPredWAR15);
%% %%% Case 6: EA + RCSP
% 16. EA-RCSP-LDA
idxAlg=16;
[fTrain,fTest]=RCSPfeature(XsEA,ys,XtEA(:,:,[idsTrain idsTest]),ytTrain);
LDA = fitcdiscr(fTrain(1:n,:),ytTrain); yPred=predict(LDA,fTest);
tempAcc(idxAlg,(n-minN)/nStep+1)=100*mean(ytTest==yPred);
% 17. EA-RCSP-CLDA
idxAlg=17;
LDA = fitcdiscr(fTrain,yTrain); yPred=predict(LDA,fTest);
tempAcc(idxAlg,(n-minN)/nStep+1)=100*mean(ytTest==yPred);
% 18. EA-RCSP-wAR
idxAlg=18;
yPredWAR18=OwAR(fTrain(n+1:end,:),ys,cat(1,fTrain(1:n,:),fTest),ytTrain);
tempAcc(idxAlg,(n-minN)/nStep+1)=100*mean(ytTest==yPredWAR18);
%% %%%%%%%%%%%%% PS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% PS for target data
refPS=mean_covariances(covariances(XtTrainRaw),'riemann');
sqrtRefPS=refPS^(-1/2);
XtPS=nan(size(XtRaw));
for j=1:size(XtRaw,3)
XtPS(:,:,j)=sqrtRefPS*XtRaw(:,:,j);
end
%% %%% Case 7: PS + CSP, no TL in CSP
% 19. PS-CSP-LDA
idxAlg=19;
[fTrain,fTest]=CSPfeature(XtPS(:,:,idsTrain),ytTrain,cat(3,XsPS,XtPS(:,:,idsTest)));
fTrain=cat(1,fTrain,fTest(1:length(ys),:)); fTest(1:length(ys),:)=[];
LDA = fitcdiscr(fTrain(1:n,:),ytTrain); yPred=predict(LDA,fTest);
tempAcc(idxAlg,(n-minN)/nStep+1)=100*mean(ytTest==yPred);
% 20. PS-CSP-CLDA
idxAlg=20;
LDA = fitcdiscr(fTrain,yTrain); yPred=predict(LDA,fTest);
tempAcc(idxAlg,(n-minN)/nStep+1)=100*mean(ytTest==yPred);
% 21. PS-CSP-wAR
idxAlg=21;
yPredWAR21=OwAR(fTrain(n+1:end,:),ys,cat(1,fTrain(1:n,:),fTest),ytTrain);
tempAcc(idxAlg,(n-minN)/nStep+1)=100*mean(ytTest==yPredWAR21);
%% %%% Case 8: PS + CCSP
% 22. PS-CCSP-LDA
idxAlg=22;
[fTrain,fTest]=CSPfeature(cat(3,XtPS(:,:,idsTrain),XsPS),yTrain,XtPS(:,:,idsTest));
LDA = fitcdiscr(fTrain(1:n,:),ytTrain); yPred=predict(LDA,fTest);
tempAcc(idxAlg,(n-minN)/nStep+1)=100*mean(ytTest==yPred);
% 23. PS-CCSP-CLDA
idxAlg=23;
LDA = fitcdiscr(fTrain,yTrain); yPred=predict(LDA,fTest);
tempAcc(idxAlg,(n-minN)/nStep+1)=100*mean(ytTest==yPred);
% 24. PS-CCSP-wAR
idxAlg=24;
yPredWAR24=OwAR(fTrain(n+1:end,:),ys,cat(1,fTrain(1:n,:),fTest),ytTrain);
tempAcc(idxAlg,(n-minN)/nStep+1)=100*mean(ytTest==yPredWAR24);
%% %%% Case 9: PS + RCSP
% 25. PS-RCSP-LDA
idxAlg=25;
[fTrain,fTest]=RCSPfeature(XsPS,ys,XtPS(:,:,[idsTrain idsTest]),ytTrain);
LDA = fitcdiscr(fTrain(1:n,:),ytTrain); yPred=predict(LDA,fTest);
tempAcc(idxAlg,(n-minN)/nStep+1)=100*mean(ytTest==yPred);
% 26. PS-RCSP-CLDA
idxAlg=26;
LDA = fitcdiscr(fTrain,yTrain); yPred=predict(LDA,fTest);
tempAcc(idxAlg,(n-minN)/nStep+1)=100*mean(ytTest==yPred);
% 27. PS-RCSP-wAR
idxAlg=27;
yPredWAR27=OwAR(fTrain(n+1:end,:),ys,cat(1,fTrain(1:n,:),fTest),ytTrain);
tempAcc(idxAlg,(n-minN)/nStep+1)=100*mean(ytTest==yPredWAR27);
end
acc(:,r,:)=tempAcc;
end
squeeze(mean(acc,2))
Accs{t}=acc;
end
eval(['Accs' num2str(ds) '=Accs;']);
mmAcc=zeros(nAlgs,(maxN-minN)/nStep+1);
for t=1:nSubs
mmAcc=mmAcc+squeeze(mean(Accs{t},2))/nSubs;
end
mmAcc
save(['OnlineMI_dataset' num2str(ds) '.mat'],'Accs','nStep','minN','maxN','nAlgs','nSubs','mmAcc');
end