collectUnfold_plotERPs.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. %%
  2. % This script loads all the deconvolved data and plots them as ERPs
  3. cd '/net/store/nbp/users/agert/itw'
  4. init_itw
  5. projectFolder=['/net/store/nbp/projects/IntoTheWild'];
  6. codingscheme = 'mashup';
  7. phase='WLFO';
  8. filtType='causal';
  9. timewin=[-0.5 1.0];
  10. % LOADING
  11. % needed for chanlocs
  12. if strcmp(phase,'Lab')
  13. Data=load(['/net/store/nbp/projects/IntoTheWild/Daten/EEG/' phase '/collectedERPs_-5001000_causal_noCar.mat']);
  14. elseif strcmp(phase,'WLFO')
  15. Data=load(['/net/store/nbp/projects/IntoTheWild/Daten/EEG/' phase '/collectedERPs_-5001000_causal.mat']);
  16. end
  17. fns=fieldnames(Data);
  18. collData = Data.(fns{1});
  19. paramNames=[];
  20. %
  21. d2nd = [];
  22. for subj = [8 9 29:36 38:41 44 45 48 51:53];
  23. fPath = fullfile(projectFolder,['Daten/EEG/' phase '/unfold_' phase '/' filtType '/']);
  24. fPath = fullfile(fPath,['ufresult_subj' num2str(subj) ,'_allElec_' codingscheme '_-500-1000_regularized0.mat']);
  25. try
  26. % load subject results
  27. if ~exist(fPath,'file')
  28. continue
  29. end
  30. fprintf('\n \n loading subject %i \n',subj)
  31. d = load(fPath);
  32. ufresult = d.ufresult;
  33. tmpBeta=[];
  34. tmpBeta_nodc=[];
  35. if strcmp(phase,'Lab') && subj==51 % sub 51 does not have predictore sacAmp -> adjust structure accordingly
  36. tmpBeta(:,:,1:9)=ufresult.beta(:,:,1:9);
  37. tmpBeta(:,:,10:13)=nan(length(d2nd.chanlocs),length(ufresult.times),4);
  38. tmpBeta(:,:,14:20)=ufresult.beta(:,:,10:16);
  39. tmpBeta_nodc(:,:,1:9)=ufresult.beta_nodc(:,:,1:9);
  40. tmpBeta_nodc(:,:,10:13)=nan(length(d2nd.chanlocs),length(ufresult.times),4);
  41. tmpBeta_nodc(:,:,14:20)=ufresult.beta_nodc(:,:,10:16);
  42. ufresult.beta=tmpBeta;
  43. ufresult.beta_nodc=tmpBeta_nodc;
  44. if ~isempty(d2nd)
  45. ufresult.param=d2nd.param;
  46. end
  47. end
  48. % first subject: initialize d2nd (2nd-level analysis)
  49. if isempty(d2nd)
  50. d2nd = ufresult;
  51. d2nd.unfold(1) = d2nd.unfold;
  52. d2nd.subject = subj;
  53. else
  54. d2nd.unfold(end+1) = ufresult.unfold;
  55. d2nd.beta(:,:,:,end+1) = ufresult.beta;
  56. d2nd.beta_nodc(:,:,:,end+1) = ufresult.beta_nodc;
  57. d2nd.subject(end+1) = subj;
  58. end
  59. catch e
  60. display(['problem with subject ',num2str(subj), ': ' ,e.message])
  61. end
  62. end
  63. %% Calculate ERPs
  64. elecfind = @(x)find(strcmp(x,{d2nd.chanlocs.labels}));
  65. chan = 1:length(d2nd.chanlocs);%[elecfind('PO8')];
  66. if strcmp(codingscheme,'effects')
  67. [x2n,x2h,n2x,h2x,x2n_nodc,x2h_nodc,n2x_nodc,h2x_nodc,SB]=ITW_unfold_calcERPs(d2nd,codingscheme,chan,phase);
  68. else
  69. [n2n,n2h,h2n,h2h,n2n_nodc,n2h_nodc,h2n_nodc,h2h_nodc,SB]=ITW_unfold_calcERPs(d2nd,codingscheme,chan,phase);
  70. end
  71. %% Save in ERP structure
  72. ufERP.times=d2nd.times;
  73. ufERP.chanlocs=d2nd.chanlocs;
  74. ufERP.subject=d2nd.subject;
  75. ufERP.data=[];
  76. ufERP.data(:,:,:,1)=n2n;
  77. ufERP.data(:,:,:,2)=n2h;
  78. ufERP.data(:,:,:,3)=h2n;
  79. ufERP.data(:,:,:,4)=h2h;
  80. ufERP.cond={'n2n','n2h','h2n','h2h'};
  81. save(['/net/store/nbp/projects/IntoTheWild/Daten/EEG/' phase '/unfold_' phase '/' filtType '/unfoldERPs_-5001000.mat'],'ufERP')
  82. %% PLOT ERPs
  83. % load(['/net/store/nbp/projects/IntoTheWild/Daten/EEG/WLFO/unfold_WLFO/' filtType '/unfoldERPs_-5001000.mat'],'ufERP')
  84. elecfind = @(x)find(strcmp(x,{d2nd.chanlocs.labels}));
  85. chan =[elecfind('PO8'),elecfind('P8'),elecfind('PO7'),elecfind('P7')];
  86. % plot 2x2 results of deconvolution
  87. figure('units','normalized','outerposition',[0 0 1 1])
  88. hold on, plot(d2nd.times,squeeze(mean(mean(n2n(:,chan,:)),2)),'b--','LineWidth',2);
  89. hold on, plot(d2nd.times,squeeze(mean(mean(n2h(:,chan,:)),2)),'r--','LineWidth',2);
  90. hold on, plot(d2nd.times,squeeze(mean(mean(h2n(:,chan,:)),2)),'b','LineWidth',2);
  91. hold on, plot(d2nd.times,squeeze(mean(mean(h2h(:,chan,:)),2)),'r','LineWidth',2);
  92. legend({'BG -> BG','BG -> HF','HF -> BG','HF -> HF'},'FontSize',20)
  93. xlim([-0.5 1])
  94. xt = get(gca, 'XTick');
  95. set(gca, 'FontSize', 18)
  96. ylim([-6 6]);
  97. vline(0,'k')
  98. hline(0,'k');
  99. box off
  100. title([phase ' Deconvolution'],'FontSize',20)
  101. xlabel('Time [sec]', 'FontSize',20)
  102. ylabel('Amplitude [microV]', 'FontSize',20);
  103. % export_fig(['/home/student/a/agert/Desktop/Into the Wild/LabvsWLFO/autoplot/2x2_ERP_Deconv_Unfold_' filtType '.eps'])
  104. % plot 2x2 design of NO deconvolution
  105. figure('units','normalized','outerposition',[0 0 1 1])
  106. hold on, plot(d2nd.times,squeeze(mean(mean(n2n_nodc(:,chan,:)),2)),'b--','LineWidth',2);
  107. hold on, plot(d2nd.times,squeeze(mean(mean(n2h_nodc(:,chan,:)),2)),'r--','LineWidth',2);
  108. hold on, plot(d2nd.times,squeeze(mean(mean(h2n_nodc(:,chan,:)),2)),'b','LineWidth',2);
  109. hold on, plot(d2nd.times,squeeze(mean(mean(h2h_nodc(:,chan,:)),2)),'r','LineWidth',2);
  110. legend({'BG -> BG','BG -> HF','HF -> BG','HF -> HF'},'FontSize',20)
  111. xlim([-0.5 1])
  112. xt = get(gca, 'XTick');
  113. set(gca, 'FontSize', 18)
  114. ylim([-6 6])
  115. vline(0,'k')
  116. hline(0,'k')
  117. box off
  118. title([phase ' No Deconvolution'],'FontSize',20)
  119. xlabel('Time [sec]', 'FontSize',20)
  120. ylabel('Amplitude [microV]', 'FontSize',20);
  121. % within face fixation
  122. if strcmp(phase,'WLFO')
  123. figure('units','normalized','outerposition',[0 0 1 1])
  124. hold on,plot(d2nd.times,squeeze(mean(mean(n2h(:,chan,:)),2)),'b','LineWidth',2);
  125. hold on, plot(d2nd.times,squeeze(mean(mean(h2h(:,chan,:)),2)),'r--','LineWidth',2);
  126. hold on, plot(d2nd.times,squeeze(mean(mean(SB (:,chan,:)),2)),'r','LineWidth',2);
  127. legend({'BG -> HF','HF -> HF','Within Box'},'FontSize',20)
  128. xlim([-0.5 1])
  129. xt = get(gca, 'XTick');
  130. set(gca, 'FontSize', 18)
  131. ylim([-6 6])
  132. vline(0,'k')
  133. % vline(0.17,'--k')
  134. hline(0,'k')
  135. box off
  136. title([phase ' Samebox Effect'],'FontSize',20)
  137. xlabel('Time [sec]', 'FontSize',20)
  138. ylabel('Amplitude [microV]', 'FontSize',20);
  139. % export_fig(['/home/student/a/agert/Desktop/Into the Wild/LabvsWLFO/autoplot/SB_ERP_' filtType '.png'])
  140. % export_fig(['/home/student/a/agert/Desktop/Into the Wild/LabvsWLFO/autoplot/SB_ERP.eps'])
  141. end
  142. %% 1x2 ERP plots
  143. n2x=(n2h+n2n)./2;
  144. h2x=(h2h+h2n)./2;
  145. x2n=(h2n+n2n)./2;
  146. x2h=(h2h+n2h)./2;
  147. % plot 1x2 for type of previous fixation
  148. figure('units','normalized','outerposition',[0 0 1 1])
  149. hold on, plot(d2nd.times,squeeze(mean(mean(n2x(:,chan,:)),2)),'k--','LineWidth',2);
  150. hold on, plot(d2nd.times,squeeze(mean(mean(h2x(:,chan,:)),2)),'k','LineWidth',2);
  151. legend({'BG -> X','HF -> X'},'FontSize',20)
  152. xlim([-0.5 1])
  153. xt = get(gca, 'XTick');
  154. set(gca, 'FontSize', 18)
  155. ylim([-6 6])
  156. vline(0,'k')
  157. % vline(0.17,'--k')
  158. hline(0,'k')
  159. box off
  160. title([phase ' Main Effect Prev'],'FontSize',20)
  161. xlabel('Time [sec]', 'FontSize',20)
  162. ylabel('Amplitude [microV]', 'FontSize',20);
  163. %export_fig(['/home/student/a/agert/Desktop/Into the Wild/LabvsWLFO/autoplot/1x2_ERP_MainPrev_Unfold_' filtType '.eps'])
  164. % plot 1x2 for type of current fixation
  165. x2nCI=zeros(length(squeeze(mean(x2n(:,chan,:),2))),2);
  166. x2hCI=zeros(length(squeeze(mean(x2h(:,chan,:),2))),2);
  167. for timep=1:length(squeeze(mean(x2n(:,chan,:),2)))
  168. x2nCI(timep,:)= bootci(10000,@mean,mean(x2n(:,chan,timep),2));
  169. x2hCI(timep,:)= bootci(10000,@mean,mean(x2h(:,chan,timep),2));
  170. end
  171. figure('units','normalized','outerposition',[0 0 1 1])
  172. % hold on, plot(d2nd.times,squeeze(mean(mean(x2h(:,chan,:)),2)),'r','LineWidth',2);
  173. %
  174. x1 = [squeeze(d2nd.times), fliplr(squeeze(d2nd.times))];
  175. inBetween = [x2nCI(:,1)', fliplr(x2nCI(:,2)')];
  176. fill(x1, inBetween, [0,0.5,0.5]);
  177. hold on;
  178. plot(d2nd.times,squeeze(mean(mean(x2n(:,chan,:)),2)),'b','LineWidth',2);
  179. % hold on, plot(d2nd.times,x2nCI,'b--','LineWidth',2);
  180. % hold on, plot(d2nd.times,x2hCI,'r--','LineWidth',2);
  181. inBetween2 = [x2hCI(:,1)', fliplr(x2hCI(:,2)')];
  182. fill(x1, inBetween2, [1,0.5,0]);
  183. hold on;
  184. plot(d2nd.times,squeeze(mean(mean(x2h(:,chan,:)),2)),'r','LineWidth',2);
  185. legend({'X -> BG','X -> HF'},'FontSize',20)
  186. xlim([-0.5 1])
  187. xt = get(gca, 'XTick');
  188. set(gca, 'FontSize', 18)
  189. ylim([-7 7])
  190. vline(0,'k')
  191. vline(0.12,'k--') %N170 start
  192. vline(0.2,'k--') %N170 stop
  193. vline(-0.3,'k--')
  194. % vline(0.17,'--k')
  195. hline(0,'k')
  196. box off
  197. title([phase ' Main Effect Curr'],'FontSize',20)
  198. xlabel('Time [sec]', 'FontSize',20)
  199. ylabel('Amplitude [microV]', 'FontSize',20);
  200. export_fig(['/home/student/a/agert/Desktop/Into the Wild/LabvsWLFO/autoplot/1x2_ERP_MainCurr_Unfold_' filtType '_' phase '_withCI.eps'])
  201. %% Plot effect topos (timerange specified for N170)
  202. effectData=squeeze(mean(x2h(:,:,:)))-squeeze(mean(x2n(:,:,:)));
  203. tp1=find(d2nd.times>0.120, 1, 'first');
  204. tp2=find(d2nd.times<0.200, 1, 'last');
  205. topoData=squeeze(mean(effectData(:,tp1:tp2),2));
  206. figure, topoplot(topoData',d2nd.chanlocs,'conv','on');
  207. title(['N170 topo 120:200 ' phase])
  208. %% viewing behavior
  209. d2nd2 = subjectwise_getparam(d2nd,{{'sac_amplitude',[0.5 1 2.5 5 7.5 10]}},1);
  210. uf_plotParam(d2nd2,'channel','Oz','plotSeparate','all','plotParam',{'fix_avgpos_y','fix_avgpos_x'})
  211. uf_plotParam(d2nd2,'channel','Oz', 'plotSeparate','all','plotParam','sac_amplitude','deconv',1)
  212. set(findobj(gca,'-property','LineWidth'),'LineWidth',2)
  213. %% Component amplitudes
  214. %P100: 80-130
  215. %N170: 120-200
  216. p100Start=find(d2nd.times>=0.080,1,'first');
  217. p100End=find(d2nd.times<=0.130,1,'last');
  218. n170Start=find(d2nd.times>=0.120,1,'first');
  219. n170End=find(d2nd.times<=0.200,1,'last');
  220. meanN2N.p100=squeeze(mean(ufERP.data(:,chan,p100Start:p100End,1),2));
  221. meanN2H.p100=squeeze(mean(ufERP.data(:,chan,p100Start:p100End,2),2));
  222. meanH2N.p100=squeeze(mean(ufERP.data(:,chan,p100Start:p100End,3),2));
  223. meanH2H.p100=squeeze(mean(ufERP.data(:,chan,p100Start:p100End,4),2));
  224. meanN2N.n170=squeeze(mean(ufERP.data(:,chan,n170Start:n170End,1),2));
  225. meanN2H.n170=squeeze(mean(ufERP.data(:,chan,n170Start:n170End,2),2));
  226. meanH2N.n170=squeeze(mean(ufERP.data(:,chan,n170Start:n170End,3),2));
  227. meanH2H.n170=squeeze(mean(ufERP.data(:,chan,n170Start:n170End,4),2));
  228. meanX2N.p100=squeeze(mean(mean(ufERP.data(:,chan,p100Start:p100End,[1,3]),2),4));
  229. meanX2H.p100=squeeze(mean(mean(ufERP.data(:,chan,p100Start:p100End,[2,4]),2),4));
  230. meanX2N.n170=squeeze(mean(mean(ufERP.data(:,chan,n170Start:n170End,[1,3]),2),4));
  231. meanX2H.n170=squeeze(mean(mean(ufERP.data(:,chan,n170Start:n170End,[2,4]),2),4));
  232. meanN2X.p100=squeeze(mean(mean(ufERP.data(:,chan,p100Start:p100End,[1,2]),2),4));
  233. meanH2X.p100=squeeze(mean(mean(ufERP.data(:,chan,p100Start:p100End,[3,4]),2),4));
  234. meanN2X.n170=squeeze(mean(mean(ufERP.data(:,chan,n170Start:n170End,[1,2]),2),4));
  235. meanH2X.n170=squeeze(mean(mean(ufERP.data(:,chan,n170Start:n170End,[3,4]),2),4));
  236. %%
  237. p100=meanH2H.p100;
  238. n170=meanH2H.n170;
  239. [p100Amp,p100Time]=max(p100,[],2);
  240. [n170Amp,n170Time]=min(n170,[],2);
  241. p2pEff=p100Amp-n170Amp;
  242. p100PeakTime=ufERP.times(p100Start+p100Time-1);
  243. n170PeakTime=ufERP.times(n170Start+n170Time-1);
  244. p100AmpCI = bootci(10000,@mean,p100Amp);
  245. n170AmpCI = bootci(10000,@mean,n170Amp);
  246. p2pAmpCI = bootci(10000,@mean,p2pEff);
  247. p100TimeCI = bootci(10000,@mean,p100PeakTime);
  248. n170TimeCI = bootci(10000,@mean,n170PeakTime);
  249. t=0;
  250. if t==100
  251. fprintf(['P100 for ' phase ' H2H on average ' num2str(mean(p100Amp)) 'uV 95CI [' num2str(p100AmpCI(1)) ';' num2str(p100AmpCI(2)) '] at ' num2str(mean(p100PeakTime)) 'ms [' num2str(p100TimeCI(1)) ';' num2str(p100TimeCI(2)) '].\n'])
  252. elseif t==170
  253. fprintf(['N170 for ' phase ' H2H on average ' num2str(mean(n170Amp)) 'uV 95CI [' num2str(n170AmpCI(1)) ';' num2str(n170AmpCI(2)) '] at ' num2str(mean(n170PeakTime)) 'ms [' num2str(n170TimeCI(1)) ';' num2str(n170TimeCI(2)) '].\n'])
  254. elseif t==0
  255. fprintf(['P2P for ' phase ' H2H on average ' num2str(mean(p2pEff)) 'uV 95CI [' num2str(p2pAmpCI(1)) ';' num2str(p2pAmpCI(2)) '].\n'])
  256. end
  257. %% conditionwise differences
  258. cond1=meanX2N;
  259. cond2=meanX2H;
  260. [cond1p100Amp,cond1p100Time]=max(cond1.p100,[],2);
  261. [cond1n170Amp,cond1n170Time]=min(cond1.n170,[],2);
  262. [cond2p100Amp,cond2p100Time]=max(cond2.p100,[],2);
  263. [cond2n170Amp,cond2n170Time]=min(cond2.n170,[],2);
  264. condDiffp100=cond2p100Amp-cond1p100Amp;
  265. condDiffn170=cond2n170Amp-cond1n170Amp;
  266. p100AmpCI = bootci(10000,@mean,condDiffp100);
  267. n170AmpCI = bootci(10000,@mean,condDiffn170);
  268. fprintf(['Diff P100 for ' phase ' on average ' num2str(mean(condDiffp100)) 'uV 95CI [' num2str(p100AmpCI(1)) ';' num2str(p100AmpCI(2)) '].\n'])
  269. fprintf(['Diff N170 for ' phase ' on average ' num2str(mean(condDiffn170)) 'uV 95CI [' num2str(n170AmpCI(1)) ';' num2str(n170AmpCI(2)) '].\n'])
  270. % [N2NAmp,N2NTime]=min(meanN2N,[],2);
  271. % [N2HAmp,N2HTime]=min(meanN2H,[],2);
  272. % [H2NAmp,H2NTime]=min(meanH2N,[],2);
  273. % [H2HAmp,H2HTime]=min(meanH2H,[],2);
  274. %
  275. %
  276. % [X2HAmp,X2HTime]=max(meanX2H,[],2);
  277. % [X2NAmp,X2NTime]=max(meanX2N,[],2);
  278. %
  279. % X2NPeakTime=ufERP.times(n170Start+X2NTime);
  280. % X2HPeakTime=ufERP.times(n170Start+X2HTime);
  281. % X2NAmpCI = bootci(10000,@mean,X2NAmp);
  282. % X2HAmpCI = bootci(10000,@mean,X2HAmp);
  283. % X2NTimeCI = bootci(10000,@mean,X2NPeakTime);
  284. % X2HTimeCI = bootci(10000,@mean,X2HPeakTime);
  285. % fprintf(['N170 for ' phase ' N2N on average ' num2str(mean(N2NRange)) 'uV.\n'])
  286. % fprintf(['N170 for ' phase ' N2H on average ' num2str(mean(N2HRange)) 'uV.\n'])
  287. % fprintf(['N170 for ' phase ' H2N on average ' num2str(mean(H2NRange)) 'uV.\n'])
  288. % fprintf(['N170 for ' phase ' H2H on average ' num2str(mean(H2HRange)) 'uV.\n'])
  289. % fprintf(['N170 for ' phase ' X2N on average ' num2str(mean(X2NAmp)) 'uV 95CI [' num2str(X2NCI(1)) ' ' num2str(X2NAmpCI(2)) '] at ' num2str(mean(X2NPeakTime)) 'ms.\n'])
  290. % fprintf(['N170 for ' phase ' X2H on average ' num2str(mean(X2HAmp)) 'uV 95CI [' num2str(X2HCI(1)) ' ' num2str(X2HAmpCI(2)) '] at ' num2str(mean(X2HPeakTime)) 'ms.\n'])