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.

process_cGC_within_V1V4_MultiMethods_PreFirstDim.m 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. addpath(genpath('./support_routines'));
  2. addpath(genpath('./support_tools'));
  3. clear all; close all; clc;
  4. sSubjectName='monkey 2';
  5. sVisualArea='V4';
  6. reComputeGCs=0;
  7. reComputeGCsShuffling=0;
  8. plotSinglePenGCs=0;
  9. plotPenAvgGCs=1;
  10. reportSignifThreshold=1;
  11. plotPenAvgGCsSingChRes=0;
  12. lamLayerTag={'Supra','Granr','Infra'};
  13. gratCondTag={'RF','OUTrp','OUT1','OUT2'};
  14. spectWinTag={'Full','Alpha','Beta','Gamma'};
  15. %% cGC implementation settings
  16. numInformChs=-2;
  17. % numInformChs = [-Inf -2 -1 0 1 2 Inf];
  18. % number of informative channels used as regressors in the cGC strategy
  19. % 0 = Unconditional GC
  20. % >0 = cGC to most informative channels
  21. % <0 = cGC to m. inf. outside laminar compartments of each X,Y channel pair
  22. % Inf = cGC to ALL channles (ignores most informative computation)
  23. compMethod='Mvgc';
  24. % compMethod = 'Mvgc','Geweke','MatPart'; choose method to compute cGC
  25. % Mvgc = (Barnett & Seth, J. Neurosci. Methods, 2014)
  26. % Geweke = (Geweke, J. of the American Stat. Assoc., 1984)
  27. % MatPart = (Chen et al., J. Neurosci Methods 2006)
  28. %% DATA SAMPLING SETTINGS
  29. SAMPLING_FREQ=1017.375;
  30. NUM_TIME_SAMPLES_EXTRACTED=1024;
  31. timeAxis0=linspace(-NUM_TIME_SAMPLES_EXTRACTED/SAMPLING_FREQ,0,NUM_TIME_SAMPLES_EXTRACTED)*1e3;
  32. NUM_TIME_SAMPLES=512;
  33. timeAxisPreFirstDim=timeAxis0(NUM_TIME_SAMPLES_EXTRACTED-NUM_TIME_SAMPLES+1:end);
  34. freqAxisPreFirstDim=linspace(0,SAMPLING_FREQ/2,NUM_TIME_SAMPLES);
  35. DOWNSAMPLING_FACTOR=4;
  36. NUM_TIME_DWSAMPLES=NUM_TIME_SAMPLES/DOWNSAMPLING_FACTOR;
  37. DWSAMPLING_FREQ=SAMPLING_FREQ/DOWNSAMPLING_FACTOR;
  38. timeAxisPreFirstDimDwSamp=linspace(timeAxisPreFirstDim(1),timeAxisPreFirstDim(end),NUM_TIME_DWSAMPLES);
  39. freqAxisPreFirstDimDwSamp=linspace(0,DWSAMPLING_FREQ/2,NUM_TIME_DWSAMPLES+1);
  40. freqIndices2Plot=find(freqAxisPreFirstDimDwSamp>2);
  41. if reComputeGCs
  42. subjectInfos=getSubjectInfos(sSubjectName);
  43. subjectData=getSubjectData(sSubjectName,sVisualArea,'ALL','LFPs','ALL');
  44. % REMOVE DATA IN TIME WINDOWS OTHER THAN PREFIRSTDIM TO SAVE MEMORY
  45. for pp=1:length(subjectData.LfpStruct)
  46. if ~isempty(subjectData.LfpStruct(pp).Sorted)
  47. rmfn=fieldnames(subjectData.LfpStruct(pp).Sorted.Full.Supra.RF);
  48. rmfn(cellfun(@(fnm) strcmpi(fnm,'PreFirstDimDataBiZSc'),rmfn))=[];
  49. for ll=1:3
  50. subjectData.LfpStruct(pp).Sorted.Full.(lamLayerTag{ll}).RF=rmfield(subjectData.LfpStruct(pp).Sorted.Full.(lamLayerTag{ll}).RF,rmfn);
  51. subjectData.LfpStruct(pp).Sorted.Full.(lamLayerTag{ll}).OUT1=rmfield(subjectData.LfpStruct(pp).Sorted.Full.(lamLayerTag{ll}).OUT1,rmfn);
  52. subjectData.LfpStruct(pp).Sorted.Full.(lamLayerTag{ll}).OUT2=rmfield(subjectData.LfpStruct(pp).Sorted.Full.(lamLayerTag{ll}).OUT2,rmfn);
  53. end
  54. end
  55. end
  56. %% RANDPERM FOR AVG SUBSAMPLING OF TRIALS AT OUTSIDE LOCATIONS
  57. rng(0); % will pick exactly the same trials at every run,
  58. % Note: varying this seed might give slightly different cGC magnitudes
  59. % (different trials are selected) but results are robust to permutation
  60. for pp=1:length(subjectData.LfpStruct)
  61. if ~isempty(subjectData.LfpStruct(pp).Sorted)
  62. currNtrOUT1=size(subjectData.LfpStruct(pp).Sorted.Full.Supra.OUT1.PreFirstDimDataBiZSc,2);
  63. currRandPermTrials=randperm(2*currNtrOUT1,currNtrOUT1);
  64. for ll=1:3
  65. currOUTcat=cat(2,subjectData.LfpStruct(pp).Sorted.Full.(lamLayerTag{ll}).OUT1.PreFirstDimDataBiZSc,...
  66. subjectData.LfpStruct(pp).Sorted.Full.(lamLayerTag{ll}).OUT2.PreFirstDimDataBiZSc);
  67. subjectData.LfpStruct(pp).Sorted.Full.(lamLayerTag{ll}).OUTrp.PreFirstDimDataBiZSc=currOUTcat(:,currRandPermTrials,:);
  68. end
  69. end
  70. end
  71. eval(clearoutmem('curr*'));
  72. %% Information Toolbox settings (support_tools) - DOI: 10.1186/1471-2202-10-81
  73. numQuantBins=4;
  74. MImethod='dr';
  75. MIbias='qe';
  76. MIbtsp=0;
  77. bandWinsWidth=2;
  78. bandWinsStart=.01; % Starts at 1 Hz
  79. bandWinsEnd=100; % Stops at ~100 Hz
  80. bandWinsEnd=min(bandWinsEnd,SAMPLING_FREQ/2);
  81. NUM_BAND_WINS=floor((bandWinsEnd-bandWinsStart)/bandWinsWidth);
  82. bandWinsCutoffs=[linspace(bandWinsStart,bandWinsEnd-bandWinsWidth,NUM_BAND_WINS);...
  83. linspace(bandWinsStart,bandWinsEnd-bandWinsWidth,NUM_BAND_WINS)+bandWinsWidth]';
  84. freqAxisBandWins=mean(bandWinsCutoffs,2);
  85. filterOrder=2;
  86. filterBCoeffs=nan(NUM_BAND_WINS,2*filterOrder+1);
  87. filterACoeffs=nan(NUM_BAND_WINS,2*filterOrder+1);
  88. filterFreqResp=nan(NUM_BAND_WINS,512);
  89. filterImpResp=nan(NUM_BAND_WINS,2048);
  90. filterFreqAxis=nan(1,512);
  91. filterTimeAxis=nan(1,512);
  92. for bw=1:NUM_BAND_WINS
  93. [filterBCoeffs(bw,:),filterACoeffs(bw,:)]=butter(filterOrder,bandWinsCutoffs(bw,:)./(SAMPLING_FREQ/2),'bandpass');
  94. [filterFreqResp(bw,:),filterFreqAxis]=freqz(filterBCoeffs(bw,:),filterACoeffs(bw,:),512,SAMPLING_FREQ);
  95. [filterImpResp(bw,:),filterTimeAxis]=impz(filterBCoeffs(bw,:),filterACoeffs(bw,:),2048,SAMPLING_FREQ);
  96. end
  97. fresEstim=1/4*NUM_TIME_DWSAMPLES;
  98. lambdafreqAxis=linspace(0,freqAxisPreFirstDimDwSamp(end),fresEstim+1);
  99. %% MVGC Toolbox settings (support_tools) - DOI: 10.1016/j.jneumeth.2013.10.018
  100. regmode='LWR';
  101. morder=10; % set up by optimization of AIC and R² (see Suppl. Figure S7)
  102. acmaxlags=[]; % autocovariance max lags
  103. acdectol=[]; % autocovariance decay tolerance (sets memory of the process)
  104. NShuffles=100; % number of shuffles used to compute significance threshold
  105. parfor pp=1:length(subjectData.LfpStruct)
  106. if ~isempty(subjectData.LfpStruct(pp).Sorted)
  107. for cnd=1:2
  108. disp(['Processing cGC for ' sSubjectName ' ' sVisualArea ' PEN ' num2str(subjectData.LfpStruct(pp).penNum) ', ' gratCondTag{cnd}]);
  109. currX0=[subjectData.LfpStruct(pp).Sorted.Full.Supra.(gratCondTag{cnd}).PreFirstDimDataBiZSc(:,:,NUM_TIME_SAMPLES_EXTRACTED-NUM_TIME_SAMPLES+1:end);...
  110. subjectData.LfpStruct(pp).Sorted.Full.Granr.(gratCondTag{cnd}).PreFirstDimDataBiZSc(:,:,NUM_TIME_SAMPLES_EXTRACTED-NUM_TIME_SAMPLES+1:end);...
  111. subjectData.LfpStruct(pp).Sorted.Full.Infra.(gratCondTag{cnd}).PreFirstDimDataBiZSc(:,:,NUM_TIME_SAMPLES_EXTRACTED-NUM_TIME_SAMPLES+1:end)];
  112. currXp=permute(currX0,[1 3 2]);
  113. %eval(clearoutmem('currX0'));
  114. currSchs=1:size(subjectData.LfpStruct(pp).Sorted.Full.Supra.(gratCondTag{cnd}).PreFirstDimDataBiZSc,1);
  115. currGchs=max([currSchs 0])+(1:size(subjectData.LfpStruct(pp).Sorted.Full.Granr.(gratCondTag{cnd}).PreFirstDimDataBiZSc,1));
  116. currIchs=max([currSchs currGchs 0])+(1:size(subjectData.LfpStruct(pp).Sorted.Full.Infra.(gratCondTag{cnd}).PreFirstDimDataBiZSc,1));
  117. % very awful but required by parfor transparency constraints
  118. currSGIchs=nan(3,16); % set to 16, higher than every max_{j=I,G,S}{length(currjchs)}, allows to cat along 1st dim.
  119. currSGIchs(1,1:length(currSchs))=currSchs;
  120. currSGIchs(2,1:length(currGchs))=currGchs;
  121. currSGIchs(3,1:length(currIchs))=currIchs;
  122. currSGIalgns=nan(3,16);
  123. currSGIalgns(1,1:length(currSchs))=subjectData.LfpStruct(pp).Sorted.Labels.SupraChsLamAlignsBi;
  124. currSGIalgns(2,1:length(currGchs))=subjectData.LfpStruct(pp).Sorted.Labels.GranrChsLamAlignsBi;
  125. currSGIalgns(3,1:length(currIchs))=subjectData.LfpStruct(pp).Sorted.Labels.InfraChsLamAlignsBi;
  126. % De-trend along trial-dimension may help for stationarity aspects
  127. currXDetr=mvdetrend(currXp);
  128. % De-mean along time-dimension
  129. currXDemn=currXDetr-repmat(mean(currXDetr,2),[1 NUM_TIME_SAMPLES 1]);
  130. % Downsampling by movmean
  131. currXDemnp=permute(currXDemn,[2 1 3]);
  132. currXDownp=movmeandecimatrix(currXDemnp,DOWNSAMPLING_FACTOR);
  133. currXDwSamp=permute(currXDownp,[2 1 3]);
  134. % Downsampling by brute decimation
  135. %currXDwSamp=currXDemn(:,1:DOWNSAMPLING_FACTOR:end,:);
  136. [currNvars,currNobs,currNtrials]=size(currXDwSamp);
  137. currfpw=nan(currNvars,currNvars,NUM_TIME_DWSAMPLES+1);
  138. currfpwSh=nan(NShuffles,currNvars,currNvars,NUM_TIME_DWSAMPLES+1);
  139. currFFiltHilbPowDown=nan(currNvars,NUM_BAND_WINS,currNtrials,NUM_TIME_DWSAMPLES/DOWNSAMPLING_FACTOR);
  140. % BINNING AND INFORMATION EVALUATION
  141. paramsMI=[];
  142. paramsMI.method=MImethod;
  143. paramsMI.bias=MIbias;
  144. paramsMI.btsp=MIbtsp;
  145. currMIMat=nan(NUM_BAND_WINS,NUM_TIME_DWSAMPLES/DOWNSAMPLING_FACTOR,currNvars,currNvars);
  146. for bw=1:NUM_BAND_WINS % LOOP OVER BAND WINDOWS
  147. for cch=1:currNvars
  148. % Filter for each band window
  149. currBwinFFilt=filtfilt(filterBCoeffs(bw,:),filterACoeffs(bw,:),squeeze(currXDwSamp(cch,:,:)));
  150. % Hilbert Transform and abs^2
  151. currBwinFFiltHilbPow=(abs(hilbertpad(currBwinFFilt)).^2);
  152. % Downsampling by moving average [ch x NUM_BAND_WINS x trials x NUM_TIME_DWSAMAPLES]
  153. currFFiltHilbPowDown(cch,bw,:,:)=movmeandecimatrix(currBwinFFiltHilbPow,DOWNSAMPLING_FACTOR)';
  154. end
  155. % [currNvars x NBWINS x currNtrials x NTIMEDWS]
  156. currBwXDwMat=reshape(vectorisen(currFFiltHilbPowDown(:,bw,:,:),[2 3]),[1 currNvars*currNtrials*NUM_TIME_DWSAMPLES/DOWNSAMPLING_FACTOR]);
  157. currChsLabels=reshape(repmat(1:currNvars,currNtrials*NUM_TIME_DWSAMPLES/DOWNSAMPLING_FACTOR,1)',[1 currNvars*currNtrials*NUM_TIME_DWSAMPLES/DOWNSAMPLING_FACTOR]);
  158. % [1 x (currNtrials*NTIMEDWS) x currNvars]
  159. [currRespMatAllChs, currnTrAllChs]= buildr(currChsLabels,currBwXDwMat);
  160. currRespMatBinAllChs = binr(currRespMatAllChs, currnTrAllChs, numQuantBins, 'eqpop');
  161. currRespMatBinAllChsRes=reshape(currRespMatBinAllChs(1,:,:),[currNtrials NUM_TIME_DWSAMPLES/DOWNSAMPLING_FACTOR currNvars]);
  162. paramsMI.nt=currNtrials;%currnTrAllChs(1);
  163. for tt=1:(NUM_TIME_DWSAMPLES/DOWNSAMPLING_FACTOR)
  164. for ch1=1:currNvars
  165. for ch2=ch1:currNvars
  166. currMIMat(bw,tt,ch1,ch2)=information(reshape(currRespMatBinAllChsRes(:,tt,[ch1 ch2]),[1 currNtrials 2]), paramsMI, 'I');
  167. end
  168. end
  169. end
  170. end
  171. currTimeCumulInformMat=squeeze(nansum(vectorisen(currMIMat,[1 2]),2));%squeeze(nansum(currMIMat,1));
  172. currTimeCumulInformMat2=currTimeCumulInformMat+triu(currTimeCumulInformMat',-1);
  173. currSrtMIMat=nan(currNvars,currNvars);
  174. currSrtMIChs=nan(currNvars,currNvars);
  175. currSrtRangeNormMIMat=nan(currNvars,currNvars);
  176. for ch=1:currNvars
  177. [currSrtMIMat(ch,:),currSrtMIChs(ch,:)]=sort(currTimeCumulInformMat2(ch,:),'descend');
  178. end
  179. for chY=1:currNvars
  180. for chX=1:currNvars
  181. if chX~=chY %required by parfor, ch2=ch1:nvars is not allowed
  182. chsZOut=[];
  183. if ~(any(chX==currSchs) || any(chY==currSchs))
  184. chsZOut=currSchs;
  185. end
  186. if ~(any(chX==currGchs) || any(chY==currGchs))
  187. chsZOut=[chsZOut currGchs];
  188. end
  189. if ~(any(chX==currIchs) || any(chY==currIchs))
  190. chsZOut=[chsZOut currIchs];
  191. end
  192. if numInformChs==0
  193. chsZ=[];
  194. elseif numInformChs>0
  195. chsZMIwrtX=currSrtMIChs(chX,1:min(numInformChs,end));
  196. chsZMIwrtY=currSrtMIChs(chY,1:min(numInformChs,end));
  197. chsZ=setdiff(unique([chsZMIwrtX,chsZMIwrtY],'stable'),[chX chY]);
  198. else
  199. if numInformChs==-inf
  200. chsZ=chsZOut;
  201. else
  202. numInformChsAbs=abs(numInformChs);
  203. chsZMIwrtX=intersect(currSrtMIChs(chX,1:min(numInformChsAbs,end)),chsZOut,'stable');
  204. chsZMIwrtY=intersect(currSrtMIChs(chY,1:min(numInformChsAbs,end)),chsZOut,'stable');
  205. chsZ=unique([chsZMIwrtX,chsZMIwrtY],'stable');
  206. end
  207. end
  208. numZvars=length(chsZ);
  209. disp([sSubjectName '-' sVisualArea '-PEN-' num2str(subjectData.LfpStruct(pp).penNum) '-' gratCondTag{cnd} '-' compMethod '-cGC_( ' num2str(chY) ' -> ' num2str(chX) ' \ ' num2str(chsZ) ')']);
  210. % FIT A VAR(p) MODEL FOR (X,Y,Z) - (Barnett & Seth, J. Neurosci. Methods, 2014)
  211. [BxyzMat,ExyzMat]=tsdata_to_var(currXDwSamp([chX chY chsZ],:,:),morder,regmode);
  212. assert(~isbad(BxyzMat),'VAR estimation failed');
  213. if strcmpi(compMethod,'Mvgc') && (chX < chY)
  214. % Autocovariance calculation (<mvgc|A5|>)
  215. [GammaMat,GammaInfo] = var_to_autocov(BxyzMat,ExyzMat,acmaxlags,acdectol);
  216. var_info(GammaInfo,true); % report results (and bail out on error)
  217. % Pairwise Granger causality calculation: frequency domain (<mvgc|A14|>)
  218. f_multichs_fres_21 = autocov_to_smvgc(GammaMat,2,1,fresEstim); % from 1 to 2
  219. f_multichs_fres_12 = autocov_to_smvgc(GammaMat,1,2,fresEstim); % from 2 to 1
  220. % assert(~isbad(f_multichs_fres,false),'Mvgc spectral GC calculation failed');
  221. % if fres=[] the freqAxis ends up being non-uniform
  222. % currch1ch2freqAxis=linspace(0,DWSAMPLING_FREQ/2,size(multichsfpw,3));
  223. % so interp1 is used to set a uniform output fres
  224. f_y_to_x_given_z=interp1(lambdafreqAxis,squeeze(f_multichs_fres_12),freqAxisPreFirstDimDwSamp,'spline');
  225. f_x_to_y_given_z=interp1(lambdafreqAxis,squeeze(f_multichs_fres_21),freqAxisPreFirstDimDwSamp,'spline');
  226. currfpw(chX,chY,:)=abs(f_y_to_x_given_z);
  227. currfpw(chY,chX,:)=abs(f_x_to_y_given_z);
  228. if reComputeGCsShuffling %SHUFFLE TRIALS
  229. for nnsh=1:NShuffles
  230. currXDwSampShuf=nan(size(currXDwSamp([chX chY chsZ],:,:)));
  231. for chshuf=1:length([chX chY chsZ])
  232. currXDwSampShuf(chshuf,:,:)=currXDwSamp(chshuf,:,randperm(currNtrials));
  233. end
  234. [BxyzMatShuf,ExyzMatShuf]=tsdata_to_var(currXDwSampShuf,morder,regmode);
  235. assert(~isbad(BxyzMatShuf),'VAR estimation failed');
  236. [GammaMatShuf,GammaInfoShuf] = var_to_autocov(BxyzMatShuf,ExyzMatShuf,acmaxlags,acdectol);
  237. %f_multichs_fres_Sh = autocov_to_spwcgc(GammaMatShuf,fresEstim);
  238. f_multichs_fres_Sh_21 = autocov_to_smvgc(GammaMatShuf,2,1,fresEstim);
  239. f_multichs_fres_Sh_12 = autocov_to_smvgc(GammaMatShuf,1,2,fresEstim);
  240. %assert(~isbad(f_multichs_fres_Sh,false),'Mvgc spectral GC calculation failed');
  241. f_y_to_x_given_z_sh=interp1(lambdafreqAxis,squeeze(f_multichs_fres_Sh_12),freqAxisPreFirstDimDwSamp,'spline');
  242. f_x_to_y_given_z_sh=interp1(lambdafreqAxis,squeeze(f_multichs_fres_Sh_21),freqAxisPreFirstDimDwSamp,'spline');
  243. currfpwSh(nnsh,chX,chY,:)=abs(f_y_to_x_given_z_sh);
  244. currfpwSh(nnsh,chY,chX,:)=abs(f_x_to_y_given_z_sh);
  245. % max(currfpwSh(:,chX,chY,:),[],4) is to be used for prctile
  246. end
  247. end
  248. end
  249. if strcmpi(compMethod,'Geweke')
  250. % ORIGINAL GEWEKE SPECTRAL FACTORIZATION METHOD
  251. % (Geweke, J. of the American Stat. Assoc., 1984)
  252. % FIT A SECOND, REDUCED VAR(p) MODEL FOR (X,Z)
  253. [DxzMat,ExzMat]=tsdata_to_var(currXDwSamp([chX chsZ],:,:),morder,regmode);
  254. assert(~isbad(DxzMat),'VAR estimation failed');
  255. % P MATRICES FOR COVARIANCE NORMALIZATION
  256. PxzMat=[1 zeros(1,numZvars); -ExzMat(1,2:end)'/(ExzMat(1,1)) eye(numZvars)];
  257. PxyzMat=[1 0 zeros(1,numZvars); -ExyzMat(1,2)/(ExyzMat(1,1)) 1 zeros(1,numZvars); -ExyzMat(1,3:end)'/(ExyzMat(1,1)) zeros(numZvars,1) eye(numZvars)]*...
  258. [1 0 zeros(1,numZvars); 0 1 zeros(1,numZvars); zeros(numZvars,1) -(ExyzMat(3:end,2) - ExyzMat(3:end,1)/ExyzMat(1,1)*ExyzMat(1,2))/(ExyzMat(2,2) - ExyzMat(2,1)/ExyzMat(1,1)*ExyzMat(1,2)) eye(numZvars)];
  259. % APPLYING NORMALIZATION BY P-MATRICES MULTIPLICATION IN TIME
  260. PExzMat=PxzMat*ExzMat*PxzMat';
  261. PExyzMat=PxyzMat*ExyzMat*PxyzMat';
  262. PDxzMat=nan(numZvars+1,numZvars+1,morder); PBxyzMat=nan(numZvars+2,numZvars+2,morder);
  263. for mm=1:morder
  264. PDxzMat(:,:,mm)=PxzMat*DxzMat(:,:,mm);
  265. PBxyzMat(:,:,mm)=PxyzMat*BxyzMat(:,:,mm);
  266. end
  267. [PSxzMat,PGxzMat]=var_to_cpsd(PDxzMat,PExzMat,fresEstim);
  268. [PSxyzMat,PHxyzMat]=var_to_cpsd(PBxyzMat,PExyzMat,fresEstim);
  269. % APPLYING NORMALIZATION BY P-MATRICES MULTIPLICATION IN FREQUENCY
  270. %[SxzMat,GxzMat]=var_to_cpsd(DxzMat,ExzMat,fresEstim);
  271. %[SxyzMat,HxyzMat]=var_to_cpsd(BxyzMat,ExyzMat,fresEstim);
  272. f_y_to_x_given_z_fres=nan(1,fresEstim+1);
  273. for ll=1:fresEstim+1
  274. % NORMALIZED TRANSFER FUNCTIONS P*G(X,Z) and P2*P1*H(X,Y,Z)
  275. %PGxzMat=GxzMat(:,:,lambda)/(PxzMat); %G*inv(P)
  276. %PHxyzMat=HxyzMat(:,:,lambda)/(PxyzMat); %H*inv(P)
  277. % EXPANDED G MATRIX FOR SPECTRAL FACTORIZATION
  278. GxyzMat=[PGxzMat(1,1,ll) 0 PGxzMat(1,2:end,ll); 0 1 zeros(1,numZvars); PGxzMat(2:end,1,ll) zeros(numZvars,1) PGxzMat(2:end,2:end,ll)];
  279. QxyzMat=GxyzMat\PHxyzMat(:,:,ll); %inv(G)*H
  280. %SIGxyz=abs(QxyzMat(1,1)*ExyzMat(1,1)*conj(QxyzMat(1,1)) + QxyzMat(1,2)*ExyzMat(2,2)*conj(QxyzMat(1,2)) + QxyzMat(1,3:end)*ExyzMat(3:end,3:end)*conj(QxyzMat(1,3:end))');
  281. SIGxyz=abs(QxyzMat(1,1)*PExyzMat(1,1)*conj(QxyzMat(1,1)) + QxyzMat(1,2)*PExyzMat(2,2)*conj(QxyzMat(1,2)) + QxyzMat(1,3:end)*PExyzMat(3:end,3:end)*conj(QxyzMat(1,3:end))');
  282. %SIGx=abs(QxyzMat(1,1)*ExyzMat(1,1)*conj(QxyzMat(1,1)));
  283. SIGx=abs(QxyzMat(1,1)*PExyzMat(1,1)*conj(QxyzMat(1,1)));
  284. if QxyzMat(1,1)*PExyzMat(1,1)*conj(QxyzMat(1,1)) <0
  285. warning('Qxx product is negative');
  286. end
  287. if QxyzMat(1,2)*PExyzMat(2,2)*conj(QxyzMat(1,2)) <0
  288. warning('Qxy product is negative');
  289. end
  290. if QxyzMat(1,3:end)*PExyzMat(3:end,3:end)*conj(QxyzMat(1,3:end)') <0
  291. warning('Qxz product is negative');
  292. end
  293. f_y_to_x_given_z_fres(ll)= log( SIGxyz ./ SIGx );
  294. %f_y_to_x_given_z_fres(lambda)= log( abs(PExzMat(1,1)) ./ SIGx );
  295. end
  296. f_y_to_x_given_z=interp1(lambdafreqAxis,f_y_to_x_given_z_fres,freqAxisPreFirstDimDwSamp,'spline');
  297. currfpw(chX,chY,:)=f_y_to_x_given_z;
  298. end
  299. if strcmpi(compMethod,'MatPart')
  300. % MATRIX PARTITIONING METHOD (Chen et al., J. Neurosci Methods 2006)
  301. % P MATRIX FOR COVARIANCE NORMALIZATION
  302. PxyzMat=[1 0 zeros(1,numZvars); -ExyzMat(1,2)/(ExyzMat(1,1)) 1 zeros(1,numZvars); -ExyzMat(1,3:end)'/(ExyzMat(1,1)) zeros(numZvars,1) eye(numZvars)]*...
  303. [1 0 zeros(1,numZvars); 0 1 zeros(1,numZvars); zeros(numZvars,1) -(ExyzMat(3:end,2) - ExyzMat(3:end,1)/ExyzMat(1,1)*ExyzMat(1,2))/(ExyzMat(2,2) - ExyzMat(2,1)/ExyzMat(1,1)*ExyzMat(1,2)) eye(numZvars)];
  304. % APPLYING NORMALIZATION BY P-MATRICES MULTIPLICATION IN TIME
  305. PExyzMat=PxyzMat*ExyzMat*PxyzMat';
  306. PBxyzMat=nan(numZvars+2,numZvars+2,morder);
  307. for mm=1:morder
  308. PBxyzMat(:,:,mm)=PxyzMat*BxyzMat(:,:,mm);
  309. end
  310. [PSxyzMat,PHxyzMat]=var_to_cpsd(PBxyzMat,PExyzMat,fresEstim);
  311. f_y_to_x_given_z_fres=nan(1,fresEstim+1);
  312. for ll=1:fresEstim+1
  313. %PHxyzMat=HxyzMat(:,:,lambda)/(PxyzMat); %H*inv(P)
  314. % MIXED TRANSFER FUNCTIONS HBARxy_zy=[HBARxy; HBARzy]
  315. HBAR_xy_zy=([PHxyzMat(1,1,ll) PHxyzMat(1,3:end,ll); PHxyzMat(3:end,1,ll) PHxyzMat(3:end,3:end,ll)]) \ [PHxyzMat(1,2,ll); PHxyzMat(3:end,2,ll)];
  316. % REDUCED COVARIANCE MATRIX SIGBAR_xz=[SIGBARxx SIGBARxz; SIGBARzx SIGBARzz]
  317. EBAR_xz=[PExyzMat(1,1) PExyzMat(1,3:end); PExyzMat(3:end,1) PExyzMat(3:end,3:end)]+HBAR_xy_zy*[PExyzMat(1,2)' PExyzMat(3:end,2)']+[PExyzMat(1,2)' PExyzMat(3:end,2)']'*conj(HBAR_xy_zy')+PExyzMat(2,2)*HBAR_xy_zy*conj(HBAR_xy_zy');
  318. % REDUCED P MATRIX FOR COVARIANCE NORMALIZATION
  319. PBARMat_xz=[1 zeros(1,numZvars); -EBAR_xz(2:end,1)/EBAR_xz(1,1) eye(numZvars)];
  320. GMat_xz=[PHxyzMat(1,1,ll) PHxyzMat(1,3:end,ll); PHxyzMat(3:end,1,ll) PHxyzMat(3:end,3:end,ll)]/(PBARMat_xz); % G/P = G*inv(P)
  321. % EXPANDED G MATRIX FOR SPECTRAL FACTORIZATION
  322. GMat_xyz=[GMat_xz(1,1) 0 GMat_xz(1,2:end); 0 1 zeros(1,numZvars); GMat_xz(2:end,1) zeros(numZvars,1) GMat_xz(2:end,2:end)];
  323. Q_xyz=(GMat_xyz)\PHxyzMat(:,:,ll); % G\H = inv(G)*H
  324. %SIGxyz=abs( Q_xyz(1,1) * ExyzMat(1,1) * conj(Q_xyz(1,1)) + Q_xyz(1,2) * ExyzMat(2,2) * conj(Q_xyz(1,2)) + Q_xyz(1,3:end) * ExyzMat(3:end,3:end) * conj(Q_xyz(1,3:end))');
  325. SIGxyz=abs( Q_xyz(1,1) * PExyzMat(1,1) * conj(Q_xyz(1,1)) + Q_xyz(1,2) * PExyzMat(2,2) * conj(Q_xyz(1,2)) + Q_xyz(1,3:end) * PExyzMat(3:end,3:end) * conj(Q_xyz(1,3:end))');
  326. %SIGx=abs( Q_xyz(1,1) * ExyzMat(1,1) * conj(Q_xyz(1,1)) );
  327. SIGx=abs( Q_xyz(1,1) * PExyzMat(1,1) * conj(Q_xyz(1,1)) );
  328. f_y_to_x_given_z_fres(ll)=log( SIGxyz ./ SIGx );
  329. %f_y_to_x_given_z_fres(lambda)=log( abs(EBAR_xz(1,1)) ./ SIGx );
  330. end
  331. f_y_to_x_given_z=interp1(lambdafreqAxis,f_y_to_x_given_z_fres,freqAxisPreFirstDimDwSamp,'spline');
  332. currfpw(chX,chY,:)=f_y_to_x_given_z;
  333. end
  334. end
  335. end
  336. end
  337. currGCsStruct(pp).(gratCondTag{cnd}).pwgcf=currfpw;
  338. currGCsStruct(pp).(gratCondTag{cnd}).pwgcfSh=currfpwSh;
  339. end
  340. currGCsStruct(pp).penNum=subjectData.LfpStruct(pp).penNum;
  341. currGCsStruct(pp).currSGIchs=currSGIchs;
  342. currGCsStruct(pp).currSGIalgns=currSGIalgns;
  343. end
  344. end
  345. % SORTING BY VISUAL AREA / LAMINAR LAYER / GRATC CONDITION
  346. for pp=1:length(currGCsStruct)
  347. if ~isempty(currGCsStruct(pp).currSGIchs)
  348. currSGIchs=currGCsStruct(pp).currSGIchs;
  349. currSGIalgns=currGCsStruct(pp).currSGIalgns;
  350. for cnd=1:2
  351. for ll1=1:3
  352. for ll2=1:3
  353. % eval is awful but it saves from extra indexing
  354. currll1chs=currSGIchs(ll1,~isnan(currSGIchs(ll1,:)));
  355. currll2chs=currSGIchs(ll2,~isnan(currSGIchs(ll2,:)));
  356. % NOTE: reverse to get fi->j|[ij] instead of fj->i|[ij]
  357. currllsfpw=currGCsStruct(pp).(gratCondTag{cnd}).pwgcf(currll2chs,currll1chs,:);
  358. currllsfpwSh=currGCsStruct(pp).(gratCondTag{cnd}).pwgcfSh(:,currll2chs,currll1chs,:);
  359. if ~isempty(currllsfpw)
  360. % pool chsxchs within ll1xll2
  361. currllsfpwRes=reshape(currllsfpw,size(currllsfpw,1)*size(currllsfpw,2),size(currllsfpw,3));
  362. currllsfpwResSh=reshape(currllsfpwSh,NShuffles,size(currllsfpwSh,2)*size(currllsfpwSh,3),size(currllsfpwSh,4));
  363. % get rid of all-NaNs rowsacdpt[]
  364. currllsfpwRes((sum(isnan(currllsfpwRes),2)==size(currllsfpwRes,2)),:)=[];
  365. currllsfpwResSh(:,sum(isnan(currllsfpwResSh(1,:,:)),3)==size(currllsfpwResSh,3),:)=[];
  366. sGCsStruct(pp).(gratCondTag{cnd}).([lamLayerTag{ll1} 'to' lamLayerTag{ll2}]).pwgcf=currllsfpwRes;
  367. sGCsStruct(pp).(gratCondTag{cnd}).([lamLayerTag{ll1} 'to' lamLayerTag{ll2}]).pwgcfSh=currllsfpwResSh;
  368. %eval(clearoutmem('currlls*'));
  369. else
  370. sGCsStruct(pp).(gratCondTag{cnd}).([lamLayerTag{ll1} 'to' lamLayerTag{ll2}]).pwgcf=[];
  371. sGCsStruct(pp).(gratCondTag{cnd}).([lamLayerTag{ll1} 'to' lamLayerTag{ll2}]).pwgcfSh=[];
  372. end
  373. currll1algns=currSGIalgns(ll1,~isnan(currSGIalgns(ll1,:)));
  374. currll2algns=currSGIalgns(ll2,~isnan(currSGIalgns(ll2,:)));
  375. if length(currll1chs)~=length(currll1algns) || length(currll2chs)~=length(currll2algns); error('Layer labels do not match alignments.'); end
  376. for algn1=1:length(currll1algns)
  377. for algn2=1:length(currll2algns)
  378. currllalgnsfpw=currGCsStruct(pp).(gratCondTag{cnd}).pwgcf(currll2chs(algn2),currll1chs(algn1),:);
  379. currllalgnsfpwRes=reshape(currllalgnsfpw,1,size(currllalgnsfpw,3));
  380. currllalgnsfpwSh=currGCsStruct(pp).(gratCondTag{cnd}).pwgcfSh(:,currll2chs(algn2),currll1chs(algn1),:);
  381. currllalgnsfpwResSh=reshape(currllalgnsfpwSh,NShuffles,1,size(currllalgnsfpwSh,4));
  382. if currll1algns(algn1)<0
  383. strcurrll1algn1=['m' num2str(abs(currll1algns(algn1)))];
  384. else
  385. strcurrll1algn1=['p' num2str(currll1algns(algn1))];
  386. end
  387. if currll2algns(algn2)<0
  388. strcurrll2algn2=['m' num2str(abs(currll2algns(algn2)))];
  389. else
  390. strcurrll2algn2=['p' num2str(currll2algns(algn2))];
  391. end
  392. if sum(isnan(currllalgnsfpw),2)~=size(currllalgnsfpw,2)
  393. sGCsStruct(pp).(gratCondTag{cnd}).([lamLayerTag{ll1} 'to' lamLayerTag{ll2}]).([strcurrll1algn1 'to' strcurrll2algn2 'pwgcf'])=currllalgnsfpwRes;
  394. sGCsStruct(pp).(gratCondTag{cnd}).([lamLayerTag{ll1} 'to' lamLayerTag{ll2}]).([strcurrll1algn1 'to' strcurrll2algn2 'pwgcfSh'])=currllalgnsfpwResSh;
  395. else
  396. sGCsStruct(pp).(gratCondTag{cnd}).([lamLayerTag{ll1} 'to' lamLayerTag{ll2}]).([strcurrll1algn1 'to' strcurrll2algn2 'pwgcf'])=[];
  397. sGCsStruct(pp).(gratCondTag{cnd}).([lamLayerTag{ll1} 'to' lamLayerTag{ll2}]).([strcurrll1algn1 'to' strcurrll2algn2 'pwgcfSh'])=[];
  398. end
  399. end
  400. end
  401. end
  402. if cnd==2
  403. sGCsStruct(pp).penNum=currGCsStruct(pp).penNum;
  404. sGCsStruct(pp).Labels.(['num' lamLayerTag{ll1}(1) 'chs'])=length(currll1chs);
  405. sGCsStruct(pp).Labels.([lamLayerTag{ll1} 'ChsAlgns'])=currll1algns;
  406. end
  407. end
  408. end
  409. end
  410. end
  411. save(['./' upper(sSubjectName(1)) sSubjectName(end) sVisualArea '_cGCs_' num2str(numInformChs) '_MostInfChs_' compMethod '_PreFirstDim.mat'],'sGCsStruct','-v7.3');
  412. else
  413. load(['./' upper(sSubjectName(1)) sSubjectName(end) sVisualArea '_cGCs_' num2str(numInformChs) '_MostInfChs_' compMethod '_PreFirstDim.mat']);
  414. end
  415. %% POOL ACROSS PENs GCs
  416. PoolGCs=[];
  417. for cnd=1:2
  418. PoolGCs.(gratCondTag{cnd}).AllLayers.pwgcf=[];
  419. PoolGCs.(gratCondTag{cnd}).AllLayers.pwgcfSh=[];
  420. for ll1=1:3
  421. for ll2=1:3
  422. currll1nchs=[];
  423. PoolGCs.(gratCondTag{cnd}).([lamLayerTag{ll1} 'to' lamLayerTag{ll2}])=[];
  424. for pp=1:length(sGCsStruct)
  425. if ~isempty(sGCsStruct(pp).RF)
  426. currSubFieldNames=fieldnames(sGCsStruct(pp).(gratCondTag{cnd}).([lamLayerTag{ll1} 'to' lamLayerTag{ll2}]));
  427. currSubFieldNames=currSubFieldNames(~contains(currSubFieldNames,'Sh'));
  428. for sbfs=1:length(currSubFieldNames)
  429. if ~isfield(PoolGCs.(gratCondTag{cnd}).([lamLayerTag{ll1} 'to' lamLayerTag{ll2}]),currSubFieldNames{sbfs})
  430. PoolGCs.(gratCondTag{cnd}).([lamLayerTag{ll1} 'to' lamLayerTag{ll2}]).(currSubFieldNames{sbfs})=[];
  431. PoolGCs.(gratCondTag{cnd}).([lamLayerTag{ll1} 'to' lamLayerTag{ll2}]).([currSubFieldNames{sbfs} 'Sh'])=[];
  432. end
  433. PoolGCs.(gratCondTag{cnd}).([lamLayerTag{ll1} 'to' lamLayerTag{ll2}]).(currSubFieldNames{sbfs})=...
  434. [PoolGCs.(gratCondTag{cnd}).([lamLayerTag{ll1} 'to' lamLayerTag{ll2}]).(currSubFieldNames{sbfs});...
  435. sGCsStruct(pp).(gratCondTag{cnd}).([lamLayerTag{ll1} 'to' lamLayerTag{ll2}]).(currSubFieldNames{sbfs})];
  436. PoolGCs.(gratCondTag{cnd}).([lamLayerTag{ll1} 'to' lamLayerTag{ll2}]).([currSubFieldNames{sbfs} 'Sh'])=...
  437. cat(2,PoolGCs.(gratCondTag{cnd}).([lamLayerTag{ll1} 'to' lamLayerTag{ll2}]).([currSubFieldNames{sbfs} 'Sh']),...
  438. sGCsStruct(pp).(gratCondTag{cnd}).([lamLayerTag{ll1} 'to' lamLayerTag{ll2}]).([currSubFieldNames{sbfs} 'Sh']));
  439. end
  440. currll1nchs=cat(1,currll1nchs,sGCsStruct(pp).Labels.(['num' lamLayerTag{ll1}(1) 'chs']));
  441. end
  442. end
  443. if cnd==2
  444. PoolGCs.Labels.(['num' lamLayerTag{ll1}(1) 'chs'])=currll1nchs;
  445. end
  446. PoolGCs.(gratCondTag{cnd}).AllLayers.pwgcf=[PoolGCs.(gratCondTag{cnd}).([lamLayerTag{ll1} 'to' lamLayerTag{ll2}]).pwgcf;...
  447. PoolGCs.(gratCondTag{cnd}).AllLayers.pwgcf];
  448. PoolGCs.(gratCondTag{cnd}).AllLayers.pwgcfSh=[PoolGCs.(gratCondTag{cnd}).([lamLayerTag{ll1} 'to' lamLayerTag{ll2}]).pwgcfSh;...
  449. PoolGCs.(gratCondTag{cnd}).AllLayers.pwgcfSh];
  450. end
  451. end
  452. end
  453. %% PLOT PEN AVG GCs Layers x Layers (3x3 subplot)
  454. if plotPenAvgGCs
  455. ylimplot=[0 0];
  456. for lltx=1:3
  457. for llrx=1:3
  458. if ~isempty(PoolGCs.RF.([lamLayerTag{lltx} 'to' lamLayerTag{llrx}]).pwgcf)
  459. ylimplot(1)=min([nanmean(PoolGCs.RF.([lamLayerTag{lltx} 'to' lamLayerTag{llrx}]).pwgcf(:,freqIndices2Plot),1)-...
  460. nansem(PoolGCs.RF.([lamLayerTag{lltx} 'to' lamLayerTag{llrx}]).pwgcf(:,freqIndices2Plot),1)...
  461. nanmean(PoolGCs.OUTrp.([lamLayerTag{lltx} 'to' lamLayerTag{llrx}]).pwgcf(:,freqIndices2Plot),1)-...
  462. nansem(PoolGCs.OUTrp.([lamLayerTag{lltx} 'to' lamLayerTag{llrx}]).pwgcf(:,freqIndices2Plot),1) ylimplot(1)]);
  463. ylimplot(2)=max([nanmean(PoolGCs.RF.([lamLayerTag{lltx} 'to' lamLayerTag{llrx}]).pwgcf(:,freqIndices2Plot),1)+...
  464. nansem(PoolGCs.RF.([lamLayerTag{lltx} 'to' lamLayerTag{llrx}]).pwgcf(:,freqIndices2Plot),1)...
  465. nanmean(PoolGCs.OUTrp.([lamLayerTag{lltx} 'to' lamLayerTag{llrx}]).pwgcf(:,freqIndices2Plot),1)+...
  466. nansem(PoolGCs.OUTrp.([lamLayerTag{lltx} 'to' lamLayerTag{llrx}]).pwgcf(:,freqIndices2Plot),1) ylimplot(2)]);
  467. end
  468. end
  469. end
  470. ylimplot=1*ylimplot; ylimplot(1)=-1e-2;
  471. hfig=figure('units','normalized','outerposition',[0 0 1 1]);
  472. for lltx=1:3
  473. for llrx=1:3
  474. if ~isempty(PoolGCs.RF.([lamLayerTag{lltx} 'to' lamLayerTag{llrx}]).pwgcf)
  475. for ff=1:length(freqAxisPreFirstDimDwSamp)
  476. PoolGCs.pValues.([lamLayerTag{lltx} 'to' lamLayerTag{llrx}]).pvalues(ff)=...
  477. signrank(PoolGCs.RF.([lamLayerTag{lltx} 'to' lamLayerTag{llrx}]).pwgcf(:,ff),...
  478. PoolGCs.OUTrp.([lamLayerTag{lltx} 'to' lamLayerTag{llrx}]).pwgcf(:,ff));
  479. end
  480. pBarVec=PoolGCs.pValues.([lamLayerTag{lltx} 'to' lamLayerTag{llrx}]).pvalues(freqIndices2Plot);
  481. [~,~,~,pBarVecFDR]=fdr_bh(pBarVec);
  482. currLLsPwgcfRFShFreqWise=nan(1,NUM_TIME_DWSAMPLES+1);
  483. currLLsPwgcfOUTrpShFreqWise=nan(1,NUM_TIME_DWSAMPLES+1);
  484. if reportSignifThreshold
  485. for ff=1:(NUM_TIME_DWSAMPLES+1)
  486. currLLsFreqPwgcfRFSh=squeeze(PoolGCs.RF.([lamLayerTag{lltx} 'to' lamLayerTag{llrx}]).pwgcfSh(:,:,ff));
  487. currLLsPwgcfRFShFreqWise(ff)=prctile(nanmean(currLLsFreqPwgcfRFSh,2),95);
  488. currLLsFreqPwgcfOUTrpSh=squeeze(PoolGCs.OUTrp.([lamLayerTag{lltx} 'to' lamLayerTag{llrx}]).pwgcfSh(:,:,ff));
  489. currLLsPwgcfOUTrpShFreqWise(ff)=prctile(nanmean(currLLsFreqPwgcfOUTrpSh,2),95);
  490. end
  491. end
  492. subplot(3,3,(lltx-1)*3+llrx)
  493. plotcmapdots([1 0 0; 0 0 1;.2 .6 .2]);
  494. plot([-inf -inf],[-inf -inf],'r:'); plot([-inf -inf],[-inf -inf],'b:');
  495. plotmsem(freqAxisPreFirstDimDwSamp(freqIndices2Plot),(PoolGCs.RF.([lamLayerTag{lltx} 'to' lamLayerTag{llrx}]).pwgcf(:,freqIndices2Plot)),'r');%[.73 0 0]);
  496. plotmsem(freqAxisPreFirstDimDwSamp(freqIndices2Plot),(PoolGCs.OUTrp.([lamLayerTag{lltx} 'to' lamLayerTag{llrx}]).pwgcf(:,freqIndices2Plot)),'b');%[0 .73 .73]);
  497. plot(freqAxisPreFirstDimDwSamp(freqIndices2Plot),currLLsPwgcfRFShFreqWise(freqIndices2Plot),'r:');
  498. plot(freqAxisPreFirstDimDwSamp(freqIndices2Plot),currLLsPwgcfOUTrpShFreqWise(freqIndices2Plot),'b:');
  499. plot(freqAxisPreFirstDimDwSamp(freqIndices2Plot),double(pBarVecFDR<=0.05)-1+ylimplot(1),'color',[.2 .6 .2],'linewidth',3)
  500. xlim([0 100]); ylim(ylimplot);
  501. title([lamLayerTag{lltx} ' to ' lamLayerTag{llrx}])
  502. if llrx<=lltx
  503. text(100, .9*(ylimplot(2)-ylimplot(1))+ylimplot(1),['tot n=' num2str(size(PoolGCs.RF.([lamLayerTag{lltx} 'to' lamLayerTag{llrx}]).pwgcf,1))],'color','k');
  504. end
  505. if llrx==3 && lltx==1
  506. legend({'RF','OUT','p \leq 0.05','95% sh. perc. RF','95% sh. perc. OUT'},'Location','northeast');
  507. end
  508. if llrx==1
  509. text(100, .8*(ylimplot(2)-ylimplot(1))+ylimplot(1),['avg n=' sprintf('%.2f',mean(PoolGCs.Labels.(['num' lamLayerTag{lltx}(1) 'chs'])))],'color','k');
  510. end
  511. end
  512. end
  513. end
  514. if numInformChs==0
  515. supertitle([sSubjectName ' ' sVisualArea ' Unconditional ' compMethod ' GC - Pre First Dim LFPs [-500,0 ms] - PEN AVG']);
  516. elseif numInformChs==Inf
  517. supertitle([sSubjectName ' ' sVisualArea ' Conditional (to all Complementary Channels) ' compMethod ' GC - Pre First Dim LFPs [-500,0 ms] - PEN AVG']);
  518. elseif numInformChs>0
  519. supertitle([sSubjectName ' ' sVisualArea ' Conditional (to n=' num2str(numInformChs) ' Most Inform. Complem. Chs) ' compMethod ' GC - Pre First Dim LFPs [-500,0 ms] - PEN AVG']);
  520. elseif numInformChs==-Inf
  521. supertitle([sSubjectName ' ' sVisualArea ' Conditional (to Complementary Chs outside current Layers) ' compMethod ' GC - Pre First Dim LFPs [-500,0 ms] - PEN AVG']);
  522. else
  523. supertitle([sSubjectName ' ' sVisualArea ' Conditional (to n=' num2str(abs(numInformChs)) ' Most Inf. Compl. Chs outside current Layers) ' compMethod ' GC - Pre First Dim LFPs [-500,0 ms] - PEN AVG']);
  524. end
  525. end
  526. %% PLOT PEN AVG GCs Layers x Layers (3x3 subplot)
  527. if plotPenAvgGCs
  528. ylimplot=[-1e-2 0.12];
  529. hfig=figure('units','normalized','outerposition',[0 0 1 1]);
  530. for ff=1:length(freqAxisPreFirstDimDwSamp)
  531. PoolGCs.pValues.AllLayers.pvalues(ff)=signrank(PoolGCs.RF.AllLayers.pwgcf(:,ff),PoolGCs.OUTrp.AllLayers.pwgcf(:,ff));
  532. end
  533. pBarVec=PoolGCs.pValues.AllLayers.pvalues(freqIndices2Plot);
  534. [~,~,~,pBarVecFDR]=fdr_bh(pBarVec);
  535. currLLsPwgcfRFShFreqWise=nan(1,NUM_TIME_DWSAMPLES+1);
  536. currLLsPwgcfOUTrpShFreqWise=nan(1,NUM_TIME_DWSAMPLES+1);
  537. if reportSignifThreshold
  538. for ff=1:(NUM_TIME_DWSAMPLES+1)
  539. currLLsFreqPwgcfRFSh=squeeze(PoolGCs.RF.([lamLayerTag{lltx} 'to' lamLayerTag{llrx}]).pwgcfSh(:,:,ff));
  540. currLLsPwgcfRFShFreqWise(ff)=prctile(nanmean(currLLsFreqPwgcfRFSh,2),95);
  541. currLLsFreqPwgcfOUTrpSh=squeeze(PoolGCs.OUTrp.([lamLayerTag{lltx} 'to' lamLayerTag{llrx}]).pwgcfSh(:,:,ff));
  542. currLLsPwgcfOUTrpShFreqWise(ff)=prctile(nanmean(currLLsFreqPwgcfOUTrpSh,2),95);
  543. end
  544. end
  545. plotcmapdots([1 0 0; 0 0 1;.2 .6 .2]);
  546. plot([-inf -inf],[-inf -inf],'r:'); plot([-inf -inf],[-inf -inf],'b:');
  547. plotmsem(freqAxisPreFirstDimDwSamp(freqIndices2Plot),(PoolGCs.RF.AllLayers.pwgcf(:,freqIndices2Plot)),'r');%[.73 0 0]);
  548. plotmsem(freqAxisPreFirstDimDwSamp(freqIndices2Plot),(PoolGCs.OUTrp.AllLayers.pwgcf(:,freqIndices2Plot)),'b');%[0 .73 .73]);
  549. plot(freqAxisPreFirstDimDwSamp(freqIndices2Plot),currLLsPwgcfRFShFreqWise(freqIndices2Plot),'r:');
  550. plot(freqAxisPreFirstDimDwSamp(freqIndices2Plot),currLLsPwgcfOUTrpShFreqWise(freqIndices2Plot),'b:');
  551. plot(freqAxisPreFirstDimDwSamp(freqIndices2Plot),double(pBarVecFDR<=0.05)-1+ylimplot(1),'color',[.2 .6 .2],'linewidth',3)
  552. xlim([0 100]); ylim(ylimplot);
  553. text(90, .9*(ylimplot(2)-ylimplot(1))+ylimplot(1),['tot n=' num2str(size(PoolGCs.RF.AllLayers.pwgcf,1))],'color','k');
  554. if llrx==3 && lltx==1
  555. legend({'RF','OUT','p \leq 0.05','95% sh. perc. RF','95% sh. perc. OUT'},'Location','northeast');
  556. end
  557. if numInformChs==0
  558. supertitle([sSubjectName ' ' sVisualArea ' Unconditional ' compMethod ' GC - Pre First Dim LFPs [-500,0 ms] - PEN AVG']);
  559. elseif numInformChs==Inf
  560. supertitle([sSubjectName ' ' sVisualArea ' Conditional (to all Complementary Channels) ' compMethod ' GC - Pre First Dim LFPs [-500,0 ms] - PEN AVG']);
  561. elseif numInformChs>0
  562. supertitle([sSubjectName ' ' sVisualArea ' Conditional (to n=' num2str(numInformChs) ' Most Inform. Complem. Chs) ' compMethod ' GC - Pre First Dim LFPs [-500,0 ms] - PEN AVG']);
  563. elseif numInformChs==-Inf
  564. supertitle([sSubjectName ' ' sVisualArea ' Conditional (to Complementary Chs outside current Layers) ' compMethod ' GC - Pre First Dim LFPs [-500,0 ms] - PEN AVG']);
  565. else
  566. supertitle([sSubjectName ' ' sVisualArea ' Conditional (to n=' num2str(abs(numInformChs)) ' Most Inf. Compl. Chs outside current Layers) ' compMethod ' GC - Pre First Dim LFPs [-500,0 ms] - PEN AVG']);
  567. end
  568. end