{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n\n# Generate simulated source data\n\nThis example illustrates how to use the :class:`mne.simulation.SourceSimulator`\nclass to generate source estimates and raw data. It is meant to be a brief\nintroduction and only highlights the simplest use case.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Author: Kostiantyn Maksymenko \n# Samuel Deslauriers-Gauthier \n#\n# License: BSD-3-Clause\n# Copyright the MNE-Python contributors." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\n\nimport mne\nfrom mne.datasets import sample\n\nprint(__doc__)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For this example, we will be using the information of the sample subject.\nThis will download the data if it not already on your machine. We also set\nthe subjects directory so we don't need to give it to functions.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "data_path = sample.data_path()\nsubjects_dir = data_path / \"subjects\"\nsubject = \"sample\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First, we get an info structure from the test subject.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "evoked_fname = data_path / \"MEG\" / subject / \"sample_audvis-ave.fif\"\ninfo = mne.io.read_info(evoked_fname)\ntstep = 1.0 / info[\"sfreq\"]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To simulate sources, we also need a source space. It can be obtained from the\nforward solution of the sample subject.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fwd_fname = data_path / \"MEG\" / subject / \"sample_audvis-meg-eeg-oct-6-fwd.fif\"\nfwd = mne.read_forward_solution(fwd_fname)\nsrc = fwd[\"src\"]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To select a region to activate, we use the caudal middle frontal to grow\na region of interest.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "selected_label = mne.read_labels_from_annot(\n subject, regexp=\"caudalmiddlefrontal-lh\", subjects_dir=subjects_dir\n)[0]\nlocation = \"center\" # Use the center of the region as a seed.\nextent = 10.0 # Extent in mm of the region.\nlabel = mne.label.select_sources(\n subject, selected_label, location=location, extent=extent, subjects_dir=subjects_dir\n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Define the time course of the activity for each source of the region to\nactivate. Here we use a sine wave at 18 Hz with a peak amplitude\nof 10 nAm.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "source_time_series = np.sin(2.0 * np.pi * 18.0 * np.arange(100) * tstep) * 10e-9" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Define when the activity occurs using events. The first column is the sample\nof the event, the second is not used, and the third is the event id. Here the\nevents occur every 200 samples.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "n_events = 50\nevents = np.zeros((n_events, 3), int)\nevents[:, 0] = 100 + 200 * np.arange(n_events) # Events sample.\nevents[:, 2] = 1 # All events have the sample id." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create simulated source activity. Here we use a SourceSimulator whose\nadd_data method is key. It specified where (label), what\n(source_time_series), and when (events) an event type will occur.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "source_simulator = mne.simulation.SourceSimulator(src, tstep=tstep)\nsource_simulator.add_data(label, source_time_series, events)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Project the source time series to sensor space and add some noise. The source\nsimulator can be given directly to the simulate_raw function.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "raw = mne.simulation.simulate_raw(info, source_simulator, forward=fwd)\ncov = mne.make_ad_hoc_cov(raw.info)\nmne.simulation.add_noise(raw, cov, iir_filter=[0.2, -0.2, 0.04])\nraw.plot()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plot evoked data to get another view of the simulated raw data.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "events = mne.find_events(raw)\nepochs = mne.Epochs(raw, events, 1, tmin=-0.05, tmax=0.2)\nevoked = epochs.average()\nevoked.plot()" ] } ], "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 }