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.

neuralAnalyses.m 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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','-v7.3');
  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. % checktps_ds=zeros(1,length(poolcovhist_ds));
  80. % for tw=1:8
  81. % tpvec=zeros(1,length(poolcovhist));
  82. % tpvec(ntce(tw,1):ntce(tw,2))=1;
  83. % tpvec_ds=zeros(1,length(poolcovhist_ds));
  84. % tpvec_ds(ntce_ds(tw,1):ntce_ds(tw,2))=1;
  85. % for cc=1:Nccs
  86. % nnspktemp=nanmovmean(squeeze(poolspksqmat(:,cc,tpvec==1))',Nlag,[0 Nspan])';
  87. % poolspksqmat_ds(:,cc,tpvec_ds==1)=nnspktemp(:,1:sum(tpvec_ds==1));
  88. % nnepxtemp=nanmovmean(squeeze(pooleposxmat(:,cc,tpvec==1))',Nlag,Nlag)';
  89. % pooleposxmat_ds(:,cc,tpvec_ds==1)=nnepxtemp(:,1:sum(tpvec_ds==1));
  90. % end
  91. % checktps_ds(tpvec_ds==1)=checktps_ds(tpvec_ds==1)+tw;
  92. % end
  93. vtmax_ds=(tmax_ds.*vtps_ds);
  94. vtmax_ds(isnan(vtmax_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. %% Regress EV vs Spiking
  110. if recompute_mfit
  111. lmELpv=nan(Nccs,Ntps_ds); lmELpv_shf=nan(Nccs,Ntps_ds,Nshf); lmERpv=nan(Nccs,Ntps_ds); lmERpv_shf=nan(Nccs,Ntps_ds,Nshf);
  112. lmELpvLL=nan(Nccs,Ntps_ds); lmELpvLL_shf=nan(Nccs,Ntps_ds,Nshf); lmERpvLL=nan(Nccs,Ntps_ds); lmERpvLL_shf=nan(Nccs,Ntps_ds,Nshf);
  113. lmELpvLR=nan(Nccs,Ntps_ds); lmELpvLR_shf=nan(Nccs,Ntps_ds,Nshf); lmERpvLR=nan(Nccs,Ntps_ds); lmERpvLR_shf=nan(Nccs,Ntps_ds,Nshf);
  114. lmELR2=nan(Nccs,Ntps_ds); lmELR2_shf=nan(Nccs,Ntps_ds,Nshf); lmERR2=nan(Nccs,Ntps_ds); lmERR2_shf=nan(Nccs,Ntps_ds,Nshf);
  115. lmELR2LL=nan(Nccs,Ntps_ds); lmELR2LL_shf=nan(Nccs,Ntps_ds,Nshf); lmERR2LL=nan(Nccs,Ntps_ds); lmERR2LL_shf=nan(Nccs,Ntps_ds,Nshf);
  116. lmELR2LR=nan(Nccs,Ntps_ds); lmELR2LR_shf=nan(Nccs,Ntps_ds,Nshf); lmERR2LR=nan(Nccs,Ntps_ds); lmERR2LR_shf=nan(Nccs,Ntps_ds,Nshf);
  117. lmELb0=nan(Nccs,Ntps_ds); lmELb0_shf=nan(Nccs,Ntps_ds,Nshf); lmERb0=nan(Nccs,Ntps_ds); lmERb0_shf=nan(Nccs,Ntps_ds,Nshf);
  118. lmELb0LL=nan(Nccs,Ntps_ds); lmELb0LL_shf=nan(Nccs,Ntps_ds,Nshf); lmERb0LL=nan(Nccs,Ntps_ds); lmERb0LL_shf=nan(Nccs,Ntps_ds,Nshf);
  119. lmELb0LR=nan(Nccs,Ntps_ds); lmELb0LR_shf=nan(Nccs,Ntps_ds,Nshf); lmERb0LR=nan(Nccs,Ntps_ds); lmERb0LR_shf=nan(Nccs,Ntps_ds,Nshf);
  120. lmELb1=nan(Nccs,Ntps_ds); lmELb1_shf=nan(Nccs,Ntps_ds,Nshf); lmERb1=nan(Nccs,Ntps_ds); lmERb1_shf=nan(Nccs,Ntps_ds,Nshf);
  121. lmELb1LL=nan(Nccs,Ntps_ds); lmELb1LL_shf=nan(Nccs,Ntps_ds,Nshf); lmERb1LL=nan(Nccs,Ntps_ds); lmERb1LL_shf=nan(Nccs,Ntps_ds,Nshf);
  122. lmELb1LR=nan(Nccs,Ntps_ds); lmELb1LR_shf=nan(Nccs,Ntps_ds,Nshf); lmERb1LR=nan(Nccs,Ntps_ds); lmERb1LR_shf=nan(Nccs,Ntps_ds,Nshf);
  123. lmntrsLL=nan(Nccs,Ntps_ds); lmntrsLR=nan(Nccs,Ntps_ds);
  124. for cc=1:Nccs
  125. ccoff1ev=pooloffer1ev(:,cc);
  126. ccoff2ev=pooloffer2ev(:,cc);
  127. % ccoff1sd=pooloffer1sd(:,cc);
  128. ccspk=squeeze(poolspksqmat_ds_bln(:,cc,:)); % For linear fit
  129. % ccspk=squeeze(poolspksqmat_ds(:,cc,:)); % Force NO BL correction
  130. ccepx=squeeze(pooleposxmat_ds(:,cc,:)); % Force NO BL correction
  131. rmallnans=isnan(ccoff1ev);
  132. rmtrials=rmallnans;%(rmallnans | ccoff1rw~=1 | ccoff2rw~=1);
  133. % rmtrials=(rmallnans | ccoff1sd==1); % runs only first Left
  134. % rmtrials=(rmallnans | ccoff1sd==0); % runs only first Right
  135. ccspk(rmtrials,:)=[];
  136. ccepx(rmtrials,:)=[];
  137. ccoff1ev(rmtrials)=[];
  138. ccoff2ev(rmtrials)=[];
  139. ccntrs=size(ccspk,1);
  140. parfor tt=1:Ntps_ds
  141. %for tt=1:Ntps_ds
  142. warning('off','all')
  143. if vtps_ds(tt)==1 % discards low trials times
  144. ctspk=squeeze(ccspk(:,tt));
  145. ctepx=squeeze(ccepx(:,tt));
  146. if ~poissfitflag
  147. ctlm1=fitlm(ccoff1ev,ctspk);
  148. ctlm2=fitlm(ccoff2ev,ctspk);
  149. else
  150. ctlm1=fitglm(ccoff1ev,ctspk,'linear','Distribution','poisson');
  151. ctlm2=fitglm(ccoff2ev,ctspk,'linear','Distribution','poisson');
  152. end
  153. lmELpv(cc,tt)=ctlm1.Coefficients.pValue(2);
  154. lmERpv(cc,tt)=ctlm2.Coefficients.pValue(2);
  155. lmELR2(cc,tt)=ctlm1.Rsquared.Ordinary;
  156. lmERR2(cc,tt)=ctlm2.Rsquared.Ordinary;
  157. lmELb0(cc,tt)=ctlm1.Coefficients.Estimate(1);
  158. lmERb0(cc,tt)=ctlm2.Coefficients.Estimate(1);
  159. lmELb1(cc,tt)=ctlm1.Coefficients.Estimate(2);
  160. lmERb1(cc,tt)=ctlm2.Coefficients.Estimate(2);
  161. isLL=find(ctepx<0);
  162. niLL=length(isLL);
  163. if ~poissfitflag
  164. ctlm1LL=fitlm(ccoff1ev(isLL),ctspk(isLL));
  165. ctlm2LL=fitlm(ccoff2ev(isLL),ctspk(isLL));
  166. else
  167. ctlm1LL=fitglm(ccoff1ev(isLL),ctspk(isLL),'linear','Distribution','poisson');
  168. ctlm2LL=fitglm(ccoff2ev(isLL),ctspk(isLL),'linear','Distribution','poisson');
  169. end
  170. lmELpvLL(cc,tt)=ctlm1LL.Coefficients.pValue(2);
  171. lmERpvLL(cc,tt)=ctlm2LL.Coefficients.pValue(2);
  172. lmELR2LL(cc,tt)=ctlm1LL.Rsquared.Ordinary;
  173. lmERR2LL(cc,tt)=ctlm2LL.Rsquared.Ordinary;
  174. lmELb0LL(cc,tt)=ctlm1LL.Coefficients.Estimate(1);
  175. lmERb0LL(cc,tt)=ctlm2LL.Coefficients.Estimate(1);
  176. lmELb1LL(cc,tt)=ctlm1LL.Coefficients.Estimate(2);
  177. lmERb1LL(cc,tt)=ctlm2LL.Coefficients.Estimate(2);
  178. lmntrsLL(cc,tt)=niLL;
  179. isLR=find(ctepx>0);
  180. niLR=length(isLR);
  181. if ~poissfitflag
  182. ctlm1LR=fitlm(ccoff1ev(isLR),ctspk(isLR));
  183. ctlm2LR=fitlm(ccoff2ev(isLR),ctspk(isLR));
  184. else
  185. ctlm1LR=fitglm(ccoff1ev(isLR),ctspk(isLR),'linear','Distribution','poisson');
  186. ctlm2LR=fitglm(ccoff2ev(isLR),ctspk(isLR),'linear','Distribution','poisson');
  187. end
  188. lmELpvLR(cc,tt)=ctlm1LR.Coefficients.pValue(2);
  189. lmERpvLR(cc,tt)=ctlm2LR.Coefficients.pValue(2);
  190. lmELR2LR(cc,tt)=ctlm1LR.Rsquared.Ordinary;
  191. lmERR2LR(cc,tt)=ctlm2LR.Rsquared.Ordinary;
  192. lmELb0LR(cc,tt)=ctlm1LR.Coefficients.Estimate(1);
  193. lmERb0LR(cc,tt)=ctlm2LR.Coefficients.Estimate(1);
  194. lmELb1LR(cc,tt)=ctlm1LR.Coefficients.Estimate(2);
  195. lmERb1LR(cc,tt)=ctlm2LR.Coefficients.Estimate(2);
  196. lmntrsLR(cc,tt)=niLR;
  197. for shf=1:Nshf
  198. i1shf=randperm(ccntrs); %trial-shuffle
  199. i2shf=randperm(ccntrs); %trial-shuffle
  200. isshf=randperm(ccntrs); %trial-shuffle
  201. if ~poissfitflag
  202. ctlm1_shf=fitlm(ccoff1ev(i1shf),ctspk(isshf)); %trial-shuffle
  203. ctlm2_shf=fitlm(ccoff2ev(i2shf),ctspk(isshf)); %trial-shuffle
  204. else
  205. ctlm1_shf=fitglm(ccoff1ev(i1shf),ctspk(isshf),'linear','Distribution','poisson');
  206. ctlm2_shf=fitglm(ccoff2ev(i2shf),ctspk(isshf),'linear','Distribution','poisson');
  207. end
  208. %ctlm1_shf=fitlm(ccoff1ev,squeeze(ccspk_shf(shf,:,tt))); %time-shuffle
  209. %ctlm2_shf=fitlm(ccoff2ev,squeeze(ccspk_shf(shf,:,tt))); %time-shuffle
  210. lmELpv_shf(cc,tt,shf)=ctlm1_shf.Coefficients.pValue(2);
  211. lmERpv_shf(cc,tt,shf)=ctlm2_shf.Coefficients.pValue(2);
  212. lmELR2_shf(cc,tt,shf)=ctlm1_shf.Rsquared.Ordinary;
  213. lmERR2_shf(cc,tt,shf)=ctlm2_shf.Rsquared.Ordinary;
  214. lmELb0_shf(cc,tt,shf)=ctlm1_shf.Coefficients.Estimate(1);
  215. lmERb0_shf(cc,tt,shf)=ctlm2_shf.Coefficients.Estimate(1);
  216. lmELb1_shf(cc,tt,shf)=ctlm1_shf.Coefficients.Estimate(2);
  217. lmERb1_shf(cc,tt,shf)=ctlm2_shf.Coefficients.Estimate(2);
  218. i1LLshf=isLL(randperm(niLL)); %trial-shuffle
  219. i2LLshf=isLL(randperm(niLL)); %trial-shuffle
  220. isLLshf=isLL(randperm(niLL)); %trial-shuffle
  221. if ~poissfitflag
  222. ctlm1LL_shf=fitlm(ccoff1ev(i1LLshf),ctspk(isLLshf)); %trial-shuffle
  223. ctlm2LL_shf=fitlm(ccoff2ev(i2LLshf),ctspk(isLLshf)); %trial-shuffle
  224. else
  225. ctlm1LL_shf=fitglm(ccoff1ev(i1LLshf),ctspk(isLLshf),'linear','Distribution','poisson');
  226. ctlm2LL_shf=fitglm(ccoff2ev(i2LLshf),ctspk(isLLshf),'linear','Distribution','poisson');
  227. end
  228. %ctlm1LL_shf=fitlm(ccoff1ev(isLL),squeeze(ccspk_shf(shf,isLL,tt))); %time-shuffle
  229. %ctlm2LL_shf=fitlm(ccoff2ev(isLL),squeeze(ccspk_shf(shf,isLL,tt))); %time-shuffle
  230. lmELpvLL_shf(cc,tt,shf)=ctlm1LL_shf.Coefficients.pValue(2);
  231. lmERpvLL_shf(cc,tt,shf)=ctlm2LL_shf.Coefficients.pValue(2);
  232. lmELR2LL_shf(cc,tt,shf)=ctlm1LL_shf.Rsquared.Ordinary;
  233. lmERR2LL_shf(cc,tt,shf)=ctlm2LL_shf.Rsquared.Ordinary;
  234. lmELb0LL_shf(cc,tt,shf)=ctlm1LL_shf.Coefficients.Estimate(1);
  235. lmERb0LL_shf(cc,tt,shf)=ctlm2LL_shf.Coefficients.Estimate(1);
  236. lmELb1LL_shf(cc,tt,shf)=ctlm1LL_shf.Coefficients.Estimate(2);
  237. lmERb1LL_shf(cc,tt,shf)=ctlm2LL_shf.Coefficients.Estimate(2);
  238. i1LRshf=isLR(randperm(niLR)); %trial-shuffle
  239. i2LRshf=isLR(randperm(niLR)); %trial-shuffle
  240. isLRshf=isLR(randperm(niLR)); %trial-shuffle
  241. if ~poissfitflag
  242. ctlm1LR_shf=fitlm(ccoff1ev(i1LRshf),ctspk(isLRshf)); %trial-shuffle
  243. ctlm2LR_shf=fitlm(ccoff2ev(i2LRshf),ctspk(isLRshf)); %trial-shuffle
  244. else
  245. ctlm1LR_shf=fitglm(ccoff1ev(i1LRshf),ctspk(isLRshf),'linear','Distribution','poisson');
  246. ctlm2LR_shf=fitglm(ccoff2ev(i2LRshf),ctspk(isLRshf),'linear','Distribution','poisson');
  247. end
  248. %ctlm1LR_shf=fitlm(ccoff1ev(isLR),squeeze(ccspk_shf(shf,isLR,tt))); %time-shuffle
  249. %ctlm2LR_shf=fitlm(ccoff2ev(isLR),squeeze(ccspk_shf(shf,isLR,tt))); %time-shuffle
  250. lmELpvLR_shf(cc,tt,shf)=ctlm1LR_shf.Coefficients.pValue(2);
  251. lmERpvLR_shf(cc,tt,shf)=ctlm2LR_shf.Coefficients.pValue(2);
  252. lmELR2LR_shf(cc,tt,shf)=ctlm1LR_shf.Rsquared.Ordinary;
  253. lmERR2LR_shf(cc,tt,shf)=ctlm2LR_shf.Rsquared.Ordinary;
  254. lmELb0LR_shf(cc,tt,shf)=ctlm1LR_shf.Coefficients.Estimate(1);
  255. lmERb0LR_shf(cc,tt,shf)=ctlm2LR_shf.Coefficients.Estimate(1);
  256. lmELb1LR_shf(cc,tt,shf)=ctlm1LR_shf.Coefficients.Estimate(2);
  257. lmERb1LR_shf(cc,tt,shf)=ctlm2LR_shf.Coefficients.Estimate(2);
  258. end
  259. end
  260. warning('on','all')
  261. end
  262. disp([sprintf('%s',datetime('now','Format','HH:mm')) ' - done: ' num2str(cc) '/' num2str(Nccs) ', ' num2str(cc/Nccs.*100) '%']);
  263. end
  264. save(['fitlmstats_span_' num2str(Nspan) 'ms_lag_' num2str(Nlag) 'ms_' date '.mat'],'lmE*','lmntr*');
  265. else
  266. load(['fitlmstats_span_' num2str(Nspan) 'ms_lag_' num2str(Nlag) 'ms_16-Feb-2023.mat']);
  267. end
  268. %% Plot colors
  269. o1col=[55 188 52]/255;
  270. o2col=[55 131 200]/255;
  271. o3col=[121 121 121]/255;
  272. o1LLcol=[50 148 48]/255;
  273. o2LLcol=[55 97 135]/255;
  274. o3LLcol=[43 43 43]/255;
  275. o1LRcol=[135 215 133]/255;
  276. o2LRcol=[144 187 226]/255;
  277. o3LRcol=[168 168 168]/255;
  278. %% Fig 3B - Time-resolved fraction of significant cells
  279. for ss=1:3
  280. if ss==1; cc2p=1:sum(nccs(1:4)); elseif ss==2; cc2p=(sum(nccs(1:4))+1):Nccs; else; cc2p=1:Nccs; end
  281. hfig=figure('units','normalized','position',[0 0 1 1]);
  282. subplot(3,1,1); hold on;
  283. plotcmapdots([o1col; o2col]);
  284. runlength(mean(lmELpv(cc2p,vtps_ds==1)<0.05,1),squeeze(mean(lmELpv_shf(cc2p,vtps_ds==1,:)<0.05,1)),1,rtmax_ds,o1col,.005);
  285. runlength(mean(lmERpv(cc2p,vtps_ds==1)<0.05,1),squeeze(mean(lmERpv_shf(cc2p,vtps_ds==1,:)<0.05,1)),1,rtmax_ds,o2col,.010);
  286. for jj=1:8; plot([1 1].*sum(rtmes_ds(1:jj)),[0 0.2],'k:'); text(sum(rtmes_ds(1:jj-1)),.2,twLabels{jj}); end
  287. hl=legend({'E(L)','E(R)'}); ylim([0 0.2]); box(hl,'on');
  288. subplot(3,1,2);
  289. plotcmapdots([o1LLcol; o1LRcol]);
  290. runlength(mean(lmELpvLL(cc2p,vtps_ds==1)<0.05,1),squeeze(mean(lmELpvLL_shf(cc2p,vtps_ds==1,:)<0.05,1)),1,rtmax_ds,o1LLcol,.005);
  291. runlength(mean(lmELpvLR(cc2p,vtps_ds==1)<0.05,1),squeeze(mean(lmELpvLR_shf(cc2p,vtps_ds==1,:)<0.05,1)),1,rtmax_ds,o1LRcol,.010);
  292. for jj=1:8; plot([1 1].*sum(rtmes_ds(1:jj)),[0 0.2],'k:'); text(sum(rtmes_ds(1:jj-1)),.2,twLabels{jj}); end
  293. hl=legend({'E(L), Look L','E(L), Look R'}); box(hl,'on'); ylim([0 0.2]);
  294. subplot(3,1,3);
  295. plotcmapdots([o2LLcol; o2LRcol]);
  296. runlength(mean(lmERpvLL(cc2p,vtps_ds==1)<0.05,1),squeeze(mean(lmERpvLL_shf(cc2p,vtps_ds==1,:)<0.05,1)),1,rtmax_ds,o2LLcol,.005);
  297. runlength(mean(lmERpvLR(cc2p,vtps_ds==1)<0.05,1),squeeze(mean(lmERpvLR_shf(cc2p,vtps_ds==1,:)<0.05,1)),1,rtmax_ds,o2LRcol,.010);
  298. for jj=1:8; plot([1 1].*sum(rtmes_ds(1:jj)),[0 0.2],'k:'); text(sum(rtmes_ds(1:jj-1)),.2,twLabels{jj}); end
  299. hl=legend({'E(R), Look L','E(R), Look R'}); box(hl,'on'); ylim([0 0.2]);
  300. if ss<3; supertitle(['Subject ' num2str(ss)]); else; supertitle('Both Subjects'); end
  301. saveas(hfig,[figures_folder '/perc_sig_cells_pv_s' num2str(ss) '.png']);
  302. saveas(hfig,[figures_folder '/perc_sig_cells_pv_s' num2str(ss) '.svg']);
  303. end
  304. %% Fig 3C - Time-averaged fractions of significant cells
  305. for ss=1:3
  306. if ss==1; cc2p=1:sum(nccs(1:4)); elseif ss==2; cc2p=(sum(nccs(1:4))+1):Nccs; else; cc2p=1:Nccs; end
  307. hfig=figure('units','normalized','position',[0 0 1 1]);
  308. for tw=1:8
  309. subplot(3,1,1); plotcmapdots([o1col; o2col]); ylim([0 .15]);
  310. nnm1=nanmean(lmELpv_shf(cc2p,ntce_ds(tw,1):ntce_ds(tw,2),:)<0.05,1);
  311. nnm2=nanmean(lmERpv_shf(cc2p,ntce_ds(tw,1):ntce_ds(tw,2),:)<0.05,1);
  312. prc051=prctile(nnm1,05,3); prc951=prctile(nnm1,95,3);
  313. prc052=prctile(nnm2,05,3); prc952=prctile(nnm2,95,3);
  314. alpha1=0.05+(mean(nanmean(lmELpv(cc2p,ntce_ds(tw,1):ntce_ds(tw,2),:)<0.05,2))>mean(prc951)).*.95;
  315. alpha2=0.05+(mean(nanmean(lmERpv(cc2p,ntce_ds(tw,1):ntce_ds(tw,2),:)<0.05,2))>mean(prc952)).*.95;
  316. barmsem(tw-.125,nanmean(lmELpv(cc2p,ntce_ds(tw,1):ntce_ds(tw,2))<0.05,2),o1col,.1,'none',1,alpha1);
  317. barmsem(tw+.125,nanmean(lmERpv(cc2p,ntce_ds(tw,1):ntce_ds(tw,2))<0.05,2),o2col,.1,'none',1,alpha2);
  318. plot(tw-.125+[-.1 .1],[1 1].*mean(prc051,2),'k:'); plot(tw-.125+[-.1 .1],[1 1].*mean(prc951,2),'k:');
  319. plot(tw+.125+[-.1 .1],[1 1].*mean(prc052,2),'k:'); plot(tw+.125+[-.1 .1],[1 1].*mean(prc952,2),'k:');
  320. text(tw-.1,0.14,getpstars(signrank(nanmean(lmELpv(cc2p,ntce_ds(tw,1):ntce_ds(tw,2))<0.05,2),...
  321. nanmean(lmERpv(cc2p,ntce_ds(tw,1):ntce_ds(tw,2))<0.05,2))));
  322. subplot(3,1,2); plotcmapdots([o1LLcol; o1LRcol]); ylim([0 .15]);
  323. nnm1=nanmean(lmELpvLL_shf(cc2p,ntce_ds(tw,1):ntce_ds(tw,2),:)<0.05);
  324. nnm2=nanmean(lmELpvLR_shf(cc2p,ntce_ds(tw,1):ntce_ds(tw,2),:)<0.05);
  325. prc051=prctile(nnm1,05,3); prc951=prctile(nnm1,95,3);
  326. prc052=prctile(nnm2,05,3); prc952=prctile(nnm2,95,3);
  327. alpha1=0.05+(mean(nanmean(lmELpvLL(cc2p,ntce_ds(tw,1):ntce_ds(tw,2),:)<0.05,2))>mean(prc951)).*.95;
  328. alpha2=0.05+(mean(nanmean(lmELpvLR(cc2p,ntce_ds(tw,1):ntce_ds(tw,2),:)<0.05,2))>mean(prc952)).*.95;
  329. barmsem(tw-.125,nanmean(lmELpvLL(cc2p,ntce_ds(tw,1):ntce_ds(tw,2))<0.05,2),o1LLcol,.1,'none',1,alpha1);
  330. barmsem(tw+.125,nanmean(lmELpvLR(cc2p,ntce_ds(tw,1):ntce_ds(tw,2))<0.05,2),o1LRcol,.1,'none',1,alpha2);
  331. plot(tw-.125+[-.1 .1],[1 1].*mean(prc051,2),'k:'); plot(tw-.125+[-.1 .1],[1 1].*mean(prc951,2),'k:');
  332. plot(tw+.125+[-.1 .1],[1 1].*mean(prc052,2),'k:'); plot(tw+.125+[-.1 .1],[1 1].*mean(prc952,2),'k:');
  333. text(tw-.1,0.14,getpstars(signrank(nanmean(lmELpvLL(cc2p,ntce_ds(tw,1):ntce_ds(tw,2))<0.05,2),...
  334. nanmean(lmELpvLR(cc2p,ntce_ds(tw,1):ntce_ds(tw,2))<0.05,2))));
  335. subplot(3,1,3); plotcmapdots([o2LLcol; o2LRcol]); ylim([0 .15]);
  336. nnm1=nanmean(lmERpvLL_shf(cc2p,ntce_ds(tw,1):ntce_ds(tw,2),:)<0.05);
  337. nnm2=nanmean(lmERpvLR_shf(cc2p,ntce_ds(tw,1):ntce_ds(tw,2),:)<0.05);
  338. prc051=prctile(nnm1,05,3); prc951=prctile(nnm1,95,3);
  339. prc052=prctile(nnm2,05,3); prc952=prctile(nnm2,95,3);
  340. alpha1=0.05+(mean(nanmean(lmERpvLL(cc2p,ntce_ds(tw,1):ntce_ds(tw,2),:)<0.05,2))>mean(prc951)).*.95;
  341. alpha2=0.05+(mean(nanmean(lmERpvLR(cc2p,ntce_ds(tw,1):ntce_ds(tw,2),:)<0.05,2))>mean(prc952)).*.95;
  342. barmsem(tw-.125,nanmean(lmERpvLL(cc2p,ntce_ds(tw,1):ntce_ds(tw,2))<0.05,2),o2LLcol,.1,'none',1,alpha1);
  343. barmsem(tw+.125,nanmean(lmERpvLR(cc2p,ntce_ds(tw,1):ntce_ds(tw,2))<0.05,2),o2LRcol,.1,'none',1,alpha2);
  344. plot(tw-.125+[-.1 .1],[1 1].*mean(prc051,2),'k:'); plot(tw-.125+[-.1 .1],[1 1].*mean(prc951,2),'k:');
  345. plot(tw+.125+[-.1 .1],[1 1].*mean(prc052,2),'k:'); plot(tw+.125+[-.1 .1],[1 1].*mean(prc952,2),'k:');
  346. text(tw-.1,0.14,getpstars(signrank(nanmean(lmERpvLL(cc2p,ntce_ds(tw,1):ntce_ds(tw,2))<0.05,2),...
  347. nanmean(lmERpvLR(cc2p,ntce_ds(tw,1):ntce_ds(tw,2))<0.05,2))));
  348. end
  349. if ss<3; supertitle(['Subject ' num2str(ss)]); else supertitle('Both subjects'); end
  350. saveas(hfig,[figures_folder '/timewins_sig_in_time_pv_subj' num2str(ss) '.png']);
  351. saveas(hfig,[figures_folder '/timewins_sig_in_time_pv_subj' num2str(ss) '.svg']);
  352. end
  353. %% Supp. Fig. 8A - R2 for the linear regression
  354. cc2p=1:Nccs;
  355. hfig=figure('units','normalized','position',[0 0 1 1]);
  356. subplot(3,1,1); plotcmapdots([o1col; o2col]);
  357. runlength(nanmean(lmELR2(cc2p,vtps_ds==1),1),squeeze(nanmean(lmELR2_shf(cc2p,vtps_ds==1,:),1)),1,rtmax_ds*10,o1col,.0002);
  358. runlength(nanmean(lmERR2(cc2p,vtps_ds==1),1),squeeze(nanmean(lmERR2_shf(cc2p,vtps_ds==1,:),1)),1,rtmax_ds*10,o2col,.0004);
  359. for jj=1:8; plot([1 1].*sum(rtmes_ds(1:jj))*10,[0 0.01],'k:'); text(sum(rtmes_ds(1:jj-1))*10,.005,twLabels{jj}); end
  360. hl=legend({'E(L)','E(R)'}); box(hl,'on'); ylim([0 0.005]);
  361. subplot(3,1,2); plotcmapdots([o1LLcol; o1LRcol]);
  362. runlength(nanmean(lmELR2LR(cc2p,vtps_ds==1),1),squeeze(nanmean(lmELR2LR_shf(cc2p,vtps_ds==1,:),1)),1,rtmax_ds*10,o1LRcol,.0002);
  363. runlength(nanmean(lmELR2LL(cc2p,vtps_ds==1),1),squeeze(nanmean(lmELR2LL_shf(cc2p,vtps_ds==1,:),1)),1,rtmax_ds*10,o1LLcol,.0004);
  364. for jj=1:8; plot([1 1].*sum(rtmes_ds(1:jj))*10,[0 0.02],'k:'); text(sum(rtmes_ds(1:jj-1))*10,.02,twLabels{jj}); end
  365. hl=legend({'E(L), Look L','E(L), Look R'}); box(hl,'on'); ylim([0 0.025]);
  366. subplot(3,1,3); plotcmapdots([o2LLcol; o2LRcol]);
  367. runlength(nanmean(lmERR2LR(cc2p,vtps_ds==1),1),squeeze(nanmean(lmERR2LR_shf(cc2p,vtps_ds==1,:),1)),1,rtmax_ds*10,o2LRcol,.0002);
  368. runlength(nanmean(lmERR2LL(cc2p,vtps_ds==1),1),squeeze(nanmean(lmERR2LL_shf(cc2p,vtps_ds==1,:),1)),1,rtmax_ds*10,o2LLcol,.0004);
  369. for jj=1:8; plot([1 1].*sum(rtmes_ds(1:jj))*10,[0 0.02],'k:'); text(sum(rtmes_ds(1:jj-1))*10,.02,twLabels{jj}); end
  370. hl=legend({'E(R), Look L','E(R), Look R'}); box(hl,'on'); ylim([0 0.025]);
  371. xlabel('task time (ms)');
  372. saveas(hfig,[figures_folder '/R2_vs_R2shf_ELER_sig_in_time.png']);
  373. saveas(hfig,[figures_folder '/R2_vs_R2shf_ELER_sig_in_time.svg']);