Commit b8d5e7de authored by Ard Kastrati's avatar Ard Kastrati
Browse files

Merge branch 'master' of https://gitlab.ethz.ch/kard/dl-project

parents a33b2f4e adbe285e
clear all
clc
system = 0 %0 (1 = MAC, 0 = Windows, 2=thinkpad martyna)
if system == 1
cd('/Volumes/methlab/Neurometric/Antisaccades/code/eeglab14_1_2b/')
load('/Volumes/methlab/eyepredict/labels105.mat')
eeglab;
close
raw = '/Volumes/methlab/ETH_AS/preprocessed2' % path to preprocessed eeg files
etfolder='/Volumes/methlab/methlab/ETH_AS\EYE'; %et files
%WINDOWS
elseif system == 0 % Windows
cd('\\130.60.169.45\methlab\Neurometric\Antisaccades\code\eeglab14_1_2b')
load('\\130.60.169.45\methlab\eyepredict\labels105.mat')
eeglab;
close
raw= '\\130.60.169.45\methlab\ETH_AS\preprocessed2'; % path to preprocessed eeg files
etfolder='\\130.60.169.45\methlab\ETH_AS\EYE'; %only one file per sbj
end
d=dir(raw) %what folders are in there (each folder = one subject)
d(1:2)=[] % get rid of the . and .. folders as well as .DS_Store on mac/ NOT THE CASE ON WINDOWS
done = 0;
%%
all_eeg_chunks = {};
all_amplitude = {};
all_id = {};
all_phase = {};
all_rel_amplitude = {};
for i=154:370%loop over all subjects
if done >= 370
break
end
if d(i).isdir
subjectfolder=dir([d(i).folder filesep d(i).name ]);
deleteindex=[];
for ii=1:length(subjectfolder)
if not(endsWith(subjectfolder(ii).name, '_EEG.mat')) || startsWith(subjectfolder(ii).name,'bip') || startsWith(subjectfolder(ii).name,'red')
deleteindex(end+1)=ii;
end
end
subjectfolder(deleteindex)=[];
names ={};
for kkk=1:length(subjectfolder)
names{kkk,1} = subjectfolder(kkk).name;
end
clear ind_t ind
for kk = 1:length(subjectfolder)
ind(kk) = ~isempty(strfind(names{kk},['AS']));
end
ind_t = find(ind);
if isempty(ind_t)
continue;
end
load ([subjectfolder(ind_t).folder filesep subjectfolder(ind_t).name]) % gets loaded as EEG
load([etfolder filesep d(i).name filesep d(i).name '_AS_EYE.mat']) %define string of the complete path to the matching ET file.
EEG.id = d(i).name;
%
% %% REDUCE TO 105 electrodes
%
% tbl_channels = struct2table(EEG.chanlocs);
% el_excl = {'E48' 'E49' 'E56' 'E63' 'E68' 'E73' 'E81' 'E88' 'E94' 'E99' 'E107' 'E113' 'E119' 'E1' 'E8' 'E14' 'E17' 'E21' 'E25' 'E32' 'E125' 'E126' 'E127' 'E128'}
%
%
% tbl_excl = table(el_excl');
% tbl_excl.Properties.VariableNames = {'labels'};
%
%
% ind_excl = [];
% for j = 1:size(tbl_excl,1)
% ind_excl(end+1,1) = find(strcmp(tbl_excl.labels(j),[tbl_channels.labels]));
% end
% ind_excl';
%
%
% EEG = pop_select(EEG,'nochannel',ind_excl );
%% filtering and rereferencing
% EEG = pop_eegfiltnew(EEG,0.5,[]);% high pass filter 0.5Hz
EEG = pop_reref(EEG,[]); % reref
orig_nbchan = EEG.nbchan;
EEG.data(EEG.nbchan+1:EEG.nbchan+size(EYE,1),:) = EYE;
EEG.nbchan = EEG.nbchan+size(EYE,1);
% EEG = pop_resample(EEG,200)
%here remove electr from ET and save them as a separate structure
save([d(i).folder filesep d(i).name filesep 'eye.mat'], ...
'EYE', '-v7.3');
%saving ET data TBD
EEG.data = EEG.data(1:orig_nbchan,:)
EEG.nbchan = orig_nbchan ;
% EEG_chunk = {};
% for k=1:28
% EEG_tmp = pop_eegfiltnew(EEG, k, k+2);
% EEG_chunk{k} = struct('id', EEG_tmp.id);
% EEG_chunk{k}.hbert = hilbert(EEG_tmp.data);
% %ampl +phase
% end
% all_eeg_chunks{end+1} = EEG_chunk;
done = done + 1;
eeg_total = pop_eegfiltnew(EEG,1, 30);
eeg_total.hilbert = hilbert(eeg_total.data')';
eeg_total.amplitude = abs(eeg_total.hilbert);
clear phase
clear amplitude
clear rel_amplitude
%%%
for frl = 1:2:28 % or 1:2:30
frh = frl+2;
clear eeg_tmp
eeg_tmp = pop_eegfiltnew(eeg_total,frl, frh);
eeg_tmp.hilbert = hilbert(eeg_tmp.data')';
eeg_tmp.amplitude = abs(eeg_tmp.hilbert);
eeg_tmp.phase = angle(eeg_tmp.hilbert); % phase in radian
eeg_tmp.phase = 180+eeg_tmp.phase*360/2/pi; % phase in degrees (for PAC according to Moritz Köster)
eeg_tmp.rel_amplitude = abs(eeg_tmp.hilbert)./eeg_total.amplitude;
amplitude(frl,:,:) = eeg_tmp.amplitude ;
phase(frl,:,:) = eeg_tmp.phase;
rel_amplitude(frl,:,:) = eeg_tmp.rel_amplitude ;
end
save([d(i).folder filesep d(i).name filesep 'data.mat'], ...
'amplitude', 'phase', 'rel_amplitude', '-v7.3');
% all_amplitude{end+1} = amplitude;
% all_phase{end+1} = phase;
% all_rel_amplitude{end+1} = rel_amplitude;
% all_id{end+1} = EEG.id;
%
end
end
clear all
clc
system = 0 %0 (1 = MAC, 0 = Windows, 2=thinkpad martyna)
if system == 1
cd('/Volumes/methlab/Neurometric/Antisaccades/code/eeglab14_1_2b/')
load('/Volumes/methlab/eyepredict/labels105.mat')
eeglab;
close
raw = '/Volumes/methlab/ETH_AS/preprocessed2' % path to preprocessed eeg files
etfolder='/Volumes/methlab/methlab/ETH_AS\EYE'; %et files
%WINDOWS
elseif system == 0 % Windows
cd('\\130.60.169.45\methlab\Neurometric\Antisaccades\code\eeglab14_1_2b')
load('\\130.60.169.45\methlab\eyepredict\labels105.mat')
eeglab;
close
raw= '\\130.60.169.45\methlab\ETH_AS\preprocessed2'; % path to preprocessed eeg files
etfolder='\\130.60.169.45\methlab\ETH_AS\EYE'; %only one file per sbj
end
d=dir(raw) %what folders are in there (each folder = one subject)
d(1:2)=[] % get rid of the . and .. folders as well as .DS_Store on mac/ NOT THE CASE ON WINDOWS
done = 0;
%%
all_eeg_chunks = {};
all_amplitude = {};
all_id = {};
all_phase = {};
all_rel_amplitude = {};
for i=1:370%loop over all subjects
if done >= 370
break
end
if d(i).isdir
subjectfolder=dir([d(i).folder filesep d(i).name ]);
deleteindex=[];
for ii=1:length(subjectfolder)
if not(endsWith(subjectfolder(ii).name, '_EEG.mat')) || startsWith(subjectfolder(ii).name,'bip') || startsWith(subjectfolder(ii).name,'red')
deleteindex(end+1)=ii;
end
end
subjectfolder(deleteindex)=[];
names ={};
for kkk=1:length(subjectfolder)
names{kkk,1} = subjectfolder(kkk).name;
end
clear ind_t ind
for kk = 1:length(subjectfolder)
ind(kk) = ~isempty(strfind(names{kk},['AS']));
end
ind_t = find(ind);
if isempty(ind_t)
continue;
end
load ([subjectfolder(ind_t).folder filesep subjectfolder(ind_t).name]) % gets loaded as EEG
load([etfolder filesep d(i).name filesep d(i).name '_AS_EYE.mat']) %define string of the complete path to the matching ET file.
EEG.id = d(i).name;
%
% %% REDUCE TO 105 electrodes
%
% tbl_channels = struct2table(EEG.chanlocs);
% el_excl = {'E48' 'E49' 'E56' 'E63' 'E68' 'E73' 'E81' 'E88' 'E94' 'E99' 'E107' 'E113' 'E119' 'E1' 'E8' 'E14' 'E17' 'E21' 'E25' 'E32' 'E125' 'E126' 'E127' 'E128'}
%
%
% tbl_excl = table(el_excl');
% tbl_excl.Properties.VariableNames = {'labels'};
%
%
% ind_excl = [];
% for j = 1:size(tbl_excl,1)
% ind_excl(end+1,1) = find(strcmp(tbl_excl.labels(j),[tbl_channels.labels]));
% end
% ind_excl';
%
%
% EEG = pop_select(EEG,'nochannel',ind_excl );
%% filtering and rereferencing
% EEG = pop_eegfiltnew(EEG,0.5,[]);% high pass filter 0.5Hz
EEG = pop_reref(EEG,[]); % reref
orig_nbchan = EEG.nbchan;
EEG.data(EEG.nbchan+1:EEG.nbchan+size(EYE,1),:) = EYE;
EEG.nbchan = EEG.nbchan+size(EYE,1);
EEG = pop_resample(EEG,200)
a = EEG.data(EEG.nbchan-3:EEG.nbchan,:)
%here remove electr from ET and save them as a separate structure
save([d(i).folder filesep d(i).name filesep '128eyedownsampled200.mat'], ...
'a', '-v7.3');
%saving ET data TBD
EEG.data = EEG.data(1:orig_nbchan,:)
EEG.nbchan = orig_nbchan ;
% EEG_chunk = {};
% for k=1:28
% EEG_tmp = pop_eegfiltnew(EEG, k, k+2);
% EEG_chunk{k} = struct('id', EEG_tmp.id);
% EEG_chunk{k}.hbert = hilbert(EEG_tmp.data);
% %ampl +phase
% end
% all_eeg_chunks{end+1} = EEG_chunk;
done = done + 1;
eeg_total = pop_eegfiltnew(EEG,1, 30);
eeg_total.hilbert = hilbert(eeg_total.data')';
eeg_total.amplitude = abs(eeg_total.hilbert);
clear dphase
clear damplitude
clear drel_amplitude
%%%
for frl = 1:2:28 % or 1:2:30
frh = frl+2;
clear eeg_tmp
eeg_tmp = pop_eegfiltnew(eeg_total,frl, frh);
eeg_tmp.hilbert = hilbert(eeg_tmp.data')';
eeg_tmp.amplitude = abs(eeg_tmp.hilbert);
eeg_tmp.phase = angle(eeg_tmp.hilbert); % phase in radian
eeg_tmp.phase = 180+eeg_tmp.phase*360/2/pi; % phase in degrees (for PAC according to Moritz Köster)
eeg_tmp.rel_amplitude = abs(eeg_tmp.hilbert)./eeg_total.amplitude;
damplitude(frl,:,:) = eeg_tmp.amplitude ;
dphase(frl,:,:) = eeg_tmp.phase;
drel_amplitude(frl,:,:) = eeg_tmp.rel_amplitude ;
end
save([d(i).folder filesep d(i).name filesep '128chandownsampled200data.mat'], ...
'damplitude', 'dphase', 'drel_amplitude', '-v7.3');
% all_amplitude{end+1} = amplitude;
% all_phase{end+1} = phase;
% all_rel_amplitude{end+1} = rel_amplitude;
% all_id{end+1} = EEG.id;
%
end
end
clear all
clc
system = 0 %0 (1 = MAC, 0 = Windows, 2=thinkpad martyna)
if system == 1
cd('/Volumes/methlab/Neurometric/Antisaccades/code/eeglab14_1_2b/')
load('/Volumes/methlab/eyepredict/labels105.mat')
eeglab;
close
raw = '/Volumes/methlab/ETH_AS/preprocessed2' % path to preprocessed eeg files
etfolder='/Volumes/methlab/methlab/ETH_AS\EYE'; %et files
%WINDOWS
elseif system == 0 % Windows
cd('\\130.60.169.45\methlab\Neurometric\Antisaccades\code\eeglab14_1_2b')
load('\\130.60.169.45\methlab\eyepredict\labels105.mat')
eeglab;
close
raw= '\\130.60.169.45\methlab\ETH_AS\preprocessed2'; % path to preprocessed eeg files
etfolder='\\130.60.169.45\methlab\ETH_AS\EYE'; %only one file per sbj
end
d=dir(raw) %what folders are in there (each folder = one subject)
d(1:2)=[] % get rid of the . and .. folders as well as .DS_Store on mac/ NOT THE CASE ON WINDOWS
done = 0;
%%
all_eeg_chunks = {};
all_amplitude = {};
all_id = {};
all_phase = {};
all_rel_amplitude = {};
for i=1:370%loop over all subjects
if done >= 370
break
end
if d(i).isdir
subjectfolder=dir([d(i).folder filesep d(i).name ]);
deleteindex=[];
for ii=1:length(subjectfolder)
if not(endsWith(subjectfolder(ii).name, '_EEG.mat')) || startsWith(subjectfolder(ii).name,'bip') || startsWith(subjectfolder(ii).name,'red')
deleteindex(end+1)=ii;
end
end
subjectfolder(deleteindex)=[];
names ={};
for kkk=1:length(subjectfolder)
names{kkk,1} = subjectfolder(kkk).name;
end
clear ind_t ind
for kk = 1:length(subjectfolder)
ind(kk) = ~isempty(strfind(names{kk},['AS']));
end
ind_t = find(ind);
if isempty(ind_t)
continue;
end
load ([subjectfolder(ind_t).folder filesep subjectfolder(ind_t).name]) % gets loaded as EEG
load([etfolder filesep d(i).name filesep d(i).name '_AS_EYE.mat']) %define string of the complete path to the matching ET file.
EEG.id = d(i).name;
%% REDUCE TO 105 electrodes
tbl_channels = struct2table(EEG.chanlocs);
el_excl = {'E48' 'E49' 'E56' 'E63' 'E68' 'E73' 'E81' 'E88' 'E94' 'E99' 'E107' 'E113' 'E119' 'E1' 'E8' 'E14' 'E17' 'E21' 'E25' 'E32' 'E125' 'E126' 'E127' 'E128'}
tbl_excl = table(el_excl');
tbl_excl.Properties.VariableNames = {'labels'};
ind_excl = [];
for j = 1:size(tbl_excl,1)
ind_excl(end+1,1) = find(strcmp(tbl_excl.labels(j),[tbl_channels.labels]));
end
ind_excl';
EEG = pop_select(EEG,'nochannel',ind_excl );
%% filtering and rereferencing
% EEG = pop_eegfiltnew(EEG,0.5,[]);% high pass filter 0.5Hz
EEG = pop_reref(EEG,[]); % reref
orig_nbchan = EEG.nbchan;
EEG.data(EEG.nbchan+1:EEG.nbchan+size(EYE,1),:) = EYE;
EEG.nbchan = EEG.nbchan+size(EYE,1);
EEG = pop_resample(EEG,200)
a = EEG.data(EEG.nbchan-3:EEG.nbchan,:)
%here remove electr from ET and save them as a separate structure
save([d(i).folder filesep d(i).name filesep 'eyedownsampled200.mat'], ...
'a', '-v7.3');
%saving ET data TBD
EEG.data = EEG.data(1:orig_nbchan,:)
EEG.nbchan = orig_nbchan ;
% EEG_chunk = {};
% for k=1:28
% EEG_tmp = pop_eegfiltnew(EEG, k, k+2);
% EEG_chunk{k} = struct('id', EEG_tmp.id);
% EEG_chunk{k}.hbert = hilbert(EEG_tmp.data);
% %ampl +phase
% end
% all_eeg_chunks{end+1} = EEG_chunk;
done = done + 1;
eeg_total = pop_eegfiltnew(EEG,1, 30);
eeg_total.hilbert = hilbert(eeg_total.data')';
eeg_total.amplitude = abs(eeg_total.hilbert);
clear dphase
clear damplitude
clear drel_amplitude
%%%
for frl = 1:2:28 % or 1:2:30
frh = frl+2;
clear eeg_tmp
eeg_tmp = pop_eegfiltnew(eeg_total,frl, frh);
eeg_tmp.hilbert = hilbert(eeg_tmp.data')';
eeg_tmp.amplitude = abs(eeg_tmp.hilbert);
eeg_tmp.phase = angle(eeg_tmp.hilbert); % phase in radian
eeg_tmp.phase = 180+eeg_tmp.phase*360/2/pi; % phase in degrees (for PAC according to Moritz Köster)
eeg_tmp.rel_amplitude = abs(eeg_tmp.hilbert)./eeg_total.amplitude;
damplitude(frl,:,:) = eeg_tmp.amplitude ;
dphase(frl,:,:) = eeg_tmp.phase;
drel_amplitude(frl,:,:) = eeg_tmp.rel_amplitude ;
end
save([d(i).folder filesep d(i).name filesep 'downsampled200data.mat'], ...
'damplitude', 'dphase', 'drel_amplitude', '-v7.3');
% all_amplitude{end+1} = amplitude;
% all_phase{end+1} = phase;
% all_rel_amplitude{end+1} = rel_amplitude;
% all_id{end+1} = EEG.id;
%
end
end
clc
clear
cd('\\130.60.169.45\methlab\Neurometric\Antisaccades\code\eeglab14_1_2b')
eeglab;
close all
addpath('\\130.60.169.45\methlab\Decoding_workshop\fieldtrip\fieldtrip-20200109\fieldtrip-20200109');
addpath('\\130.60.169.45\methlab\Decoding_workshop\mvpa_light\MVPA-Light-master\MVPA-Light-master\startup');
ft_defaults
startup_MVPA_Light
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)
load EEGprocuelong
load trialinfocuelockedlong %called trialinfopro/trialinfoanti
EEG=EEGprocuelong
%% reorganize data to be compatible with FieldTrip
ftdata.fsample = EEG.srate;
ftdata.label = {EEG.chanlocs(:).labels};
for trl = 1:EEG.trials
ftdata.trial{trl} = EEG.data(:,:,trl);
ftdata.time{trl} = EEG.times./1000;
end
ftdata.trialinfo = trialinfopro.cues(1:trialinfopro.epochs)
cuedata = ftdata;
clear ftdata
%% reference of time freq
% http://www.fieldtriptoolbox.org/tutorial/timefrequencyanalysis/
cfg = [];
cfg.output = 'pow';
cfg.method = 'mtmconvol';
cfg.taper = 'hanning';
cfg.foi = 1:1:40; % analysis 2 to 30 Hz in steps of 2 Hz
cfg.t_ftimwin = ones(length(cfg.foi),1).*1; % length of time window = 1 sec, in order to have 1 Hz freq resolution
cfg.toi = (ftdata.time{1}(1)+cfg.t_ftimwin(1)/2):0.05:(ftdata.time{1}(end)-cfg.t_ftimwin(1)/2); % time window "slides" from -1.5 to 2.5 sec in steps of 0.05 sec (50 ms).
cfg.keeptrials = 'yes';
tfrcue = ft_freqanalysis(cfg, cuedata);
tfrcue.trialinfo = cuedata.trialinfo;
%% do baseline first for cue locked data
cfg = [];
cfg.baseline = [-.8 -.5];
cfg.baselinetype = 'db';
tfrblcue = ft_freqbaseline(cfg,tfrcue);
%%
save tfrcue tfrcue tfrblcue
%% select single trials data
cfg = [];
cfg.latency = [-.8 2];
cfg.frequency = [8 12];% set here the freq of interest
%number of frequency bins as loop
cfg.avgoverfreq = 'yes';
erdcue = ft_selectdata(cfg,tfrblcue);
%% create raw erd data for cue locked data (event related desynchonisation)
for trl = 1:EEG.trials
rawerdcue.trial{trl} = squeeze(erdcue.powspctrm(trl,:,:,:));
rawerdcue.time{trl} = erdcue.time;
end