Commit 7cd82809 authored by Martynka's avatar Martynka
Browse files

feature extraction stage

parent 208d8eec
clc
clear
cd('\\130.60.169.45\methlab\Neurometric\Antisaccades\code\eeglab14_1_2b')
eeglab;
close all
x = dir('\\130.60.169.45\methlab\ETH_AS\preprocessed2')
subjects = {x.name};
subjects = {subjects{4:end-3}}';
clear x
cd('\\130.60.169.45\methlab\ETH_AS')
%%
for subj = 347:length(subjects) %186 - BA5 didnt work, 346- BY2
datapath = strcat('\\130.60.169.45\methlab\ETH_AS\preprocessed2\',subjects{subj});
cd (datapath)
if exist(strcat('gip_',subjects{subj},'_AS_EEG.mat')) > 0
datafile= strcat('gip_',subjects{subj},'_AS_EEG.mat');
load (datafile)
elseif exist(strcat('oip_',subjects{subj},'_AS_EEG.mat')) > 0
datafile= strcat('oip_',subjects{subj},'_AS_EEG.mat');
load (datafile)
end
%% Re-reference to average reference
EEG = pop_reref(EEG,[])
%% triggers renaming
countblocks = 1;
for e = 1:length(EEG.event)
if strcmp(EEG.event(e).type, 'boundary')
countblocks = countblocks + 1;
continue;
end
if countblocks == 2 || countblocks == 3 || countblocks == 4 % antisaccade blocks
if strcmp(EEG.event(e).type,'10 ') % change 10 to 12 for AS
EEG.event(e).type = '12 ';
elseif strcmp(EEG.event(e).type,'11 ')
EEG.event(e).type = '13 '; % change 11 to 13 for AS
end
if strcmp(EEG.event(e).type,'40 ')
EEG.event(e).type = '41 ';
end
end
end
EEG.event(strcmp('boundary',{EEG.event.type})) = [];
rmEventsIx = strcmp('L_fixation',{EEG.event.type});
rmEv = EEG.event(rmEventsIx);
EEG.event(rmEventsIx) = [];
EEG.event(1).dir = []; %left or right
EEG.event(1).cond = [];%pro or anti
%% rename EEG.event.type
previous = '';
for e = 1:length(EEG.event)
if strcmp(EEG.event(e).type, 'L_saccade')
if strcmp(previous, '10 ')
EEG.event(e).type = 'saccade_pro_left'
EEG.event(e).cond = 'pro';
EEG.event(e).dir = 'left';
%pro left
elseif strcmp(previous, '11 ')
EEG.event(e).type = 'saccade_pro_right'
EEG.event(e).cond = 'pro';
EEG.event(e).dir = 'right';
elseif strcmp(previous, '12 ')
EEG.event(e).type = 'saccade_anti_left'
EEG.event(e).cond = 'anti';
EEG.event(e).dir = 'left';
elseif strcmp(previous, '13 ')
EEG.event(e).type = 'saccade_anti_right'
EEG.event(e).cond = 'anti';
EEG.event(e).dir = 'right';
else
EEG.event(e).type = 'invalid';
end
end
if ~strcmp(EEG.event(e).type, 'L_fixation') ...
&& ~strcmp(EEG.event(e).type, 'L_blink')
previous = EEG.event(e).type;
end
end
%% remove everything from EEG.event which is not saccade or trigger
tmpinv=find(strcmp({EEG.event.type}, 'invalid') | strcmp({EEG.event.type}, 'L_blink'))
EEG.event(tmpinv)=[]
%% removing errors
% if 10 and the sub didn't look left then error
% pro left sac_start_x > sac_endpos_x --> correct condition
tmperrsacc1=find(strcmp({EEG.event.type}, 'saccade_pro_left') & [EEG.event.sac_startpos_x]< [EEG.event.sac_endpos_x])
tmperr1=[tmperrsacc1 (tmperrsacc1-1)]
EEG.event(tmperr1)=[];
tmperrsacc2=find(strcmp({EEG.event.type}, 'saccade_anti_left') & [EEG.event.sac_startpos_x]> [EEG.event.sac_endpos_x])
tmperr2=[tmperrsacc2 (tmperrsacc2-1)]
EEG.event(tmperr2)=[];
tmperrsacc3=find(strcmp({EEG.event.type}, 'saccade_pro_right') & [EEG.event.sac_startpos_x]> [EEG.event.sac_endpos_x])
tmperr3=[tmperrsacc3 (tmperrsacc3-1)]
EEG.event(tmperr3)=[];
tmperrsacc4=find(strcmp({EEG.event.type}, 'saccade_anti_right') & [EEG.event.sac_startpos_x]< [EEG.event.sac_endpos_x])
tmperr4=[tmperrsacc4 (tmperrsacc4-1)]
EEG.event(tmperr4)=[];
tmperrsacc6=find(strcmp({EEG.event.type}, 'saccade_pro_right') ...
& [EEG.event.sac_amplitude]<1.5)
tmperrsacc7=find(strcmp({EEG.event.type}, 'saccade_pro_left') ...
& [EEG.event.sac_amplitude]<1.5)
tmperrsacc8=find(strcmp({EEG.event.type}, 'saccade_anti_left') ...
& [EEG.event.sac_amplitude]<1.5)
tmperrsacc9=find(strcmp({EEG.event.type}, 'saccade_anti_right') ...
& [EEG.event.sac_amplitude]<1.5)
tmperr69=[tmperrsacc6 (tmperrsacc6-1) tmperrsacc7 (tmperrsacc7-1) tmperrsacc8 (tmperrsacc8-1) tmperrsacc9 (tmperrsacc9-1)]
EEG.event(tmperr69)=[];
clear tmperrsacc1 tmperrsacc2 tmperrsacc3 tmperrsacc4 tmperrsacc6 tmperrsacc7 tmperrsacc8 tmperrsacc9
%% delete cues where there was no saccade afterwards
tmpevent=length(EEG.event)
%start with pro left cue 10
tmperrsacc10=find(strcmp({EEG.event.type}, 'saccade_pro_left'))
tmperrcue10= find(strcmp({EEG.event.type}, '10 '))
for iii=1:tmpevent
try
while tmperrcue10(iii)~=tmperrsacc10(iii)-1
EEG.event(tmperrcue10(iii))=[];
tmperrsacc10=find(strcmp({EEG.event.type}, 'saccade_pro_left'))
tmperrcue10= find(strcmp({EEG.event.type}, '10 '))
% end
end
catch
end
end
% do for pro right cue 11
tmperrsacc11=find(strcmp({EEG.event.type}, 'saccade_pro_right'))
tmperrcue11=find(strcmp({EEG.event.type}, '11 '))
for iii=1:tmpevent
try
while tmperrcue11(iii)~=tmperrsacc11(iii)-1
% if tmperrcue11(iii)~=tmperrsacc11(iii)-1
EEG.event(tmperrcue11(iii))=[];
tmperrsacc11=find(strcmp({EEG.event.type}, 'saccade_pro_right'))
tmperrcue11= find(strcmp({EEG.event.type}, '11 '))
end
catch
end
end
% do for anti right cue 13
tmperrsacc13=find(strcmp({EEG.event.type}, 'saccade_anti_right'))
tmperrcue13= find(strcmp({EEG.event.type}, '13 '))
for iii=1:tmpevent
try
while tmperrcue13(iii)~=tmperrsacc13(iii)-1
EEG.event(tmperrcue13(iii))=[];
tmperrsacc13=find(strcmp({EEG.event.type}, 'saccade_anti_right'))
tmperrcue13= find(strcmp({EEG.event.type}, '13 '))
end
catch
end
end
%do for anti left cue 12
tmperrsacc12=find(strcmp({EEG.event.type}, 'saccade_anti_left'))
tmperrcue12=find(strcmp({EEG.event.type}, '12 '))
for iii=1:tmpevent
try
while tmperrcue12(iii)~=tmperrsacc12(iii)-1
EEG.event(tmperrcue12(iii))=[];
tmperrsacc12=find(strcmp({EEG.event.type}, 'saccade_anti_left'))
tmperrcue12=find(strcmp({EEG.event.type}, '12 '))
end
catch
end
end
clear tmperrsacc10 tmperrsacc11 tmperrsacc13 tmperrsacc12
%% delete saccades and cues when the saccade comes faster than 100ms after cue
tmpevent=length(EEG.event)
saccpro=find(strcmp({EEG.event.type},'saccade_pro_right')==1 | strcmp({EEG.event.type},'saccade_pro_left')==1)% find rows where there is a saccade
saccanti=find(strcmp({EEG.event.type},'saccade_anti_right')==1 | strcmp({EEG.event.type},'saccade_anti_left')==1);%find rows where there is a saccade
for b=1:size(saccpro,2)
if (EEG.event(saccpro(1,b)).latency-EEG.event(saccpro(1,b)-1).latency)<50 %50 because 100ms
EEG.event(saccpro(b)).type='micro'; %saccade
EEG.event(saccpro(b)-1).type = 'micro'; %cue
end
end
for b=1:size(saccanti,2)
if (EEG.event(saccanti(b)).latency-EEG.event(saccanti(1,b)-1).latency)<50;
EEG.event(saccanti(b)-1).type ='micro';
EEG.event(saccanti(b)).type ='micro';
end
end
tmpinv=find(strcmp({EEG.event.type}, 'micro')) ;
EEG.event(tmpinv)=[];
%% reaction time
saccpro=find(strcmp({EEG.event.type},'saccade_pro_right')==1 | strcmp({EEG.event.type},'saccade_pro_left')==1)% find rows where there is a saccade
saccanti=find(strcmp({EEG.event.type},'saccade_anti_right')==1 | strcmp({EEG.event.type},'saccade_anti_left')==1);%find rows where there is a saccade
trialinfoprosaccadelocked.reactsacc=[]
trialinfoantisaccadelocked.reactsacc=[]
for iii=1:length(EEG.event)
try
tmpreactsaccpro=(EEG.event(saccpro(1,iii)-1).latency-EEG.event(saccpro(1,iii)).latency)*2
trialinfoprosaccadelocked.reactsacc=[trialinfoprosaccadelocked.reactsacc tmpreactsaccpro]
tmpreactsaccantisaccadelocked=(EEG.event(saccanti(1,iii)-1).latency-EEG.event(saccanti(1,iii)).latency)*2
trialinfoantisaccadelocked.reactsacc=[trialinfoantisaccadelocked.reactsacc tmpreactsaccantisaccadelocked]
catch
end
end
%
tmp=find(strcmp({EEG.event.type}, '11 ') | strcmp({EEG.event.type}, '10 '))
right= find(strcmp({EEG.event(tmp).type},'11 ')==1);
left= find(strcmp({EEG.event(tmp).type},'10 ')==1);
trialinfopro.cues = nan(length(tmp),1);
trialinfopro.cues(left)= 1;
trialinfopro.cues(right)= 2;
tmp1=find(strcmp({EEG.event.type}, '12 ') | strcmp({EEG.event.type}, '13 '))
right= find(strcmp({EEG.event(tmp1).type},'12 ')==1);
left= find(strcmp({EEG.event(tmp1).type},'13 ')==1);
trialinfoanti.cues = nan(length(tmp1),1);
trialinfoanti.cues(left)= 1;
trialinfoanti.cues(right)= 2;
%% how many saccades in pro and antisaccade task for epoching to saccades
indsaccadepro = find(strcmp({EEG.event.type},'saccade_pro_left')==1 | strcmp({EEG.event.type},'saccade_pro_right')==1);
indsaccadeanti = find(strcmp({EEG.event.type},'saccade_anti_left')==1 | strcmp({EEG.event.type},'saccade_anti_right')==1);
% do pro
leftsaccpro= find(strcmp({EEG.event(indsaccadepro).type},'saccade_pro_left')==1);
rightsaccpro = find(strcmp({EEG.event(indsaccadepro).type},'saccade_pro_right')==1);
trialinfoprosaccadelocked.saccades = nan(length(indsaccadepro),1);
trialinfoprosaccadelocked.saccades(leftsaccpro)= 1;
trialinfoprosaccadelocked.saccades(rightsaccpro)= 2;
% do anti
leftsaccanti = find(strcmp({EEG.event(indsaccadeanti).type},'saccade_anti_left')==1);
rightsaccanti = find(strcmp({EEG.event(indsaccadeanti).type},'saccade_anti_right')==1);
trialinfoantisaccadelocked.saccades = nan(length(indsaccadeanti),1);
trialinfoantisaccadelocked.saccades(leftsaccanti)= 1;
trialinfoantisaccadelocked.saccades(rightsaccanti)= 2;
%% epoching
EEGprocuelong = pop_epoch(EEG, {'10','11'}, [-2, 3]);
EEGanticuelong = pop_epoch(EEG, {'12','13'}, [-2, 3]);
%how many epochs
trialinfoprolong.epochs=size(EEGprocuelong.data, 3)
% trialinfoanti.epochs=size(EEGanticue.data, 3)
% trialinfoprosaccadelocked.leftepochs=size(EEGprosaccleft.data, 3)
% trialinfoprosaccadelocked.rightepochs=size(EEGprosaccright.data, 3)
% trialinfoprosaccadelocked.epochstotal=size(EEGprosacc.data, 3)
% trialinfoantisaccadelocked.leftepochs=size(EEGantisaccleft.data, 3)
% trialinfoantisaccadelocked.rightepochs=size(EEGantisaccright.data, 3)
% trialinfoantisaccadelocked.epochstotal=size(EEGantisacc.data, 3)
%% save epoched data
save EEGprocuelong EEGprocuelong
% save EEGanticue EEGanticue
% save EEGprosacc EEGprosacc
% save EEGantisacc EEGantisacc
% save EEGprosacc EEGprosaccleft EEGprosaccright EEGprosacc
% save EEGantisacc EEGantisaccleft EEGantisaccright EEGantisacc
% save trialinfo trialinfopro trialinfoanti
save trialinfocuelockedlong trialinfoprolong %trialinfoanti
% save trialinfosaccadelocked trialinfoprosaccadelocked trialinfoantisaccadelocked
end
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment