[Eeglablist] Premature infants preprocessing pipeline with EEGLab tools

Cedric Cannard ccannard at protonmail.com
Wed Aug 7 12:03:56 PDT 2024


Hi,

What do you mean by "accounting for" flat lines if you are not removing them?


Here is a function adapted from the original clean_channels() code of clean_rawdata to detect and remove flat segments. You can adjust to keep them or do whatever you want with them instead of rejecting. The only difficulty is finding the right threshold for your EEG system/signal, it varies depending on active/passive electrodes, amplification, highpass filtering, etc.

Hope this helps.

Cedric

CODE: 

%% Detect flat lines in EEG data, excludes very small segments (< 5 samples),
% plots them in red (superimposed on raw EEG data), and print portion of data 
% that were removed (seconds and ratio).
%
% Inputs:
%      EEG        - EEGLAB dataset
%      flatThresh - threshold to detect flat lines (e.g., 20*eps, 0.05, 0.001)
%      vis        - visualize what has been removed (true) or not (false)
%
% Copyright (C) - Cedric Cannard, 2023

function EEG = rm_flatSegments(EEG,flatThresh,vis)

% Thresholds
if ~exist('flatThresh','var') || isempty(flatThresh)
    % flatThresh = 20*eps;
    flatThresh = 0.05;
end

% Visualization
if ~exist('vis','var') || isempty(vis)
    vis = true;
end

% copy of original dataset for visualization
if vis
    oriEEG = EEG;
end

nChan = EEG.nbchan;
mask = nan(nChan,EEG.pnts+1);
for iChan = 1:nChan
    mask(iChan,:) = [false abs(diff(EEG.data(iChan,:)))<flatThresh false]; % false 1st and last sample to preserve original size
    % mask(iChan,:) = [false isoutlier(abs(diff(EEG.data(iChan,:))),'mean') false];   % this can detect EEG artifacts
end
if nChan == 4
    mask_front = or(mask(2,:),mask(3,:));
    mask_post = or(mask(1,:),mask(4,:));
    mask = or(mask_front, mask_post);
elseif nChan == 3
    flat_mask_tmp = or(mask(1,:),mask(2,:));
    mask = or(mask(3,:),flat_mask_tmp);
elseif nChan == 2
    mask = or(mask(1,:),mask(2,:));
end
mask(end) = [];

% Remove them if present
if sum(mask) > 0
    flatSeg = reshape(find(diff([false mask false])),2,[])';
    flatSeg(:,2) = flatSeg(:,2)-1;

    % Exclude flat segments shorter than 5 samples
    smallIntervals = diff(flatSeg,[],2) <= 5;
    flatSeg(smallIntervals,:) = [];

    % Expand segments because edges that are progressively flat are missed
    if ~isempty(flatSeg)
        flatSeg(:,1) = flatSeg(:,1) - floor(EEG.srate/4);
        flatSeg(:,2) = flatSeg(:,2) + floor(EEG.srate/4);
        if flatSeg(1,1) < 0
            flatSeg(1,1) = 1;
        end
    end

    % Update flat mask for vis_artifacts
    mask = false(1,EEG.pnts);
    for iSeg = 1:size(flatSeg,1)
        lowBound = flatSeg(iSeg,1);
        highBound = flatSeg(iSeg,2);
        mask(lowBound:highBound) = true;
    end

    % Calculate total length and ratio removed (before removing them)
    flatSamples = sum(diff(flatSeg,[],2));
    flatSegRatio = round(flatSamples/EEG.pnts*100,1);
    % flatSegSec = round(flatSamples/EEG.srate,1);

    % Remove from data
    EEG = pop_select(EEG,'nopoint',flatSeg);

    % Visualize
    if vis
        EEG.etc.clean_sample_mask = ~mask;
        vis_artifacts(EEG,oriEEG);
    end

else
    flatSegRatio = 0;
    % flatSegSec = 0;
end

% Clear mask to avoid interference with ASR later
if isfield(EEG.etc,'clean_sample_mask')
    EEG.etc = rmfield(EEG.etc,'clean_sample_mask');
end
EEG = eeg_checkset(EEG);

% Print what was removed
% fprintf("%g %% were flat segments that were removed (%g min). \n", flatSegRatio, flatSegSec/60)
fprintf("%g %% were flat segments that were removed. \n", flatSegRatio)






On Wednesday, August 7th, 2024 at 2:22 AM, Marco Buiatti via eeglablist <eeglablist at sccn.ucsd.edu> wrote:

