G_STSW_segmentation_raw_data_180111_noTardis.m 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. function G_STSW_segmentation_raw_data_180111_noTardis()
  2. %% G_STSW_segmentation_raw_data_180111
  3. % 170921 | JQK adapted from MD script
  4. % 180118 | adapted for STSW study YA, adapted for tardis
  5. % config.data_file still refers to the non-tardis location
  6. % 180124 | skip data without eye info from processing
  7. %% initialize
  8. % restoredefaultpath;
  9. % clear all; close all; pack; clc;
  10. %% pathdef
  11. if ismac
  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.triggerTiming= [pn.eeg_root, 'C_figures/D_TriggerTiming/'];
  16. pn.EEG = [pn.eeg_root, 'B_data/C_EEG_FT/']; mkdir(pn.EEG);
  17. pn.History = [pn.eeg_root, 'B_data/D_History/']; mkdir(pn.History);
  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.helper = [pn.eeg_root, 'A_scripts/helper/']; addpath(pn.helper);
  25. else
  26. pn.root = '/home/mpib/LNDG/StateSwitch/WIP_eeg/SA_preproc_study/';
  27. pn.EEG = [pn.root, 'B_data/C_EEG_FT/'];
  28. pn.History = [pn.root, 'B_data/D_History/'];
  29. pn.triggerTiming= [pn.root, 'C_figures/D_TriggerTiming/'];
  30. end
  31. %% define Condition & IDs for preprocessing
  32. condEEG = 'dynamic';
  33. %% define IDs for segmentation
  34. % N = 47 YAs + 53 OAs;
  35. IDs = {'1117';'1118';'1120';'1124';'1126';'1131';'1132';'1135';'1136';'1138';...
  36. '1144';'1151';'1158';'1160';'1163';'1164';'1167';'1169';'1172';'1173';...
  37. '1178';'1182';'1215';'1216';'1219';'1221';'1223';'1227';'1228';'1233';...
  38. '1234';'1237';'1239';'1240';'1243';'1245';'1247';'1250';'1252';'1257';...
  39. '1261';'1265';'1266';'1268';'1270';'1276';'1281';...
  40. '2104';'2107';'2108';'2112';'2118';'2120';'2121';'2123';'2125';'2129';...
  41. '2130';'2131';'2132';'2133';'2134';'2135';'2139';'2140';'2145';'2147';...
  42. '2149';'2157';'2160';'2201';'2202';'2203';'2205';'2206';'2209';'2210';...
  43. '2211';'2213';'2214';'2215';'2216';'2217';'2219';'2222';'2224';'2226';...
  44. '2227';'2236';'2237';'2238';'2241';'2244';'2246';'2248';'2250';'2251';...
  45. '2252';'2258';'2261'};
  46. %% loop IDs
  47. clc;
  48. for id = 1:length(IDs)
  49. display(['processing ID ' num2str(IDs{id})]);
  50. for iRun = 1:4
  51. %% load raw data
  52. if 1%~exist([pn.EEG, IDs{id}, '_r',num2str(iRun), '_', condEEG, '_eyeEEG_Rlm_Fhl_rdSeg.mat'],'file')
  53. % load config
  54. load([pn.History, IDs{id}, '_r',num2str(iRun), '_', condEEG, '_config.mat'],'config');
  55. % copy ICA labeling info
  56. configWithICA = load([pn.History, IDs{id}, '_', condEEG, '_config.mat'],'config');
  57. config.ica1 = configWithICA.config.ica1;
  58. %% ---- generate segmentation ---- %%
  59. % load marker file
  60. mrk = config.mrk;
  61. for i = 1:size(mrk,2)
  62. mrk_val{i,1} = mrk(1,i).value;
  63. end
  64. % for 2160, the onset marker was not recorded: extract at beginning
  65. if strcmp(IDs{id}, '2160') && iRun ==1
  66. mrk_val{2,1} = 'S 17';
  67. end
  68. % for 2203, the onset marker was not recorded: extract at beginning
  69. if strcmp(IDs{id}, '2203') && iRun ==1
  70. mrk_val{2,1} = 'S 17';
  71. end
  72. % generate trial structure
  73. indOnset = find(strcmp(mrk_val(:,:),'S 17')); % (fix cue onset trigger = 'S 17')
  74. indOnset = sortrows(indOnset,1);
  75. indOffset = find(strcmp(mrk_val(:,:),'S 64')); % (ITI trigger = 'S 64')
  76. indOffset = sortrows(indOffset,1);
  77. h = figure;
  78. plot(diff([mrk(indOnset).sample]))
  79. hold on; plot(diff([mrk(indOffset).sample]))
  80. pn.plotFolder = pn.triggerTiming;
  81. figureName = ['A_triggerTiming_',IDs{id}, '_r',num2str(iRun)];
  82. saveas(h, [pn.plotFolder, figureName], 'png');
  83. close(h);
  84. % Stim trials
  85. trl = zeros(length(indOnset),3);
  86. TOI1 = 1500; % segmentation before trigger
  87. TOI2 = 1500; % segmentation following trigger
  88. for j = 1:size(trl,1)
  89. trl(j,1) = mrk(1,indOnset(j)).sample - TOI1; % segmentation from 1500 ms before stim
  90. trl(j,2) = mrk(1,indOffset(j)).sample + TOI2; % to 1500 ms after trigger
  91. trl(j,3) = -TOI1;% offset
  92. end; clear j
  93. % if an onset occurs before recording onset: set to extract at 1
  94. if numel(find(trl(:,1)<0))>0
  95. trl(find(trl(:,1)<0),3) = trl(find(trl(:,1)<0),3)-trl(find(trl(:,1)<0),1);
  96. trl(find(trl(:,1)<0),1) = 1;
  97. end
  98. % add trial structure to config
  99. config.trl = trl;
  100. % save config
  101. save([pn.History, IDs{id}, '_r',num2str(iRun), '_', condEEG, '_config.mat'],'config')
  102. %% ---- load, filter, and re-reference raw data ---- %%
  103. % define reading & preprocessing parameters
  104. % read in all data first
  105. cfg = [];
  106. cfg.datafile = [config.data_file];
  107. cfg.trl = config.trl;
  108. cfg.channel = {'all'};
  109. cfg.implicitref = 'REF'; % recover implicit reference
  110. data = ft_preprocessing(cfg);
  111. %% SWITCH CHANNELS ACCORDING TO ARRANGEMENT!
  112. if max(strcmp(IDs{id}, {'1126'; '2227'}))==1
  113. data = SS_switchChannels_GreenYellow(data); % green and yellow boxes exchanged
  114. data = SS_switchChannels_Study_noA1(data); % TP9 and TP10 exchanged manually
  115. elseif max(strcmp(IDs{id}, {'1216'}))==1
  116. data = SS_switchChannels_GreenYellow(data); % green and yellow boxes exchanged
  117. data = SS_switchChannels_Study(data); % TP9(A1) and TP10(FCz) wrongly ordered in workspace
  118. elseif max(strcmp(IDs{id}, {'1118'; '1215'; '1124'}))==1
  119. data = SS_switchChannels_Study_noA1(data); % TP9 and TP10 exchanged manually
  120. else
  121. data = SS_switchChannels_Study(data); % TP9(A1) and TP10(FCz) wrongly ordered in workspace
  122. end
  123. data = SS_changeChannels(data);
  124. %%
  125. % select eye data
  126. cfg = [];
  127. cfg.channel = {'TIME', 'L_GAZE_X', 'L_GAZE_Y', ...
  128. 'L_AREA', 'L_VEL_X', 'L_VEL_Y', 'RES_X', 'RES_Y', 'INPUT'};
  129. data_eye = ft_preprocessing(cfg, data);
  130. %%
  131. % select EEG (non-eye/ECG/trigger) data
  132. cfg = [];
  133. cfg.channel = {'all', ...
  134. '-TIME', '-L_GAZE_X', '-L_GAZE_Y', ...
  135. '-L_AREA', '-L_VEL_X', '-L_VEL_Y', '-RES_X', '-RES_Y', '-INPUT', '-ECG'};
  136. data_EEG = ft_preprocessing(cfg, data);
  137. %%
  138. % filter & reref EEG data
  139. cfg = [];
  140. cfg.continuous = 'yes';
  141. cfg.demean = 'yes';
  142. cfg.reref = 'yes';
  143. cfg.refchannel = {'A1','A2'};
  144. cfg.implicitref = 'A2';
  145. cfg.hpfilter = 'yes';
  146. cfg.hpfreq = .2;
  147. cfg.hpfiltord = 4;
  148. cfg.hpfilttype = 'but';
  149. cfg.lpfilter = 'yes';
  150. cfg.lpfreq = 125;
  151. cfg.lpfiltord = 4;
  152. cfg.lpfilttype = 'but';
  153. % get data
  154. data_EEG = ft_preprocessing(cfg, data_EEG);
  155. flt = cfg;
  156. % clear cfg structure
  157. clear cfg
  158. %% ---- resampling [500 Hz] ---- %%
  159. % define settings for resampling
  160. cfg.resamplefs = 500; % raw_eeg (ICA loop) at srate of 250Hz! now resample raw files (original files) to 500 Hz!
  161. cfg.detrend = 'no';
  162. cfg.feedback = 'no';
  163. cfg.trials = 'all';
  164. % resample ALL data
  165. data = ft_resampledata(cfg,data);
  166. if str2num(IDs{id})==1223 & (iRun == 2 | iRun == 3 | iRun == 4)
  167. disp('Eye data skipped');
  168. elseif str2num(IDs{id})==1228 & (iRun == 4)
  169. disp('Eye data skipped');
  170. else
  171. data_eye = ft_resampledata(cfg,data_eye);
  172. end
  173. data_EEG = ft_resampledata(cfg,data_EEG);
  174. resample = cfg;
  175. % clear variables
  176. clear cfg
  177. % update config
  178. config.seg.flt = flt;
  179. config.seg.resample = resample;
  180. % save config, data
  181. save([pn.EEG, IDs{id}, '_r',num2str(iRun), '_', condEEG, '_eyeEEG_Rlm_Fhl_rdSeg'],'data')
  182. if str2num(IDs{id})==1223 & (iRun == 2 | iRun == 3 | iRun == 4)
  183. disp('Eye data skipped');
  184. elseif str2num(IDs{id})==1228 & (iRun == 4)
  185. disp('Eye data skipped');
  186. else
  187. save([pn.EEG, IDs{id}, '_r',num2str(iRun), '_', condEEG, '_eye_Rlm_Fhl_rdSeg'],'data_eye')
  188. end
  189. save([pn.EEG, IDs{id}, '_r',num2str(iRun), '_', condEEG, '_EEG_Rlm_Fhl_rdSeg'],'data_EEG')
  190. save([pn.History, IDs{id}, '_r',num2str(iRun), '_', condEEG, '_config'],'config')
  191. % clear variables
  192. clear config data data_eye data_EEG indOnset indOffset mrk mrk_val trl TOI1 TOI2
  193. else
  194. disp(['Data already exists: ', IDs{id}, ' Run ',num2str(iRun)]);
  195. end % exist
  196. end; clear iRun
  197. end; clear id