gastropy.ica_denoise

Contents

gastropy.ica_denoise#

gastropy.ica_denoise(data, sfreq, low_hz=None, high_hz=None, band=None, snr_threshold=3.0, random_state=None)[source]#

Denoise multi-channel EGG using Independent Component Analysis.

Decomposes the multi-channel signal into independent components via FastICA. Components whose peak-to-mean power ratio within the gastric frequency band falls below snr_threshold are zeroed out. The cleaned components are then projected back to the original channel space.

Parameters:
  • data (array_like) – Multi-channel EGG data, shape (n_channels, n_samples). Must have at least 2 channels. For single-channel data use standard filtering instead.

  • sfreq (float) – Sampling frequency in Hz.

  • low_hz (float, optional) – Lower edge of the frequency band of interest (Hz). If None, uses band.f_lo.

  • high_hz (float, optional) – Upper edge of the frequency band of interest (Hz). If None, uses band.f_hi.

  • band (GastricBand, optional) – Gastric band supplying default low_hz/high_hz. Default is NORMOGASTRIA (2–4 cpm, 0.033–0.067 Hz).

  • snr_threshold (float, optional) – Components with gastric-band SNR below this value are removed. Default is 3.0.

  • random_state (int or None, optional) – Random seed for FastICA reproducibility. Default is None.

Returns:

  • denoised (np.ndarray) – ICA-denoised signal, shape (n_channels, n_samples).

  • info (dict) – Processing metadata:

    • n_components : int — total number of ICA components.

    • n_kept : int — components retained above threshold.

    • n_removed : int — components zeroed out.

    • component_snr : np.ndarray — SNR for each component.

    • snr_threshold : float — the threshold used.

    • band : dict — {"f_lo": ..., "f_hi": ...} used.

Raises:
  • ImportError – If scikit-learn is not installed. Install with pip install 'gastropy[ica]'.

  • ValueError – If data is 1-dimensional (requires multi-channel input).

  • RuntimeError – If all ICA components are removed (SNR below threshold for every component), indicating the signal may not contain a gastric rhythm or the threshold is too strict.

References

Dalmaijer, E. S. (2025). electrography v1.1.1. esdalmaijer/electrography

Hyvärinen, A., & Oja, E. (2000). Independent component analysis: algorithms and applications. Neural Networks, 13, 411–430.

Examples

>>> import numpy as np
>>> from gastropy.signal import ica_denoise
>>> rng = np.random.default_rng(42)
>>> t = np.arange(0, 300, 0.1)
>>> gastric = np.sin(2 * np.pi * 0.05 * t)
>>> data = np.stack([gastric + 0.2 * rng.standard_normal(len(t)),
...                  gastric + 0.2 * rng.standard_normal(len(t))])
>>> denoised, info = ica_denoise(data, sfreq=10.0)
>>> denoised.shape == data.shape
True
>>> info["n_kept"] >= 1
True