-
Notifications
You must be signed in to change notification settings - Fork 0
/
rd_Cupcake3_decode.m
342 lines (289 loc) · 9.19 KB
/
rd_Cupcake3_decode.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
% function rd_Cupcake3_decode(exptDir, sessionDir)
%% Setup
exptName = 'CupcakeAperture';
% exptDir = '/Local/Users/denison/Data/Cupcake';
exptDir = '/Volumes/purplab2/EXPERIMENTS/1_Current_Experiments/Rachel/Cupcake/Cupcake_Aperture'; % '/Local/Users/denison/Google Drive/Shared/Projects/Cupcake/Code/MEG_Expt/Pilot1_Aperture';
megDir = 'MEG';
sessionDir = 'R1507_20200311/vignette'; %'R1507_20190425/concentric';
analStr = 'vignette_bie'; %'concentric_ebci';
fileBase = sessionDirToFileBase(sessionDir, exptName);
megDataDir = sprintf('%s/%s/%s', exptDir, megDir, sessionDir);
matDir = sprintf('%s/mat', megDataDir);
figDir = sprintf('%s/figures/%s', megDataDir, analStr);
analysisFileName = sprintf('%s/classAcc', matDir);
% load data header for plotting topologies
load data/data_hdr.mat
saveFigs = 1;
saveAnalysis = 1;
%% Load data
dataFile = dir(sprintf('%s/*%s_condData.mat', matDir, analStr));
load(sprintf('%s/%s', dataFile.folder, dataFile.name));
%% Unpack data structure
t = D.t;
Fs = D.Fs;
eventTimes = D.eventTimes;
orientations = D.orientations;
condData = D.condData;
condTrials = D.condTrials;
trialsHeaders = D.trialsHeaders;
nT = numel(t);
nOr = numel(orientations);
nTrials = size(condData,3);
%% Baseline
% baselinePeriod = t;
% inBaseline = ismember(t,baselinePeriod);
% baselineDC = mean(condData(inBaseline,:,:,:),1);
% baselineTSeries = repmat(baselineDC,[size(condData,1),1,1,1]);
%
% % condDataB = condData-baselineTSeries;
% condData = condData-baselineTSeries;
%% Decoding setup
getWeights = 1;
syntheticTrials = 0;
%% remove nan data
dataInput = [];
for iOr = 1:nOr
for iTrial = 1:nTrials
vals = condData(:,:,iTrial,iOr);
idx = isnan(vals(1,:));
vals(:,idx) = [];
dataInput{iOr}(:,~idx,iTrial) = vals; % sets nan to zero, which maybe we don't want
end
end
%% decoding setup
targetWindow = [0 400];
nSynTrials = 100; % if constructing synthetic trials
nt = 5; % 5 % average this many trials together to improve SNR
sp = 5; % 5 % sampling period
kfold = 5;
svmops = sprintf('-s 0 -t 0 -c 1 -v %d -q', kfold);
svmopsNoCV = '-s 0 -t 0 -c 1 -q';
decodeAnalStr = sprintf('sp%d_nt%d', sp, nt);
figName = {'classAcc'};
% classNames = {'0 vs 90','22.5 vs 112.5','45 vs 135','67.5 vs 157.5'};
classNames = {'0 vs 90'};
if syntheticTrials
nReps = 1;
else
nReps = nt;
end
%% decoding
% % grid search
% nTarget = 1;
% cParams = 2.^(-1:.5:3); %2.^(-5:2:15);
% nCParams = numel(cParams);
% channels = [15 60 26 14 43 23 26 8 7 1 50 51 2 20 25 13 32 63];
channels = 1:157;
times = targetWindow(1):sp:targetWindow(2);
classAccNT = [];
for iRep = 1:nReps
% % grid search
% classAccC = [];
% for iC = 1:nCParams
% svmops = sprintf('-s 0 -t 0 -c %f -v %d -q', cParams(iC), kfold);
% disp(svmops)
classAcc = [];
for iOr = 1:nOr/2
vals1 = dataInput{iOr}; % orientation 1
vals2 = dataInput{iOr+nOr/2}; % orientation 1 + 90 degrees
% average trials
if nt > 1
vals1a = []; vals2a = [];
n = size(vals1,3);
if syntheticTrials
nIdx = nSynTrials*nt;
trialsIdx = [];
for i = 1:ceil(nIdx/n)
trialsIdx = [trialsIdx randperm(n)];
end
startTrials = 1:nt:nIdx;
else
trialsIdx = randperm(n);
startTrials = 1:nt:n-nt; % n -> n-nt
end
for iST = 1:numel(startTrials)
trIdx = trialsIdx(startTrials(iST):startTrials(iST)+nt-1);
vals1a(:,:,iST) = mean(vals1(:,:,trIdx),3);
vals2a(:,:,iST) = mean(vals2(:,:,trIdx),3);
end
vals1 = vals1a; vals2 = vals2a;
end
vals0 = cat(3, vals1, vals2);
labels0 = [ones(size(vals1,3),1); zeros(size(vals2,3),1)];
%% stratify
nSamples = numel(labels0);
foldSize = ceil(nSamples/kfold/2); % 2 classes
stratIdx = [];
for iFold = 1:kfold
idx1 = (1:foldSize) + (iFold-1)*foldSize;
idx2 = idx1 + nSamples/2;
stratIdx = [stratIdx idx1 idx2];
end
stratIdxS = sort(stratIdx);
r = stratIdxS(diff(stratIdxS)==0);
ridx = [];
for iR = 1:numel(r)
ridx(iR) = find(stratIdx==r(iR),1,'last');
end
stratIdx(ridx) = [];
if numel(stratIdx)>numel(labels0)
stratIdx(numel(labels0)+1:end) = [];
end
vals = vals0(:,channels,stratIdx);
labels = labels0(stratIdx);
%% classify
tic
acc = [];
for iTime = 1:numel(times)
fprintf(' ')
time = times(iTime);
% classification data
X = squeeze(mean(vals(find(t==time):find(t==time+sp-1),:,:),1))'; % average across time window
Y = labels;
% remove nan
idx = isnan(X(:,1));
X(idx,:) = [];
Y(idx) = [];
% scale data
Xs = zscore(X);
% Xss = Xs./repmat(max(abs(Xs)),size(Xs,1),1); % range [-1,1]
% fit and cross validate classifier
acc(iTime) = svmtrain(Y, Xs, svmops);
% % example of separate prediction and classification steps
% model1 = svmtrain(trainlabels1, trainfeatures1, '-s 0 -t 0 -c 1');
% predlabels = svmpredict(testlabels1, testfeatures1, model1);
% predacc = mean(predlabels==testlabels1);
% get the svm model, no cv
if getWeights
model(iTime) = svmtrain(Y, Xs, svmopsNoCV);
else
model = [];
end
end
toc
classAcc(:,iOr) = acc;
classModel{iOr} = model;
end
% % grid search
% classAccC(:,:,:,iC) = classAcc;
% end
% trial average
classAccNT(:,:,iRep) = classAcc;
classModelNT(:,iRep) = classModel';
end
%% extract channel weights
if getWeights
classWeights = [];
for iOr = 1:nOr/2
for iTime = 1:numel(times)
for iRep = 1:nReps
model = classModelNT{iOr,iRep}(iTime);
w = model.SVs' * model.sv_coef;
b = -model.rho;
if (model.Label(1) == -1)
w = -w; b = -b;
end
classWeights(:,iTime,iOr,iRep) = w;
end
end
end
else
classWeights = [];
end
%% plot
xlims = targetWindow;
ylims = [30 100];
figure
hold on
plot(times, mean(classAccNT,3),'LineWidth',1)
plot(times, mean(mean(classAccNT,3),2), 'k')
plot(xlims,[50 50],'k')
xlim(xlims)
ylim(ylims)
xlabel('Time (ms)')
ylabel('Classification accuracy (%)')
legend(classNames)
if saveFigs
rd_saveAllFigs(gcf, {sprintf('%s_%s',figName{1},decodeAnalStr)}, 'plot', figDir)
end
%% topo weights movie T1 and T2
if getWeights
clims = [-2 2];
figure('Position',[250 850 950 450])
for iTime = 1:numel(times)
for iOr = 1:nOr/2
vals = squeeze(mean(classWeights(:,iTime,iOr,:),4))';
subplot(1,nOr/2,iOr)
ssm_plotOnMesh(vals, '', [], data_hdr, '2d');
set(gca,'CLim',clims)
colorbar
title(classNames{iOr})
end
rd_supertitle2(sprintf('t = %d', times(iTime)))
pause(0.2)
% input('go')
end
end
%% topo weights for specific time intervals
twins = {[110 140], [140 230], [230 280], [280 315], [110 315]};
if getWeights
clims = [0 1.5];
for iTW = 1:numel(twins)
twin = twins{iTW};
tidx = find(times==twin(1)):find(times==twin(2));
figure('Position',[250 850 950 450])
for iOr = 1:nOr/2
vals = squeeze(mean(mean(abs(classWeights(:,tidx,iOr,:)),4),2))';
subplot(1,nOr/2,iOr)
ssm_plotOnMesh(vals, '', [], data_hdr, '2d');
set(gca,'CLim',clims)
colorbar
title(classNames{iOr})
end
rd_supertitle2(sprintf('%d-%d ms', twin(1), twin(2)))
if saveFigs
rd_saveAllFigs(gcf, ...
{sprintf('%s_%s_%d-%dms','svmWeights',decodeAnalStr, twin(1), twin(2))}, 'map', figDir)
end
end
end
%% mean across longest interval, reps, and orientations
twin = [110 315];
tidx = find(times==twin(1)):find(times==twin(2));
nTopChannels = 10;
vals = squeeze(mean(mean(mean(abs(classWeights(:,tidx,:,:)),4),2),3))';
[sortedVals, idx] = sort(vals,'descend');
topChannels = idx(1:nTopChannels);
figure('Position',[250 850 950 450])
subplot(1,3,1)
histogram(vals)
xlabel('Unsigned SVM weight')
ylabel('Count')
subplot(1,3,2)
ssm_plotOnMesh(vals, '', [], data_hdr, '2d');
title('Unsigned SVM weights')
subplot(1,3,3)
ssm_plotOnMesh(double(vals>=sortedVals(nTopChannels)), '', [], data_hdr, '2d');
title(['Channels ' sprintf('%d ',topChannels)])
rd_supertitle2(sprintf('%d-%d ms', twin(1), twin(2)))
if saveFigs
rd_saveAllFigs(gcf, ...
{sprintf('%s_%s_%d-%dms_top%dCh','svmWeights',decodeAnalStr, twin(1), twin(2), nTopChannels)},...
'map', figDir)
end
%% store results
A.classNames = classNames;
A.targetWindows = targetWindow;
A.decodingOps.channels = channels;
A.decodingOps.nTrialsAveraged = nt;
A.decodingOps.binSize = sp;
A.decodingOps.kfold = kfold;
A.decodingOps.svmops = svmops;
A.classTimes = times;
A.classAcc = classAcc;
A.classModel = classModel;
A.classWeights = classWeights;
%% save analysis
if saveAnalysis
save(sprintf('%s_%s_%s.mat',analysisFileName,analStr,decodeAnalStr), 'A')
end