Scheduled service maintenance on November 22


On Friday, November 22, 2024, between 06:00 CET and 18:00 CET, GIN services will undergo planned maintenance. Extended service interruptions should be expected. We will try to keep downtimes to a minimum, but recommend that users avoid critical tasks, large data uploads, or DOI requests during this time.

We apologize for any inconvenience.

neuralAnalysesChoice.m 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. addpath('./support_functions');
  2. clearvars -except subjsData poolstack* pooltw*
  3. close all; clc; %clear all;
  4. recompute_mfit=0;
  5. reload_eye_spk_data=0;
  6. Nlag=10; Nspan=200; Nshf=10;
  7. %Nlag=10; Nspan=200; Nshf=1000; %Set default
  8. normstr='minus'; %normstr='no'; %normstr='div';
  9. poissfitflag=0;
  10. if poissfitflag && strcmpi(normstr,'div'); error('please combine poissfitflag with normstr=no'); end
  11. figures_folder='../figures';
  12. if ~exist('subjsData','var')
  13. subjsData(1).sbdata=getSubjectData(1);
  14. subjsData(2).sbdata=getSubjectData(2);
  15. end
  16. if ~exist('poolstackvars','var')
  17. poolstackvars=getPoolStackVars(subjsData);
  18. end
  19. twLabels={'preoffer1','offer1','delay1','offer2','delay2','refixate','choice-go','ch-hold'};
  20. nccs=arrayfun(@(sn) poolstackvars(sn).ncells, 1:8);
  21. ntrs=arrayfun(@(sn) size(poolstackvars(sn).offer1sd,1),1:8);
  22. Nccs=sum(nccs);
  23. Ntrs12=max(ntrs);
  24. pooloffer1sd=[]; poolofferLev=[]; poolofferRev=[]; pooloffer1ev=[]; pooloffer2ev=[]; pooloffer1rw=[]; pooloffer2rw=[];
  25. for sn=1:8
  26. pooloffer1sd=cat(2, pooloffer1sd, repmat(cat(1,poolstackvars(sn).offer1sd,nan(Ntrs12-ntrs(sn),1)),[1 nccs(sn)]));
  27. poolofferLev=cat(2, poolofferLev, repmat(cat(1,poolstackvars(sn).offerLev,nan(Ntrs12-ntrs(sn),1)),[1 nccs(sn)]));
  28. poolofferRev=cat(2, poolofferRev, repmat(cat(1,poolstackvars(sn).offerRev,nan(Ntrs12-ntrs(sn),1)),[1 nccs(sn)]));
  29. pooloffer1ev=cat(2, pooloffer1ev, repmat(cat(1,poolstackvars(sn).offer1ev,nan(Ntrs12-ntrs(sn),1)),[1 nccs(sn)]));
  30. pooloffer2ev=cat(2, pooloffer2ev, repmat(cat(1,poolstackvars(sn).offer2ev,nan(Ntrs12-ntrs(sn),1)),[1 nccs(sn)]));
  31. pooloffer1rw=cat(2, pooloffer1rw, repmat(cat(1,poolstackvars(sn).offer1rw,nan(Ntrs12-ntrs(sn),1)),[1 nccs(sn)]));
  32. pooloffer2rw=cat(2, pooloffer2rw, repmat(cat(1,poolstackvars(sn).offer2rw,nan(Ntrs12-ntrs(sn),1)),[1 nccs(sn)]));
  33. end
  34. if reload_eye_spk_data
  35. if ~exist('pooltweyepos','var')
  36. pooltweyepos=getTimewinsPoolEyePos();
  37. end
  38. if ~exist('pooltwspksq','var')
  39. pooltwspksq=getTimewinsPoolSpikeSeqs();
  40. end
  41. Ntps12=arrayfun(@(tw) size(pooltweyepos(tw).posX,3), 1:8);
  42. Ntps12(1)=400;
  43. pooleposxmat=cat(3,pooltweyepos(1).posX,pooltweyepos(2).posX,pooltweyepos(3).posX,pooltweyepos(4).posX,...
  44. pooltweyepos(5).posX,pooltweyepos(6).posX,pooltweyepos(7).posX,pooltweyepos(8).posX);
  45. poolspksqmat=cat(3,pooltwspksq(1).spkseqs,pooltwspksq(2).spkseqs,pooltwspksq(3).spkseqs,pooltwspksq(4).spkseqs,...
  46. pooltwspksq(5).spkseqs,pooltwspksq(6).spkseqs,pooltwspksq(7).spkseqs,pooltwspksq(8).spkseqs);
  47. poolcovhist=cat(2,pooltwspksq(1).covhist',pooltwspksq(2).covhist',pooltwspksq(3).covhist',pooltwspksq(4).covhist',...
  48. pooltwspksq(5).covhist',pooltwspksq(6).covhist',pooltwspksq(7).covhist',pooltwspksq(8).covhist');
  49. clearvars pooltwspksq pooltweyepos % to reduce memory usage keep pooled version
  50. fchg=find(poolcovhist>=.999); fchl=find(poolcovhist<.999); % use 99.9% coverage
  51. ntcv=[fchg(diff([fchg sum(Ntps12)])>1)' fchl([diff(fchl) sum(Ntps12)]>1)'];
  52. ntce=[[1; 401; ntcv(1:end-1,2)] [400; ntcv(:,1)]];
  53. vtps=ones(1,sum(Ntps12)); for jj=1:7; vtps(ntcv(jj,1):ntcv(jj,2))=nan; end
  54. %if sparsity==1 it means 1spike/ms that is 1000 spikes/s
  55. %nanmean(vectorise((nansum(poolspksqmat,3))./(nansum(poolspksqmat>=0,3))))*1000
  56. %nanstd(vectorise((nansum(poolspksqmat,3))./(nansum(poolspksqmat>=0,3))))*1000
  57. % smooth by movmean filtering, downsample to 20ms, 50Hz resolution
  58. Ntps_ds=ceil(sum(Ntps12)/Nlag);
  59. tmax_ds=nanmovmean(1:size(pooleposxmat,3),Nlag,Nlag);
  60. poolspksqmat_ds=nan(Ntrs12,Nccs,length(tmax_ds));
  61. pooleposxmat_ds=nan(Ntrs12,Nccs,length(tmax_ds));
  62. for cc=1:Nccs
  63. poolspksqmat_ds(:,cc,:)=nanmovmean(squeeze(poolspksqmat(:,cc,:))',Nlag,[0 Nspan])';
  64. pooleposxmat_ds(:,cc,:)=nanmovmean(squeeze(pooleposxmat(:,cc,:))',Nlag,Nlag)';
  65. end
  66. clearvars poolspksqmat pooleposxmat % to reduce memory usage keep downsampled version
  67. save('../data/downsampled_eye_spk_data.mat','Ntps_ds','tmax_ds','vtps','ntc*','fch*','Ntps12',...
  68. 'poolcovhist','poolspksqmat_ds','pooleposxmat_ds');
  69. else
  70. load('../data/downsampled_eye_spk_data.mat');
  71. end
  72. poolcovhist_ds=nanmovmean(poolcovhist,Nlag,Nlag);
  73. %fcgh_ds=find(poolcovhist_ds>=.8); fchl_ds=find(poolcovhist_ds<.8);
  74. fcgh_ds=find(poolcovhist_ds>=.999); fchl_ds=find(poolcovhist_ds<.999);
  75. ntcv_ds=[fcgh_ds(diff([fcgh_ds Ntps_ds])>1)' fchl_ds([diff(fchl_ds) Ntps_ds]>1)'];
  76. vtps_ds=ones(1,Ntps_ds); for jj=1:7; vtps_ds(ntcv_ds(jj,1):ntcv_ds(jj,2))=nan; end
  77. ntce_ds=[[1 ceil(400/Nlag)+1 find(vtps_ds(2:end)+isnan(vtps_ds(1:end-1))==2)+1]' [ceil(400/Nlag) find(vtps_ds(1:end-1)+isnan(vtps_ds(2:end))==2)]'];
  78. % Apply movmean independently across time windows
  79. % (yields same final results but all at once is faster)
  80. % checktps_ds=zeros(1,length(poolcovhist_ds));
  81. % for tw=1:8
  82. % tpvec=zeros(1,length(poolcovhist));
  83. % tpvec(ntce(tw,1):ntce(tw,2))=1;
  84. % tpvec_ds=zeros(1,length(poolcovhist_ds));
  85. % tpvec_ds(ntce_ds(tw,1):ntce_ds(tw,2))=1;
  86. % for cc=1:Nccs
  87. % nnspktemp=nanmovmean(squeeze(poolspksqmat(:,cc,tpvec==1))',Nlag,[0 Nspan])';
  88. % poolspksqmat_ds(:,cc,tpvec_ds==1)=nnspktemp(:,1:sum(tpvec_ds==1));
  89. % nnepxtemp=nanmovmean(squeeze(pooleposxmat(:,cc,tpvec==1))',Nlag,Nlag)';
  90. % pooleposxmat_ds(:,cc,tpvec_ds==1)=nnepxtemp(:,1:sum(tpvec_ds==1));
  91. % end
  92. % checktps_ds(tpvec_ds==1)=checktps_ds(tpvec_ds==1)+tw;
  93. % end
  94. vtmax_ds=rmnans(tmax_ds.*vtps_ds);%find(rmnans(vtps_ds));
  95. rtmax_ds=1:length(vtmax_ds);
  96. rtmes_ds=arrayfun(@(jj) length(ntce_ds(jj,1):ntce_ds(jj,2)), 1:8);
  97. % baseline normalization
  98. if strcmpi(normstr,'no')
  99. poolspksqmat_ds_bln=poolspksqmat_ds;
  100. %pooleposxmat_ds_bln=pooleposxmat_ds;
  101. elseif strcmpi(normstr,'div')
  102. poolspksqmat_ds_bln=poolspksqmat_ds./repmat(nanmean(poolspksqmat_ds(:,:,1:floor(400/Nlag)),3)+eps,[1 1 Ntps_ds]);
  103. %pooleposxmat_ds_bln=pooleposxmat_ds./repmat(nanmean(pooleposxmat_ds(:,:,1:floor(400/Nlag)),3)+eps,[1 1 Ntps_ds]);
  104. elseif strcmpi(normstr,'minus')
  105. poolspksqmat_ds_bln=poolspksqmat_ds-repmat(nanmean(poolspksqmat_ds(:,:,1:floor(400/Nlag)),3),[1 1 Ntps_ds]);
  106. %pooleposxmat_ds_bln=pooleposxmat_ds-repmat(nanmean(pooleposxmat_ds(:,:,1:floor(400/Nlag)),3),[1 1 Ntps_ds]);
  107. end
  108. clearvars poolspksqmat_ds %pooleposxmat_ds % to reduce memory usage
  109. %%
  110. chRb1=nan(Ntps_ds,Nccs); chRb1_shf=nan(Ntps_ds,Nccs,Nshf);
  111. chRb2=nan(Ntps_ds,Nccs); chRb2_shf=nan(Ntps_ds,Nccs,Nshf);
  112. chRb3=nan(Ntps_ds,Nccs); chRb3_shf=nan(Ntps_ds,Nccs,Nshf);
  113. chRp1=nan(Ntps_ds,Nccs); chRp1_shf=nan(Ntps_ds,Nccs,Nshf);
  114. chRp2=nan(Ntps_ds,Nccs); chRp2_shf=nan(Ntps_ds,Nccs,Nshf);
  115. chRp3=nan(Ntps_ds,Nccs); chRp3_shf=nan(Ntps_ds,Nccs,Nshf);
  116. chRr=nan(Ntps_ds,Nccs); chRr_shf=nan(Ntps_ds,Nccs,Nshf);
  117. chRa=nan(Ntps_ds,Nccs,4);%chRa_shf=nan(Ntps_ds,Nccs,Nshf);
  118. chRb1_LL=nan(Ntps_ds,Nccs); chRb1_LL_shf=nan(Ntps_ds,Nccs,Nshf);
  119. chRb2_LL=nan(Ntps_ds,Nccs); chRb2_LL_shf=nan(Ntps_ds,Nccs,Nshf);
  120. chRb3_LL=nan(Ntps_ds,Nccs); chRb3_LL_shf=nan(Ntps_ds,Nccs,Nshf);
  121. chRp1_LL=nan(Ntps_ds,Nccs); chRp1_LL_shf=nan(Ntps_ds,Nccs,Nshf);
  122. chRp2_LL=nan(Ntps_ds,Nccs); chRp2_LL_shf=nan(Ntps_ds,Nccs,Nshf);
  123. chRp3_LL=nan(Ntps_ds,Nccs); chRp3_LL_shf=nan(Ntps_ds,Nccs,Nshf);
  124. chRr_LL=nan(Ntps_ds,Nccs); chRr_LL_shf=nan(Ntps_ds,Nccs,Nshf);
  125. chRa_LL=nan(Ntps_ds,Nccs,4);%chRa_LL_shf=nan(Ntps_ds,Nccs,Nshf);
  126. chRb1_LR=nan(Ntps_ds,Nccs); chRb1_LR_shf=nan(Ntps_ds,Nccs,Nshf);
  127. chRb2_LR=nan(Ntps_ds,Nccs); chRb2_LR_shf=nan(Ntps_ds,Nccs,Nshf);
  128. chRb3_LR=nan(Ntps_ds,Nccs); chRb3_LR_shf=nan(Ntps_ds,Nccs,Nshf);
  129. chRp1_LR=nan(Ntps_ds,Nccs); chRp1_LR_shf=nan(Ntps_ds,Nccs,Nshf);
  130. chRp2_LR=nan(Ntps_ds,Nccs); chRp2_LR_shf=nan(Ntps_ds,Nccs,Nshf);
  131. chRp3_LR=nan(Ntps_ds,Nccs); chRp3_LR_shf=nan(Ntps_ds,Nccs,Nshf);
  132. chRr_LR=nan(Ntps_ds,Nccs); chRr_LR_shf=nan(Ntps_ds,Nccs,Nshf);
  133. chRa_LR=nan(Ntps_ds,Nccs,4);%chRa_LR_shf=nan(Ntps_ds,Nccs,Nshf);
  134. if recompute_mfit
  135. for cc=1:Nccs
  136. %ccspk=squeeze(poolspksqmat_ds(:,cc,:));
  137. ccspk=squeeze(poolspksqmat_ds_bln(:,cc,:));
  138. ccepx=squeeze(pooleposxmat_ds(:,cc,:));
  139. ccoffEVL=squeeze(pooloffer1ev(:,cc));
  140. ccoffEVR=squeeze(pooloffer2ev(:,cc));
  141. ccoffchR=squeeze(poolofferchR(:,cc));
  142. parfor tt=1:Ntps_ds
  143. %for tt=1%
  144. warning('off','all')
  145. if vtps_ds(tt)==1
  146. ctccspk=squeeze(ccspk(:,tt));
  147. ctccepx=squeeze(ccepx(:,tt));
  148. toskip=isnan(ccoffchR) | isnan(ctccspk) | isnan(ctccepx);
  149. vctccspk=ctccspk(~toskip);
  150. vctccepx=ctccepx(~toskip);
  151. vccoffchR=ccoffchR(~toskip);
  152. vccoffEVR=ccoffEVR(~toskip);
  153. vccoffEVL=ccoffEVL(~toskip);
  154. nicc=length(vctccspk);
  155. isLL=find(vctccepx<0); niLL=length(isLL);
  156. isLR=find(vctccepx>0); niLR=length(isLR);
  157. %ctwcclmR=fitglm(vctccspk,vccoffchR,'distribution','binomial','link','logit');
  158. %ctwcclmR_LL=fitglm(vctccspk(isLL),vccoffchR(isLL),'distribution','binomial','link','logit');
  159. %ctwcclmR_LR=fitglm(vctccspk(isLR),vccoffchR(isLR),'distribution','binomial','link','logit');
  160. %ctwcclmR=fitglm([vccoffEVR vccoffEVL vctccspk],vccoffchR,'distribution','binomial','link','logit');
  161. %ctwcclmR_LL=fitglm([vccoffEVR(isLL) vccoffEVL(isLL) vctccspk(isLL)],vccoffchR(isLL),'distribution','binomial','link','logit');
  162. %ctwcclmR_LR=fitglm([vccoffEVR(isLR) vccoffEVL(isLR) vctccspk(isLR)],vccoffchR(isLR),'distribution','binomial','link','logit');
  163. ctwcclmR=fitlm([vccoffEVL vccoffEVR vccoffchR], vctccspk);
  164. ctwcclmR_LL=fitlm([vccoffEVL(isLL) vccoffEVR(isLL) vccoffchR(isLL)], vctccspk(isLL));
  165. ctwcclmR_LR=fitlm([vccoffEVL(isLR) vccoffEVR(isLR) vccoffchR(isLR)], vctccspk(isLR));
  166. % Main results
  167. %ccoff1ev=squeeze(pooloffer1ev(:,cc));
  168. %ccoff2ev=squeeze(pooloffer2ev(:,cc));
  169. %ctwcclm1=fitlm(ccoff1ev,ctccspk);
  170. %ctwcclm2=fitlm(ccoff2ev,ctccspk);
  171. chRb1(tt,cc)=ctwcclmR.Coefficients.Estimate(2); chRb2(tt,cc)=ctwcclmR.Coefficients.Estimate(3); chRb3(tt,cc)=ctwcclmR.Coefficients.Estimate(4);
  172. chRp1(tt,cc)=ctwcclmR.Coefficients.pValue(2); chRp2(tt,cc)=ctwcclmR.Coefficients.pValue(3); chRp3(tt,cc)=ctwcclmR.Coefficients.pValue(4);
  173. chRr(tt,cc)=ctwcclmR.Rsquared.Ordinary;
  174. chRb1_LL(tt,cc)=ctwcclmR_LL.Coefficients.Estimate(2); chRb2_LL(tt,cc)=ctwcclmR_LL.Coefficients.Estimate(3); chRb3_LL(tt,cc)=ctwcclmR_LL.Coefficients.Estimate(4);
  175. chRp1_LL(tt,cc)=ctwcclmR_LL.Coefficients.pValue(2); chRp2_LL(tt,cc)=ctwcclmR_LL.Coefficients.pValue(3); chRp3_LL(tt,cc)=ctwcclmR_LL.Coefficients.pValue(4);
  176. chRr_LL(tt,cc)=ctwcclmR_LL.Rsquared.Ordinary;
  177. chRb1_LR(tt,cc)=ctwcclmR_LR.Coefficients.Estimate(2); chRb2_LR(tt,cc)=ctwcclmR_LR.Coefficients.Estimate(3); chRb3_LR(tt,cc)=ctwcclmR_LR.Coefficients.Estimate(4);
  178. chRp1_LR(tt,cc)=ctwcclmR_LR.Coefficients.pValue(2); chRp2_LR(tt,cc)=ctwcclmR_LR.Coefficients.pValue(3); chRp3_LR(tt,cc)=ctwcclmR_LR.Coefficients.pValue(4);
  179. chRr_LR(tt,cc)=ctwcclmR_LR.Rsquared.Ordinary;
  180. for sh=1:Nshf
  181. isshf=randperm(nicc); i1shf=randperm(nicc); i2shf=randperm(nicc); icshf=randperm(nicc); %trial-shuffle
  182. %ctwcclmR_shf=fitglm(vccoffchR(i2shf),vctccspk(isshf),'distribution','binomial','link','logit');
  183. ctwcclmR_shf=fitlm([vccoffEVL(i2shf) vccoffEVR(i2shf) vccoffchR(i2shf)],vctccspk(isshf));
  184. %ctwcclmR_shf=fitglm([vccoffEVR(i2shf) vccoffEVL(i1shf) vctccspk(icshf)],vccoffchR(isshf),'distribution','binomial','link','logit');
  185. chRp1_shf(tt,cc,sh)=ctwcclmR_shf.Coefficients.pValue(2); chRp2_shf(tt,cc,sh)=ctwcclmR_shf.Coefficients.pValue(3); chRp3_shf(tt,cc,sh)=ctwcclmR_shf.Coefficients.pValue(4);
  186. chRb1_shf(tt,cc,sh)=ctwcclmR_shf.Coefficients.Estimate(2); chRb2_shf(tt,cc,sh)=ctwcclmR_shf.Coefficients.Estimate(3); chRb3_shf(tt,cc,sh)=ctwcclmR_shf.Coefficients.Estimate(4);
  187. chRr_shf(tt,cc,sh)=ctwcclmR_shf.Rsquared.Ordinary;
  188. %LL randperm
  189. isLLshf=isLL(randperm(niLL)); i1LLshf=isLL(randperm(niLL)); i2LLshf=isLL(randperm(niLL)); icLLshf=isLL(randperm(niLL)); %trial-shuffle
  190. %ctwcclmR_LL_shf=fitglm(vccoffchR(i2LLshf),vctccspk(isLLshf),'distribution','binomial','link','logit');
  191. ctwcclmR_LL_shf=fitlm([vccoffEVL(i2LLshf) vccoffEVR(i2LLshf) vccoffchR(i2LLshf)],vctccspk(isLLshf));
  192. %ctwcclmR_LL_shf=fitglm([vccoffEVR(i2LLshf) vccoffEVL(i1LLshf) vctccspk(icLLshf)],vccoffchR(isLLshf),'distribution','binomial','link','logit');
  193. chRp1_LL_shf(tt,cc,sh)=ctwcclmR_LL_shf.Coefficients.pValue(2); chRp2_LL_shf(tt,cc,sh)=ctwcclmR_LL_shf.Coefficients.pValue(3); chRp3_LL_shf(tt,cc,sh)=ctwcclmR_LL_shf.Coefficients.pValue(4);
  194. chRb1_LL_shf(tt,cc,sh)=ctwcclmR_LL_shf.Coefficients.Estimate(2); chRb2_LL_shf(tt,cc,sh)=ctwcclmR_LL_shf.Coefficients.Estimate(3); chRb3_LL_shf(tt,cc,sh)=ctwcclmR_LL_shf.Coefficients.Estimate(4);
  195. chRr_LL_shf(tt,cc,sh)=ctwcclmR_LL_shf.Rsquared.Ordinary;
  196. %LR randperm
  197. isLRshf=isLR(randperm(niLR)); i1LRshf=isLR(randperm(niLR)); i2LRshf=isLR(randperm(niLR)); icLRshf=isLR(randperm(niLR)); %trial-shuffle
  198. %ctwcclmR_LR_shf=fitglm(vccoffchR(i2LRshf),vctccspk(isLRshf),'distribution','binomial','link','logit');
  199. ctwcclmR_LR_shf=fitlm([vccoffEVL(i2LRshf) vccoffEVR(i2LRshf) vccoffchR(i2LRshf)],vctccspk(isLRshf));
  200. %ctwcclmR_LR_shf=fitglm([vccoffEVR(i2LRshf) vccoffEVL(i1LRshf) vctccspk(icLRshf)],vccoffchR(isLRshf),'distribution','binomial','link','logit');
  201. chRp1_LR_shf(tt,cc,sh)=ctwcclmR_LR_shf.Coefficients.pValue(2); chRp2_LR_shf(tt,cc,sh)=ctwcclmR_LR_shf.Coefficients.pValue(3); chRp3_LR_shf(tt,cc,sh)=ctwcclmR_LR_shf.Coefficients.pValue(4);
  202. chRb1_LR_shf(tt,cc,sh)=ctwcclmR_LR_shf.Coefficients.Estimate(2); chRb2_LR_shf(tt,cc,sh)=ctwcclmR_LR_shf.Coefficients.Estimate(3); chRb3_LR_shf(tt,cc,sh)=ctwcclmR_LR_shf.Coefficients.Estimate(4);
  203. chRr_LR_shf(tt,cc,sh)=ctwcclmR_LR_shf.Rsquared.Ordinary;
  204. end
  205. end
  206. warning('on','all')
  207. end
  208. fprintf('%s - done: %d/%d, %2.1f%%\n',datetime('now','Format','HH:mm'),cc,Nccs,cc/Nccs.*100);
  209. end
  210. save(['fitlmstats_chR_evR_evL_spk_span_' num2str(Nspan) 'ms_lag_' num2str(Nlag) 'ms_' date '.mat'],'chR*');
  211. else
  212. load('fitlmstats_chR_evR_evL_spk_span_200ms_lag_10ms_16-Feb-2023');
  213. end
  214. %% Plot colors
  215. o1col=[55 188 52]/255;
  216. o2col=[55 131 200]/255;
  217. o3col=[121 121 121]/255;
  218. o1LLcol=[50 148 48]/255;
  219. o2LLcol=[55 97 135]/255;
  220. o3LLcol=[43 43 43]/255;
  221. o1LRcol=[135 215 133]/255;
  222. o2LRcol=[55 130 200]/255;
  223. o3LRcol=[168 168 168]/255;
  224. %% Fig 4A - Time-resolved fraction of significant cells
  225. for ss=1:3
  226. if ss==1; cc2p=1:sum(nccs(1:4)); elseif ss==2; cc2p=(sum(nccs(1:4))+1):Nccs; else; cc2p=1:Nccs; end
  227. hfig=figure('units','normalized','position',[0 0 1 1]);
  228. subplot(4,1,1); hold on; gch=gca; plotcmapdots([o1col; o2col; o3col]);
  229. runlength(nanmean(chRp1(vtps_ds==1,cc2p)<0.05,2),squeeze(nanmean(chRp1_shf(vtps_ds==1,cc2p,:)<0.05,2)),1,rtmax_ds,o1col,.005);
  230. runlength(nanmean(chRp2(vtps_ds==1,cc2p)<0.05,2),squeeze(nanmean(chRp2_shf(vtps_ds==1,cc2p,:)<0.05,2)),1,rtmax_ds,o2col,.01);
  231. runlength(nanmean(chRp3(vtps_ds==1,cc2p)<0.05,2),squeeze(nanmean(chRp3_shf(vtps_ds==1,cc2p,:)<0.05,2)),1,rtmax_ds,o3col,.015);
  232. ylim([0 .25]); for jj=1:8; plot([1 1].*sum(rtmes_ds(1:jj)),gch.YLim,'k:'); text(sum(rtmes_ds(1:jj-1)),1.02.*gch.YLim(2),twLabels{jj}); end
  233. title('All trials'); ylabel('% signif. cells'); xlabel('time bins (10ms)'); legend('EV_L','EV_R','chR');
  234. subplot(4,1,2); hold on; gch=gca; plotcmapdots([o1LLcol; o1LRcol]);
  235. runlength(nanmean(chRp1_LL(vtps_ds==1,cc2p)<0.05,2),squeeze(nanmean(chRp1_LL_shf(vtps_ds==1,cc2p,:)<0.05,2)),1,rtmax_ds,o1LLcol,.005);
  236. runlength(nanmean(chRp1_LR(vtps_ds==1,cc2p)<0.05,2),squeeze(nanmean(chRp1_LR_shf(vtps_ds==1,cc2p,:)<0.05,2)),1,rtmax_ds,o1LRcol,.005);
  237. ylim([0 .25]); for jj=1:8; plot([1 1].*sum(rtmes_ds(1:jj)),gch.YLim,'k:'); text(sum(rtmes_ds(1:jj-1)),1.02.*gch.YLim(2),twLabels{jj}); end
  238. title('EV_L'); ylabel('% signif. cells'); xlabel('time bins (10ms)'); legend('LookL','LookR');
  239. subplot(4,1,3); hold on; gch=gca; plotcmapdots([o2LLcol; o2LRcol]);
  240. runlength(nanmean(chRp2_LL(vtps_ds==1,cc2p)<0.05,2),squeeze(nanmean(chRp2_LL_shf(vtps_ds==1,cc2p,:)<0.05,2)),1,rtmax_ds,o2LLcol,.01);
  241. runlength(nanmean(chRp2_LR(vtps_ds==1,cc2p)<0.05,2),squeeze(nanmean(chRp2_LR_shf(vtps_ds==1,cc2p,:)<0.05,2)),1,rtmax_ds,o2LRcol,.01);
  242. ylim([0 .25]); for jj=1:8; plot([1 1].*sum(rtmes_ds(1:jj)),gch.YLim,'k:'); text(sum(rtmes_ds(1:jj-1)),1.02.*gch.YLim(2),twLabels{jj}); end
  243. title('EV_R'); ylabel('% signif. cells'); xlabel('time bins (10ms)'); legend('LookL','LookR');
  244. subplot(4,1,4); hold on; gch=gca; plotcmapdots([o3LLcol; o3LRcol]);
  245. runlength(nanmean(chRp3_LL(vtps_ds==1,cc2p)<0.05,2),squeeze(nanmean(chRp3_LL_shf(vtps_ds==1,cc2p,:)<0.05,2)),1,rtmax_ds,o3LLcol,.015);
  246. runlength(nanmean(chRp3_LR(vtps_ds==1,cc2p)<0.05,2),squeeze(nanmean(chRp3_LR_shf(vtps_ds==1,cc2p,:)<0.05,2)),1,rtmax_ds,o3LRcol,.015);
  247. ylim([0 .25]); for jj=1:8; plot([1 1].*sum(rtmes_ds(1:jj)),gch.YLim,'k:'); text(sum(rtmes_ds(1:jj-1)),1.02.*gch.YLim(2),twLabels{jj}); end
  248. title('Choice'); ylabel('% signif. cells'); xlabel('time bins (10ms)'); legend('LookL','LookR');
  249. if ss<3
  250. supertitle(['Subject ' num2str(ss)]);
  251. saveas(hfig,[figures_folder '/SuppFigS10_sig_in_time_pv_chR_subj' num2str(ss) '.png']);
  252. saveas(hfig,[figures_folder '/SuppFigS10_sig_in_time_pv_chR_subj' num2str(ss) '.svg']);
  253. else
  254. supertitle(['Subjects 1:2']);
  255. saveas(hfig,[figures_folder '/Fig4A_sig_in_time_pv_chR.png']);
  256. saveas(hfig,[figures_folder '/Fig4A_sig_in_time_pv_chR.svg']);
  257. end
  258. end
  259. %% Fig 4B - Fraction of significant cells in time epochs
  260. hfig=figure('units','normalized','position',[0 0 1 1]);
  261. for tw=1:8
  262. subplot(4,1,1); plotcmapdots([o1col; o2col; o3col]);
  263. nnm1=nanmean(chRp1_shf(ntce_ds(tw,1):ntce_ds(tw,2),:,:)<0.05,2);
  264. nnm2=nanmean(chRp2_shf(ntce_ds(tw,1):ntce_ds(tw,2),:,:)<0.05,2);
  265. nnm3=nanmean(chRp3_shf(ntce_ds(tw,1):ntce_ds(tw,2),:,:)<0.05,2);
  266. prc051=prctile(nnm1,05,3); prc951=prctile(nnm1,95,3);
  267. prc052=prctile(nnm2,05,3); prc952=prctile(nnm2,95,3);
  268. prc053=prctile(nnm3,05,3); prc953=prctile(nnm3,95,3);
  269. alpha1=0.1+(mean(nanmean(chRp1(ntce_ds(tw,1):ntce_ds(tw,2),:)<0.05,1))>mean(prc951)).*.9;
  270. alpha2=0.1+(mean(nanmean(chRp2(ntce_ds(tw,1):ntce_ds(tw,2),:)<0.05,1))>mean(prc952)).*.9;
  271. alpha3=0.1+(mean(nanmean(chRp3(ntce_ds(tw,1):ntce_ds(tw,2),:)<0.05,1))>mean(prc953)).*.9;
  272. barmsem(tw-.125,nanmean(chRp1(ntce_ds(tw,1):ntce_ds(tw,2),:)<0.05,1),o1col,.1,'none',1,alpha1);
  273. barmsem(tw+.000,nanmean(chRp2(ntce_ds(tw,1):ntce_ds(tw,2),:)<0.05,1),o2col,.1,'none',1,alpha2);
  274. barmsem(tw+.125,nanmean(chRp3(ntce_ds(tw,1):ntce_ds(tw,2),:)<0.05,1),o3col,.1,'none',1,alpha3);
  275. plot(tw-.125+[-.1 .1],[1 1].*mean(prc051(:)),'k:'); plot(tw-.125+[-.1 .1],[1 1].*mean(prc951(:)),'k:');
  276. plot(tw+.000+[-.1 .1],[1 1].*mean(prc052(:)),'k:'); plot(tw+.000+[-.1 .1],[1 1].*mean(prc952(:)),'k:');
  277. plot(tw+.125+[-.1 .1],[1 1].*mean(prc053(:)),'k:'); plot(tw+.125+[-.1 .1],[1 1].*mean(prc953(:)),'k:');
  278. ylim([0 .2]); legend('EV_L','EV_R','choice'); title('All trials');
  279. %text(tw-.1,0.14,getpstars(signrank(nanmean(lmELpv(:,ntce_ds(tw,1):ntce_ds(tw,2))<0.05,2),nanmean(lmERpv(:,ntce_ds(tw,1):ntce_ds(tw,2))<0.05,2))));
  280. subplot(4,1,2); plotcmapdots([o1LLcol; o1LRcol]);
  281. nnm1=nanmean(chRp1_LL_shf(ntce_ds(tw,1):ntce_ds(tw,2),:,:)<0.05,2);
  282. nnm2=nanmean(chRp1_LR_shf(ntce_ds(tw,1):ntce_ds(tw,2),:,:)<0.05,2);
  283. prc051=prctile(nnm1,05,3); prc951=prctile(nnm1,95,3);
  284. prc052=prctile(nnm2,05,3); prc952=prctile(nnm2,95,3);
  285. alpha1=0.1+(mean(nanmean(chRp1_LL(ntce_ds(tw,1):ntce_ds(tw,2),:)<0.05,1))>mean(prc951)).*.9;
  286. alpha2=0.1+(mean(nanmean(chRp1_LR(ntce_ds(tw,1):ntce_ds(tw,2),:)<0.05,1))>mean(prc952)).*.9;
  287. barmsem(tw-.125,nanmean(chRp1_LL(ntce_ds(tw,1):ntce_ds(tw,2),:)<0.05,1),o1LLcol,.1,'none',1,alpha1);
  288. barmsem(tw+.000,nanmean(chRp1_LR(ntce_ds(tw,1):ntce_ds(tw,2),:)<0.05,1),o1LRcol,.1,'none',1,alpha2);
  289. plot(tw-.125+[-.1 .1],[1 1].*mean(prc051(:)),'k:'); plot(tw-.125+[-.1 .1],[1 1].*mean(prc951(:)),'k:');
  290. plot(tw+.000+[-.1 .1],[1 1].*mean(prc052(:)),'k:'); plot(tw+.000+[-.1 .1],[1 1].*mean(prc952(:)),'k:');
  291. ylim([0 .2]); legend('LookL','LookR'); title('Encoding of EV_L');
  292. % text(tw-.1,0.14,getpstars(signrank(nanmean(lmELpvLL(:,ntce_ds(tw,1):ntce_ds(tw,2))<0.05,2),nanmean(lmELpvLR(:,ntce_ds(tw,1):ntce_ds(tw,2))<0.05,2))));
  293. subplot(4,1,3); plotcmapdots([o2LLcol; o2LRcol]);
  294. nnm1=nanmean(chRp2_LL_shf(ntce_ds(tw,1):ntce_ds(tw,2),:,:)<0.05,2);
  295. nnm2=nanmean(chRp2_LR_shf(ntce_ds(tw,1):ntce_ds(tw,2),:,:)<0.05,2);
  296. prc051=prctile(nnm1,05,3); prc951=prctile(nnm1,95,3);
  297. prc052=prctile(nnm2,05,3); prc952=prctile(nnm2,95,3);
  298. alpha1=0.1+(mean(nanmean(chRp2_LL(ntce_ds(tw,1):ntce_ds(tw,2),:)<0.05,1))>mean(prc951)).*.9;
  299. alpha2=0.1+(mean(nanmean(chRp2_LR(ntce_ds(tw,1):ntce_ds(tw,2),:)<0.05,1))>mean(prc952)).*.9;
  300. barmsem(tw-.125,nanmean(chRp2_LL(ntce_ds(tw,1):ntce_ds(tw,2),:)<0.05,1),o2LLcol,.1,'none',1,alpha1);
  301. barmsem(tw+.000,nanmean(chRp2_LR(ntce_ds(tw,1):ntce_ds(tw,2),:)<0.05,1),o2LRcol,.1,'none',1,alpha2);
  302. plot(tw-.125+[-.1 .1],[1 1].*mean(prc051(:)),'k:'); plot(tw-.125+[-.1 .1],[1 1].*mean(prc951(:)),'k:');
  303. plot(tw+.000+[-.1 .1],[1 1].*mean(prc052(:)),'k:'); plot(tw+.000+[-.1 .1],[1 1].*mean(prc952(:)),'k:');
  304. ylim([0 .2]); legend('LookL','LookR'); title('Encoding of EV_R');
  305. subplot(4,1,4); plotcmapdots([o3LLcol; o3LRcol]);
  306. nnm1=nanmean(chRp3_LL_shf(ntce_ds(tw,1):ntce_ds(tw,2),:,:)<0.05,2);
  307. nnm2=nanmean(chRp3_LR_shf(ntce_ds(tw,1):ntce_ds(tw,2),:,:)<0.05,2);
  308. prc051=prctile(nnm1,05,3); prc951=prctile(nnm1,95,3);
  309. prc052=prctile(nnm2,05,3); prc952=prctile(nnm2,95,3);
  310. alpha1=0.1+(mean(nanmean(chRp3_LL(ntce_ds(tw,1):ntce_ds(tw,2),:)<0.05,1))>mean(prc951)).*.9;
  311. alpha2=0.1+(mean(nanmean(chRp3_LR(ntce_ds(tw,1):ntce_ds(tw,2),:)<0.05,1))>mean(prc952)).*.9;
  312. barmsem(tw-.125,nanmean(chRp3_LL(ntce_ds(tw,1):ntce_ds(tw,2),:)<0.05,1),o3LLcol,.1,'none',1,alpha1);
  313. barmsem(tw+.000,nanmean(chRp3_LR(ntce_ds(tw,1):ntce_ds(tw,2),:)<0.05,1),o3LRcol,.1,'none',1,alpha2);
  314. plot(tw-.125+[-.1 .1],[1 1].*mean(prc051(:)),'k:'); plot(tw-.125+[-.1 .1],[1 1].*mean(prc951(:)),'k:');
  315. plot(tw+.000+[-.1 .1],[1 1].*mean(prc052(:)),'k:'); plot(tw+.000+[-.1 .1],[1 1].*mean(prc952(:)),'k:');
  316. ylim([0 .2]); legend('LookL','LookR'); title('Encoding of choice');
  317. %tt=text(tw-.125,0.2,getpstars(signrank(nanmean(chRp1_LR(ntce_ds(tw,1):ntce_ds(tw,2),:)<0.05,1)));
  318. %text(tw-.1,0.14,getpstars(signrank(nanmean(lmERpvLL(:,ntce_ds(tw,1):ntce_ds(tw,2))<0.05,2),nanmean(lmERpvLR(:,ntce_ds(tw,1):ntce_ds(tw,2))<0.05,2))));
  319. end
  320. saveas(hfig,[figures_folder '/Fig4B_timewins_sig_in_time_pv_chR.png']);
  321. saveas(hfig,[figures_folder '/Fig4B_timewins_sig_in_time_pv_chR.svg']);
  322. %% Plotting R2
  323. hfig=figure('units','normalized','position',[0 0 1 1]);
  324. subplot(3,1,1); hold on; gch=gca; plotcmapdots([0 0 0; .33 .33 .33; .66 .66 .66]);
  325. runlength(nanmean(chRr(vtps_ds==1,:),2),squeeze(nanmean(chRr_shf(vtps_ds==1,:,:),2)),1,rtmax_ds,[0 0 0] + 0.0,.005);
  326. for jj=1:8; plot([1 1].*sum(rtmes_ds(1:jj)),gch.YLim,'k:'); text(sum(rtmes_ds(1:jj-1)),1.02.*gch.YLim(2),twLabels{jj}); end
  327. title('All trials'); ylabel('R^2'); xlabel('time bins (10ms)'); legend('R^2');
  328. subplot(3,1,2); hold on; gch=gca; plotcmapdots([0 0 1; 0 0 .66; 0 0 .33]);
  329. runlength(nanmean(chRr_LL(vtps_ds==1,:),2),squeeze(nanmean(chRr_LL_shf(vtps_ds==1,:,:),2)),1,rtmax_ds,[0 0 1].* 1.0,.005);
  330. for jj=1:8; plot([1 1].*sum(rtmes_ds(1:jj)),gch.YLim,'k:'); text(sum(rtmes_ds(1:jj-1)),1.02.*gch.YLim(2),twLabels{jj}); end
  331. title('LookL'); ylabel('R^2'); xlabel('time bins (10ms)'); legend('R^2');
  332. subplot(3,1,3); hold on; gch=gca; plotcmapdots([1 0 0; .66 0 0; .33 0 0]);
  333. runlength(nanmean(chRr_LR(vtps_ds==1,:),2),squeeze(nanmean(chRr_LR_shf(vtps_ds==1,:,:),2)),1,rtmax_ds,[1 0 0].* 1.0,.005);
  334. for jj=1:8; plot([1 1].*sum(rtmes_ds(1:jj)),gch.YLim,'k:'); text(sum(rtmes_ds(1:jj-1)),1.02.*gch.YLim(2),twLabels{jj}); end
  335. title('LookR'); ylabel('R^2'); xlabel('time bins (10ms)'); legend('R^2');
  336. saveas(hfig,[figures_folder '/SuppFig_Rsq_chR.png']);
  337. saveas(hfig,[figures_folder '/SuppFig_Rsq_chR.svg']);