[FieldTrip] Baseline removal errors in trials preprocessing

Marion Vincent marion.vincent at univ-lille.fr
Thu Jan 17 17:59:44 CET 2019


An HTML attachment was scrubbed...
URL: <http://mailman.science.ru.nl/pipermail/fieldtrip/attachments/20190117/7931247b/attachment.html>
-------------- next part --------------
Dear Stephen,
Thanks for your comments.
In fact I only used the cgf1, cfg2.. for plotting the figure I sent you. So it was not my «real» code.
But you’re rigth cleaning the cfg will prevent some errors.
I’ve done so and still have some errors: (for each case, I compared it to a personnal calculation)
If I use the ‘all’ default cfg.baselinewindow => I have the result I expected: the Baseline is the mean value of all the signal. (cf. figure Baseline_AllSignal)If I use a given time window (here from 0.1 to 0 sec before the stimulsu onset) => in fieldtrip, the Baseline is only the «first point» of the raw signal, but not its mean value on the Baseline window (as in my own calculation: cf. figure Baseline_TimeWindow). I also tried with exorbitant window [-10000 0], [0 0.1], etc ) and each time I Always have the exact same result for the filedtrip calculation.
Here is my entire code(I hope it will be clearer):
**************************************************************
seg_window=[0.1 1]; % in seconds %preStim/postStim
trig_Type=[1 5 2 6 3 7 4 8]; % trigger value
load(filename_Trig);
% ** READ FILE INFO
% read the header information and the events from the data
hdr   = ft_read_header(filename_Data);
% Config
cfg= [];
cfg.dataset       = filename_Data;
% **RE-REFERENCES
cfg.reref         = 'yes';
cfg.refchannel    = {'EXG3', 'EXG4'} ; % electrode 131/132 // cell-array with new EEG reference channel(s), this can be 'all' for a common average reference
cfg.refmethod     = 'avg'; % 'avg', 'median', or 'bipolar' for bipolar derivation of sequential channels (default = 'avg')
% ** TRIAL DEFINITION
event_All = ft_read_event(filename_Data);
% Select only the trigger codes, not the battery and CMS status
sel = find(strcmp({event_All.type}, 'STATUS'));
event_All = event_All(sel);
cfg.eventType=[trig_Type(1) trig_Type(2)];
cfg.trialfun = 'AVExp_trialfun';
cfg.trialdef.pre  = seg_window(1);
cfg.trialdef.post = seg_window(2);
cfg.Fs=hdr.Fs; % convert seconds in sample
cfg = ft_definetrial(cfg);
% Remove trials from Cartool selection
cfg.trl(find(Triggers{num_Cond}(:,1)==0),:) = [];
data_Raw = ft_preprocessing(cfg);
% Plot data to check
figure ; chan_num=1;
trial_num=1;
figure;
plot(data_Raw.trial{trial_num}(chan_num,:));
% ** BASELINE CORRECTION: BASELINE IS ALL THE SIGNAL
%baseline correct the raw data structure, automatic
cfg                     = [] ;
cfg.demean              = 'yes';
cfg.baselinewindow      = 'all';
data_Raw_reref        = ft_preprocessing(cfg, data_Raw);
% Manual correction test
for trial_num=1:length(data_Raw.trial)
    for chan_num=1:length(data_Raw.label)
        Signal=data_Raw.trial{trial_num}(chan_num,:);
        baseline_value=mean(Signal); % for comparison with the default value of
        data_Raw_reref_Manual.trial{trial_num}(chan_num,:)=data_Raw.trial{trial_num}(chan_num,:)-baseline_value*ones(1,length(Signal));
    end
end
% plot comparison
figure;chan_num=1;
trial_num=1;
hold all
plot(data_Raw_reref.trial{trial_num}(chan_num,:));
plot(data_Raw_reref_Manual.trial{trial_num}(chan_num,:));
legend('FieldTrip','Manual');
title('Baseline is all the signal');
% ** BASELINE CORRECTION: BASELINE IS THE MEAN VALUE OF A WINDOW of 0.1sec before the stimulus onset
%baseline correct the raw data structure, automatic
cfg                     = [] ;
cfg.demean              = 'yes';
cfg.baselinewindow      = [-0.1 0] ; %as the baseline lasts 0.1 s  before the stimulus onset
data_Raw_reref_1       = ft_preprocessing(cfg, data_Raw);
% Manual correction test for trial 1 - comparison to automatic calculation
for trial_num=1:length(data_Raw.trial)
    for chan_num=1:length(data_Raw.label)
        Signal=data_Raw.trial{trial_num}(chan_num,:);
        baseline_window=[1  round(hdr.Fs*0.1)];% because baseline lasts 0.1 s  before the stimulus onset
        baseline_value=mean(Signal(baseline_window(1):baseline_window(2)));
        data_Raw_reref_Manual_1.trial{trial_num}(chan_num,:)=data_Raw.trial{trial_num}(chan_num,:)-baseline_value*ones(1,length(Signal));
    end
