b2a_taskPLS_novelty_frontal_erp_plot_trace.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. %% load erp
  13. for ind_id = 1:33
  14. id = sprintf('sub-%03d', ind_id);
  15. load(fullfile(pn.data_erp, [id,'_erp_bl.mat']));
  16. for ind_option = 1:numel(conds.old)
  17. if ind_id == 1
  18. erpgroup.old.(conds.old{ind_option}) = erp_bl.old{ind_option};
  19. erpgroup.old.(conds.old{ind_option}) = ...
  20. rmfield(erpgroup.old.(conds.old{ind_option}), {'avg', 'var', 'dof'});
  21. erpgroup.old.(conds.old{ind_option}).dimord = 'sub_chan_time';
  22. end
  23. erpgroup.old.(conds.old{ind_option}).avg(ind_id,:,:) = erp_bl.old{ind_option}.avg;
  24. end
  25. end
  26. time = erpgroup.old.old.time;
  27. elec = erpgroup.old.old.elec;
  28. channels = erpgroup.old.old.label;
  29. %idx_chans = find(ismember(channels, {'O1', 'Oz', 'O2'}));
  30. %idx_chans = find(ismember(channels, {'PO7', 'PO8'}));
  31. %idx_chans = find(ismember(channels, {'Pz', 'CPz', 'P1'}));
  32. mergeddata = cat(4, erpgroup.old.old.avg, ...
  33. erpgroup.old.new.avg);
  34. %% plot ERP topography
  35. % set custom colormap
  36. cBrew = brewermap(500,'RdBu');
  37. cBrew = flipud(cBrew);
  38. colormap(cBrew)
  39. h = figure('units','centimeters','position',[0 0 10 10]);
  40. set(gcf,'renderer','Painters')
  41. cfg = [];
  42. cfg.layout = 'biosemi64.lay';
  43. cfg.parameter = 'powspctrm';
  44. cfg.comment = 'no';
  45. cfg.colormap = cBrew;
  46. cfg.colorbar = 'EastOutside';
  47. plotData = [];
  48. plotData.label = elec.label; % {1 x N}
  49. plotData.dimord = 'chan';
  50. plotData.powspctrm = squeeze(nanmean(nanmean(nanmean(mergeddata(:,:,time>=0.3& time <=0.5,1:2),3),1),4))';
  51. cfg.zlim = [-14 14]*10^-4;
  52. cfg.figure = h;
  53. ft_topoplotER(cfg,plotData);
  54. cb = colorbar('location', 'EastOutside'); set(get(cb,'ylabel'),'string','Amplitude');
  55. % figureName = ['xxx'];
  56. % saveas(h, fullfile(pn.figures, figureName), 'epsc');
  57. % saveas(h, fullfile(pn.figures, figureName), 'png');
  58. %% visualize frontal ERP
  59. idx_chans = [38];
  60. % avg across channels and conditions
  61. condAvg = squeeze(nanmean(nanmean(mergeddata(:,idx_chans,:,1:2),2),4));
  62. h = figure('units','centimeters','position',[0 0 10 8]);
  63. cla; hold on;
  64. % highlight relevant phase in background
  65. patches.timeVec = [0.3 0.5];
  66. patches.colorVec = [1 .95 .8];
  67. for indP = 1:size(patches.timeVec,2)-1
  68. YLim = [-8 2]*10^-4;
  69. p = patch([patches.timeVec(indP) patches.timeVec(indP+1) patches.timeVec(indP+1) patches.timeVec(indP)], ...
  70. [YLim(1) YLim(1) YLim(2), YLim(2)], patches.colorVec(indP,:));
  71. p.EdgeColor = 'none';
  72. end
  73. % new value = old value ? subject average + grand average
  74. curData = squeeze(nanmean(mergeddata(:,idx_chans,:,1),2));
  75. curData = curData-condAvg+repmat(nanmean(condAvg,1),size(condAvg,1),1);
  76. standError = nanstd(curData,1)./sqrt(size(curData,1));
  77. l1 = shadedErrorBar(time,nanmean(curData,1),standError, 'lineprops', {'color', 'k','linewidth', 2}, 'patchSaturation', .1);
  78. curData = squeeze(nanmean(mergeddata(:,idx_chans,:,2),2));
  79. curData = curData-condAvg+repmat(nanmean(condAvg,1),size(condAvg,1),1);
  80. standError = nanstd(curData,1)./sqrt(size(curData,1));
  81. l2 = shadedErrorBar(time,nanmean(curData,1),standError, 'lineprops', {'color', 'r','linewidth', 2}, 'patchSaturation', .1);
  82. % ax = gca; ax.YDir = 'reverse';
  83. legend([l1.mainLine, l2.mainLine],{'old', 'new'}, ...
  84. 'location', 'southwest'); legend('boxoff')
  85. xlabel('Time (s) from stim onset')
  86. xlim([-.05 .6]); %ylim([-.03 .18])
  87. ylabel({'ERP';'(microVolts)'});
  88. xlabel({'Time (s)'});
  89. set(findall(gcf,'-property','FontSize'),'FontSize',14)
  90. %% plot difference
  91. idx_chans = [38];
  92. h = figure('units','centimeters','position',[0 0 10 8]);
  93. cla; hold on;
  94. % highlight relevant phase in background
  95. patches.timeVec = [0.3 0.5];
  96. patches.colorVec = [1 .95 .8];
  97. for indP = 1:size(patches.timeVec,2)-1
  98. YLim = [-10 5]*10^-5;
  99. p = patch([patches.timeVec(indP) patches.timeVec(indP+1) patches.timeVec(indP+1) patches.timeVec(indP)], ...
  100. [YLim(1) YLim(1) YLim(2), YLim(2)], patches.colorVec(indP,:));
  101. p.EdgeColor = 'none';
  102. end
  103. % new value = old value ? subject average + grand average
  104. curData = squeeze(mergeddata(:,idx_chans,:,2)-mergeddata(:,idx_chans,:,1));
  105. standError = nanstd(curData,1)./sqrt(size(curData,1));
  106. l1 = shadedErrorBar(time,nanmean(curData,1),standError, 'lineprops', {'color', 'k','linewidth', 2}, 'patchSaturation', .1);
  107. legend([l1.mainLine],{'new-old'}, ...
  108. 'location', 'southwest'); legend('boxoff')
  109. xlabel('Time (s) from stim onset')
  110. xlim([-.05 .6]); %ylim([-.03 .18])
  111. ylabel({'ERP';'(microVolts)'});
  112. xlabel({'Time (s)'});
  113. set(findall(gcf,'-property','FontSize'),'FontSize',14)
  114. %% plot difference between conditions
  115. h = figure('units','centimeters','position',[0 0 10 10]);
  116. set(gcf,'renderer','Painters')
  117. cfg = [];
  118. cfg.layout = 'biosemi64.lay';
  119. cfg.parameter = 'powspctrm';
  120. cfg.comment = 'no';
  121. cfg.colormap = cBrew;
  122. cfg.colorbar = 'EastOutside';
  123. plotData = [];
  124. plotData.label = elec.label; % {1 x N}
  125. plotData.dimord = 'chan';
  126. oldminnew = mergeddata(:,:,:,1)-mergeddata(:,:,:,2);
  127. plotData.powspctrm = squeeze(nanmean(nanmean(oldminnew(:,:,time>=.3 & time <=.5),3),1))';
  128. cfg.figure = h;
  129. ft_topoplotER(cfg,plotData);
  130. cb = colorbar('location', 'EastOutside'); set(get(cb,'ylabel'),'string','Amplitude');