b_scenecat_n1.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 event info
  13. load(fullfile(pn.data_eeg, ['sub-001_task-xxxx_eeg_art.mat']), 'events');
  14. parameter = {'scene_category'; 'old'; 'behavior'; 'subsequent_memory'};
  15. for ind_param = 1:numel(parameter)
  16. conds.(parameter{ind_param}) = unique(events.(parameter{ind_param}));
  17. end
  18. %% load erp
  19. for ind_id = 1:33
  20. id = sprintf('sub-%03d', ind_id);
  21. load(fullfile(pn.data_erp, [id,'_erp.mat']));
  22. for ind_option = 1:numel(conds.scene_category)
  23. if ind_id == 1
  24. erpgroup.scene_category.(conds.scene_category{ind_option}) = erp.scene_category{ind_option};
  25. erpgroup.scene_category.(conds.scene_category{ind_option}) = ...
  26. rmfield(erpgroup.scene_category.(conds.scene_category{ind_option}), {'avg', 'var', 'dof'});
  27. erpgroup.scene_category.(conds.scene_category{ind_option}).dimord = 'sub_chan_time';
  28. end
  29. erpgroup.scene_category.(conds.scene_category{ind_option}).avg(ind_id,:,:) = erp.scene_category{ind_option}.avg;
  30. end
  31. end
  32. time = erpgroup.scene_category.manmade.time;
  33. elec = erpgroup.scene_category.manmade.elec;
  34. channels = erpgroup.scene_category.manmade.label;
  35. %idx_chans = find(ismember(channels, {'O1', 'Oz', 'O2'}));
  36. %idx_chans = find(ismember(channels, {'PO7', 'PO8'}));
  37. %idx_chans = find(ismember(channels, {'Pz', 'CPz', 'P1'}));
  38. mergeddata = cat(4, erpgroup.scene_category.manmade.avg, ...
  39. erpgroup.scene_category.natural.avg);
  40. %% plot topography of visual N1
  41. % set custom colormap
  42. cBrew = brewermap(500,'RdBu');
  43. cBrew = flipud(cBrew);
  44. colormap(cBrew)
  45. h = figure('units','centimeters','position',[0 0 10 10]);
  46. set(gcf,'renderer','Painters')
  47. cfg = [];
  48. cfg.layout = 'EEG1010.lay';
  49. cfg.parameter = 'powspctrm';
  50. cfg.comment = 'no';
  51. cfg.colormap = cBrew;
  52. cfg.colorbar = 'EastOutside';
  53. plotData = [];
  54. plotData.label = elec.label(1:64); % {1 x N}
  55. plotData.dimord = 'chan';
  56. plotData.powspctrm = squeeze(nanmean(nanmean(nanmin(mergeddata(:,:,time>0.04 & time <0.12,1:2),[],3),1),4))';
  57. [~, sortidx] = sort(plotData.powspctrm, 'ascend');
  58. idx_chans = sortidx(1);
  59. idx_chans_visual = idx_chans;
  60. cfg.marker = 'off';
  61. cfg.highlight = 'yes';
  62. cfg.highlightchannel = plotData.label(idx_chans);
  63. cfg.highlightcolor = [1 0 0];
  64. cfg.highlightsymbol = '.';
  65. cfg.highlightsize = 18;
  66. cfg.zlim = [-10 10]*10^-4;
  67. cfg.figure = h;
  68. ft_topoplotER(cfg,plotData);
  69. cb = colorbar('location', 'EastOutside'); set(get(cb,'ylabel'),'string','Amplitude');
  70. %% visualize N1 over negative maximum
  71. % avg across channels and conditions
  72. condAvg = squeeze(nanmean(nanmean(mergeddata(:,idx_chans,:,1:2),2),4));
  73. condAvg1 = squeeze(nanmean(nanmean(mergeddata(:,idx_chans,:,1),2),4));
  74. condAvg2 = squeeze(nanmean(nanmean(mergeddata(:,idx_chans,:,2),2),4));
  75. h = figure('units','centimeters','position',[0 0 10 8]);
  76. cla; hold on;
  77. % new value = old value ? subject average + grand average
  78. curData = squeeze(nanmean(mergeddata(:,idx_chans,:,1),2));
  79. curData = curData-condAvg+repmat(nanmean(condAvg,1),size(condAvg,1),1);
  80. standError = nanstd(curData,1)./sqrt(size(curData,1));
  81. l1 = shadedErrorBar(time,nanmean(curData,1),standError, 'lineprops', {'color', 'k','linewidth', 2}, 'patchSaturation', .1);
  82. curData = squeeze(nanmean(mergeddata(:,idx_chans,:,2),2));
  83. curData = curData-condAvg+repmat(nanmean(condAvg,1),size(condAvg,1),1);
  84. standError = nanstd(curData,1)./sqrt(size(curData,1));
  85. l2 = shadedErrorBar(time,nanmean(curData,1),standError, 'lineprops', {'color', 'r','linewidth', 2}, 'patchSaturation', .1);
  86. % ax = gca; ax.YDir = 'reverse';
  87. xlabel('Time (s) from stim onset')
  88. xlim([-.025 .16]); %ylim([-.03 .18])
  89. ylabel({'ERP';'(microVolts)'});
  90. xlabel({'Time (s)'});
  91. set(findall(gcf,'-property','FontSize'),'FontSize',12)
  92. %% ERP components for subjects 1-17 and 18-33 are shifted in time!
  93. h = figure('units','centimeters','position',[0 0 10 8]);
  94. cla; hold on;
  95. % new value = old value ? subject average + grand average
  96. curData = squeeze(nanmean(mergeddata(1:17,idx_chans,:,1),2));
  97. standError = nanstd(curData,1)./sqrt(size(curData,1));
  98. l1 = shadedErrorBar(time,nanmean(curData,1),standError, 'lineprops', {'color', 'k','linewidth', 2}, 'patchSaturation', .1);
  99. curData = squeeze(nanmean(mergeddata(18:end,idx_chans,:,1),2));
  100. standError = nanstd(curData,1)./sqrt(size(curData,1));
  101. l2 = shadedErrorBar(time,nanmean(curData,1),standError, 'lineprops', {'color', 'r','linewidth', 2}, 'patchSaturation', .1);
  102. % ax = gca; ax.YDir = 'reverse';
  103. xlabel('Time (s) from stim onset')
  104. xlim([-.025 .16]); %ylim([-.03 .18])
  105. ylabel({'ERP';'(microVolts)'});
  106. xlabel({'Time (s)'});
  107. set(findall(gcf,'-property','FontSize'),'FontSize',12)
  108. %% align individual subjects N1 to first negative peak
  109. % find individual minimum (avg. across conditions) between 40 and 120 ms
  110. time2search = find(time>0.04 & time <0.12);
  111. newtime = 0-100*(time(2)-time(1)):(time(2)-time(1)):0+100*(time(2)-time(1));
  112. for indID = 1:size(condAvg,1)
  113. %[peaks, locs] = findpeaks(condAvg1(indID,:));
  114. tmp = find(islocalmin(condAvg(indID,time2search), ...
  115. 'FlatSelection', 'center', ...
  116. 'MinSeparation', 25));
  117. minVal1(indID) = condAvg1(indID,time2search(tmp(1)));
  118. curmin = time2search(tmp(1));
  119. alignedN1_1(indID,:) = condAvg1(indID,curmin-100:curmin+100);
  120. minVal2(indID) = condAvg2(indID,time2search(tmp(1)));
  121. curmin = time2search(tmp(1));
  122. alignedN1_2(indID,:) = condAvg2(indID,curmin-100:curmin+100);
  123. alignedTopo(indID,:,:) = squeeze(nanmean(mergeddata(indID,:,curmin-100:curmin+100,1:2),4));
  124. end
  125. % alternatively: consider global minimum
  126. % for indID = 1:size(condAvg,1)
  127. % [~, minLoc1(indID)] = min(condAvg(indID,time2search));
  128. % curmin = time2search(minLoc1(indID));
  129. % minVal1(indID) = condAvg1(indID,curmin);
  130. % alignedN1_1(indID,:) = condAvg1(indID,curmin-100:curmin+100);
  131. % [~, minLoc2(indID)] = min(condAvg(indID,time2search));
  132. % curmin = time2search(minLoc2(indID));
  133. % minVal1(indID) = condAvg2(indID,curmin);
  134. % alignedN1_2(indID,:) = condAvg2(indID,curmin-100:curmin+100);
  135. % end
  136. mergeddata_aligned = cat(3, alignedN1_1, alignedN1_2);
  137. [h, p] = ttest(minVal1, minVal2)
  138. % avg across channels and conditions
  139. condAvg_al = squeeze(nanmean(mergeddata_aligned(:,:,1:2),3));
  140. % check that troughs are aligned
  141. % figure; imagesc(zscore(condAvg_al,[],2))
  142. % figure; imagesc(condAvg_al)
  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(mergeddata_aligned(:,:,1));
  147. curData = curData-condAvg_al+repmat(nanmean(condAvg_al,1),size(condAvg_al,1),1);
  148. standError = nanstd(curData,1)./sqrt(size(curData,1));
  149. l1 = shadedErrorBar(newtime*1000,nanmean(curData,1),standError, 'lineprops', {'color', 'k','linewidth', 2}, 'patchSaturation', .1);
  150. curData = squeeze(mergeddata_aligned(:,:,2));
  151. curData = curData-condAvg_al+repmat(nanmean(condAvg_al,1),size(condAvg_al,1),1);
  152. standError = nanstd(curData,1)./sqrt(size(curData,1));
  153. l2 = shadedErrorBar(newtime*1000,nanmean(curData,1),standError, 'lineprops', {'color', 'r','linewidth', 2}, 'patchSaturation', .1);
  154. % ax = gca; ax.YDir = 'reverse';
  155. legend({'manmade', 'natural'}, 'location', 'NorthWest'); legend('boxoff')
  156. xlabel('Time (s) from local minimum')
  157. xlim([-100 100]); %ylim([-.03 .18])
  158. ylabel({'ERP';'(microVolts)'});
  159. xlabel({'Time (ms) from local minimum'});
  160. set(findall(gcf,'-property','FontSize'),'FontSize',12)
  161. %% plot topography around detected trough
  162. h = figure('units','centimeters','position',[0 0 10 10]);
  163. set(gcf,'renderer','Painters')
  164. cfg = [];
  165. cfg.layout = 'EEG1010.lay';
  166. cfg.parameter = 'powspctrm';
  167. cfg.comment = 'no';
  168. cfg.colormap = cBrew;
  169. cfg.colorbar = 'EastOutside';
  170. plotData = [];
  171. plotData.label = elec.label(1:64); % {1 x N}
  172. plotData.dimord = 'chan';
  173. plotData.powspctrm = squeeze(nanmean(nanmean(alignedTopo(:, :, newtime>-.01 & newtime < .01),3),1))';
  174. [~, sortidx] = sort(plotData.powspctrm, 'ascend');
  175. idx_chans = sortidx(1);
  176. idx_chans_visual = idx_chans;
  177. cfg.marker = 'off';
  178. cfg.highlight = 'yes';
  179. cfg.highlightchannel = plotData.label(idx_chans);
  180. cfg.highlightcolor = [1 0 0];
  181. cfg.highlightsymbol = '.';
  182. cfg.highlightsize = 18;
  183. cfg.zlim = [-5 5]*10^-4;
  184. cfg.figure = h;
  185. ft_topoplotER(cfg,plotData);
  186. cb = colorbar('location', 'EastOutside'); set(get(cb,'ylabel'),'string','Amplitude');
  187. %% plot difference between conditions
  188. h = figure('units','centimeters','position',[0 0 10 10]);
  189. set(gcf,'renderer','Painters')
  190. cfg = [];
  191. cfg.layout = 'biosemi64.lay';
  192. cfg.parameter = 'powspctrm';
  193. cfg.comment = 'no';
  194. cfg.colormap = cBrew;
  195. cfg.colorbar = 'EastOutside';
  196. plotData = [];
  197. plotData.label = elec.label; % {1 x N}
  198. plotData.dimord = 'chan';
  199. plotData.powspctrm = squeeze(nanmean(nanmean(nanmean(mergeddata(:,:,time>0.08 & time <0.12,2),3),1),4))'...
  200. -squeeze(nanmean(nanmean(nanmean(mergeddata(:,:,time>0.08 & time <0.12,1),3),1),4))';
  201. [~, sortidx] = sort(plotData.powspctrm, 'ascend');
  202. idx_chans = sortidx(1:6);
  203. cfg.marker = 'off';
  204. cfg.highlight = 'yes';
  205. cfg.highlightchannel = plotData.label(idx_chans);
  206. cfg.highlightcolor = [1 0 0];
  207. cfg.highlightsymbol = '.';
  208. cfg.highlightsize = 18;
  209. %cfg.zlim = [-5 5]*10^-4;
  210. cfg.figure = h;
  211. ft_topoplotER(cfg,plotData);
  212. cb = colorbar('location', 'EastOutside'); set(get(cb,'ylabel'),'string','Amplitude');
  213. %% visualize for negative pls channels
  214. idx_chans = [28:30];
  215. condAvg = squeeze(nanmean(nanmean(mergeddata(:,idx_chans,:,1:2),2),4));
  216. h = figure('units','centimeters','position',[0 0 10 8]);
  217. cla; hold on;
  218. % new value = old value ? subject average + grand average
  219. curData = squeeze(nanmean(mergeddata(:,idx_chans,:,1),2));
  220. curData = curData-condAvg+repmat(nanmean(condAvg,1),size(condAvg,1),1);
  221. standError = nanstd(curData,1)./sqrt(size(curData,1));
  222. l1 = shadedErrorBar(time,nanmean(curData,1),standError, 'lineprops', {'color', 'k','linewidth', 2}, 'patchSaturation', .1);
  223. curData = squeeze(nanmean(mergeddata(:,idx_chans,:,2),2));
  224. curData = curData-condAvg+repmat(nanmean(condAvg,1),size(condAvg,1),1);
  225. standError = nanstd(curData,1)./sqrt(size(curData,1));
  226. l2 = shadedErrorBar(time,nanmean(curData,1),standError, 'lineprops', {'color', 'r','linewidth', 2}, 'patchSaturation', .1);
  227. xlabel('Time (s) from stim onset')
  228. xlim([-1 2]); %ylim([-.03 .18])
  229. ylabel({'ERP';'(microVolts)'});
  230. xlabel({'Time (s)'});
  231. set(findall(gcf,'-property','FontSize'),'FontSize',12)
  232. %% visualize for positive pls channels
  233. idx_chans = [21:23, 58:62];
  234. condAvg = squeeze(nanmean(nanmean(mergeddata(:,idx_chans,:,1:2),2),4));
  235. h = figure('units','centimeters','position',[0 0 10 8]);
  236. cla; hold on;
  237. % new value = old value ? subject average + grand average
  238. curData = squeeze(nanmean(mergeddata(:,idx_chans,:,1),2));
  239. curData = curData-condAvg+repmat(nanmean(condAvg,1),size(condAvg,1),1);
  240. standError = nanstd(curData,1)./sqrt(size(curData,1));
  241. l1 = shadedErrorBar(time,nanmean(curData,1),standError, 'lineprops', {'color', 'k','linewidth', 2}, 'patchSaturation', .1);
  242. curData = squeeze(nanmean(mergeddata(:,idx_chans,:,2),2));
  243. curData = curData-condAvg+repmat(nanmean(condAvg,1),size(condAvg,1),1);
  244. standError = nanstd(curData,1)./sqrt(size(curData,1));
  245. l2 = shadedErrorBar(time,nanmean(curData,1),standError, 'lineprops', {'color', 'r','linewidth', 2}, 'patchSaturation', .1);
  246. xlabel('Time (s) from stim onset')
  247. xlim([-1 2]); %ylim([-.03 .18])
  248. ylabel({'ERP';'(microVolts)'});
  249. xlabel({'Time (s)'});
  250. set(findall(gcf,'-property','FontSize'),'FontSize',12)