b2x_novelty_plot_theta_alpha.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. %% add seed for reproducibility
  16. rng(0, 'twister');
  17. %% load erp
  18. for ind_id = 1:33
  19. id = sprintf('sub-%03d', ind_id); disp(id)
  20. load(fullfile(pn.data_erf, [id,'_erf.mat']));
  21. for ind_option = 1:numel(conds.old)
  22. if ind_id == 1
  23. erpgroup.old.(conds.old{ind_option}) = erf.old{ind_option};
  24. erpgroup.old.(conds.old{ind_option}) = ...
  25. rmfield(erpgroup.old.(conds.old{ind_option}), {'powspctrm'});
  26. erpgroup.old.(conds.old{ind_option}).dimord = 'sub_chan_freq_time';
  27. freq = erpgroup.old.(conds.old{ind_option}).freq;
  28. end
  29. idx_freq_t = freq >2 & freq <8;
  30. thetagroup.old.(conds.old{ind_option}).avg(ind_id,:,:,:) = squeeze(nanmean(erf.old{ind_option}.powspctrm(:,idx_freq_t,:),2));
  31. idx_freq_a = freq >7 & freq <13;
  32. alphagroup.old.(conds.old{ind_option}).avg(ind_id,:,:,:) = squeeze(nanmean(erf.old{ind_option}.powspctrm(:,idx_freq_a,:),2));
  33. end
  34. end
  35. time = erpgroup.old.old.time;
  36. channels = erpgroup.old.old.label;
  37. % identify frontal channels
  38. %idx_chans_t = startsWith(channels, 'F') | startsWith(channels, 'A') | startsWith(channels, 'C') & ~startsWith(channels, 'CP');
  39. % identify parieto-occipital channels
  40. idx_chans_a = startsWith(channels, 'P') | startsWith(channels, 'O');
  41. %% visualize for frontal theta
  42. mergeddata = cat(4, thetagroup.old.old.avg, thetagroup.old.new.avg);
  43. %figure; imagesc(squeeze(nanmean(thetagroup.old.new.avg-thetagroup.old.old.avg,1)))
  44. idx_chans = [33,38,47];
  45. condAvg = squeeze(nanmean(nanmean(mergeddata(:,idx_chans,:,1:2),2),4));
  46. h = figure('units','centimeters','position',[0 0 10 8]);
  47. cla; hold on;
  48. % highlight relevant phase in background
  49. patches.timeVec = [0.3 0.5];
  50. patches.colorVec = [1 .95 .8];
  51. for indP = 1:size(patches.timeVec,2)-1
  52. YLim = [-5 -4.8];
  53. p = patch([patches.timeVec(indP) patches.timeVec(indP+1) patches.timeVec(indP+1) patches.timeVec(indP)], ...
  54. [YLim(1) YLim(1) YLim(2), YLim(2)], patches.colorVec(indP,:));
  55. p.EdgeColor = 'none';
  56. end
  57. % new value = old value ? subject average + grand average
  58. curData = squeeze(nanmean(mergeddata(:,idx_chans,:,1),2));
  59. curData = curData-condAvg+repmat(nanmean(condAvg,1),size(condAvg,1),1);
  60. standError = nanstd(curData,1)./sqrt(size(curData,1));
  61. l1 = shadedErrorBar(time,nanmean(curData,1),standError, 'lineprops', ...
  62. {'color', 'k','linewidth', 2}, 'patchSaturation', .1);
  63. curData = squeeze(nanmean(mergeddata(:,idx_chans,:,2),2));
  64. curData = curData-condAvg+repmat(nanmean(condAvg,1),size(condAvg,1),1);
  65. standError = nanstd(curData,1)./sqrt(size(curData,1));
  66. l2 = shadedErrorBar(time,nanmean(curData,1),standError, 'lineprops', ...
  67. {'color', 'r','linewidth', 2}, 'patchSaturation', .1);
  68. xlabel('Time (s) from stim onset')
  69. legend([l1.mainLine, l2.mainLine],{'old', 'new'}, ...
  70. 'location', 'southeast'); legend('boxoff')
  71. xlim([-.5 1.5]); ylim(YLim)
  72. %xlim([-.05 .3]);
  73. ylabel({'theta power';'(log10)'});
  74. xlabel({'Time (s)'});
  75. set(findall(gcf,'-property','FontSize'),'FontSize',12)
  76. %% visualize for posterior alpha
  77. mergeddata = cat(4, alphagroup.old.old.avg, alphagroup.old.new.avg);
  78. idx_chans = idx_chans_a;
  79. condAvg = squeeze(nanmean(nanmean(mergeddata(:,idx_chans,:,1:2),2),4));
  80. h = figure('units','centimeters','position',[0 0 10 8]);
  81. cla; hold on;
  82. % highlight relevant phase in background
  83. patches.timeVec = [0.3 0.5];
  84. patches.colorVec = [1 .95 .8];
  85. for indP = 1:size(patches.timeVec,2)-1
  86. YLim = [-5.2 -4.5];
  87. p = patch([patches.timeVec(indP) patches.timeVec(indP+1) patches.timeVec(indP+1) patches.timeVec(indP)], ...
  88. [YLim(1) YLim(1) YLim(2), YLim(2)], patches.colorVec(indP,:));
  89. p.EdgeColor = 'none';
  90. end
  91. % new value = old value ? subject average + grand average
  92. curData = squeeze(nanmean(mergeddata(:,idx_chans,:,1),2));
  93. curData = curData-condAvg+repmat(nanmean(condAvg,1),size(condAvg,1),1);
  94. standError = nanstd(curData,1)./sqrt(size(curData,1));
  95. l1 = shadedErrorBar(time,nanmean(curData,1),standError, 'lineprops', ...
  96. {'color', 'k','linewidth', 3}, 'patchSaturation', .1);
  97. curData = squeeze(nanmean(mergeddata(:,idx_chans,:,2),2));
  98. curData = curData-condAvg+repmat(nanmean(condAvg,1),size(condAvg,1),1);
  99. standError = nanstd(curData,1)./sqrt(size(curData,1));
  100. l2 = shadedErrorBar(time,nanmean(curData,1),standError, 'lineprops', ...
  101. {'color', 'r','linewidth', 3}, 'patchSaturation', .1);
  102. xlabel('Time (s) from stim onset')
  103. legend([l1.mainLine, l2.mainLine],{'old', 'new'}, 'location', 'northeast'); legend('boxoff')
  104. xlim([-.5 1.5]); ylim(YLim)
  105. %xlim([-.05 .3]);
  106. ylabel({'alpha power';'(log10)'});
  107. xlabel({'Time (s)'});
  108. set(findall(gcf,'-property','FontSize'),'FontSize',15)