b4b_taskPLS_memory_erf_LV1.m 7.0 KB

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