123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- function G_STSW_segmentation_raw_data_180111(id)
- %% G_STSW_segmentation_raw_data_180111
- % 170921 | JQK adapted from MD script
- % 180118 | adapted for STSW study YA, adapted for tardis
- %% initialize
- % restoredefaultpath;
- % clear all; close all; pack; clc;
- %% pathdef
- if ismac
- pn.study = '/Volumes/LNDG/Projects/StateSwitch/';
- pn.dynamic_In = [pn.eeg_root, 'B_data/B_EEG_ET_ByRun/'];
- pn.triggerTiming= [pn.eeg_root, 'C_figures/D_TriggerTiming/'];
- pn.EEG = [pn.eeg_root, 'B_data/C_EEG_FT/']; mkdir(pn.EEG);
- pn.History = [pn.eeg_root, 'B_data/D_History/']; mkdir(pn.History);
- % add ConMemEEG tools
- pn.MWBtools = [pn.eeg_root, 'T_tools/fnct_MWB/']; addpath(genpath(pn.MWBtools));
- pn.THGtools = [pn.eeg_root, 'T_tools/fnct_THG/']; addpath(genpath(pn.THGtools));
- pn.commontools = [pn.eeg_root, 'T_tools/fnct_common/']; addpath(genpath(pn.commontools));
- pn.fnct_JQK = [pn.eeg_root, 'T_tools/fnct_JQK/']; addpath(genpath(pn.fnct_JQK));
- pn.FT = [pn.eeg_root, 'T_tools/fieldtrip-20170904/']; addpath(pn.FT); ft_defaults;
- pn.helper = [pn.eeg_root, 'A_scripts/helper/']; addpath(pn.helper);
- else
- pn.root = '/home/mpib/LNDG/StateSwitch/WIP_eeg/SA_preproc_study_YA/';
- pn.EEG = [pn.root, 'B_data/C_EEG_FT/'];
- pn.History = [pn.root, 'B_data/D_History/'];
- pn.triggerTiming= [pn.root, 'C_figures/D_TriggerTiming/'];
- end
- %% define Condition & IDs for preprocessing
- condEEG = 'dynamic';
- %% define IDs for segmentation
- % N = 47;
- 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'};
- id = str2num(id);
- % loop IDs
- clc;
- %for id = 1:length(IDs)
- display(['processing ID ' num2str(IDs{id})]);
- for iRun = 1:4
- %% load raw data
- if ~exist([pn.EEG, IDs{id}, '_r',num2str(iRun), '_', condEEG, '_eyeEEG_Rlm_Fhl_rdSeg.mat'],'file')
- % load config
- load([pn.History, IDs{id}, '_r',num2str(iRun), '_', condEEG, '_config.mat'],'config');
- % copy ICA labeling info
- configWithICA = load([pn.History, IDs{id}, '_', condEEG, '_config.mat'],'config');
- config.ica1 = configWithICA.config.ica1;
- %% ---- generate segmentation ---- %%
- % load marker file
- mrk = config.mrk;
- for i = 1:size(mrk,2)
- mrk_val{i,1} = mrk(1,i).value;
- end
- % generate trial structure
- indOnset = find(strcmp(mrk_val(:,:),'S 17')); % (fix cue onset trigger = 'S 17')
- indOnset = sortrows(indOnset,1);
- indOffset = find(strcmp(mrk_val(:,:),'S 64')); % (ITI trigger = 'S 64')
- indOffset = sortrows(indOffset,1);
- h = figure;
- plot(diff([mrk(indOnset).sample]))
- hold on; plot(diff([mrk(indOffset).sample]))
- pn.plotFolder = pn.triggerTiming;
- figureName = ['A_triggerTiming_',IDs{id}, '_r',num2str(iRun)];
- saveas(h, [pn.plotFolder, figureName], 'fig');
- saveas(h, [pn.plotFolder, figureName], 'epsc');
- saveas(h, [pn.plotFolder, figureName], 'png');
- close(h);
-
- % Stim trials
- trl = zeros(length(indOnset),3);
- TOI1 = 1500; % segmentation before trigger
- TOI2 = 1500; % segmentation following trigger
- for j = 1:size(trl,1)
- trl(j,1) = mrk(1,indOnset(j)).sample - TOI1; % segmentation from 1500 ms before stim
- trl(j,2) = mrk(1,indOffset(j)).sample + TOI2; % to 1500 ms after trigger
- trl(j,3) = -TOI1;% offset
- end; clear j
- % add trial structure to config
- config.trl = trl;
- % save config
- save([pn.History, IDs{id}, '_r',num2str(iRun), '_', condEEG, '_config.mat'],'config')
- %% ---- load, filter, and re-reference raw data ---- %%
- % define reading & preprocessing parameters
- % read in all data first
- cfg = [];
- %cfg.datafile = [config.data_file];
- cfg.datafile = [pn.root, '/B_data/B_EEG_ET_ByRun/',IDs{id}, '_r',num2str(iRun), '_', condEEG, '_eyeEEG.set'];
- cfg.trl = config.trl;
- cfg.channel = {'all'};
- cfg.implicitref = 'REF'; % recover implicit reference
- data = ft_preprocessing(cfg);
- %% SWITCH CHANNELS ACCORDING TO ARRANGEMENT!
- if max(strcmp(IDs{id}, {'1126'}))==1
- data = SS_switchChannels_GreenYellow(data); % green and yellow boxes exchanged
- data = SS_switchChannels_Study_noA1(data); % TP9 and TP10 exchanged manually
- elseif max(strcmp(IDs{id}, {'1216'}))==1
- data = SS_switchChannels_GreenYellow(data); % green and yellow boxes exchanged
- data = SS_switchChannels_Study(data); % TP9(A1) and TP10(FCz) wrongly ordered in workspace
- elseif max(strcmp(IDs{id}, {'1118'; '1215'; '1124'}))==1
- data = SS_switchChannels_Study_noA1(data); % TP9 and TP10 exchanged manually
- else
- data = SS_switchChannels_Study(data); % TP9(A1) and TP10(FCz) wrongly ordered in workspace
- end
- data = SS_changeChannels(data);
- %%
- % select eye data
- cfg = [];
- cfg.channel = {'TIME', 'L_GAZE_X', 'L_GAZE_Y', ...
- 'L_AREA', 'L_VEL_X', 'L_VEL_Y', 'RES_X', 'RES_Y', 'INPUT'};
- data_eye = ft_preprocessing(cfg, data);
- %%
- % select EEG (non-eye/ECG/trigger) data
- cfg = [];
- cfg.channel = {'all', ...
- '-TIME', '-L_GAZE_X', '-L_GAZE_Y', ...
- '-L_AREA', '-L_VEL_X', '-L_VEL_Y', '-RES_X', '-RES_Y', '-INPUT', '-ECG'};
- data_EEG = ft_preprocessing(cfg, data);
- %%
- % filter & reref EEG data
- cfg = [];
- cfg.continuous = 'yes';
- cfg.demean = 'yes';
- cfg.reref = 'yes';
- cfg.refchannel = {'A1','A2'};
- cfg.implicitref = 'A2';
- cfg.hpfilter = 'yes';
- cfg.hpfreq = .2;
- cfg.hpfiltord = 4;
- cfg.hpfilttype = 'but';
- cfg.lpfilter = 'yes';
- cfg.lpfreq = 125;
- cfg.lpfiltord = 4;
- cfg.lpfilttype = 'but';
- % get data
- data_EEG = ft_preprocessing(cfg, data_EEG);
- flt = cfg;
- % clear cfg structure
- clear cfg
- %% ---- resampling [500 Hz] ---- %%
- % define settings for resampling
- cfg.resamplefs = 500; % raw_eeg (ICA loop) at srate of 250Hz! now resample raw files (original files) to 500 Hz!
- cfg.detrend = 'no';
- cfg.feedback = 'no';
- cfg.trials = 'all';
- % resample ALL data
- data = ft_resampledata(cfg,data);
- if str2num(IDs{id})==1223 & (iRun == 2 | iRun == 3 | iRun == 4)
- disp('Eye data skipped');
- elseif str2num(IDs{id})==1228 & (iRun == 4)
- disp('Eye data skipped');
- else
- data_eye = ft_resampledata(cfg,data_eye);
- end
- data_EEG = ft_resampledata(cfg,data_EEG);
- resample = cfg;
- % clear variables
- clear cfg
- % update config
- config.seg.flt = flt;
- config.seg.resample = resample;
- % save config, data
- save([pn.EEG, IDs{id}, '_r',num2str(iRun), '_', condEEG, '_eyeEEG_Rlm_Fhl_rdSeg'],'data')
- if str2num(IDs{id})==1223 & (iRun == 2 | iRun == 3 | iRun == 4)
- disp('Eye data skipped');
- elseif str2num(IDs{id})==1228 & (iRun == 4)
- disp('Eye data skipped');
- else
- save([pn.EEG, IDs{id}, '_r',num2str(iRun), '_', condEEG, '_eye_Rlm_Fhl_rdSeg'],'data_eye')
- end
- save([pn.EEG, IDs{id}, '_r',num2str(iRun), '_', condEEG, '_EEG_Rlm_Fhl_rdSeg'],'data_EEG')
- save([pn.History, IDs{id}, '_r',num2str(iRun), '_', condEEG, '_config'],'config')
- % clear variables
- clear config data data_eye data_EEG indOnset indOffset mrk mrk_val trl TOI1 TOI2
- else
- end % exist
- end; clear iRun
- %end; clear id
|