01_prepare_preprocessing.m 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. %% A_prepare_preprocessing_StateSwitch_170913
  2. % prepare (i.e. filter + downsample data for ICA1)
  3. % INPUT: eeglab structures: EEG data, merged with eye tracking data
  4. % 170913 | JQK adapted function from MD's LC-NE scripts
  5. % 180205 | added OA IDs, corrected paths
  6. % 180207 | 2227 changed channels
  7. %% initialize
  8. restoredefaultpath;
  9. clear all; close all; pack; clc;
  10. %% pathdef
  11. %pn.eeg_root = '/Users/kosciessa/Desktop/mountpoint_tardis_LNDG/LNDG/StateSwitch/WIP_eeg/SA_preproc_study_YA/';
  12. pn.study = '/Volumes/LNDG/Projects/StateSwitch/';
  13. pn.eeg_root = [pn.study, 'dynamic/data/eeg/task/A_preproc/SA_preproc_study/'];
  14. pn.dynamic_In = [pn.eeg_root, 'B_data/B_EEG_ET_ByRun/'];
  15. pn.eyePlots = [pn.eeg_root, 'C_figures/B_EyeChannels/']; mkdir(pn.eyePlots);
  16. pn.EEG_out = [pn.eeg_root, 'B_data/C_EEG_FT/']; mkdir(pn.EEG_out);
  17. pn.History_out = [pn.eeg_root, 'B_data/D_History/']; mkdir(pn.History_out);
  18. %% add ConMemEEG tools
  19. pn.MWBtools = [pn.eeg_root, 'T_tools/fnct_MWB']; addpath(genpath(pn.MWBtools));
  20. pn.THGtools = [pn.eeg_root, 'T_tools/fnct_THG']; addpath(genpath(pn.THGtools));
  21. pn.commontools = [pn.eeg_root, 'T_tools/fnct_common']; addpath(genpath(pn.commontools));
  22. pn.fnct_JQK = [pn.eeg_root, 'T_tools/fnct_JQK/']; addpath(genpath(pn.fnct_JQK));
  23. pn.FT = [pn.eeg_root, 'T_tools/fieldtrip-20170904/']; addpath(pn.FT); ft_defaults;
  24. pn.scripts = [pn.eeg_root, 'A_scripts/helper/']; addpath(pn.scripts);
  25. %% datadef
  26. condEEG = 'dynamic';
  27. %% define IDs for preprocessing
  28. % % N = 47 (1213 excluded);
  29. % IDs = {'1117';'1118';'1120';'1124';'1126';'1131';'1132';'1135';'1136';'1138';'1144';'1151';'1158';'1160';'1163';'1164';'1167';'1169';'1172';'1173';'1178';'1182';'1215';'1216';'1219';'1221';'1223';'1227';'1228';'1233';'1234';'1237';'1239';'1240';'1243';'1245';'1247';'1250';'1252';'1257';'1261';'1265';'1266';'1268';'1270';'1276';'1281'};
  30. % N = 53 OAs;
  31. IDs = {'2104';'2107';'2108';'2112';'2118';'2120';'2121';'2123';'2125';'2129';'2130';'2131';'2132';'2133';'2134';'2135';'2139';'2140';'2145';'2147';'2149';'2157';'2160';'2201';'2202';'2203';'2205';'2206';'2209';'2210';'2211';'2213';'2214';'2215';'2216';'2217';'2219';'2222';'2224';'2226';'2227';'2236';'2237';'2238';'2241';'2244';'2246';'2248';'2250';'2251';'2252';'2258';'2261'};
  32. %% loop IDs
  33. for id = 1:length(IDs)
  34. %% preprocessing
  35. %if ~exist([pdat.analyses.EEG_dat '\' num2str(ID(id)) '\raw_eeg\' num2str(ID(id)) '_' condEEG '_EEG_Raw_Rlm_Flh_Res.mat'],'file')
  36. % try
  37. for indRun = 1:4
  38. display(['processing ID ' IDs{id}, ' Run ', num2str(indRun)]);
  39. file = [pn.([condEEG, '_In']), IDs{id} ,'_r',num2str(indRun), '_', condEEG, '_eyeEEG.set'];
  40. %% load data
  41. cfg_preprocessing = [];
  42. cfg_preprocessing.datafile = file;
  43. % load header & event information
  44. config = ft_read_header(cfg_preprocessing.datafile);
  45. config.data_file = cfg_preprocessing.datafile;
  46. config.mrk = ft_read_event(cfg_preprocessing.datafile);
  47. % define reading & preprocessing parameters
  48. cfg_preprocessing.channel = {'all'};
  49. cfg_preprocessing.implicitref = 'REF'; % recover implicit reference
  50. % get all data first, then apply specific steps only for subsets
  51. data_eyeEEG = ft_preprocessing(cfg_preprocessing);
  52. %% SWITCH CHANNELS ACCORDING TO ARRANGEMENT!
  53. % if max(strcmp(IDs{id}, {'1213'}))==1
  54. % data_eyeEEG = SS_switchChannels_Pilot_Study(data_eyeEEG);
  55. if max(strcmp(IDs{id}, {'1126'; '2227'}))==1
  56. data_eyeEEG = SS_switchChannels_GreenYellow(data_eyeEEG); % green and yellow boxes exchanged
  57. data_eyeEEG = SS_switchChannels_Study_noA1(data_eyeEEG); % TP9 and TP10 exchanged manually
  58. elseif max(strcmp(IDs{id}, {'1216'}))==1
  59. data_eyeEEG = SS_switchChannels_GreenYellow(data_eyeEEG); % green and yellow boxes exchanged
  60. data_eyeEEG = SS_switchChannels_Study(data_eyeEEG); % TP9(A1) and TP10(FCz) wrongly ordered in workspace
  61. elseif max(strcmp(IDs{id}, {'1118'; '1215'; '1124'}))==1
  62. data_eyeEEG = SS_switchChannels_Study_noA1(data_eyeEEG); % TP9 and TP10 exchanged manually
  63. else
  64. data_eyeEEG = SS_switchChannels_Study(data_eyeEEG); % TP9(A1) and TP10(FCz) wrongly ordered in workspace
  65. end
  66. data_eyeEEG = SS_changeChannels(data_eyeEEG);
  67. %% Plot excerpt from eye channels
  68. h = figure;
  69. subplot(3,1,1);
  70. plot(zscore(data_eyeEEG.trial{1,1}(63,5000:15000),[],2)); % LHEOG
  71. hold on; plot(zscore(data_eyeEEG.trial{1,1}(64,5000:15000),[],2)); % RHEOG
  72. xlim([0 10000]); legend({'LHEOG'; 'RHEOG'});
  73. subplot(3,1,2);
  74. %hold on; plot(zscore(data_eyeEEG.trial{1,1}(65,5000:15000),[],2)); % ECG
  75. hold on; plot(zscore(data_eyeEEG.trial{1,1}(62,5000:15000),[],2)); % IOR
  76. hold on; plot(zscore(data_eyeEEG.trial{1,1}(24,5000:15000),[],2)); % FT8
  77. xlim([0 10000]); legend({'IOR'; 'FT8'});
  78. subplot(3,1,3);
  79. if size(data_eyeEEG.trial{1,1},1)>66
  80. hold on; plot(zscore(data_eyeEEG.trial{1,1}(67,5000:15000),[],2)); % left Gaze X
  81. hold on; plot(zscore(data_eyeEEG.trial{1,1}(68,5000:15000),[],2)); % left Gaze Y
  82. xlim([0 10000]);legend({'left X'; 'left Y'});
  83. end
  84. pn.plotFolder = pn.eyePlots;
  85. figureName = [IDs{id} ,'_r',num2str(indRun), '_', condEEG, '_eyeChannels'];
  86. saveas(h, [pn.plotFolder, figureName], 'png');
  87. close(h);
  88. %% continue preprocessing
  89. % spare ECG from reref!
  90. tmp.data_ECG = data_eyeEEG;
  91. cfg_ECG = [];
  92. cfg_ECG.channel = {'ECG'};
  93. cfg_ECG.bsfilter = 'yes';
  94. cfg_ECG.bsfiltord = 4;
  95. cfg_ECG.bsfreq = [48 52];
  96. tmp.data_ECG = ft_preprocessing(cfg_ECG, tmp.data_ECG);
  97. % exclude ET, ECG channel (and others) here?
  98. cfg_preprocessing = [];
  99. cfg_preprocessing.channel = {'all','-TIME','-L-GAZE-X','-L-GAZE-Y','-L-AREA','-L-VEL-X','-L-VEL-Y','-RES-X','-RES-Y','-INPUT'};
  100. cfg_preprocessing.continuous = 'yes';
  101. cfg_preprocessing.demean = 'yes';
  102. cfg_preprocessing.reref = 'yes';
  103. cfg_preprocessing.refchannel = {'A1', 'A2'};
  104. cfg_preprocessing.implicitref = 'A2';
  105. cfg_preprocessing.hpfilter = 'yes';
  106. cfg_preprocessing.hpfreq = 1;
  107. cfg_preprocessing.hpfiltord = 4;
  108. cfg_preprocessing.hpfilttype = 'but';
  109. cfg_preprocessing.lpfilter = 'yes';
  110. cfg_preprocessing.lpfreq = 100;
  111. cfg_preprocessing.lpfiltord = 4;
  112. cfg_preprocessing.lpfilttype = 'but';
  113. % cfg_preprocessing.bsfilter = 'yes';
  114. % cfg_preprocessing.bsfiltord = 4;
  115. % cfg_preprocessing.bsfreq = [48 52];
  116. % define settings for resampling
  117. cfg_resample.resamplefs = 250;
  118. cfg_resample.detrend = 'no';
  119. cfg_resample.feedback = 'no';
  120. cfg_resample.trials = 'all';
  121. % get data
  122. % load complete data at once & resample afterwards
  123. data_EEG = ft_preprocessing(cfg_preprocessing, data_eyeEEG);
  124. % after reref, copy original ECG (we don't want it rerefed)
  125. data_EEG.trial{1,1}(find(strcmp(data_EEG.label , 'ECG')),:) = tmp.data_ECG.trial{1,1};
  126. clear tmp;
  127. data_EEG = ft_resampledata(cfg_resample,data_EEG);
  128. % change data precision to single
  129. for t = 1:length(data_EEG.trial)
  130. data_EEG.trial{t} = single(data_EEG.trial{t});
  131. end; clear t
  132. %% remove data at edges
  133. % not implemented & maybe not necessary
  134. %% save data
  135. save([pn.EEG_out, IDs{id}, '_r',num2str(indRun), '_', condEEG, '_EEG_Raw_Rlm_Flh_Res.mat'],'data_EEG', '-v7.3');
  136. save([pn.EEG_out, IDs{id}, '_r',num2str(indRun), '_', condEEG, '_eyeEEG_Raw.mat'],'data_eyeEEG', '-v7.3');
  137. % save config
  138. save([pn.History_out, IDs{id}, '_r',num2str(indRun), '_', condEEG, '_config.mat'],'config');
  139. % clear variables
  140. clear cfg_* config data_EEG data_eyeEEG file
  141. end % run loop
  142. end; clear id;