Skip to content

PyPREP starts re-referencing with more 'bad' channels than Matlab PREP #76

Closed

Description

@a-hurst

In PyPREP, we do a first pass of NoisyChannels prior to initial average referencing to

a) keep track of what the original channels flagged as "bad" were, and

b) figure out which channels are "unusable" for median-referencing before we start the actual NoisyChannels -> interpolate bads -> re-reference loop.

In PyPREP, we use the bad channels from this initial pass of NoisyChannels as the starting state for the subsequent loop (and once a channel is considered 'bad', it's permanently excluded from being a reference channel during re-referencing). See the last line here, where self.noisy_channels gets initialized with a copy of self.noisy_channels_original:

pyprep/pyprep/reference.py

Lines 216 to 230 in 051edd6

noisy_detector.find_all_bads(**self.ransac_settings)
self.noisy_channels_original = {
"bad_by_nan": noisy_detector.bad_by_nan,
"bad_by_flat": noisy_detector.bad_by_flat,
"bad_by_deviation": noisy_detector.bad_by_deviation,
"bad_by_hf_noise": noisy_detector.bad_by_hf_noise,
"bad_by_correlation": noisy_detector.bad_by_correlation,
"bad_by_SNR": noisy_detector.bad_by_SNR,
"bad_by_dropout": noisy_detector.bad_by_dropout,
"bad_by_ransac": noisy_detector.bad_by_ransac,
"bad_all": noisy_detector.get_bads(),
}
self._extra_info['initial_bad'] = noisy_detector._extra_info
self.noisy_channels = self.noisy_channels_original.copy()

MatPREP, however, only adds the 'unusable' channels (i.e. those bad by NaN, flat, or SNR) to the starting state of permanently bad channels, leaving the deviation, HF noise, correlation, dropout, and RANSAC initial bads empty.

Personally I think MatPREP's behaviour makes more sense here, given that the signal hasn't necessarily been average-referenced coming into PyPREP (e.g. could be referenced to Cz) and the initial average referencing could change the "bad" status for a lot of channels (e.g. if Cz was particularly noisy). In fact, I'd go one further and say that "bad-by-flat" channels from the first NoisyChannels pass shouldn't be permanently excluded either for this same reason: if it's flat because it's a reference channel, it won't be flat anymore after the initial average referencing.

Activity

  1. sappelhoff commented on May 4, 2021

    @sappelhoff
    Owner

    MatPREP, however, only adds the 'unusable' channels (i.e. those bad by NaN, flat, or SNR) to the starting state of permanently bad channels, leaving the deviation, HF noise, correlation, dropout, and RANSAC initial bads empty.

    I see 🤔

    e.g. if Cz was particularly noisy

    actually, if Cz was noisy and the online reference, then no amount of post processing will get the data clean, due to the way the EEG signal is amplified during recording 😞 (signal_elec_i = (elec_i - ground) - (ref - ground))

    But yes -> if we just pick another elec in your example, it makes total sense ... +1 to go into matlab direction here and assume that the data are not necessarily average referenced to begin with (but if they are, that shouldn't be a problem).

    In fact, I'd go one further and say that "bad-by-flat" channels from the first NoisyChannels pass shouldn't be permanently excluded either for this same reason: if it's flat because it's a reference channel, it won't be flat anymore after the initial average referencing.

    Typical EEG datasets do not include the reference channel for that exact reason (it's flat) ... and when changing the reference of the data to e.g., average reference, you need to drop that (no not flat anymore) reference channel to have "full rank" data again (e.g., for ICA) ... I don't think I get the math of that exactly, but you can try it yourself using https://numpy.org/doc/stable/reference/generated/numpy.linalg.matrix_rank.html:

    so that's more or less an "aside" note that adding the reference channel after recording is (at least in my experience) not often done.

    But let's imagine somebody would want to do that:

    • add ref chn (flat) to data -> from 64 chns to 64 + 1 flat (65)
    • pass data through pyprep
    • --> want to get back 65 non-flat channels

    would the ref chn always be bad after pyprep (in the current state of the code), or does it get a chance to "redeem" itself later and get interpolated?

    Overall I'm +1 to fixing pyprep to be like matprep here, and -0.5 to go beyond what matprep does.

  2. added this to the 0.4.0 milestone on May 4, 2021
  3. a-hurst commented on May 4, 2021

    @a-hurst
    CollaboratorAuthor

    Typical EEG datasets do not include the reference channel for that exact reason (it's flat) ... and when changing the reference of the data to e.g., average reference, you need to drop that (no not flat anymore) reference channel to have "full rank" data again

    Ahhhh okay, I think this makes sense: since the channel doesn't actually contain any new information not contained in other channels post-reference, it has nothing useful to contribute. This is another instance of me still being pretty new to the theory and conventions of EEG, so please disregard my extra suggestion here. I'll match behaviour to MATLAB PREP for my PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions