{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n\n# XDAWN Denoising\n\nXDAWN filters are trained from epochs, signal is projected in the sources\nspace and then projected back in the sensor space using only the first two\nXDAWN components. The process is similar to an ICA, but is\nsupervised in order to maximize the signal to signal + noise ratio of the\nevoked response :footcite:`RivetEtAl2009, RivetEtAl2011`.\n\n

Warning

As this denoising method exploits the known events to\n maximize SNR of the contrast between conditions it can lead\n to overfitting. To avoid a statistical analysis problem you\n should split epochs used in fit with the ones used in\n apply method.

\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Authors: Alexandre Barachant \n#\n# License: BSD-3-Clause\n# Copyright the MNE-Python contributors." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from mne import Epochs, compute_raw_covariance, io, pick_types, read_events\nfrom mne.datasets import sample\nfrom mne.preprocessing import Xdawn\nfrom mne.viz import plot_epochs_image\n\nprint(__doc__)\n\ndata_path = sample.data_path()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Set parameters and read data\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "meg_path = data_path / \"MEG\" / \"sample\"\nraw_fname = meg_path / \"sample_audvis_filt-0-40_raw.fif\"\nevent_fname = meg_path / \"sample_audvis_filt-0-40_raw-eve.fif\"\ntmin, tmax = -0.1, 0.3\nevent_id = dict(vis_r=4)\n\n# Setup for reading the raw data\nraw = io.read_raw_fif(raw_fname, preload=True)\nraw.filter(1, 20, fir_design=\"firwin\") # replace baselining with high-pass\nevents = read_events(event_fname)\n\nraw.info[\"bads\"] = [\"MEG 2443\"] # set bad channels\npicks = pick_types(raw.info, meg=True, eeg=False, stim=False, eog=False, exclude=\"bads\")\n# Epoching\nepochs = Epochs(\n raw,\n events,\n event_id,\n tmin,\n tmax,\n proj=False,\n picks=picks,\n baseline=None,\n preload=True,\n verbose=False,\n)\n\n# Plot image epoch before xdawn\nplot_epochs_image(epochs[\"vis_r\"], picks=[230], vmin=-500, vmax=500)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, we estimate a set of xDAWN filters for the epochs (which contain only\nthe ``vis_r`` class).\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Estimates signal covariance\nsignal_cov = compute_raw_covariance(raw, picks=picks)\n\n# Xdawn instance\nxd = Xdawn(n_components=2, signal_cov=signal_cov)\n\n# Fit xdawn\nxd.fit(epochs)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Epochs are denoised by calling ``apply``, which by default keeps only the\nsignal subspace corresponding to the first ``n_components`` specified in the\n``Xdawn`` constructor above.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "epochs_denoised = xd.apply(epochs)\n\n# Plot image epoch after Xdawn\nplot_epochs_image(epochs_denoised[\"vis_r\"], picks=[230], vmin=-500, vmax=500)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## References\n.. footbibliography::\n\n" ] } ], "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 }