> Hi Efthymios,
>
> I think it would be feasible to avoid the removal of flat lines in NEAR. I
> put you in contact with Velu Kumaravel (cc), the developer of NEAR, maybe
> he can help you.
>
> Best,
>
> Marco
>
> On Mon, 5 Aug 2024 at 11:09, Efthymios Papatzikis efp331 at mail.harvard.edu
>
> wrote:
>
> > Dear Marco,
> >
> > Thank you for this email. My inquiry still stands indeed, although I have
> > managed since then to create an analysis and statistical preprocessing
> > configuration of steps for my specific needs.
> >
> > Thank you for sharing your pipeline, though. Very informative and
> > particularly the ASR configuration (TuneASR) part. I will definitely study
> > this as it will provide me valuable input. However, I see your pipeline
> > does exactly what I am trying to avoid; that is, reject flat lines instead
> > of accounting for them in the preprocessing pipeline. This is a main
> > characteristic (i.e., continuity/discontinuity ratio) of premature
> > infants that I am working with in my project, and all along, from the very
> > beginning, I was trying to find a pipeline pre-configured for that. I
> > couldn’t, so I tried to create mine. Still needs some fine-tuning of
> > course, but now it seems to do what is meant for.
> >
> > Happy to discuss more if you are interested.
> >
> > Best,
> > Efthymios
> >
> > On 5 Aug 2024, at 10:47, Marco Buiatti marco.buiatti at unitn.it wrote:
> >
> > Dear Efthymios,
> >
> > If your request is still ongoing, I suggest you to also consider NEAR, our
> > pipeline for artifact removal in newborn and infant EEG data, available as
> > an EEGLAB plugin:
> > https://urldefense.com/v3/__https://github.com/vpKumaravel/NEAR__;!!Mih3wA!D57UmdQYinCHOCnNcTXyb5ed0ro9PGfYVitKTCNZMOxsrMzHF4nV8z_xZrPuVwvGXsXm0AarWKpzM6vc4U4rkhRaFT3hO6A$
> >
> > Best,
> >
> > Marco
> >
> > On Tue, 19 Mar 2024 at 22:41, Dr. Efthymios Papatzikis via eeglablist <
> > eeglablist at sccn.ucsd.edu> wrote:
> >
> > > Dear List,
> > >
> > > Is there anyone who has experience with EEGLab on data from premature
> > > infants, and can suggest a preprocessing pipeline or relevant published
> > > articles where EEGLab tools have been used to preprocess the data?
> > >
> > > Any suggestions more than welcome.
> > >
> > > Dr. Efthymios Papatzikis
> > > Infant Brain Development
> > > Oslo Metropolitan University, Norway
> > >
> > > _______________________________________________
> > > Eeglablist page: http://sccn.ucsd.edu/eeglab/eeglabmail.html
> > > To unsubscribe, send an empty email to
> > > eeglablist-unsubscribe at sccn.ucsd.edu
> > > For digest mode, send an email with the subject "set digest mime" to
> > > eeglablist-request at sccn.ucsd.edu
> >
> > --
> > Marco Buiatti
> >
> > Baby Lab & Neonatal Neuroimaging Unit
> > Center for Mind/Brain Sciences
> > University of Trento,
> > Piazza della Manifattura 1, 38068 Rovereto (TN), Italy
> > E-mail: marco.buiatti at unitn.it
> > Phone: +39 0464-808178
> > https://urldefense.com/v3/__https://sites.google.com/a/unitn.it/marcobuiatti/__;!!Mih3wA!D57UmdQYinCHOCnNcTXyb5ed0ro9PGfYVitKTCNZMOxsrMzHF4nV8z_xZrPuVwvGXsXm0AarWKpzM6vc4U4rkhRau70QE1E$
> > https://urldefense.com/v3/__https://r1.unitn.it/babylab/__;!!Mih3wA!D57UmdQYinCHOCnNcTXyb5ed0ro9PGfYVitKTCNZMOxsrMzHF4nV8z_xZrPuVwvGXsXm0AarWKpzM6vc4U4rkhRaZ00Mp7g$
> > ***********************************************
>
>
> --
> Marco Buiatti
>
> Baby Lab & Neonatal Neuroimaging Unit
> Center for Mind/Brain Sciences
> University of Trento,
> Piazza della Manifattura 1, 38068 Rovereto (TN), Italy
> E-mail: marco.buiatti at unitn.it
> Phone: +39 0464-808178
> https://urldefense.com/v3/__https://sites.google.com/a/unitn.it/marcobuiatti/__;!!Mih3wA!D57UmdQYinCHOCnNcTXyb5ed0ro9PGfYVitKTCNZMOxsrMzHF4nV8z_xZrPuVwvGXsXm0AarWKpzM6vc4U4rkhRau70QE1E$
> https://urldefense.com/v3/__https://r1.unitn.it/babylab/__;!!Mih3wA!D57UmdQYinCHOCnNcTXyb5ed0ro9PGfYVitKTCNZMOxsrMzHF4nV8z_xZrPuVwvGXsXm0AarWKpzM6vc4U4rkhRaZ00Mp7g$
> ***********************************************
> _______________________________________________
> Eeglablist page: http://sccn.ucsd.edu/eeglab/eeglabmail.html
> To unsubscribe, send an empty email to eeglablist-unsubscribe at sccn.ucsd.edu
> For digest mode, send an email with the subject "set digest mime" to eeglablist-request at sccn.ucsd.edu


More information about the eeglablist mailing list