forked from restrepd-zz/CaImAnDR
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdrgCaImAn_analyze_SVZ_entire_session_shuffling.m
executable file
·805 lines (558 loc) · 26 KB
/
drgCaImAn_analyze_SVZ_entire_session_shuffling.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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
function handles_out2=drgCaImAn_analyze_SVZ_entire_session_shuffling(handles_choices)
%This program trains several decoding algorithms with the post odorant and then determines what happens throughout the entire timecouse
%The user enters the choices entered under exist('handles_choices')==0
%
% processing_algorithm= 1 and 2 were used for troublehsooting and do not
% produce reliable results because of overtraining, use
% processing_algoritm=3, that was vetted for our manuscript
%
%
% the input is a pre_per file version 2
if exist('handles_choices')==0
clear all
close all
%Load file
[pre_perFileName,pre_perPathName] = uigetfile({'*pre_per_dec2.mat'},'Select the .m file with all the choices for analysis');
% processing_algorithm=3; %Use 3
% k_fold=5; %Only used for processing_algorithm=2,
% post_time=5; %The decoding model will be trained with all points in post_time sec interval starting post_shift secs after odor on
% post_shift=0; %Set to 0 if you want to train with odor on points
% pre_time=5; %Used to calculate the decoding accuracy pre_time sec before post_shift
iiMLalgo=6; %Vector with the decoding algorithms you want to use
% ii_cost=3;
p_threshold=1.1; %This limits the ROIs used in the decoding model to those whose p value in a ttest for the two odors in <=p_threshold
% dt_p_threshold=20; %Time to be used after the odor on for the p_threshold t_test
show_figures=1; %Show the figures
else
pre_perPathName=handles_choices.pre_per_PathName;
pre_perFileName=handles_choices.pre_per_FileName;
processing_algorithm=handles_choices.processing_algorithm;
post_time=handles_choices.post_time;
k_fold=handles_choices.k_fold;
post_shift=handles_choices.post_shift;
MLalgo_to_use=handles_choices.MLalgo_to_use;
pre_time=handles_choices.pre_time;
p_threshold=handles_choices.p_threshold;
dt_p_threshold=handles_choices.dt_p_threshold;
show_figures=handles_choices.show_figures;
ii_cost=handles_choices.ii_cost;
end
warning('off')
figNo=0;
% convert_z=1; %Convert dFF traces to z
% dt_span=40; %Seconds shown before and after odor on in the figures
% moving_mean_n=30; %Points used to calculate moving mean for the prediction label figure
% no_shuffles=10; %Number of shuffles for per trial shuffling
load([pre_perPathName pre_perFileName])
fprintf(1, ['\ndrgCaImAn_analyze_SVZ_entire_session_shuffling run for ' pre_perFileName '\n\n']);
% fprintf(1, 'post_time = %d, p_threshold= %d, post_shift= %d, cost %d\n',post_time,p_threshold,post_shift, ii_cost);
% if convert_z==1
% for trace_no=1:size(traces,1)
% traces(trace_no,:)=traces(trace_no,:)/std(traces(trace_no,:));
% end
% end
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';
classifier_names{6}='Binomial glm';
delta_odor=4.127634e+00;
delta_odor_on_reinf_on=4.415787e+00;
delta_reinf=4.078266e-01;
%Plot the mean timecourse for S+ and S-, Euclidean distance and KL
%divergence
ii_thr=find(handles_out.handles.p_threshold==p_threshold);
meandFFsp=handles_out.ii_out(ii_thr).handles_out.meandFFsp;
meandFFsm=handles_out.ii_out(ii_thr).handles_out.meandFFsm;
time_span=handles_out.ii_out(ii_thr).handles_out.time_span;
dist_euclid=handles_out.ii_out(ii_thr).handles_out.dist_euclid-handles_out.ii_out(ii_thr).handles_out.dist_euclid_zero;
KLdivergence=handles_out.ii_out(ii_thr).handles_out.KLdivergence;
if show_figures==1
figNo=figNo+1;
try
close(figNo)
catch
end
hFig = figure(figNo);
set(hFig, 'units','normalized','position',[.05 .1 .3 .6])
subplot(3,1,1)
hold on
%S+ mean dFF
plot(time_span,meandFFsp, '-' ,'Color',[80/255 194/255 255/255]);
%S- mean dFF
plot(time_span,meandFFsm, '-' ,'Color',[238/255 111/255 179/255]);
text(30,0.75,'S-','Color',[80/255 194/255 255/255])
text(30,0.65,'S+','Color',[0 114/255 178/255])
title(['Mean dFF'])
xlabel('Time(sec)')
ylabel('dFF')
subplot(3,1,2)
hold on
%Euclidean distance
plot(time_span,dist_euclid, 'Color',[238/255 111/255 179/255]);
title(['Euclidean distance'])
xlabel('Time(sec)')
ylabel('Distance')
subplot(3,1,3)
hold on
%KL divergence
plot(time_span',KLdivergence', 'Color',[238/255 111/255 179/255]);
title(['KL divergence'])
xlabel('Time(sec)')
ylabel('KL divergence')
end
accuracy_tr=handles_out.ii_out(ii_thr).handles_out.MLalgo(iiMLalgo).accuracy_tr;
accuracy_tr_wta=handles_out.ii_out(ii_thr).handles_out.MLalgo(iiMLalgo).accuracy_tr_wta;
accuracy_tr_sh=handles_out.ii_out(ii_thr).handles_out.MLalgo(iiMLalgo).accuracy_tr_sh;
accuracy_tr_wta_sh=handles_out.ii_out(ii_thr).handles_out.MLalgo(iiMLalgo).accuracy_tr_wta_sh;
accuracy_tr_sh2=handles_out.ii_out(ii_thr).handles_out.MLalgo(iiMLalgo).accuracy_tr_sh2;
accuracy_tr_wta_sh2=handles_out.ii_out(ii_thr).handles_out.MLalgo(iiMLalgo).accuracy_tr_wta_sh2;
fprintf(1, ['Training accuracy for ' classifier_names{iiMLalgo} ' is %d, wta accuracy is %d\n']...
,accuracy_tr,accuracy_tr_wta);
fprintf(1, ['Shuffled training accuracy for ' classifier_names{iiMLalgo} ' is %d, wta accuracy is %d\n']...
,accuracy_tr_sh,accuracy_tr_wta_sh);
fprintf(1, ['Training accuracy for shuffled per trial for ' classifier_names{iiMLalgo} ' is %d, wta accuracy is %d\n']...
,accuracy_tr_sh2,accuracy_tr_wta_sh2);
fprintf(1, ['Percent correct for behavior is %d\n'],...
handles_out.ii_out(1).handles_out.percent_correct);
%Plot the timecourses for accuracy and label
per_trial_sp_timecourse=handles_out.ii_out(ii_thr).handles_out.MLalgo(iiMLalgo).per_trial_sp_timecourse;
per_trial_sm_timecourse=handles_out.ii_out(ii_thr).handles_out.MLalgo(iiMLalgo).per_trial_sm_timecourse;
this_correct_predict=handles_out.ii_out(ii_thr).handles_out.MLalgo(iiMLalgo).this_correct_predict;
this_correct_predict_sh=handles_out.ii_out(ii_thr).handles_out.MLalgo(iiMLalgo).this_correct_predict_sh;
per_trial_scores_sp=handles_out.ii_out(ii_thr).handles_out.MLalgo(iiMLalgo).per_trial_scores_sp;
per_trial_scores_sm=handles_out.ii_out(ii_thr).handles_out.MLalgo(iiMLalgo).per_trial_scores_sm;
this_moving_mean_n=10;
moving_mean_n=this_moving_mean_n;
moving_mean_per_trial_sp_timecourse = movmean(per_trial_sp_timecourse',this_moving_mean_n)';
moving_mean_per_trial_sm_timecourse = movmean(per_trial_sm_timecourse',this_moving_mean_n)';
if show_figures==1
%Plot the prediction for S+ and S- and the accuracy
figNo=figNo+1;
try
close(figNo)
catch
end
hFig = figure(figNo);
set(hFig, 'units','normalized','position',[.05 .1 .4 .4])
hold on
CIsm = bootci(1000, @mean, moving_mean_per_trial_sm_timecourse);
meansm=mean(moving_mean_per_trial_sm_timecourse,1);
CIsm(1,:)=meansm-CIsm(1,:);
CIsm(2,:)=CIsm(2,:)-meansm;
[hlsm, hpsm] = boundedline(time_span',mean(moving_mean_per_trial_sm_timecourse,1)', CIsm', 'cmap',[158/255 31/255 99/255]);
CIsp = bootci(1000, @mean, moving_mean_per_trial_sp_timecourse);
meansp=mean(moving_mean_per_trial_sp_timecourse,1);
CIsp(1,:)=meansp-CIsp(1,:);
CIsp(2,:)=CIsp(2,:)-meansp;
[hlsp, hpsp] = boundedline(time_span',mean(moving_mean_per_trial_sp_timecourse,1)', CIsp', 'cmap',[0 114/255 178/255]);
plot(time_span',mean(moving_mean_per_trial_sm_timecourse,1)','-','Color',[158/255 31/255 99/255],'DisplayName','S-')
plot(time_span',mean(moving_mean_per_trial_sp_timecourse,1)', '-','Color',[0 114/255 178/255],'DisplayName','S+');
text(30,0.75,'S-','Color',[158/255 31/255 99/255])
text(30,0.85,'S+','Color',[0 114/255 178/255])
ylim([0 1.1])
xlim([-7 15])
title(['Label prediction per trial for ' classifier_names{iiMLalgo} ' and p threshold ' num2str(p_threshold)])
xlabel('Time(sec)')
ylabel('Label prediction, S+=1, S-=0')
figNo=figNo+1;
try
close(figNo)
catch
end
hFig = figure(figNo);
set(hFig, 'units','normalized','position',[.05 .1 .4 .7])
hold on
y_shift=0;
for ii=1:size(moving_mean_per_trial_sm_timecourse,1)
plot(time_span',moving_mean_per_trial_sm_timecourse(ii,:)+y_shift,'Color',[158/255 31/255 99/255]);
y_shift=y_shift+1.5;
end
for ii=1:size(moving_mean_per_trial_sp_timecourse,1)
plot(time_span',moving_mean_per_trial_sp_timecourse(ii,:)+y_shift,'Color',[0 114/255 178/255]);
y_shift=y_shift+1.5;
end
plot(time_span',mean(this_correct_predict_sh,1)','-k','DisplayName','Shuffled')
plot(time_span',mean(this_correct_predict,1)', '-','Color',[0 114/255 178/255]);
text(30,0.75,'Shuffled','Color','k')
text(30,0.85,'S+ vs S-','Color',[0 114/255 178/255])
xlim([-7 15])
title(['Prediction for each trial for ' classifier_names{iiMLalgo}])
xlabel('Time(sec)')
ylabel('Prediction')
figNo=figNo+1;
try
close(figNo)
catch
end
hFig = figure(figNo);
set(hFig, 'units','normalized','position',[.05 .1 .4 .4])
hold on
CIsm = bootci(1000, @mean, this_correct_predict_sh);
meansm=mean(this_correct_predict_sh,1);
CIsm(1,:)=meansm-CIsm(1,:);
CIsm(2,:)=CIsm(2,:)-meansm;
[hlsm, hpsm] = boundedline(time_span',mean(this_correct_predict_sh,1)', CIsm', 'k');
CIsm = bootci(1000, @mean, this_correct_predict);
meansm=mean(this_correct_predict,1);
CIsm(1,:)=meansm-CIsm(1,:);
CIsm(2,:)=CIsm(2,:)-meansm;
[hlsm, hpsm] = boundedline(time_span',mean(this_correct_predict,1)', CIsm', 'cmap',[0 114/255 178/255]);
plot(time_span',mean(this_correct_predict_sh,1)','-k','DisplayName','Shuffled')
plot(time_span',mean(this_correct_predict,1)', '-','Color',[0 114/255 178/255]);
text(30,0.75,'Shuffled','Color','k')
text(30,0.85,'S+ vs S-','Color',[0 114/255 178/255])
ylim([0 1.1])
this_ylim=ylim;
%Place epoch markers
%FV
rectangle(Position=[-1.5,this_ylim(1)+0.1*(this_ylim(2)-this_ylim(1)),1.5,0.03*(this_ylim(2)-this_ylim(1))], FaceColor=[0.9 0.9 0.9], EdgeColor=[0.9 0.9 0.9])
plot([-1.5 -1.5],[this_ylim],'-','Color',[0.5 0.5 0.5])
%Odor
rectangle(Position=[0,this_ylim(1)+0.1*(this_ylim(2)-this_ylim(1)),mean(delta_odor),0.03*(this_ylim(2)-this_ylim(1))], FaceColor=[0 0 0], EdgeColor=[0 0 0])
plot([0 0],[this_ylim],'-k')
plot([mean(delta_odor) mean(delta_odor)],[this_ylim],'-k')
%Reinforcement
rectangle(Position=[mean(delta_odor_on_reinf_on),this_ylim(1)+0.1*(this_ylim(2)-this_ylim(1)),mean(delta_reinf),0.03*(this_ylim(2)-this_ylim(1))], FaceColor=[1 0 0], EdgeColor=[1 0 0])
plot([mean(delta_odor_on_reinf_on)+mean(delta_reinf) mean(delta_odor_on_reinf_on)+mean(delta_reinf)],[this_ylim],'-r')
plot([mean(delta_odor_on_reinf_on) mean(delta_odor_on_reinf_on)],[this_ylim],'-r')
xlim([-7 15])
title(['Accuracy for ' classifier_names{iiMLalgo}])
xlabel('Time(sec)')
ylabel('Accuracy')
odor_window=[2 4.1]; %Note: This is the time window I use in drgCaImAn_analyze_batch_pre_per_to_decode_per_mouse_v2
odor_acc=mean(meansm((time_span>=odor_window(1))&(time_span<=odor_window(2))));
fprintf(1, ['Accuracy for odor window is %d\n'],...
odor_acc);
%Plot the posterior probabilities for Sp (scores,:,2) and Sm (scores(:,1))
figNo=figNo+1;
try
close(figNo)
catch
end
hFig = figure(figNo);
set(hFig, 'units','normalized','position',[.05 .1 .3 .3])
subplot(2,1,1)
hold on
these_scores_sm=zeros(size(per_trial_scores_sp,1),size(per_trial_scores_sp,3));
these_scores_sm(:,:)=per_trial_scores_sp(:,1,:);
CIsm = bootci(1000, @mean, these_scores_sm);
meansm=mean(these_scores_sm,1);
CIsm(1,:)=meansm-CIsm(1,:);
CIsm(2,:)=CIsm(2,:)-meansm;
[hlsm, hpsm] = boundedline(time_span',mean(these_scores_sm,1)', CIsm', 'cmap',[158/255 31/255 99/255]);
these_scores_sp=zeros(size(per_trial_scores_sp,1),size(per_trial_scores_sp,3));
these_scores_sp(:,:)=per_trial_scores_sp(:,2,:);
CIsp = bootci(1000, @mean, these_scores_sp);
meansp=mean(these_scores_sp,1);
CIsp(1,:)=meansp-CIsp(1,:);
CIsp(2,:)=CIsp(2,:)-meansp;
[hlsp, hpsp] = boundedline(time_span',mean(these_scores_sp,1)', CIsp', 'cmap',[0 114/255 178/255]);
plot(time_span',mean(these_scores_sm,1)','-','Color',[158/255 31/255 99/255],'DisplayName','S-')
plot(time_span',mean(these_scores_sp,1)', '-','Color',[0 114/255 178/255],'DisplayName','S+');
text(30,0.75,'S-','Color',[158/255 31/255 99/255])
text(30,0.65,'S+','Color',[0 114/255 178/255])
ylim([0 1])
title(['Posterior probability for S+ or S- prediction for S+ trials ' classifier_names{iiMLalgo} ' ' num2str(p_threshold)])
xlabel('Time(sec)')
ylabel('Posterior probability')
subplot(2,1,2)
hold on
these_scores_sm=zeros(size(per_trial_scores_sm,1),size(per_trial_scores_sm,3));
these_scores_sm(:,:)=per_trial_scores_sm(:,1,:);
CIsm = bootci(1000, @mean, these_scores_sm);
meansm=mean(these_scores_sm,1);
CIsm(1,:)=meansm-CIsm(1,:);
CIsm(2,:)=CIsm(2,:)-meansm;
[hlsm, hpsm] = boundedline(time_span',mean(these_scores_sm,1)', CIsm', 'cmap',[158/255 31/255 99/255]);
these_scores_sp=zeros(size(per_trial_scores_sm,1),size(per_trial_scores_sm,3));
these_scores_sp(:,:)=per_trial_scores_sm(:,2,:);
CIsp = bootci(1000, @mean, these_scores_sp);
meansp=mean(these_scores_sp,1);
CIsp(1,:)=meansp-CIsp(1,:);
CIsp(2,:)=CIsp(2,:)-meansp;
[hlsp, hpsp] = boundedline(time_span',mean(these_scores_sp,1)', CIsp', 'cmap',[0 114/255 178/255]);
plot(time_span',mean(these_scores_sm,1)','-','Color',[158/255 31/255 99/255],'DisplayName','S-')
plot(time_span',mean(these_scores_sp,1)', '-','Color',[0 114/255 178/255],'DisplayName','S+');
text(30,0.75,'S-','Color',[158/255 31/255 99/255])
text(30,0.65,'S+','Color',[0 114/255 178/255])
ylim([0 1])
title(['Posterior probability for S+ or S- prediction for S- trials ' classifier_names{iiMLalgo} ' ' num2str(p_threshold)])
xlabel('Time(sec)')
ylabel('Posterior probability')
end
%Now let's take a look at the entire session, is the animal musing about odorants?!
label_traces=handles_out.ii_out(ii_thr).handles_out.MLalgo(iiMLalgo).label_traces;
label_traces_sh=handles_out.ii_out(ii_thr).handles_out.MLalgo(iiMLalgo).label_traces_sh;
label_traces_sh2=handles_out.ii_out(ii_thr).handles_out.MLalgo(iiMLalgo).label_traces_sh2;
% label_traces_sh=handles_out.ii_out(ii_thr).handles_out.MLalgo(iiMLalgo).label_traces_sh;
moving_mean_label_traces = movmean(label_traces,moving_mean_n);
moving_mean_label_traces_sh2 = movmean(label_traces_sh2,moving_mean_n);
moving_mean_label_traces_sh = movmean(label_traces_sh,moving_mean_n);
time=handles_out.ii_out(ii_thr).handles_out.time;
sp_times=handles_out.ii_out(ii_thr).handles_out.sp_times;
sm_times=handles_out.ii_out(ii_thr).handles_out.sm_times;
if show_figures==1
figNo=figNo+1;
try
close(figNo)
catch
end
hFig = figure(figNo);
set(hFig, 'units','normalized','position',[.05 .1 .85 .6])
subplot(3,1,1)
hold on
% plot(time,moving_mean_label_traces_sh,'-','Color',[80/255 194/255 255/255])
plot(time,moving_mean_label_traces,'-k','LineWidth',1)
for ii=1:length(sp_times)
plot([sp_times(ii) sp_times(ii)],[0 1],'-r')
end
for ii=1:length(sm_times)
plot([sm_times(ii) sm_times(ii)],[0 1],'-b')
end
ylim([-0.2 1.2])
title('original data')
subplot(3,1,2)
hold on
plot(time,moving_mean_label_traces_sh,'-m','LineWidth',1)
for ii=1:length(sp_times)
plot([sp_times(ii) sp_times(ii)],[0 1],'-r')
end
for ii=1:length(sm_times)
plot([sm_times(ii) sm_times(ii)],[0 1],'-b')
end
ylim([-0.2 1.2])
title('shuffled')
subplot(3,1,3)
hold on
for ii=1:length(sp_times)
plot([sp_times(ii) sp_times(ii)],[0 1],'-r')
end
for ii=1:length(sm_times)
plot([sm_times(ii) sm_times(ii)],[0 1],'-b')
end
ylim([-0.2 1.2])
title('trials')
sgtitle(['Label prediction for entire session for ' classifier_names{iiMLalgo} ' and p threshold ' num2str(p_threshold)])
%Now find the changes in prediction that take place between trials
per95=prctile(moving_mean_label_traces_sh2(:),95);
per5=prctile(moving_mean_label_traces_sh2(:),5);
all_trial_times=[sp_times sm_times];
all_trial_times=sortrows(all_trial_times');
window_dt=[-7 15];
spontaneous_trial_times=[];
spontaneous_delta_prediction=[];
before_spontaneous_spm=[];
after_spontaneous_spm=[];
time_before=[];
dt_from_before=[];
ii_spontaneous=0;
pre_time=2;
for ii_trial=1:2:length(all_trial_times)-2
%find the next trial
this_ii=find(time>all_trial_times(ii_trial)+delta_odor,1,'first');
if ~isempty(this_ii)
%Now find the first baseline period
delta_ii=find(moving_mean_label_traces(this_ii:end) < per5,1,'first');
if ~isempty(delta_ii)
if time(this_ii+delta_ii-1)<all_trial_times(ii_trial+2)
%Find the increase
delta_ii2=find(moving_mean_label_traces(this_ii+delta_ii-1:end) > per95,1,'first');
if time(this_ii+delta_ii-1+delta_ii2-2)<all_trial_times(ii_trial+2)-pre_time
ii_spontaneous=ii_spontaneous+1;
spontaneous_trial_times(ii_spontaneous)=time(this_ii+delta_ii-1+delta_ii2-2);
time_before(ii_spontaneous)=all_trial_times(ii_trial);
dt_from_before(ii_spontaneous)=spontaneous_trial_times(ii_spontaneous)-time_before(ii_spontaneous);
if sum(sp_times==time_before(ii_spontaneous))>0
before_spontaneous_spm(ii_spontaneous)=1;
else
before_spontaneous_spm(ii_spontaneous)=0;
end
if ii_trial+2<=length(all_trial_times)
if sum(sp_times==all_trial_times(ii_trial+2))>0
after_spontaneous_spm(ii_spontaneous)=1;
else
after_spontaneous_spm(ii_spontaneous)=0;
end
else
after_spontaneous_spm(ii_spontaneous)=NaN;
end
spontaneous_delta_prediction(ii_spontaneous,:)=moving_mean_label_traces((time>=-7+spontaneous_trial_times(ii_spontaneous))&(time<=15+spontaneous_trial_times(ii_spontaneous)));
pffft=1;
end
end
end
end
end
%Plot label predictions alone
figNo=figNo+1;
try
close(figNo)
catch
end
hFig = figure(figNo);
set(hFig, 'units','normalized','position',[.05 .1 .85 .3])
hold on
% CIsm = bootci(1000, @mean, moving_mean_label_traces_sh2);
% meansm=mean(moving_mean_label_traces_sh2,1);
% CIsm(1,:)=meansm-CIsm(1,:);
% CIsm(2,:)=CIsm(2,:)-meansm;
%
% %S- Proficient
% [hlsm, hpsm] = boundedline(time',mean(moving_mean_label_traces_sh2,1)', CIsm', 'cmap',[80/255 194/255 255/255]);
%
CIsh=[mean(moving_mean_label_traces_sh2(:))-per5 per95-mean(moving_mean_label_traces_sh2(:))]';
[hlCR, hpCR] = boundedline([time(1) time(end)],[mean(moving_mean_label_traces_sh2(:)) mean(moving_mean_label_traces_sh2(:))], CIsh', 'cmap',[80/255 194/255 255/255]);
% plot(time,moving_mean_label_traces_sh,'-','Color',[80/255 194/255 255/255])
plot(time,moving_mean_label_traces,'-k','LineWidth',1)
for ii=1:length(sp_times)
plot([sp_times(ii) sp_times(ii)],[0 1],'-r')
end
for ii=1:length(sm_times)
plot([sm_times(ii) sm_times(ii)],[0 1],'-b')
end
% plot(time,moving_mean_label_traces_sh(1,:),'-b')
ylim([-0.2 1.2])
title(['Label prediction for entire session for ' classifier_names{iiMLalgo} ' and p threshold ' num2str(p_threshold)])
%Plot the spontaneous between trial dFF
figNo=figNo+1;
try
close(figNo)
catch
end
hFig = figure(figNo);
set(hFig, 'units','normalized','position',[.05 .1 .85 .3])
hold on
% CIsm = bootci(1000, @mean, moving_mean_label_traces_sh2);
% meansm=mean(moving_mean_label_traces_sh2,1);
% CIsm(1,:)=meansm-CIsm(1,:);
% CIsm(2,:)=CIsm(2,:)-meansm;
%
% %S- Proficient
% [hlsm, hpsm] = boundedline(time',mean(moving_mean_label_traces_sh2,1)', CIsm', 'cmap',[80/255 194/255 255/255]);
%
dt=time(2)-time(1);
this_time_span=[-7:dt:15];
this_time_span=this_time_span(1:size(spontaneous_delta_prediction,2));
hold on
CIsm = bootci(1000, @mean, spontaneous_delta_prediction);
meansm=mean(spontaneous_delta_prediction,1);
CIsm(1,:)=meansm-CIsm(1,:);
CIsm(2,:)=CIsm(2,:)-meansm;
[hlsm, hpsm] = boundedline(this_time_span',mean(spontaneous_delta_prediction,1)', CIsm', 'cmap',[0/255 0/255 0/255]);
for ii=1:size(spontaneous_delta_prediction,1)
plot(this_time_span',spontaneous_delta_prediction(ii,:), 'Color',[150/255 150/255 150/255])
end
text(30,0.75,'S-','Color',[158/255 31/255 99/255])
text(30,0.85,'S+','Color',[0 114/255 178/255])
ylim([0 1.1])
xlim([-7 15])
title(['Label prediction spontaneous between trials for ' classifier_names{iiMLalgo} ' and p threshold ' num2str(p_threshold)])
xlabel('Time(sec)')
ylabel('Label prediction')
figNo=figNo+1;
try
close(figNo)
catch
end
hFig = figure(figNo);
set(hFig, 'units','normalized','position',[.05 .1 .3 .3])
hold on
bar_offset=1;
bar(bar_offset,sum(label_traces==0),'k')
bar_offset=bar_offset+1;
bar(bar_offset,sum(label_traces_sh==0),'m')
bar_offset=bar_offset+2;
bar(bar_offset,sum(label_traces==1),'k')
bar_offset=bar_offset+1;
bar(bar_offset,sum(label_traces_sh==1),'m')
xticks([1 2 4 5])
xticklabels({'S-', 'S- sh', 'S+','S+ sh'})
ylabel('Number of timepoints')
%Now do a Chi Squared test
[p, Q]= chi2test([sum(label_traces==1), sum(label_traces==0); sum(label_traces_sh==1), sum(label_traces_sh==0)]);
title(['Histogram for labels, Chi squared p value = ' num2str(p)])
%Histogram for all points
figNo=figNo+1;
try
close(figNo)
catch
end
hFig = figure(figNo);
set(hFig, 'units','normalized','position',[.05 .1 .3 .3])
hold on
edges=[0:0.1:1];
histogram(moving_mean_label_traces,edges)
hold on
histogram(moving_mean_label_traces_sh,edges)
title(['Histogram for labels ' ])
end
prior_p_sm=handles_out.ii_out(ii_thr).handles_out.MLalgo(iiMLalgo).scores(:,1);
prior_p_sp=handles_out.ii_out(ii_thr).handles_out.MLalgo(iiMLalgo).scores(:,2);
prior_p_sm_sh=handles_out.ii_out(ii_thr).handles_out.MLalgo(iiMLalgo).scores_sh(:,1);
prior_p_sp_sh=handles_out.ii_out(ii_thr).handles_out.MLalgo(iiMLalgo).scores_sh(:,2);
prior_p_sm_sh2=zeros(1,length(time));
prior_p_sp_sh2=zeros(1,length(time));
prior_p_sm_sh2(1,:)=mean(handles_out.ii_out(ii_thr).handles_out.MLalgo(iiMLalgo).scores_sh2(:,:,1),1);
prior_p_sp_sh2(1,:)=mean(handles_out.ii_out(ii_thr).handles_out.MLalgo(iiMLalgo).scores_sh2(:,:,2),1);
if show_figures==1
figNo=figNo+1;
try
close(figNo)
catch
end
hFig = figure(figNo);
set(hFig, 'units','normalized','position',[.05 .1 .85 .6])
subplot(3,1,1)
hold on
% plot(time,moving_mean_label_traces_sh,'-','Color',[80/255 194/255 255/255])
plot(time,prior_p_sp,'-r','LineWidth',1)
plot(time,prior_p_sm,'-b','LineWidth',1)
for ii=1:length(sp_times)
plot([sp_times(ii) sp_times(ii)],[0 1],'-r')
end
for ii=1:length(sm_times)
plot([sm_times(ii) sm_times(ii)],[0 1],'-b')
end
ylim([-0.2 1.2])
xlabel('Time(sec)')
ylabel('p')
title('original data')
subplot(3,1,2)
hold on
% plot(time,moving_mean_label_traces_sh,'-','Color',[80/255 194/255 255/255])
plot(time,prior_p_sp_sh,'-r','LineWidth',1)
plot(time,prior_p_sm_sh,'-b','LineWidth',1)
for ii=1:length(sp_times)
plot([sp_times(ii) sp_times(ii)],[0 1],'-r')
end
for ii=1:length(sm_times)
plot([sm_times(ii) sm_times(ii)],[0 1],'-b')
end
ylim([-0.2 1.2])
xlabel('Time(sec)')
ylabel('p')
title('shuffled')
subplot(3,1,3)
hold on
% plot(time,moving_mean_label_traces_sh,'-','Color',[80/255 194/255 255/255])
plot(time,prior_p_sp_sh2,'-r','LineWidth',1)
plot(time,prior_p_sm_sh2,'-b','LineWidth',1)
for ii=1:length(sp_times)
plot([sp_times(ii) sp_times(ii)],[0 1],'-r')
end
for ii=1:length(sm_times)
plot([sm_times(ii) sm_times(ii)],[0 1],'-b')
end
ylim([-0.2 1.2])
xlabel('Time(sec)')
ylabel('p')
title('shuffled 2')
sgtitle(['Prior probability for ' classifier_names{iiMLalgo} ' and p threshold ' num2str(p_threshold)])
[h,p]=vartest2(prior_p_sp,prior_p_sp_sh);
fprintf(1, ['p value for variance test for prior p sp vs. prior p sp sh ' num2str(p) '\n']);
[h,p]=vartest2(prior_p_sm,prior_p_sm_sh);
fprintf(1, ['p value for variance test for prior p sm vs. prior p sm sh ' num2str(p) '\n']);
end
pffft=1;