{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n\n# Extracting the time series of activations in a label\n\nWe first apply a dSPM inverse operator to get signed activations in a label\n(with positive and negative values) and we then compare different strategies\nto average the times series in a label. We compare a simple average, with an\naveraging using the dipoles normal (flip mode) and then a PCA,\nalso using a sign flip.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Authors: Alexandre Gramfort \n# 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 matplotlib.patheffects as path_effects\nimport matplotlib.pyplot as plt\n\nimport mne\nfrom mne.datasets import sample\nfrom mne.minimum_norm import apply_inverse, read_inverse_operator\n\nprint(__doc__)\n\ndata_path = sample.data_path()\nlabel = \"Aud-lh\"\nmeg_path = data_path / \"MEG\" / \"sample\"\nlabel_fname = meg_path / \"labels\" / f\"{label}.label\"\nfname_inv = meg_path / \"sample_audvis-meg-oct-6-meg-inv.fif\"\nfname_evoked = meg_path / \"sample_audvis-ave.fif\"\n\nsnr = 3.0\nlambda2 = 1.0 / snr**2\nmethod = \"dSPM\" # use dSPM method (could also be MNE or sLORETA)\n\n# Load data\nevoked = mne.read_evokeds(fname_evoked, condition=0, baseline=(None, 0))\ninverse_operator = read_inverse_operator(fname_inv)\nsrc = inverse_operator[\"src\"]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Compute inverse solution\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "pick_ori = \"normal\" # Get signed values to see the effect of sign flip\nstc = apply_inverse(evoked, inverse_operator, lambda2, method, pick_ori=pick_ori)\n\nlabel = mne.read_label(label_fname)\n\nstc_label = stc.in_label(label)\nmodes = (\"mean\", \"mean_flip\", \"pca_flip\")\ntcs = dict()\nfor mode in modes:\n tcs[mode] = stc.extract_label_time_course(label, src, mode=mode)\nprint(f\"Number of vertices : {len(stc_label.data)}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## View source activations\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, ax = plt.subplots(1, layout=\"constrained\")\nt = 1e3 * stc_label.times\nax.plot(t, stc_label.data.T, \"k\", linewidth=0.5, alpha=0.5)\npe = [\n path_effects.Stroke(linewidth=5, foreground=\"w\", alpha=0.5),\n path_effects.Normal(),\n]\nfor mode, tc in tcs.items():\n ax.plot(t, tc[0], linewidth=3, label=str(mode), path_effects=pe)\nxlim = t[[0, -1]]\nylim = [-27, 22]\nax.legend(loc=\"upper right\")\nax.set(\n xlabel=\"Time (ms)\",\n ylabel=\"Source amplitude\",\n title=f\"Activations in Label {label.name!r}\",\n xlim=xlim,\n ylim=ylim,\n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using vector solutions\nIt's also possible to compute label time courses for a\n:class:`mne.VectorSourceEstimate`, but only with ``mode='mean'``.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "pick_ori = \"vector\"\nstc_vec = apply_inverse(evoked, inverse_operator, lambda2, method, pick_ori=pick_ori)\ndata = stc_vec.extract_label_time_course(label, src)\nfig, ax = plt.subplots(1, layout=\"constrained\")\nstc_vec_label = stc_vec.in_label(label)\ncolors = [\"#EE6677\", \"#228833\", \"#4477AA\"]\nfor ii, name in enumerate(\"XYZ\"):\n color = colors[ii]\n ax.plot(\n t, stc_vec_label.data[:, ii].T, color=color, lw=0.5, alpha=0.5, zorder=5 - ii\n )\n ax.plot(\n t,\n data[0, ii],\n lw=3,\n color=color,\n label=\"+\" + name,\n zorder=8 - ii,\n path_effects=pe,\n )\nax.legend(loc=\"upper right\")\nax.set(\n xlabel=\"Time (ms)\",\n ylabel=\"Source amplitude\",\n title=f\"Mean vector activations in Label {label.name!r}\",\n xlim=xlim,\n ylim=ylim,\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 }