a7_prep_data_for_analysis.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. function a7_prep_data_for_analysis(id, rootpath)
  2. if ismac % run if function is not pre-compiled
  3. id = '1'; % test for example subject
  4. currentFile = mfilename('fullpath');
  5. [pathstr,~,~] = fileparts(currentFile);
  6. cd(fullfile(pathstr,'..', '..'))
  7. rootpath = pwd;
  8. end
  9. % inputs
  10. pn.eeg_BIDS = fullfile(rootpath, 'data', 'inputs', 'rawdata', 'eeg_BIDS');
  11. pn.channel_locations = fullfile(rootpath, 'data', 'inputs', 'rawdata', 'channel_locations');
  12. pn.events = fullfile(rootpath, 'data', 'inputs', 'rawdata', 'events');
  13. pn.tools = fullfile(rootpath, 'tools');
  14. % outputs
  15. pn.eeg_ft = fullfile(rootpath, 'data', 'outputs', 'eeg');
  16. pn.history = fullfile(rootpath, 'data', 'outputs', 'history');
  17. if ismac % run if function is not pre-compiled
  18. addpath(fullfile(pn.tools, 'fieldtrip')); ft_defaults;
  19. addpath(fullfile(pn.tools, 'helpers'));
  20. end
  21. % N = 33
  22. IDs = tdfread(fullfile(pn.eeg_BIDS, 'participants.tsv'));
  23. IDs = cellstr(IDs.participant_id);
  24. id = str2num(id);
  25. display(['processing ID ' num2str(IDs{id})]);
  26. %% load files
  27. % load data
  28. load(fullfile(pn.eeg_ft, [IDs{id}, '_task-xxxx_eeg_seg.mat']),'data_eeg', 'events');
  29. data_ica = load(fullfile(pn.eeg_ft, [IDs{id}, '_task-xxxx_eeg_ica.mat']),'data');
  30. data = data_eeg; clear data_eeg;
  31. data.elec = data_ica.data.elec;
  32. data.chanlocs = data_ica.data.chanlocs; clear data_ica;
  33. % load config
  34. load(fullfile(pn.history, [IDs{id}, '_task-xxxx_config.mat']),'config');
  35. %% define parameter
  36. % ICs to reject
  37. iclabels = config.ica1.iclabels.manual;
  38. ics2reject = [iclabels.bli(:); iclabels.mov(:); iclabels.hrt(:); iclabels.ref(:); iclabels.art(:); iclabels.emg(:)];
  39. clear iclabels
  40. % trials to remove
  41. trls2remove = config.ArtDect.trials;
  42. % channels to interpolate
  43. chns2interp = config.ArtDect.channels;
  44. %% ICA (from weights)
  45. % ica config
  46. cfg.method = 'runica';
  47. cfg.channel = {'all'};
  48. cfg.trials = 'all';
  49. cfg.numcomponent = 'all';
  50. cfg.demean = 'yes';
  51. cfg.runica.extended = 1;
  52. % ICA solution
  53. cfg.unmixing = config.ica1.unmixing;
  54. cfg.topolabel = config.ica1.topolabel;
  55. % components
  56. comp = ft_componentanalysis(cfg,data);
  57. % clear cfg
  58. clear cfg data
  59. %% remove components
  60. % cfg for rejecting components (reject: blinks, eye movements, ecg, ref)
  61. cfg.component = sortrows(ics2reject)';
  62. cfg.demean = 'yes';
  63. % reject components
  64. data = ft_rejectcomponent(cfg,comp);
  65. % clear cfg
  66. clear cfg comp ics2reject
  67. %% remove trials
  68. % define trials to keep
  69. trials = 1:length(data.trial);
  70. trials(trls2remove) = [];
  71. % config for deleting trials
  72. cfg.trials = trials;
  73. % remove trials
  74. data = ft_preprocessing(cfg,data);
  75. nartfree_trials = trials;
  76. config.nartfree_trials = nartfree_trials;
  77. % clear variables
  78. clear cfg trials trls2remove
  79. %% remove eye & reference channels (before interpolation)
  80. cfg.channel = {'all','-IO1','-IO2','-M1', '-M2','-VEOG','-HEOG'};
  81. cfg.demean = 'yes';
  82. % remove channels
  83. data = ft_preprocessing(cfg,data);
  84. % clear cfg
  85. clear cfg
  86. % interpolation cfg
  87. cfg = [];
  88. cfg.method = 'spline';
  89. cfg.badchannel = data.label(chns2interp);
  90. cfg.trials = 'all';
  91. cfg.lambda = 1e-5;
  92. cfg.order = 4;
  93. cfg.elec = config.elec;
  94. % interpolate
  95. data = ft_channelrepair(cfg,data);
  96. % clear cfg
  97. clear cfg chns2interp
  98. %% keep channel x trial artifacts
  99. data.ArtDect.channels_x_trials = config.ArtDect.channels_x_trials;
  100. %% create overview of percentage of excluded trials, update config.trl
  101. tmp_trialNumbers(1,1) = length(data.trial);
  102. tmp_trialNumbers(2,1) = 1200; % maximum amount of trials
  103. tmp_trialNumbers(3,1) = (100-((tmp_trialNumbers(1,1)/tmp_trialNumbers(2,1))*100)); % percentage of excluded trials
  104. config.overview_trial_numbers = tmp_trialNumbers;
  105. %% update config.trl with trigger codes
  106. % add info whether trial is included in final set; 1 = trial containing no artifact
  107. config.trl(:,4) = 0;
  108. config.trl(nartfree_trials, 4) = 1;
  109. % add trial number
  110. config.trl(:,5) = 0;
  111. config.trl(nartfree_trials, 5) = nartfree_trials;
  112. % load marker file
  113. mrk = config.mrk;
  114. for i = 1:size(mrk,2)
  115. mrk_val{i,1} = mrk(1,i).value;
  116. end
  117. % save config
  118. save(fullfile(pn.history, [IDs{id}, '_task-xxxx_config.mat']),'config');
  119. %% update event table
  120. % remove trials that were not retained during preprocessing
  121. events(config.trl(:,5)==0,:) = [];
  122. %% save data
  123. data_eeg = data; clear data;
  124. save(fullfile(pn.eeg_ft, [IDs{id}, '_task-xxxx_eeg_art.mat']),'data_eeg', 'events');