pyprep.Reference#

class pyprep.Reference(raw, params, ransac=True, channel_wise=False, max_chunk_size=None, random_state=None, reject_by_annotation=None, matlab_strict=False)[source]#

Bases: object

Estimate the ‘true’ reference with all the bad channels interpolated.

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

Parameters:
rawmne.io.Raw

The raw data.

paramsdict

Parameters of PREP which include at least the following keys: - ref_chs - reref_chs

ransacbool

Whether or not to use RANSAC for noisy channel detection in addition to the other methods in NoisyChannels. RANSAC can detect bad channels that other methods are unable to catch, but also slows down noisy channel detection considerably. Defaults to True.

channel_wisebool

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. This can be None, an integer, or a RandomState object. If None, a random seed will be obtained from the operating system. Defaults to None.

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

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.

References

[1]

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

interpolate_bads()[source]#

Interpolate any remaining bad channels following robust referencing.

This method can only be called if perform_reference() has already been run with the interpolate_bads parameter set to False. It cannot be run more than once per instance of Reference.

perform_reference(max_iterations=4, interpolate_bads=True)[source]#

Estimate the true signal mean and interpolate bad channels.

This function implements the functionality of the performReference function as part of the PREP pipeline on mne raw object.

Parameters:
max_iterationsint | None

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

interpolate_badsbool, optional

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

Notes

This function calls robust_reference first.

static remove_reference(signal, reference, index=None)[source]#

Remove the reference signal from the original EEG signal.

This function implements the functionality of the removeReference function as part of the PREP pipeline on mne raw object.

Parameters:
signalnp.ndarray, shape(channels, times)

The original EEG signal.

referencenp.ndarray, shape(times,)

The reference signal.

index{list, None} | None

A list of channel indices from which the reference signal should be subtracted. Defaults to all channels in signal.

Returns:
np.ndarray, shape(channels, times)

The referenced EEG signal.

robust_reference(max_iterations=4)[source]#

Detect bad channels and estimate the robust reference signal.

This function implements the functionality of the robustReference function as part of the PREP pipeline on mne raw object.

Parameters:
max_iterationsint | None

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

Returns:
noisy_channels: dict

A dictionary of names of noisy channels detected from all methods after referencing.

reference_signal: np.ndarray, shape(n, )

Estimation of the ‘true’ signal mean