c2_taskPLS_novelty_frontal_theta_plotLV1.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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, 'c2_taskpls_theta.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 multivariate topographies
  37. h = figure('units','centimeters','position',[0 0 10 10]);
  38. set(gcf,'renderer','Painters')
  39. cfg = [];
  40. cfg.layout = 'biosemi64.lay';
  41. cfg.parameter = 'powspctrm';
  42. cfg.comment = 'no';
  43. cfg.colormap = cBrew;
  44. cfg.colorbar = 'EastOutside';
  45. plotData = [];
  46. plotData.label = elec.label; % {1 x N}
  47. plotData.dimord = 'chan';
  48. cfg.zlim = [-3 3];
  49. cfg.figure = h;
  50. plotData.powspctrm = squeeze(nanmean(nanmean(stat.mask(:,:,:).*...
  51. stat.prob(:,:,:),3),2));
  52. [~, sortind] = sort(plotData.powspctrm, 'descend');
  53. cfg.marker = 'off';
  54. cfg.highlight = 'yes';
  55. cfg.highlightchannel = plotData.label(sortind(1:3)); %33, 4, 38
  56. cfg.highlightcolor = [0 0 0];
  57. cfg.highlightsymbol = '.';
  58. cfg.highlightsize = 15;
  59. ft_topoplotER(cfg,plotData);
  60. cb = colorbar('location', 'EastOutside'); set(get(cb,'ylabel'),'string','Max BSR');
  61. figureName = ['c_theta_topo'];
  62. saveas(h, fullfile(pn.figures, figureName), 'epsc');
  63. saveas(h, fullfile(pn.figures, figureName), 'png');
  64. %% plot using raincloud plot
  65. groupsizes=result.num_subj_lst;
  66. conditions=lv_evt_list;
  67. conds = {'old'; 'new'};
  68. condData = []; uData = [];
  69. for indGroup = 1
  70. if indGroup == 1
  71. relevantEntries = 1:groupsizes(1)*numel(conds);
  72. elseif indGroup == 2
  73. relevantEntries = groupsizes(1)*numel(conds)+1:...
  74. groupsizes(1)*numel(conds)+groupsizes(2)*numel(conds);
  75. end
  76. for indCond = 1:numel(conds)
  77. targetEntries = relevantEntries(conditions(relevantEntries)==indCond);
  78. condData{indGroup}(indCond,:) = result.vsc(targetEntries,indLV);
  79. uData{indGroup}(indCond,:) = result.usc(targetEntries,indLV);
  80. end
  81. end
  82. cBrew(1,:) = 2.*[.3 .1 .1];
  83. idx_outlier = cell(1); idx_standard = cell(1);
  84. for indGroup = 1
  85. dataToPlot = uData{indGroup}';
  86. % define outlier as lin. modulation that is more than three scaled median absolute deviations (MAD) away from the median
  87. X = [1 1; 1 2]; b=X\dataToPlot'; IndividualSlopes = b(2,:);
  88. outliers = isoutlier(IndividualSlopes, 'median');
  89. idx_outlier{indGroup} = find(outliers);
  90. idx_standard{indGroup} = find(outliers==0);
  91. end
  92. h = figure('units','centimeter','position',[0 0 12 10]);
  93. ax = subplot(1,1,1);
  94. for indGroup = 1
  95. dataToPlot = uData{indGroup}';
  96. % read into cell array of the appropriate dimensions
  97. data = []; data_ws = [];
  98. for i = 1:2
  99. for j = 1:1
  100. data{i, j} = dataToPlot(:,i);
  101. % individually demean for within-subject visualization
  102. data_ws{i, j} = dataToPlot(:,i)-...
  103. nanmean(dataToPlot(:,:),2)+...
  104. repmat(nanmean(nanmean(dataToPlot(:,:),2),1),size(dataToPlot(:,:),1),1);
  105. data_nooutlier{i, j} = data{i, j};
  106. data_nooutlier{i, j}(idx_outlier{indGroup}) = [];
  107. data_ws_nooutlier{i, j} = data_ws{i, j};
  108. data_ws_nooutlier{i, j}(idx_outlier{indGroup}) = [];
  109. % sort outliers to back in original data for improved plot overlap
  110. data_ws{i, j} = [data_ws{i, j}(idx_standard{indGroup}); data_ws{i, j}(idx_outlier{indGroup})];
  111. end
  112. end
  113. % IMPORTANT: plot individually centered estimates, stats on uncentered estimates!
  114. set(gcf,'renderer','Painters')
  115. cla;
  116. cl = cBrew(indGroup,:);
  117. [~, dist] = rm_raincloud_fixedSpacing(data_ws, [.8 .8 .8],1);
  118. h_rc = rm_raincloud_fixedSpacing(data_ws_nooutlier, cl,1,[],[],[],dist);
  119. view([90 -90]);
  120. axis ij
  121. box(gca,'off')
  122. set(gca, 'YTickLabels', {conds{2}; conds{1}});
  123. yticks = get(gca, 'ytick'); ylim([yticks(1)-(yticks(2)-yticks(1))./2, yticks(2)+(yticks(2)-yticks(1))./1.5]);
  124. minmax = [min(min(cat(2,data_ws{:}))), max(max(cat(2,data_ws{:})))];
  125. xlim(minmax+[-0.2*diff(minmax), 0.2*diff(minmax)])
  126. ylabel('novelty'); xlabel({'Brainscore'; '[Individually centered]'})
  127. % test linear effect
  128. curData = [data_nooutlier{1, 1}, data_nooutlier{2, 1}];
  129. X = [1 1; 1 2]; b=X\curData'; IndividualSlopes = b(2,:);
  130. [~, p, ci, stats] = ttest(IndividualSlopes);
  131. title(['M:', num2str(round(mean(IndividualSlopes),3)), '; p=', num2str(round(p,3))])
  132. end
  133. % change tick order
  134. ax.XDir = 'reverse';
  135. set(ax, 'YTickLabels', {conds{1}; conds{2}});
  136. set(findall(gcf,'-property','FontSize'),'FontSize',12)
  137. figureName = ['c_theta_rcp'];
  138. saveas(h, fullfile(pn.figures, figureName), 'epsc');
  139. saveas(h, fullfile(pn.figures, figureName), 'png');