G_STSW_segmentation_raw_data_180111.m 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. function G_STSW_segmentation_raw_data_180111(id)
  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. %% initialize
  6. % restoredefaultpath;
  7. % clear all; close all; pack; clc;
  8. %% pathdef
  9. if ismac
  10. pn.study = '/Volumes/LNDG/Projects/StateSwitch/';
  11. pn.dynamic_In = [pn.eeg_root, 'B_data/B_EEG_ET_ByRun/'];
  12. pn.triggerTiming= [pn.eeg_root, 'C_figures/D_TriggerTiming/'];
  13. pn.EEG = [pn.eeg_root, 'B_data/C_EEG_FT/']; mkdir(pn.EEG);
  14. pn.History = [pn.eeg_root, 'B_data/D_History/']; mkdir(pn.History);
  15. % add ConMemEEG tools
  16. pn.MWBtools = [pn.eeg_root, 'T_tools/fnct_MWB/']; addpath(genpath(pn.MWBtools));
  17. pn.THGtools = [pn.eeg_root, 'T_tools/fnct_THG/']; addpath(genpath(pn.THGtools));
  18. pn.commontools = [pn.eeg_root, 'T_tools/fnct_common/']; addpath(genpath(pn.commontools));
  19. pn.fnct_JQK = [pn.eeg_root, 'T_tools/fnct_JQK/']; addpath(genpath(pn.fnct_JQK));
  20. pn.FT = [pn.eeg_root, 'T_tools/fieldtrip-20170904/']; addpath(pn.FT); ft_defaults;
  21. pn.helper = [pn.eeg_root, 'A_scripts/helper/']; addpath(pn.helper);
  22. else
  23. pn.root = '/home/mpib/LNDG/StateSwitch/WIP_eeg/SA_preproc_study_YA/';
  24. pn.EEG = [pn.root, 'B_data/C_EEG_FT/'];
  25. pn.History = [pn.root, 'B_data/D_History/'];
  26. pn.triggerTiming= [pn.root, 'C_figures/D_TriggerTiming/'];
  27. end
  28. %% define Condition & IDs for preprocessing
  29. condEEG = 'dynamic';
  30. %% define IDs for segmentation
  31. % N = 47;
  32. 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'};
  33. id = str2num(id);
  34. % loop IDs
  35. clc;
  36. %for id = 1:length(IDs)
  37. display(['processing ID ' num2str(IDs{id})]);
  38. for iRun = 1:4
  39. %% load raw data
  40. if ~exist([pn.EEG, IDs{id}, '_r',num2str(iRun), '_', condEEG, '_eyeEEG_Rlm_Fhl_rdSeg.mat'],'file')
  41. % load config
  42. load([pn.History, IDs{id}, '_r',num2str(iRun), '_', condEEG, '_config.mat'],'config');
  43. % copy ICA labeling info
  44. configWithICA = load([pn.History, IDs{id}, '_', condEEG, '_config.mat'],'config');
  45. config.ica1 = configWithICA.config.ica1;
  46. %% ---- generate segmentation ---- %%
  47. % load marker file
  48. mrk = config.mrk;
  49. for i = 1:size(mrk,2)
  50. mrk_val{i,1} = mrk(1,i).value;
  51. end
  52. % generate trial structure
  53. indOnset = find(strcmp(mrk_val(:,:),'S 17')); % (fix cue onset trigger = 'S 17')
  54. indOnset = sortrows(indOnset,1);
  55. indOffset = find(strcmp(mrk_val(:,:),'S 64')); % (ITI trigger = 'S 64')
  56. indOffset = sortrows(indOffset,1);
  57. h = figure;
  58. plot(diff([mrk(indOnset).sample]))
  59. hold on; plot(diff([mrk(indOffset).sample]))
  60. pn.plotFolder = pn.triggerTiming;
  61. figureName = ['A_triggerTiming_',IDs{id}, '_r',num2str(iRun)];
  62. saveas(h, [pn.plotFolder, figureName], 'fig');
  63. saveas(h, [pn.plotFolder, figureName], 'epsc');
  64. saveas(h, [pn.plotFolder, figureName], 'png');
  65. close(h);
  66. % Stim trials
  67. trl = zeros(length(indOnset),3);
  68. TOI1 = 1500; % segmentation before trigger
  69. TOI2 = 1500; % segmentation following trigger
  70. for j = 1:size(trl,1)
  71. trl(j,1) = mrk(1,indOnset(j)).sample - TOI1; % segmentation from 1500 ms before stim
  72. trl(j,2) = mrk(1,indOffset(j)).sample + TOI2; % to 1500 ms after trigger
  73. trl(j,3) = -TOI1;% offset
  74. end; clear j
  75. % add trial structure to config
  76. config.trl = trl;
  77. % save config
  78. save([pn.History, IDs{id}, '_r',num2str(iRun), '_', condEEG, '_config.mat'],'config')
  79. %% ---- load, filter, and re-reference raw data ---- %%
  80. % define reading & preprocessing parameters
  81. % read in all data first
  82. cfg = [];
  83. %cfg.datafile = [config.data_file];
  84. cfg.datafile = [pn.root, '/B_data/B_EEG_ET_ByRun/',IDs{id}, '_r',num2str(iRun), '_', condEEG, '_eyeEEG.set'];
  85. cfg.trl = config.trl;
  86. cfg.channel = {'all'};
  87. cfg.implicitref = 'REF'; % recover implicit reference
  88. data = ft_preprocessing(cfg);
  89. %% SWITCH CHANNELS ACCORDING TO ARRANGEMENT!
  90. if max(strcmp(IDs{id}, {'1126'}))==1
  91. data = SS_switchChannels_GreenYellow(data); % green and yellow boxes exchanged
  92. data = SS_switchChannels_Study_noA1(data); % TP9 and TP10 exchanged manually
  93. elseif max(strcmp(IDs{id}, {'1216'}))==1
  94. data = SS_switchChannels_GreenYellow(data); % green and yellow boxes exchanged
  95. data = SS_switchChannels_Study(data); % TP9(A1) and TP10(FCz) wrongly ordered in workspace
  96. elseif max(strcmp(IDs{id}, {'1118'; '1215'; '1124'}))==1
  97. data = SS_switchChannels_Study_noA1(data); % TP9 and TP10 exchanged manually
  98. else
  99. data = SS_switchChannels_Study(data); % TP9(A1) and TP10(FCz) wrongly ordered in workspace
  100. end
  101. data = SS_changeChannels(data);
  102. %%
  103. % select eye data
  104. cfg = [];
  105. cfg.channel = {'TIME', 'L_GAZE_X', 'L_GAZE_Y', ...
  106. 'L_AREA', 'L_VEL_X', 'L_VEL_Y', 'RES_X', 'RES_Y', 'INPUT'};
  107. data_eye = ft_preprocessing(cfg, data);
  108. %%
  109. % select EEG (non-eye/ECG/trigger) data
  110. cfg = [];
  111. cfg.channel = {'all', ...
  112. '-TIME', '-L_GAZE_X', '-L_GAZE_Y', ...
  113. '-L_AREA', '-L_VEL_X', '-L_VEL_Y', '-RES_X', '-RES_Y', '-INPUT', '-ECG'};
  114. data_EEG = ft_preprocessing(cfg, data);
  115. %%
  116. % filter & reref EEG data
  117. cfg = [];
  118. cfg.continuous = 'yes';
  119. cfg.demean = 'yes';
  120. cfg.reref = 'yes';
  121. cfg.refchannel = {'A1','A2'};
  122. cfg.implicitref = 'A2';
  123. cfg.hpfilter = 'yes';
  124. cfg.hpfreq = .2;
  125. cfg.hpfiltord = 4;
  126. cfg.hpfilttype = 'but';
  127. cfg.lpfilter = 'yes';
  128. cfg.lpfreq = 125;
  129. cfg.lpfiltord = 4;
  130. cfg.lpfilttype = 'but';
  131. % get data
  132. data_EEG = ft_preprocessing(cfg, data_EEG);
  133. flt = cfg;
  134. % clear cfg structure
  135. clear cfg
  136. %% ---- resampling [500 Hz] ---- %%
  137. % define settings for resampling
  138. cfg.resamplefs = 500; % raw_eeg (ICA loop) at srate of 250Hz! now resample raw files (original files) to 500 Hz!
  139. cfg.detrend = 'no';
  140. cfg.feedback = 'no';
  141. cfg.trials = 'all';
  142. % resample ALL data
  143. data = ft_resampledata(cfg,data);
  144. if str2num(IDs{id})==1223 & (iRun == 2 | iRun == 3 | iRun == 4)
  145. disp('Eye data skipped');
  146. elseif str2num(IDs{id})==1228 & (iRun == 4)
  147. disp('Eye data skipped');
  148. else
  149. data_eye = ft_resampledata(cfg,data_eye);
  150. end
  151. data_EEG = ft_resampledata(cfg,data_EEG);
  152. resample = cfg;
  153. % clear variables
  154. clear cfg
  155. % update config
  156. config.seg.flt = flt;
  157. config.seg.resample = resample;
  158. % save config, data
  159. save([pn.EEG, IDs{id}, '_r',num2str(iRun), '_', condEEG, '_eyeEEG_Rlm_Fhl_rdSeg'],'data')
  160. if str2num(IDs{id})==1223 & (iRun == 2 | iRun == 3 | iRun == 4)
  161. disp('Eye data skipped');
  162. elseif str2num(IDs{id})==1228 & (iRun == 4)
  163. disp('Eye data skipped');
  164. else
  165. save([pn.EEG, IDs{id}, '_r',num2str(iRun), '_', condEEG, '_eye_Rlm_Fhl_rdSeg'],'data_eye')
  166. end
  167. save([pn.EEG, IDs{id}, '_r',num2str(iRun), '_', condEEG, '_EEG_Rlm_Fhl_rdSeg'],'data_EEG')
  168. save([pn.History, IDs{id}, '_r',num2str(iRun), '_', condEEG, '_config'],'config')
  169. % clear variables
  170. clear config data data_eye data_EEG indOnset indOffset mrk mrk_val trl TOI1 TOI2
  171. else
  172. end % exist
  173. end; clear iRun
  174. %end; clear id