.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/asr/plot_02_mne_raw_qc.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_02_mne_raw_qc.py: Artifact Subspace Reconstruction: Raw QC and Annotations. ========================================================= This example shows ASR on an ``mne.io.RawArray`` with optional clean_windows- style final window rejection. The repaired spans and rejected spans are kept as annotations for downstream QC instead of deleting samples immediately. .. GENERATED FROM PYTHON SOURCE LINES 11-13 Imports ------- .. GENERATED FROM PYTHON SOURCE LINES 13-20 .. code-block:: Python import matplotlib.pyplot as plt import mne import numpy as np from mne_denoise.asr import ASR from mne_denoise.viz import plot_signal_overlay .. GENERATED FROM PYTHON SOURCE LINES 21-23 Synthetic Raw ------------- .. GENERATED FROM PYTHON SOURCE LINES 23-55 .. code-block:: Python sfreq = 250.0 duration = 15.0 n_eeg = 8 n_times = int(sfreq * duration) times = np.arange(n_times) / sfreq rng = np.random.default_rng(7) brain = np.zeros((n_eeg, n_times)) for ch_idx in range(n_eeg): phase = rng.uniform(0, 2 * np.pi) brain[ch_idx] = ( 0.4 * np.sin(2 * np.pi * 10 * times + phase) + 0.15 * np.sin(2 * np.pi * 6 * times + 0.5 * phase) + 0.04 * rng.standard_normal(n_times) ) eog = 0.25 * np.sin(2 * np.pi * 1.2 * times)[np.newaxis, :] data = brain.copy() spatial = rng.standard_normal((n_eeg, 2)) spatial /= np.linalg.norm(spatial, axis=0, keepdims=True) for onset, stop in ((4.5, 5.3), (9.0, 9.7), (12.0, 12.6)): start = int(onset * sfreq) end = int(stop * sfreq) data[:, start:end] += spatial @ (7.0 * rng.standard_normal((2, end - start))) raw_data = np.vstack([data, eog]) ch_names = [f"EEG {idx:02d}" for idx in range(n_eeg)] + ["EOG 01"] ch_types = ["eeg"] * n_eeg + ["eog"] info = mne.create_info(ch_names, sfreq, ch_types) raw = mne.io.RawArray(raw_data, info, verbose=False) .. GENERATED FROM PYTHON SOURCE LINES 56-58 Fit and Apply ASR ----------------- .. GENERATED FROM PYTHON SOURCE LINES 58-77 .. code-block:: Python asr = ASR( cutoff=5.0, calibration="auto", picks="eeg", filter_kind="none", window_criterion=0.25, window_criterion_tolerances=(-np.inf, 2.5), verbose=False, ) raw_clean = asr.fit_transform(raw) repair_annotations = asr.to_annotations() reject_annotations = asr.to_annotations("rejection") print(f"Repaired sample fraction: {asr.fraction_reconstructed_samples_:.2%}") print( "Retained after final window rejection: " f"{asr.fraction_retained_after_window_rejection_:.2%}" ) .. rst-class:: sphx-glr-script-out .. code-block:: none Repaired sample fraction: 52.08% Retained after final window rejection: 86.67% .. GENERATED FROM PYTHON SOURCE LINES 78-83 Plot One EEG Channel -------------------- The repair and rejection annotations are passed straight to ``plot_signal_overlay`` as ``highlight_spans`` instead of shading the axis by hand; the reference trace uses the ``reference`` argument. .. GENERATED FROM PYTHON SOURCE LINES 83-124 .. code-block:: Python channel = "EEG 00" noisy = raw.get_data(picks=[channel])[0] clean = raw_clean.get_data(picks=[channel])[0] spans = [ { "onset": onset, "duration": dur, "color": "C3", "alpha": 0.12, "label": "ASR repair", } for onset, dur in zip(repair_annotations.onset, repair_annotations.duration) ] + [ { "onset": onset, "duration": dur, "color": "0.2", "alpha": 0.08, "label": "Window reject mask", } for onset, dur in zip(reject_annotations.onset, reject_annotations.duration) ] plot_signal_overlay( noisy, clean, times, scale_after=False, before_label="Noisy EEG", after_label="ASR cleaned EEG", x_label="Time (s)", y_label="Amplitude (a.u.)", title="ASR repairs plus optional final window rejection mask", reference=brain[0], reference_label="Reference EEG", highlight_spans=spans, show=False, ) plt.show() .. image-sg:: /auto_examples/asr/images/sphx_glr_plot_02_mne_raw_qc_001.png :alt: ASR repairs plus optional final window rejection mask :srcset: /auto_examples/asr/images/sphx_glr_plot_02_mne_raw_qc_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.918 seconds) .. _sphx_glr_download_auto_examples_asr_plot_02_mne_raw_qc.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_mne_raw_qc.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_mne_raw_qc.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_mne_raw_qc.zip `