.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/sns/plot_01_sns_basics.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_sns_plot_01_sns_basics.py: Basic Sensor Noise Suppression ============================== This example adds independent sensor noise to a spatially redundant signal, fits SNS, and applies the same workflow to an MNE Raw object. .. GENERATED FROM PYTHON SOURCE LINES 8-56 .. image-sg:: /auto_examples/sns/images/sphx_glr_plot_01_sns_basics_001.png :alt: Sensor Noise Suppression — channel 0 :srcset: /auto_examples/sns/images/sphx_glr_plot_01_sns_basics_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Relative error before SNS: 0.201 Relative error after SNS: 0.117 Effective neighbors: 12 Samples rejected while fitting: 0 RawArray (24, 2500) | .. code-block:: Python import mne import numpy as np from mne_denoise.sns import SNS from mne_denoise.viz import plot_signal_overlay rng = np.random.default_rng(12) n_channels, n_sources, n_times = 24, 4, 2500 sources = rng.standard_normal((n_sources, n_times)) shared = rng.standard_normal((n_channels, n_sources)) @ sources observed = shared + 0.45 * rng.standard_normal(shared.shape) model = SNS(n_neighbors=12, outlier_threshold=8.0) cleaned = model.fit_transform(observed) error_before = np.linalg.norm(observed - shared) / np.linalg.norm(shared) error_after = np.linalg.norm(cleaned - shared) / np.linalg.norm(shared) print(f"Relative error before SNS: {error_before:.3f}") print(f"Relative error after SNS: {error_after:.3f}") print(f"Effective neighbors: {model.n_neighbors_}") print(f"Samples rejected while fitting: {model.rejected_sample_count_}") plot_signal_overlay( observed, cleaned, np.arange(n_times) / 250.0, pick=0, start=0.0, stop=1.6, scale_after=False, before_label="Observed", after_label="SNS", reference=shared[0], reference_label="Shared signal", x_label="Time (s)", title="Sensor Noise Suppression — channel 0", show=False, ) # MNE containers use the same estimator. The returned object is a copy with its # metadata and channel layout preserved. info = mne.create_info( [f"EEG {index:03d}" for index in range(n_channels)], 250.0, "eeg" ) raw = mne.io.RawArray(observed, info, verbose=False) raw_clean = SNS(n_neighbors=12).fit_transform(raw) print(type(raw_clean).__name__, raw_clean.get_data().shape) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.343 seconds) .. _sphx_glr_download_auto_examples_sns_plot_01_sns_basics.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_sns_basics.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_sns_basics.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_sns_basics.zip `