end
% plot comparison
figure;chan_num=1;
trial_num=1;
hold all
plot(data_Raw_reref_1.trial{trial_num}(chan_num,:));
plot(data_Raw_reref_Manual_1.trial{trial_num}(chan_num,:));
legend('FieldTrip','Manual');
title('Baseline is a given time window’);
**************************************************************
I’m sure it’s a silly mistake, but I really can’t find it.
Thanks
Marion
Marion VINCENT
Eng., PhD , CNRS Research Engineer
Tel: +33 607 59 46 76
Laboratoire SCALab UMR CNRS 9193
Université Lille 3
BP 60149
59653 Villeneuve d'Ascq Cedex
http://scalab.cnrs.fr
--------------------------------------------
L’Imaginarium / SCV-IrDIVE Equipex
99a Boulevard Descat
59200 Tourcoing
http://www.irdive.fr/
De: Stephen Whitmarsh
Envoyé le:jeudi 17 janvier 2019 15:03
À: 'FieldTrip discussion list'
Objet:Re: [FieldTrip] Baseline removal errors in trials preprocessing


Dear Marion,

 

Your script is a bit messy. I would start with:

1)      Always clear your cfg before calling a FT function. Don’t go around saving different cfg’s. This is the nr. 1 cause of confusing results J

2)      Load/segment etc. your data first with ft_preprocessing. Then call ft_preprocessing again to baseline correct, entering the output of the first run.

I have a hunch that might fix your problem. So, something like:

 

% load your data, apply rereferencing and segment in one go

cfg               = [];

cfg.dataset       = filename_Data;

cfg.reref         = 'yes';

cfg.refchannel    = {'EXG3', 'EXG4'} ; 

cfg.refmethod     = 'avg';

cfg.trl           = … ;

cfg               = ft_definetrial(cfg);

data_Raw          = ft_preprocessing(cfg);

 

% somehow plot your data to check

figure ; plot(data_Raw.trial{1}(1, :)) ; 

 

% baseline correct your raw data structure, method 1

cfg                     = [] ;

cfg.demean              = ‘yes’ ;

cfg.baselinewindow      = [0 0.1] ;

data_Raw_reref_1        = ft_preprocessing(cfg, data_Raw);

 

% somehow plot your data to check

figure ; plot(data_Raw_reref_1.trial{1}(1, :)) ; 

 

% baseline correct your raw data structure, method 2

cfg                     = [] ;

cfg.demean              = ‘yes’ ;

cfg.baselinewindow      = [-1 -0.1] ; % or whatever

data_Raw_reref_2        = ft_preprocessing(cfg, data_Raw);

 

% somehow plot your data to check

figure ; plot(data_Raw_reref_2.trial{1}(1, :)) ; 

 

And oh, wait, I see what you did there… Don’t use different variables for your cfg, (e.g. cfg1, cfg2, etc.). See the mistake? Those are too easy to make if you don’t do something like the above.

 

Cheers and good FieldTripping!

Stepen

 

 

 

From: fieldtrip <fieldtrip-bounces at science.ru.nl> On Behalf Of Marion Vincent
Sent: Thursday, January 17, 2019 2:00 PM
To: FieldTrip discussion list <fieldtrip at science.ru.nl>
Subject: Re: [FieldTrip] Baseline removal errors in trials preprocessing

 

Dear Stephen, 

 

Here is my script : 

 

%segmentation

seg_window=[0.1 1]; % in seconds %preStim/postStim

 

%config

cfg= [];

cfg.dataset = filename_Data;

 

%% *********** TRIAL DEFINITION PART *********** (that works )

[…]

cfg = ft_definetrial(cfg);

 

% ***********  Re-References *********** (that also works )

cfg.reref = 'yes';

cfg.refchannel = {'EXG3', 'EXG4'};% electrode 131/132 // cell-array with new EEG reference channel(s), this can be 'all' for a common average reference

