{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n\n# Computing various MNE solutions\n\nThis example shows example fixed- and free-orientation source localizations\nproduced by the minimum-norm variants implemented in MNE-Python:\nMNE, dSPM, sLORETA, and eLORETA.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Author: Eric Larson \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.datasets import sample\nfrom mne.minimum_norm import apply_inverse, apply_inverse_cov, make_inverse_operator\n\nprint(__doc__)\n\ndata_path = sample.data_path()\nsubjects_dir = data_path / \"subjects\"\n\n# Read data (just MEG here for speed, though we could use MEG+EEG)\nmeg_path = data_path / \"MEG\" / \"sample\"\nfname_evoked = meg_path / \"sample_audvis-ave.fif\"\nevoked = mne.read_evokeds(fname_evoked, condition=\"Right Auditory\", baseline=(None, 0))\nfname_fwd = meg_path / \"sample_audvis-meg-oct-6-fwd.fif\"\nfname_cov = meg_path / \"sample_audvis-cov.fif\"\nfwd = mne.read_forward_solution(fname_fwd)\ncov = mne.read_cov(fname_cov)\n# crop for speed in these examples\nevoked.crop(0.05, 0.15)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Fixed orientation\nFirst let's create a fixed-orientation inverse, with the default weighting.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "inv = make_inverse_operator(evoked.info, fwd, cov, loose=0.0, depth=0.8, verbose=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's look at the current estimates using MNE. We'll take the absolute\nvalue of the source estimates to simplify the visualization.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "snr = 3.0\nlambda2 = 1.0 / snr**2\nkwargs = dict(\n initial_time=0.08,\n hemi=\"lh\",\n subjects_dir=subjects_dir,\n size=(600, 600),\n clim=dict(kind=\"percent\", lims=[90, 95, 99]),\n smoothing_steps=10,\n)\n\nstc = abs(apply_inverse(evoked, inv, lambda2, \"MNE\", verbose=True))\nbrain = stc.plot(**kwargs)\nbrain.add_text(0.1, 0.9, \"MNE\", \"title\", font_size=14)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next let's use the default noise normalization, dSPM:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "stc = abs(apply_inverse(evoked, inv, lambda2, \"dSPM\", verbose=True))\nbrain = stc.plot(**kwargs)\nbrain.add_text(0.1, 0.9, \"dSPM\", \"title\", font_size=14)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And sLORETA:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "stc = abs(apply_inverse(evoked, inv, lambda2, \"sLORETA\", verbose=True))\nbrain = stc.plot(**kwargs)\nbrain.add_text(0.1, 0.9, \"sLORETA\", \"title\", font_size=14)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And finally eLORETA:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "stc = abs(apply_inverse(evoked, inv, lambda2, \"eLORETA\", verbose=True))\nbrain = stc.plot(**kwargs)\nbrain.add_text(0.1, 0.9, \"eLORETA\", \"title\", font_size=14)\ndel inv" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Free orientation\nNow let's not constrain the orientation of the dipoles at all by creating\na free-orientation inverse.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "inv = make_inverse_operator(evoked.info, fwd, cov, loose=1.0, depth=0.8, verbose=True)\ndel fwd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's look at the current estimates using MNE. We'll take the absolute\nvalue of the source estimates to simplify the visualization.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "stc = apply_inverse(evoked, inv, lambda2, \"MNE\", verbose=True)\nbrain = stc.plot(**kwargs)\nbrain.add_text(0.1, 0.9, \"MNE\", \"title\", font_size=14)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next let's use the default noise normalization, dSPM:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "stc = apply_inverse(evoked, inv, lambda2, \"dSPM\", verbose=True)\nbrain = stc.plot(**kwargs)\nbrain.add_text(0.1, 0.9, \"dSPM\", \"title\", font_size=14)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "sLORETA:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "stc = apply_inverse(evoked, inv, lambda2, \"sLORETA\", verbose=True)\nbrain = stc.plot(**kwargs)\nbrain.add_text(0.1, 0.9, \"sLORETA\", \"title\", font_size=14)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And finally eLORETA:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "stc = apply_inverse(\n evoked, inv, lambda2, \"eLORETA\", verbose=True, method_params=dict(eps=1e-4)\n) # larger eps just for speed\nbrain = stc.plot(**kwargs)\nbrain.add_text(0.1, 0.9, \"eLORETA\", \"title\", font_size=14)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And one interesting property to note is the noise normalization of dSPM\ncan be easily seen by visualizing the source reconstruction of the noise\ncovariance used to compute the inverse operator -- it's takes on the\nvalue of 1. (orange in the colormap here) across the entire brain:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "stc_baseline = apply_inverse_cov(\n cov, evoked.info, inv, lambda2=lambda2, method=\"dSPM\", verbose=True\n)\nkwargs_baseline = kwargs.copy()\nkwargs_baseline[\"clim\"] = dict(kind=\"value\", lims=[0, 1, 2])\nbrain = stc_baseline.plot(**kwargs_baseline)\nbrain.add_text(0.1, 0.9, \"dSPM of the baseline\", \"title\", font_size=14)" ] } ], "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 }