-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdrgCaImAn_pre_per_to_LDA_fsdz.m
463 lines (350 loc) · 16.4 KB
/
drgCaImAn_pre_per_to_LDA_fsdz.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
function handles_out2=drgCaImAn_pre_per_to_LDA_fsdz(pre_perBatchPathName, pre_perFileName, p_threshold, MLalgo, show_figures,no_sp_sm_trials_to_use,first_sp_sm_trial_no)
%
% reads the pre_per file and saves .mat files to process with
% Kording's lab
%
close all
clearvars -except pre_perBatchPathName pre_perFileName p_threshold MLalgo show_figures no_sp_sm_trials_to_use
warning('off')
simulation=0;
classifier_names{1}='Linear Discriminant';
classifier_names{2}='Support Vector Machine';
classifier_names{3}='Naive Bayes Classifier';
classifier_names{4}='Neural Network';
classifier_names{5}='Decision tree';
handles_out2.classifier_names=classifier_names;
if nargin==0
[pre_perFileName,pre_perBatchPathName] = uigetfile({'*pre_per.mat'},'Select the pre_per.mat file');
fprintf(1, ['\ndrgCaImAn_pre_per_to_pydec run for ' pre_perFileName '\n\n']);
p_threshold=0.05;
%Choose the machine learning algorithm
% 1 = linear discriminant analysis
% 2 = SVM
% 3 = Naive Bayes Classifier
% 4 = neural network
% 5 = decision tree
MLalgo=1;
show_figures=1;
end
min_no_trials=10;
figNo=0;
handles_out2.pre_perBatchPathName=pre_perBatchPathName;
handles_out2.pre_perFileName=pre_perFileName;
handles_out2.p_threshold=p_threshold;
handles_out2.MLalgo=MLalgo;
handles_out2.decoding_processed=1;
if ~iscell(pre_perFileName)
load([pre_perBatchPathName pre_perFileName])
if (handles_out.no_sp_trials>=min_no_trials)&(handles_out.no_sm_trials>=min_no_trials)
szSp=size(splus_traces);
szSm=size(sminus_traces);
%time_to_event=([1:szSm(2)]*dt-dt_before);
time_to_eventSm=([1:szSm(2)]*dt-dt_before);
time_to_eventSp=([1:szSp(2)]*dt-dt_before);
handles_out2.time_to_eventSm=time_to_eventSm;
handles_out2.time_to_eventSp=time_to_eventSp;
if show_figures==1
%S+, S-, all snips
CIsm = bootci(1000, @mean, sminus_traces);
meansm=mean(sminus_traces,1);
CIsm(1,:)=meansm-CIsm(1,:);
CIsm(2,:)=CIsm(2,:)-meansm;
CIsp = bootci(1000, @mean, splus_traces);
meansp=mean(splus_traces,1);
CIsp(1,:)=meansp-CIsp(1,:);
CIsp(2,:)=CIsp(2,:)-meansp;
%First plot the average Splus and Sminus
figNo=figNo+1;
try
close(figNo)
catch
end
hFig = figure(figNo);
hold on
pct1=prctile([mean(sminus_traces,1)'; mean(splus_traces(:,1:szSp(2)),1)'],1);
pct99=prctile([mean(sminus_traces,1)'; mean(splus_traces(:,1:szSp(2)),1)'],99);
[hlsm, hpsm] = boundedline(time_to_eventSm',mean(sminus_traces,1)', CIsm', 'b');
[hlsp, hpsp] = boundedline(time_to_eventSp',mean(splus_traces,1)', CIsp', 'r');
%Odor on markers
plot([0 0],[pct1-0.1*(pct99-pct1) pct99+0.1*(pct99-pct1)],'-k')
odorhl=plot([0 mean(delta_odor)],[pct1-0.1*(pct99-pct1) pct1-0.1*(pct99-pct1)],'-k','LineWidth',5);
plot([mean(delta_odor) mean(delta_odor)],[pct1-0.1*(pct99-pct1) pct99+0.1*(pct99-pct1)],'-k')
%Reinforcement markers
plot([mean(delta_odor_on_reinf_on) mean(delta_odor_on_reinf_on)],[pct1-0.1*(pct99-pct1) pct99+0.1*(pct99-pct1)],'-r')
reinfhl=plot([mean(delta_odor_on_reinf_on) mean(delta_odor_on_reinf_on)+mean(delta_reinf)],[pct1-0.1*(pct99-pct1) pct1-0.1*(pct99-pct1)],'-r','LineWidth',5);
plot([mean(delta_odor_on_reinf_on)+mean(delta_reinf) mean(delta_odor_on_reinf_on)+mean(delta_reinf)],[pct1-0.1*(pct99-pct1) pct99+0.1*(pct99-pct1)],'-r')
title("Ca changes aligned to odor onset")
legend([hlsp hlsm odorhl reinfhl],'S+','S-','Odor','Reinforcement')
xlabel('Time (sec)')
ylabel('dF/F')
ylim([pct1-0.2*(pct99-pct1) pct99+0.2*(pct99-pct1)])
xlim([-10 19.8])
end
%Now save the data for python
pffft=1;
time_bins=length(handles_out.time_to_eventSp);
time=time_to_eventSp;
trNo=1;
ii=1;
if no_sp_sm_trials_to_use>handles_out.no_sp_trials
last_sp_trial=handles_out.no_sp_trials;
else
last_sp_trial=no_sp_sm_trials_to_use;
end
if no_sp_sm_trials_to_use>handles_out.no_sm_trials
last_sm_trial=handles_out.no_sm_trials;
else
last_sm_trial=no_sp_sm_trials_to_use;
end
decisions=zeros(1,last_sm_trial+last_sp_trial);
neural_recordings=zeros(last_sm_trial+last_sp_trial,handles_out.no_components,time_bins);
%Save S+
ii=1;
for trialNo=1:last_sp_trial
for traceNo=1:handles_out.no_components
neural_recordings(trialNo,traceNo,:)=splus_traces(ii,:);
ii=ii+1;
end
decisions(trNo)=1;
trNo=trNo+1;
end
%Save S-
ii=1;
for trialNo=1:last_sm_trial
for traceNo=1:handles_out.no_components
neural_recordings(trialNo+last_sp_trial,traceNo,:)=sminus_traces(ii,:);
ii=ii+1;
end
decisions(trNo)=0;
trNo=trNo+1;
end
%Calculate p values for the difference per neuron
no_neurons=handles_out.no_components;
no_trials=trNo-1;
no_timepoints=length(time_to_eventLDA);
p=[];
for ii_neuron=1:handles_out.no_components
these_recordings_sp=zeros(last_sp_trial,no_timepoints);
these_recordings_sp=neural_recordings(logical(decisions),ii_neuron,:);
these_recordings_sm=zeros(last_sm_trial,no_timepoints);
these_recordings_sm=neural_recordings(~logical(decisions),ii_neuron,:);
mean_sp_odor=zeros(last_sp_trial,1);
mean_sp_odor(:,1)=mean(these_recordings_sp(:,time_to_eventLDA>0),2);
mean_sm_odor=zeros(last_sm_trial,1);
mean_sm_odor(:,1)=mean(these_recordings_sm(:,time_to_eventLDA>0),2);
[h,p(ii_neuron)]=ttest2(mean_sp_odor,mean_sm_odor);
end
handles_out2.p=p;
else
handles_out2.decoding_processed=0;
end
else
sp_trials_per_file=[];
sm_trials_per_file=[];
len_time_to_eventSp=[];
len_time_to_eventSm=[];
no_components=[];
for ii_file=1:length(pre_perFileName)
load([pre_perBatchPathName{ii_file} pre_perFileName{ii_file}])
sp_trials_per_file(ii_file)=handles_out.no_sp_trials;
sm_trials_per_file(ii_file)=handles_out.no_sm_trials;
len_time_to_eventSp(ii_file)=length(handles_out.time_to_eventSp);
len_time_to_eventSm(ii_file)=length(handles_out.time_to_eventSm);
no_components(ii_file)=handles_out.no_components;
end
if (max(sp_trials_per_file)<min_no_trials)||(max(sm_trials_per_file)<min_no_trials)
handles_out2.decoding_processed=0;
else
no_sp_trials=max([min_no_trials min(sp_trials_per_file)]);
no_sm_trials=max([min_no_trials min(sm_trials_per_file)]);
no_timepoints=min([min(len_time_to_eventSp) min(len_time_to_eventSm)]);
time=handles_out.time_to_eventSp(1:no_timepoints);
neural_recordings=zeros(no_sp_trials+no_sm_trials,sum(no_components),no_timepoints);
decisions=zeros(1,no_sp_trials+no_sm_trials);
no_neurons=0;
for ii_file=1:length(pre_perFileName)
load([pre_perBatchPathName{ii_file} pre_perFileName{ii_file}])
%Save S+
trNo=1;
ii=1;
for trialNo=1:no_sp_trials
for traceNo=1:handles_out.no_components
neural_recordings(trialNo,traceNo+no_neurons,1:no_timepoints)=splus_traces(ii,1:no_timepoints);
ii=ii+1;
end
decisions(trNo)=1;
trNo=trNo+1;
end
%Save S-
ii=1;
for trialNo=1:no_sm_trials
for traceNo=1:handles_out.no_components
neural_recordings(trialNo+no_sp_trials,traceNo+no_neurons,1:no_timepoints)=sminus_traces(ii,1:no_timepoints);
ii=ii+1;
end
decisions(trNo)=0;
trNo=trNo+1;
end
no_neurons=no_neurons+handles_out.no_components;
end
%Calculate p values for the difference per neuron
p=[];
for ii_neuron=1:no_neurons
these_recordings_sp=zeros(no_sp_trials,no_timepoints);
these_recordings_sp=neural_recordings(logical(decisions),ii_neuron,:);
these_recordings_sm=zeros(no_sm_trials,no_timepoints);
these_recordings_sm=neural_recordings(~logical(decisions),ii_neuron,:);
mean_sp_odor=zeros(no_sp_trials,1);
mean_sp_odor(:,1)=mean(these_recordings_sp(:,time>0),2);
mean_sm_odor=zeros(no_sm_trials,1);
mean_sm_odor(:,1)=mean(these_recordings_sm(:,time>0),2);
[h,p(ii_neuron)]=ttest2(mean_sp_odor,mean_sm_odor);
end
handles_out2.p=p;
handles_out2.time=time;
no_trials=trNo-1;
end
end
if handles_out2.decoding_processed==1
if simulation==1
%This is here to troubleshoot the algorithm
PathName='/Users/restrepd/Documents/Projects/SFTP/Artigo_HomeOdor/AmylAcetateAcetophenone/';
load([PathName 'simulated_data.mat'])
end
Nall=length(decisions);
accuracy=zeros(1,no_timepoints);
sh_accuracy=zeros(1,no_timepoints);
%These are used to calculate the z values
%I use time<0
mean_per_neuron=zeros(1,no_neurons);
STD_per_neuron=zeros(1,no_neurons);
for ii_neurons=1:no_neurons
all_pre=[];
for ii_trials=1:no_trials
these_pre=zeros(1,sum(time_to_eventLDA<0));
these_pre(1,:)=neural_recordings(ii_trials,ii_neuron,time_to_eventLDA<0);
all_pre=[all_pre these_pre];
end
mean_per_neuron(ii_neurons)=mean(all_pre);
STD_per_neuron(ii_neurons)=std(all_pre);
end
z_neural_recordings=zeros(Nall,handles_out.no_components,no_timepoints);
for ii_neurons=1:no_neurons
z_neural_recordings(:,ii_neurons,:)=(neural_recordings(:,ii_neurons,:)-mean_per_neuron(ii_neurons))/STD_per_neuron(ii_neurons);
end
if sum(p<=p_threshold)>0
gcp;
handles_out2.decoding_processed=1;
for time_point=1:no_timepoints
%dFF per trial per component
measurements=zeros(Nall,sum(p<p_threshold));
measurements(:,:)=z_neural_recordings(:,p<p_threshold,time_point);
%Now calculate the z value
scores=[];
trials_processed=[];
correct_predict=[];
correct_predict_shuffled=[];
lda_no_trials=[];
parfor ii=1:Nall
%Partition the data into training and test sets.
%Create input and target vectors leaving one trial out
%For per_input each column has the dF/F for one trial
%each row is a single time point for dF/F for one of the cells
%For per_target the top row is 1 if the odor is S+ and 0 if it is
%S-, and row 2 has 1 for S-
idxTrn=ones(Nall,1);
idxTrn(ii)=0;
idxTest=zeros(Nall,1);
idxTest(ii)=1;
%Store the training data in a table.
tblTrn=[];
tblTrn = array2table(measurements(logical(idxTrn),:));
%Store the decisions in Y
Y=decisions(logical(idxTrn));
%Train a discriminant analysis model using the training set and default options.
%By default this is a regularized linear discriminant analysis (LDA)
switch MLalgo
case 1
Mdl = fitcdiscr(tblTrn,Y);
case 2
Mdl = fitcsvm(tblTrn,Y);
case 3
Mdl = fitcnb(tblTrn,Y);
case 4
Mdl = fitcnet(tblTrn,Y);
case 5
Mdl = fitctree(tblTrn,Y);
end
%Predict labels for the test set. You trained Mdl using a table of data, but you can predict labels using a matrix.
[label,score] = predict(Mdl,measurements(logical(idxTest),:));
%label is the predicted label, and score is the predicted class
%posterior probability
if label==decisions(ii)
correct_predict(ii)=1;
else
correct_predict(ii)=0;
end
ii_shuffled=randperm(Nall);
if label==decisions(ii_shuffled(ii))
correct_predict_shuffled(ii)=1;
else
correct_predict_shuffled(ii)=0;
end
end
accuracy(time_point)=mean(correct_predict);
sh_accuracy(time_point)=mean(correct_predict_shuffled);
if show_figures==1
fprintf(1, ['For timepoint %d accuracy= %d and shuffled accuracy= %d\n'],time_point,accuracy(time_point),sh_accuracy(time_point));
end
end
else
handles_out2.decoding_processed=0;
end
handles_out2.accuracy=accuracy;
handles_out2.sh_accuracy=sh_accuracy;
handles_out2.mean_accuracy=mean(accuracy(time>=0));
handles_out2.mean_sh_accuracy=mean(sh_accuracy(time>=0));
handles_out2.delta_odor=mean(delta_odor);
handles_out2.delta_odor_on_reinf_on=mean(delta_odor_on_reinf_on);
handles_out2.delta_reinf=mean(delta_reinf);
if show_figures==1
figNo=figNo+1;
try
close(figNo)
catch
end
figure(figNo)
% subplot(1,2,1)
hold on
%Plot the bounded line for the 5 percentile for the shuffled trials
per95=prctile(sh_accuracy(1,:),95);
per5=prctile(sh_accuracy(1,:),5);
CIsh=[mean(sh_accuracy)-per5 per95-mean(sh_accuracy)]';
[hlCR, hpCR] = boundedline([time_to_eventLDA(1) time_to_eventLDA(end)],[mean(sh_accuracy) mean(sh_accuracy)], CIsh', 'r');
%Plot the accuracy
plot(time',accuracy,'-k')
%Now plot the bootstrapped confidence intervals for the original and
%For the accuracy use odor 0 to the end
CI = bootci(1000, {@mean, accuracy(time>=0)},'type','cper');
bar_offset=max(time)+1;
plot([bar_offset bar_offset],CI,'-k','LineWidth',3)
plot([bar_offset],mean(accuracy(time>=0)),'ok')
%For the shuffled accuracy use all points
CI = bootci(1000, {@mean, sh_accuracy},'type','cper');
bar_offset=max(time)+1;
plot([bar_offset bar_offset],CI,'-r','LineWidth',3)
plot([bar_offset],mean(sh_accuracy),'ok')
%Odor on markers
plot([0 0],[0 1.1],'-k')
odorhl=plot([0 mean(delta_odor)],[0.32 0.32],'-k','LineWidth',5);
plot([mean(delta_odor) mean(delta_odor)],[0 1.10],'-k')
%Reinforcement markers
plot([mean(delta_odor_on_reinf_on) mean(delta_odor_on_reinf_on)],[0 1.10],'-r')
reinfhl=plot([mean(delta_odor_on_reinf_on) mean(delta_odor_on_reinf_on)+mean(delta_reinf)],[.32 .32],'-r','LineWidth',5);
plot([mean(delta_odor_on_reinf_on)+mean(delta_reinf) mean(delta_odor_on_reinf_on)+mean(delta_reinf)],[0 1.10],'-r')
ylim([00 1.10])
xlabel('Time (sec)')
ylabel('Accuracy')
end
end
pffft=1;