d1_taskPLS_recognition_erp_LV1.m 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. % Create an overview plot featuring the results of the multivariate PLS
  2. % comparing spectral changes during the stimulus period under load
  3. clear all; cla; clc;
  4. currentFile = mfilename('fullpath');
  5. [pathstr,~,~] = fileparts(currentFile);
  6. cd(fullfile(pathstr,'..', '..'))
  7. rootpath = pwd;
  8. pn.data = fullfile(rootpath, 'data', 'test');
  9. pn.figures = fullfile(rootpath, 'figures');
  10. pn.tools = fullfile(rootpath, 'tools');
  11. addpath(genpath(fullfile(pn.tools, '[MEG]PLS', 'MEGPLS_PIPELINE_v2.02b')))
  12. addpath(fullfile(pn.tools, 'fieldtrip')); ft_defaults;
  13. addpath(genpath(fullfile(pn.tools, 'RainCloudPlots')));
  14. addpath(fullfile(pn.tools, 'BrewerMap'));
  15. addpath(fullfile(pn.tools, 'winsor'));
  16. % set custom colormap
  17. cBrew = brewermap(500,'RdBu');
  18. cBrew = flipud(cBrew);
  19. colormap(cBrew)
  20. load(fullfile(pn.data, 'd01_taskpls_erp.mat'),...
  21. 'stat', 'result', 'lvdat', 'lv_evt_list', 'num_chans', 'num_freqs', 'num_time')
  22. load(fullfile(rootpath, 'data','erp', ['sub-001_erp.mat']));
  23. elec = erp.scene_category{1}.elec;
  24. result.perm_result.sprob
  25. indLV = 1;
  26. lvdat = reshape(result.boot_result.compare_u(:,indLV), num_chans, num_freqs, num_time);
  27. stat.prob = lvdat;
  28. stat.mask = lvdat > 3 | lvdat < -3;
  29. % maskNaN = double(stat.mask);
  30. % maskNaN(maskNaN==0) = NaN;
  31. %% invert solution
  32. stat.mask = stat.mask;
  33. stat.prob = stat.prob.*-1;
  34. result.vsc = result.vsc.*-1;
  35. result.usc = result.usc.*-1;
  36. %% plot temporal loadings
  37. h = figure('units','centimeter','position',[0 0 10 10]);
  38. set(gcf,'renderer','Painters')
  39. hold on;
  40. % highlight relevant phase in background
  41. patches.timeVec = [.35 .55];
  42. patches.colorVec = [.8 .95 1];
  43. for indP = 1:size(patches.timeVec,2)-1
  44. YLim = [-.5 .5];
  45. p = patch([patches.timeVec(indP) patches.timeVec(indP+1) patches.timeVec(indP+1) patches.timeVec(indP)], ...
  46. [YLim(1) YLim(1) YLim(2), YLim(2)], patches.colorVec(indP,:));
  47. p.EdgeColor = 'none';
  48. end
  49. % highlight relevant phase in background
  50. patches.timeVec = [.6 .8];
  51. patches.colorVec = [1 .95 .8];
  52. for indP = 1:size(patches.timeVec,2)-1
  53. YLim = [-.5 .5];
  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. % highlight relevant phase in background
  59. patches.timeVec = [1.2 1.7];
  60. patches.colorVec = [1 .8 .7];
  61. for indP = 1:size(patches.timeVec,2)-1
  62. YLim = [-.5 .5];
  63. p = patch([patches.timeVec(indP) patches.timeVec(indP+1) patches.timeVec(indP+1) patches.timeVec(indP)], ...
  64. [YLim(1) YLim(1) YLim(2), YLim(2)], patches.colorVec(indP,:));
  65. p.EdgeColor = 'none';
  66. end
  67. statsPlot = [];
  68. statsPlot = cat(1, statsPlot,squeeze(nanmean(stat.mask(1:64,:,:).*stat.prob(1:64,:,:),1)));
  69. plot(stat.time,statsPlot, 'k')
  70. xlabel('Time [s from stim onset]'); ylabel('mean BSR');
  71. title({'ERP changes'; ['p = ', num2str(round(result.perm_result.sprob(indLV),4))]})
  72. set(findall(gcf,'-property','FontSize'),'FontSize',18)
  73. %% plot early positivity/negativity
  74. h = figure('units','centimeters','position',[0 0 10 10]);
  75. set(gcf,'renderer','Painters')
  76. cfg = [];
  77. cfg.layout = 'biosemi64.lay';
  78. cfg.parameter = 'powspctrm';
  79. cfg.comment = 'no';
  80. cfg.colormap = cBrew;
  81. cfg.colorbar = 'EastOutside';
  82. plotData = [];
  83. plotData.label = elec.label; % {1 x N}
  84. plotData.dimord = 'chan';
  85. cfg.zlim = [-4 4];
  86. cfg.figure = h;
  87. plotData.powspctrm = squeeze(nanmean(stat.mask(:,:,stat.time>.35 & stat.time<.55).*...
  88. stat.prob(:,:,stat.time>.35 & stat.time<.55),3));
  89. ft_topoplotER(cfg,plotData);
  90. cb = colorbar('location', 'EastOutside'); set(get(cb,'ylabel'),'string','Mean BSR');
  91. %% plot later positivity
  92. h = figure('units','centimeters','position',[0 0 10 10]);
  93. set(gcf,'renderer','Painters')
  94. cfg = [];
  95. cfg.layout = 'biosemi64.lay';
  96. cfg.parameter = 'powspctrm';
  97. cfg.comment = 'no';
  98. cfg.colormap = cBrew;
  99. cfg.colorbar = 'EastOutside';
  100. plotData = [];
  101. plotData.label = elec.label; % {1 x N}
  102. plotData.dimord = 'chan';
  103. cfg.zlim = [-5 5];
  104. cfg.figure = h;
  105. plotData.powspctrm = squeeze(nanmean(stat.mask(:,:,stat.time>.6 & stat.time<.8).*...
  106. stat.prob(:,:,stat.time>0.6 & stat.time<.8),3));
  107. ft_topoplotER(cfg,plotData);
  108. cb = colorbar('location', 'EastOutside'); set(get(cb,'ylabel'),'string','Mean BSR');
  109. %% plot late negativity
  110. h = figure('units','centimeters','position',[0 0 10 10]);
  111. set(gcf,'renderer','Painters')
  112. cfg = [];
  113. cfg.layout = 'biosemi64.lay';
  114. cfg.parameter = 'powspctrm';
  115. cfg.comment = 'no';
  116. cfg.colormap = cBrew;
  117. cfg.colorbar = 'EastOutside';
  118. plotData = [];
  119. plotData.label = elec.label; % {1 x N}
  120. plotData.dimord = 'chan';
  121. cfg.zlim = [-2 2];
  122. cfg.figure = h;
  123. plotData.powspctrm = squeeze(nanmean(stat.mask(:,:,stat.time>1.2 & stat.time<1.7).*...
  124. stat.prob(:,:,stat.time>1.2 & stat.time<1.7),3));
  125. ft_topoplotER(cfg,plotData);
  126. cb = colorbar('location', 'EastOutside'); set(get(cb,'ylabel'),'string','Mean BSR');
  127. %% plot using raincloud plot
  128. groupsizes=result.num_subj_lst;
  129. conditions=lv_evt_list;
  130. conds = {'hit'; 'miss'};
  131. condData = []; uData = [];
  132. for indGroup = 1
  133. if indGroup == 1
  134. relevantEntries = 1:groupsizes(1)*numel(conds);
  135. elseif indGroup == 2
  136. relevantEntries = groupsizes(1)*numel(conds)+1:...
  137. groupsizes(1)*numel(conds)+groupsizes(2)*numel(conds);
  138. end
  139. for indCond = 1:numel(conds)
  140. targetEntries = relevantEntries(conditions(relevantEntries)==indCond);
  141. condData{indGroup}(indCond,:) = result.vsc(targetEntries,indLV);
  142. uData{indGroup}(indCond,:) = result.usc(targetEntries,indLV);
  143. end
  144. end
  145. cBrew(1,:) = 2.*[.3 .1 .1];
  146. cBrew(2,:) = [.6 .6 .6];
  147. idx_outlier = cell(1); idx_standard = cell(1);
  148. for indGroup = 1
  149. dataToPlot = uData{indGroup}';
  150. % define outlier as lin. modulation that is more than three scaled median absolute deviations (MAD) away from the median
  151. X = [1 1; 1 2]; b=X\dataToPlot'; IndividualSlopes = b(2,:);
  152. outliers = isoutlier(IndividualSlopes, 'median');
  153. idx_outlier{indGroup} = find(outliers);
  154. idx_standard{indGroup} = find(outliers==0);
  155. end
  156. h = figure('units','centimeter','position',[0 0 10 10]);
  157. for indGroup = 1
  158. dataToPlot = uData{indGroup}';
  159. % read into cell array of the appropriate dimensions
  160. data = []; data_ws = [];
  161. for i = 1:2
  162. for j = 1:1
  163. data{i, j} = dataToPlot(:,i);
  164. % individually demean for within-subject visualization
  165. data_ws{i, j} = dataToPlot(:,i)-...
  166. nanmean(dataToPlot(:,:),2)+...
  167. repmat(nanmean(nanmean(dataToPlot(:,:),2),1),size(dataToPlot(:,:),1),1);
  168. data_nooutlier{i, j} = data{i, j};
  169. data_nooutlier{i, j}(idx_outlier{indGroup}) = [];
  170. data_ws_nooutlier{i, j} = data_ws{i, j};
  171. data_ws_nooutlier{i, j}(idx_outlier{indGroup}) = [];
  172. % sort outliers to back in original data for improved plot overlap
  173. data_ws{i, j} = [data_ws{i, j}(idx_standard{indGroup}); data_ws{i, j}(idx_outlier{indGroup})];
  174. end
  175. end
  176. % IMPORTANT: plot individually centered estimates, stats on uncentered estimates!
  177. set(gcf,'renderer','Painters')
  178. cla;
  179. cl = cBrew(indGroup,:);
  180. [~, dist] = rm_raincloud_fixedSpacing(data_ws, [.8 .8 .8],1);
  181. h_rc = rm_raincloud_fixedSpacing(data_ws_nooutlier, cl,1,[],[],[],dist);
  182. view([90 -90]);
  183. axis ij
  184. box(gca,'off')
  185. set(gca, 'YTickLabels', {conds{2}; conds{1}});
  186. yticks = get(gca, 'ytick'); ylim([yticks(1)-(yticks(2)-yticks(1))./2, yticks(2)+(yticks(2)-yticks(1))./1.5]);
  187. minmax = [min(min(cat(2,data_ws{:}))), max(max(cat(2,data_ws{:})))];
  188. xlim(minmax+[-0.2*diff(minmax), 0.2*diff(minmax)])
  189. ylabel('recognition'); xlabel({'Brainscore'; '[Individually centered]'})
  190. % test linear effect
  191. curData = [data_nooutlier{1, 1}, data_nooutlier{2, 1}];
  192. X = [1 1; 1 2]; b=X\curData'; IndividualSlopes = b(2,:);
  193. [~, p, ci, stats] = ttest(IndividualSlopes);
  194. title(['M:', num2str(round(mean(IndividualSlopes),3)), '; p=', num2str(round(p,3))])
  195. end