-
Notifications
You must be signed in to change notification settings - Fork 0
/
dfs_ripcontent.m
2976 lines (2798 loc) · 222 KB
/
dfs_ripcontent.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
%% Sungod manu: Plot main results
animals = {'jaq','roquefort','despereaux','montague'}; %,, 'remy',};%};
epochfilter{1} = ['$ripthresh>=0 & (isequal($environment,''goal'')) & $forageassist==0 & $gooddecode==1']; % & $decode_error<=1
%epochfilter{1} = ['$session==27'];
% resultant excludeperiods will define times when velocity is high
timefilter{1} = {'ag_get2dstate', '($immobility == 1)','immobility_velocity',4,'immobility_buffer',0};
iterator = 'epochbehaveanal';
f = createfilter('animal',animals,'epochs',epochfilter,'excludetime', timefilter, 'iterator', iterator);
f = setfilterfunction(f, 'dfa_ripcontent', {'ripdecodesv3','trials','pos'});
f = runfilter(f);
animcol = [27 92 41; 25 123 100; 33 159 169; 123 225 191]./255; %ctrlcols
%% 1. plot behavioral metrics: reward rate during repeat phase, length of search phase, fraction of search trials that are pg (Fig. 1D)
byep = figure();
pgfracs = figure();
for a = 1:length(animals)
eps = find(arrayfun(@(x) ~isempty(x.trips),f(a).output{1}));
c=1;
for e = 1:length(eps)
taskphase= f(a).output{1}(eps(e)).trips.taskphase;
valtrials = ~isnan(taskphase);
taskphase = taskphase(valtrials);
conts = f(a).output{1}(eps(e)).trips.contingency(valtrials);
outers = f(a).output{1}(eps(e)).trips.outerarm(valtrials);
goals = f(a).output{1}(eps(e)).trips.goalarm(valtrials,:);
for cont = 1:length(unique(conts)) % just up to 7th cont so there's a comparison
ctphase = taskphase(conts==cont);
if any(ctphase==1) % at least search is complete; usable contingency
epnum{a}(c,1) = e;
searchlength{a}(c,:) = [sum(ctphase<=1) cont];
if cont>1 % count fraction of search that is visits to prevgoal
outercounts = histcounts(outers(conts==cont & taskphase<=1)',[1:9]);
pgoalfrac{a}(c,1)=outercounts(unique(goals(conts==cont & taskphase<=1,2)))/sum(ctphase<=1);
others = find(~ismember([1:8],goals(conts<=cont &taskphase<=1,2))); % taskphase<=1 to exclude last trial of block
otherfrac{a}{c} = outercounts(others)./sum(ctphase<=1);
else
pgoalfrac{a}(c,1) = nan;
end
if cont>2
ppgoalfrac{a}(c,1)=sum(outers(conts==cont & taskphase<=1)'==goals(conts==cont & taskphase<=1,3))/sum(ctphase<=1);
else
ppgoalfrac{a}(c,1) = [nan];
end
if any(ctphase>=4) % enough of repeat phase to count
rewrate{a}(c,1) = sum(mod(ctphase,1)==0 & ctphase>1)/sum(ctphase>1 & mod(ctphase,1)<=.8); % dont count final .9 unrew visit in total
totallength{a}(c,1) = length(ctphase);
fullblocksperep{a}(e,1) = cont;
% all trials before vs during repeat phase vs subsequent cont vs all trials after
firstrew = find(cont==conts & taskphase==1);
if any(conts<cont & taskphase<=1)
beforeafter{a}(c,1) = sum(outers(conts<cont & taskphase<=1)==goals(firstrew,1))/sum(conts<cont & taskphase<=1);
else
beforeafter{a}(c,1) = nan;
end
beforeafter{a}(c,2) = sum(ctphase>1 & (mod(ctphase,1)==0|mod(ctphase,1)>.85))/sum(ctphase>1);
if any(conts==(cont+1) & taskphase<=1)
beforeafter{a}(c,3) = sum(outers(conts==(cont+1) & taskphase<=1)==goals(firstrew,1))/sum(conts==(cont+1) & taskphase<=1);
else
beforeafter{a}(c,3) = nan;
end
if any(conts==(cont+2) & taskphase<=1)
beforeafter{a}(c,4) = sum(outers(conts>=(cont+2) & taskphase<=1)==goals(firstrew,1))/sum(conts>=(cont+2) & taskphase<=1);
else
beforeafter{a}(c,4) = nan;
end
else
rewrate{a}(c,1) = nan;
totallength{a}(c,1) = nan;
fullblocksperep{a}(e,1) = cont-1;
end
c = c+1;
end
end
end
figure(byep)
val = searchlength{a}(:,2)<=8;
subplot(2,2,a); hold on; plot(searchlength{a}(val,2),searchlength{a}(val,1),'.','Color',animcol(a,:)); lsline
[r,p] = corrcoef(searchlength{a}(val,:)); text(5,30,sprintf('r2=%.03f,p=%.03f',r(2)^2,p(2)),'Color',animcol(a,:));
xlim([0 10]); ylim([0 45]);
% [totmean,totsd] = grpstats(totallength{a},epnum{a},{'mean','sem'});
% subplot(3,1,1); hold on; errorbar(totmean,totsd,'Color',animcol(a,:)); title('totallength')
% [searchmean,searchsd] = grpstats(searchlength{a},epnum{a},{'mean','sem'});
% subplot(3,1,2); hold on; errorbar(searchmean,searchsd,'Color',animcol(a,:)); title('searchlength')
% [ratemean,ratesd] = grpstats(rewrate{a},epnum{a},{'mean','sem'});
% subplot(3,1,3); hold on; errorbar(ratemean,ratesd,'Color',animcol(a,:)); title('repeat rewrate')
%
figure(pgfracs);
subplot(1,2,1); hold on;
allother = horzcat(otherfrac{a}{:});
errorbar([a a+.2],[nanmean(pgoalfrac{a}),nanmean(allother)],[nanstd(pgoalfrac{a})/sqrt(sum(~isnan(pgoalfrac{a}))),nanstd(allother)/sqrt(length(allother))],'k.');
bar([a a+.2 ],[nanmean(pgoalfrac{a}),nanmean(allother)],'FaceColor',animcol(a,:));
text([a ],[.27 ],num2str(ranksum(pgoalfrac{a},allother),'%.02f'));
ylabel('frac visits'); title('search at pg vs other arms');ylim([0 .3])
% [~,pgpval(a)] = ttest2(pgoalfrac{a},horzcat(otherfrac{a}{:}));
% [~,ppgpval(a)] = ttest2(ppgoalfrac{a},horzcat(otherfrac{a}{:}));
%
% errorbar(a,nanmean(pgoalfrac{a}),nanstd(pgoalfrac{a})/sqrt(sum(~isnan(pgoalfrac{a}))),'k.'); bar(a,nanmean(pgoalfrac{a}),.2,'FaceColor',animcol(a,:));
% errorbar(a+.2,nanmean(allother),nanstd(allother)/sqrt(length(allother)),'k.');bar(a+.2,nanmean(allother),.2,'FaceColor',animcol(a,:),'FaceAlpha',.3);
% errorbar(a+.4,nanmean(ppgoalfrac{a}),nanstd(ppgoalfrac{a})/sqrt(sum(~isnan(ppgoalfrac{a}))),'k.'); bar(a+.4,nanmean(ppgoalfrac{a}),.2,'FaceColor',animcol(a,:));
% text(a,.27,num2str(pgpval(a),'%.02f'));
% text(a+.1,.25,num2str(ppgpval(a),'%.02f'));
% ylabel('frac of search at pg/ppg')
subplot(1,2,2); hold on
errorbar([a a+.2 ],nanmean(beforeafter{a}(:,[1 4])),nanstd(beforeafter{a}(:,[1 4]))./sqrt(sum(~isnan(beforeafter{a}(:,[1 4])))),'k.');
bar([a a+.2 ],nanmean(beforeafter{a}(:,[1 4])),'FaceColor',animcol(a,:));
text([a ],[.27 ],num2str(ranksum(beforeafter{a}(:,1),beforeafter{a}(:,4)),'%.02f'));
ylabel('visits/trial at goal'); title('before/remainder search trials');ylim([0 .3])
end
figure; subplot(2,3,1); hold on; plot4a(totallength); title('totallength'); ylim([0 70]);
subplot(2,3,2); hold on; plot4a(searchlength); title('searchlength'); ylim([0 20]);
subplot(2,3,3); hold on; plot4a(rewrate); title('rewrate'); ylim([0 1]); plot([0 15],[.125 .125],'k:')
subplot(2,3,4); hold on; plot4a(fullblocksperep); title('trialblocks/epoch')
subplot(2,3,5); text(0,.5,sprintf('n=%d',cellfun(@length,fullblocksperep)'))
%hold on; plot4a(pgoalfrac); title('pg frac of search'); plot([0 15],[.125 .125],'k:')
%ylim([0 1]); text([7:10],[.7 .7 .7 .7],num2str(pgpval','%.02f'))
%subplot(2,3,6); hold on; plot4a(ppgoalfrac); title('ppg frac of search'); plot([0 15],[.125 .125],'k:')
%ylim([0 1]); text([7:10],[.7 .7 .7 .7],num2str(ppgpval','%.02f'))
%% 2. plot behavioral metrics: reward rate curves aligned to first/last reward at goal [Fig 1E,F]
figure; clearvars -except f animals animcol
pad=100;
for a = 1:length(animals)
eps = find(arrayfun(@(x) ~isempty(x.trips),f(a).output{1}));
c=1;
for e = 1:length(eps)
taskphase= f(a).output{1}(eps(e)).trips.taskphase;
valtrials = ~isnan(taskphase);
taskphase = taskphase(valtrials);
conts = f(a).output{1}(eps(e)).trips.contingency(valtrials);
for cont = 1:length(unique(conts))
ctphase = taskphase(conts==cont);
if any(ctphase>=4) % enough of repeat phase to count
if mod(ctphase(end),1)>.8 % get rid of the final trial (unrewarded) .9 (doesnt change curves much either way)
ctphase = ctphase(1:end-1);
end
firstgoal = find(ctphase==1);
rewards = mod(ctphase,1)==0 & ctphase>=1 %) | mod(ctphase,1)>.8;
repeataligned{a}(c,:) = [nan(pad-firstgoal,1);rewards;nan(pad-(length(ctphase)-firstgoal),1)];
searchaligned{a}(c,:) = [rewards;nan(pad-length(ctphase),1)];
c = c+1;
end
end
end
subplot(1,2,1); hold on;
sreps = 1:15;
% use binomial SE rather than standard sqrt(p*(1-p)/n)
bse = sqrt((nanmean(searchaligned{a}(:,sreps)).*(1-nanmean(searchaligned{a}(:,sreps))))./sum(~isnan(searchaligned{a}(:,sreps))));
h = fill([sreps, fliplr(sreps)], [nanmean(searchaligned{a}(:,sreps))-bse, fliplr(nanmean(searchaligned{a}(:,sreps))+bse)],animcol(a,:),'FaceAlpha',.3);%
%h = fill([sreps, fliplr(sreps)], [nanmean(searchaligned{a}(:,sreps))-nanstd(searchaligned{a}(:,sreps))./sqrt(sum(~isnan(searchaligned{a}(:,sreps)))), fliplr(nanmean(searchaligned{a}(:,sreps))+nanstd(searchaligned{a}(:,sreps))./sqrt(sum(~isnan(searchaligned{a}(:,sreps)))))],animcol(a,:),'FaceAlpha',.3);%
set(h,'EdgeColor','none'); plot(sreps, nanmean(searchaligned{a}(:,sreps)),'.-','Color',animcol(a,:),'Linewidth',2); plot([1 15],[.125 .125],'k:')
xlabel('trials since goal change'); ylim([0 1]); title('aligned to start of search phase'); ylabel('reward rate');
ntext = sprintf('n=%dconts/%deps',size(repeataligned{a},1),length(eps)); text(10,a/10,ntext,'Color',animcol(a,:));
subplot(1,2,2); hold on;
reps = pad+sreps;
bse = sqrt((nanmean(repeataligned{a}(:,reps)).*(1-nanmean(repeataligned{a}(:,reps))))./sum(~isnan(repeataligned{a}(:,reps))));
h = fill([sreps, fliplr(sreps)], [nanmean(repeataligned{a}(:,reps))-bse, fliplr(nanmean(repeataligned{a}(:,reps))+bse)],animcol(a,:),'FaceAlpha',.3);%
%h = fill([sreps, fliplr(sreps)], [nanmean(repeataligned{a}(:,reps))-nanstd(repeataligned{a}(:,reps))./sqrt(sum(~isnan(repeataligned{a}(:,reps)))), fliplr(nanmean(repeataligned{a}(:,reps))+nanstd(repeataligned{a}(:,reps))./sqrt(sum(~isnan(repeataligned{a}(:,reps)))))],animcol(a,:),'FaceAlpha',.3);%
set(h,'EdgeColor','none'); plot(sreps, nanmean(repeataligned{a}(:,reps)),'.-','Color',animcol(a,:),'Linewidth',2); plot([1 15],[.125 .125],'k:')
xlabel('trials since newgoal found'); ylim([0 1]); title('aligned to start of repeat phase'); ylabel('reward rate')
ntext = sprintf('n=%dconts/%deps',size(repeataligned{a},1),length(eps)); text(10,a/10,ntext,'Color',animcol(a,:));
end
%% 3. plot fraction coherent, local, salient, overall and by trial phase and taskphase [2F,3A]
clearvars -except f animals animcol
bars = figure(); set(gcf,'Position',[66 305 1853 551]);
contentthresh = .3;
for a = 1:length(animals)
eps = find(arrayfun(@(x) ~isempty(x.trips),f(a).output{1}));
for e = 1:length(eps)
tphasenum = f(a).output{1}(eps(e)).trips.taskphase;
valtrials = ~isnan(tphasenum);
tphasenum = [tphasenum(valtrials), [1:sum(valtrials)]']; % add trial numbers
homerips = f(a).output{1}(eps(e)).trips.homeripcontent(valtrials);
hometypes = f(a).output{1}(eps(e)).trips.homeripmaxtypes(valtrials);
rwrips = f(a).output{1}(eps(e)).trips.RWripcontent(valtrials);
postrwrips = f(a).output{1}(eps(e)).trips.postRWripcontent(valtrials);
rwtypes = f(a).output{1}(eps(e)).trips.RWripmaxtypes(valtrials); %
postrwtypes = f(a).output{1}(eps(e)).trips.postRWripmaxtypes(valtrials); %
outerrips = f(a).output{1}(eps(e)).trips.outerripcontent(valtrials);
outertypes = f(a).output{1}(eps(e)).trips.outerripmaxtypes(valtrials); %
goals = f(a).output{1}(eps(e)).trips.goalarm(valtrials,:);
goals(tphasenum(:,1)<=1,1) = nan; % turn currgoals during search trials into nans
goals(goals(:,1)==0,1) = nan;
outers = f(a).output{1}(eps(e)).trips.outerarm(valtrials);
pastwlock = f(a).output{1}(eps(e)).trips.prevarm(valtrials,2); % only consider the including lockout option
trialstack = [outers', pastwlock, goals,tphasenum(:,1)];
clear replays homereplays rwreplays postrwreplays outerreplays boxreplays
% salient - can be past, future, or any previously rewarded arm OR just any previously rewarded arm (p/f not salient)(as for FIG7A)
for t=1:length(homerips) % extract valid rips and tack on trial info: [replay future past currgoal prevgoal ppgoal tphase salient local]
if ~isempty(homerips{t})
[maxval,ind] = max(homerips{t},[],2); %(:,2:end)
valid = hometypes{t}'==1 & maxval>contentthresh;
%homereplays{t} =[ind(valid)-1,repmat(trialstack(t,:),sum(valid),1), ismember(ind(valid)-1,[trialstack(t,1:2),unique(goals(1:t,1))']),ind(valid)-1==0];
homereplays{t} =[ind(valid)-1,repmat(trialstack(t,:),sum(valid),1), ismember(ind(valid)-1,[unique(goals(1:t,1))']),ind(valid)-1==0];
else homereplays{t} = []; end
if ~isempty(rwrips{t})
[maxval,ind] = max(rwrips{t},[],2); %(:,2:end)
valid = rwtypes{t}'==1 & maxval>contentthresh;
%rwreplays{t} = [ind(valid)-1,repmat(trialstack(t,:),sum(valid),1), ismember(ind(valid)-1,[trialstack(t,1:2),unique(goals(1:t,1))']),ind(valid)-1==0];
rwreplays{t} = [ind(valid)-1,repmat(trialstack(t,:),sum(valid),1), ismember(ind(valid)-1,[unique(goals(1:t,1))']),ind(valid)-1==0];
else rwreplays{t} = []; end
if ~isempty(postrwrips{t})
[maxval,ind] = max(postrwrips{t},[],2); %(:,2:end)
valid = postrwtypes{t}'==1 & maxval>contentthresh;
%postrwreplays{t} = [ind(valid)-1,repmat(trialstack(t,:),sum(valid),1), ismember(ind(valid)-1,[trialstack(t,1:2),unique(goals(1:t,1))']),ind(valid)-1==0];
postrwreplays{t} = [ind(valid)-1,repmat(trialstack(t,:),sum(valid),1), ismember(ind(valid)-1,[unique(goals(1:t,1))']),ind(valid)-1==0];
else postrwreplays{t} = []; end
if ~isempty(outerrips{t}) % box is considered salient in option 1, not in option 2
[maxval,ind] = max(outerrips{t},[],2); %(:,2:end)
valid = outertypes{t}'==1 & maxval>contentthresh;
%outerreplays{t} = [ind(valid)-1,repmat(trialstack(t,:),sum(valid),1), ismember(ind(valid)-1,[0,trialstack(t,1:2),unique(goals(1:t,1))']),ind(valid)-1==trialstack(t,1)]; % include box as salient
outerreplays{t} = [ind(valid)-1,repmat(trialstack(t,:),sum(valid),1), ismember(ind(valid)-1,[unique(goals(1:t,1))']),ind(valid)-1==trialstack(t,1)]; % include box as salient
if any(valid)
outerreplays{t}(outerreplays{t}(:,9)==1,8) = 0; % correct salience to not include local events
end
else outerreplays{t} = []; end
end
allreplay = [homereplays';rwreplays'; postrwreplays'; outerreplays']; allreplay = vertcat(allreplay{:});
allhome{a}{e} = vertcat(homereplays{:}); allrw{a}{e} = vertcat(rwreplays{:}); allpostrw{a}{e} = vertcat(postrwreplays{:}); allouter{a}{e} = vertcat(outerreplays{:});
allbox{a}{e} = [homereplays';rwreplays'; postrwreplays']; allbox{a}{e} = vertcat(allbox{a}{e}{:});
fraccohall{a}(e,1) = size(allreplay,1)/size([vertcat(homerips{:});vertcat(rwrips{:});vertcat(postrwrips{:});vertcat(outerrips{:})],1);
fracremoteall{a}(e,1) = sum(allreplay(:,9)==0)/size([vertcat(homerips{:});vertcat(rwrips{:});vertcat(postrwrips{:});vertcat(outerrips{:})],1); % out of ALL detected SWRSsize(allreplay,1)
fracsalientall{a}(e,1) = sum(allreplay(:,8)==1 & allreplay(:,9)==0)/sum(allreplay(:,9)==0); % out of nonlocal events
fraccohbyphase{a}(e,:) = [size(allhome{a}{e},1)/size(vertcat(homerips{:}),1),size(allrw{a}{e},1)/size(vertcat(rwrips{:}),1), ...
size(allpostrw{a}{e},1)/size(vertcat(postrwrips{:}),1),size(allouter{a}{e},1)/size(vertcat(outerrips{:}),1)];
fracremotebyphase{a}(e,:) = [sum(allhome{a}{e}(:,9)==0)/size(allhome{a}{e},1),sum(allrw{a}{e}(:,9)==0)/size(allrw{a}{e},1),sum(allpostrw{a}{e}(:,9)==0)/size(allpostrw{a}{e},1),sum(allouter{a}{e}(:,9)==0)/size(allouter{a}{e},1)];
fracsalientbyphase{a}(e,:) = [sum(allhome{a}{e}(:,8)==1 & allhome{a}{e}(:,9)==0)/sum(allhome{a}{e}(:,9)==0),sum(allrw{a}{e}(:,8)==1 & allrw{a}{e}(:,9)==0)/sum(allrw{a}{e}(:,9)==0), ...
sum(allpostrw{a}{e}(:,8)==1 & allpostrw{a}{e}(:,9)==0)/sum(allpostrw{a}{e}(:,9)==0),sum(allouter{a}{e}(:,8)==1 & allouter{a}{e}(:,9)==0)/sum(allouter{a}{e}(:,9)==0)]; % out of nonlocal events
fraccohboxouter{a}(e,:) = [size(allbox{a}{e},1)/size([vertcat(homerips{:});vertcat(rwrips{:});vertcat(postrwrips{:})],1),size(allouter{a}{e},1)/size(vertcat(outerrips{:}),1)];
fracremoteboxouter{a}(e,:) = [sum(allbox{a}{e}(:,9)==0)/size(allbox{a}{e},1),sum(allouter{a}{e}(:,9)==0)/size(allouter{a}{e},1)];
fracsalientboxouter{a}(e,:) = [sum(allbox{a}{e}(:,8)==1 & allbox{a}{e}(:,9)==0)/sum(allbox{a}{e}(:,9)==0),sum(allouter{a}{e}(:,8)==1 & allouter{a}{e}(:,9)==0)/sum(allouter{a}{e}(:,9)==0)]; % out of nonlocal events
searchonly_fracNONsalientboxouter{a}(e,1) = sum(allbox{a}{e}(:,7)<1 & allbox{a}{e}(:,8)==0 & allbox{a}{e}(:,9)==0)/sum(allbox{a}{e}(:,7)<1 & allbox{a}{e}(:,9)==0); % out of nonlocal events
%subplot(221); hold on; plot(histcounts(allhome{a}{e}(:,1),[0:9],'Normalization','probability'),'.'); title([animals{a} 'home'])
%subplot(222); hold on; plot(histcounts(allrw{a}{e}(:,1),[0:9],'Normalization','probability'),'.'); title('rw')
%subplot(223); hold on; plot(histcounts(allpostrw{a}{e}(:,1),[0:9],'Normalization','probability'),'.'); title('postrw')
%subplot(224); hold on; plot(histcounts(allouter{a}{e}(:,1),[0:9],'Normalization','probability'),'.'); title('outer')
end
%boxfracs{a} = mean(cell2mat(cellfun(@(x) [sum(x(:,9)==1);sum(x(:,9)==0 & x(:,8)==1);sum(x(:,9)==0 & x(:,8)==0)]./size(x,1),allbox{a},'un',0)),2);
%fracs = cell2mat(cellfun(@(x) [sum(x(:,9)==1);sum(x(:,1)==x(:,5));sum(x(:,1)~=x(:,5) & x(:,8)==1); sum(x(:,9)==0 & x(:,8)==0)]./size(x,1),allbox{a},'un',0));
%subplot(2,1,1); %pie(mean(fracs,2),{'local','prevgoal','salient nonprevgoal','nonsalient'});
%bar(a,mean(fracs,2),'stacked');title([animals{a} 'box'])
% for outer need to isolate box (is considered salient). local is not salient
%outerfracs{a} = mean(cell2mat(cellfun(@(x) [sum(x(:,9)==1);sum(x(:,1)==0); sum(x(:,1)~=0 & x(:,8)==1); sum(x(:,9)==0 & x(:,8)==0 & x(:,1)~=0)]./size(x,1),allouter{a},'un',0)),2);
%outerfracs{a} = mean(cell2mat(cellfun(@(x) [sum(x(:,9)==1);sum(x(:,1)==0); sum(x(:,1)==x(:,5)); sum(x(:,1)~=x(:,5) & x(:,8)==1 & x(:,1)~=0); sum(x(:,8)==0 & x(:,9)==0 & x(:,1)~=0)]./size(x,1),allouter{a},'un',0));
%subplot(2,4,a+4); pie(mean(fracs,2),{'local','box','salient nonbox','nonsalient'}); title('outer')
% total num box local/remote + outer local/box/remote arm pie
totalnums{a} = [sum(cellfun(@(x) sum(x(:,9)==1),allbox{a})), sum(cellfun(@(x) sum(x(:,9)==0),allbox{a})), ...
sum(cellfun(@(x) sum(x(:,9)==1),allouter{a})), sum(cellfun(@(x) sum(x(:,1)==0),allouter{a})),sum(cellfun(@(x) sum(x(:,9)==0 & x(:,1)>0),allouter{a}))] ...
./(sum(cellfun(@(x) size(x,1),allbox{a}))+sum(cellfun(@(x) size(x,1),allouter{a})));
subplot(1,4,a); pie(totalnums{a}); text(0,-1.5,['n=',num2str(sum(cellfun(@(x) size(x,1),allbox{a}))+sum(cellfun(@(x) size(x,1),allouter{a})))]);
title(animals{a})
end
legend({'box local','box remote','outer local','outer box','outer remotearm'})
%subplot(1,2,1); bar(horzcat(boxfracs{:})','stacked'); title('box replays'); legend({'local','salient','nonsalient'}); ylim([0 1]); ylabel('fraction of coherent SWRs')
%subplot(1,2,2); bar(horzcat(outerfracs{:})','stacked'); title('outer replays'); legend({'local','box','salient','nonsalient'}); xlabel('subject')
figure;
subplot(3,3,1); hold on; plot4a(fraccohall,'gnames',{'coherent'}); ylim([0 1]); title('out of all swrs'); ylabel('frac of all events')
subplot(3,3,2); hold on; plot4a(fracremoteall,'gnames',{'remote'}); ylim([0 1]); title('out of ALL SWRs')
text([6:9],[.6 .7 .8 .9],num2str(cellfun(@(x) mean(x),fracremoteall)','%.04f')); text([7:10],[.6 .7 .8 .9],num2str(cellfun(@length,fracremoteall)'))
subplot(3,3,3); hold on; plot4a(fracsalientall,'gnames',{'salient'}); ylim([0 1]); title('out of all remote')
subplot(3,3,4); hold on; plot4a(fraccohbyphase,'gnames',{'home','rw','postrw','outer'}); ylim([0 1]); title('coherent'); ylabel('frac of all events')
subplot(3,3,5); hold on; plot4a(fracremotebyphase,'gnames',{'home','rw','postrw','outer'}); ylim([0 1]); title('remote out of all coh')
subplot(3,3,6); hold on; plot4a(fracsalientbyphase,'gnames',{'home','rw','postrw','outer'}); ylim([0 1]); title('salient')
subplot(3,3,7); hold on; plot4a(fraccohboxouter,'gnames',{'box','outer'}); ylim([0 1]); title('coherent'); ylabel('frac of all events')
subplot(3,3,8); hold on; plot4a(fracremoteboxouter,'gnames',{'box','outer'}); ylim([0 1]); title('remote out of all coh')
subplot(3,3,9); hold on; plot4a(searchonly_fracNONsalientboxouter,'gnames',{'box'}); ylim([0 1]); title('NOT salient(prevrewarded) of out of all rem')
%% 4. boxplot version of violin plot with epoch means of replay fractions by behavioral category [HOME / RW] [3B,C]
clearvars -except f animals animcol
bars = figure(); set(gcf,'Position',[66 305 1853 551]);
reps = 100;
mintrials = 5;
contentthresh = .3;
for a = 1:length(animals)
%figure; set(gcf,'Position',[1008 1009 663 833]);
eps = find(arrayfun(@(x) ~isempty(x.trips),f(a).output{1}));
for e = 1:length(eps)
tphasenum = f(a).output{1}(eps(e)).trips.taskphase;
valtrials = ~isnan(tphasenum);
tphasenum = [tphasenum(valtrials), [1:sum(valtrials)]']; % add trial numbers
homerips = f(a).output{1}(eps(e)).trips.homeripcontent(valtrials);
hometypes = f(a).output{1}(eps(e)).trips.homeripmaxtypes(valtrials);
rwrips = f(a).output{1}(eps(e)).trips.RWripcontent(valtrials);
postrwrips = f(a).output{1}(eps(e)).trips.postRWripcontent(valtrials);
rwtypes = f(a).output{1}(eps(e)).trips.RWripmaxtypes(valtrials); %
postrwtypes = f(a).output{1}(eps(e)).trips.postRWripmaxtypes(valtrials); %
rips = cellfun(@(x,y,z) [x;y;z],homerips,rwrips,postrwrips,'un',0);
types = cellfun(@(x,y,z) [x,y,z]',hometypes,rwtypes,postrwtypes,'un',0);
clear replays
for t=1:length(rips)
if ~isempty(rips{t})
[maxval,ind] = max(rips{t},[],2); %(:,2:end)
valid = types{t}==1 & maxval>contentthresh;
replays{t} = ind(valid)'-1; %
else replays{t} = []; end
end
goals = f(a).output{1}(eps(e)).trips.goalarm(valtrials,:);
outers = f(a).output{1}(eps(e)).trips.outerarm(valtrials);
pastwlock = f(a).output{1}(eps(e)).trips.prevarm(valtrials,2); % only consider the including lockout option
valtrials = ~cellfun(@isempty, replays);
if any(valtrials)
ripcomb = cellfun(@(x,y,z,g,h) [x', repmat([y,z,g,h],length(x),1)], replays(valtrials), num2cell(outers(valtrials)), ...
num2cell(pastwlock(valtrials))', num2cell(goals(valtrials,:),2)',num2cell(tphasenum(valtrials,:),2)','un',0);
ripcomb = vertcat(ripcomb{:});
salient = ripcomb(:,1)>0 & (ripcomb(:,1)==ripcomb(:,2) | ripcomb(:,1)==ripcomb(:,3) | table2array(rowfun(@(x) ismember(x(1),goals(1:x(8),1)),table(ripcomb))));
contentfracs{a}(e,:) = [length(vertcat(rips{:}))-size(ripcomb,1), sum(ripcomb(:,1)==0), sum(salient), sum(ripcomb(:,1)>0 & ~salient)]./length(vertcat(rips{:}));
% all ripples during search trials; f vs past vs pgoal
search = ripcomb((ripcomb(:,7) <= 1 & ~isnan(ripcomb(:,5))),:); %~isnan(abovethresh(:,4)) & & ~isnan(abovethresh(:,6))
alluniqueinds = table2array(rowfun(@(x) length(unique(x([2,3,5])))==3,table(search)));
allunique = search(alluniqueinds,:); %
if length(unique(allunique(:,8)))>=1
search_future{a}(e) = sum(allunique(:,1)==allunique(:,2))/sum(allunique(:,1)>0);
search_past{a}(e) = sum(allunique(:,1)==allunique(:,3))/sum(allunique(:,1)>0);
search_prevgoal{a}(e) = sum(allunique(:,1)==allunique(:,5))/sum(allunique(:,1)>0);
search_other{a}(e) = sum(allunique(:,1)>0 & allunique(:,1)~=allunique(:,2) & allunique(:,1)~=allunique(:,3) & allunique(:,1)~=allunique(:,5))/5/sum(allunique(:,1)>0);
search_totalother{a}(e) = sum(allunique(:,1)>0 & allunique(:,1)~=allunique(:,2) & allunique(:,1)~=allunique(:,3) & allunique(:,1)~=allunique(:,5))/sum(allunique(:,1)>0);
for r = 1:reps
randlist = randi([1 8],size(allunique,1),1);
search_randshuff{a}(e,r) = sum(allunique(:,1)==randlist)/sum(allunique(:,1)>0);
end
end
search_rtcounts{a}(e,:) = [size(allunique,1), length(unique(allunique(:,8))), sum(tphasenum(:,1)<1)]; % #rips #trials #totalsearchtrials
% all ripples during repeat trials; future==past==goal vs pg
repeat = ripcomb((ripcomb(:,7) > 1 & ~isnan(ripcomb(:,5))),:); %~isnan(abovethresh(:,4)) & & ~isnan(abovethresh(:,6))
alluniqueinds = table2array(rowfun(@(x) length(unique(x([2,3,4])))==1,table(repeat))) & repeat(:,5)~=repeat(:,2);
allunique = repeat(alluniqueinds,:); %
if length(unique(allunique(:,8)))>=1
rep1_pfg{a}(e) = sum(allunique(:,1)==allunique(:,2))/sum(allunique(:,1)>0);
rep1_prevgoal{a}(e) = sum(allunique(:,1)==allunique(:,5))/sum(allunique(:,1)>0);
rep1_other{a}(e) = sum(allunique(:,1)>0 & allunique(:,1)~=allunique(:,2) & allunique(:,1)~=allunique(:,5))/6/sum(allunique(:,1)>0);
rep1_totalother{a}(e) = sum(allunique(:,1)>0 & allunique(:,1)~=allunique(:,2) & allunique(:,1)~=allunique(:,5))/sum(allunique(:,1)>0);
for r = 1:reps
randlist = randi([1 8],size(allunique,1),1);
rep1_randshuff{a}(e,r) = sum(allunique(:,1)==randlist)/sum(allunique(:,1)>0);
end
end
rep1_rtcounts{a}(e,:) = [size(allunique,1), length(unique(allunique(:,8))), sum(tphasenum(:,1)>=1)]; % #rips #trials #totalreptrials
end
end
% past future cg pg
searchcomb{a} = [search_past{a}(search_rtcounts{a}(:,2)>=mintrials)' search_future{a}(search_rtcounts{a}(:,2)>=mintrials)' search_prevgoal{a}(search_rtcounts{a}(:,2)>=mintrials)' search_other{a}(search_rtcounts{a}(:,2)>=mintrials)' search_totalother{a}(search_rtcounts{a}(:,2)>=mintrials)'];
searchshuffcomb{a} = reshape(search_randshuff{a}(search_rtcounts{a}(:,2)>=mintrials,:),[],1);
repcomb{a} = [rep1_pfg{a}(rep1_rtcounts{a}(:,2)>=mintrials)' rep1_prevgoal{a}(rep1_rtcounts{a}(:,2)>=mintrials)' rep1_other{a}(rep1_rtcounts{a}(:,2)>=mintrials)' rep1_totalother{a}(rep1_rtcounts{a}(:,2)>=mintrials)'];
repshuffcomb{a} = reshape(rep1_randshuff{a}(rep1_rtcounts{a}(:,2)>=mintrials,:),[],1);
search_p{a} = [ranksum(searchcomb{a}(:,1),searchshuffcomb{a}),ranksum(searchcomb{a}(:,2),searchshuffcomb{a}),ranksum(searchcomb{a}(:,3),searchshuffcomb{a}),ranksum(searchcomb{a}(:,4),searchshuffcomb{a})];
rep_p{a} = [ranksum(repcomb{a}(:,1),repshuffcomb{a}),ranksum(repcomb{a}(:,2),repshuffcomb{a}),ranksum(repcomb{a}(:,3),repshuffcomb{a})];
end
subplot(1,8,1); ylabel('fraction of remote replay')
plot4a(searchshuffcomb,'gnames',{'null'}); ylim([0 .5]); text(6,.4,num2str(cellfun(@(x) sum(x(:,2)>=mintrials),search_rtcounts)))
subplot(1,8,[2 3 ]); hold on; title('search')
plot4a(searchcomb,'gnames',{'past','future','pg','other'}); xlim([5 30])
text(8,.45,num2str(vertcat(search_p{:}))); plot([1 40],[.125 .125],'k:'); ylim([0 .5])
subplot(1,8,4); hold on; plot4a(searchcomb,'gnames',{'past','future','pg','other','totalother'}); xlim([30 35]); ylim([0 1])
subplot(1,8,5);
plot4a(repshuffcomb,'gnames',{'null'}); ylim([0 .5]); text(6,.4,num2str(cellfun(@(x) sum(x(:,2)>=mintrials),rep1_rtcounts)))
subplot(1,8,[6 7]); hold on; title('repeat')
plot4a(repcomb,'gnames',{'pfg','pg','other'}); xlim([5 24])
plot([1 40],[.125 .125],'k:'); ylim([0 .5])
text(8,.45,num2str(vertcat(rep_p{:}))); xlabel(['mintrials=' num2str(mintrials)]);ylabel('fraction of remote replay')
subplot(1,8,8); hold on; plot4a(repcomb,'gnames',{'pfg','pg','other','totalother'}); xlim([24 29]); ylim([0 1])
% pooled
figure; set(gcf,'Position',[66 305 1853 551]);
subplot(1,8,1); ylabel('fraction of remote replay')
plot4a(searchshuffcomb,'gnames',{'null'},'pooled',1); ylim([0 .5]); xlim([8.25 8.75])
text(8.5,.4,num2str(sum(cellfun(@(x) sum(x(:,2)>=mintrials),search_rtcounts))))
subplot(1,8,[2 3 ]); hold on; title('search')
plot4a(searchcomb,'gnames',{'past','future','pg','other'},'pooled',1); xlim([5 30]);
allsearchcomb = vertcat(searchcomb{:}); allrepcomb = vertcat(repcomb{:});
text(8,.45,num2str([ranksum(vertcat(searchshuffcomb{:}),allsearchcomb(:,1)), ranksum(vertcat(searchshuffcomb{:}),allsearchcomb(:,2)), ranksum(vertcat(searchshuffcomb{:}),allsearchcomb(:,3)), ranksum(vertcat(searchshuffcomb{:}),allsearchcomb(:,4))]));
plot([1 40],[.125 .125],'k:'); ylim([0 .5])
subplot(1,8,4); hold on; plot4a(searchcomb,'gnames',{'past','future','pg','other','totalother'},'pooled',1); xlim([30 35]); ylim([0 1])
subplot(1,8,5);
plot4a(repshuffcomb,'gnames',{'null'},'pooled',1); ylim([0 .5]); xlim([8.25 8.75])
text(8.5,.4,num2str(sum(cellfun(@(x) sum(x(:,2)>=mintrials),rep1_rtcounts))))
subplot(1,8,[6 7]); hold on; title('repeat')
plot4a(repcomb,'gnames',{'pfg','pg','other'},'pooled',1); xlim([5 24])
plot([1 40],[.125 .125],'k:'); ylim([0 .5])
text(8,.45,num2str([ranksum(vertcat(repshuffcomb{:}),allrepcomb(:,1)),ranksum(vertcat(repshuffcomb{:}),allrepcomb(:,2)),ranksum(vertcat(repshuffcomb{:}),allrepcomb(:,3))]));
xlabel(['mintrials=' num2str(mintrials)]);ylabel('fraction of remote replay')
subplot(1,8,8); hold on; plot4a(repcomb,'gnames',{'pfg','pg','other','totalother'},'pooled',1); xlim([24 29]); ylim([0 1])
%% 5. fit linear model of category predictors for each arm, for box and outer [FIG 3D,E and FIG 4 A,B and FIG 8B]
clearvars -except f animals animcol
contentthresh = .3;
all = figure(); byarm = figure(); cov = figure(); set(gcf,'Position',[90 262 1822 697]); poold = figure();
search_pooled = []; repeat_pooled = [];
for a = 1:length(animals)
eps = find(arrayfun(@(x) ~isempty(x.trips),f(a).output{1}));
for e = 1:length(eps)
tphasenum = f(a).output{1}(eps(e)).trips.taskphase;
valtrials = ~isnan(tphasenum);
tphasenum = [tphasenum(valtrials), [1:sum(valtrials)]']; % add trial numbers
homerips = f(a).output{1}(eps(e)).trips.homeripcontent(valtrials);
hometypes = f(a).output{1}(eps(e)).trips.homeripmaxtypes(valtrials);
rwrips = f(a).output{1}(eps(e)).trips.RWripcontent(valtrials);
postrwrips = f(a).output{1}(eps(e)).trips.postRWripcontent(valtrials);
rwtypes = f(a).output{1}(eps(e)).trips.RWripmaxtypes(valtrials); %
postrwtypes = f(a).output{1}(eps(e)).trips.postRWripmaxtypes(valtrials); %
rips = cellfun(@(x,y,z) [x;y;z],homerips,rwrips,postrwrips,'un',0);
types = cellfun(@(x,y,z) [x,y,z]',hometypes,rwtypes,postrwtypes,'un',0);
outerrips = f(a).output{1}(eps(e)).trips.outerripcontent(valtrials);
outertypes = f(a).output{1}(eps(e)).trips.outerripmaxtypes(valtrials);
clear replays outerreplays
for t=1:length(rips)
if ~isempty(rips{t})
[maxval,ind] = max(rips{t},[],2); %(:,2:end)
valid = types{t}==1 & maxval>contentthresh;
replays{t} = ind(valid)'-1; %
else replays{t} = []; end
if ~isempty(outerrips{t})
[maxval,ind] = max(outerrips{t},[],2); %(:,2:end)
valid = outertypes{t}'==1 & maxval>contentthresh;
outerreplays{t} = ind(valid)'-1; %
else outerreplays{t} = []; end
end
goals = f(a).output{1}(eps(e)).trips.goalarm(valtrials,:);
outers = f(a).output{1}(eps(e)).trips.outerarm(valtrials);
pastwlock = f(a).output{1}(eps(e)).trips.prevarm(valtrials,2); % only consider the including lockout option
countspertrial = zeros(8,length(outers));
countspertrial(:,~cellfun(@isempty,replays)) = cell2mat(cellfun(@(x) histcounts(x,[1:9])',replays(~cellfun(@isempty,replays)),'un',0));
future = cell2mat(cellfun(@(x) histcounts(x,[1:9])',num2cell(outers),'un',0));
past = cell2mat(cellfun(@(x) histcounts(x,[1:9])',num2cell(pastwlock'),'un',0));
prevgoal = cell2mat(cellfun(@(x) histcounts(x,[1:9])',num2cell(goals(:,2)'),'un',0));
currgoal = cell2mat(cellfun(@(x) histcounts(x,[1:9])',num2cell(goals(:,1)'),'un',0));
allsearch{a}{e} = [reshape(future(:,tphasenum(:,1)<=1),[],1),reshape(past(:,tphasenum(:,1)<=1),[],1),reshape(prevgoal(:,tphasenum(:,1)<=1),[],1) ...
reshape(countspertrial(:,tphasenum(:,1)<=1),[],1)]; % [future past prevgoal #replays];
allrepeat{a}{e} = [reshape(future(:,tphasenum(:,1)>1),[],1),reshape(past(:,tphasenum(:,1)>1),[],1),reshape(currgoal(:,tphasenum(:,1)>1),[],1) ...
,reshape(prevgoal(:,tphasenum(:,1)>1),[],1), reshape(countspertrial(:,tphasenum(:,1)>1),[],1)]; % [future past currgoal prevgoal #replays];
earlycorr = tphasenum(:,1)>1 & tphasenum(:,1)<5 & outers'==goals(:,1);
earlycorrrepeat{a}{e} = [reshape(future(:,earlycorr),[],1),reshape(past(:,earlycorr),[],1),reshape(currgoal(:,earlycorr),[],1) ...
,reshape(prevgoal(:,earlycorr),[],1), reshape(countspertrial(:,earlycorr),[],1)]; % [future past currgoal prevgoal #replays];
latecorr = tphasenum(:,1)>4 & outers'==goals(:,1);
latecorrrepeat{a}{e} = [reshape(future(:,latecorr ),[],1),reshape(past(:,latecorr ),[],1),reshape(currgoal(:,latecorr ),[],1) ...
,reshape(prevgoal(:,latecorr ),[],1), reshape(countspertrial(:,latecorr ),[],1)]; % [future past currgoal prevgoal #replays];
earlyerr = tphasenum(:,1)>1 & tphasenum(:,1)<5 & outers'~=goals(:,1);
earlyerrrepeat{a}{e} = [reshape(future(:,earlyerr),[],1),reshape(past(:,earlyerr),[],1),reshape(currgoal(:,earlyerr),[],1) ...
,reshape(prevgoal(:,earlyerr),[],1), reshape(countspertrial(:,earlyerr),[],1)]; % [future past currgoal prevgoal #replays];
lateerr = tphasenum(:,1)>4 & outers'~=goals(:,1);
lateerrrepeat{a}{e} = [reshape(future(:,lateerr ),[],1),reshape(past(:,lateerr ),[],1),reshape(currgoal(:,lateerr ),[],1) ...
,reshape(prevgoal(:,lateerr ),[],1), reshape(countspertrial(:,lateerr ),[],1)]; % [future past currgoal prevgoal #replays];
for arm = 1:8
searchbyarm{a}{arm}{e} = [future(arm,tphasenum(:,1)<=1)',past(arm,tphasenum(:,1)<=1)',prevgoal(arm,tphasenum(:,1)<=1)',countspertrial(arm,tphasenum(:,1)<=1)'];
repeatbyarm{a}{arm}{e} = [future(arm,tphasenum(:,1)>1)',past(arm,tphasenum(:,1)>1)',currgoal(arm,tphasenum(:,1)>1)',prevgoal(arm,tphasenum(:,1)>1)',countspertrial(arm,tphasenum(:,1)>1)'];
end
outercountspertrial = zeros(8,length(outers));
outercountspertrial(:,~cellfun(@isempty,outerreplays)) = cell2mat(cellfun(@(x) histcounts(x,[1:9])',outerreplays(~cellfun(@isempty,outerreplays)),'un',0));
rewd = mod(tphasenum(:,1),1)==0 & tphasenum(:,1)>0;
allouterrew{a}{e} = [reshape(past(:,rewd),[],1),reshape(currgoal(:,rewd),[],1), ...
reshape(prevgoal(:,rewd),[],1), reshape(outercountspertrial(:,rewd),[],1)]; % [ past current prevgoal #replays];
end
figure(all);
searchcat = vertcat(allsearch{a}{:});
searchtbl = table(searchcat(:,2),searchcat(:,1),searchcat(:,3),searchcat(:,4),'VariableNames',{'past','future','prevgoal','replaynum'});
s_mdl = fitglm(searchtbl,'linear','Distribution','poisson');
CI = coefCI(s_mdl,.01);
subplot(1,3,1); hold on; title('allsearch')
%errorbar(a+[0:5:19],table2array(mdl.Coefficients(:,1)), table2array(mdl.Coefficients(:,2)),'.','Color',animcol(a,:))
plot(a+[0:5:19],exp(table2array(s_mdl.Coefficients(:,1))),'.','MarkerSize',20,'Color',animcol(a,:));
plot([a+[0:5:19];a+[0:5:19]],exp(CI)','Color',animcol(a,:));
%cr = [corrcoef(searchcat(:,1),searchcat(:,2)), corrcoef(searchcat(:,1),searchcat(:,3)), corrcoef(searchcat(:,2),searchcat(:,3))];
%searchr2(a, :)=cr(1,[2 4 6]).^2;
text(5,2+.2*a,['trial n=',num2str(size(searchcat,1)/8)],'Color',animcol(a,:));
repeatcat = vertcat(allrepeat{a}{:});
reptbl = table(repeatcat(:,2),repeatcat(:,1),repeatcat(:,3),repeatcat(:,4),repeatcat(:,5),'VariableNames',{'past','future','currgoal','prevgoal','replaynum'});
r_mdl = fitglm(reptbl,'linear','Distribution','poisson');
CI = coefCI(r_mdl,.01);
subplot(1,3,2); hold on; title('allrepeat')
%errorbar(a+[0:5:24],table2array(mdl.Coefficients(:,1)), table2array(mdl.Coefficients(:,2)),'.','Color',animcol(a,:))
plot(a+[0:5:24],exp(table2array(r_mdl.Coefficients(:,1))),'.','MarkerSize',20,'Color',animcol(a,:));
plot([a+[0:5:24];a+[0:5:24]],exp(CI)','Color',animcol(a,:));
%cr = [corrcoef(repeatcat(:,1),repeatcat(:,2)), corrcoef(repeatcat(:,1),repeatcat(:,3)),corrcoef(repeatcat(:,1),repeatcat(:,4)), ...
% corrcoef(repeatcat(:,2),repeatcat(:,3)), corrcoef(repeatcat(:,2),repeatcat(:,4)),corrcoef(repeatcat(:,3),repeatcat(:,4))];
%repr2(a,:)=cr(1,[2:2:12]).^2;
%text(5,2+.2*a,['f vs cg corrcoef=',num2str(cr(2),'%.03f')],'Color',animcol(a,:));
text(5,2+.2*a,['trial n=',num2str(size(repeatcat,1)/8)],'Color',animcol(a,:));
outercat = vertcat(allouterrew{a}{:});
mdl = fitglm(outercat(:,1:3),outercat(:,4),'linear','Distribution','poisson');
CI = coefCI(mdl,.01);
subplot(1,3,3); hold on; title('outer rewarded')
plot(a+[0:5:19],exp(table2array(mdl.Coefficients(:,1))),'.','MarkerSize',20,'Color',animcol(a,:));
plot([a+[0:5:19];a+[0:5:19]],exp(CI)','Color',animcol(a,:));
text(5,2+.2*a,['trial n=',num2str(size(outercat,1)/8)],'Color',animcol(a,:));
figure(cov);
% note that predictors haven't be reordered to match main figs (future past cg pg)
subplot(2,4,a); imagesc(1:3,1:3,corrcov(s_mdl.CoefficientCovariance(2:end,2:end))); caxis([-1 1])
set(gca,'ytick',1:3,'xticklabel',{'pst','fut','pg'},'yticklabel',{'fut','pst','pg'}); colorbar; title('search')
nums = sprintfc('%.02f',corrcov(s_mdl.CoefficientCovariance(2:end,2:end)));
[x,y] = meshgrid(1:3,1:3); text(x(:),y(:),nums,'horizontalalignment','center','verticalalignment','middle')
subplot(2,4,a+4); imagesc(corrcov(r_mdl.CoefficientCovariance(2:end,2:end))); caxis([-1 1])
colorbar; set(gca,'xticklabel',{'pst','fut','cg','pg'},'yticklabel',{'fut','pst','cg','pg'}); title('rep')
nums = sprintfc('%.02f',corrcov(r_mdl.CoefficientCovariance(2:end,2:end)));
[x,y] = meshgrid(1:4,1:4); text(x(:),y(:),nums,'horizontalalignment','center','verticalalignment','middle')
search_pooled = [search_pooled;searchcat]; repeat_pooled = [repeat_pooled;repeatcat];
figure(byarm);
% subplot(2,2,[1:2]); hold on;
% reptbl = table(repeatcat(:,1),repeatcat(:,2),repeatcat(:,3),repeatcat(:,4),repeatcat(:,5),'VariableNames',{'future','past','currgoal','prevgoal','replaynum'});
% modelspec = 'replaynum ~ future + past +currgoal + prevgoal + future:currgoal'; %
% mdl = fitglm(reptbl,modelspec,'Distribution','poisson');
% coefinds = 1:6;
% plot(a/5+coefinds(table2array(mdl.Coefficients(:,4))<.05),ones(sum(table2array(mdl.Coefficients(:,4))<.05),1),'*','Color',animcol(a,:));
% plot(a/5+coefinds(table2array(mdl.Coefficients(:,4))>=.05),zeros(sum(table2array(mdl.Coefficients(:,4))>=.05),1),'o','Color',animcol(a,:));
% set(gca,'XTickLabel',{'int','future','past','currg','pg','f+cg'})
% subplot(2,2,3); hold on;
% [sims,simCI] = predict(mdl,[1 0 1 0; 1 0 0 0; 0 0 1 0; 0 0 0 0]);
% plot(a/5+[1:4],sims, 'ko'); plot(a/5+[1:4;1:4],simCI','Color',animcol(a,:)); set(gca,'XTickLabel',{'f+ cg+','f+ cg-','f- cg+','f-cg-'})
%subplot(2,2,4); hold on;
%[sims,simCI] = predict(mdl,[1 0 1; 1 0 0; 0 0 1; 0 0 0]);
%plot(a/5+[1:4],sims, 'ko'); plot(a/5+[1:4;1:4],simCI','Color',animcol(a,:)); set(gca,'XTickLabel',{'f+ pg+','f+ pg-','f- pg+','f-pg-'})
%
% reptbl = table(repeatcat(:,1),repeatcat(:,2),repeatcat(:,3),repeatcat(:,4),repeatcat(:,5),'VariableNames',{'future','past','currgoal','prevgoal','replaynum'});
% modelspec = 'replaynum ~ future + past + currgoal + prevgoal + future:currgoal + future:past+ future:prevgoal + past:currgoal +past:prevgoal'; %
% mdl = fitglm(reptbl,modelspec,'Distribution','poisson');
% coefinds = 1:10;
% plot(a/5+coefinds(table2array(mdl.Coefficients(:,4))<.05),ones(sum(table2array(mdl.Coefficients(:,4))<.05),1),'*','Color',animcol(a,:));
% plot(a/5+coefinds(table2array(mdl.Coefficients(:,4))>=.05),zeros(sum(table2array(mdl.Coefficients(:,4))>=.05),1),'o','Color',animcol(a,:));
% set(gca,'XTickLabel',{'int','future','past','currg','pg','f+p','f+cg','p+cg','f+pg','p+pg'})
% subplot(3,1,2); hold on;
% modelspec = 'replaynum ~ future + past + currgoal + prevgoal + future:currgoal + future:past';%
% mdl = fitglm(reptbl,modelspec,'Distribution','poisson');
% coefinds = 1:7;
% plot(a/5+coefinds(table2array(mdl.Coefficients(:,4))<.05),ones(sum(table2array(mdl.Coefficients(:,4))<.05),1),'*','Color',animcol(a,:));
% plot(a/5+coefinds(table2array(mdl.Coefficients(:,4))>=.05),zeros(sum(table2array(mdl.Coefficients(:,4))>=.05),1),'o','Color',animcol(a,:));
% set(gca,'XTickLabel',{'int','future','past','currg','pg','f+p','f+cg'})
% subplot(3,1,3); hold on;
% modelspec = 'replaynum ~ future + past + currgoal + prevgoal + future:currgoal ';%
% mdl = fitglm(reptbl,modelspec,'Distribution','poisson');
% coefinds = 1:6;
% plot(a/5+coefinds(table2array(mdl.Coefficients(:,4))<.05),ones(sum(table2array(mdl.Coefficients(:,4))<.05),1),'*','Color',animcol(a,:));
% plot(a/5+coefinds(table2array(mdl.Coefficients(:,4))>=.05),zeros(sum(table2array(mdl.Coefficients(:,4))>=.05),1),'o','Color',animcol(a,:));
% set(gca,'XTickLabel',{'int','future','past','currg','pg','f+cg'})
% [sims,simCI] = predict(mdl,[1 0 1 0; 1 0 0 0; 0 0 1 0; 0 0 0 0]);
% plot(1:4,sims, '.'); plot([1:4;1:4],simCI','k'); set(gca,'XTickLabel',{'f+ cg+','f+ cg-','f- cg+','f-cg-'})
% subplot(1,2,2); hold on;
% [sims,simCI] = predict(mdl,[1 1 1 0; 1 0 1 0; 0 1 1 0; 0 0 1 0]);
% plot(1:4,sims, '.'); plot([1:4;1:4],simCI','k'); set(gca,'XTickLabel',{'f+ p+','f+ p-','f- p+','f-p-'})
% mdl2 = step(mdl,'NSteps',5)
% plotSlice(mdl)
% plotInteraction(mdl,'future','past')
% CI = coefCI(mdl,.01);
% plot(a+[0:5:29],exp(table2array(mdl.Coefficients(:,1))),'.','MarkerSize',20,'Color',animcol(a,:));
% plot([a+[0:5:29];a+[0:5:29]],exp(CI)','Color',animcol(a,:)); %title('repeat f cg diff')
% OLD FIG S5
% subplot(1,2,1); hold on;
% fcgdiffcat = vertcat(fcgdiffrepeat{a}{:}); fcgsamecat = vertcat(fcgsamerepeat{a}{:});
% mdl = fitglm(fcgdiffcat(:,[2 1 3 4]),fcgdiffcat(:,5),'linear','Distribution','poisson');
% CI = coefCI(mdl,.01);
% plot(a+[0:5:24],exp(table2array(mdl.Coefficients(:,1))),'.','MarkerSize',20,'Color',animcol(a,:));
% plot([a+[0:5:24];a+[0:5:24]],exp(CI)','Color',animcol(a,:)); title('repeat f cg diff')
% text(5,2+.2*a,['n=',num2str(size(fcgdiffcat,1))],'Color',animcol(a,:));
% subplot(1,2,2); hold on;
% % Note that each time you subsample fcgsame, you will get a slightly different result
% %subset = randi(size(fcgsamecat,1),size(fcgdiffcat,1),1);
% %mdl = fitglm(fcgsamecat(subset,[2:4]),fcgsamecat(subset,5),'linear','Distribution','poisson');
% mdl = fitglm(fcgsamecat(:,[2:4]),fcgsamecat(:,5),'linear','Distribution','poisson');
% CI = coefCI(mdl,.01);
% plot(a+[0:5:19],exp(table2array(mdl.Coefficients(:,1))),'.','MarkerSize',20,'Color',animcol(a,:));
% plot([a+[0:5:19];a+[0:5:19]],exp(CI)','Color',animcol(a,:)); title('repeat f cg same')
% text(5,2+.2*a,['n=',num2str(size(fcgsamecat,1))],'Color',animcol(a,:));
% %text(5,2+.2*a,['n=',num2str(size(fcgsamecat(subset,:),1))],'Color',animcol(a,:));
subplot(3,2,1); hold on;
earlycorr = vertcat(earlycorrrepeat{a}{:}); latecorr = vertcat(latecorrrepeat{a}{:});
earlyerr = vertcat(earlyerrrepeat{a}{:}); lateerr = vertcat(lateerrrepeat{a}{:});
mdl = fitglm([earlycorr(:,[2 1 3 4]);earlyerr(:,[2 1 3 4])],[earlycorr(:,5);earlyerr(:,5)],'linear','Distribution','poisson');
CI = coefCI(mdl,.01);
plot(a+[0:5:24],exp(table2array(mdl.Coefficients(:,1))),'.','MarkerSize',20,'Color',animcol(a,:));
plot([a+[0:5:24];a+[0:5:24]],exp(CI)','Color',animcol(a,:)); title('repeat early (<5) all')
text(5,2+.2*a,['trial n=',num2str(size([earlycorr;earlyerr],1)/8)],'Color',animcol(a,:));
subplot(3,2,2); hold on;
mdl = fitglm([latecorr(:,[2 1 3 4]);lateerr(:,[2 1 3 4])],[latecorr(:,5);lateerr(:,5)],'linear','Distribution','poisson');
CI = coefCI(mdl,.01);
plot(a+[0:5:24],exp(table2array(mdl.Coefficients(:,1))),'.','MarkerSize',20,'Color',animcol(a,:));
plot([a+[0:5:24];a+[0:5:24]],exp(CI)','Color',animcol(a,:)); title('repeat late (5+) all')
text(5,2+.2*a,['trial n=',num2str(size([latecorr;lateerr],1)/8)],'Color',animcol(a,:));
subplot(3,2,3); hold on;
mdl = fitglm(earlycorr(:,[2:4]),earlycorr(:,5),'linear','Distribution','poisson');
CI = coefCI(mdl,.01);
plot(a+[0:5:19],exp(table2array(mdl.Coefficients(:,1))),'.','MarkerSize',20,'Color',animcol(a,:));
plot([a+[0:5:19];a+[0:5:19]],exp(CI)','Color',animcol(a,:)); title('repeat early (<5) corr')
text(5,2+.2*a,['trial n=',num2str(size(earlycorr,1)/8)],'Color',animcol(a,:));
subplot(3,2,4); hold on;
mdl = fitglm(latecorr(:,[2:4]),latecorr(:,5),'linear','Distribution','poisson');
CI = coefCI(mdl,.01);
plot(a+[0:5:19],exp(table2array(mdl.Coefficients(:,1))),'.','MarkerSize',20,'Color',animcol(a,:));
plot([a+[0:5:19];a+[0:5:19]],exp(CI)','Color',animcol(a,:)); title('repeat late (5+) corr')
text(5,2+.2*a,['trial n=',num2str(size(latecorr,1)/8)],'Color',animcol(a,:));
subplot(3,2,5); hold on;
mdl = fitglm(earlyerr(:,[2 1 3 4]),earlyerr(:,5),'linear','Distribution','poisson');
CI = coefCI(mdl,.01);
plot(a+[0:5:24],exp(table2array(mdl.Coefficients(:,1))),'.','MarkerSize',20,'Color',animcol(a,:));
plot([a+[0:5:24];a+[0:5:24]],exp(CI)','Color',animcol(a,:)); title('repeat early (<5) err')
text(5,2+.2*a,['trial n=',num2str(size(earlyerr,1)/8)],'Color',animcol(a,:));
subplot(3,2,6); hold on;
mdl = fitglm(lateerr(:,[2 1 3 4]),lateerr(:,5),'linear','Distribution','poisson');
CI = coefCI(mdl,.01);
plot(a+[0:5:24],exp(table2array(mdl.Coefficients(:,1))),'.','MarkerSize',20,'Color',animcol(a,:));
plot([a+[0:5:24];a+[0:5:24]],exp(CI)','Color',animcol(a,:)); title('repeat late (5+) err')
text(5,2+.2*a,['trial n=',num2str(size(lateerr,1)/8)],'Color',animcol(a,:));
% mdl = fitglm(repeatcat(:,[1,2,4]),repeatcat(:,5),'linear','Distribution','poisson');
% CI = coefCI(mdl,.01);
% plot(a+[0:5:19],exp(table2array(mdl.Coefficients(:,1))),'.','MarkerSize',20,'Color',animcol(a,:));
% plot([a+[0:5:19];a+[0:5:19]],exp(CI)','Color',animcol(a,:)); title('future only')
% text(5,2+.2*a,['AIC=',num2str(mdl.ModelCriterion.AIC,'%.03f')],'Color',animcol(a,:));
% subplot(1,2,2); hold on; title('currgoal only')
% mdl = fitglm(repeatcat(:,[2:4]),repeatcat(:,5),'linear','Distribution','poisson');
% CI = coefCI(mdl,.01);
% plot(a+[0:5:19],exp(table2array(mdl.Coefficients(:,1))),'.','MarkerSize',20,'Color',animcol(a,:));
% plot([a+[0:5:19];a+[0:5:19]],exp(CI)','Color',animcol(a,:));
% text(5,2+.2*a,['AIC=',num2str(mdl.ModelCriterion.AIC,'%.03f')],'Color',animcol(a,:));
% figure(byarm);
% for arm = 1:8
% subplot(2,4,a); hold on; title([animals{a},'searchbyarm']);
% searchcat = vertcat(searchbyarm{a}{arm}{:});
% mdl = fitglm(searchcat(:,1:3),searchcat(:,4),'linear','Distribution','poisson');
% errorbar(arm+[0:11:43],table2array(mdl.Coefficients(:,1)), table2array(mdl.Coefficients(:,2)),'.','Color',animcol(a,:))
% subplot(2,4,a+4); hold on; title([animals{a},'repbyarm']);
% repeatcat = vertcat(repeatbyarm{a}{arm}{:});
% mdl = fitglm(repeatcat(:,1:4),repeatcat(:,5),'linear','Distribution','poisson');
% errorbar(arm+[0:11:54],table2array(mdl.Coefficients(:,1)), table2array(mdl.Coefficients(:,2)),'.','Color',animcol(a,:))
% end
% subplot(2,4,a); xlim([0 43]); set(gca,'XTick',5.5+[0:11:43],'XTickLabel',{'int','fut','pst','pg'})
% subplot(2,4,a+4); xlim([0 53]);set(gca,'XTick',5.5+[0:11:54],'XTickLabel',{'int','fut','pst','cg','pg'})
end
figure(all)
subplot(1,3,1); xlim([0 20]); ylabel('exp(beta)'); set(gca,'XTick',2+[0:5:19],'XTickLabel',{'intrcpt','past','future','prevgoal'})
plot([0 20],[1 1],'k:'); set(gca,'YScale','log'); ylim([.4 3]);
subplot(1,3,2); set(gca,'YScale','log'); ylim([.4 3]); xlim([0 25]);set(gca,'XTick',2+[0:5:24],'XTickLabel',{'intrcpt','past','future','curgoal','prevgoal'})
plot([0 25],[1 1],'k:');
subplot(1,3,3); set(gca,'YScale','log'); ylim([.7 20]); xlim([0 20]);set(gca,'XTick',2+[0:5:20],'XTickLabel',{'intrcpt','past','current','prevgoal'})
plot([0 20],[1 1],'k:'); ylabel('[.7 20]')
figure(poold)
s_tbl = table(search_pooled(:,2),search_pooled(:,1),search_pooled(:,3),search_pooled(:,4),'VariableNames',{'past','future','prevgoal','replaynum'});
s_mdl = fitglm(s_tbl,'linear','Distribution','poisson');
CI = coefCI(s_mdl,.01);
subplot(1,2,1); hold on; title('pooledsearch')
plot([0:5:19],exp(table2array(s_mdl.Coefficients(:,1))),'k.','MarkerSize',20);
plot([[0:5:19];[0:5:19]],exp(CI)','k');
text(5,2,['trial n=',num2str(size(search_pooled,1)/8)]);
xlim([0 20]); ylabel('exp(beta)'); set(gca,'XTick',[0:5:19],'XTickLabel',{'intrcpt','past','future','prevgoal'})
plot([0 20],[1 1],'k:'); set(gca,'YScale','log'); ylim([.5 3]);
%text(1,1.5,mat2str(exp(table2array(s_mdl.Coefficients(:,1)))',3)) %'%.03f'
%text(1,1.4,mat2str(exp(CI(:,1))',3)) %'%.03f'
%text(1,1.3,mat2str(exp(CI(:,2))',3)) %set(gca,'YScale','log');
r_tbl = table(repeat_pooled(:,2),repeat_pooled(:,1),repeat_pooled(:,3),repeat_pooled(:,4),repeat_pooled(:,5),'VariableNames',{'past','future','currgoal','prevgoal','replaynum'});
r_mdl = fitglm(r_tbl,'linear','Distribution','poisson');
CI = coefCI(r_mdl,.01);
subplot(1,2,2); hold on; title('pooledrepeat')
plot([0:5:24],exp(table2array(r_mdl.Coefficients(:,1))),'k.','MarkerSize',20);
plot([[0:5:24];[0:5:24]],exp(CI)','k');
text(5,2,['trial n=',num2str(size(repeat_pooled,1)/8)]);
xlim([0 25]); ylabel('exp(beta)'); set(gca,'XTick',[0:5:24],'XTickLabel',{'intrcpt','past','future','currgoal','prevgoal'})
plot([0 25],[1 1],'k:'); set(gca,'YScale','log'); ylim([.5 3]);%text(1,1.5,mat2str(exp(table2array(r_mdl.Coefficients(:,1)))',3)) %'%.03f'
%text(1,1.4,mat2str(exp(CI(:,1))',3)) %'%.03f'
%text(1,1.3,mat2str(exp(CI(:,2))',3)) %
figure(byarm)
subplot(3,2,1); xlim([0 25]); ylabel('exp(beta)'); set(gca,'XTick',2+[0:5:24],'XTickLabel',{'intrcpt','past','future','currgoal','prevgoal'})
plot([0 25],[1 1],'k:'); set(gca,'YScale','log'); ylim([.3 3]);
subplot(3,2,2); xlim([0 25]); ylabel('exp(beta)'); set(gca,'XTick',2+[0:5:24],'XTickLabel',{'intrcpt','past','future','currgoal','prevgoal'})
plot([0 25],[1 1],'k:'); set(gca,'YScale','log'); ylim([.3 3]);
subplot(3,2,3); xlim([0 20]); ylabel('exp(beta)'); set(gca,'XTick',2+[0:5:19],'XTickLabel',{'intrcpt','past','currgoal','prevgoal'})
plot([0 20],[1 1],'k:'); set(gca,'YScale','log'); ylim([.3 4]);
subplot(3,2,4); xlim([0 20]); ylabel('exp(beta)'); set(gca,'XTick',2+[0:5:19],'XTickLabel',{'intrcpt','past','currgoal','prevgoal'})
plot([0 20],[1 1],'k:'); set(gca,'YScale','log'); ylim([.3 4]);
subplot(3,2,5); xlim([0 25]); ylabel('exp(beta)'); set(gca,'XTick',2+[0:5:24],'XTickLabel',{'intrcpt','past','future','currgoal','prevgoal'})
plot([0 25],[1 1],'k:'); set(gca,'YScale','log'); ylim([.3 4]);
subplot(3,2,6); xlim([0 25]); ylabel('exp(beta)'); set(gca,'XTick',2+[0:5:24],'XTickLabel',{'intrcpt','past','future','currgoal','prevgoal'})
plot([0 25],[1 1],'k:'); set(gca,'YScale','log'); ylim([.3 4]);
% save correlations (r2) to csv for supplementary table
%csvwrite('search_glm_r2table.txt',searchr2);
%csvwrite('repeat_glm_r2table.txt',repr2);
% sanity checks for the model
% randpred = randi([0 1],size(searchcat,1),3);
% randpred = searchcat(randperm(size(searchcat,1)),1:3);
% replay = zeros(size(searchcat,1),1); replay(searchcat(:,3)==1)=randi([1 2],sum(searchcat(:,3)==1),1); replay(searchcat(:,2)==1)=randi([1 2],sum(searchcat(:,2)==1),1);
% mdl = fitglm(searchcat(:,1:3),replay,'linear','Distribution','poisson'); CI = coefCI(mdl,.01);
% mdl = fitglm(randpred,searchcat(:,4),'linear','Distribution','poisson'); CI = coefCI(mdl,.01);
% figure; hold on;
% plot(1:4,table2array(mdl.Coefficients(:,1)),'.','MarkerSize',20,'Color',animcol(a,:));
% plot([1:4;1:4],CI','Color',animcol(a,:));
%simresponse = random(mdl,searchcat(:,1:3));
%[simres,simCI] = predict(mdl,searchcat(:,1:3));
%[simres,simCI] = predict(mdl,[0 1 0],'Alpha',.01)
% mdl = fitglm(searchcat(:,1:3),searchcat(:,4),'linear','Distribution','poisson');
% aic(1) = mdl.ModelCriterion.AIC;
% mdl = fitglm(searchcat(:,1),searchcat(:,4),'linear','Distribution','poisson');
% aic(2) = mdl.ModelCriterion.AIC;
% mdl = fitglm(searchcat(:,2),searchcat(:,4),'linear','Distribution','poisson');
% aic(3) = mdl.ModelCriterion.AIC;
% mdl = fitglm(searchcat(:,3),searchcat(:,4),'linear','Distribution','poisson');
% aic(4) = mdl.ModelCriterion.AIC;
% mdl = fitglm(searchcat(:,1:2),searchcat(:,4),'linear','Distribution','poisson');
% aic(5) = mdl.ModelCriterion.AIC;
% mdl = fitglm(searchcat(:,[1,3]),searchcat(:,4),'linear','Distribution','poisson');
% aic(6) = mdl.ModelCriterion.AIC;
% mdl = fitglm(searchcat(:,2:3),searchcat(:,4),'linear','Distribution','poisson');
% aic(7) = mdl.ModelCriterion.AIC;
% figure; plot(aic,'ro'); ylabel('AIC');
% set(gca,'XTickLabel',{'all','future','past','pg','futpast','futpg','pastpg'},'XTickLabelRotation',45)
%% 6. Compare replay on correct/incorrect (repeat only), GLM to predict corr/err, corr goal replay over trials [FIG 4C and 5B,C,D]
clearvars -except f animals animcol
contentthresh = .3;
dists = figure(); set(gcf,'Position',[675 1 974 973]); scat = figure(); glms = figure();
reps = 100;
for a = 1:length(animals)
eps = find(arrayfun(@(x) ~isempty(x.trips),f(a).output{1}));
for e = 1:length(eps)
tphasenum = f(a).output{1}(eps(e)).trips.taskphase;
valtrials = ~isnan(tphasenum);
tphasenum = [tphasenum(valtrials), [1:sum(valtrials)]']; % add trial numbers
homerips = f(a).output{1}(eps(e)).trips.homeripcontent(valtrials);
hometypes = f(a).output{1}(eps(e)).trips.homeripmaxtypes(valtrials);
rwrips = f(a).output{1}(eps(e)).trips.RWripcontent(valtrials);
postrwrips = f(a).output{1}(eps(e)).trips.postRWripcontent(valtrials);
rwtypes = f(a).output{1}(eps(e)).trips.RWripmaxtypes(valtrials); %
postrwtypes = f(a).output{1}(eps(e)).trips.postRWripmaxtypes(valtrials); %
rips = cellfun(@(x,y,z) [x;y;z],homerips,rwrips,postrwrips,'un',0);
types = cellfun(@(x,y,z) [x,y,z]',hometypes,rwtypes,postrwtypes,'un',0);
outerrips = f(a).output{1}(eps(e)).trips.outerripcontent(valtrials);
outertypes = f(a).output{1}(eps(e)).trips.outerripmaxtypes(valtrials);
clear replays outerreplays
for t=1:length(rips)
if ~isempty(rips{t})
[maxval,ind] = max(rips{t},[],2); %(:,2:end)
valid = types{t}==1 & maxval>contentthresh;
replays{t} = ind(valid)'-1; %
else replays{t} = []; end
if ~isempty(outerrips{t})
[maxval,ind] = max(outerrips{t},[],2); %(:,2:end)
valid = outertypes{t}'==1 & maxval>contentthresh;
outerreplays{t} = ind(valid)'-1; %
else outerreplays{t} = []; end
end
goals = f(a).output{1}(eps(e)).trips.goalarm(valtrials,:);
goals(mod(tphasenum(:,1),1)>.85,:) = goals(find(mod(tphasenum(:,1),1)>.85)-1,:); % fix the last visit of cont (.9) to not reflect new goal yet
outers = f(a).output{1}(eps(e)).trips.outerarm(valtrials);
pastwlock = f(a).output{1}(eps(e)).trips.prevarm(valtrials,2); % only consider the including lockout option
countspertrial = zeros(length(outers),4);
valtrials = ~cellfun(@isempty,replays);
countspertrial(valtrials,:) = cell2mat(cellfun(@(x,fut,pst,crg,prg) [sum(x==fut), sum(x==pst), sum(x==crg), sum(x==prg)], ...
replays(valtrials),num2cell(outers(valtrials)),num2cell(pastwlock(valtrials))',num2cell(goals(valtrials,1)'),...
num2cell(goals(valtrials,2))','un',0)');
valtrials = tphasenum(:,1)>1 & ~isnan(goals(:,2)); %
% [future past currg prevg correct/err early/late goalnum]
allrep{a}{e} = [countspertrial(valtrials,:), (mod(tphasenum(valtrials,1),1)==0 |mod(tphasenum(valtrials,1),1)>.85),tphasenum(valtrials,1)<5, tphasenum(valtrials,1)];
end
repcat = vertcat(allrep{a}{:});
%repcat(:,5) = repcat(randperm(size(repcat,1)),5); % for shufffle
corr = repcat(repcat(:,5)==1,:);
folds = 5;
for r = 1:reps
subsamp = [repcat(repcat(:,5)==0,:);corr(randperm(size(corr,1),sum(repcat(:,5)==0)),:)]; %match number of corr and err trials
cv = cvpartition(size(subsamp,1),'kfold',folds);
%cv = cvpartition(size(repcat,1),'kfold',folds);
for k = 1:folds
%mdl_int = fitglm(repcat(cv.training(k),1),repcat(cv.training(k),5),'constant','Distribution','binomial');
%sim_int = random(mdl_int,repcat(cv.test(k),1));
mdl = fitglm(subsamp(cv.training(k),1),subsamp(cv.training(k),5),'linear','Distribution','binomial');
sim = random(mdl,subsamp(cv.test(k),1));
tmp_future(r,k) = mean(sim~=subsamp(cv.test(k),5)); %-mean(sim_int==subsamp(cv.test(k),5))
mdl = fitglm(subsamp(cv.training(k),3),subsamp(cv.training(k),5),'linear','Distribution','binomial');
sim = random(mdl,subsamp(cv.test(k),3));
tmp_cg(r,k) = mean(sim~=subsamp(cv.test(k),5)); %-mean(sim_int==subsamp(cv.test(k),5))
mdl = fitglm(subsamp(cv.training(k),[1:4]),subsamp(cv.training(k),5),'linear','Distribution','binomial');
sim = random(mdl,subsamp(cv.test(k),[1:4]));
tmp_all(r,k) = mean(sim~=subsamp(cv.test(k),5)); %-mean(sim_int==subsamp(cv.test(k),5))
%mdl = fitglm(subsamp(cv.training(k),8),subsamp(cv.training(k),5),'linear','Distribution','binomial');
%sim = random(mdl,subsamp(cv.test(k),8));
%tmp_ripno(r,k) = mean(sim~=subsamp(cv.test(k),5)); %-mean(sim_int==subsamp(cv.test(k),5))
end
end
mcr_future{a} = mean(tmp_future,2);
mcr_cg{a} = mean(tmp_cg,2);
mcr_all{a} = mean(tmp_all,2);
%mcr_ripno{a} = mean(tmp_ripno,2);
%mcr_future{a} = reshape(tmp_future,[],1);
%mcr_cg{a} = reshape(tmp_cg,[],1);
%mcr_all{a} = reshape(tmp_all,[],1);
ci99 = tinv([.005 .995],reps-1);
mcr_futureCI = (std(mcr_future{a})/sqrt(reps))*ci99; %reps*
mcr_cgCI = (std(mcr_cg{a})/sqrt(reps))*ci99; %reps*
mcr_allCI = (std(mcr_all{a})/sqrt(reps))*ci99; %reps*
%mcr_ripnoCI = (std(mcr_ripno{a})/sqrt(reps))*ci99; %reps*
figure(glms); hold on;
plot(a+[0 5 10],[mean(mcr_future{a}) mean(mcr_cg{a}) mean(mcr_all{a})],'.','MarkerSize',20,'Color',animcol(a,:));
plot(repmat(a+[0 5 10],2,1),[mean(mcr_future{a})+mcr_futureCI' mean(mcr_cg{a})+mcr_cgCI' mean(mcr_all{a})+mcr_allCI'],'Color',animcol(a,:));
figure(dists);
subplot(2,4,a);hold on; title('future (incl1stcont) early and late')
earlycorr = repcat(:,5)==1 & repcat(:,6)==1;
latecorr = repcat(:,5)==1 & repcat(:,6)==0;
earlyerr = repcat(:,5)==0 & repcat(:,6)==1;
lateerr = repcat(:,5)==0 & repcat(:,6)==0;
errorbar(a+[0 .3],[mean(repcat(earlycorr|latecorr,1)),mean(repcat(earlyerr|lateerr,1))],[std(repcat(earlycorr|latecorr,1))./sqrt(sum(earlycorr|latecorr)), std(repcat(earlyerr|lateerr,1))./sqrt(sum(earlyerr|lateerr))],'k.');
bar(a+[0 .3],[mean(repcat(earlycorr|latecorr,1)),mean(repcat(earlyerr|lateerr,1))],.8,'FaceColor',animcol(a,:));
wrds = sprintf('pooled\np=%.03f\nn=%d,%d',ranksum(repcat(earlycorr|latecorr,1),repcat(earlyerr|lateerr,1)),sum(earlycorr|latecorr),sum(earlyerr|lateerr));
text(a,1.5,wrds); ylim([0 2])
%edges = [0:5];
%bar([1,2],[histcounts(repcat(earlycorr,1),edges,'Normalization','probability'); histcounts(repcat(earlyerr,1),edges,'Normalization','probability')],'stacked') %,.3,'FaceColor',animcol(a,:));
%[h,p]=kstest2(repcat(earlycorr,1),repcat(earlyerr,1));
%wrds = sprintf('kstest\np=%.03f\nn=%d,%d',p,sum(earlycorr),sum(earlyerr));
%text(1,.4,wrds); %ylim([0 .8]); xlim([-.5 4.5]); set(gca,'yTick',[0:.2:.8])
subplot(2,4,a+4);hold on; title('currgoal early and late')
errorbar(a+[0 .3],[mean(repcat(earlycorr|latecorr,3)),mean(repcat(earlyerr|lateerr,3))],[std(repcat(earlycorr|latecorr,3))./sqrt(sum(earlycorr|latecorr)), std(repcat(earlyerr|lateerr,3))./sqrt(sum(earlyerr|lateerr))],'k.');
bar(a+[0 .3],[mean(repcat(earlycorr|latecorr,3)),mean(repcat(earlyerr|lateerr,3))],.8,'FaceColor',animcol(a,:));
wrds = sprintf('pooled\np=%.03f\nn=%d,%d',ranksum(repcat(earlycorr|latecorr,3),repcat(earlyerr|lateerr,3)),sum(earlycorr|latecorr),sum(earlyerr|lateerr));
text(a,1.5,wrds); ylim([0 2])
%bar([4,5], [histcounts(repcat(latecorr,1),edges,'Normalization','probability'); histcounts(repcat(lateerr,1),edges,'Normalization','probability')],'stacked') %,.3,'FaceColor',animcol(a,:));
%bar(edges(1:end-1)-.2,histcounts(repcat(latecorr,1),edges,'Normalization','probability'),.3,'FaceColor',animcol(a,:));
%bar(edges(1:end-1)+.2, histcounts(repcat(lateerr,1),edges,'Normalization','probability'),.3,'FaceColor',animcol(a,:),'FaceAlpha',.3)
%[h,p]=kstest2(repcat(latecorr,1),repcat(lateerr,1));
%wrds = sprintf('kstest\np=%.03f\nn=%d,%d',p,sum(latecorr),sum(lateerr));
%text(4,.4,wrds); %ylim([0 .8]); xlim([-.5 4.5]);
%subplot(2,4,a+4);hold on; title('currgoal ')
%bar([1,2],[histcounts(repcat(earlycorr,3),edges,'Normalization','probability'); histcounts(repcat(earlyerr,3),edges,'Normalization','probability')],'stacked') %,.3,'FaceColor',animcol(a,:));
%bar(edges(1:end-1)-.2,histcounts(repcat(earlycorr,3),edges,'Normalization','probability'),.3,'FaceColor',animcol(a,:));
%bar(edges(1:end-1)+.2, histcounts(repcat(earlyerr,3),edges,'Normalization','probability'),.3,'FaceColor',animcol(a,:),'FaceAlpha',.3)
%[h,p]=kstest2(repcat(earlycorr,3),repcat(earlyerr,3));
%wrds = sprintf('kstest\np=%.03f\nn=%d,%d',p,sum(earlycorr),sum(earlyerr));
%text(1,.4,wrds); % xlim([-.5 4.5]); set(gca,'yTick',[0:.2:.8]); ylim([0 .8]);
%subplot(4,4,a+12);hold on; title('currgoal late')
%bar([4 5],[histcounts(repcat(latecorr,3),edges,'Normalization','probability'); histcounts(repcat(lateerr,3),edges,'Normalization','probability')],'stacked') %,.3,'FaceColor',animcol(a,:));
%bar(edges(1:end-1)-.2,histcounts(repcat(latecorr,3),edges,'Normalization','probability'),.3,'FaceColor',animcol(a,:));
%bar(edges(1:end-1)+.2, histcounts(repcat(lateerr,3),edges,'Normalization','probability'),.3,'FaceColor',animcol(a,:),'FaceAlpha',.3)
%[h,p]=kstest2(repcat(latecorr,1),repcat(lateerr,3));
%wrds = sprintf('kstest\np=%.03f\nn=%d,%d',p,sum(latecorr),sum(lateerr));
%text(4,.4,wrds); set(gca,'xTick',[1 2 4 5],'xTicklabel',{'earlycor','earlyerr','latecorr','lateerr'}) % ylim([0 .8]); xlim([-.5 4.5]); ylabel('frac trials'); xlabel('#replays'); set(gca,'yTick',[0:.2:.8])
figure(scat); hold on;
ints = 2:16;
[means,sds,counts] = grpstats(repcat(repcat(:,5)==1,3),ceil(repcat(repcat(:,5)==1,7)),{'mean','std','numel'});
plot(ints(counts>=10),means(counts>=10),'.','Color',animcol(a,:));lsline %plot([2:16;2:16],[means+sds, means-sds]','Color',animcol(a,:));
[r,p] = corrcoef(repcat(repcat(:,5)==1,3),ceil(repcat(repcat(:,5)==1,7)));
wrds = sprintf('r2=%.03f,p=%.07f,n=%d range %d-%d',r(2)^2,p(2),sum(repcat(:,5)==1),min(counts(counts>=10)),max(counts));
text(1,1+a/5,wrds,'Color',animcol(a,:)); ylim([0 2]); ylabel('goal replay rate'); xlabel('visit #'); title('corronly, >=10 trials')
% subplot(3,4,[9 10]);hold on;
% plot(a,mean(mcr_future),'.','MarkerSize',20,'Color',animcol(a,:));
% plot([a;a],mean(mcr_future)+mcr_futureCI','Color',animcol(a,:));
% text(a,.6+a/15,['n=',num2str(size(subsamp,1))]);
% subplot(3,4,[11 12]); hold on;
% plot(a,mean(mcr_cg),'.','MarkerSize',20,'Color',animcol(a,:));
% plot([a;a],mean(mcr_cg)+mcr_cgCI','Color',animcol(a,:));
end
figure(glms); plot([0 15],[.5 .5],'k:'); ylim([.3 .7])
set(gca,'xTick',[2 7 12],'xTicklabel',{'future','cg','all'})
title('5xval x 100reps for supsamp=500df'); ylabel('misclassification rate')
%subplot(3,4,[9 10]); plot([0 5],[.5 .5],'k:'); ylim([0 1]); title('future only'); ylabel('misclassification rate')
%subplot(3,4,[11 12]); plot([0 5],[.5 .5],'k:'); ylim([0 1]); title('currgoal only'); xlabel('99% CI')
%5testdata = [randi([1 12],100,1),zeros(100,1); randi([10 22],100,1),ones(100,1)];
%% 7. Calc mean rate of local, past, future, pg replay by trial (box vs outer & search vs rep) [FIG 5A]
clearvars -except f animals animcol
bars = figure(); set(gcf,'Position',[675 286 570 688]);
contentthresh=.3;
for a = 1:length(animals)
eps = find(arrayfun(@(x) ~isempty(x.trips),f(a).output{1}));
for e = 1:length(eps)
tphasenum = f(a).output{1}(eps(e)).trips.taskphase;
valtrials = ~isnan(tphasenum);
tphasenum = [tphasenum(valtrials), [1:sum(valtrials)]'];
goals = f(a).output{1}(eps(e)).trips.goalarm(valtrials,:);
outers = f(a).output{1}(eps(e)).trips.outerarm(valtrials);
pastwlock = f(a).output{1}(eps(e)).trips.prevarm(valtrials,2); % only consider the including lockout option
homerips = f(a).output{1}(eps(e)).trips.homeripcontent(valtrials);
hometypes = f(a).output{1}(eps(e)).trips.homeripmaxtypes(valtrials);
rwrips = f(a).output{1}(eps(e)).trips.RWripcontent(valtrials);
postrwrips = f(a).output{1}(eps(e)).trips.postRWripcontent(valtrials);
rwtypes = f(a).output{1}(eps(e)).trips.RWripmaxtypes(valtrials); %
postrwtypes = f(a).output{1}(eps(e)).trips.postRWripmaxtypes(valtrials); %
rips = cellfun(@(x,y,z) [x;y;z],homerips,rwrips,postrwrips,'un',0);
types = cellfun(@(x,y,z) [x,y,z]',hometypes,rwtypes,postrwtypes,'un',0);
outerrips = f(a).output{1}(eps(e)).trips.outerripcontent(valtrials);
outertypes = f(a).output{1}(eps(e)).trips.outerripmaxtypes(valtrials); %
clear replays outerreplays
for t=1:length(rips)
if ~isempty(rips{t})
[maxval,ind] = max(rips{t},[],2); %(:,2:end)
valid = types{t}==1 & maxval>contentthresh;
replays{t} = ind(valid)'-1; %
else replays{t} = []; end
end
for t=1:length(outerrips)
if ~isempty(outerrips{t})
[maxval,ind] = max(outerrips{t},[],2); %(:,2:end)
valid = outertypes{t}'==1 & maxval>contentthresh;
outerreplays{t} = ind(valid)'-1; %
else outerreplays{t} = []; end
end
% valtrials = ~cellfun(@isempty, replays);
% if any(valtrials) % [replay future past pg]
% ripcomb = cellfun(@(x,y,z,g,h) [x', repmat([y,z,g,h],length(x),1)], replays(valtrials), num2cell(outers(valtrials)), ...
% num2cell(pastwlock(valtrials))', num2cell(goals(valtrials,:),2)',num2cell(tphasenum(valtrials,:),2)','un',0);
% ripcomb = vertcat(ripcomb{:});
% end
% valtrials = ~cellfun(@isempty, outerreplays);
% if any(valtrials)
% outerripcomb = cellfun(@(x,y,z,g,h) [x', repmat([y,z,g,h],length(x),1)], outerreplays(valtrials), num2cell(outers(valtrials)), ...
% num2cell(pastwlock(valtrials))', num2cell(goals(valtrials,:),2)',num2cell(tphasenum(valtrials,:),2)','un',0);
% outerripcomb = vertcat(outerripcomb{:});
% end
valtrials = ~cellfun(@isempty,replays);
countspertrial = zeros(length(valtrials),5); % [future past currentg previousg totalreplays]
countspertrial(valtrials,:) = cell2mat(cellfun(@(x,fut,pst,crg,prg) [sum(x==fut), sum(x==pst), sum(x==crg), sum(x==prg), length(x)], ...
replays(valtrials),num2cell(outers(valtrials)),num2cell(pastwlock(valtrials))',num2cell(goals(valtrials,1)'),...
num2cell(goals(valtrials,2))','un',0)');
valtrials = ~cellfun(@isempty,outerreplays);
outercountspertrial = zeros(length(valtrials),5);
outercountspertrial(valtrials,:) = cell2mat(cellfun(@(x,fut,pst,crg,prg) [sum(x==fut), sum(x==pst), sum(x==crg), sum(x==prg), length(x)], ...
outerreplays(valtrials),num2cell(outers(valtrials)),num2cell(pastwlock(valtrials))',num2cell(goals(valtrials,1)'),...
num2cell(goals(valtrials,2))','un',0)');
% boxSearch boxRep outerrew
rewouternums = tphasenum(tphasenum(:,1)>=1 & mod(tphasenum(:,1),1)==0,2);
totaleventrate{a}(e,:) = [mean(countspertrial(tphasenum(:,1)<=1,5)),mean(countspertrial(tphasenum(:,1)>1,5)),mean(outercountspertrial(rewouternums,5))];
% [future pg ]
searchboxrates{a}(e,:) = [mean(countspertrial(tphasenum(:,1)<=1 & ~isnan(goals(:,2)),1)), mean(countspertrial(tphasenum(:,1)<=1 & ~isnan(goals(:,2)),4))];
pooledsearchfuture{a}{e} = [countspertrial(tphasenum(:,1)<=1 & ~isnan(goals(:,2)),1)];
pooledsearchpg{a}{e} = [countspertrial(tphasenum(:,1)<=1 & ~isnan(goals(:,2)),4)];
repeatboxrates{a}(e,:) = [mean(countspertrial(tphasenum(:,1)>1 & ~isnan(goals(:,2)),1)), mean(countspertrial(tphasenum(:,1)>1 & ~isnan(goals(:,2)),4))];
pooledrepeatfuture{a}{e} = [countspertrial(tphasenum(:,1)>1 & ~isnan(goals(:,2)),1)];
pooledrepeatpg{a}{e} = [countspertrial(tphasenum(:,1)>1 & ~isnan(goals(:,2)),4)];
% future only, [correct err] (.9 trials count as correct
correcttrinums = tphasenum(~isnan(goals(:,2)) & tphasenum(:,1)>1 & (mod(tphasenum(:,1),1)==0 | mod(tphasenum(:,1),1)>.8),2);
errtrinums = tphasenum(~isnan(goals(:,2)) & tphasenum(:,1)>1 & (mod(tphasenum(:,1),1)>0 & mod(tphasenum(:,1),1)<.9),2);
repcorr_errboxrates{a}(e,:) = [mean(countspertrial(correcttrinums,1)), mean(countspertrial(errtrinums,1))];
end
totalp(a) = ranksum(totaleventrate{a}(:,1),totaleventrate{a}(:,2));
%boxlocalp(a) = ranksum(boxlocalrates{a}(:,1),boxlocalrates{a}(:,2));
searchboxp(a) = [ranksum(searchboxrates{a}(:,1),searchboxrates{a}(:,2))];
repeatboxp(a) = [ranksum(repeatboxrates{a}(:,1),repeatboxrates{a}(:,2))];
errboxp(a) = [ranksum(repcorr_errboxrates{a}(:,1),repcorr_errboxrates{a}(:,2))];
% subplot(1,4,1); hold on; title('total box events search vs rep'); ylim([0 15]);
% errorbar([a a+.5],mean(totaleventrate{a}(:,[1 2])),std(totaleventrate{a}(:,[1 2]))./sqrt(size(totaleventrate{a},1)),'k.')
% bar([a a+.5],mean(totaleventrate{a}(:,[1 2])),.8,'FaceColor',animcol(a,:));
% text([a+.3],[14],num2str(ranksum(totaleventrate{a}(:,1),totaleventrate{a}(:,2)),'%.02f'));
hold on; title('future replay on search/rep') %subplot(1,4,a);
% meaned by epoch
% errorbar([a a+6],mean(searchboxrates{a}),std(searchboxrates{a})./sqrt(size(searchboxrates{a},1)),'k.')
% bar([a a+6],mean(searchboxrates{a}),.05,'FaceColor',animcol(a,:));
% errorbar([a+.4 a+6.4],mean(repeatboxrates{a}),std(repeatboxrates{a})./sqrt(size(repeatboxrates{a},1)),'k.')
% bar([a+.4 a+6.4],mean(repeatboxrates{a}),.05,'FaceColor',animcol(a,:),'FaceAlpha',.3);
% text([a+.3 a+6.3],[1.7 1.7],num2str([ranksum(searchboxrates{a}(:,1),repeatboxrates{a}(:,1));ranksum(searchboxrates{a}(:,2),repeatboxrates{a}(:,2))],'%.02f'));
% pooled across epochs
errorbar([a],mean(vertcat(pooledsearchfuture{a}{:})),std(vertcat(pooledsearchfuture{a}{:}))./sqrt(length(vertcat(pooledsearchfuture{a}{:}))),'k.')
bar([a],mean(vertcat(pooledsearchfuture{a}{:})),.6,'FaceColor',animcol(a,:));
errorbar([a+6],mean(vertcat(pooledrepeatfuture{a}{:})),std(vertcat(pooledrepeatfuture{a}{:}))./sqrt(length(vertcat(pooledrepeatfuture{a}{:}))),'k.')
bar([ a+6],mean(vertcat(pooledrepeatfuture{a}{:})),.6,'FaceColor',animcol(a,:),'FaceAlpha',.3);
text([a],[1+a/5],num2str(ranksum(vertcat(pooledsearchfuture{a}{:}),vertcat(pooledrepeatfuture{a}{:}))),'Color',animcol(a,:));
text(a,1.1+a/5,sprintf('search n=%d,repeat n=%d',length(vertcat(pooledsearchfuture{a}{:})),length(vertcat(pooledrepeatfuture{a}{:}))),'Color',animcol(a,:));
% subplot(1,4,4); hold on; title('future on correct vs error')
% errorbar([a a+.5],nanmean(repcorr_errboxrates{a}),nanstd(repcorr_errboxrates{a})./sqrt(sum(~isnan(repcorr_errboxrates{a}))),'k.')