{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n\n# Maxwell filter data with movement compensation\n\nDemonstrate movement compensation on simulated data. The simulated data\ncontains bilateral activation of auditory cortices, repeated over 14\ndifferent head rotations (head center held fixed). See the following for\ndetails:\n\n https://github.com/mne-tools/mne-misc-data/blob/master/movement/simulate.py\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Authors: 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.preprocessing import maxwell_filter\n\nprint(__doc__)\n\ndata_path = mne.datasets.misc.data_path(verbose=True) / \"movement\"\n\nhead_pos = mne.chpi.read_head_pos(data_path / \"simulated_quats.pos\")\nraw = mne.io.read_raw_fif(data_path / \"simulated_movement_raw.fif\")\nraw_stat = mne.io.read_raw_fif(data_path / \"simulated_stationary_raw.fif\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Visualize the \"subject\" head movements. By providing the measurement\ninformation, the distance to the nearest sensor in each direction\n(e.g., left/right for the X direction, forward/backward for Y) can\nbe shown in blue, and the destination (if given) shown in red.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "mne.viz.plot_head_positions(\n head_pos, mode=\"traces\", destination=raw.info[\"dev_head_t\"], info=raw.info\n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This can also be visualized using a quiver.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "mne.viz.plot_head_positions(\n head_pos, mode=\"field\", destination=raw.info[\"dev_head_t\"], info=raw.info\n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Process our simulated raw data (taking into account head movements).\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# extract our resulting events\nevents = mne.find_events(raw, stim_channel=\"STI 014\")\nevents[:, 2] = 1\nraw.plot(events=events)\n\ntopo_kwargs = dict(times=[0, 0.1, 0.2], ch_type=\"mag\", vlim=(-500, 500))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First, take the average of stationary data (bilateral auditory patterns).\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "evoked_stat = mne.Epochs(raw_stat, events, 1, -0.2, 0.8).average()\nfig = evoked_stat.plot_topomap(**topo_kwargs)\nfig.suptitle(\"Stationary\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Second, take a naive average, which averages across epochs that have been\nsimulated to have different head positions and orientations, thereby\nspatially smearing the activity.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "epochs = mne.Epochs(raw, events, 1, -0.2, 0.8)\nevoked = epochs.average()\nfig = evoked.plot_topomap(**topo_kwargs)\nfig.suptitle(\"Moving: naive average\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Third, use raw movement compensation (restores pattern).\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "raw_sss = maxwell_filter(raw, head_pos=head_pos)\nevoked_raw_mc = mne.Epochs(raw_sss, events, 1, -0.2, 0.8).average()\nfig = evoked_raw_mc.plot_topomap(**topo_kwargs)\nfig.suptitle(\"Moving: movement compensated (raw)\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Fourth, use evoked movement compensation. For these data, which contain\nvery large rotations, it does not as cleanly restore the pattern.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "evoked_evo_mc = mne.epochs.average_movements(epochs, head_pos=head_pos)\nfig = evoked_evo_mc.plot_topomap(**topo_kwargs)\nfig.suptitle(\"Moving: movement compensated (evoked)\")" ] } ], "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 }