.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/asr/plot_05_asr_visualization.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_asr_plot_05_asr_visualization.py: Visualizing ASR with mne_denoise.viz ==================================== After cleaning EEG with ASR you usually want to *see* what happened: which segments were repaired, how aggressive the cleaning was, and how the variants compare. ``mne_denoise.viz`` keeps three ASR-specific diagnostics that have no generic equivalent --- the per-window repair timeline, the window-by-component reconstruction map, and the calibration / reference fraction --- and reuses the **generic** before/after plots (:func:`~mne_denoise.viz.plot_signal_overlay`, :func:`~mne_denoise.viz.plot_psd_comparison`, :func:`~mne_denoise.viz.plot_power_ratio_map`) for everything else. This example showcases that split on synthetic burst data, across two backends: - standard ASR (``method="standard"``), - Juggler GEV reference selection (``JugglerASR(strategy="gev")``). Every helper accepts MNE objects or NumPy arrays and honours ``show=`` / ``fname=`` (the ASR-specific helpers also take ``ax=``). .. GENERATED FROM PYTHON SOURCE LINES 26-28 Imports and synthetic data -------------------------- .. GENERATED FROM PYTHON SOURCE LINES 28-63 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mne_denoise.asr import ASR, JugglerASR from mne_denoise.viz import ( plot_asr_calibration_fraction, plot_asr_component_reconstruction, plot_asr_repair_timeline, plot_psd_comparison, plot_signal_overlay, ) rng = np.random.default_rng(2026) sfreq = 250.0 n_channels, n_times = 16, 15000 t = np.arange(n_times) / sfreq # Oscillatory "brain" background. brain = np.zeros((n_channels, n_times)) for ch in range(n_channels): phase = rng.uniform(0, 2 * np.pi) brain[ch] = ( 0.6 * np.sin(2 * np.pi * 10.0 * t + phase) + 0.15 * np.sin(2 * np.pi * 6.5 * t + 0.8 * phase) + 0.05 * rng.standard_normal(n_times) ) # Inject spatially-structured high-amplitude bursts. contaminated = brain.copy() for start in np.linspace(1000, n_times - 800, 8).astype(int): spatial = rng.standard_normal(n_channels) spatial /= np.linalg.norm(spatial) temporal = rng.standard_normal(300) contaminated[:, start : start + 300] += 12.0 * np.outer(spatial, temporal) .. GENERATED FROM PYTHON SOURCE LINES 64-68 Clean with standard ASR and overlay before/after (generic helper) ----------------------------------------------------------------- ``plot_signal_overlay`` is the generic before/after trace viewer; ASR no longer ships its own overlay wrapper. .. GENERATED FROM PYTHON SOURCE LINES 68-84 .. code-block:: Python asr = ASR(sfreq=sfreq, cutoff=20.0, picks=None, verbose=False) cleaned = np.asarray(asr.fit_transform(contaminated)) plot_signal_overlay( contaminated, cleaned, t, pick=0, before_label="contaminated", after_label="ASR-cleaned", x_label="Time (s)", y_label="Amplitude (a.u.)", title="Standard ASR — channel 0", show=False, ) .. image-sg:: /auto_examples/asr/images/sphx_glr_plot_05_asr_visualization_001.png :alt: Standard ASR — channel 0 :srcset: /auto_examples/asr/images/sphx_glr_plot_05_asr_visualization_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none
.. GENERATED FROM PYTHON SOURCE LINES 85-87 PSD before/after (generic helper) --------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 87-89 .. code-block:: Python plot_psd_comparison(contaminated, cleaned, sfreq=sfreq, fmax=60.0, show=False) .. image-sg:: /auto_examples/asr/images/sphx_glr_plot_05_asr_visualization_002.png :alt: PSD Comparison :srcset: /auto_examples/asr/images/sphx_glr_plot_05_asr_visualization_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none
.. GENERATED FROM PYTHON SOURCE LINES 90-93 Repair timeline (ASR-specific) ------------------------------ Which windows were reconstructed, and how many components each lost. .. GENERATED FROM PYTHON SOURCE LINES 93-95 .. code-block:: Python plot_asr_repair_timeline(asr, show=False) .. image-sg:: /auto_examples/asr/images/sphx_glr_plot_05_asr_visualization_003.png :alt: ASR repair timeline (24% of windows modified) :srcset: /auto_examples/asr/images/sphx_glr_plot_05_asr_visualization_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none (
, ) .. GENERATED FROM PYTHON SOURCE LINES 96-99 Component-reconstruction map (ASR-specific) ------------------------------------------- A window x component heatmap of the per-window principal-subspace rejection. .. GENERATED FROM PYTHON SOURCE LINES 99-101 .. code-block:: Python plot_asr_component_reconstruction(asr, show=False) .. image-sg:: /auto_examples/asr/images/sphx_glr_plot_05_asr_visualization_004.png :alt: ASR component reconstruction map :srcset: /auto_examples/asr/images/sphx_glr_plot_05_asr_visualization_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none (
, ) .. GENERATED FROM PYTHON SOURCE LINES 102-106 Calibration / reference fraction across variants (ASR-specific) --------------------------------------------------------------- JugglerASR selects calibration samples point-by-point, which survives heavy contamination where the standard clean-windows criterion would struggle. .. GENERATED FROM PYTHON SOURCE LINES 106-123 .. code-block:: Python juggler = JugglerASR( sfreq=sfreq, cutoff=20.0, strategy="gev", picks=None, verbose=False ) juggler.fit_transform(contaminated) print( "Juggler GEV reference fraction: " f"{juggler.calibration_info_['reference_selected_fraction'] * 100:.1f}%" ) plot_asr_calibration_fraction( [asr, juggler], labels=["standard", "juggler-gev"], title="Calibration fraction by variant", show=False, ) plt.show() .. image-sg:: /auto_examples/asr/images/sphx_glr_plot_05_asr_visualization_005.png :alt: Calibration fraction by variant :srcset: /auto_examples/asr/images/sphx_glr_plot_05_asr_visualization_005.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Juggler GEV reference fraction: 10.3% .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.641 seconds) .. _sphx_glr_download_auto_examples_asr_plot_05_asr_visualization.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_05_asr_visualization.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_05_asr_visualization.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_05_asr_visualization.zip `