a7_prep_data_for_analysis.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. function a7_prep_data_for_analysis(id, rootpath)
  2. if ismac % run if function is not pre-compiled
  3. id = '11'; % 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. % cache original number of trials
  36. Ntrial = numel(data.trial);
  37. %% define parameter
  38. % ICs to reject
  39. iclabels = config.ica1.iclabels.manual;
  40. ics2reject = [iclabels.bli(:); iclabels.mov(:); iclabels.hrt(:); iclabels.ref(:); iclabels.art(:); iclabels.emg(:)];
  41. clear iclabels
  42. % trials to remove
  43. trls2remove = config.ArtDect.trials;
  44. % channels to interpolate
  45. chns2interp = config.ArtDect.channels;
  46. %% ICA (from weights)
  47. % ica config
  48. cfg.method = 'runica';
  49. cfg.channel = {'all'};
  50. cfg.trials = 'all';
  51. cfg.numcomponent = 'all';
  52. cfg.demean = 'yes';
  53. cfg.runica.extended = 1;
  54. % ICA solution
  55. cfg.unmixing = config.ica1.unmixing;
  56. cfg.topolabel = config.ica1.topolabel;
  57. % components
  58. comp = ft_componentanalysis(cfg,data);
  59. % clear cfg
  60. clear cfg data
  61. %% remove components
  62. % cfg for rejecting components (reject: blinks, eye movements, ecg, ref)
  63. cfg.component = sortrows(ics2reject)';
  64. cfg.demean = 'yes';
  65. % reject components
  66. data = ft_rejectcomponent(cfg,comp);
  67. % clear cfg
  68. clear cfg comp ics2reject
  69. %% remove trials
  70. % define trials to keep
  71. trials = 1:Ntrial;
  72. trials(trls2remove) = [];
  73. % config for deleting trials
  74. cfg.trials = trials;
  75. % remove trials
  76. data = ft_preprocessing(cfg,data);
  77. nartfree_trials = trials;
  78. config.nartfree_trials = nartfree_trials;
  79. % clear variables
  80. clear cfg trials trls2remove
  81. %% remove eye & reference channels (before interpolation)
  82. cfg.channel = {'all','-IO1','-IO2','-M1', '-M2','-VEOG','-HEOG'};
  83. cfg.demean = 'yes';
  84. % remove channels
  85. data = ft_preprocessing(cfg,data);
  86. % clear cfg
  87. clear cfg
  88. if ~isempty(chns2interp)
  89. % interpolation cfg
  90. cfg = [];
  91. cfg.method = 'spline';
  92. cfg.badchannel = data.label(chns2interp);
  93. cfg.trials = 'all';
  94. cfg.lambda = 1e-5;
  95. cfg.order = 4;
  96. cfg.elec = config.elec;
  97. % interpolate
  98. data = ft_channelrepair(cfg,data);
  99. % clear cfg
  100. clear cfg chns2interp
  101. end
  102. %% keep channel x trial artifacts
  103. data.ArtDect.channels_x_trials = config.ArtDect.channels_x_trials;
  104. %% create overview of percentage of excluded trials, update config.trl
  105. tmp_trialNumbers(1,1) = length(data.trial);
  106. tmp_trialNumbers(2,1) = 1200; % maximum amount of trials
  107. tmp_trialNumbers(3,1) = (100-((tmp_trialNumbers(1,1)/tmp_trialNumbers(2,1))*100)); % percentage of excluded trials
  108. config.overview_trial_numbers = tmp_trialNumbers;
  109. %% update config.trl with trigger codes
  110. % add info whether trial is included in final set; 1 = trial containing no artifact
  111. config.trl(1:Ntrial,4) = 0;
  112. config.trl(nartfree_trials, 4) = 1;
  113. % add trial number
  114. config.trl(1:Ntrial,5) = 0;
  115. config.trl(nartfree_trials, 5) = nartfree_trials;
  116. % load marker file
  117. mrk = config.mrk;
  118. for i = 1:size(mrk,2)
  119. mrk_val{i,1} = mrk(1,i).value;
  120. end
  121. % save config
  122. save(fullfile(pn.history, [IDs{id}, '_task-xxxx_config.mat']),'config');
  123. %% update event table
  124. % remove trials that were not retained during preprocessing
  125. events(config.trl(:,5)==0,:) = [];
  126. %% save data
  127. data_eeg = data; clear data;
  128. save(fullfile(pn.eeg_ft, [IDs{id}, '_task-xxxx_eeg_art.mat']),'data_eeg', 'events');