-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildVEPTraces.m
326 lines (245 loc) · 8.06 KB
/
buildVEPTraces.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
322
323
324
325
326
gendir = '\\192.168.1.206\LabData';
USE_FITLERED_DATA = 1;
dirIn = [ gendir, '\adeeti\ecog\matIsoPropMultiStim\'];
dirOut = 'C:\Users\adeeti\Dropbox\ProektLab_code\machinelearning\';
dirOut = 'F:\Dropbox\ProektLab_code\MachineLearning\';
identifer = '2018*mat';
if USE_FITLERED_DATA
allData = dir([dirIn 'Wavelets\FiltData\' identifer]);
else
allData = dir([dirIn identifer]);
end
load([dirIn 'dataMatrixFlashes.mat'])
spontAct = [1:1001];
VEP = [1030:1300];
inputs = [];
outputs = [];
IDs = [];
inputsOneExp = [];
outputsOneExp = [];
IDsOneExp = [];
% counter = 1;
% for i = 1:length(allData)-4
% load(allData(i).name, 'meanSubData', 'info', 'uniqueSeries', 'indexSeries')
% disp(num2str(i));
% [indices] = getStimIndices([0 inf], indexSeries, uniqueSeries);
% useData = squeeze(meanSubData(info.lowLat,indices,:));
% for j = 1:size(useData,1)
% inputs(counter,:) = useData(j,spontAct);
% outputs(counter,:) = useData(j,VEP);
% IDs(counter) = i;
% counter = counter +1;
% end
% end
useEXP = 5;
drugType = [];
conc = [];
stimIndex = [0, Inf];
[MFE]=findMyExpMulti(dataMatrixFlashes, useEXP, drugType, conc, stimIndex);
times = 1:2000;
VEPs = {};
infos = {};
counter = 1;
for i = MFE
%for i = 1:size(dataMatrixFlashesVIS_ONLY,2)-4
if USE_FITLERED_DATA
load([allData(i).folder '\' allData(i).name], 'filtSig35', 'info', 'indexSeries', 'uniqueSeries')
else
load([allData(i).folder '\' allData(i).name], 'smallSnippits', 'info', 'indexSeries', 'uniqueSeries')
end
disp([num2str(counter) ' -> ' info.AnesType ' ' num2str(info.AnesLevel)]);
[indices] = getStimIndices([0 inf], indexSeries, uniqueSeries);
if USE_FITLERED_DATA
VEPs{counter} = squeeze(filtSig35(times,:,indices));
else
VEPs{counter} = squeeze(smallSnippits(:,indices,times));
VEPs{counter} = permute(VEPs{counter}, [3,1,2]);
end
infos{counter} = info;
counter = counter +1;
end
% save([dirOut,'VEPs_IsoProp.mat'], 'inputs', 'outputs', 'IDs')
%save([dirOut,'SingleEXP_VEPs_IsoProp.mat'], 'inputs', 'outputs', 'IDs')
%%
trialID = 75;
playData = VEPs{4};
avgCovMatrix = {};
covMatrix = {};
for j = 1
if j == 1
time = 1:500;
else
time = 1000:1500;
end
average = squeeze(mean(playData, 3));
covMatrix{j} = corr(playData(time, :, trialID));
avgCovMatrix{j} = corr(average(time, :));
end
%%
aboveOrBelow = [];
corrValues = [];
for i = 1:2
for j = 1:size(playData,3)
if i == 1
time = 1:500;
else
time = 1000:1500;
end
corrValues(i,j) = nansum(nansum(abs(corr(playData(time, :, j)))));
end
[~, midPoint] = kmeans(corrValues(i,:)', 2);
midPoint = mean(midPoint);
aboveOrBelow(i,:) = corrValues(i,:) > midPoint;
end
figure(1);
straws = 20;
clf;
hold on
histogram(corrValues(1,:), straws)
histogram(corrValues(2,:), straws)
figure(2);
clf;
hold on;
% plot(aboveOrBelow', '-o');
scatter(corrValues(1,:), corrValues(2,:))
corr(corrValues(1,:)', corrValues(2,:)')
%%
figure(3)
clf
% subplot(1,2,1)
% covMatrix = corr(playData(time, :, trialID));
% imagesc(covMatrix);
imagesc(covMatrix{2} - covMatrix{1})
% subplot(1,2,2)
% covMatrix = corr(average(time, :));
% imagesc(covMatrix);
% imagesc(avgCovMatrix{2} - avgCovMatrix{1})
%%
USE_SVD = 0;
Z_SCORE = 0;
SPLIT_SINGLE_TRIAL = 0;
NUM_BOOTS = 1;
times = 950:1350;
colors = {'k', 'r', 'g', 'b'};
electrodes = [47, 31, 21, 46, 30, 20, 45, 29, 19];
figure(1);
clf
hold on;
allData = {};
allAverages = [];
counter = 1;
for expID = [1,2]
allData{counter} = VEPs{expID};%(:,electrodes,:);
for i = 1:size(allData{counter},2)
allData{counter}(:,i,:) = hilbert(squeeze(allData{counter}(:,i,:)));
allData{counter}(:,i,:) = (angle(allData{counter}(:,i,:)));
end
allData{counter} = permute(allData{counter}, [1, 3, 2]);
p = plot(allData{counter}(:,:,infos{counter}.lowLat), colors{counter});
for i = 1:length(p)
p(i).Color(4) = 0.3;
end
xlim([950 1150]);
%
if Z_SCORE
for i = 1:size(allData{counter},2)
allData{counter}(:,i,:) = zscore(squeeze(allData{counter}(:,i,:)), [], 1);
end
% else
% for i = 1:size(allData{counter},2)
% allData{counter}(:,i,:) = (squeeze(allData{counter}(:,i,:))) .* std(squeeze(allData{counter}(:,i,:)));
% end
end
indexList = 1:size(allData{counter},2);
if SPLIT_SINGLE_TRIAL
trialEnds = 50;
if counter == 1
% indexList = 1:floor(length(indexList)/2);
indexList = 1:trialEnds;
else
% indexList = floor(length(indexList)/2)+1:length(indexList);
indexList = length(indexList)-trialEnds+1:length(indexList);
end
% indexList = randsample(1:length(indexList), length(indexList), true);
end
allData{counter} = allData{counter}(:,indexList,:);
average = squeeze(mean(allData{counter}, 2));
allAverages = [allAverages; average];
counter = counter + 1;
end
badIndices = [];
for i = 1:size(allAverages,2)
if sum(isnan(allAverages(:,i))) > 0
badIndices = [badIndices i];
end
end
goodIndices = setdiff(1:size(allAverages,2), badIndices);
allAverages = allAverages(:,goodIndices);
if USE_SVD
[U,S,V] = svd(allAverages');
spatialModes = U;
invSpatialModes = inv(spatialModes);
else
[pcaBasis, pcaOutputs, ~, ~, explained] = pca(allAverages, 'NumComponents', 3);
end
figure(2);
clf
hold on;
for expID = 1:length(allData)
plotData = allData{expID}(times,:,goodIndices);
% plotData = permute(plotData, [1, 3, 2]);
% plotData = plotData(50:100,:,find(~isnan(plotData(1,1,:))));
%plotData = plotData(:,:,find(~isnan(plotData(1,1,:))));
% for i = 1:size(plotData,2)
% plotData(:,i,:) = zscore(squeeze(plotData(:,i,:)), [], 1);
% end
average = squeeze(mean(plotData, 2));
% pcaTrace = reshape(plotData, [size(plotData,1) * size(plotData,2), size(plotData,3)]);
% pcaTrace(isnan(pcaTrace)) = 0;
%
%
for i = 1:50
thisIndicies = randsample(size(plotData, 2), NUM_BOOTS, 1);
thisData = squeeze(mean(plotData(:,thisIndicies,:), 2));
if USE_SVD
plotTrace = (invSpatialModes * thisData')';
else
plotTrace = thisData*pcaBasis;
end
if USE_SVD
p = plot3(plotTrace(:,1), plotTrace(:,3), plotTrace(:,5), colors{expID}, 'linewidth', 0.5);
else
p = plot3(plotTrace(:,1), plotTrace(:,2), plotTrace(:,3), colors{expID}, 'linewidth', 0.5);
end
p.Color(4) = 0.2;
end
if USE_SVD
plotTrace = (invSpatialModes * average')';
else
plotTrace = average*pcaBasis;
end
if USE_SVD
plot3(plotTrace(:,1), plotTrace(:,3), plotTrace(:,5), colors{expID}, 'linewidth', 3);
else
plot3(plotTrace(:,1), plotTrace(:,2), plotTrace(:,3), colors{expID}, 'linewidth', 3);
end
end
%%
[U,S,V] = svd(average');
spatialModes = U;
invSpatialModes = inv(spatialModes);
times = 1:150;
clf
hold on
% for i = 1:10:size(plotData, 2)
for i = 1:10
thisIndicies = randsample(1:size(plotData, 2), ceil(size(plotData, 2)), 1);
% thisIndicies = 1:size(plotData, 2);
thisData = squeeze(mean(plotData(:,thisIndicies,:), 2));
temporalModes = invSpatialModes * thisData';
plot3(temporalModes(1,times), temporalModes(3,times), temporalModes(5,times));
% plot(temporalModes(1,times), temporalModes(2,times));
end
temporalModes = invSpatialModes * average';
plot3(temporalModes(1,times), temporalModes(3,times), temporalModes(5,times), 'k', 'linewidth', 3);
% plot(temporalModes(1,times), temporalModes(2,times), 'k', 'linewidth', 3);