forked from restrepd-zz/CaImAnDR
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdrgCaImAn_analyze_batch_pre_per_to_decode_entire_session_fsdzv3.m
executable file
·2188 lines (1808 loc) · 87.1 KB
/
drgCaImAn_analyze_batch_pre_per_to_decode_entire_session_fsdzv3.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
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%drgCaImAn_analyze_batch_pre_per_to_decode_entire_session_fsdzv3
close all
clear all
[choiceFileName,choiceBatchPathName] = uigetfile({'drgCaImAn_LDAfsdz_choices*.m'},'Select the .m file with all the choices for analysis');
fprintf(1, ['\ndrgCaImAn_analyze_batch_pre_per_to_decode_entire_session_fsdzv2 run for ' choiceFileName '\n\n']);
tempDirName=['temp' choiceFileName(12:end-2)];
addpath(choiceBatchPathName)
eval(['handles=' choiceFileName(1:end-2) ';'])
handles.choiceFileName=choiceFileName;
handles.choiceBatchPathName=choiceBatchPathName;
no_files=handles.no_files;
moving_mean_n=10;
min_trial_no=16;
%Which threshold value should we use?
ii_thr=1;
%Time tinterval for shifting time base due to slow olfactometer computer
t_shift=0.61;
%Which algorithm do we use
iiMLalgo_to_use=6;
is_Fabio=0;
switch is_Fabio
case 0
%Choices for Ming's go-no go processing
no_pcorr=4;
%groups to be shown in the zoomed figures for Ming's data
grNo1=4; %Forward proficient
grNo1_label='forward proficient';
grNo2=8; %Forward proficient
grNo2_label='reversed proficient';
case 1
%Fabio's passive experiments
%groups to be shown in the zoomed figures for Fabio's data
% grNo1=1; %AAAP
% grNo1_label='AAAP';
grNo1=2; %female bedding
grNo1_label='female bedding';
grNo2=4; %male bedding
grNo2_label='male bedding';
%Choices for Fabio's passive odorant exposure processing
no_pcorr=1;
case 2
%Ming's passive
grNo1=1; %female bedding
grNo1_label='passive';
grNo2=1; %male bedding
grNo2_label='passive';
%Choices for Fabio's passive odorant exposure processing
no_pcorr=1;
end
fprintf(1, ['\nData were processed with p value threshold = ' num2str(handles.p_threshold(ii_thr)) '\n\n']);
%Now plot for each algorithm the prediction accuracy
handles_out2=[];
handles_out2.classifier_names{1}='Linear Discriminant';
handles_out2.classifier_names{2}='Support Vector Machine';
handles_out2.classifier_names{3}='Naive Bayes Classifier';
handles_out2.classifier_names{4}='Neural Network';
handles_out2.classifier_names{5}='Decision tree';
handles_out2.classifier_names{6}='Binomial glm';
%First and last sessions per mouse
first_last=[20 2;
5 15;
25 1;
13 11;
26 12];
handles_out2.first_last=first_last;
%Mouse names
handles_out2.mouse_names{1}='GRIN1';
handles_out2.mouse_names{2}='GRIN3';
handles_out2.mouse_names{3}='GRIN4';
handles_out2.mouse_names{4}='GRIN6';
handles_out2.mouse_names{5}='GRIN7';
per_names{1}='<40%';
per_names{2}='40-65%';
per_names{3}='65-80%';
per_names{4}='>=80%';
if no_pcorr==1
per_names{1}='';
end
these_groups=unique(handles.group);
if exist([choiceBatchPathName choiceFileName(1:end-2) '.mat'])==0
%Process each file separately
for grNo=1:no_pcorr*length(these_groups)
handles_out2.group_no(grNo).ii_euclid=0;
for iiMLalgo=handles.MLalgo_to_use
for ii_out=1:length(handles.p_threshold)
handles_out2.group_no(grNo).ii_thr(ii_out).MLalgo(iiMLalgo).ii=0;
end
end
end
group_per_file=handles.group;
handles_out2.pcorr_per_file=zeros(1,length(group_per_file));
for fileNo=1:no_files
tic
if iscell(handles.PathName_out)
pre_per_outPathName=handles.PathName_out{fileNo};
else
pre_per_outPathName=handles.PathName_out;
end
pre_per_FileName=handles.FileName_pre_per{fileNo};
this_group=handles.group(fileNo);
this_grNo=find(these_groups==this_group);
if exist([pre_per_outPathName pre_per_FileName(1:end-4) handles.suffix_out])~=0
load([pre_per_outPathName pre_per_FileName(1:end-4) handles.suffix_out])
%Exclude if there are few trials
no_trials=handles_out.ii_out(1).handles_out.no_sp_trials+handles_out.ii_out(1).handles_out.no_sm_trials;
if no_trials>=min_trial_no
if isfield(handles_out.ii_out(1).handles_out,'first_digital_in_ii')
shift_time=0;
else
shift_time=1;
end
if isfield(handles_out.ii_out(1).handles_out,'percent_correct')
pCorr=handles_out.ii_out(1).handles_out.percent_correct;
handles_out2.pcorr_per_file(fileNo)=pCorr;
ii_pCorr=1;
if (pCorr>=40)&(pCorr<=65)
ii_pCorr=2;
else
if (pCorr>65)&(pCorr<80)
ii_pCorr=3;
else
if pCorr>=80
ii_pCorr=4;
end
end
end
else
ii_pCorr=1;
end
if is_Fabio>0
ii_pCorr=1;
end
grNo=(this_grNo-1)*no_pcorr+ii_pCorr;
%Is this the first session?
if sum(first_last(:,1)==fileNo)==1
mouseNo=find(first_last(:,1)==fileNo);
for iiMLalgo=handles.MLalgo_to_use
for ii_out=1:length(handles_out.ii_out)
this_mean_correct_predict=mean(handles_out.ii_out(ii_out).handles_out.MLalgo(iiMLalgo).this_correct_predict);
handles_out2.mouse_no(mouseNo).ii_thr(ii_out).MLalgo(iiMLalgo).first_mean_correct_predict(1,1:length(this_mean_correct_predict))=this_mean_correct_predict;
this_mean_correct_predict_sh=mean(handles_out.ii_out(ii_out).handles_out.MLalgo(iiMLalgo).this_correct_predict_sh);
handles_out2.mouse_no(mouseNo).ii_thr(ii_out).MLalgo(iiMLalgo).first_mean_correct_predict_sh(1,1:length(this_mean_correct_predict_sh))=this_mean_correct_predict_sh;
end
end
end
%Is this the last session?
if sum(first_last(:,2)==fileNo)
mouseNo=find(first_last(:,2)==fileNo);
for iiMLalgo=handles.MLalgo_to_use
for ii_out=1:length(handles_out.ii_out)
this_mean_correct_predict=mean(handles_out.ii_out(ii_out).handles_out.MLalgo(iiMLalgo).this_correct_predict);
handles_out2.mouse_no(mouseNo).ii_thr(ii_out).MLalgo(iiMLalgo).last_mean_correct_predict(1,1:length(this_mean_correct_predict))=this_mean_correct_predict;
this_mean_correct_predict_sh=mean(handles_out.ii_out(ii_out).handles_out.MLalgo(iiMLalgo).this_correct_predict_sh);
handles_out2.mouse_no(mouseNo).ii_thr(ii_out).MLalgo(iiMLalgo).last_mean_correct_predict_sh(1,1:length(this_mean_correct_predict_sh))=this_mean_correct_predict_sh;
end
end
end
for iiMLalgo=handles.MLalgo_to_use
if iiMLalgo==handles.MLalgo_to_use(1)
handles_out2.group_no(grNo).ii_euclid=handles_out2.group_no(grNo).ii_euclid+1;
ii_euclid=handles_out2.group_no(grNo).ii_euclid;
handles_out2.group_no(grNo).shift_time=shift_time;
handles_out2.group_no(grNo).dist_euclid(ii_euclid,1:length(handles_out.ii_out(1).handles_out.dist_euclid))=handles_out.ii_out(1).handles_out.dist_euclid-handles_out.ii_out(1).handles_out.dist_euclid_zero;
handles_out2.group_no(grNo).KLdivergence(ii_euclid,1:length(handles_out.ii_out(1).handles_out.KLdivergence))=handles_out.ii_out(1).handles_out.KLdivergence;
handles_out2.group_no(grNo).time_span_euclid(ii_euclid,1:length(handles_out.ii_out(1).handles_out.time_span))=handles_out.ii_out(1).handles_out.time_span;
handles_out2.group_no(grNo).ii_time_span(ii_euclid,1)=length(handles_out.ii_out(1).handles_out.time_span);
handles_out2.group_no(grNo).meandFFsp(ii_euclid,1:length(handles_out.ii_out(1).handles_out.time_span))=handles_out.ii_out(1).handles_out.meandFFsp;
handles_out2.group_no(grNo).meandFFsm(ii_euclid,1:length(handles_out.ii_out(1).handles_out.time_span))=handles_out.ii_out(1).handles_out.meandFFsm;
end
for ii_out=1:length(handles_out.ii_out)
handles_out2.group_no(grNo).ii_thr(ii_out).MLalgo(iiMLalgo).ii=handles_out2.group_no(grNo).ii_thr(ii_out).MLalgo(iiMLalgo).ii+1;
ii=handles_out2.group_no(grNo).ii_thr(ii_out).MLalgo(iiMLalgo).ii;
accuracy_tr=handles_out.ii_out(ii_out).handles_out.MLalgo(iiMLalgo).accuracy_tr;
handles_out2.group_no(grNo).ii_thr(ii_out).MLalgo(iiMLalgo).accuracy_tr(ii)=accuracy_tr;
handles_out2.group_no(grNo).ii_thr(ii_out).MLalgo(iiMLalgo).shift_time(ii)=shift_time;
accuracy_tr_sh=handles_out.ii_out(ii_out).handles_out.MLalgo(iiMLalgo).accuracy_tr_sh;
handles_out2.group_no(grNo).ii_thr(ii_out).MLalgo(iiMLalgo).accuracy_tr_sh(ii)=accuracy_tr_sh;
accuracy_tr_sh2=handles_out.ii_out(ii_out).handles_out.MLalgo(iiMLalgo).accuracy_tr_sh2;
handles_out2.group_no(grNo).ii_thr(ii_out).MLalgo(iiMLalgo).accuracy_tr_sh2(ii)=accuracy_tr_sh2;
this_mean_correct_predict=mean(handles_out.ii_out(ii_out).handles_out.MLalgo(iiMLalgo).this_correct_predict);
handles_out2.group_no(grNo).ii_thr(ii_out).MLalgo(iiMLalgo).mean_correct_predict(ii,1:length(this_mean_correct_predict))=this_mean_correct_predict;
this_mean_correct_predict=mean(handles_out.ii_out(ii_out).handles_out.MLalgo(iiMLalgo).this_correct_predict);
handles_out2.group_no(grNo).ii_thr(ii_out).MLalgo(iiMLalgo).mean_correct_predict(ii,1:length(this_mean_correct_predict))=this_mean_correct_predict;
this_mean_correct_predict_sh=mean(handles_out.ii_out(ii_out).handles_out.MLalgo(iiMLalgo).this_correct_predict_sh);
handles_out2.group_no(grNo).ii_thr(ii_out).MLalgo(iiMLalgo).mean_correct_predict_sh(ii,1:length(this_mean_correct_predict_sh))=this_mean_correct_predict_sh;
this_per_trial_sp_timecourse=handles_out.ii_out(ii_out).handles_out.MLalgo(iiMLalgo).per_trial_sp_timecourse;
this_per_trial_sm_timecourse=handles_out.ii_out(ii_out).handles_out.MLalgo(iiMLalgo).per_trial_sm_timecourse;
this_moving_mean_per_trial_sp_timecourse = movmean(this_per_trial_sp_timecourse',moving_mean_n)';
this_mean_moving_mean_per_trial_sp_timecourse=mean(this_moving_mean_per_trial_sp_timecourse);
handles_out2.group_no(grNo).ii_thr(ii_out).MLalgo(iiMLalgo).mean_moving_mean_per_trial_sp_timecourse(ii,1:length(this_mean_moving_mean_per_trial_sp_timecourse))=this_mean_moving_mean_per_trial_sp_timecourse;
this_moving_mean_per_trial_sm_timecourse = movmean(this_per_trial_sm_timecourse',moving_mean_n)';
this_mean_moving_mean_per_trial_sm_timecourse=mean(this_moving_mean_per_trial_sm_timecourse);
handles_out2.group_no(grNo).ii_thr(ii_out).MLalgo(iiMLalgo).mean_moving_mean_per_trial_sm_timecourse(ii,1:length(this_mean_moving_mean_per_trial_sm_timecourse))=this_mean_moving_mean_per_trial_sm_timecourse;
per_trial_scores_sp=handles_out.ii_out(ii_out).handles_out.MLalgo(iiMLalgo).per_trial_scores_sp;
mean_per_trial_scores_sp=zeros(size(per_trial_scores_sp,2),size(per_trial_scores_sp,3));
mean_per_trial_scores_sp(:,:)=mean(per_trial_scores_sp);
handles_out2.group_no(grNo).ii_thr(ii_out).MLalgo(iiMLalgo).mean_per_trial_scores_sp(ii,1:2,1:size(per_trial_scores_sp,3))=mean_per_trial_scores_sp;
per_trial_scores_sm=handles_out.ii_out(ii_out).handles_out.MLalgo(iiMLalgo).per_trial_scores_sm;
mean_per_trial_scores_sm=zeros(size(per_trial_scores_sm,2),size(per_trial_scores_sm,3));
mean_per_trial_scores_sm(:,:)=mean(per_trial_scores_sm);
handles_out2.group_no(grNo).ii_thr(ii_out).MLalgo(iiMLalgo).mean_per_trial_scores_sm(ii,1:2,1:size(per_trial_scores_sm,3))=mean_per_trial_scores_sm;
end
end
fprintf(1, ['Import for file No ' num2str(fileNo) ' with ' num2str(no_trials) ' trials done in ' num2str(toc) ' sec\n'])
else
fprintf(1, ['Import for file No ' num2str(fileNo) ' was not performed because it has only ' num2str(no_trials) ' trials\n'])
end
else
fprintf(1, ['Import for file No ' num2str(fileNo) ' failed becasue the file does not exist\n'])
end
end
else
load([choiceBatchPathName choiceFileName(1:end-2) '.mat'])
end
% % ii_thr=length(handles.p_threshold);
% ii_thr=1;
%If shift_time does not exist enter shift_time=1
for grNo=1:length(handles_out2.group_no)
if ~isfield(handles_out2.group_no(grNo),'shift_time')
handles_out2.group_no(grNo).shift_time=1;
end
for ii_out=1:length(handles_out2.group_no(grNo).ii_thr)
for iiMLalgo=1:length(handles_out2.group_no(grNo).ii_thr(ii_out).MLalgo)
% if (iiMLalgo==4)&(ii_out==1)&(grNo==2)
% pfft=1;
% end
if ~isfield(handles_out2.group_no(grNo).ii_thr(ii_out).MLalgo(iiMLalgo),'shift_time')
handles_out2.group_no(grNo).ii_thr(ii_out).MLalgo(iiMLalgo).shift_time=1;
end
end
end
end
%Bar graph plot for accuracy
figureNo=0;
for iiMLalgo=handles.MLalgo_to_use
figureNo = figureNo + 1;
try
close(figureNo)
catch
end
hFig=figure(figureNo);
ax=gca;ax.LineWidth=3;
set(hFig, 'units','normalized','position',[.3 .3 .5 .25])
hold on
edges=[0:0.05:1];
rand_offset=0.8;
bar_offset=0;
for grNo=1:no_pcorr*length(these_groups)
if handles_out2.group_no(grNo).ii_thr(ii_thr).MLalgo(iiMLalgo).ii>0
%Shuffled
bar_offset=bar_offset+1;
these_accuracy_tr_sh2=handles_out2.group_no(grNo).ii_thr(ii_thr).MLalgo(iiMLalgo).accuracy_tr_sh2;
bar(bar_offset,mean(these_accuracy_tr_sh2),'LineWidth', 3,'EdgeColor','none','FaceColor',[80/255 194/255 255/255])
if length(these_accuracy_tr_sh2)>2
%Violin plot
[mean_out, CIout]=drgViolinPoint(these_accuracy_tr_sh2...
,edges,bar_offset,rand_offset,'k','k',3);
end
%Accuracy
bar_offset=bar_offset+1;
these_accuracy_tr=handles_out2.group_no(grNo).ii_thr(ii_thr).MLalgo(iiMLalgo).accuracy_tr;
bar(bar_offset,mean(these_accuracy_tr),'LineWidth', 3,'EdgeColor','none','FaceColor',[0 114/255 178/255])
if length(these_accuracy_tr)>2
%Violin plot
[mean_out, CIout]=drgViolinPoint(these_accuracy_tr...
,edges,bar_offset,rand_offset,'k','k',3);
end
else
bar_offset=bar_offset+2;
end
bar_offset=bar_offset+1;
end
xticks([1.5:3:22.5])
labels='xticklabels({';
for ii_label=1:length(these_groups)
for ii_pcorr=1:no_pcorr
labels=[labels '''' handles.group_names{these_groups(ii_label)} per_names{ii_pcorr} ''', '];
end
end
labels=[labels(1:end-2) '})'];
eval(labels)
title(['Prediction accuracy for ' handles_out2.classifier_names{iiMLalgo}])
ylabel('Accuracy')
ylim([0 1])
xlim([0 24])
pffft=1;
end
%Plot the timecourse of mean dFF
figureNo = figureNo + 1;
try
close(figureNo)
catch
end
hFig=figure(figureNo);
ax=gca;ax.LineWidth=3;
set(hFig, 'units','normalized','position',[.2 .2 .3 .6])
for grNo=1:no_pcorr*length(these_groups)
subplot(8,1,grNo)
hold on
[ii_tspan,ii_file]=max(handles_out2.group_no(grNo).ii_time_span);
if ~isempty(ii_tspan)
%Extrapolate all points onto the longest ii_tspan
time_span=handles_out2.group_no(grNo).time_span_euclid(ii_file,1:ii_tspan);
these_meandFFsp=zeros(size(handles_out2.group_no(grNo).ii_time_span,1),ii_tspan);
these_meandFFsm=handles_out2.group_no(grNo).meandFFsm(1,1:ii_tspan);
for ii_f=1:size(handles_out2.group_no(grNo).ii_time_span,1)
if ii_f==ii_file
these_meandFFsp=handles_out2.group_no(grNo).meandFFsp(ii_file,1:ii_tspan);
these_meandFFsm=handles_out2.group_no(grNo).meandFFsm(ii_file,1:ii_tspan);
else
this_ii_tspan=handles_out2.group_no(grNo).ii_time_span(ii_f);
this_time_span=handles_out2.group_no(grNo).time_span_euclid(ii_f,1:this_ii_tspan);
this_meandFFsp=zeros(1,ii_tspan);
for ii_tsp=1:ii_tspan
if time_span(ii_tsp)<this_time_span(1)
these_meandFFsp(ii_f,ii_tsp)=handles_out2.group_no(grNo).meandFFsp(ii_f,1);
these_meandFFsm(ii_f,ii_tsp)=handles_out2.group_no(grNo).meandFFsm(ii_f,1);
else
if time_span(ii_tsp)>this_time_span(end)
these_meandFFsp(ii_f,ii_tsp)=handles_out2.group_no(grNo).meandFFsp(ii_f,end);
these_meandFFsm(ii_f,ii_tsp)=handles_out2.group_no(grNo).meandFFsm(ii_f,end);
else
ii_0=find(this_time_span<=time_span(ii_tsp),1,'last');
ii_1=find(this_time_span>time_span(ii_tsp),1,'first');
these_meandFFsp(ii_f,ii_tsp)=handles_out2.group_no(grNo).meandFFsp(ii_f,ii_0)+...
(handles_out2.group_no(grNo).meandFFsp(ii_f,ii_1)-handles_out2.group_no(grNo).meandFFsp(ii_f,ii_0))...
*(time_span(ii_tsp)-this_time_span(ii_0))/(this_time_span(ii_1)-this_time_span(ii_0));
these_meandFFsm(ii_f,ii_tsp)=handles_out2.group_no(grNo).meandFFsm(ii_f,ii_0)+...
(handles_out2.group_no(grNo).meandFFsm(ii_f,ii_1)-handles_out2.group_no(grNo).meandFFsm(ii_f,ii_0))...
*(time_span(ii_tsp)-this_time_span(ii_0))/(this_time_span(ii_1)-this_time_span(ii_0));
end
end
end
end
end
if size(these_meandFFsm,1)>2
CIpv = bootci(1000, @mean, these_meandFFsm);
meanpv=mean(these_meandFFsm,1);
CIpv(1,:)=meanpv-CIpv(1,:);
CIpv(2,:)=CIpv(2,:)-meanpv;
[hlpvl, hppvl] = boundedline(time_span',mean(these_meandFFsm,1)', CIpv', 'b');
else
if size(these_meandFFsm,1)>0
plot(time_span',mean(these_meandFFsm,1)', 'b');
end
end
if size(these_meandFFsp,1)>2
CIpv = bootci(1000, @mean, these_meandFFsp);
meanpv=mean(these_meandFFsp,1);
CIpv(1,:)=meanpv-CIpv(1,:);
CIpv(2,:)=CIpv(2,:)-meanpv;
[hlpvl, hppvl] = boundedline(time_span',mean(these_meandFFsp,1)', CIpv', 'r');
else
if size(these_meandFFsp,1)>0
plot(time_span',mean(these_meandFFsp,1)', 'r');
end
end
end
xlim([-10 20])
xlabel('Time(sec)')
if no_pcorr==1
title([handles.group_names{grNo}])
else
this_grNo=floor((grNo-1)/4)+1;
ii_pcorr=grNo-4*(this_grNo-1);
title([handles.group_names{these_groups(this_grNo)} ' ' per_names{ii_pcorr}])
end
pffft=1;
end
sgtitle('Mean dFF')
%Now plot the timecourse for euclidean distance
figureNo = figureNo + 1;
try
close(figureNo)
catch
end
hFig=figure(figureNo);
ax=gca;ax.LineWidth=3;
set(hFig, 'units','normalized','position',[.2 .2 .3 .6])
for grNo=1:no_pcorr*length(these_groups)
[ii_tspan,ii_file]=max(handles_out2.group_no(grNo).ii_time_span);
subplot(8,1,grNo)
hold on
if ~isempty(ii_tspan)
time_span=handles_out2.group_no(grNo).time_span_euclid(1,1:ii_tspan);
%Extrapolate all points onto the longest ii_tspan
time_span=handles_out2.group_no(grNo).time_span_euclid(ii_file,1:ii_tspan);
these_dist_euclid=zeros(size(handles_out2.group_no(grNo).ii_time_span,1),ii_tspan);
for ii_f=1:size(handles_out2.group_no(grNo).ii_time_span,1)
if ii_f==ii_file
these_dist_euclid=handles_out2.group_no(grNo).dist_euclid(ii_file,1:ii_tspan);
else
this_ii_tspan=handles_out2.group_no(grNo).ii_time_span(ii_f);
this_time_span=handles_out2.group_no(grNo).time_span_euclid(ii_f,1:this_ii_tspan);
this_dist_euclid=zeros(1,ii_tspan);
for ii_tsp=1:ii_tspan
if time_span(ii_tsp)<this_time_span(1)
these_dist_euclid(ii_f,ii_tsp)=handles_out2.group_no(grNo).dist_euclid(ii_f,1);
else
if time_span(ii_tsp)>this_time_span(end)
these_dist_euclid(ii_f,ii_tsp)=handles_out2.group_no(grNo).dist_euclid(ii_f,end);
else
ii_0=find(this_time_span<=time_span(ii_tsp),1,'last');
ii_1=find(this_time_span>time_span(ii_tsp),1,'first');
these_dist_euclid(ii_f,ii_tsp)=handles_out2.group_no(grNo).dist_euclid(ii_f,ii_0)+...
(handles_out2.group_no(grNo).dist_euclid(ii_f,ii_1)-handles_out2.group_no(grNo).dist_euclid(ii_f,ii_0))...
*(time_span(ii_tsp)-this_time_span(ii_0))/(this_time_span(ii_1)-this_time_span(ii_0));
end
end
end
end
end
if size(these_dist_euclid,1)>2
CIpv = bootci(1000, @mean, these_dist_euclid);
meanpv=mean(these_dist_euclid,1);
CIpv(1,:)=meanpv-CIpv(1,:);
CIpv(2,:)=CIpv(2,:)-meanpv;
[hlpvl, hppvl] = boundedline(time_span',mean(these_dist_euclid,1)', CIpv', 'm');
else
if size(these_dist_euclid,1)>0
plot(time_span',mean(these_dist_euclid,1)', 'm');
end
end
this_ylim=ylim;
plot([0 0],this_ylim,'-k')
end
xlim([-10 20])
xlabel('Time(sec)')
if no_pcorr==1
title([handles.group_names{grNo}])
else
this_grNo=floor((grNo-1)/4)+1;
ii_pcorr=grNo-4*(this_grNo-1);
title([handles.group_names{these_groups(this_grNo)} ' ' per_names{ii_pcorr}])
end
end
sgtitle('Euclidean distance')
figureNo = figureNo + 1;
try
close(figureNo)
catch
end
hFig=figure(figureNo);
ax=gca;ax.LineWidth=3;
set(hFig, 'units','normalized','position',[.2 .2 .3 .6])
delta_KLdiv_post=[];
for grNo=1:no_pcorr*length(these_groups)
[ii_tspan,ii_file]=max(handles_out2.group_no(grNo).ii_time_span);
subplot(8,1,grNo)
hold on
if ~isempty(ii_tspan)
time_span=handles_out2.group_no(grNo).time_span_euclid(1,1:ii_tspan);
%Extrapolate all points onto the longest ii_tspan
time_span=handles_out2.group_no(grNo).time_span_euclid(ii_file,1:ii_tspan);
for ii_f=1:size(handles_out2.group_no(grNo).ii_time_span,1)
if ii_f==ii_file
these_KLdivergence=handles_out2.group_no(grNo).KLdivergence(ii_file,1:ii_tspan);
else
this_ii_tspan=handles_out2.group_no(grNo).ii_time_span(ii_f);
this_time_span=handles_out2.group_no(grNo).time_span_euclid(ii_f,1:this_ii_tspan);
this_KLdivergence=zeros(1,ii_tspan);
for ii_tsp=1:ii_tspan
if time_span(ii_tsp)<this_time_span(1)
these_KLdivergence(ii_f,ii_tsp)=handles_out2.group_no(grNo).KLdivergence(ii_f,1);
else
if time_span(ii_tsp)>this_time_span(end)
these_KLdivergence(ii_f,ii_tsp)=handles_out2.group_no(grNo).KLdivergence(ii_f,end);
else
ii_0=find(this_time_span<=time_span(ii_tsp),1,'last');
ii_1=find(this_time_span>time_span(ii_tsp),1,'first');
these_KLdivergence(ii_f,ii_tsp)=handles_out2.group_no(grNo).KLdivergence(ii_f,ii_0)+...
(handles_out2.group_no(grNo).KLdivergence(ii_f,ii_1)-handles_out2.group_no(grNo).KLdivergence(ii_f,ii_0))...
*(time_span(ii_tsp)-this_time_span(ii_0))/(this_time_span(ii_1)-this_time_span(ii_0));
end
end
end
end
end
%Calculate the delta_KLdiv
delta_KLdiv=[];
these_delta_KLdiv_post=[];
for ii_file=1:ii_f
this_delta_KLdiv=[];
this_delta_KLdiv=these_KLdivergence(ii_file,:)-mean(these_KLdivergence(ii_file,(time_span'>-20)&(time_span'<-2)));
delta_KLdiv(ii_file,:)=this_delta_KLdiv;
these_delta_KLdiv_post=[these_delta_KLdiv_post mean(this_delta_KLdiv((time_span'>=0)&(time_span'<=handles.post_time)))];
end
delta_KLdiv_post.group(grNo).delta_KLdiv_post=these_delta_KLdiv_post;
if size(these_KLdivergence,1)>2
CIpv = bootci(1000, @mean, delta_KLdiv);
meanpv=mean(delta_KLdiv,1);
CIpv(1,:)=meanpv-CIpv(1,:);
CIpv(2,:)=CIpv(2,:)-meanpv;
delta_mean_KLdiv=mean(delta_KLdiv,1)';
[hlpvl, hppvl] = boundedline(time_span',delta_mean_KLdiv, CIpv', 'm');
else
if size(these_KLdivergence,1)>0
delta_mean_KLdiv=mean(delta_KLdiv,1)';
plot(time_span',delta_mean_KLdiv, 'm');
end
end
this_ylim=ylim;
plot([0 0],this_ylim,'-k')
end
xlim([-10 20])
xlabel('Time(sec)')
if no_pcorr==1
title([handles.group_names{grNo}])
else
this_grNo=floor((grNo-1)/4)+1;
ii_pcorr=grNo-4*(this_grNo-1);
title([handles.group_names{these_groups(this_grNo)} ' ' per_names{ii_pcorr}])
end
end
sgtitle('KL divergence')
num_groups=length(delta_KLdiv_post.group);
%Plot a bar graph for post_time KL divergence
figureNo = figureNo + 1;
try
close(figureNo)
catch
end
hFig=figure(figureNo);
ax=gca;ax.LineWidth=3;
set(hFig, 'units','normalized','position',[.3 .3 .5 .25])
hold on
edges=[0:0.05:1];
rand_offset=0.8;
bar_offset=0;
for grNo=1:num_groups
if length(delta_KLdiv_post.group(grNo).delta_KLdiv_post)>0
bar_offset=bar_offset+1;
these_delta_KLdiv=delta_KLdiv_post.group(grNo).delta_KLdiv_post;
bar(bar_offset,mean(these_delta_KLdiv),'LineWidth', 3,'EdgeColor','none','FaceColor',[0 114/255 178/255])
if length(these_delta_KLdiv)>2
%Violin plot
[mean_out, CIout]=drgViolinPoint(these_delta_KLdiv...
,edges,bar_offset,rand_offset,'k','k',3);
end
else
bar_offset=bar_offset+1;
end
bar_offset=bar_offset+1;
end
xticks([1:2:15])
labels='xticklabels({';
for ii_label=1:length(these_groups)
for ii_pcorr=1:no_pcorr
labels=[labels '''' handles.group_names{these_groups(ii_label)} per_names{ii_pcorr} ''', '];
end
end
labels=[labels(1:end-2) '})'];
eval(labels)
title('delta KL divergence ')
ylabel('delta KL div')
%Plot the delta KL divergence for grNo1 and grNo2
figureNo = figureNo + 1;
try
close(figureNo)
catch
end
hFig=figure(figureNo);
hold on
ax=gca;ax.LineWidth=3;
set(hFig, 'units','normalized','position',[.2 .2 .3 .3])
%Do forward
grNo=grNo1;
[ii_tspan,ii_file]=max(handles_out2.group_no(grNo).ii_time_span);
if ~isempty(ii_tspan)
time_span=handles_out2.group_no(grNo).time_span_euclid(1,1:ii_tspan);
%Extrapolate all points onto the longest ii_tspan
time_span=handles_out2.group_no(grNo).time_span_euclid(ii_file,1:ii_tspan);
for ii_f=1:size(handles_out2.group_no(grNo).ii_time_span,1)
if ii_f==ii_file
these_KLdivergence=handles_out2.group_no(grNo).KLdivergence(ii_file,1:ii_tspan);
else
this_ii_tspan=handles_out2.group_no(grNo).ii_time_span(ii_f);
this_time_span=handles_out2.group_no(grNo).time_span_euclid(ii_f,1:this_ii_tspan);
this_KLdivergence=zeros(1,ii_tspan);
for ii_tsp=1:ii_tspan
if time_span(ii_tsp)<this_time_span(1)
these_KLdivergence(ii_f,ii_tsp)=handles_out2.group_no(grNo).KLdivergence(ii_f,1);
else
if time_span(ii_tsp)>this_time_span(end)
these_KLdivergence(ii_f,ii_tsp)=handles_out2.group_no(grNo).KLdivergence(ii_f,end);
else
ii_0=find(this_time_span<=time_span(ii_tsp),1,'last');
ii_1=find(this_time_span>time_span(ii_tsp),1,'first');
these_KLdivergence(ii_f,ii_tsp)=handles_out2.group_no(grNo).KLdivergence(ii_f,ii_0)+...
(handles_out2.group_no(grNo).KLdivergence(ii_f,ii_1)-handles_out2.group_no(grNo).KLdivergence(ii_f,ii_0))...
*(time_span(ii_tsp)-this_time_span(ii_0))/(this_time_span(ii_1)-this_time_span(ii_0));
end
end
end
end
end
%Calculate the delta_KLdiv
delta_KLdiv=[];
for ii_file=1:ii_f
delta_KLdiv(ii_file,:)=these_KLdivergence(ii_file,:)-mean(these_KLdivergence(ii_file,(time_span'>-20)&(time_span'<-2)));
end
if size(these_KLdivergence,1)>2
CIpv = bootci(1000, @mean, delta_KLdiv);
meanpv=mean(delta_KLdiv,1);
CIpv(1,:)=meanpv-CIpv(1,:);
CIpv(2,:)=CIpv(2,:)-meanpv;
delta_mean_KLdiv=mean(delta_KLdiv,1)';
[hlpvl, hppvl] = boundedline(time_span',delta_mean_KLdiv, CIpv', 'b');
else
if size(these_KLdivergence,1)>0
delta_mean_KLdiv=mean(delta_KLdiv,1)';
plot(time_span',delta_mean_KLdiv, 'b');
end
end
this_ylim=ylim;
plot([0 0],this_ylim,'-k')
end
%Do reversed
try
grNo=grNo2;
[ii_tspan,ii_file]=max(handles_out2.group_no(grNo).ii_time_span);
if ~isempty(ii_tspan)
time_span=handles_out2.group_no(grNo).time_span_euclid(1,1:ii_tspan);
%Extrapolate all points onto the longest ii_tspan
time_span=handles_out2.group_no(grNo).time_span_euclid(ii_file,1:ii_tspan);
for ii_f=1:size(handles_out2.group_no(grNo).ii_time_span,1)
if ii_f==ii_file
these_KLdivergence=handles_out2.group_no(grNo).KLdivergence(ii_file,1:ii_tspan);
else
this_ii_tspan=handles_out2.group_no(grNo).ii_time_span(ii_f);
this_time_span=handles_out2.group_no(grNo).time_span_euclid(ii_f,1:this_ii_tspan);
this_KLdivergence=zeros(1,ii_tspan);
for ii_tsp=1:ii_tspan
if time_span(ii_tsp)<this_time_span(1)
these_KLdivergence(ii_f,ii_tsp)=handles_out2.group_no(grNo).KLdivergence(ii_f,1);
else
if time_span(ii_tsp)>this_time_span(end)
these_KLdivergence(ii_f,ii_tsp)=handles_out2.group_no(grNo).KLdivergence(ii_f,end);
else
ii_0=find(this_time_span<=time_span(ii_tsp),1,'last');
ii_1=find(this_time_span>time_span(ii_tsp),1,'first');
these_KLdivergence(ii_f,ii_tsp)=handles_out2.group_no(grNo).KLdivergence(ii_f,ii_0)+...
(handles_out2.group_no(grNo).KLdivergence(ii_f,ii_1)-handles_out2.group_no(grNo).KLdivergence(ii_f,ii_0))...
*(time_span(ii_tsp)-this_time_span(ii_0))/(this_time_span(ii_1)-this_time_span(ii_0));
end
end
end
end
end
%Calculate the delta_KLdiv
delta_KLdiv=[];
for ii_file=1:ii_f
delta_KLdiv(ii_file,:)=these_KLdivergence(ii_file,:)-mean(these_KLdivergence(ii_file,(time_span'>-20)&(time_span'<-2)));
end
if size(these_KLdivergence,1)>2
CIpv = bootci(1000, @mean, delta_KLdiv);
meanpv=mean(delta_KLdiv,1);
CIpv(1,:)=meanpv-CIpv(1,:);
CIpv(2,:)=CIpv(2,:)-meanpv;
delta_mean_KLdiv=mean(delta_KLdiv,1)';
[hlpvl, hppvl] = boundedline(time_span',delta_mean_KLdiv, CIpv', 'r');
else
if size(these_KLdivergence,1)>0
delta_mean_KLdiv=mean(delta_KLdiv,1)';
plot(time_span',delta_mean_KLdiv, 'r');
end
end
this_ylim=ylim;
plot([0 0],this_ylim,'-k')
end
catch
end
% ylim([-10 10])
xlim([-10 20])
xlabel('Time(sec)')
title(['delta KL distance for blue=' grNo1_label ' red= ' grNo2_label])
% %Plot the delta KL divergence for forward proficient and naive
% figureNo = figureNo + 1;
% try
% close(figureNo)
% catch
% end
% hFig=figure(figureNo);
% hold on
%
% ax=gca;ax.LineWidth=3;
% set(hFig, 'units','normalized','position',[.2 .2 .3 .3])
%
% %Do forward naive
% grNo=2;
%
% [ii_tspan,ii_file]=max(handles_out2.group_no(grNo).ii_time_span);
%
%
% if ~isempty(ii_tspan)
% time_span=handles_out2.group_no(grNo).time_span_euclid(1,1:ii_tspan);
%
% %Extrapolate all points onto the longest ii_tspan
% time_span=handles_out2.group_no(grNo).time_span_euclid(ii_file,1:ii_tspan);
%
% for ii_f=1:size(handles_out2.group_no(grNo).ii_time_span,1)
% if ii_f==ii_file
% these_KLdivergence=handles_out2.group_no(grNo).KLdivergence(ii_file,1:ii_tspan);
% else
% this_ii_tspan=handles_out2.group_no(grNo).ii_time_span(ii_f);
% this_time_span=handles_out2.group_no(grNo).time_span_euclid(ii_f,1:this_ii_tspan);
% this_KLdivergence=zeros(1,ii_tspan);
% for ii_tsp=1:ii_tspan
% if time_span(ii_tsp)<this_time_span(1)
% these_KLdivergence(ii_f,ii_tsp)=handles_out2.group_no(grNo).KLdivergence(ii_f,1);
%
% else
% if time_span(ii_tsp)>this_time_span(end)
% these_KLdivergence(ii_f,ii_tsp)=handles_out2.group_no(grNo).KLdivergence(ii_f,end);
% else
% ii_0=find(this_time_span<=time_span(ii_tsp),1,'last');
% ii_1=find(this_time_span>time_span(ii_tsp),1,'first');
% these_KLdivergence(ii_f,ii_tsp)=handles_out2.group_no(grNo).KLdivergence(ii_f,ii_0)+...
% (handles_out2.group_no(grNo).KLdivergence(ii_f,ii_1)-handles_out2.group_no(grNo).KLdivergence(ii_f,ii_0))...
% *(time_span(ii_tsp)-this_time_span(ii_0))/(this_time_span(ii_1)-this_time_span(ii_0));
% end
% end
% end
%
% end
%
% end
%
% %Calculate the delta_KLdiv
% delta_KLdiv=[];
% for ii_file=1:ii_f
% delta_KLdiv(ii_file,:)=these_KLdivergence(ii_file,:)-mean(these_KLdivergence(ii_file,(time_span'>-20)&(time_span'<-2)));
% end
%
% if size(these_KLdivergence,1)>2
%
% CIpv = bootci(1000, @mean, delta_KLdiv);
% meanpv=mean(delta_KLdiv,1);
% CIpv(1,:)=meanpv-CIpv(1,:);
% CIpv(2,:)=CIpv(2,:)-meanpv;
%
% delta_mean_KLdiv=mean(delta_KLdiv,1)';
% [hlpvl, hppvl] = boundedline(time_span',delta_mean_KLdiv, CIpv', 'cmap',[80/255 194/255 255/255]);
% else
% if size(these_KLdivergence,1)>0
% delta_mean_KLdiv=mean(delta_KLdiv,1)';
% plot(time_span',delta_mean_KLdiv, 'Color' ,[80/255 194/255 255/255]);
% end
%
% end
%
% end
%
% %Do forward proficient
% grNo=4;
%
% [ii_tspan,ii_file]=max(handles_out2.group_no(grNo).ii_time_span);
%
%
% if ~isempty(ii_tspan)
% time_span=handles_out2.group_no(grNo).time_span_euclid(1,1:ii_tspan);
%
% %Extrapolate all points onto the longest ii_tspan
% time_span=handles_out2.group_no(grNo).time_span_euclid(ii_file,1:ii_tspan);
%
% for ii_f=1:size(handles_out2.group_no(grNo).ii_time_span,1)
% if ii_f==ii_file
% these_KLdivergence=handles_out2.group_no(grNo).KLdivergence(ii_file,1:ii_tspan);
% else
% this_ii_tspan=handles_out2.group_no(grNo).ii_time_span(ii_f);
% this_time_span=handles_out2.group_no(grNo).time_span_euclid(ii_f,1:this_ii_tspan);
% this_KLdivergence=zeros(1,ii_tspan);
% for ii_tsp=1:ii_tspan
% if time_span(ii_tsp)<this_time_span(1)
% these_KLdivergence(ii_f,ii_tsp)=handles_out2.group_no(grNo).KLdivergence(ii_f,1);
%
% else
% if time_span(ii_tsp)>this_time_span(end)
% these_KLdivergence(ii_f,ii_tsp)=handles_out2.group_no(grNo).KLdivergence(ii_f,end);
% else
% ii_0=find(this_time_span<=time_span(ii_tsp),1,'last');
% ii_1=find(this_time_span>time_span(ii_tsp),1,'first');
% these_KLdivergence(ii_f,ii_tsp)=handles_out2.group_no(grNo).KLdivergence(ii_f,ii_0)+...
% (handles_out2.group_no(grNo).KLdivergence(ii_f,ii_1)-handles_out2.group_no(grNo).KLdivergence(ii_f,ii_0))...
% *(time_span(ii_tsp)-this_time_span(ii_0))/(this_time_span(ii_1)-this_time_span(ii_0));
% end
% end
% end
%
% end
%
% end
%
% %Calculate the delta_KLdiv
% delta_KLdiv=[];
% for ii_file=1:ii_f
% delta_KLdiv(ii_file,:)=these_KLdivergence(ii_file,:)-mean(these_KLdivergence(ii_file,(time_span'>-20)&(time_span'<-2)));
% end
%
% if size(these_KLdivergence,1)>2
%
% CIpv = bootci(1000, @mean, delta_KLdiv);
% meanpv=mean(delta_KLdiv,1);
% CIpv(1,:)=meanpv-CIpv(1,:);
% CIpv(2,:)=CIpv(2,:)-meanpv;