.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_tutorials/plot_30_epochs.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_tutorials_plot_30_epochs.py: .. _tut-epochs: Epochs, artifact rejection and combining runs ============================================= Preprocessing works on each run separately. Epoching is a second stage, applied to each preprocessed run; the epochs of a session's runs are then combined. This tutorial cuts MNE's ``sample`` EEG into epochs around the auditory and visual stimuli, repairs or rejects the bad ones with autoreject, and combines two runs. .. GENERATED FROM PYTHON SOURCE LINES 13-15 .. code-block:: Python :dedent: 1 .. GENERATED FROM PYTHON SOURCE LINES 18-30 .. code-block:: Python import mne import meeg_utils as meu from meeg_utils import steps as S mne.set_log_level("ERROR") data_path = mne.datasets.sample.data_path() raw = mne.io.read_raw_fif(data_path / "MEG" / "sample" / "sample_audvis_raw.fif") raw.pick(["eeg", "eog", "stim"]).load_data() raw.info["line_freq"] = 60.0 .. GENERATED FROM PYTHON SOURCE LINES 31-35 The sample recording is a single run. To show how runs are combined, we treat its two halves as two runs, each preprocessed on its own (here only a high-pass filter and an average reference, to keep the tutorial fast; in practice the ``eeg-erp`` preset). .. GENERATED FROM PYTHON SOURCE LINES 35-39 .. code-block:: Python pre = meu.Pipeline([("highpass", S.Filter(0.1, None)), ("reference", S.Reference("average"))]) runs = [raw.copy().crop(0, 138), raw.copy().crop(138, None)] clean_runs = [pre.fit_transform(run) for run in runs] .. GENERATED FROM PYTHON SOURCE LINES 40-45 The epochs stage ---------------- The ``eeg-erp`` epochs stage low-passes the data at 40 Hz, cuts epochs from -0.2 to 0.5 s with a pre-stimulus baseline, and runs local autoreject (all MNE-BIDS-Pipeline defaults). Only the events need to be given: .. GENERATED FROM PYTHON SOURCE LINES 45-49 .. code-block:: Python event_id = {"auditory/left": 1, "auditory/right": 2, "visual/left": 3, "visual/right": 4} ep = meu.Pipeline.preset("eeg-erp", stage="epochs", event_id=event_id) ep .. raw:: html
#StepConfiguration (changed parameters)
0lowpassFilter(h_freq=40.0, l_freq=None)
1epochEpoch(event_id={'auditory/left': 1, 'auditory/right': 2, 'visual/left': 3, 'visual/right': 4})
2autorejectAutoReject(n_interpolate=[4, 8, 16])


