c1_taskPLS_novelty_frontal_erp_trace.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. currentFile = mfilename('fullpath');
  2. [pathstr,~,~] = fileparts(currentFile);
  3. cd(fullfile(pathstr,'..'))
  4. rootpath = pwd;
  5. pn.data_eeg = fullfile(rootpath, '..', 'eegmp_preproc', 'data', 'outputs', 'eeg');
  6. pn.data_erp = fullfile(rootpath, 'data', 'erp');
  7. pn.data_erf = fullfile(rootpath, 'data', 'erf');
  8. pn.tools = fullfile(rootpath, 'tools');
  9. addpath(fullfile(rootpath, '..', 'eegmp_preproc', 'tools', 'fieldtrip')); ft_defaults
  10. addpath(fullfile(pn.tools, 'BrewerMap'));
  11. addpath(fullfile(pn.tools, 'shadedErrorBar'));
  12. pn.figures = fullfile(rootpath, 'figures');
  13. %% load erp
  14. for ind_id = 1:33
  15. id = sprintf('sub-%03d', ind_id);
  16. load(fullfile(pn.data_erp, [id,'_erp_bl.mat']));
  17. for ind_option = 1:numel(conds.old)
  18. if ind_id == 1
  19. erpgroup.old.(conds.old{ind_option}) = erp_bl.old{ind_option};
  20. erpgroup.old.(conds.old{ind_option}) = ...
  21. rmfield(erpgroup.old.(conds.old{ind_option}), {'avg', 'var', 'dof'});
  22. erpgroup.old.(conds.old{ind_option}).dimord = 'sub_chan_time';
  23. end
  24. erpgroup.old.(conds.old{ind_option}).avg(ind_id,:,:) = erp_bl.old{ind_option}.avg;
  25. end
  26. end
  27. time = erpgroup.old.old.time;
  28. elec = erpgroup.old.old.elec;
  29. channels = erpgroup.old.old.label;
  30. mergeddata = cat(4, erpgroup.old.old.avg, ...
  31. erpgroup.old.new.avg);
  32. %% plot ERP topography
  33. %
  34. % % set custom colormap
  35. % cBrew = brewermap(500,'RdBu');
  36. % cBrew = flipud(cBrew);
  37. % colormap(cBrew)
  38. %
  39. % h = figure('units','centimeters','position',[0 0 10 10]);
  40. % set(gcf,'renderer','Painters')
  41. %
  42. % cfg = [];
  43. % cfg.layout = 'biosemi64.lay';
  44. % cfg.parameter = 'powspctrm';
  45. % cfg.comment = 'no';
  46. % cfg.colormap = cBrew;
  47. % cfg.colorbar = 'EastOutside';
  48. %
  49. % plotData = [];
  50. % plotData.label = elec.label; % {1 x N}
  51. % plotData.dimord = 'chan';
  52. % plotData.powspctrm = squeeze(nanmean(nanmean(nanmean(mergeddata(:,:,time>=0.3& time <=0.5,1:2),3),1),4))';
  53. %
  54. % cfg.zlim = [-14 14]*10^-4;
  55. % cfg.figure = h;
  56. % ft_topoplotER(cfg,plotData);
  57. % cb = colorbar('location', 'EastOutside'); set(get(cb,'ylabel'),'string','Amplitude');
  58. % % figureName = ['xxx'];
  59. % % saveas(h, fullfile(pn.figures, figureName), 'epsc');
  60. % % saveas(h, fullfile(pn.figures, figureName), 'png');
  61. %% visualize frontal ERP
  62. idx_chans = [38];
  63. % avg across channels and conditions
  64. condAvg = squeeze(nanmean(nanmean(mergeddata(:,idx_chans,:,1:2),2),4));
  65. h = figure('units','centimeters','position',[0 0 10 8]);
  66. cla; hold on;
  67. % highlight relevant phase in background
  68. patches.timeVec = [0.3 0.5];
  69. patches.colorVec = [1 .95 .8];
  70. for indP = 1:size(patches.timeVec,2)-1
  71. YLim = [-8 2]*10^-4;
  72. p = patch([patches.timeVec(indP) patches.timeVec(indP+1) patches.timeVec(indP+1) patches.timeVec(indP)], ...
  73. [YLim(1) YLim(1) YLim(2), YLim(2)], patches.colorVec(indP,:));
  74. p.EdgeColor = 'none';
  75. end
  76. % new value = old value ? subject average + grand average
  77. curData = squeeze(nanmean(mergeddata(:,idx_chans,:,1),2));
  78. curData = curData-condAvg+repmat(nanmean(condAvg,1),size(condAvg,1),1);
  79. standError = nanstd(curData,1)./sqrt(size(curData,1));
  80. l1 = shadedErrorBar(time,nanmean(curData,1),standError, 'lineprops', {'color', 'k','linewidth', 2}, 'patchSaturation', .1);
  81. curData = squeeze(nanmean(mergeddata(:,idx_chans,:,2),2));
  82. curData = curData-condAvg+repmat(nanmean(condAvg,1),size(condAvg,1),1);
  83. standError = nanstd(curData,1)./sqrt(size(curData,1));
  84. l2 = shadedErrorBar(time,nanmean(curData,1),standError, 'lineprops', {'color', 'r','linewidth', 2}, 'patchSaturation', .1);
  85. % ax = gca; ax.YDir = 'reverse';
  86. legend([l1.mainLine, l2.mainLine],{'old', 'new'}, ...
  87. 'location', 'southwest'); legend('boxoff')
  88. xlabel('Time (s) from stim onset')
  89. xlim([-.05 .6]); %ylim([-.03 .18])
  90. ylabel({'ERP';'(microVolts)'});
  91. xlabel({'Time (s)'});
  92. set(findall(gcf,'-property','FontSize'),'FontSize',14)
  93. figureName = ['c_novelty_frontalERP'];
  94. saveas(h, fullfile(pn.figures, figureName), 'epsc');
  95. saveas(h, fullfile(pn.figures, figureName), 'png');
  96. %% visualize central ERP
  97. idx_chans = [32, 19, 56];
  98. % avg across channels and conditions
  99. condAvg = squeeze(nanmean(nanmean(mergeddata(:,idx_chans,:,1:2),2),4));
  100. h = figure('units','centimeters','position',[0 0 10 8]);
  101. cla; hold on;
  102. % highlight relevant phase in background
  103. patches.timeVec = [0.3 0.5];
  104. patches.colorVec = [1 .95 .8];
  105. for indP = 1:size(patches.timeVec,2)-1
  106. YLim = [-8 4]*10^-4;
  107. p = patch([patches.timeVec(indP) patches.timeVec(indP+1) patches.timeVec(indP+1) patches.timeVec(indP)], ...
  108. [YLim(1) YLim(1) YLim(2), YLim(2)], patches.colorVec(indP,:));
  109. p.EdgeColor = 'none';
  110. end
  111. % new value = old value ? subject average + grand average
  112. curData = squeeze(nanmean(mergeddata(:,idx_chans,:,1),2));
  113. curData = curData-condAvg+repmat(nanmean(condAvg,1),size(condAvg,1),1);
  114. standError = nanstd(curData,1)./sqrt(size(curData,1));
  115. l1 = shadedErrorBar(time,nanmean(curData,1),standError, 'lineprops', {'color', 'k','linewidth', 2}, 'patchSaturation', .1);
  116. curData = squeeze(nanmean(mergeddata(:,idx_chans,:,2),2));
  117. curData = curData-condAvg+repmat(nanmean(condAvg,1),size(condAvg,1),1);
  118. standError = nanstd(curData,1)./sqrt(size(curData,1));
  119. l2 = shadedErrorBar(time,nanmean(curData,1),standError, 'lineprops', {'color', 'r','linewidth', 2}, 'patchSaturation', .1);
  120. % ax = gca; ax.YDir = 'reverse';
  121. legend([l1.mainLine, l2.mainLine],{'old', 'new'}, ...
  122. 'location', 'southwest'); legend('boxoff')
  123. xlabel('Time (s) from stim onset')
  124. xlim([-.05 .6]); %ylim([-.03 .18])
  125. ylabel({'ERP';'(microVolts)'});
  126. xlabel({'Time (s)'});
  127. set(findall(gcf,'-property','FontSize'),'FontSize',14)
  128. figureName = ['c_novelty_centralERP'];
  129. saveas(h, fullfile(pn.figures, figureName), 'epsc');
  130. saveas(h, fullfile(pn.figures, figureName), 'png');
  131. %% plot by group
  132. idx_chans = [4, 38];
  133. % avg across channels and conditions
  134. condAvg = squeeze(nanmean(nanmean(mergeddata(1:17,idx_chans,:,1:2),2),4));
  135. h = figure('units','centimeters','position',[0 0 20 8]);
  136. subplot(1,2,1); cla; hold on;
  137. % highlight relevant phase in background
  138. patches.timeVec = [0.3 0.5];
  139. patches.colorVec = [1 .95 .8];
  140. for indP = 1:size(patches.timeVec,2)-1
  141. YLim = [-8 2]*10^-4;
  142. p = patch([patches.timeVec(indP) patches.timeVec(indP+1) patches.timeVec(indP+1) patches.timeVec(indP)], ...
  143. [YLim(1) YLim(1) YLim(2), YLim(2)], patches.colorVec(indP,:));
  144. p.EdgeColor = 'none';
  145. end
  146. % new value = old value ? subject average + grand average
  147. curData = squeeze(nanmean(mergeddata(1:17,idx_chans,:,1),2));
  148. curData = curData-condAvg+repmat(nanmean(condAvg,1),size(condAvg,1),1);
  149. standError = nanstd(curData,1)./sqrt(size(curData,1));
  150. l1 = shadedErrorBar(time,nanmean(curData,1),standError, 'lineprops', {'color', 'k','linewidth', 2}, 'patchSaturation', .1);
  151. curData = squeeze(nanmean(mergeddata(1:17,idx_chans,:,2),2));
  152. curData = curData-condAvg+repmat(nanmean(condAvg,1),size(condAvg,1),1);
  153. standError = nanstd(curData,1)./sqrt(size(curData,1));
  154. l2 = shadedErrorBar(time,nanmean(curData,1),standError, 'lineprops', {'color', 'r','linewidth', 2}, 'patchSaturation', .1);
  155. % ax = gca; ax.YDir = 'reverse';
  156. legend([l1.mainLine, l2.mainLine],{'old', 'new'}, ...
  157. 'location', 'southwest'); legend('boxoff')
  158. xlabel('Time (s) from stim onset')
  159. xlim([-.05 .6]); %ylim([-.03 .18])
  160. ylabel({'ERP';'(microVolts)'});
  161. xlabel({'Time (s)'});
  162. title('Initial 17')
  163. set(findall(gcf,'-property','FontSize'),'FontSize',14)
  164. subplot(1,2,2); cla; hold on;
  165. % avg across channels and conditions
  166. condAvg = squeeze(nanmean(nanmean(mergeddata(18:end,idx_chans,:,1:2),2),4));
  167. % highlight relevant phase in background
  168. patches.timeVec = [0.3 0.5];
  169. patches.colorVec = [1 .95 .8];
  170. for indP = 1:size(patches.timeVec,2)-1
  171. YLim = [-8 2]*10^-4;
  172. p = patch([patches.timeVec(indP) patches.timeVec(indP+1) patches.timeVec(indP+1) patches.timeVec(indP)], ...
  173. [YLim(1) YLim(1) YLim(2), YLim(2)], patches.colorVec(indP,:));
  174. p.EdgeColor = 'none';
  175. end
  176. % new value = old value ? subject average + grand average
  177. curData = squeeze(nanmean(mergeddata(18:end,idx_chans,:,1),2));
  178. curData = curData-condAvg+repmat(nanmean(condAvg,1),size(condAvg,1),1);
  179. standError = nanstd(curData,1)./sqrt(size(curData,1));
  180. l1 = shadedErrorBar(time,nanmean(curData,1),standError, 'lineprops', {'color', 'k','linewidth', 2}, 'patchSaturation', .1);
  181. curData = squeeze(nanmean(mergeddata(18:end,idx_chans,:,2),2));
  182. curData = curData-condAvg+repmat(nanmean(condAvg,1),size(condAvg,1),1);
  183. standError = nanstd(curData,1)./sqrt(size(curData,1));
  184. l2 = shadedErrorBar(time,nanmean(curData,1),standError, 'lineprops', {'color', 'r','linewidth', 2}, 'patchSaturation', .1);
  185. % ax = gca; ax.YDir = 'reverse';
  186. legend([l1.mainLine, l2.mainLine],{'old', 'new'}, ...
  187. 'location', 'southwest'); legend('boxoff')
  188. xlabel('Time (s) from stim onset')
  189. xlim([-.05 .6]); %ylim([-.03 .18])
  190. ylabel({'ERP';'(microVolts)'});
  191. xlabel({'Time (s)'});
  192. title('Final 16')
  193. set(findall(gcf,'-property','FontSize'),'FontSize',14)
  194. figureName = ['c_novelty_frontalERP_bygroup'];
  195. saveas(h, fullfile(pn.figures, figureName), 'epsc');
  196. saveas(h, fullfile(pn.figures, figureName), 'png');
  197. %% plot difference
  198. idx_chans = [38];
  199. h = figure('units','centimeters','position',[0 0 10 8]);
  200. cla; hold on;
  201. % highlight relevant phase in background
  202. patches.timeVec = [0.3 0.5];
  203. patches.colorVec = [1 .95 .8];
  204. for indP = 1:size(patches.timeVec,2)-1
  205. YLim = [-10 5]*10^-5;
  206. p = patch([patches.timeVec(indP) patches.timeVec(indP+1) patches.timeVec(indP+1) patches.timeVec(indP)], ...
  207. [YLim(1) YLim(1) YLim(2), YLim(2)], patches.colorVec(indP,:));
  208. p.EdgeColor = 'none';
  209. end
  210. % new value = old value ? subject average + grand average
  211. curData = squeeze(mergeddata(:,idx_chans,:,2)-mergeddata(:,idx_chans,:,1));
  212. standError = nanstd(curData,1)./sqrt(size(curData,1));
  213. l1 = shadedErrorBar(time,nanmean(curData,1),standError, 'lineprops', {'color', 'k','linewidth', 2}, 'patchSaturation', .1);
  214. legend([l1.mainLine],{'new-old'}, ...
  215. 'location', 'southwest'); legend('boxoff')
  216. xlabel('Time (s) from stim onset')
  217. xlim([-.05 .6]); %ylim([-.03 .18])
  218. ylabel({'ERP';'(microVolts)'});
  219. xlabel({'Time (s)'});
  220. set(findall(gcf,'-property','FontSize'),'FontSize',14)
  221. %% plot difference between conditions
  222. h = figure('units','centimeters','position',[0 0 10 10]);
  223. set(gcf,'renderer','Painters')
  224. cfg = [];
  225. cfg.layout = 'biosemi64.lay';
  226. cfg.parameter = 'powspctrm';
  227. cfg.comment = 'no';
  228. cfg.colormap = cBrew;
  229. cfg.colorbar = 'EastOutside';
  230. plotData = [];
  231. plotData.label = elec.label; % {1 x N}
  232. plotData.dimord = 'chan';
  233. oldminnew = mergeddata(:,:,:,1)-mergeddata(:,:,:,2);
  234. plotData.powspctrm = squeeze(nanmean(nanmean(oldminnew(:,:,time>=.3 & time <=.5),3),1))';
  235. cfg.figure = h;
  236. ft_topoplotER(cfg,plotData);
  237. cb = colorbar('location', 'EastOutside'); set(get(cb,'ylabel'),'string','Amplitude');