c2_taskPLS_novelty_frontal_theta.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. % Set up EEG PLS for a task PLS using spectral power
  2. clear all; cla; clc;
  3. currentFile = mfilename('fullpath');
  4. [pathstr,~,~] = fileparts(currentFile);
  5. cd(fullfile(pathstr,'..'))
  6. rootpath = pwd;
  7. pn.data_eeg = fullfile(rootpath, '..', 'eegmp_preproc', 'data', 'outputs', 'eeg');
  8. pn.data_erp = fullfile(rootpath, 'data', 'erp');
  9. pn.data_erf = fullfile(rootpath, 'data', 'erf');
  10. pn.data = fullfile(rootpath, 'data', 'stats'); mkdir(pn.data);
  11. pn.tools = fullfile(rootpath, 'tools');
  12. addpath(fullfile(rootpath, '..', 'eegmp_preproc', 'tools', 'fieldtrip')); ft_defaults
  13. addpath(genpath(fullfile(pn.tools, '[MEG]PLS', 'MEGPLS_PIPELINE_v2.02b')))
  14. addpath(fullfile(pn.tools, 'BrewerMap'));
  15. addpath(fullfile(pn.tools, 'shadedErrorBar'));
  16. %% add seed for reproducibility
  17. rng(0, 'twister');
  18. %% load erp
  19. for ind_id = 1:33
  20. id = sprintf('sub-%03d', ind_id); disp(id)
  21. load(fullfile(pn.data_erf, [id,'_erf.mat']));
  22. for ind_option = 1:numel(conds.old)
  23. if ind_id == 1
  24. erpgroup.old.(conds.old{ind_option}) = erf.old{ind_option};
  25. erpgroup.old.(conds.old{ind_option}) = ...
  26. rmfield(erpgroup.old.(conds.old{ind_option}), {'powspctrm'});
  27. erpgroup.old.(conds.old{ind_option}).dimord = 'sub_chan_freq_time';
  28. freq = erpgroup.old.(conds.old{ind_option}).freq;
  29. idx_freq = freq >2 & freq <8;
  30. end
  31. % only include time points from 300 - 500 ms
  32. time = erf.old{ind_option}.time;
  33. toi = time >0.3 & time <0.5;
  34. erpgroup.old.(conds.old{ind_option}).avg(ind_id,:,:,:) = erf.old{ind_option}.powspctrm(:,idx_freq,toi);
  35. end
  36. end
  37. time = erpgroup.old.old.time(toi);
  38. freq = erpgroup.old.old.freq(idx_freq);
  39. channels = erpgroup.old.old.label;
  40. mergeddata = cat(5, erpgroup.old.old.avg, erpgroup.old.new.avg);
  41. % identify frontal channels, and set everything else to zero
  42. idx_chans = startsWith(channels, 'F') | startsWith(channels, 'A') | startsWith(channels, 'C'); % & ~startsWith(channels, 'CP');
  43. mergeddata(:, idx_chans==0,:,:,:) = 0;
  44. num_chans = numel(channels);
  45. num_freqs = numel(freq);
  46. num_time = numel(time);
  47. num_subj_lst = [33];
  48. num_cond = 2;
  49. num_grp = 1;
  50. datamat_lst = cell(num_grp); lv_evt_list = [];
  51. indCount_cont = 1;
  52. indCount = 1;
  53. indGroup = 1;
  54. for indCond = 1:num_cond
  55. for indID = 1:num_subj_lst(indGroup)
  56. datamat_lst{indGroup}(indCount,:) = reshape(squeeze(mergeddata(indID,:,:,:,indCond)), [], 1);
  57. lv_evt_list(indCount_cont) = indCond;
  58. indCount = indCount+1;
  59. indCount_cont = indCount_cont+1;
  60. end
  61. end
  62. datamat_lst{indGroup}(isnan(datamat_lst{indGroup})) = 0;
  63. %% set PLS options and run PLS
  64. option = [];
  65. option.method = 1; % [1] | 2 | 3 | 4 | 5 | 6
  66. option.num_perm = 1000; %( single non-negative integer )
  67. option.num_split = 0; %( single non-negative integer )
  68. option.num_boot = 1000; % ( single non-negative integer )
  69. option.cormode = 0; % [0] | 2 | 4 | 6
  70. option.meancentering_type = 0;% [0] | 1 | 2 | 3
  71. option.boot_type = 'strat'; %['strat'] | 'nonstrat'
  72. result = pls_analysis(datamat_lst, num_subj_lst, num_cond, option);
  73. %% rearrange into fieldtrip structure
  74. lvdat = reshape(result.boot_result.compare_u(:,1), num_chans, num_freqs, num_time);
  75. %udat = reshape(result.u, num_chans, num_freqs, num_time);
  76. stat = [];
  77. stat.prob = lvdat;
  78. stat.dimord = 'chan_freq_time';
  79. stat.clusters = [];
  80. stat.clusters.prob = result.perm_result.sprob; % check for significance of LV
  81. stat.mask = lvdat > 3 | lvdat < -3;
  82. stat.cfg = option;
  83. stat.freq = freq;
  84. stat.time = time;
  85. save(fullfile(pn.data, 'c2_taskpls_theta.mat'),...
  86. 'stat', 'result', 'lvdat', 'lv_evt_list', 'num_chans', 'num_freqs', 'num_time')