{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Reduce EOG artifacts through regression\n\nReduce artifacts by regressing the EOG channels onto the rest of the channels\nand then subtracting the EOG signal.\n\nThis is a quick example to show the most basic application of the technique.\nSee the `tutorial ` for a more thorough\nexplanation that demonstrates more advanced approaches.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Author: Marijn van Vliet \n#\n# License: BSD-3-Clause\n# Copyright the MNE-Python contributors." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Import packages and load data\n\nWe begin as always by importing the necessary Python modules and loading some\ndata, in this case the `MNE sample dataset `.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from matplotlib import pyplot as plt\n\nimport mne\nfrom mne.datasets import sample\nfrom mne.preprocessing import EOGRegression\n\nprint(__doc__)\n\ndata_path = sample.data_path()\nraw_fname = data_path / \"MEG\" / \"sample\" / \"sample_audvis_filt-0-40_raw.fif\"\n\n# Read raw data\nraw = mne.io.read_raw_fif(raw_fname, preload=True)\nevents = mne.find_events(raw, \"STI 014\")\n\n# Highpass filter to eliminate slow drifts\nraw.filter(0.3, None, picks=\"all\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Perform regression and remove EOG\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Fit the regression model\nweights = EOGRegression().fit(raw)\nraw_clean = weights.apply(raw, copy=True)\n\n# Show the filter weights in a topomap\nweights.plot()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Before/after comparison\nLet's compare the signal before and after cleaning with EOG regression. This\nis best visualized by extracting epochs and plotting the evoked potential.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "tmin, tmax = -0.1, 0.5\nevent_id = {\"visual/left\": 3, \"visual/right\": 4}\nevoked_before = mne.Epochs(\n raw, events, event_id, tmin, tmax, baseline=(tmin, 0)\n).average()\nevoked_after = mne.Epochs(\n raw_clean, events, event_id, tmin, tmax, baseline=(tmin, 0)\n).average()\n\n# Create epochs after EOG correction\nepochs_after = mne.Epochs(raw_clean, events, event_id, tmin, tmax, baseline=(tmin, 0))\nevoked_after = epochs_after.average()\n\nfig, ax = plt.subplots(\n nrows=3, ncols=2, figsize=(10, 7), sharex=True, sharey=\"row\", layout=\"constrained\"\n)\nevoked_before.plot(axes=ax[:, 0], spatial_colors=True)\nevoked_after.plot(axes=ax[:, 1], spatial_colors=True)\nfig.suptitle(\"Before --> After\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.2" } }, "nbformat": 4, "nbformat_minor": 0 }