{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n\n# Interpolate EEG data to any montage\n\nThis example demonstrates how to interpolate EEG channels to match a given montage.\nThis can be useful for standardizing\nEEG channel layouts across different datasets (see :footcite:`MellotEtAl2024`).\n\n- Using the field interpolation for EEG data.\n- Using the target montage \"biosemi16\".\n\nIn this example, the data from the original EEG channels will be\ninterpolated onto the positions defined by the \"biosemi16\" montage.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Authors: Antoine Collas \n# License: BSD-3-Clause\n# Copyright the MNE-Python contributors.\n\nimport matplotlib.pyplot as plt\n\nimport mne\nfrom mne.channels import make_standard_montage\nfrom mne.datasets import sample\n\nprint(__doc__)\nylim = (-10, 10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Load EEG data\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "data_path = sample.data_path()\neeg_file_path = data_path / \"MEG\" / \"sample\" / \"sample_audvis-ave.fif\"\nevoked = mne.read_evokeds(eeg_file_path, condition=\"Left Auditory\", baseline=(None, 0))\n\n# Select only EEG channels\nevoked.pick(\"eeg\")\n\n# Plot the original EEG layout\nevoked.plot(exclude=[], picks=\"eeg\", ylim=dict(eeg=ylim))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Define the target montage\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "standard_montage = make_standard_montage(\"biosemi16\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use interpolate_to to project EEG data to the standard montage\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "evoked_interpolated_spline = evoked.copy().interpolate_to(\n standard_montage, method=\"spline\"\n)\n\n# Plot the interpolated EEG layout\nevoked_interpolated_spline.plot(exclude=[], picks=\"eeg\", ylim=dict(eeg=ylim))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use interpolate_to to project EEG data to the standard montage\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "evoked_interpolated_mne = evoked.copy().interpolate_to(standard_montage, method=\"MNE\")\n\n# Plot the interpolated EEG layout\nevoked_interpolated_mne.plot(exclude=[], picks=\"eeg\", ylim=dict(eeg=ylim))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Comparing before and after interpolation\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, axs = plt.subplots(3, 1, figsize=(8, 6), constrained_layout=True)\nevoked.plot(exclude=[], picks=\"eeg\", axes=axs[0], show=False, ylim=dict(eeg=ylim))\naxs[0].set_title(\"Original EEG Layout\")\nevoked_interpolated_spline.plot(\n exclude=[], picks=\"eeg\", axes=axs[1], show=False, ylim=dict(eeg=ylim)\n)\naxs[1].set_title(\"Interpolated to Standard 1020 Montage using spline interpolation\")\nevoked_interpolated_mne.plot(\n exclude=[], picks=\"eeg\", axes=axs[2], show=False, ylim=dict(eeg=ylim)\n)\naxs[2].set_title(\"Interpolated to Standard 1020 Montage using MNE interpolation\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## References\n.. footbibliography::\n\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.3" } }, "nbformat": 4, "nbformat_minor": 0 }