cfg.refmethod     = 'avg'; % 'avg', 'median', or 'bipolar' for bipolar derivation of sequential channels (default = 'avg') 

 

data_Raw = ft_preprocessing(cfg);

 

%% *********** Baseline ***********

cfg.demean = 'yes';

 

cfg1=cfg;

cfg1.baselinewindow = [-seg_window(1) 0 ];

cfg2=cfg;

cfg2.baselinewindow = [0 0.1];

cfg3=cfg;

cfg3.baselinewindow = 'all';

 

data1 = ft_preprocessing(cfg1);

data2 = ft_preprocessing(cfg2);

data3 = ft_preprocessing(cfg3);

 

 

ð  I’ve attached the figure with the results.

 

PS : In fact I made a mistake in my previous email : the result wasn’t the same when the Baseline window was ‘all’.

 

Let me know if you need more informations. 

Thanks for your help.

 

Marion

 

 

Marion VINCENT
Eng., PhD , CNRS Research Engineer
Tel: +33 607 59 46 76

Laboratoire SCALab UMR CNRS 9193
Université Lille 3
BP 60149
59653 Villeneuve d'Ascq Cedex
http://scalab.cnrs.fr
--------------------------------------------
L’Imaginarium / SCV-IrDIVE Equipex
99a Boulevard Descat
59200 Tourcoing
http://www.irdive.fr <http://www.irdive.fr/> /

 

De : Stephen Whitmarsh <mailto:stephen.whitmarsh at gmail.com> 
Envoyé le :jeudi 17 janvier 2019 11:51
À : 'FieldTrip discussion list' <mailto:fieldtrip at science.ru.nl> 
Objet :Re: [FieldTrip] Baseline removal errors in trials preprocessing

 

 

Dear Marion,

 

It sounds it might indeed be something as simple as a typo. Could you paste the part of your script – from baseline removal to plotting (where you don’t see a difference) - so we can take a look?

 

Cheers,

Stephen

 

From: fieldtrip <fieldtrip-bounces at science.ru.nl <mailto:fieldtrip-bounces at science.ru.nl> > On Behalf Of Marion Vincent
Sent: Thursday, January 17, 2019 10:37 AM
To: fieldtrip at science.ru.nl <mailto:fieldtrip at science.ru.nl> 
Subject: [FieldTrip] Baseline removal errors in trials preprocessing

 

Dear FIeldtrip users, 

 

I’m new in using Fieldtrip for EEG processing and I’ve encountred some issues while removing the Baseline of my trials. 

 

I’m working on EEG signals recorded with the BioSemi system.  My pre-processing method is : 

(1)    Re-referecing the channels (necessary with BioSemi)

(2)    Trial definition

(3)    Baseline removal

 

I’ve read on the documentation that ft_preprocessing xcan have several cfg parameter for the Baseline removal :

 

% cfg.demean  = 'yes';%'no' or 'yes', whether to apply baseline correction (default = 'no')

% cfg.baselinewindow = 'all';%[begin end] ;%in seconds, the default is the complete trial (default = 'all')

% cfg.detrend       = 'no'; %'no' or 'yes', remove linear trend from the data (done per trial) (default = 'no')

 

I wanted to remove a Baseline defined as the average of a given window preceeding my stimulus onset. 

 

I tried to use : cfg.baselinewindow = [-0.5 0 ]; cfg.baselinewindow = [0 0.1] or the default window .

But the problem is that I always have the same result, no matter the window I use : the first point of my trial is put to zero and substracted to all my trial. 

 

I’m sure I’m doing something wrong (and that the answer is very simple), but I can’t manage to understand what is the issue.

 

Do you have any advice ?

 

Thanks ! 

Marion

 

Marion VINCENT
Eng., PhD , CNRS Research Engineer

 



_______________________________________________
fieldtrip mailing list
https://mailman.science.ru.nl/mailman/listinfo/fieldtrip
https://doi.org/10.1371/journal.pcbi.1002202
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Baseline_AllSignal.png
Type: image/png
Size: 21466 bytes
Desc: not available
URL: <http://mailman.science.ru.nl/pipermail/fieldtrip/attachments/20190117/7931247b/attachment.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Baseline_TimeWindow.png
Type: image/png
Size: 69724 bytes
Desc: not available
URL: <http://mailman.science.ru.nl/pipermail/fieldtrip/attachments/20190117/7931247b/attachment-0001.png>


More information about the fieldtrip mailing list