c23_novelty_plot_theta_alpha.m 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. % new value = old value ? subject average + grand average
  117. curData = squeeze(nanmean(mergeddata(18:end,idx_chans,:,1),2));
  118. curData = curData-condAvg+repmat(nanmean(condAvg,1),size(condAvg,1),1);
  119. standError = nanstd(curData,1)./sqrt(size(curData,1));
  120. l1 = shadedErrorBar(time,nanmean(curData,1),standError, 'lineprops', ...
  121. {'color', 'k','linewidth', 2}, 'patchSaturation', .1);
  122. curData = squeeze(nanmean(mergeddata(18:end,idx_chans,:,2),2));
  123. curData = curData-condAvg+repmat(nanmean(condAvg,1),size(condAvg,1),1);
  124. standError = nanstd(curData,1)./sqrt(size(curData,1));
  125. l2 = shadedErrorBar(time,nanmean(curData,1),standError, 'lineprops', ...
  126. {'color', 'r','linewidth', 2}, 'patchSaturation', .1);
  127. xlabel('Time (s) from stim onset')
  128. legend([l1.mainLine, l2.mainLine],{'old', 'new'}, ...
  129. 'location', 'NorthEast'); legend('boxoff')
  130. xlim([-.5 1.5]);
  131. %xlim([-.05 .3]);
  132. ylabel({'theta power';'(log10)'});
  133. xlabel({'Time (s)'});
  134. title('Final 16');
  135. set(findall(gcf,'-property','FontSize'),'FontSize',12)
  136. figureName = ['c_novelty_frontaltheta_bygroup'];
  137. saveas(h, fullfile(pn.figures, figureName), 'epsc');
  138. saveas(h, fullfile(pn.figures, figureName), 'png');
  139. %% visualize for posterior alpha
  140. mergeddata = cat(4, alphagroup.old.old.avg, alphagroup.old.new.avg);
  141. idx_chans = idx_chans_a;
  142. condAvg = squeeze(nanmean(nanmean(mergeddata(:,idx_chans,:,1:2),2),4));
  143. h = figure('units','centimeters','position',[0 0 10 8]);
  144. cla; hold on;
  145. % new value = old value ? subject average + grand average
  146. curData = squeeze(nanmean(mergeddata(:,idx_chans,:,1),2));
  147. curData = curData-condAvg+repmat(nanmean(condAvg,1),size(condAvg,1),1);
  148. standError = nanstd(curData,1)./sqrt(size(curData,1));
  149. l1 = shadedErrorBar(time,nanmean(curData,1),standError, 'lineprops', ...
  150. {'color', 'k','linewidth', 3}, 'patchSaturation', .1);
  151. curData = squeeze(nanmean(mergeddata(:,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', ...
  155. {'color', 'r','linewidth', 3}, 'patchSaturation', .1);
  156. xlabel('Time (s) from stim onset')
  157. legend([l1.mainLine, l2.mainLine],{'old', 'new'}, 'location', 'northeast'); legend('boxoff')
  158. xlim([-.5 1.5]);
  159. %xlim([-.05 .3]);
  160. ylabel({'alpha power';'(log10)'});
  161. xlabel({'Time (s)'});
  162. set(findall(gcf,'-property','FontSize'),'FontSize',15)
  163. figureName = ['c_novelty_alpha'];
  164. saveas(h, fullfile(pn.figures, figureName), 'epsc');
  165. saveas(h, fullfile(pn.figures, figureName), 'png');