process_cGC_between_V1V4_MultiMethods_PreFirstDim.m 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. addpath(genpath('./support_routines'));
  2. addpath(genpath('./support_tools'));
  3. clear all; close all; clc;
  4. sSubjectName='monkey 1';
  5. sVisualArea='V1V4';
  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. subjectDataV1=getSubjectData(sSubjectName,'V1','ALL','LFPs','ALL');
  44. subjectDataV4=getSubjectData(sSubjectName,'V4','ALL','LFPs','ALL');
  45. % REMOVE DATA IN TIME WINDOWS OTHER THAN PREFIRSTDIM TO SAVE MEMORY
  46. rmpp=[];
  47. for pp=1:length(subjectDataV1.LfpStruct)
  48. if ~isempty(subjectDataV1.LfpStruct(pp).Sorted)
  49. rmfn=fieldnames(subjectDataV1.LfpStruct(pp).Sorted.Full.Supra.RF);
  50. rmfn(cellfun(@(fnm) strcmpi(fnm,'PreFirstDimDataBiZSc'),rmfn))=[];
  51. for ll=1:3
  52. subjectDataV1.LfpStruct(pp).Sorted.Full.(lamLayerTag{ll}).RF=rmfield(subjectDataV1.LfpStruct(pp).Sorted.Full.(lamLayerTag{ll}).RF,rmfn);
  53. subjectDataV1.LfpStruct(pp).Sorted.Full.(lamLayerTag{ll}).OUT1=rmfield(subjectDataV1.LfpStruct(pp).Sorted.Full.(lamLayerTag{ll}).OUT1,rmfn);
  54. subjectDataV1.LfpStruct(pp).Sorted.Full.(lamLayerTag{ll}).OUT2=rmfield(subjectDataV1.LfpStruct(pp).Sorted.Full.(lamLayerTag{ll}).OUT2,rmfn);
  55. end
  56. else
  57. rmpp=cat(1,rmpp,pp);
  58. end
  59. end
  60. subjectDataV1.LfpStruct(rmpp)=[]; subjectDataV1.penInfos(rmpp)=[]; subjectDataV1.penIDs(rmpp)=[];
  61. rmpp=[];
  62. for pp=1:length(subjectDataV4.LfpStruct)
  63. if ~isempty(subjectDataV4.LfpStruct(pp).Sorted)
  64. rmfn=fieldnames(subjectDataV4.LfpStruct(pp).Sorted.Full.Supra.RF);
  65. rmfn(cellfun(@(fnm) strcmpi(fnm,'PreFirstDimDataBiZSc'),rmfn))=[];
  66. for ll=1:3
  67. subjectDataV4.LfpStruct(pp).Sorted.Full.(lamLayerTag{ll}).RF=rmfield(subjectDataV4.LfpStruct(pp).Sorted.Full.(lamLayerTag{ll}).RF,rmfn);
  68. subjectDataV4.LfpStruct(pp).Sorted.Full.(lamLayerTag{ll}).OUT1=rmfield(subjectDataV4.LfpStruct(pp).Sorted.Full.(lamLayerTag{ll}).OUT1,rmfn);
  69. subjectDataV4.LfpStruct(pp).Sorted.Full.(lamLayerTag{ll}).OUT2=rmfield(subjectDataV4.LfpStruct(pp).Sorted.Full.(lamLayerTag{ll}).OUT2,rmfn);
  70. end
  71. else
  72. rmpp=cat(1,rmpp,pp);
  73. end
  74. end
  75. subjectDataV4.LfpStruct(rmpp)=[]; subjectDataV4.penInfos(rmpp)=[]; subjectDataV4.penIDs(rmpp)=[];
  76. %% FIND PENs W/SAME TRIALS IN V1 AND V4
  77. penIDsV1=arrayfun(@(jj) subjectDataV1.LfpStruct(jj).penNum,1:length(subjectDataV1.LfpStruct));
  78. penIDsV4=arrayfun(@(jj) subjectDataV4.LfpStruct(jj).penNum,1:length(subjectDataV4.LfpStruct));
  79. penIDsV1V4=intersect(penIDsV1,penIDsV4);% penIDsV1(arrayfun(@(jj) any(penIDsV4==penIDsV1(jj)), 1:length(penIDsV1)));
  80. penIndicesV1=arrayfun(@(jj) find(penIDsV1==penIDsV1V4(jj)), 1:length(penIDsV1V4));
  81. penIndicesV4=arrayfun(@(jj) find(penIDsV4==penIDsV1V4(jj)), 1:length(penIDsV1V4));
  82. sLfpStructV1V4=[];
  83. parfor pp=1:length(penIDsV1V4) % LOOP OVER PENS
  84. if (subjectDataV1.LfpStruct(penIndicesV1(pp)).penNum==subjectDataV4.LfpStruct(penIndicesV4(pp)).penNum)
  85. sLfpStructV1V4(pp).penNum=subjectDataV1.LfpStruct(penIndicesV1(pp)).penNum;
  86. else
  87. error('PEN Numbers in V1 and V4 do not match.');
  88. end
  89. for cnd=1:3 % LOOP OVER ATTENTIONAL CONDITIONS
  90. % FIND TRIALS WITH SAME EVENT IDs FOR V1 AND V4
  91. currPenCndTrialIDsV1=subjectDataV1.LfpStruct(penIndicesV1(pp)).Sorted.Labels.selectedNlxEvents(remod(ceil(subjectDataV1.LfpStruct(penIndicesV1(pp)).Sorted.Labels.GratCondition/6),3)==cnd);
  92. currPenCndTrialIDsV4=subjectDataV4.LfpStruct(penIndicesV4(pp)).Sorted.Labels.selectedNlxEvents(remod(ceil(subjectDataV4.LfpStruct(penIndicesV4(pp)).Sorted.Labels.GratCondition/6),3)==cnd);
  93. currPenCndTrialIDsV1V4=intersect(currPenCndTrialIDsV1,currPenCndTrialIDsV4);
  94. currPenCndTrialIndicesV1=arrayfun(@(jj) find(currPenCndTrialIDsV1==currPenCndTrialIDsV1V4(jj)), 1:length(currPenCndTrialIDsV1V4));
  95. currPenCndTrialIndicesV4=arrayfun(@(jj) find(currPenCndTrialIDsV4==currPenCndTrialIDsV1V4(jj)), 1:length(currPenCndTrialIDsV1V4));
  96. if ~all(currPenCndTrialIDsV1(currPenCndTrialIndicesV1) == currPenCndTrialIDsV4(currPenCndTrialIndicesV4))
  97. error('Some of the trials IDs for V1 do not match trial IDs for V4.');
  98. end
  99. % FIND TRIALS WITH SAME FIRST DIM DELAY FOR V1 AND V4
  100. currPenCndFirstDimDelayV1=subjectDataV1.LfpStruct(penIndicesV1(pp)).Sorted.Delays.FirstDim(remod(ceil(subjectDataV1.LfpStruct(penIndicesV1(pp)).Sorted.Labels.GratCondition/6),3)==cnd);
  101. currPenCndFirstDimDelayV4=subjectDataV4.LfpStruct(penIndicesV4(pp)).Sorted.Delays.FirstDim(remod(ceil(subjectDataV4.LfpStruct(penIndicesV4(pp)).Sorted.Labels.GratCondition/6),3)==cnd);
  102. if ~all(currPenCndFirstDimDelayV1(currPenCndTrialIndicesV1)==currPenCndFirstDimDelayV4(currPenCndTrialIndicesV4))
  103. error('FirstDim Delays not matching in V1 and V4 trials');
  104. end
  105. for lls=1:3 % LOOP OVER LAMINAR LAYERS PICK SIMULTANEOUSLY VALID TRIALS
  106. cnd134=cnd; if cnd>1; cnd134=cnd+1; end
  107. sLfpStructV1V4(pp).V1.Sorted.Full.(lamLayerTag{lls}).(gratCondTag{cnd134}).PreFirstDimDataBiZSc=...
  108. subjectDataV1.LfpStruct(penIndicesV1(pp)).Sorted.Full.(lamLayerTag{lls}).(gratCondTag{cnd134}).PreFirstDimDataBiZSc(:,currPenCndTrialIndicesV1,:);
  109. sLfpStructV1V4(pp).V4.Sorted.Full.(lamLayerTag{lls}).(gratCondTag{cnd134}).PreFirstDimDataBiZSc=...
  110. subjectDataV4.LfpStruct(penIndicesV4(pp)).Sorted.Full.(lamLayerTag{lls}).(gratCondTag{cnd134}).PreFirstDimDataBiZSc(:,currPenCndTrialIndicesV4,:);
  111. end
  112. end
  113. sLfpStructV1V4(pp).V1.Sorted.Labels.SupraChsLamAlignsBi=subjectDataV1.LfpStruct(penIndicesV1(pp)).Sorted.Labels.SupraChsLamAlignsBi;
  114. sLfpStructV1V4(pp).V1.Sorted.Labels.GranrChsLamAlignsBi=subjectDataV1.LfpStruct(penIndicesV1(pp)).Sorted.Labels.GranrChsLamAlignsBi;
  115. sLfpStructV1V4(pp).V1.Sorted.Labels.InfraChsLamAlignsBi=subjectDataV1.LfpStruct(penIndicesV1(pp)).Sorted.Labels.InfraChsLamAlignsBi;
  116. sLfpStructV1V4(pp).V4.Sorted.Labels.SupraChsLamAlignsBi=subjectDataV4.LfpStruct(penIndicesV4(pp)).Sorted.Labels.SupraChsLamAlignsBi;
  117. sLfpStructV1V4(pp).V4.Sorted.Labels.GranrChsLamAlignsBi=subjectDataV4.LfpStruct(penIndicesV4(pp)).Sorted.Labels.GranrChsLamAlignsBi;
  118. sLfpStructV1V4(pp).V4.Sorted.Labels.InfraChsLamAlignsBi=subjectDataV4.LfpStruct(penIndicesV4(pp)).Sorted.Labels.InfraChsLamAlignsBi;
  119. end
  120. %% RANDPERM FOR AVG SUBSAMPLING OF TRIALS AT OUTSIDE LOCATIONS
  121. rng(0); % will pick exactly the same trials at every run,
  122. % Note: varying this seed might give slightly different cGC magnitudes
  123. % (different trials are selected) but results are robust to permutation
  124. for pp=1:length(penIDsV1V4)
  125. currNtrRF=size(sLfpStructV1V4(pp).V1.Sorted.Full.Supra.RF.PreFirstDimDataBiZSc,2);
  126. currNtrOUT1=size(sLfpStructV1V4(pp).V1.Sorted.Full.Supra.OUT1.PreFirstDimDataBiZSc,2);
  127. currNtrOUT2=size(sLfpStructV1V4(pp).V1.Sorted.Full.Supra.OUT2.PreFirstDimDataBiZSc,2);
  128. currV1V4OUTrp=randperm(currNtrOUT1+currNtrOUT2,currNtrRF);
  129. for lls=1:3
  130. currOUTcatV1=cat(2,sLfpStructV1V4(pp).V1.Sorted.Full.(lamLayerTag{lls}).OUT1.PreFirstDimDataBiZSc,...
  131. sLfpStructV1V4(pp).V1.Sorted.Full.(lamLayerTag{lls}).OUT2.PreFirstDimDataBiZSc);
  132. currOUTcatV4=cat(2,sLfpStructV1V4(pp).V4.Sorted.Full.(lamLayerTag{lls}).OUT1.PreFirstDimDataBiZSc,...
  133. sLfpStructV1V4(pp).V4.Sorted.Full.(lamLayerTag{lls}).OUT2.PreFirstDimDataBiZSc);
  134. sLfpStructV1V4(pp).V1.Sorted.Full.(lamLayerTag{lls}).OUTrp.PreFirstDimDataBiZSc=currOUTcatV1(:,currV1V4OUTrp,:);
  135. sLfpStructV1V4(pp).V4.Sorted.Full.(lamLayerTag{lls}).OUTrp.PreFirstDimDataBiZSc=currOUTcatV4(:,currV1V4OUTrp,:);
  136. end
  137. end
  138. eval(clearoutmem('curr*'));
  139. %% Information Toolbox settings (support_tools) - DOI: 10.1186/1471-2202-10-81
  140. numQuantBins=4;
  141. MImethod='dr';
  142. MIbias='qe';
  143. MIbtsp=0;
  144. bandWinsWidth=2;
  145. bandWinsStart=.01; % Starts at 1 Hz
  146. bandWinsEnd=100; % Stops at ~100 Hz
  147. bandWinsEnd=min(bandWinsEnd,SAMPLING_FREQ/2);
  148. NUM_BAND_WINS=floor((bandWinsEnd-bandWinsStart)/bandWinsWidth);
  149. bandWinsCutoffs=[linspace(bandWinsStart,bandWinsEnd-bandWinsWidth,NUM_BAND_WINS);...
  150. linspace(bandWinsStart,bandWinsEnd-bandWinsWidth,NUM_BAND_WINS)+bandWinsWidth]';
  151. freqAxisBandWins=mean(bandWinsCutoffs,2);
  152. filterOrder=2;
  153. filterBCoeffs=nan(NUM_BAND_WINS,2*filterOrder+1);
  154. filterACoeffs=nan(NUM_BAND_WINS,2*filterOrder+1);
  155. filterFreqResp=nan(NUM_BAND_WINS,512);
  156. filterImpResp=nan(NUM_BAND_WINS,2048);
  157. filterFreqAxis=nan(1,512);
  158. filterTimeAxis=nan(1,512);
  159. for bw=1:NUM_BAND_WINS
  160. [filterBCoeffs(bw,:),filterACoeffs(bw,:)]=butter(filterOrder,bandWinsCutoffs(bw,:)./(SAMPLING_FREQ/2),'bandpass');
  161. [filterFreqResp(bw,:),filterFreqAxis]=freqz(filterBCoeffs(bw,:),filterACoeffs(bw,:),512,SAMPLING_FREQ);
  162. [filterImpResp(bw,:),filterTimeAxis]=impz(filterBCoeffs(bw,:),filterACoeffs(bw,:),2048,SAMPLING_FREQ);
  163. end
  164. fresEstim=1/4*NUM_TIME_DWSAMPLES;
  165. lambdafreqAxis=linspace(0,freqAxisPreFirstDimDwSamp(end),fresEstim+1);
  166. %% MVGC Toolbox settings (support_tools) - DOI: 10.1016/j.jneumeth.2013.10.018
  167. regmode='LWR';
  168. morder=10; % set up by optimization of AIC and R² (see Suppl. Figure S7)
  169. acmaxlags=[]; % autocovariance max lags
  170. acdectol=[]; % autocovariance decay tolerance (sets memory of the process)
  171. NShuffles=100; % number of shuffles used to compute significance threshold
  172. for pp=1:length(sLfpStructV1V4)
  173. for cnd=1:2
  174. disp(['Processing cGC for ' sSubjectName ' ' sVisualArea ' PEN ' num2str(sLfpStructV1V4(pp).penNum) ', ' gratCondTag{cnd}]);
  175. currX0=[sLfpStructV1V4(pp).V1.Sorted.Full.Supra.(gratCondTag{cnd}).PreFirstDimDataBiZSc(:,:,NUM_TIME_SAMPLES_EXTRACTED-NUM_TIME_SAMPLES+1:end);...
  176. sLfpStructV1V4(pp).V1.Sorted.Full.Granr.(gratCondTag{cnd}).PreFirstDimDataBiZSc(:,:,NUM_TIME_SAMPLES_EXTRACTED-NUM_TIME_SAMPLES+1:end);...
  177. sLfpStructV1V4(pp).V1.Sorted.Full.Infra.(gratCondTag{cnd}).PreFirstDimDataBiZSc(:,:,NUM_TIME_SAMPLES_EXTRACTED-NUM_TIME_SAMPLES+1:end);...
  178. sLfpStructV1V4(pp).V4.Sorted.Full.Supra.(gratCondTag{cnd}).PreFirstDimDataBiZSc(:,:,NUM_TIME_SAMPLES_EXTRACTED-NUM_TIME_SAMPLES+1:end);...
  179. sLfpStructV1V4(pp).V4.Sorted.Full.Granr.(gratCondTag{cnd}).PreFirstDimDataBiZSc(:,:,NUM_TIME_SAMPLES_EXTRACTED-NUM_TIME_SAMPLES+1:end);...
  180. sLfpStructV1V4(pp).V4.Sorted.Full.Infra.(gratCondTag{cnd}).PreFirstDimDataBiZSc(:,:,NUM_TIME_SAMPLES_EXTRACTED-NUM_TIME_SAMPLES+1:end)];
  181. %swap time and trial dimensions
  182. currXp=permute(currX0,[1 3 2]);
  183. %eval(clearoutmem('currX0'));
  184. currV1Schs=1:size(sLfpStructV1V4(pp).V1.Sorted.Full.Supra.(gratCondTag{cnd}).PreFirstDimDataBiZSc,1);
  185. currV1Gchs=max([currV1Schs 0])+(1:size(sLfpStructV1V4(pp).V1.Sorted.Full.Granr.(gratCondTag{cnd}).PreFirstDimDataBiZSc,1));
  186. currV1Ichs=max([currV1Gchs currV1Schs 0])+(1:size(sLfpStructV1V4(pp).V1.Sorted.Full.Infra.(gratCondTag{cnd}).PreFirstDimDataBiZSc,1));
  187. currV4Schs=max([currV1Ichs currV1Gchs currV1Schs 0])+(1:size(sLfpStructV1V4(pp).V4.Sorted.Full.Supra.(gratCondTag{cnd}).PreFirstDimDataBiZSc,1));
  188. currV4Gchs=max([currV4Schs currV1Ichs currV1Gchs currV1Schs 0])+(1:size(sLfpStructV1V4(pp).V4.Sorted.Full.Granr.(gratCondTag{cnd}).PreFirstDimDataBiZSc,1));
  189. currV4Ichs=max([currV4Gchs currV4Schs currV1Ichs currV1Gchs currV1Schs 0])+(1:size(sLfpStructV1V4(pp).V4.Sorted.Full.Infra.(gratCondTag{cnd}).PreFirstDimDataBiZSc,1));
  190. % required if you run parfor pp=1:length(sLfpStructV1V4) for transparency constraints
  191. currV1SGIchs=nan(3,16); % set to 16, higher than every max_{j=I,G,S}{length(currjchs)}, allows to cat along 1st dim.
  192. currV1SGIchs(1,1:length(currV1Schs))=currV1Schs;
  193. currV1SGIchs(2,1:length(currV1Gchs))=currV1Gchs;
  194. currV1SGIchs(3,1:length(currV1Ichs))=currV1Ichs;
  195. currV4SGIchs=nan(3,16);
  196. currV4SGIchs(1,1:length(currV4Schs))=currV4Schs;
  197. currV4SGIchs(2,1:length(currV4Gchs))=currV4Gchs;
  198. currV4SGIchs(3,1:length(currV4Ichs))=currV4Ichs;
  199. currV1SGIalgns=nan(3,16);
  200. currV1SGIalgns(1,1:length(currV1Schs))=sLfpStructV1V4(pp).V1.Sorted.Labels.SupraChsLamAlignsBi;
  201. currV1SGIalgns(2,1:length(currV1Gchs))=sLfpStructV1V4(pp).V1.Sorted.Labels.GranrChsLamAlignsBi;
  202. currV1SGIalgns(3,1:length(currV1Ichs))=sLfpStructV1V4(pp).V1.Sorted.Labels.InfraChsLamAlignsBi;
  203. currV4SGIalgns=nan(3,16);
  204. currV4SGIalgns(1,1:length(currV4Schs))=sLfpStructV1V4(pp).V4.Sorted.Labels.SupraChsLamAlignsBi;
  205. currV4SGIalgns(2,1:length(currV4Gchs))=sLfpStructV1V4(pp).V4.Sorted.Labels.GranrChsLamAlignsBi;
  206. currV4SGIalgns(3,1:length(currV4Ichs))=sLfpStructV1V4(pp).V4.Sorted.Labels.InfraChsLamAlignsBi;
  207. % De-trend along trial-dimension may help for stationarity aspects
  208. currXDetr=mvdetrend(currXp);
  209. % De-mean along time-dimension
  210. currXDemn=currXDetr-repmat(mean(currXDetr,2),[1 NUM_TIME_SAMPLES 1]);
  211. % Downsampling by movmean
  212. currXDemnp=permute(currXDemn,[2 1 3]);
  213. currXDownp=movmeandecimatrix(currXDemnp,DOWNSAMPLING_FACTOR);
  214. currXDwSamp=permute(currXDownp,[2 1 3]);
  215. % Downsampling by brute decimation
  216. %currXDwSamp=currXDemn(:,1:DOWNSAMPLING_FACTOR:end,:);
  217. [currNvars,currNobs,currNtrials]=size(currXDwSamp);
  218. currfpw=nan(currNvars,currNvars,NUM_TIME_DWSAMPLES+1);
  219. currfpwSh=nan(NShuffles,currNvars,currNvars,NUM_TIME_DWSAMPLES+1);
  220. currFFiltHilbPowDown=nan(currNvars,NUM_BAND_WINS,currNtrials,NUM_TIME_DWSAMPLES/DOWNSAMPLING_FACTOR);
  221. % BINNING AND INFORMATION EVALUATION
  222. paramsMI=[];
  223. paramsMI.method=MImethod;
  224. paramsMI.bias=MIbias;
  225. paramsMI.btsp=MIbtsp;
  226. currMIMat=nan(NUM_BAND_WINS,NUM_TIME_DWSAMPLES/DOWNSAMPLING_FACTOR,currNvars,currNvars);
  227. for bw=1:NUM_BAND_WINS % LOOP OVER BAND WINDOWS
  228. for cch=1:currNvars
  229. % Filter for each band window
  230. currBwinFFilt=filtfilt(filterBCoeffs(bw,:),filterACoeffs(bw,:),squeeze(currXDwSamp(cch,:,:)));
  231. %currBwinFFiltABCtot(:,bw,:)=currBwinFFiltABC;
  232. % Hilbert Transform and abs^2
  233. currBwinFFiltHilbPow=(abs(hilbertpad(currBwinFFilt)).^2);
  234. %currBwinFFiltHilbPowtot(:,bw,:)=currBwinFFiltHilbPow;
  235. % Downsampling by moving average [ch x NUM_BAND_WINS x trials x NUM_TIME_DWSAMAPLES]
  236. currFFiltHilbPowDown(cch,bw,:,:)=movmeandecimatrix(currBwinFFiltHilbPow,DOWNSAMPLING_FACTOR)';
  237. end
  238. % [currNvars x NBWINS x currNtrials x NTIMEDWS]
  239. currBwXDwMat=reshape(vectorisen(currFFiltHilbPowDown(:,bw,:,:),[2 3]),[1 currNvars*currNtrials*NUM_TIME_DWSAMPLES/DOWNSAMPLING_FACTOR]);
  240. currChsLabels=reshape(repmat(1:currNvars,currNtrials*NUM_TIME_DWSAMPLES/DOWNSAMPLING_FACTOR,1)',[1 currNvars*currNtrials*NUM_TIME_DWSAMPLES/DOWNSAMPLING_FACTOR]);
  241. % [1 x (currNtrials*NTIMEDWS) x currNvars]
  242. [currRespMatAllChs, currnTrAllChs]= buildr(currChsLabels,currBwXDwMat);
  243. currRespMatBinAllChs = binr(currRespMatAllChs, currnTrAllChs, numQuantBins, 'eqpop');
  244. currRespMatBinAllChsRes=reshape(currRespMatBinAllChs(1,:,:),[currNtrials NUM_TIME_DWSAMPLES/DOWNSAMPLING_FACTOR currNvars]);
  245. paramsMI.nt=currNtrials;%currnTrAllChs(1);
  246. for tt=1:(NUM_TIME_DWSAMPLES/DOWNSAMPLING_FACTOR)
  247. for ch1=1:currNvars
  248. for ch2=ch1:currNvars
  249. currMIMat(bw,tt,ch1,ch2)=information(reshape(currRespMatBinAllChsRes(:,tt,[ch1 ch2]),[1 currNtrials 2]), paramsMI, 'I');
  250. end
  251. end
  252. end
  253. end
  254. currTimeCumulInformMat=squeeze(nansum(vectorisen(currMIMat,[1 2]),2));%squeeze(nansum(currMIMat,1));
  255. currTimeCumulInformMat2=currTimeCumulInformMat+triu(currTimeCumulInformMat',-1);
  256. currSrtMIMat=nan(currNvars,currNvars);
  257. currSrtMIChs=nan(currNvars,currNvars);
  258. currSrtRangeNormMIMat=nan(currNvars,currNvars);
  259. for ch=1:currNvars
  260. [currSrtMIMat(ch,:),currSrtMIChs(ch,:)]=sort(currTimeCumulInformMat2(ch,:),'descend');
  261. end
  262. for chY=1:currNvars
  263. for chX=1:currNvars
  264. if chX~=chY %required by parfor, ch2=ch1:nvars is not allowed
  265. chsZOut=[];
  266. if ~(any(chX==currV1Schs) || any(chY==currV1Schs))
  267. chsZOut=currV1Schs;
  268. end
  269. if ~(any(chX==currV1Gchs) || any(chY==currV1Gchs))
  270. chsZOut=[chsZOut currV1Gchs];
  271. end
  272. if ~(any(chX==currV1Ichs) || any(chY==currV1Ichs))
  273. chsZOut=[chsZOut currV1Ichs];
  274. end
  275. if ~(any(chX==currV4Schs) || any(chY==currV4Schs))
  276. chsZOut=[chsZOut currV4Schs];
  277. end
  278. if ~(any(chX==currV4Gchs) || any(chY==currV4Gchs))
  279. chsZOut=[chsZOut currV4Gchs];
  280. end
  281. if ~(any(chX==currV4Ichs) || any(chY==currV4Ichs))
  282. chsZOut=[chsZOut currV4Ichs];
  283. end
  284. if numInformChs==0
  285. chsZ=[];
  286. elseif numInformChs>0
  287. chsZMIwrtX=currSrtMIChs(chX,1:min(numInformChs,end));
  288. chsZMIwrtY=currSrtMIChs(chY,1:min(numInformChs,end));
  289. chsZ=setdiff(unique([chsZMIwrtX,chsZMIwrtY],'stable'),[chX chY]);
  290. else
  291. if numInformChs==-inf
  292. chsZ=chsZOut;
  293. else
  294. numInformChsAbs=abs(numInformChs);
  295. chsZMIwrtX=intersect(currSrtMIChs(chX,1:min(numInformChsAbs,end)),chsZOut,'stable');
  296. chsZMIwrtY=intersect(currSrtMIChs(chY,1:min(numInformChsAbs,end)),chsZOut,'stable');
  297. chsZ=unique([chsZMIwrtX,chsZMIwrtY],'stable');
  298. end
  299. end
  300. numZvars=length(chsZ);
  301. disp([sSubjectName '-' sVisualArea '-PEN-' num2str(sLfpStructV1V4(pp).penNum) '-' gratCondTag{cnd} '-' compMethod '-cGC_( ' num2str(chY) ' -> ' num2str(chX) ' \ ' num2str(chsZ) ')']);
  302. % FIT A VAR(p) MODEL FOR (X,Y,Z)
  303. [BxyzMat,ExyzMat]=tsdata_to_var(currXDwSamp([chX chY chsZ],:,:),morder,regmode);
  304. assert(~isbad(BxyzMat),'VAR estimation failed');
  305. if strcmpi(compMethod,'Mvgc') && (chX < chY)
  306. % Autocovariance calculation (<mvgc|A5|>)
  307. [GammaMat,GammaInfo] = var_to_autocov(BxyzMat,ExyzMat,acmaxlags,acdectol);
  308. var_info(GammaInfo,true); % report results (and bail out on error)
  309. % Pairwise Granger causality calculation: frequency domain (<mvgc|A14|>)
  310. %f_multichs_fres = autocov_to_spwcgc(GammaMat,fresEstim);
  311. f_multichs_fres_21 = autocov_to_smvgc(GammaMat,2,1,fresEstim); % from 1 to 2
  312. f_multichs_fres_12 = autocov_to_smvgc(GammaMat,1,2,fresEstim); % from 2 to 1
  313. % assert(~isbad(f_multichs_fres,false),'Mvgc spectral GC calculation failed');
  314. % if fres=[] the freqAxis ends up being non-uniform
  315. % currch1ch2freqAxis=linspace(0,DWSAMPLING_FREQ/2,size(multichsfpw,3));
  316. % so interp1 is used to set a uniform output fres
  317. f_y_to_x_given_z=interp1(lambdafreqAxis,squeeze(f_multichs_fres_12),freqAxisPreFirstDimDwSamp,'spline');
  318. f_x_to_y_given_z=interp1(lambdafreqAxis,squeeze(f_multichs_fres_21),freqAxisPreFirstDimDwSamp,'spline');
  319. currfpw(chX,chY,:)=abs(f_y_to_x_given_z);
  320. currfpw(chY,chX,:)=abs(f_x_to_y_given_z);
  321. if reComputeGCsShuffling %SHUFFLE TRIALS
  322. for nnsh=1:NShuffles
  323. currXDwSampShuf=nan(size(currXDwSamp([chX chY chsZ],:,:)));
  324. for chshuf=1:length([chX chY chsZ])
  325. currXDwSampShuf(chshuf,:,:)=currXDwSamp(chshuf,:,randperm(currNtrials));
  326. end
  327. [BxyzMatShuf,ExyzMatShuf]=tsdata_to_var(currXDwSampShuf,morder,regmode);
  328. assert(~isbad(BxyzMatShuf),'VAR estimation failed');
  329. [GammaMatShuf,GammaInfoShuf] = var_to_autocov(BxyzMatShuf,ExyzMatShuf,acmaxlags,acdectol);
  330. %f_multichs_fres_Sh = autocov_to_spwcgc(GammaMatShuf,fresEstim);
  331. f_multichs_fres_Sh_21 = autocov_to_smvgc(GammaMatShuf,2,1,fresEstim);
  332. f_multichs_fres_Sh_12 = autocov_to_smvgc(GammaMatShuf,1,2,fresEstim);
  333. %assert(~isbad(f_multichs_fres_Sh,false),'Mvgc spectral GC calculation failed');
  334. f_y_to_x_given_z_sh=interp1(lambdafreqAxis,squeeze(f_multichs_fres_Sh_12),freqAxisPreFirstDimDwSamp,'spline');
  335. f_x_to_y_given_z_sh=interp1(lambdafreqAxis,squeeze(f_multichs_fres_Sh_21),freqAxisPreFirstDimDwSamp,'spline');
  336. currfpwSh(nnsh,chX,chY,:)=abs(f_y_to_x_given_z_sh);
  337. currfpwSh(nnsh,chY,chX,:)=abs(f_x_to_y_given_z_sh);
  338. % max(currfpwSh(:,chX,chY,:),[],4) is to be used for prctile
  339. end
  340. end
  341. end
  342. if strcmpi(compMethod,'Geweke')
  343. % ORIGINAL GEWEKE SPECTRAL FACTORIZATION METHOD
  344. % (Geweke, J. of the American Stat. Assoc., 1984)
  345. % FIT A SECOND, REDUCED VAR(p) MODEL FOR (X,Z)
  346. [DxzMat,ExzMat]=tsdata_to_var(currXDwSamp([chX chsZ],:,:),morder,regmode);
  347. assert(~isbad(DxzMat),'VAR estimation failed');
  348. % P MATRICES FOR COVARIANCE NORMALIZATION
  349. PxzMat=[1 zeros(1,numZvars); -ExzMat(1,2:end)'/(ExzMat(1,1)) eye(numZvars)];
  350. 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)]*...
  351. [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)];
  352. % APPLYING NORMALIZATION BY P-MATRICES MULTIPLICATION IN TIME
  353. PExzMat=PxzMat*ExzMat*PxzMat';
  354. PExyzMat=PxyzMat*ExyzMat*PxyzMat';
  355. PDxzMat=nan(numZvars+1,numZvars+1,morder); PBxyzMat=nan(numZvars+2,numZvars+2,morder);
  356. for mm=1:morder
  357. PDxzMat(:,:,mm)=PxzMat*DxzMat(:,:,mm);
  358. PBxyzMat(:,:,mm)=PxyzMat*BxyzMat(:,:,mm);
  359. end
  360. [PSxzMat,PGxzMat]=var_to_cpsd(PDxzMat,PExzMat,fresEstim);
  361. [PSxyzMat,PHxyzMat]=var_to_cpsd(PBxyzMat,PExyzMat,fresEstim);
  362. % APPLYING NORMALIZATION BY P-MATRICES MULTIPLICATION IN FREQUENCY
  363. %[SxzMat,GxzMat]=var_to_cpsd(DxzMat,ExzMat,fresEstim);
  364. %[SxyzMat,HxyzMat]=var_to_cpsd(BxyzMat,ExyzMat,fresEstim);
  365. f_y_to_x_given_z_fres=nan(1,fresEstim+1);
  366. for lambda=1:fresEstim+1
  367. % NORMALIZED TRANSFER FUNCTIONS P*G(X,Z) and P2*P1*H(X,Y,Z)
  368. %PGxzMat=GxzMat(:,:,lambda)/(PxzMat); %G*inv(P)
  369. %PHxyzMat=HxyzMat(:,:,lambda)/(PxyzMat); %H*inv(P)
  370. % EXPANDED G MATRIX FOR SPECTRAL FACTORIZATION
  371. GxyzMat=[PGxzMat(1,1,lambda) 0 PGxzMat(1,2:end,lambda); 0 1 zeros(1,numZvars); PGxzMat(2:end,1,lambda) zeros(numZvars,1) PGxzMat(2:end,2:end,lambda)];
  372. QxyzMat=GxyzMat\PHxyzMat(:,:,lambda); %inv(G)*H
  373. %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))');
  374. 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))');
  375. %SIGx=abs(QxyzMat(1,1)*ExyzMat(1,1)*conj(QxyzMat(1,1)));
  376. SIGx=abs(QxyzMat(1,1)*PExyzMat(1,1)*conj(QxyzMat(1,1)));
  377. if QxyzMat(1,1)*PExyzMat(1,1)*conj(QxyzMat(1,1)) <0
  378. warning('Qxx product is negative');
  379. end
  380. if QxyzMat(1,2)*PExyzMat(2,2)*conj(QxyzMat(1,2)) <0
  381. warning('Qxy product is negative');
  382. end
  383. if QxyzMat(1,3:end)*PExyzMat(3:end,3:end)*conj(QxyzMat(1,3:end)') <0
  384. warning('Qxz product is negative');
  385. end
  386. f_y_to_x_given_z_fres(lambda)= log( SIGxyz ./ SIGx );
  387. %f_y_to_x_given_z_fres(lambda)= log( abs(PExzMat(1,1)) ./ SIGx );
  388. end
  389. f_y_to_x_given_z=interp1(lambdafreqAxis,f_y_to_x_given_z_fres,freqAxisPreFirstDimDwSamp,'spline');
  390. currfpw(chX,chY,:)=f_y_to_x_given_z;
  391. end
  392. if strcmpi(compMethod,'MatPart')
  393. % MATRIX PARTITIONING METHOD (Y. Chen et al., JNeurosci Methods 2006)
  394. % P MATRIX FOR COVARIANCE NORMALIZATION
  395. 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)]*...
  396. [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)];
  397. % APPLYING NORMALIZATION BY P-MATRICES MULTIPLICATION IN TIME
  398. PExyzMat=PxyzMat*ExyzMat*PxyzMat';
  399. PBxyzMat=nan(numZvars+2,numZvars+2,morder);
  400. for mm=1:morder
  401. PBxyzMat(:,:,mm)=PxyzMat*BxyzMat(:,:,mm);
  402. end
  403. [PSxyzMat,PHxyzMat]=var_to_cpsd(PBxyzMat,PExyzMat,fresEstim);
  404. f_y_to_x_given_z_fres=nan(1,fresEstim+1);
  405. for lambda=1:fresEstim+1
  406. %PHxyzMat=HxyzMat(:,:,lambda)/(PxyzMat); %H*inv(P)
  407. % MIXED TRANSFER FUNCTIONS HBARxy_zy=[HBARxy; HBARzy]
  408. HBAR_xy_zy=([PHxyzMat(1,1,lambda) PHxyzMat(1,3:end,lambda); PHxyzMat(3:end,1,lambda) PHxyzMat(3:end,3:end,lambda)]) \ [PHxyzMat(1,2,lambda); PHxyzMat(3:end,2,lambda)];
  409. % REDUCED COVARIANCE MATRIX SIGBAR_xz=[SIGBARxx SIGBARxz; SIGBARzx SIGBARzz]
  410. 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');
  411. % REDUCED P MATRIX FOR COVARIANCE NORMALIZATION
  412. PBARMat_xz=[1 zeros(1,numZvars); -EBAR_xz(2:end,1)/EBAR_xz(1,1) eye(numZvars)];
  413. GMat_xz=[PHxyzMat(1,1,lambda) PHxyzMat(1,3:end,lambda); PHxyzMat(3:end,1,lambda) PHxyzMat(3:end,3:end,lambda)]/(PBARMat_xz); % G/P = G*inv(P)
  414. % EXPANDED G MATRIX FOR SPECTRAL FACTORIZATION
  415. 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)];
  416. Q_xyz=(GMat_xyz)\PHxyzMat(:,:,lambda); % G\H = inv(G)*H
  417. %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))');
  418. 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))');
  419. %SIGx=abs( Q_xyz(1,1) * ExyzMat(1,1) * conj(Q_xyz(1,1)) );
  420. SIGx=abs( Q_xyz(1,1) * PExyzMat(1,1) * conj(Q_xyz(1,1)) );
  421. f_y_to_x_given_z_fres(lambda)=log( SIGxyz ./ SIGx );
  422. %f_y_to_x_given_z_fres(lambda)=log( abs(EBAR_xz(1,1)) ./ SIGx );
  423. end
  424. f_y_to_x_given_z=interp1(lambdafreqAxis,f_y_to_x_given_z_fres,freqAxisPreFirstDimDwSamp,'spline');
  425. currfpw(chX,chY,:)=f_y_to_x_given_z;
  426. end
  427. end
  428. end
  429. end
  430. currGCsStructV1V4(pp).(gratCondTag{cnd}).pwgcf=currfpw;
  431. currGCsStructV1V4(pp).(gratCondTag{cnd}).pwgcfSh=currfpwSh;
  432. end
  433. currGCsStructV1V4(pp).currV1SGIchs=currV1SGIchs;
  434. currGCsStructV1V4(pp).currV4SGIchs=currV4SGIchs;
  435. currGCsStructV1V4(pp).currV1SGIalgns=currV1SGIalgns;
  436. currGCsStructV1V4(pp).currV4SGIalgns=currV4SGIalgns;
  437. currGCsStructV1V4(pp).penNum=sLfpStructV1V4(pp).penNum;
  438. end
  439. % SORTING BY VISUAL AREA / LAMINAR LAYER / GRATC CONDITION
  440. for pp=1:length(sLfpStructV1V4)
  441. currV1SGIchs=currGCsStructV1V4(pp).currV1SGIchs;
  442. currV4SGIchs=currGCsStructV1V4(pp).currV4SGIchs;
  443. currV1SGIalgns=currGCsStructV1V4(pp).currV1SGIalgns;
  444. currV4SGIalgns=currGCsStructV1V4(pp).currV4SGIalgns;
  445. for cnd=1:2
  446. for vv1=[1 4]
  447. for vv2=[1 4]
  448. for ll1=1:3
  449. for ll2=1:3
  450. % eval is awful but it saves from extra indexing
  451. currll1chs=eval(['currV' num2str(vv1) 'SGIchs(ll1,~isnan(currV' num2str(vv1) 'SGIchs(ll1,:)))']);
  452. currll2chs=eval(['currV' num2str(vv2) 'SGIchs(ll2,~isnan(currV' num2str(vv2) 'SGIchs(ll2,:)))']);
  453. % NOTE: reverse to get fi->j|[ij] instead of fj->i|[ij]
  454. currllsfpw=currGCsStructV1V4(pp).(gratCondTag{cnd}).pwgcf(currll2chs,currll1chs,:);
  455. currllsfpwSh=currGCsStructV1V4(pp).(gratCondTag{cnd}).pwgcfSh(:,currll2chs,currll1chs,:);
  456. if ~isempty(currllsfpw)
  457. % pool chsxchs within ll1xll2
  458. currllsfpwRes=(reshape(currllsfpw,size(currllsfpw,1)*size(currllsfpw,2),size(currllsfpw,3)));
  459. currllsfpwResSh=reshape(currllsfpwSh,NShuffles,size(currllsfpwSh,2)*size(currllsfpwSh,3),size(currllsfpwSh,4));
  460. % get rid of all-NaNs rows
  461. currllsfpwRes((sum(isnan(currllsfpwRes),2)==size(currllsfpwRes,2)),:)=[];
  462. currllsfpwResSh(:,sum(isnan(currllsfpwResSh(1,:,:)),3)==size(currllsfpwResSh,3),:)=[];
  463. sGCsStructV1V4(pp).(gratCondTag{cnd}).(['V' num2str(vv1) 'toV' num2str(vv2)]).([lamLayerTag{ll1} 'to' lamLayerTag{ll2}]).pwgcf=currllsfpwRes;
  464. sGCsStructV1V4(pp).(gratCondTag{cnd}).(['V' num2str(vv1) 'toV' num2str(vv2)]).([lamLayerTag{ll1} 'to' lamLayerTag{ll2}]).pwgcfSh=currllsfpwResSh;
  465. %eval(clearoutmem('currlls*'));
  466. else
  467. sGCsStructV1V4(pp).(gratCondTag{cnd}).(['V' num2str(vv1) 'toV' num2str(vv2)]).([lamLayerTag{ll1} 'to' lamLayerTag{ll2}]).pwgcf=[];
  468. sGCsStructV1V4(pp).(gratCondTag{cnd}).(['V' num2str(vv1) 'toV' num2str(vv2)]).([lamLayerTag{ll1} 'to' lamLayerTag{ll2}]).pwgcfSh=[];
  469. end
  470. currll1algns=eval(['currV' num2str(vv1) 'SGIalgns(ll1,~isnan(currV' num2str(vv1) 'SGIalgns(ll1,:)))']);
  471. currll2algns=eval(['currV' num2str(vv2) 'SGIalgns(ll2,~isnan(currV' num2str(vv2) 'SGIalgns(ll2,:)))']);
  472. if length(currll1chs)~=length(currll1algns) || length(currll2chs)~=length(currll2algns); error('Layer labels do not match alignments.'); end
  473. for algn1=1:length(currll1algns)
  474. for algn2=1:length(currll2algns)
  475. currllalgnsfpw=currGCsStructV1V4(pp).(gratCondTag{cnd}).pwgcf(currll2chs(algn2),currll1chs(algn1),:);
  476. currllalgnsfpwRes=reshape(currllalgnsfpw,1,size(currllalgnsfpw,3));
  477. currllalgnsfpwSh=currGCsStructV1V4(pp).(gratCondTag{cnd}).pwgcfSh(:,currll2chs(algn2),currll1chs(algn1),:);
  478. currllalgnsfpwResSh=reshape(currllalgnsfpwSh,NShuffles,1,size(currllalgnsfpwSh,4));
  479. if currll1algns(algn1)<0
  480. strcurrll1algn1=['m' num2str(abs(currll1algns(algn1)))];
  481. else
  482. strcurrll1algn1=['p' num2str(currll1algns(algn1))];
  483. end
  484. if currll2algns(algn2)<0
  485. strcurrll2algn2=['m' num2str(abs(currll2algns(algn2)))];
  486. else
  487. strcurrll2algn2=['p' num2str(currll2algns(algn2))];
  488. end
  489. if sum(isnan(currllalgnsfpw),2)~=size(currllalgnsfpw,2)
  490. sGCsStructV1V4(pp).(gratCondTag{cnd}).(['V' num2str(vv1) 'toV' num2str(vv2)]).([lamLayerTag{ll1} 'to' lamLayerTag{ll2}]).([strcurrll1algn1 'to' strcurrll2algn2 'pwgcf'])=currllalgnsfpwRes;
  491. sGCsStructV1V4(pp).(gratCondTag{cnd}).(['V' num2str(vv1) 'toV' num2str(vv2)]).([lamLayerTag{ll1} 'to' lamLayerTag{ll2}]).([strcurrll1algn1 'to' strcurrll2algn2 'pwgcfSh'])=currllalgnsfpwResSh;
  492. else
  493. sGCsStructV1V4(pp).(gratCondTag{cnd}).(['V' num2str(vv1) 'toV' num2str(vv2)]).([lamLayerTag{ll1} 'to' lamLayerTag{ll2}]).([strcurrll1algn1 'to' strcurrll2algn2 'pwgcf'])=[];
  494. sGCsStructV1V4(pp).(gratCondTag{cnd}).(['V' num2str(vv1) 'toV' num2str(vv2)]).([lamLayerTag{ll1} 'to' lamLayerTag{ll2}]).([strcurrll1algn1 'to' strcurrll2algn2 'pwgcfSh'])=[];
  495. end
  496. end
  497. end
  498. end
  499. if vv2==4 && cnd==2
  500. sGCsStructV1V4(pp).penNum=currGCsStructV1V4(pp).penNum;
  501. sGCsStructV1V4(pp).Labels.(['V' num2str(vv1)]).(['num' lamLayerTag{ll1}(1) 'chs'])=length(currll1chs);
  502. sGCsStructV1V4(pp).Labels.(['V' num2str(vv1)]).([lamLayerTag{ll1} 'ChsAlgns'])=currll1algns;
  503. end
  504. end
  505. end
  506. end
  507. end
  508. end
  509. save(['./' upper(sSubjectName(1)) sSubjectName(end) sVisualArea '_cGCs_' num2str(numInformChs) '_MostInfChs_' compMethod '_PreFirstDim.mat'],'sGCsStructV1V4','-v7.3');
  510. else
  511. load(['./' upper(sSubjectName(1)) sSubjectName(end) sVisualArea '_cGCs_' num2str(numInformChs) '_MostInfChs_' compMethod '_PreFirstDim.mat']);
  512. end
  513. %% POOL ACROSS PENs GCs
  514. PoolGCs=[];
  515. for cnd=1:2
  516. for vv1=[1 4]
  517. for vv2=[1 4]
  518. PoolGCs.(gratCondTag{cnd}).(['V' num2str(vv1) 'toV' num2str(vv2)]).AllLayers.pwgcf=[];
  519. PoolGCs.(gratCondTag{cnd}).(['V' num2str(vv1) 'toV' num2str(vv2)]).AllLayers.pwgcfSh=[];
  520. for ll1=1:3
  521. for ll2=1:3
  522. currll1nchs=[];
  523. PoolGCs.(gratCondTag{cnd}).(['V' num2str(vv1) 'toV' num2str(vv2)]).([lamLayerTag{ll1} 'to' lamLayerTag{ll2}])=[];
  524. for pp=1:length(sGCsStructV1V4)
  525. currSubFieldNames=fieldnames(sGCsStructV1V4(pp).(gratCondTag{cnd}).(['V' num2str(vv1) 'toV' num2str(vv2)]).([lamLayerTag{ll1} 'to' lamLayerTag{ll2}]));
  526. currSubFieldNames=currSubFieldNames(~contains(currSubFieldNames,'Sh'));
  527. for sbfs=1:length(currSubFieldNames)
  528. if ~isfield(PoolGCs.(gratCondTag{cnd}).(['V' num2str(vv1) 'toV' num2str(vv2)]).([lamLayerTag{ll1} 'to' lamLayerTag{ll2}]),currSubFieldNames{sbfs})
  529. PoolGCs.(gratCondTag{cnd}).(['V' num2str(vv1) 'toV' num2str(vv2)]).([lamLayerTag{ll1} 'to' lamLayerTag{ll2}]).(currSubFieldNames{sbfs})=[];
  530. PoolGCs.(gratCondTag{cnd}).(['V' num2str(vv1) 'toV' num2str(vv2)]).([lamLayerTag{ll1} 'to' lamLayerTag{ll2}]).([currSubFieldNames{sbfs} 'Sh'])=[];
  531. end
  532. PoolGCs.(gratCondTag{cnd}).(['V' num2str(vv1) 'toV' num2str(vv2)]).([lamLayerTag{ll1} 'to' lamLayerTag{ll2}]).(currSubFieldNames{sbfs})=...
  533. [PoolGCs.(gratCondTag{cnd}).(['V' num2str(vv1) 'toV' num2str(vv2)]).([lamLayerTag{ll1} 'to' lamLayerTag{ll2}]).(currSubFieldNames{sbfs});...
  534. sGCsStructV1V4(pp).(gratCondTag{cnd}).(['V' num2str(vv1) 'toV' num2str(vv2)]).([lamLayerTag{ll1} 'to' lamLayerTag{ll2}]).(currSubFieldNames{sbfs})];
  535. if reportSignifThreshold
  536. PoolGCs.(gratCondTag{cnd}).(['V' num2str(vv1) 'toV' num2str(vv2)]).([lamLayerTag{ll1} 'to' lamLayerTag{ll2}]).([currSubFieldNames{sbfs} 'Sh'])=...
  537. cat(2,PoolGCs.(gratCondTag{cnd}).(['V' num2str(vv1) 'toV' num2str(vv2)]).([lamLayerTag{ll1} 'to' lamLayerTag{ll2}]).([currSubFieldNames{sbfs} 'Sh']),...
  538. sGCsStructV1V4(pp).(gratCondTag{cnd}).(['V' num2str(vv1) 'toV' num2str(vv2)]).([lamLayerTag{ll1} 'to' lamLayerTag{ll2}]).([currSubFieldNames{sbfs} 'Sh']));
  539. end
  540. end
  541. currll1nchs=cat(1,currll1nchs,sGCsStructV1V4(pp).Labels.(['V' num2str(vv1)]).(['num' lamLayerTag{ll1}(1) 'chs']));
  542. end
  543. if cnd==2
  544. PoolGCs.Labels.(['V' num2str(vv1)]).(['num' lamLayerTag{ll1}(1) 'chs'])=currll1nchs;
  545. end
  546. PoolGCs.(gratCondTag{cnd}).(['V' num2str(vv1) 'toV' num2str(vv2)]).AllLayers.pwgcf=[...
  547. PoolGCs.(gratCondTag{cnd}).(['V' num2str(vv1) 'toV' num2str(vv2)]).([lamLayerTag{ll1} 'to' lamLayerTag{ll2}]).pwgcf;...
  548. PoolGCs.(gratCondTag{cnd}).(['V' num2str(vv1) 'toV' num2str(vv2)]).AllLayers.pwgcf];
  549. PoolGCs.(gratCondTag{cnd}).(['V' num2str(vv1) 'toV' num2str(vv2)]).AllLayers.pwgcfSh=[...
  550. PoolGCs.(gratCondTag{cnd}).(['V' num2str(vv1) 'toV' num2str(vv2)]).([lamLayerTag{ll1} 'to' lamLayerTag{ll2}]).pwgcfSh;...
  551. PoolGCs.(gratCondTag{cnd}).(['V' num2str(vv1) 'toV' num2str(vv2)]).AllLayers.pwgcfSh];
  552. end
  553. end
  554. end
  555. end
  556. end
  557. %% PLOT PEN AVG GCs Layers x Layers (3x3 subplot)
  558. if plotPenAvgGCs
  559. for vv1=[1 4]
  560. for vv2=[1 4]
  561. ylimplot=[0 0];
  562. for lltx=1:3
  563. for llrx=1:3
  564. if ~isempty(PoolGCs.RF.(['V' num2str(vv1) 'toV' num2str(vv2)]).([lamLayerTag{lltx} 'to' lamLayerTag{llrx}]).pwgcf)
  565. nmRF=nanmean(PoolGCs.RF.(['V' num2str(vv1) 'toV' num2str(vv2)]).([lamLayerTag{lltx} 'to' lamLayerTag{llrx}]).pwgcf(:,freqIndices2Plot),1);
  566. nsRF=nansem(PoolGCs.RF.(['V' num2str(vv1) 'toV' num2str(vv2)]).([lamLayerTag{lltx} 'to' lamLayerTag{llrx}]).pwgcf(:,freqIndices2Plot),1);
  567. nmOUTrp=nanmean(PoolGCs.OUTrp.(['V' num2str(vv1) 'toV' num2str(vv2)]).([lamLayerTag{lltx} 'to' lamLayerTag{llrx}]).pwgcf(:,freqIndices2Plot),1);
  568. nsOUTrp=nansem(PoolGCs.OUTrp.(['V' num2str(vv1) 'toV' num2str(vv2)]).([lamLayerTag{lltx} 'to' lamLayerTag{llrx}]).pwgcf(:,freqIndices2Plot),1);
  569. ylimplot(1)=min([nmRF-nsRF nmOUTrp-nsOUTrp ylimplot(1)]);
  570. ylimplot(2)=max([nmRF+nsRF nmOUTrp+nsOUTrp ylimplot(2)]);
  571. end
  572. end
  573. end
  574. if (strcmpi(sSubjectName,'Taylor') && vv1==4 && vv2==1); ylimplot=2/3*ylimplot; end
  575. if vv1==vv2; ylimplot(1)=-1e-2; else ylimtoplot(1)=-1e-3; end
  576. hfig=figure('units','normalized','outerposition',[0 0 1 1]);
  577. for lltx=1:3
  578. for llrx=1:3
  579. if ~isempty(PoolGCs.RF.(['V' num2str(vv1) 'toV' num2str(vv2)]).([lamLayerTag{lltx} 'to' lamLayerTag{llrx}]).pwgcf)
  580. for ff=1:length(freqAxisPreFirstDimDwSamp)
  581. PoolGCs.pValues.(['V' num2str(vv1) 'toV' num2str(vv2)]).([lamLayerTag{lltx} 'to' lamLayerTag{llrx}]).pvalues(ff)=...
  582. signrank(PoolGCs.RF.(['V' num2str(vv1) 'toV' num2str(vv2)]).([lamLayerTag{lltx} 'to' lamLayerTag{llrx}]).pwgcf(:,ff),...
  583. PoolGCs.OUTrp.(['V' num2str(vv1) 'toV' num2str(vv2)]).([lamLayerTag{lltx} 'to' lamLayerTag{llrx}]).pwgcf(:,ff));
  584. end
  585. pBarVec=PoolGCs.pValues.(['V' num2str(vv1) 'toV' num2str(vv2)]).([lamLayerTag{lltx} 'to' lamLayerTag{llrx}]).pvalues(freqIndices2Plot);
  586. [~,~,~,pBarVecFDR]=fdr_bh(pBarVec);
  587. currLLsPwgcfRFShFreqWise=nan(1,NUM_TIME_DWSAMPLES+1);
  588. currLLsPwgcfOUTrpShFreqWise=nan(1,NUM_TIME_DWSAMPLES+1);
  589. if reportSignifThreshold
  590. for ff=1:(NUM_TIME_DWSAMPLES+1)
  591. currLLsFreqPwgcfRFSh=squeeze(PoolGCs.RF.(['V' num2str(vv1) 'toV' num2str(vv2)]).([lamLayerTag{lltx} 'to' lamLayerTag{llrx}]).pwgcfSh(:,:,ff));
  592. currLLsPwgcfRFShFreqWise(ff)=prctile(nanmean(currLLsFreqPwgcfRFSh,2),95);
  593. currLLsFreqPwgcfOUTrpSh=squeeze(PoolGCs.OUTrp.(['V' num2str(vv1) 'toV' num2str(vv2)]).([lamLayerTag{lltx} 'to' lamLayerTag{llrx}]).pwgcfSh(:,:,ff));
  594. currLLsPwgcfOUTrpShFreqWise(ff)=prctile(nanmean(currLLsFreqPwgcfOUTrpSh,2),95);
  595. end
  596. end
  597. subplot(3,3,(lltx-1)*3+llrx)
  598. plotcmapdots([1 0 0; 0 0 1;.2 .6 .2]);
  599. plot([-inf -inf],[-inf -inf],'r:'); plot([-inf -inf],[-inf -inf],'b:');
  600. plotmsem(freqAxisPreFirstDimDwSamp(freqIndices2Plot),(PoolGCs.RF.(['V' num2str(vv1) 'toV' num2str(vv2)]).([lamLayerTag{lltx} 'to' lamLayerTag{llrx}]).pwgcf(:,freqIndices2Plot)),'r');
  601. plotmsem(freqAxisPreFirstDimDwSamp(freqIndices2Plot),(PoolGCs.OUTrp.(['V' num2str(vv1) 'toV' num2str(vv2)]).([lamLayerTag{lltx} 'to' lamLayerTag{llrx}]).pwgcf(:,freqIndices2Plot)),'b');
  602. plot(freqAxisPreFirstDimDwSamp(freqIndices2Plot),currLLsPwgcfRFShFreqWise(freqIndices2Plot),'r:');
  603. plot(freqAxisPreFirstDimDwSamp(freqIndices2Plot),currLLsPwgcfOUTrpShFreqWise(freqIndices2Plot),'b:');
  604. plot(freqAxisPreFirstDimDwSamp(freqIndices2Plot),double(pBarVecFDR<=0.05)-1+ylimplot(1),'color',[.2 .6 .2],'linewidth',3)
  605. xlim([0 100]); ylim(ylimplot);
  606. title([lamLayerTag{lltx} ' V' num2str(vv1) ' to ' lamLayerTag{llrx} ' V' num2str(vv2)])
  607. text(90, .9*(ylimplot(2)-ylimplot(1))+ylimplot(1),['tot n_' lamLayerTag{lltx}(1) '_,_' lamLayerTag{llrx}(1) '=' num2str(size(PoolGCs.RF.(['V' num2str(vv1) 'toV' num2str(vv2)]).([lamLayerTag{lltx} 'to' lamLayerTag{llrx}]).pwgcf,1))],'color','k');
  608. if llrx==3 && lltx==3
  609. hleg=legend({'RF','OUT','p \leq .05','95% sh. perc. RF','95% sh. perc. OUT'},'Location','northeast');
  610. hleg.Position=hleg.Position.*[.98 .91 1 1]; hleg.EdgeColor=[1 1 1];
  611. end
  612. if vv1~=vv2 && lltx==1
  613. if llrx==1
  614. text(90, .7*(ylimplot(2)-ylimplot(1))+ylimplot(1),['avg n_' lamLayerTag{llrx}(1) '_,_V_' num2str(vv2) '=' sprintf('%.2f',mean(PoolGCs.Labels.(['V' num2str(vv2)]).(['num' lamLayerTag{llrx}(1) 'chs'])))],'color','k');
  615. else
  616. text(90, .8*(ylimplot(2)-ylimplot(1))+ylimplot(1),['avg n_' lamLayerTag{llrx}(1) '_,_V_' num2str(vv2) '=' sprintf('%.2f',mean(PoolGCs.Labels.(['V' num2str(vv2)]).(['num' lamLayerTag{llrx}(1) 'chs'])))],'color','k');
  617. end
  618. end
  619. if llrx==1
  620. text(90, .8*(ylimplot(2)-ylimplot(1))+ylimplot(1),['avg n_' lamLayerTag{lltx}(1) '_,_V_' num2str(vv1) '=' sprintf('%.2f',mean(PoolGCs.Labels.(['V' num2str(vv1)]).(['num' lamLayerTag{lltx}(1) 'chs'])))],'color','k');
  621. end
  622. end
  623. end
  624. end
  625. if numInformChs==0
  626. supertitle([sSubjectName ' V' num2str(vv1) ' to V' num2str(vv2) ' Unconditional ' compMethod ' GC - Pre First Dim LFPs [-500,0 ms] - PEN AVG']);
  627. elseif numInformChs==Inf
  628. supertitle([sSubjectName ' V' num2str(vv1) ' to V' num2str(vv2) ' Conditional (to all Complementary Channels) ' compMethod ' GC - Pre First Dim LFPs [-500,0 ms] - PEN AVG']);
  629. elseif numInformChs>0
  630. supertitle([sSubjectName ' V' num2str(vv1) ' to V' num2str(vv2) ' Conditional (to n=' num2str(numInformChs) ' Most Inform. Complem. Chs) ' compMethod ' GC - Pre First Dim LFPs [-500,0 ms] - PEN AVG']);
  631. elseif numInformChs==-Inf
  632. supertitle([sSubjectName ' V' num2str(vv1) ' to V' num2str(vv2) ' Conditional (to Complementary Chs outside current Layers) ' compMethod ' GC - Pre First Dim LFPs [-500,0 ms] - PEN AVG']);
  633. else
  634. supertitle([sSubjectName ' V' num2str(vv1) ' to V' num2str(vv2) ' Conditional (to n=' num2str(abs(numInformChs)) ' Most Inf. Compl. Chs outside current Layers) ' compMethod ' GC - Pre First Dim LFPs [-500,0 ms] - PEN AVG']);
  635. end
  636. end
  637. end
  638. end
  639. %% PLOT PEN AVG GCs Layers x Layers (3x3 subplot)
  640. if plotPenAvgGCs
  641. hfig=figure('units','normalized','outerposition',[0 0 1 1]);
  642. for vv1=[1 4]
  643. for vv2=[1 4]
  644. if vv1==vv2; ylimplot=[-1e-3 0.12]; else; ylimplot=[-1e-3 0.016]; end
  645. if ~isempty(PoolGCs.RF.(['V' num2str(vv1) 'toV' num2str(vv2)]).AllLayers.pwgcf)
  646. for ff=1:length(freqAxisPreFirstDimDwSamp)
  647. PoolGCs.pValues.(['V' num2str(vv1) 'toV' num2str(vv2)]).AllLayers.pvalues(ff)=...
  648. signrank(PoolGCs.RF.(['V' num2str(vv1) 'toV' num2str(vv2)]).AllLayers.pwgcf(:,ff),...
  649. PoolGCs.OUTrp.(['V' num2str(vv1) 'toV' num2str(vv2)]).AllLayers.pwgcf(:,ff));
  650. end
  651. pBarVec=PoolGCs.pValues.(['V' num2str(vv1) 'toV' num2str(vv2)]).AllLayers.pvalues(freqIndices2Plot);
  652. [~,~,~,pBarVecFDR]=fdr_bh(pBarVec);
  653. currLLsPwgcfRFShFreqWise=nan(1,NUM_TIME_DWSAMPLES+1);
  654. currLLsPwgcfOUTrpShFreqWise=nan(1,NUM_TIME_DWSAMPLES+1);
  655. if reportSignifThreshold
  656. for ff=1:(NUM_TIME_DWSAMPLES+1)
  657. currLLsFreqPwgcfRFSh=squeeze(PoolGCs.RF.(['V' num2str(vv1) 'toV' num2str(vv2)]).AllLayers.pwgcfSh(:,:,ff));
  658. currLLsPwgcfRFShFreqWise(ff)=prctile(nanmean(currLLsFreqPwgcfRFSh,2),95);
  659. currLLsFreqPwgcfOUTrpSh=squeeze(PoolGCs.OUTrp.(['V' num2str(vv1) 'toV' num2str(vv2)]).AllLayers.pwgcfSh(:,:,ff));
  660. currLLsPwgcfOUTrpShFreqWise(ff)=prctile(nanmean(currLLsFreqPwgcfOUTrpSh,2),95);
  661. end
  662. end
  663. subplot(2,2,(ceil(vv1/2)-1)*2+ceil(vv2/2))
  664. plotcmapdots([1 0 0; 0 0 1;.2 .6 .2]);
  665. plot([-inf -inf],[-inf -inf],'r:'); plot([-inf -inf],[-inf -inf],'b:');
  666. plotmsem(freqAxisPreFirstDimDwSamp(freqIndices2Plot),(PoolGCs.RF.(['V' num2str(vv1) 'toV' num2str(vv2)]).AllLayers.pwgcf(:,freqIndices2Plot)),'r');
  667. plotmsem(freqAxisPreFirstDimDwSamp(freqIndices2Plot),(PoolGCs.OUTrp.(['V' num2str(vv1) 'toV' num2str(vv2)]).AllLayers.pwgcf(:,freqIndices2Plot)),'b');
  668. plot(freqAxisPreFirstDimDwSamp(freqIndices2Plot),currLLsPwgcfRFShFreqWise(freqIndices2Plot),'r:');
  669. plot(freqAxisPreFirstDimDwSamp(freqIndices2Plot),currLLsPwgcfOUTrpShFreqWise(freqIndices2Plot),'b:');
  670. plot(freqAxisPreFirstDimDwSamp(freqIndices2Plot),double(pBarVecFDR<=0.05)-1+ylimplot(1),'color',[.2 .6 .2],'linewidth',3)
  671. xlim([0 100]); ylim(ylimplot);
  672. title(['V' num2str(vv1) ' to V' num2str(vv2)])
  673. if vv1==1 && vv2==4
  674. hleg=legend({'RF','OUT','p \leq .05','95% sh. perc. RF','95% sh. perc. OUT'},'Location','northeast');
  675. hleg.Position=hleg.Position.*[.98 .91 1 1]; hleg.EdgeColor=[1 1 1];
  676. end
  677. text(90, .9*(ylimplot(2)-ylimplot(1))+ylimplot(1),['tot n=' num2str(size(PoolGCs.RF.(['V' num2str(vv1) 'toV' num2str(vv2)]).AllLayers.pwgcf,1))],'color','k');
  678. end
  679. if numInformChs==0
  680. supertitle([sSubjectName ' ' sVisualArea ' Unconditional ' compMethod ' GC - Pre First Dim LFPs [-500,0 ms] - PEN AVG']);
  681. elseif numInformChs==Inf
  682. supertitle([sSubjectName ' ' sVisualArea ' Conditional (to all Complementary Channels) ' compMethod ' GC - Pre First Dim LFPs [-500,0 ms] - PEN AVG']);
  683. elseif numInformChs>0
  684. supertitle([sSubjectName ' ' sVisualArea ' Conditional (to n=' num2str(numInformChs) ' Most Inform. Complem. Chs) ' compMethod ' GC - Pre First Dim LFPs [-500,0 ms] - PEN AVG']);
  685. elseif numInformChs==-Inf
  686. supertitle([sSubjectName ' ' sVisualArea ' Conditional (to Complementary Chs outside current Layers) ' compMethod ' GC - Pre First Dim LFPs [-500,0 ms] - PEN AVG']);
  687. else
  688. 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']);
  689. end
  690. end
  691. end
  692. end