{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n\n# Whitening evoked data with a noise covariance\n\nEvoked data are loaded and then whitened using a given noise covariance\nmatrix. It's an excellent quality check to see if baseline signals match\nthe assumption of Gaussian white noise during the baseline period.\n\nCovariance estimation and diagnostic plots are based on\n:footcite:`EngemannGramfort2015`.\n\n## References\n.. footbibliography::\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Authors: Alexandre Gramfort \n# Denis A. Engemann \n#\n# License: BSD-3-Clause\n# Copyright the MNE-Python contributors." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import mne\nfrom mne import io\nfrom mne.cov import compute_covariance\nfrom mne.datasets import sample\n\nprint(__doc__)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Set parameters\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "data_path = sample.data_path()\nmeg_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\"\n\nraw = io.read_raw_fif(raw_fname, preload=True)\nraw.filter(1, 40, fir_design=\"firwin\")\nraw.info[\"bads\"] += [\"MEG 2443\"] # bads + 1 more\nevents = mne.read_events(event_fname)\n\n# let's look at rare events, button presses\nevent_id, tmin, tmax = 2, -0.2, 0.5\nreject = dict(mag=4e-12, grad=4000e-13, eeg=80e-6)\n\nepochs = mne.Epochs(\n raw,\n events,\n event_id,\n tmin,\n tmax,\n picks=(\"meg\", \"eeg\"),\n baseline=None,\n reject=reject,\n preload=True,\n)\n\n# Uncomment next line to use fewer samples and study regularization effects\n# epochs = epochs[:20] # For your data, use as many samples as you can!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Compute covariance using automated regularization\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "method_params = dict(diagonal_fixed=dict(mag=0.01, grad=0.01, eeg=0.01))\nnoise_covs = compute_covariance(\n epochs,\n tmin=None,\n tmax=0,\n method=\"auto\",\n return_estimators=True,\n n_jobs=None,\n projs=None,\n rank=None,\n method_params=method_params,\n verbose=True,\n)\n\n# With \"return_estimator=True\" all estimated covariances sorted\n# by log-likelihood are returned.\n\nprint(\"Covariance estimates sorted from best to worst\")\nfor c in noise_covs:\n print(f'{c[\"method\"]} : {c[\"loglik\"]}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Show the evoked data:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "evoked = epochs.average()\n\nevoked.plot(time_unit=\"s\") # plot evoked response" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can then show whitening for our various noise covariance estimates.\n\nHere we should look to see if baseline signals match the\nassumption of Gaussian white noise. we expect values centered at\n0 within 2 standard deviations for 95% of the time points.\n\nFor the Global field power we expect a value of 1.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "evoked.plot_white(noise_covs, time_unit=\"s\")" ] } ], "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 }