a7_prep_data_for_analysis.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. %% I_SS_prep_data_for_analysis_20170922
  2. % 180124 | now also exclude ART and EMG artifacts
  3. % 180327 | include OAs, deal wih dropped trials of subjects with missing onsets
  4. % | interpolate data to requested times vs. choosing shortest
  5. % duration (zero interpolation)
  6. % This script fixes issues with missing onsets in the following way:
  7. % IMPORTANT: For 2160, the first two trials were not available,
  8. % hence we have to add +2 to the index to identify the correct
  9. % behavioral trials that belong to the EEG data. For 2203 the
  10. % first trial is missing. The third and second trial should
  11. % also be removed, regardless of whether they are indicated to
  12. % be dropped due to automatic correction.
  13. %% initialize
  14. restoredefaultpath;
  15. clear all; close all; pack; clc;
  16. %% pathdef
  17. if ismac
  18. pn.study = '/Volumes/LNDG/Projects/StateSwitch/';
  19. pn.eeg_root = [pn.study, 'dynamic/data/eeg/task/A_preproc/SA_preproc_study/'];
  20. pn.EEG = [pn.eeg_root, 'B_data/C_EEG_FT/']; %mkdir(pn.EEG);
  21. pn.History = [pn.eeg_root, 'B_data/D_History/']; %mkdir(pn.History);
  22. % add ConMemEEG tools
  23. pn.MWBtools = [pn.eeg_root, 'T_tools/fnct_MWB/']; addpath(genpath(pn.MWBtools));
  24. pn.THGtools = [pn.eeg_root, 'T_tools/fnct_THG/']; addpath(genpath(pn.THGtools));
  25. pn.commontools = [pn.eeg_root, 'T_tools/fnct_common/']; addpath(genpath(pn.commontools));
  26. pn.fnct_JQK = [pn.eeg_root, 'T_tools/fnct_JQK/']; addpath(genpath(pn.fnct_JQK));
  27. pn.FT = [pn.eeg_root, 'T_tools/fieldtrip-20170904/']; addpath(pn.FT); ft_defaults;
  28. pn.helper = [pn.eeg_root, 'A_scripts/helper/']; addpath(pn.helper);
  29. else
  30. pn.root = '/home/mpib/LNDG/StateSwitch/WIP_eeg/SA_preproc_study/';
  31. pn.EEG = [pn.root, 'B_data/C_EEG_FT/'];
  32. pn.History = [pn.root, 'B_data/D_History/'];
  33. end
  34. %% define Condition & IDs for preprocessing
  35. condEEG = 'dynamic';
  36. %% define IDs for segmentation
  37. % N = 47 YAs + 53 OAs;
  38. IDs = {'1117';'1118';'1120';'1124';'1126';'1131';'1132';'1135';'1136';'1138';...
  39. '1144';'1151';'1158';'1160';'1163';'1164';'1167';'1169';'1172';'1173';...
  40. '1178';'1182';'1215';'1216';'1219';'1221';'1223';'1227';'1228';'1233';...
  41. '1234';'1237';'1239';'1240';'1243';'1245';'1247';'1250';'1252';'1257';...
  42. '1261';'1265';'1266';'1268';'1270';'1276';'1281';...
  43. '2104';'2107';'2108';'2112';'2118';'2120';'2121';'2123';'2125';'2129';...
  44. '2130';'2131';'2132';'2133';'2134';'2135';'2139';'2140';'2145';'2147';...
  45. '2149';'2157';'2160';'2201';'2202';'2203';'2205';'2206';'2209';'2210';...
  46. '2211';'2213';'2214';'2215';'2216';'2217';'2219';'2222';'2224';'2226';...
  47. '2227';'2236';'2237';'2238';'2241';'2244';'2246';'2248';'2250';'2251';...
  48. '2252';'2258';'2261'};
  49. %% loop IDs
  50. setting.plot = 0; % plot imagesc of resulting timeseries
  51. % prior to loop create matrixes for overviews over artifact-free trials and total trial numbers
  52. overview_nartfree_trials = {};
  53. overview_trial_numbers = {};
  54. ID_unavailable = cell(length(IDs),4);
  55. for id = 1:length(IDs)
  56. for iRun = 1:4
  57. % if ~exist([pn.EEG, IDs{id}, '_r',num2str(iRun), '_', condEEG, '_EEG_Rlm_Fhl_rdSeg_Art.mat'])
  58. % display(['processing ID ' IDs{id}, ' Run: ', num2str(iRun)]);
  59. % else
  60. % display(['ID ' IDs{id}, ' Run: ', num2str(iRun), ' has already been processed.']);
  61. % continue;
  62. % end
  63. try
  64. %% load files
  65. % load data
  66. load([pn.EEG, IDs{id}, '_r',num2str(iRun), '_', condEEG, '_EEG_Rlm_Fhl_rdSeg'],'data_EEG');
  67. dataICA = load([pn.EEG, IDs{id}, '_', condEEG, '_EEG_Rlm_Fhl_Ica'],'data');
  68. data = data_EEG; clear data_EEG;
  69. data.elec = dataICA.data.elec;
  70. data.chanlocs = dataICA.data.chanlocs; clear dataICA;
  71. % load config
  72. load([pn.History, IDs{id}, '_r',num2str(iRun), '_', condEEG, '_config'],'config')
  73. %% cut data to requested time (necessary due to acquisition jitter)
  74. % Note that this does not affect the data, but we simply cut some
  75. % sample points from the ITI which is not of interest anyways.
  76. trig.eventDurInS = [1.5, 2, 1, 3, 2, 1.5]; % total time to extract is 11s
  77. trig.eventTriggers = {'S 17'; 'S 72'; 'S 20'; 'S 24'; 'S 64'};
  78. % 2160 has only 62 trials, i.e. the first two trials were
  79. % not recorded.
  80. cfg = [];
  81. cfg.toilim = [-1.5 9.5];
  82. data = ft_redefinetrial(cfg, data);
  83. %% define parameter
  84. % ICs to reject
  85. iclabels = config.ica1.iclabels.manual;
  86. %ics2reject = [iclabels.bli(:); iclabels.mov(:); iclabels.hrt(:); iclabels.ref(:)];
  87. ics2reject = [iclabels.bli(:); iclabels.mov(:); iclabels.hrt(:); iclabels.ref(:); iclabels.art(:); iclabels.emg(:)];
  88. clear iclabels
  89. % trials to remove
  90. trls2remove = config.ArtDect.trials;
  91. % channels to interpolate
  92. chns2interp = config.ArtDect.channels;
  93. %% for select IDs, interpolate channels prior to ICA
  94. if strcmp(IDs{id}, '2112')
  95. cfg = [];
  96. cfg.channel = {'all','-ECG','-A2'};
  97. dataSelect = ft_preprocessing(cfg,data);
  98. cfg = [];
  99. cfg.method = 'spline';
  100. if strcmp(IDs{id}, '2112')
  101. cfg.badchannel = {'Fz'; 'FCz'};
  102. end
  103. cfg.trials = 'all';
  104. cfg.lambda = 1e-5;
  105. cfg.order = 4;
  106. data = ft_channelrepair(cfg,data);
  107. clear cfg;
  108. end
  109. %% ICA (from weights)
  110. % ica config
  111. cfg.method = 'runica';
  112. cfg.channel = {'all','-ECG','-A2'};
  113. cfg.trials = 'all';
  114. cfg.numcomponent = 'all';
  115. cfg.demean = 'yes';
  116. cfg.runica.extended = 1;
  117. % ICA solution
  118. cfg.unmixing = config.ica1.unmixing;
  119. cfg.topolabel = config.ica1.topolabel;
  120. % components
  121. comp = ft_componentanalysis(cfg,data);
  122. % clear cfg
  123. clear cfg data
  124. %% remove components
  125. % cfg for rejecting components (reject: blinks, eye movements, ecg, ref)
  126. cfg.component = sortrows(ics2reject)';
  127. cfg.demean = 'yes';
  128. % reject components
  129. data = ft_rejectcomponent(cfg,comp);
  130. % clear cfg
  131. clear cfg comp ics2reject
  132. %% remove trials
  133. % IMPORTANT: For 2160, the first two trials were not available,
  134. % hence we have to add +2 to the index to identify the correct
  135. % behavioral trials that belong to the EEG data. For 2203 the
  136. % first trial is missing. The third and second trial should
  137. % also be removed, regardless of whether they are indicated to
  138. % be dropped due to automatic correction.
  139. if strcmp(IDs{id}, '2160') && iRun==1
  140. trls2remove=[trls2remove;1];
  141. elseif strcmp(IDs{id}, '2203') && iRun==1
  142. trls2remove=[trls2remove;1];
  143. end
  144. % define trials to keep
  145. trials = 1:length(data.trial);
  146. trials(trls2remove) = [];
  147. if strcmp(IDs{id}, '2160') && iRun==1
  148. trials=trials+2;
  149. data.time = cat(2,data.time{end},data.time{end},data.time);
  150. data.trial = cat(2,data.trial{end},data.trial{end},data.trial);
  151. elseif strcmp(IDs{id}, '2203') && iRun==1
  152. trials=trials+1;
  153. data.time = cat(2,data.time{end},data.time);
  154. data.trial = cat(2,data.trial{end},data.trial);
  155. end
  156. % config for deleting trials
  157. cfg.trials = trials;
  158. % remove trials
  159. data = ft_preprocessing(cfg,data);
  160. nartfree_trials = trials;
  161. overview_nartfree_trials{id,iRun} = nartfree_trials;
  162. config.nartfree_trials = nartfree_trials;
  163. % clear variables
  164. clear cfg trials trls2remove
  165. %% remove eye & reference channels (before interpolation)
  166. cfg.channel = {'all','-IOR','-LHEOG','-RHEOG','-A1'};
  167. cfg.demean = 'yes';
  168. % remove channels
  169. tmpdat = ft_preprocessing(cfg,data);
  170. % clear cfg
  171. clear cfg
  172. % interpolation cfg
  173. cfg.method = 'spline';
  174. cfg.badchannel = tmpdat.label(chns2interp);
  175. cfg.trials = 'all';
  176. cfg.lambda = 1e-5;
  177. cfg.order = 4;
  178. cfg.elec = config.elec;
  179. % interpolate
  180. tmpdat = ft_channelrepair(cfg,tmpdat);
  181. % clear cfg
  182. clear cfg chns2interp
  183. %% replace interpolated data
  184. for j = 1:length(data.trial)
  185. data.trial{j}(1:60,:) = tmpdat.trial{j};
  186. end; clear j
  187. % clear temporary data
  188. clear tmpdat
  189. %% keep channel x trial artifacts
  190. data.ArtDect.channels_x_trials = config.ArtDect.channels_x_trials;
  191. %% save data
  192. data_EEG = data; clear data;
  193. save([pn.EEG, IDs{id}, '_r',num2str(iRun), '_', condEEG, '_EEG_Rlm_Fhl_rdSeg_Art'],'data_EEG')
  194. % check data
  195. % data_EEG = THG_label_artefacts_20110916(data_EEG);
  196. % figure; imagesc(config.ArtDect.channels_x_trials)
  197. % plot resulting trials
  198. if setting.plot == 1
  199. h = figure;
  200. data = NaN(64,size(data_EEG.trial,1),5540);
  201. for indTrial = 1:size(data_EEG.trial,2)
  202. data(:,indTrial,1:size(data_EEG.trial{indTrial},2))= data_EEG.trial{indTrial};
  203. subplot(8,8,indTrial);
  204. imagesc(squeeze(data_EEG.trial{indTrial}));
  205. end; suptitle(['processing ID ' IDs{id}, ' Run: ', num2str(iRun)]);
  206. close(h);
  207. end
  208. %% create overview of percentage of excluded trials, update config.trl
  209. tmp_trialNumbers(1,1) = length(data_EEG.trial);
  210. tmp_trialNumbers(2,1) = 64; % maximum amount of trials
  211. tmp_trialNumbers(3,1) = (100-((tmp_trialNumbers(1,1)/tmp_trialNumbers(2,1))*100)); % percentage of excluded trials
  212. overview_trial_numbers{id, iRun} = tmp_trialNumbers;
  213. config.overview_trial_numbers = tmp_trialNumbers;
  214. %% update config.trl with trigger codes
  215. % add info whether trial is included in final set; 1 = trial containing no artifact
  216. config.trl(:,4) = 0;
  217. config.trl(nartfree_trials, 4) = 1;
  218. % add trial number
  219. config.trl(:,5) = 0;
  220. config.trl(nartfree_trials, 5) = nartfree_trials;
  221. % load marker file
  222. mrk = config.mrk;
  223. for i = 1:size(mrk,2)
  224. mrk_val{i,1} = mrk(1,i).value;
  225. end
  226. % add onset and offset triggers
  227. config.trl(:,6) = 17;
  228. config.trl(:,7) = 64;
  229. % save config
  230. save([pn.History, IDs{id}, '_r',num2str(iRun), '_', condEEG, '_config'],'config')
  231. catch ME
  232. warning('Error occured. Please check.');
  233. rethrow(ME)
  234. fprintf('\n ID not availble! Skip! \n')
  235. ID_unavailable{id,iRun} = (IDs{id});
  236. end % try...catch
  237. end % run
  238. end % id