{ "nbformat_minor": 0, "nbformat": 4, "cells": [ { "execution_count": null, "cell_type": "code", "source": [ "%matplotlib inline" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "\n\nComputing covariance matrix\n===========================\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "import os.path as op\n\nimport mne\nfrom mne.datasets import sample" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "Source estimation method such as MNE require a noise estimations from the\nrecordings. In this tutorial we cover the basics of noise covariance and\nconstruct a noise covariance matrix that can be used when computing the\ninverse solution. For more information, see `BABDEEEB`.\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "data_path = sample.data_path()\nraw_empty_room_fname = op.join(\n data_path, 'MEG', 'sample', 'ernoise_raw.fif')\nraw_empty_room = mne.io.read_raw_fif(raw_empty_room_fname)\nraw_fname = op.join(data_path, 'MEG', 'sample', 'sample_audvis_raw.fif')\nraw = mne.io.read_raw_fif(raw_fname)\nraw.set_eeg_reference()\nraw.info['bads'] += ['EEG 053'] # bads + 1 more" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "The definition of noise depends on the paradigm. In MEG it is quite common\nto use empty room measurements for the estimation of sensor noise. However if\nyou are dealing with evoked responses, you might want to also consider\nresting state brain activity as noise.\nFirst we compute the noise using empty room recording. Note that you can also\nuse only a part of the recording with tmin and tmax arguments. That can be\nuseful if you use resting state as a noise baseline. Here we use the whole\nempty room recording to compute the noise covariance (tmax=None is the same\nas the end of the recording, see :func:`mne.compute_raw_covariance`).\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "noise_cov = mne.compute_raw_covariance(raw_empty_room, tmin=0, tmax=None)" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "Now that you the covariance matrix in a python object you can save it to a\nfile with :func:`mne.write_cov`. Later you can read it back to a python\nobject using :func:`mne.read_cov`.\n\nYou can also use the pre-stimulus baseline to estimate the noise covariance.\nFirst we have to construct the epochs. When computing the covariance, you\nshould use baseline correction when constructing the epochs. Otherwise the\ncovariance matrix will be inaccurate. In MNE this is done by default, but\njust to be sure, we define it here manually.\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "events = mne.find_events(raw)\nepochs = mne.Epochs(raw, events, event_id=1, tmin=-0.2, tmax=0.0,\n baseline=(-0.2, 0.0))" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "Note that this method also attenuates the resting state activity in your\nsource estimates.\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "noise_cov_baseline = mne.compute_covariance(epochs)" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "Plot the covariance matrices\n----------------------------\n\nTry setting proj to False to see the effect. Notice that the projectors in\nepochs are already applied, so ``proj`` parameter has no effect.\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "noise_cov.plot(raw_empty_room.info, proj=True)\nnoise_cov_baseline.plot(epochs.info)" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "How should I regularize the covariance matrix?\n----------------------------------------------\n\nThe estimated covariance can be numerically\nunstable and tends to induce correlations between estimated source amplitudes\nand the number of samples available. The MNE manual therefore suggests to\nregularize the noise covariance matrix (see\n`cov_regularization`), especially if only few samples are available.\nUnfortunately it is not easy to tell the effective number of samples, hence,\nto choose the appropriate regularization.\nIn MNE-Python, regularization is done using advanced regularization methods\ndescribed in [1]_. For this the 'auto' option can be used. With this\noption cross-validation will be used to learn the optimal regularization:\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "cov = mne.compute_covariance(epochs, tmax=0., method='auto')" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "This procedure evaluates the noise covariance quantitatively by how well it\nwhitens the data using the\nnegative log-likelihood of unseen data. The final result can also be visually\ninspected.\nUnder the assumption that the baseline does not contain a systematic signal\n(time-locked to the event of interest), the whitened baseline signal should\nbe follow a multivariate Gaussian distribution, i.e.,\nwhitened baseline signals should be between -1.96 and 1.96 at a given time\nsample.\nBased on the same reasoning, the expected value for the global field power\n(GFP) is 1 (calculation of the GFP should take into account the true degrees\nof freedom, e.g. ``ddof=3`` with 2 active SSP vectors):\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "evoked = epochs.average()\nevoked.plot_white(cov)" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "This plot displays both, the whitened evoked signals for each channels and\nthe whitened GFP. The numbers in the GFP panel represent the estimated rank\nof the data, which amounts to the effective degrees of freedom by which the\nsquared sum across sensors is divided when computing the whitened GFP.\nThe whitened GFP also helps detecting spurious late evoked components which\ncan be the consequence of over- or under-regularization.\n\nNote that if data have been processed using signal space separation\n(SSS) [2]_,\ngradiometers and magnetometers will be displayed jointly because both are\nreconstructed from the same SSS basis vectors with the same numerical rank.\nThis also implies that both sensor types are not any longer statistically\nindependent.\nThese methods for evaluation can be used to assess model violations.\nAdditional\nintroductory materials can be found `here `_.\n\nFor expert use cases or debugging the alternative estimators can also be\ncompared:\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "covs = mne.compute_covariance(epochs, tmax=0., method=('empirical', 'shrunk'),\n return_estimators=True)\nevoked = epochs.average()\nevoked.plot_white(covs)" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "This will plot the whitened evoked for the optimal estimator and display the\nGFPs for all estimators as separate lines in the related panel.\n\n" ], "cell_type": "markdown", "metadata": {} }, { "source": [ "References\n----------\n\n.. [1] Engemann D. and Gramfort A. (2015) Automated model selection in\n covariance estimation and spatial whitening of MEG and EEG signals,\n vol. 108, 328-342, NeuroImage.\n\n.. [2] Taulu, S., Simola, J., Kajola, M., 2005. Applications of the signal\n space separation method. IEEE Trans. Signal Proc. 53, 3359-3372.\n\n" ], "cell_type": "markdown", "metadata": {} } ], "metadata": { "kernelspec": { "display_name": "Python 2", "name": "python2", "language": "python" }, "language_info": { "mimetype": "text/x-python", "nbconvert_exporter": "python", "name": "python", "file_extension": ".py", "version": "2.7.13", "pygments_lexer": "ipython2", "codemirror_mode": { "version": 2, "name": "ipython" } } } }