pyprep.PrepPipeline#

class pyprep.PrepPipeline(raw, prep_params, montage, ransac=True, channel_wise=False, max_chunk_size=None, random_state=None, filter_kwargs=None, reject_by_annotation=None, matlab_strict=False)[source]#

Bases: object

Early stage preprocessing (PREP) of EEG data.

This class implements the functionality of the PREP (preprocessing pipeline) for EEG data described in [1].

Parameters:
rawmne.io.Raw

The data. Channel types must be correctly assigned (e.g., ocular channels are assigned the type ‘eog’).

prep_paramsdict

Parameters of PREP which include at least the following keys:

  • ref_chs{list, ‘eeg’}
    • A list of channel names to be used for rereferencing. These channels will be used to construct the reference signal. Can be a str ‘eeg’ to use all EEG channels.

  • reref_chs{list, ‘eeg’}
    • A list of channel names to define from which channels the reference signal will be subtracted. Can be a str ‘eeg’ to use all EEG channels.

  • line_freqs{np.ndarray, list}
    • list of floats indicating frequencies to be removed. For example, for 60Hz you may specify np.arange(60, sfreq / 2, 60). Specify an empty list to skip the line noise removal step.

  • max_iterationsint | None
    • The maximum number of iterations of noisy channel removal to perform during robust referencing. Defaults to 4.

montagemne.channels.DigMontage

Digital montage of EEG data.

ransacbool | None

Whether or not to use RANSAC for noisy channel detection in addition to the other methods in NoisyChannels. Defaults to True.

channel_wisebool | None

Whether RANSAC should predict signals for chunks of channels over the entire signal length (“channel-wise RANSAC”, see max_chunk_size parameter). If False, RANSAC will instead predict signals for all channels at once but over a number of smaller time windows instead of over the entire signal length (“window-wise RANSAC”). Channel-wise RANSAC generally has higher RAM demands than window-wise RANSAC (especially if max_chunk_size is None), but can be faster on systems with lots of RAM to spare. Has no effect if not using RANSAC. Defaults to False.

max_chunk_size{int, None} | None

The maximum number of channels to predict at once during channel-wise RANSAC. If None, RANSAC will use the largest chunk size that will fit into the available RAM, which may slow down other programs on the host system. If using window-wise RANSAC (the default) or not using RANSAC at all, this parameter has no effect. Defaults to None.

random_state{int, None, np.random.RandomState} | None

The random seed at which to initialize the class. If random_state is an int, it will be used as a seed for RandomState. If None, the seed will be obtained from the operating system (see RandomState for details). Default is None.

filter_kwargs{dict, None} | None

Optional keywords arguments to be passed on to mne.filter.notch_filter. Do not set the “x”, Fs”, and “freqs” arguments via the filter_kwargs parameter, but use the “raw” and “prep_params” parameters instead. If None is passed, the pyprep default settings for filtering are used instead.

reject_by_annotation{None, ‘omit’} | None

How to handle BAD-annotated time segments (annotations starting with “BAD” or “bad”) during channel quality assessment. If 'omit', annotated segments are excluded from analysis (clean segments are concatenated). If None (default), annotations are ignored and the full recording is used. This is useful when recordings contain breaks or movement artifacts that shouldn’t influence channel rejection decisions.

matlab_strictbool | None

Whether or not PyPREP should strictly follow MATLAB PREP’s internal math, ignoring any improvements made in PyPREP over the original code (see Deliberate Differences from MATLAB PREP for more details). Defaults to False.

Attributes

raw

Return a version of self.raw_eeg that includes the non-eeg channels.

raw_eeg

(mne.io.Raw) The only-eeg part of the data. It is unprocessed if accessed before the fit method, processed if accessed after a successful fit method.

raw_non_eeg

({mne.io.Raw, None}) The non-eeg part of the data. It is not processed when calling the fit method. If the input was only EEG it will be None.

noisy_channels_original

(dict) Detailed bad channels in each criteria before robust reference.

noisy_channels_before_interpolation

(dict) Detailed bad channels in each criteria just before interpolation.

noisy_channels_after_interpolation

(dict) Detailed bad channels in each criteria just after interpolation.

bad_before_interpolation

(list) bad channels after robust reference but before interpolation

EEG_before_interpolation

(np.ndarray) EEG data in uV before the interpolation

reference_before_interpolation

(np.ndarray) Reference signal in uV before interpolation.

reference_after_interpolation

(np.ndarray) Reference signal in uV after interpolation.

interpolated_channels

(list) Names of the interpolated channels.

still_noisy_channels

(list) Names of the noisy channels after interpolation.

References

[1]

Bigdely-Shamlo, N., Mullen, T., Kothe, C., Su, K. M., Robbins, K. A. (2015). The PREP pipeline: standardized preprocessing for large-scale EEG analysis. Frontiers in Neuroinformatics, 9, 16.

fit()[source]#

Run the whole PREP pipeline.

property raw#

Return a version of self.raw_eeg that includes the non-eeg channels.

remove_line_noise(line_freqs=None)[source]#

Remove line noise from all EEG channels.

Line noise is removed by detrending the signal, applying a notch filter, and adding the slow drifts back. By default the notch filter uses MNE’s spectrum_fit method, which attempts to isolate and remove line noise while preserving unrelated background signal in the same frequency ranges (to minimize distortions in the power-spectral density). The filter can be configured via the filter_kwargs argument of PrepPipeline.

Parameters:
line_freqs{np.ndarray, list, None}, optional

A list of the frequencies (in Hz) at which line noise should be removed (e.g., np.arange(60, sfreq / 2, 60) for a recording with a powerline noise of 60 Hz). If None (default), the "line_freqs" entry of the prep_params passed to PrepPipeline is used.

robust_reference(max_iterations=None, interpolate_bads=True)[source]#

Perform robust referencing on the EEG signal and detect bad channels.

This method uses an iterative approach to estimate a robust average reference signal free of contamination from bad channels, as detected automatically using the methods of NoisyChannels. Once estimated, the robust average reference is applied to the data and bad channel detection is re-run to flag any noisy or unusable channels post-reference.

By default, this method will also interpolate the signals of any channels detected as bad following robust referencing, re-reference the data accordingly, and re-detect any remaining bad channels.

Parameters:
max_iterations{int, None}, optional

The maximum number of iterations of noisy channel removal to perform during robust referencing. If None (default), the "max_iterations" entry of the prep_params passed to PrepPipeline is used.

interpolate_badsbool, optional

Whether or not any remaining bad channels following robust referencing should be interpolated. Defaults to True.

Examples using pyprep.PrepPipeline#

Run the full PREP

Run the full PREP

Run PREP in stages

Run PREP in stages