channels.interpolate_bad_channels#

autoclean.functions.artifacts.channels.interpolate_bad_channels(data, bad_channels=None, reset_bads=True, mode='accurate', origin='auto', verbose=None)[source]#

Interpolate bad channels using spherical spline interpolation.

This function interpolates bad channels using spherical spline interpolation, which estimates the signal at bad channel locations based on the signals recorded at nearby good channels. This method preserves the spatial relationships in the data and maintains the original number of channels.

Spherical spline interpolation is the gold standard for EEG channel interpolation as it accounts for the spherical geometry of the head and provides smooth, realistic interpolated signals.

Parameters:
datamne.io.BaseRaw or mne.Epochs

The EEG data containing bad channels to interpolate.

bad_channelslist of str or None, default None

List of channel names to interpolate. If None, uses channels marked as bad in data.info[‘bads’]. Channels not present in the data are ignored with a warning.

reset_badsbool, default True

Whether to reset the bad channels list after interpolation. If True, interpolated channels are removed from data.info[‘bads’]. If False, they remain marked as bad for reference.

modestr, default ‘accurate’

Interpolation mode to use. Options: - ‘accurate’: More precise but slower interpolation - ‘fast’: Faster but less precise interpolation

originstr or tuple, default ‘auto’

Origin for spherical spline interpolation. Options: - ‘auto’: Automatically determine origin (recommended) - tuple: (x, y, z) coordinates in meters - ‘head’: Use head origin from digitization

verbosebool or None, default None

Control verbosity of interpolation output.

Returns:
data_interpolatedmne.io.BaseRaw or mne.Epochs

Copy of input data with bad channels interpolated.

Raises:
TypeError

If data is not an MNE Raw or Epochs object.

ValueError

If bad_channels contains invalid channel names or parameters are invalid.

RuntimeError

If interpolation fails due to insufficient good channels or other errors.

See also

mne.io.Raw.interpolate_bads

MNE’s raw data interpolation method

mne.Epochs.interpolate_bads

MNE’s epochs interpolation method

autoclean.detect_bad_channels

Detect bad channels automatically

Notes

Interpolation Method: Uses spherical spline interpolation which fits smooth functions on the sphere to the scalp potential distribution. This method: - Preserves spatial relationships in the EEG data - Provides realistic interpolated signals - Maintains the rank of the data (important for ICA and other analyses)

Requirements: - Requires channel positions (montage) to be set in the data - Needs sufficient good channels around bad channels for accurate interpolation - Interpolation quality decreases with increasing number of bad channels

Best Practices: - Interpolate no more than 10-15% of total channels - Ensure bad channels are not clustered in one region - Always visually inspect interpolated channels - Consider excluding channels rather than interpolating if >20% are bad

Performance Notes: - ‘accurate’ mode provides better results but is slower - ‘fast’ mode suitable for real-time processing or large datasets - Interpolation time scales with number of channels and bad channels

References

Perrin, F., Pernier, J., Bertrand, O., & Echallier, J. F. (1989). Spherical splines for scalp potential and current density mapping. Electroencephalography and clinical neurophysiology, 72(2), 184-187.

Ferree, T. C., Luu, P., Russell, G. S., & Tucker, D. M. (2001). Scalp electrode impedance, infection risk, and EEG data quality. Clinical Neurophysiology, 112(3), 536-544.

Examples

Basic interpolation using channels marked as bad:

>>> from autoclean import interpolate_bad_channels
>>> # Assume raw.info['bads'] = ['Fp1', 'T7']
>>> raw_interp = interpolate_bad_channels(raw)
>>> print(f"Interpolated {len(raw.info['bads'])} bad channels")

Interpolate specific channels:

>>> raw_interp = interpolate_bad_channels(
...     raw,
...     bad_channels=['Fp1', 'Fp2', 'F7'],
...     reset_bads=False  # Keep them marked as bad
... )

Fast interpolation for large datasets:

>>> raw_interp = interpolate_bad_channels(
...     raw,
...     mode='fast',
...     reset_bads=True
... )

Interpolate epochs data:

>>> epochs_interp = interpolate_bad_channels(epochs)