b3a_taskPLS_recognition_erp_LV1.m 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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', 'stats');
  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. h = figure('units','centimeter','position',[0 0 15 10]);
  37. set(gcf,'renderer','Painters')
  38. hold on;
  39. statsPlot = [];
  40. statsPlot = cat(1, statsPlot,squeeze(nanmean(stat.mask(1:64,:,:).*stat.prob(1:64,:,:),1)));
  41. plot(stat.time,statsPlot, 'k')
  42. xlabel('Time [s from stim onset]'); ylabel('max abs BSR');
  43. title({'ERP changes'; ['p = ', num2str(round(result.perm_result.sprob(indLV),4))]})
  44. set(findall(gcf,'-property','FontSize'),'FontSize',18)
  45. % figureName = ['b01_pls_traces'];
  46. % saveas(h, fullfile(pn.figures, figureName), 'epsc');
  47. % saveas(h, fullfile(pn.figures, figureName), 'png');
  48. %
  49. % h = figure('units','centimeter','position',[0 0 15 10]);
  50. % set(gcf,'renderer','Painters')
  51. % statsPlot = [];
  52. % statsPlot = cat(1, statsPlot,squeeze(mean(stat.prob(1:64,:,:),1)));
  53. % plot(stat.time,statsPlot, 'k')
  54. % xlabel('Time [s from stim onset]'); ylabel('mean BSR');
  55. % title({'ERP changes'; ['p = ', num2str(round(result.perm_result.sprob(indLV),4))]})
  56. % set(findall(gcf,'-property','FontSize'),'FontSize',18)
  57. %xlim([0 0.3])
  58. % figureName = ['b01_pls_traces'];
  59. % saveas(h, fullfile(pn.figures, figureName), 'epsc');
  60. % saveas(h, fullfile(pn.figures, figureName), 'png');
  61. %% plot multivariate topographies
  62. h = figure('units','centimeters','position',[0 0 10 10]);
  63. set(gcf,'renderer','Painters')
  64. cfg = [];
  65. cfg.layout = 'biosemi64.lay';
  66. cfg.parameter = 'powspctrm';
  67. cfg.comment = 'no';
  68. cfg.colormap = cBrew;
  69. cfg.colorbar = 'EastOutside';
  70. plotData = [];
  71. plotData.label = elec.label; % {1 x N}
  72. plotData.dimord = 'chan';
  73. cfg.zlim = [-5 5];
  74. cfg.figure = h;
  75. plotData.powspctrm = squeeze(nanmean(stat.mask(:,:,stat.time>.5 & stat.time<.8).*...
  76. stat.prob(:,:,stat.time>0.5 & stat.time<.8),3));
  77. ft_topoplotER(cfg,plotData);
  78. cb = colorbar('location', 'EastOutside'); set(get(cb,'ylabel'),'string','Mean BSR');
  79. %%
  80. h = figure('units','centimeters','position',[0 0 10 10]);
  81. set(gcf,'renderer','Painters')
  82. cfg = [];
  83. cfg.layout = 'biosemi64.lay';
  84. cfg.parameter = 'powspctrm';
  85. cfg.comment = 'no';
  86. cfg.colormap = cBrew;
  87. cfg.colorbar = 'EastOutside';
  88. plotData = [];
  89. plotData.label = elec.label; % {1 x N}
  90. plotData.dimord = 'chan';
  91. cfg.zlim = [-2 2];
  92. cfg.figure = h;
  93. plotData.powspctrm = squeeze(nanmean(stat.mask(:,:,stat.time>1.2 & stat.time<1.8).*...
  94. stat.prob(:,:,stat.time>1.2 & stat.time<1.8),3));
  95. ft_topoplotER(cfg,plotData);
  96. cb = colorbar('location', 'EastOutside'); set(get(cb,'ylabel'),'string','Mean BSR');
  97. %% plot using raincloud plot
  98. groupsizes=result.num_subj_lst;
  99. conditions=lv_evt_list;
  100. conds = {'hit'; 'miss'};
  101. condData = []; uData = [];
  102. for indGroup = 1
  103. if indGroup == 1
  104. relevantEntries = 1:groupsizes(1)*numel(conds);
  105. elseif indGroup == 2
  106. relevantEntries = groupsizes(1)*numel(conds)+1:...
  107. groupsizes(1)*numel(conds)+groupsizes(2)*numel(conds);
  108. end
  109. for indCond = 1:numel(conds)
  110. targetEntries = relevantEntries(conditions(relevantEntries)==indCond);
  111. condData{indGroup}(indCond,:) = -1.*result.vsc(targetEntries,indLV);
  112. uData{indGroup}(indCond,:) = -1.*result.usc(targetEntries,indLV);
  113. end
  114. end
  115. cBrew(1,:) = 2.*[.3 .1 .1];
  116. cBrew(2,:) = [.6 .6 .6];
  117. idx_outlier = cell(1); idx_standard = cell(1);
  118. for indGroup = 1
  119. dataToPlot = uData{indGroup}';
  120. % define outlier as lin. modulation that is more than three scaled median absolute deviations (MAD) away from the median
  121. X = [1 1; 1 2]; b=X\dataToPlot'; IndividualSlopes = b(2,:);
  122. outliers = isoutlier(IndividualSlopes, 'median');
  123. idx_outlier{indGroup} = find(outliers);
  124. idx_standard{indGroup} = find(outliers==0);
  125. end
  126. h = figure('units','centimeter','position',[0 0 25 10]);
  127. for indGroup = 1
  128. dataToPlot = uData{indGroup}';
  129. % read into cell array of the appropriate dimensions
  130. data = []; data_ws = [];
  131. for i = 1:2
  132. for j = 1:1
  133. data{i, j} = dataToPlot(:,i);
  134. % individually demean for within-subject visualization
  135. data_ws{i, j} = dataToPlot(:,i)-...
  136. nanmean(dataToPlot(:,:),2)+...
  137. repmat(nanmean(nanmean(dataToPlot(:,:),2),1),size(dataToPlot(:,:),1),1);
  138. data_nooutlier{i, j} = data{i, j};
  139. data_nooutlier{i, j}(idx_outlier{indGroup}) = [];
  140. data_ws_nooutlier{i, j} = data_ws{i, j};
  141. data_ws_nooutlier{i, j}(idx_outlier{indGroup}) = [];
  142. % sort outliers to back in original data for improved plot overlap
  143. data_ws{i, j} = [data_ws{i, j}(idx_standard{indGroup}); data_ws{i, j}(idx_outlier{indGroup})];
  144. end
  145. end
  146. % IMPORTANT: plot individually centered estimates, stats on uncentered estimates!
  147. subplot(1,2,indGroup);
  148. set(gcf,'renderer','Painters')
  149. cla;
  150. cl = cBrew(indGroup,:);
  151. [~, dist] = rm_raincloud_fixedSpacing(data_ws, [.8 .8 .8],1);
  152. h_rc = rm_raincloud_fixedSpacing(data_ws_nooutlier, cl,1,[],[],[],dist);
  153. view([90 -90]);
  154. axis ij
  155. box(gca,'off')
  156. set(gca, 'YTickLabels', {conds{2}; conds{1}});
  157. yticks = get(gca, 'ytick'); ylim([yticks(1)-(yticks(2)-yticks(1))./2, yticks(2)+(yticks(2)-yticks(1))./1.5]);
  158. minmax = [min(min(cat(2,data_ws{:}))), max(max(cat(2,data_ws{:})))];
  159. xlim(minmax+[-0.2*diff(minmax), 0.2*diff(minmax)])
  160. ylabel('scene category'); xlabel({'Brainscore'; '[Individually centered]'})
  161. % test linear effect
  162. curData = [data_nooutlier{1, 1}, data_nooutlier{2, 1}];
  163. X = [1 1; 1 2]; b=X\curData'; IndividualSlopes = b(2,:);
  164. [~, p, ci, stats] = ttest(IndividualSlopes);
  165. title(['M:', num2str(round(mean(IndividualSlopes),3)), '; p=', num2str(round(p,3))])
  166. end