c23_novelty_plot_theta_alpha.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. clear all; cla; clc;
  2. currentFile = mfilename('fullpath');
  3. [pathstr,~,~] = fileparts(currentFile);
  4. cd(fullfile(pathstr,'..'))
  5. rootpath = pwd;
  6. pn.data_eeg = fullfile(rootpath, '..', 'eegmp_preproc', 'data', 'outputs', 'eeg');
  7. pn.data_erp = fullfile(rootpath, 'data', 'erp');
  8. pn.data_erf = fullfile(rootpath, 'data', 'erf');
  9. pn.data = fullfile(rootpath, 'data', 'stats'); mkdir(pn.data);
  10. pn.tools = fullfile(rootpath, 'tools');
  11. addpath(fullfile(rootpath, '..', 'eegmp_preproc', 'tools', 'fieldtrip')); ft_defaults
  12. addpath(genpath(fullfile(pn.tools, '[MEG]PLS', 'MEGPLS_PIPELINE_v2.02b')))
  13. addpath(fullfile(pn.tools, 'BrewerMap'));
  14. addpath(fullfile(pn.tools, 'shadedErrorBar'));
  15. pn.figures = fullfile(rootpath, 'figures');
  16. %% add seed for reproducibility
  17. rng(0, 'twister');
  18. %% load erp
  19. for ind_id = 1:33
  20. id = sprintf('sub-%03d', ind_id); disp(id)
  21. load(fullfile(pn.data_erf, [id,'_erf.mat']));
  22. for ind_option = 1:numel(conds.old)
  23. if ind_id == 1
  24. erpgroup.old.(conds.old{ind_option}) = erf.old{ind_option};
  25. erpgroup.old.(conds.old{ind_option}) = ...
  26. rmfield(erpgroup.old.(conds.old{ind_option}), {'powspctrm'});
  27. erpgroup.old.(conds.old{ind_option}).dimord = 'sub_chan_freq_time';
  28. freq = erpgroup.old.(conds.old{ind_option}).freq;
  29. end
  30. idx_freq_t = freq >2 & freq <8;
  31. thetagroup.old.(conds.old{ind_option}).avg(ind_id,:,:,:) = squeeze(nanmean(erf.old{ind_option}.powspctrm(:,idx_freq_t,:),2));
  32. idx_freq_a = freq >7 & freq <13;
  33. alphagroup.old.(conds.old{ind_option}).avg(ind_id,:,:,:) = squeeze(nanmean(erf.old{ind_option}.powspctrm(:,idx_freq_a,:),2));
  34. end
  35. end
  36. time = erpgroup.old.old.time;
  37. channels = erpgroup.old.old.label;
  38. % identify frontal channels
  39. %idx_chans_t = startsWith(channels, 'F') | startsWith(channels, 'A') | startsWith(channels, 'C') & ~startsWith(channels, 'CP');
  40. % identify parieto-occipital channels
  41. idx_chans_a = startsWith(channels, 'P') | startsWith(channels, 'O');
  42. %% visualize for frontal theta
  43. mergeddata = cat(4, thetagroup.old.old.avg, thetagroup.old.new.avg);
  44. %figure; imagesc(time, [],squeeze(nanmean(thetagroup.old.new.avg-thetagroup.old.old.avg,1)))
  45. idx_chans = [33,4,38];
  46. condAvg = squeeze(nanmean(nanmean(mergeddata(:,idx_chans,:,1:2),2),4));
  47. h = figure('units','centimeters','position',[0 0 10 8]);
  48. cla; hold on;
  49. % highlight relevant phase in background
  50. patches.timeVec = [0.3 0.5];
  51. patches.colorVec = [1 .95 .8];
  52. for indP = 1:size(patches.timeVec,2)-1
  53. YLim = [-4.95 -4.8];
  54. p = patch([patches.timeVec(indP) patches.timeVec(indP+1) patches.timeVec(indP+1) patches.timeVec(indP)], ...
  55. [YLim(1) YLim(1) YLim(2), YLim(2)], patches.colorVec(indP,:));
  56. p.EdgeColor = 'none';
  57. end
  58. % new value = old value ? subject average + grand average
  59. curData = squeeze(nanmean(mergeddata(:,idx_chans,:,1),2));
  60. curData = curData-condAvg+repmat(nanmean(condAvg,1),size(condAvg,1),1);
  61. standError = nanstd(curData,1)./sqrt(size(curData,1));
  62. l1 = shadedErrorBar(time,nanmean(curData,1),standError, 'lineprops', ...
  63. {'color', 'k','linewidth', 2}, 'patchSaturation', .1);
  64. curData = squeeze(nanmean(mergeddata(:,idx_chans,:,2),2));
  65. curData = curData-condAvg+repmat(nanmean(condAvg,1),size(condAvg,1),1);
  66. standError = nanstd(curData,1)./sqrt(size(curData,1));
  67. l2 = shadedErrorBar(time,nanmean(curData,1),standError, 'lineprops', ...
  68. {'color', 'r','linewidth', 2}, 'patchSaturation', .1);
  69. xlabel('Time (s) from stim onset')
  70. legend([l1.mainLine, l2.mainLine],{'old', 'new'}, ...
  71. 'location', 'southeast'); legend('boxoff')
  72. xlim([-.5 1.5]); ylim(YLim)
  73. %xlim([-.05 .3]);
  74. ylabel({'theta power';'(log10)'});
  75. xlabel({'Time (s)'});
  76. set(findall(gcf,'-property','FontSize'),'FontSize',12)
  77. figureName = ['c_novelty_frontaltheta'];
  78. saveas(h, fullfile(pn.figures, figureName), 'epsc');
  79. saveas(h, fullfile(pn.figures, figureName), 'png');
  80. %% visualize for two groups
  81. idx_chans = [33,4,38];
  82. condAvg = squeeze(nanmean(nanmean(mergeddata(1:17,idx_chans,:,1:2),2),4));
  83. h = figure('units','centimeters','position',[0 0 20 8]);
  84. subplot(1,2,1); cla; hold on;
  85. % highlight relevant phase in background
  86. patches.timeVec = [0.3 0.5];
  87. patches.colorVec = [1 .95 .8];
  88. for indP = 1:size(patches.timeVec,2)-1
  89. YLim = [-4.95 -4.75];
  90. p = patch([patches.timeVec(indP) patches.timeVec(indP+1) patches.timeVec(indP+1) patches.timeVec(indP)], ...
  91. [YLim(1) YLim(1) YLim(2), YLim(2)], patches.colorVec(indP,:));
  92. p.EdgeColor = 'none';
  93. end
  94. % new value = old value ? subject average + grand average
  95. curData = squeeze(nanmean(mergeddata(1:17,idx_chans,:,1),2));
  96. curData = curData-condAvg+repmat(nanmean(condAvg,1),size(condAvg,1),1);
  97. standError = nanstd(curData,1)./sqrt(size(curData,1));
  98. l1 = shadedErrorBar(time,nanmean(curData,1),standError, 'lineprops', ...
  99. {'color', 'k','linewidth', 2}, 'patchSaturation', .1);
  100. curData = squeeze(nanmean(mergeddata(1:17,idx_chans,:,2),2));
  101. curData = curData-condAvg+repmat(nanmean(condAvg,1),size(condAvg,1),1);
  102. standError = nanstd(curData,1)./sqrt(size(curData,1));
  103. l2 = shadedErrorBar(time,nanmean(curData,1),standError, 'lineprops', ...
  104. {'color', 'r','linewidth', 2}, 'patchSaturation', .1);
  105. xlabel('Time (s) from stim onset')
  106. legend([l1.mainLine, l2.mainLine],{'old', 'new'}, ...
  107. 'location', 'NorthEast'); legend('boxoff')
  108. xlim([-.5 1.5]); ylim(YLim)
  109. %xlim([-.05 .3]);
  110. ylabel({'theta power';'(log10)'});
  111. xlabel({'Time (s)'});
  112. title('Initial 17');
  113. set(findall(gcf,'-property','FontSize'),'FontSize',12)
  114. subplot(1,2,2); cla; hold on;
  115. condAvg = squeeze(nanmean(nanmean(mergeddata(18:end,idx_chans,:,1:2),2),4));
  116. % highlight relevant phase in background
  117. patches.timeVec = [0.3 0.5];
  118. patches.colorVec = [1 .95 .8];
  119. for indP = 1:size(patches.timeVec,2)-1
  120. YLim = [-4.95 -4.75];
  121. p = patch([patches.timeVec(indP) patches.timeVec(indP+1) patches.timeVec(indP+1) patches.timeVec(indP)], ...
  122. [YLim(1) YLim(1) YLim(2), YLim(2)], patches.colorVec(indP,:));
  123. p.EdgeColor = 'none';
  124. end
  125. % new value = old value ? subject average + grand average
  126. curData = squeeze(nanmean(mergeddata(18:end,idx_chans,:,1),2));
  127. curData = curData-condAvg+repmat(nanmean(condAvg,1),size(condAvg,1),1);
  128. standError = nanstd(curData,1)./sqrt(size(curData,1));
  129. l1 = shadedErrorBar(time,nanmean(curData,1),standError, 'lineprops', ...
  130. {'color', 'k','linewidth', 2}, 'patchSaturation', .1);
  131. curData = squeeze(nanmean(mergeddata(18:end,idx_chans,:,2),2));
  132. curData = curData-condAvg+repmat(nanmean(condAvg,1),size(condAvg,1),1);
  133. standError = nanstd(curData,1)./sqrt(size(curData,1));
  134. l2 = shadedErrorBar(time,nanmean(curData,1),standError, 'lineprops', ...
  135. {'color', 'r','linewidth', 2}, 'patchSaturation', .1);
  136. xlabel('Time (s) from stim onset')
  137. legend([l1.mainLine, l2.mainLine],{'old', 'new'}, ...
  138. 'location', 'NorthEast'); legend('boxoff')
  139. xlim([-.5 1.5]); ylim(YLim)
  140. %xlim([-.05 .3]);
  141. ylabel({'theta power';'(log10)'});
  142. xlabel({'Time (s)'});
  143. title('Final 16');
  144. set(findall(gcf,'-property','FontSize'),'FontSize',12)
  145. figureName = ['c_novelty_frontaltheta_bygroup'];
  146. saveas(h, fullfile(pn.figures, figureName), 'epsc');
  147. saveas(h, fullfile(pn.figures, figureName), 'png');
  148. %% visualize for posterior alpha
  149. mergeddata = cat(4, alphagroup.old.old.avg, alphagroup.old.new.avg);
  150. idx_chans = idx_chans_a;
  151. condAvg = squeeze(nanmean(nanmean(mergeddata(:,idx_chans,:,1:2),2),4));
  152. h = figure('units','centimeters','position',[0 0 10 8]);
  153. cla; hold on;
  154. % highlight relevant phase in background
  155. patches.timeVec = [0.3 0.5];
  156. patches.colorVec = [1 .95 .8];
  157. for indP = 1:size(patches.timeVec,2)-1
  158. YLim = [-5.2 -4.5];
  159. p = patch([patches.timeVec(indP) patches.timeVec(indP+1) patches.timeVec(indP+1) patches.timeVec(indP)], ...
  160. [YLim(1) YLim(1) YLim(2), YLim(2)], patches.colorVec(indP,:));
  161. p.EdgeColor = 'none';
  162. end
  163. % new value = old value ? subject average + grand average
  164. curData = squeeze(nanmean(mergeddata(:,idx_chans,:,1),2));
  165. curData = curData-condAvg+repmat(nanmean(condAvg,1),size(condAvg,1),1);
  166. standError = nanstd(curData,1)./sqrt(size(curData,1));
  167. l1 = shadedErrorBar(time,nanmean(curData,1),standError, 'lineprops', ...
  168. {'color', 'k','linewidth', 3}, 'patchSaturation', .1);
  169. curData = squeeze(nanmean(mergeddata(:,idx_chans,:,2),2));
  170. curData = curData-condAvg+repmat(nanmean(condAvg,1),size(condAvg,1),1);
  171. standError = nanstd(curData,1)./sqrt(size(curData,1));
  172. l2 = shadedErrorBar(time,nanmean(curData,1),standError, 'lineprops', ...
  173. {'color', 'r','linewidth', 3}, 'patchSaturation', .1);
  174. xlabel('Time (s) from stim onset')
  175. legend([l1.mainLine, l2.mainLine],{'old', 'new'}, 'location', 'northeast'); legend('boxoff')
  176. xlim([-.5 1.5]); ylim(YLim)
  177. %xlim([-.05 .3]);
  178. ylabel({'alpha power';'(log10)'});
  179. xlabel({'Time (s)'});
  180. set(findall(gcf,'-property','FontSize'),'FontSize',15)
  181. figureName = ['c_novelty_alpha'];
  182. saveas(h, fullfile(pn.figures, figureName), 'epsc');
  183. saveas(h, fullfile(pn.figures, figureName), 'png');
  184. %% visualize for two groups
  185. idx_chans = idx_chans_a;
  186. condAvg = squeeze(nanmean(nanmean(mergeddata(1:17,idx_chans,:,1:2),2),4));
  187. h = figure('units','centimeters','position',[0 0 20 8]);
  188. subplot(1,2,1); cla; hold on;
  189. % highlight relevant phase in background
  190. patches.timeVec = [0.3 0.5];
  191. patches.colorVec = [1 .95 .8];
  192. for indP = 1:size(patches.timeVec,2)-1
  193. YLim = [-5.2 -4.5];
  194. p = patch([patches.timeVec(indP) patches.timeVec(indP+1) patches.timeVec(indP+1) patches.timeVec(indP)], ...
  195. [YLim(1) YLim(1) YLim(2), YLim(2)], patches.colorVec(indP,:));
  196. p.EdgeColor = 'none';
  197. end
  198. % new value = old value ? subject average + grand average
  199. curData = squeeze(nanmean(mergeddata(1:17,idx_chans,:,1),2));
  200. curData = curData-condAvg+repmat(nanmean(condAvg,1),size(condAvg,1),1);
  201. standError = nanstd(curData,1)./sqrt(size(curData,1));
  202. l1 = shadedErrorBar(time,nanmean(curData,1),standError, 'lineprops', ...
  203. {'color', 'k','linewidth', 2}, 'patchSaturation', .1);
  204. curData = squeeze(nanmean(mergeddata(1:17,idx_chans,:,2),2));
  205. curData = curData-condAvg+repmat(nanmean(condAvg,1),size(condAvg,1),1);
  206. standError = nanstd(curData,1)./sqrt(size(curData,1));
  207. l2 = shadedErrorBar(time,nanmean(curData,1),standError, 'lineprops', ...
  208. {'color', 'r','linewidth', 2}, 'patchSaturation', .1);
  209. xlabel('Time (s) from stim onset')
  210. legend([l1.mainLine, l2.mainLine],{'old', 'new'}, ...
  211. 'location', 'NorthEast'); legend('boxoff')
  212. xlim([-.5 1.5]); ylim(YLim)
  213. %xlim([-.05 .3]);
  214. ylabel({'alpha power';'(log10)'});
  215. xlabel({'Time (s)'});
  216. title('Initial 17');
  217. set(findall(gcf,'-property','FontSize'),'FontSize',12)
  218. subplot(1,2,2); cla; hold on;
  219. condAvg = squeeze(nanmean(nanmean(mergeddata(18:end,idx_chans,:,1:2),2),4));
  220. % highlight relevant phase in background
  221. patches.timeVec = [0.3 0.5];
  222. patches.colorVec = [1 .95 .8];
  223. for indP = 1:size(patches.timeVec,2)-1
  224. YLim = [-5.2 -4.5];
  225. p = patch([patches.timeVec(indP) patches.timeVec(indP+1) patches.timeVec(indP+1) patches.timeVec(indP)], ...
  226. [YLim(1) YLim(1) YLim(2), YLim(2)], patches.colorVec(indP,:));
  227. p.EdgeColor = 'none';
  228. end
  229. % new value = old value ? subject average + grand average
  230. curData = squeeze(nanmean(mergeddata(18:end,idx_chans,:,1),2));
  231. curData = curData-condAvg+repmat(nanmean(condAvg,1),size(condAvg,1),1);
  232. standError = nanstd(curData,1)./sqrt(size(curData,1));
  233. l1 = shadedErrorBar(time,nanmean(curData,1),standError, 'lineprops', ...
  234. {'color', 'k','linewidth', 2}, 'patchSaturation', .1);
  235. curData = squeeze(nanmean(mergeddata(18:end,idx_chans,:,2),2));
  236. curData = curData-condAvg+repmat(nanmean(condAvg,1),size(condAvg,1),1);
  237. standError = nanstd(curData,1)./sqrt(size(curData,1));
  238. l2 = shadedErrorBar(time,nanmean(curData,1),standError, 'lineprops', ...
  239. {'color', 'r','linewidth', 2}, 'patchSaturation', .1);
  240. xlabel('Time (s) from stim onset')
  241. legend([l1.mainLine, l2.mainLine],{'old', 'new'}, ...
  242. 'location', 'NorthEast'); legend('boxoff')
  243. xlim([-.5 1.5]); ylim(YLim)
  244. %xlim([-.05 .3]);
  245. ylabel({'alpha power';'(log10)'});
  246. xlabel({'Time (s)'});
  247. title('Final 16');
  248. set(findall(gcf,'-property','FontSize'),'FontSize',12)
  249. figureName = ['c_novelty_alpha_bygroup'];
  250. saveas(h, fullfile(pn.figures, figureName), 'epsc');
  251. saveas(h, fullfile(pn.figures, figureName), 'png');
  252. %% plot superimposed
  253. mergeddata = cat(4, thetagroup.old.old.avg, thetagroup.old.new.avg);
  254. idx_chans = [33,4,47];
  255. h = figure('units','centimeters','position',[0 0 10 8]);
  256. cla; hold on;
  257. % new value = old value ? subject average + grand average
  258. curData = squeeze(nanmean(nanmean(mergeddata(1:17,idx_chans,:,1:2),4),2));
  259. standError = nanstd(curData,1)./sqrt(size(curData,1));
  260. l1 = shadedErrorBar(time,nanmean(curData,1),standError, 'lineprops', ...
  261. {'color', 'k','linewidth', 2}, 'patchSaturation', .1);
  262. curData = squeeze(nanmean(nanmean(mergeddata(18:end,idx_chans,:,1:2),4),2));
  263. standError = nanstd(curData,1)./sqrt(size(curData,1));
  264. l2 = shadedErrorBar(time,nanmean(curData,1),standError, 'lineprops', ...
  265. {'color', 'r','linewidth', 2}, 'patchSaturation', .1);
  266. xlabel('Time (s) from stim onset')
  267. legend([l1.mainLine, l2.mainLine],{'initial', 'final'}, ...
  268. 'location', 'southeast'); legend('boxoff')
  269. xlim([-.5 1.5]); ylim([-5.1 -4.75])
  270. ylabel({'theta power';'(log10)'});
  271. set(findall(gcf,'-property','FontSize'),'FontSize',12)
  272. mergeddata = cat(4, alphagroup.old.old.avg, alphagroup.old.new.avg);
  273. idx_chans = idx_chans_a;
  274. h = figure('units','centimeters','position',[0 0 10 8]);
  275. cla; hold on;
  276. % new value = old value ? subject average + grand average
  277. curData = squeeze(nanmean(nanmean(mergeddata(1:17,idx_chans,:,1:2),4),2));
  278. standError = nanstd(curData,1)./sqrt(size(curData,1));
  279. l1 = shadedErrorBar(time,nanmean(curData,1),standError, 'lineprops', ...
  280. {'color', 'k','linewidth', 2}, 'patchSaturation', .1);
  281. curData = squeeze(nanmean(nanmean(mergeddata(18:end,idx_chans,:,1:2),4),2));
  282. standError = nanstd(curData,1)./sqrt(size(curData,1));
  283. l2 = shadedErrorBar(time,nanmean(curData,1),standError, 'lineprops', ...
  284. {'color', 'r','linewidth', 2}, 'patchSaturation', .1);
  285. xlabel('Time (s) from stim onset')
  286. legend([l1.mainLine, l2.mainLine],{'initial', 'final'}, ...
  287. 'location', 'southeast'); legend('boxoff')
  288. xlim([-.5 1.5]); ylim([-5.2 -4.5])
  289. ylabel({'alpha power';'(log10)'});
  290. set(findall(gcf,'-property','FontSize'),'FontSize',12)