e1_taskPLS_memory_erp_LV1.m 6.8 KB

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