Inputs for automated bad channel detection

Hi,

What is the goto method for automated bad channel detection with mne? I can see work in progress on pyrep (GitHub - sappelhoff/pyprep: A Python implementation of the Preprocessing Pipeline (PREP) for EEG data). Is there a documentation page in mne suggesting this and other alternatives with some example.

I appreciate your reply. Thank you.

I don’t think there are any tutorials/examples for automatic bad channel detection with MNE-only.
I’ve been using PyPREP to suggest bad channels for a while now, and it works very well.

Here is an example where I chose to apply a BP filter between [1, 45] Hz before; but maybe other users will have different opinions.

import pyprep
import numpy as np


# Until 0.4 release, make sure to use the development version.
if '0.3.1' in pyprep.__version__:
    assert pyprep.__version__.split('0.3.1')[1] != ''
else:
    assert 4 <= int(pyprep.__version__.split('.')[1])


def _prepapre_raw(raw):
    """
    Copy the raw instance and prepare it for PyPREP.
    Set the montage as 'standard_1020'. The reference 'CPz' is not added.
    """
    raw = raw.copy()
    raw.filter(
        l_freq=1.,
        h_freq=45.,
        picks='eeg',
        method="fir",
        phase="zero-double",
        fir_window="hamming",
        fir_design="firwin",
        pad="edge")
    raw.notch_filter(np.arange(50, 151, 50), picks='eeg')
    raw.set_montage('standard_1020')
    return raw


def PREP_bads_suggestion(raw):
    """
    Apply the PREP pipeline to detect bad channels:
        - SNR
        - Correlation
        - Deviation
        - HF Noise
        - NaN flat
        - RANSAC
    """
    raw = _prepapre_raw(raw)
    raw.pick_types(eeg=True)
    nc = pyprep.find_noisy_channels.NoisyChannels(raw)
    nc.find_all_bads()
    return nc.get_bads()
1 Like

Thank you @mscheltienne this is helpful.
A side question - Is there a way to push the output it shows on the console (or terminal) to a log file. I piped using ‘> <output.log>’ but it still prints some output on console.

Which output do you need?

To answer: it is partially possible. MNE has a logger that uses the logging library. For now, PyPREP doesn’t have one and uses prints. c.f. Use the MNE logger to set the verbosity · Issue #105 · sappelhoff/pyprep · GitHub

With a logger, it is possible to set a file handler to record all logs to a file. It is also possible to limit the verbosity to a certain level: DEBUG, ÌNFO, WARNING, CRITICAL.
With prints, I don’t see an easy way to record the console prints to a file, and it is not possible to limit the verbosity.

That said, even if I know it is possible to add a file handler to a logger, I never had to do it, and I don’t know if MNE has any easy-public API to do so.