.. GENERATED FROM PYTHON SOURCE LINES 50-53 .. code-block:: Python epochs_per_run = [ep.fit_transform(run) for run in clean_runs] epochs_per_run .. rst-class:: sphx-glr-script-out .. code-block:: none [, ] .. GENERATED FROM PYTHON SOURCE LINES 54-56 The last fitted run: why epochs were dropped, and the average per condition: .. GENERATED FROM PYTHON SOURCE LINES 56-59 .. code-block:: Python print(ep.qc_["epoch"]) figs = ep["epoch"].plot("evoked") .. image-sg:: /auto_tutorials/images/sphx_glr_plot_30_epochs_001.png :alt: plot 30 epochs :srcset: /auto_tutorials/images/sphx_glr_plot_30_epochs_001.png, /auto_tutorials/images/sphx_glr_plot_30_epochs_001_2_00x.png 2.00x :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none {'n_events_fit': {'auditory/left': 25, 'auditory/right': 26, 'visual/left': 26, 'visual/right': 27}, 'n_events': {'auditory/left': 25, 'auditory/right': 26, 'visual/left': 26, 'visual/right': 27}, 'n_kept': 104, 'n_dropped': 0, 'fraction_kept': 1.0, 'drop_reasons': {}} .. GENERATED FROM PYTHON SOURCE LINES 60-66 autoreject ---------- Local autoreject (Jas et al., 2017) learns a peak-to-peak threshold per channel. Within an epoch it interpolates the few channels above their threshold; epochs with too many bad channels are dropped. Each row is a channel and each column an epoch: .. GENERATED FROM PYTHON SOURCE LINES 66-69 .. code-block:: Python print({k: ep.qc_["autoreject"][k] for k in ("n_dropped", "fraction_dropped")}) figs = ep["autoreject"].plot("reject_log") .. image-sg:: /auto_tutorials/images/sphx_glr_plot_30_epochs_002.png :alt: plot 30 epochs :srcset: /auto_tutorials/images/sphx_glr_plot_30_epochs_002.png, /auto_tutorials/images/sphx_glr_plot_30_epochs_002_2_00x.png 2.00x :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none {'n_dropped': 6, 'fraction_dropped': 0.0577} .. GENERATED FROM PYTHON SOURCE LINES 70-71 and the learned thresholds: .. GENERATED FROM PYTHON SOURCE LINES 71-73 .. code-block:: Python figs = ep["autoreject"].plot("thresholds") .. image-sg:: /auto_tutorials/images/sphx_glr_plot_30_epochs_003.png :alt: Channel thresholds (local autoreject) :srcset: /auto_tutorials/images/sphx_glr_plot_30_epochs_003.png, /auto_tutorials/images/sphx_glr_plot_30_epochs_003_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 74-81 Combine the runs ---------------- :func:`meeg_utils.epochs.combine` concatenates the runs after checking that they agree: the same event codes, the same channels (a channel bad in one run is marked bad in all, ``bads="union"``), and for MEG a head position within 2 mm (otherwise map the runs to a common head position first with :class:`~meeg_utils.steps.HeadAlign`). .. GENERATED FROM PYTHON SOURCE LINES 81-84 .. code-block:: Python epochs = meu.epochs.combine(epochs_per_run) epochs .. raw:: html
General
MNE object type EpochsArray
Measurement date 2002-12-03 at 19:01:10 UTC
Participant Unknown
Experimenter MEG
Acquisition
Total number of events 256
Events counts auditory/left: 61
auditory/right: 64
visual/left: 66
visual/right: 65
Time range -0.200 – 0.499 s
Baseline -0.200 – 0.000 s
Sampling frequency 600.61 Hz
Time points 421
Metadata No metadata set
Channels
EEG and
EOG
Stimulus
Head & sensor digitization 146 points
Filters
Highpass 0.10 Hz
Lowpass 40.00 Hz


.. GENERATED FROM PYTHON SOURCE LINES 85-88 .. code-block:: Python evokeds = {cond: epochs[cond].average() for cond in ("auditory", "visual")} mne.viz.plot_compare_evokeds(evokeds, picks="EEG 021", show=False) .. image-sg:: /auto_tutorials/images/sphx_glr_plot_30_epochs_004.png :alt: EEG 021 :srcset: /auto_tutorials/images/sphx_glr_plot_30_epochs_004.png, /auto_tutorials/images/sphx_glr_plot_30_epochs_004_2_00x.png 2.00x :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [
] .. GENERATED FROM PYTHON SOURCE LINES 89-91 .. code-block:: Python evokeds["auditory"].plot_joint(times=[0.09, 0.2], title="Auditory stimuli", show=False) .. image-sg:: /auto_tutorials/images/sphx_glr_plot_30_epochs_005.png :alt: Auditory stimuli, 0.090 s, 0.200 s :srcset: /auto_tutorials/images/sphx_glr_plot_30_epochs_005.png, /auto_tutorials/images/sphx_glr_plot_30_epochs_005_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 92-96 Resting state ------------- Without events, the ``*-rest`` presets cut consecutive windows of a given length (the frequency resolution of later spectra is 1 / length): .. GENERATED FROM PYTHON SOURCE LINES 96-99 .. code-block:: Python rest = meu.Pipeline.preset("eeg-rest", stage="epochs", epoch_duration=2.0) windows = rest.fit_transform(clean_runs[0]) windows.compute_psd(fmax=40).plot(average=True, amplitude=False, show=False) .. image-sg:: /auto_tutorials/images/sphx_glr_plot_30_epochs_006.png :alt: EEG :srcset: /auto_tutorials/images/sphx_glr_plot_30_epochs_006.png, /auto_tutorials/images/sphx_glr_plot_30_epochs_006_2_00x.png 2.00x :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 26.575 seconds) .. _sphx_glr_download_auto_tutorials_plot_30_epochs.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_30_epochs.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_30_epochs.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_30_epochs.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_