-
Notifications
You must be signed in to change notification settings - Fork 0
/
calcMatPcaMeanEtcFinal.m
323 lines (210 loc) · 11.5 KB
/
calcMatPcaMeanEtcFinal.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
315
316
317
318
319
320
321
close all;
clear;
clc;
addpath("./functions")
%%
% subjects = [1];
% sessions = [1];
table_to_show = [];
table_to_show = [...
"subject1" ,...
"session1" ,...
"subject2" ,...
"session2" ,...
"original" ,...
"with real mean" ,...
"with train mean" ,...
"with seperate PCA" ,...
"with train PCA" ,...
];
for subject1 = 1:24
for sess1 = 1:8
for subject2 = 1:24
for sess2 = 1:8
if subject1> 7 & sess1 > 1
continue;
end
if subject2> 7 & sess2 > 1
continue;
end
if subject1 == subject2 & sess1 == sess2
continue;
end
%% get events 1
epsilon = 0.01;
max_iter = 100;
[Events1, vClass1] = getERPEvents(subject1, sess1);
% cov1 = covFromCellArrayOfEvents(Events1);
% mean_of_cov1 = riemannianMean(cov1, epsilon, max_iter);
%% get events 2
[Events2, vClass2] = getERPEvents(subject2, sess2);
% cov2 = covFromCellArrayOfEvents(Events2);
% mean_of_cov2 = riemannianMean(cov2, epsilon, max_iter);
%% transorm events 1 to events 2
% T = ( mean_of_cov2 * ( (mean_of_cov1)^(-1) ) ) ^ (1 / 2);
% Events1_transformed = {};
% for ii = 1 : length(Events1)
% Events1_transformed{ii} = (T * Events1{ii}')';
% end
%
% cov_transformed = covFromCellArrayOfEvents(Events1_transformed);
%% add extra chanels
Events1_with_oracle_mean = addAverageSub(Events1, vClass1);
Events2_with_oracle_mean = addAverageSub(Events2, vClass2);
% Events1_with_oracle_mean;
[~,Events2_with_train_mean] = addDiffrenceAverageOldDataTest( Events1 ,...
Events2 ,...
vClass1);
Events1_with_seperate_pca = addPCAAverage(Events1);
Events2_with_seperate_pca = addPCAAverage(Events2);
[Events1_with_pca_train,...
Events2_with_pca_train] = addPCATest(Events1, Events2);
%%
c_data_for_classifier = {};
c_description_for_data = {};
funcs = { @covFromCellArrayOfEvents};
funcs_names = { "Covariance"};
tmp_description = "subject " + num2str(subject1) + " session " + num2str(sess1);
events_names = {
"Events1" + tmp_description,...
"Events2" + tmp_description,...
"Events1 with oracle mean" + tmp_description,...
"Events2 with oracle mean" + tmp_description,...
"Events1 with train mean" + tmp_description,...
"Events2 with train mean" + tmp_description,...
"Events1 with seperate pca" + tmp_description,...
"Events2 with seperate pca" + tmp_description,...
"Events1 with train pca" + tmp_description,...
"Events2 with train pca" + tmp_description,...
};
events_cell = {
Events1 ,...
Events2 ,...
Events1_with_oracle_mean ,...
Events2_with_oracle_mean ,...
Events2_with_train_mean ,...
Events1_with_seperate_pca ,...
Events2_with_seperate_pca ,...
Events1_with_pca_train ,...
Events2_with_pca_train ,...
};
%set base functions
% all_base_functions = ["linear", "gaussian", "polynomial"];
all_base_functions = ["linear"];
%extract the features
[c_data_for_classifier, c_description_for_data] = extractFeatures( events_cell ,...
events_names,...
funcs ,...
funcs_names );
%%
NUM_OF_RES = 5;
accuracy = zeros(NUM_OF_RES, 1);
tmp_idx = 1;
pre1 = calPrecisionERP(...
c_data_for_classifier{1},...
c_data_for_classifier{2},...
vClass1 ,...
vClass2 ...
);
accuracy(tmp_idx) = accuracy(tmp_idx) + pre1;
tmp_idx = tmp_idx + 1;
pre1 = calPrecisionERP(...
c_data_for_classifier{3},...
c_data_for_classifier{4},...
vClass1 ,...
vClass2 ...
);
accuracy(tmp_idx) = accuracy(tmp_idx) + pre1;
tmp_idx = tmp_idx + 1;
pre1 = calPrecisionERP( ...
c_data_for_classifier{3},...
c_data_for_classifier{5},...
vClass1 ,...
vClass2 ...
);
accuracy(tmp_idx) = accuracy(tmp_idx) + pre1;
tmp_idx = tmp_idx + 1;
pre1 = calPrecisionERP( ...
c_data_for_classifier{6},...
c_data_for_classifier{7},...
vClass1 ,...
vClass2 ...
);
accuracy(tmp_idx) = accuracy(tmp_idx) + pre1;
tmp_idx = tmp_idx + 1;
pre1 = calPrecisionERP( ...
c_data_for_classifier{8},...
c_data_for_classifier{9},...
vClass1 ,...
vClass2 ...
);
accuracy(tmp_idx) = accuracy(tmp_idx) + pre1;
tmp_idx = tmp_idx + 1;
table_to_show = [table_to_show ;
[convertCharsToStrings(num2str(subject1)) ,...
convertCharsToStrings(num2str(sess1)) ,...
convertCharsToStrings(num2str(subject2)) ,...
convertCharsToStrings(num2str(sess2)) ,...
convertCharsToStrings(num2str( accuracy(1) )) ,...
convertCharsToStrings(num2str( accuracy(2) )) ,...
convertCharsToStrings(num2str( accuracy(3) )) ,...
convertCharsToStrings(num2str( accuracy(4) )) ,...
convertCharsToStrings(num2str( accuracy(5) ))]
];
str = "ERP_RES/ERP_all_subjects_all_sessions" + num2str(subject1)+ num2str(sess1)+ num2str(subject2)+ num2str(sess2);
save(str, "table_to_show");
end
end
end
end
str = "ERP_all_subjects_all_sessions";
save(str, "table_to_show");
%%
function [mean_1] = getMean(cInput, vClass)
%ADDAVERAGE Summary of this function goes here
% Detailed explanation goes here
%calc mean of vClass == 1
mat = cat(3, cInput{:});
mean_1 = mean(mat(:, :, vClass==2 ), 3);
end
function [] = showTSNElocal(flattened_cov, vClass)
tsne_points = tsne(flattened_cov');
% firsst session
class_1 = 1;
class_2 = 2;
scatter( tsne_points(vClass == class_1, 1), tsne_points(vClass == class_1, 2), 30, 'r', 'filled', 'MarkerEdgeColor', 'k' );
hold on;
scatter( tsne_points(vClass == class_2, 1), tsne_points(vClass == class_2, 2), 30, 'b', 'filled', 'MarkerEdgeColor', 'k' );
hold on;
% legend('not target', 'target');
end
function [] = showPCA(flattened_cov, vClass)
eigen_vectors = pca(flattened_cov');
two_components = (flattened_cov' * eigen_vectors(:, 1:2));
% firsst session
class_1 = 1;
class_2 = 2;
scatter( two_components(vClass == class_1, 1), two_components(vClass == class_1, 2), 30, 'r', 'filled', 'MarkerEdgeColor', 'k' );
hold on;
scatter( two_components(vClass == class_2, 1), two_components(vClass == class_2, 2), 30, 'b', 'filled', 'MarkerEdgeColor', 'k' );
hold on;
% legend('not target', 'target');
end
function [pre1] = calPrecisionERP(X, X_test,y, y_test, data_name)
t = templateSVM('Standardize', false, 'KernelFunction', 'linear');
Mdl = fitcecoc( X', ...
y, ...
'Learners', t);
try
predicted_label = predict( Mdl, X_test' );
tp = sum((y_test == 2) & (predicted_label == 2));
fp = sum((y_test == 1) & (predicted_label == 2));
if tp + fp ~= 0
pre1 = tp / (tp + fp);
else
pre1 = 0;
end
catch
pre1 = -1;
end
end