{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n\n# Extracting time course from source_estimate object\n\nLoad a SourceEstimate object from stc files and\nextract the time course of activation in\nindividual labels, as well as in a complex label\nformed through merging two labels.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Author: Christian Brodbeck \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.pyplot as plt\n\nimport mne\nfrom mne.datasets import sample\n\nprint(__doc__)\n\ndata_path = sample.data_path()\nsubjects_dir = data_path / \"subjects\"\nmeg_path = data_path / \"MEG\" / \"sample\"\n\n# load the stc\nstc = mne.read_source_estimate(meg_path / \"sample_audvis-meg\")\n\n# load the labels\naud_lh = mne.read_label(meg_path / \"labels\" / \"Aud-lh.label\")\naud_rh = mne.read_label(meg_path / \"labels\" / \"Aud-rh.label\")\n\n# extract the time course for different labels from the stc\nstc_lh = stc.in_label(aud_lh)\nstc_rh = stc.in_label(aud_rh)\nstc_bh = stc.in_label(aud_lh + aud_rh)\n\n# calculate center of mass and transform to mni coordinates\nvtx, _, t_lh = stc_lh.center_of_mass(\"sample\", subjects_dir=subjects_dir)\nmni_lh = mne.vertex_to_mni(vtx, 0, \"sample\", subjects_dir=subjects_dir)[0]\nvtx, _, t_rh = stc_rh.center_of_mass(\"sample\", subjects_dir=subjects_dir)\nmni_rh = mne.vertex_to_mni(vtx, 1, \"sample\", subjects_dir=subjects_dir)[0]\n\n# plot the activation\nplt.figure()\nplt.axes([0.1, 0.275, 0.85, 0.625])\nhl = plt.plot(stc.times, stc_lh.data.mean(0), \"b\")[0]\nhr = plt.plot(stc.times, stc_rh.data.mean(0), \"g\")[0]\nhb = plt.plot(stc.times, stc_bh.data.mean(0), \"r\")[0]\nplt.xlabel(\"Time (s)\")\nplt.ylabel(\"Source amplitude (dSPM)\")\nplt.xlim(stc.times[0], stc.times[-1])\n\n# add a legend including center-of-mass mni coordinates to the plot\nlabels = [\n f\"LH: center of mass = {mni_lh.round(2)}\",\n f\"RH: center of mass = {mni_rh.round(2)}\",\n \"Combined LH & RH\",\n]\nplt.figlegend([hl, hr, hb], labels, loc=\"lower center\")\nplt.suptitle(\"Average activation in auditory cortex labels\", fontsize=20)\nplt.show()" ] } ], "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 }