{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n\n# Generate simulated evoked data\n\nUse :func:`~mne.simulation.simulate_sparse_stc` to simulate evoked data.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Author: Daniel Strohmeier \n# Alexandre Gramfort \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\nimport numpy as np\n\nimport mne\nfrom mne.datasets import sample\nfrom mne.simulation import simulate_evoked, simulate_sparse_stc\nfrom mne.time_frequency import fit_iir_model_raw\nfrom mne.viz import plot_sparse_source_estimates\n\nprint(__doc__)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Load real data as templates\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "data_path = sample.data_path()\nmeg_path = data_path / \"MEG\" / \"sample\"\nraw = mne.io.read_raw_fif(meg_path / \"sample_audvis_raw.fif\")\nproj = mne.read_proj(meg_path / \"sample_audvis_ecg-proj.fif\")\nraw.add_proj(proj)\nraw.info[\"bads\"] = [\"MEG 2443\", \"EEG 053\"] # mark bad channels\n\nfwd_fname = meg_path / \"sample_audvis-meg-eeg-oct-6-fwd.fif\"\nave_fname = meg_path / \"sample_audvis-no-filter-ave.fif\"\ncov_fname = meg_path / \"sample_audvis-cov.fif\"\n\nfwd = mne.read_forward_solution(fwd_fname)\nfwd = mne.pick_types_forward(fwd, meg=True, eeg=True, exclude=raw.info[\"bads\"])\ncov = mne.read_cov(cov_fname)\ninfo = mne.io.read_info(ave_fname)\n\nlabel_names = [\"Aud-lh\", \"Aud-rh\"]\nlabels = [mne.read_label(meg_path / \"labels\" / f\"{ln}.label\") for ln in label_names]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Generate source time courses from 2 dipoles and the corresponding evoked data\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "times = np.arange(300, dtype=np.float64) / raw.info[\"sfreq\"] - 0.1\nrng = np.random.RandomState(42)\n\n\ndef data_fun(times):\n \"\"\"Generate random source time courses.\"\"\"\n return (\n 50e-9\n * np.sin(30.0 * times)\n * np.exp(-((times - 0.15 + 0.05 * rng.randn(1)) ** 2) / 0.01)\n )\n\n\nstc = simulate_sparse_stc(\n fwd[\"src\"],\n n_dipoles=2,\n times=times,\n random_state=42,\n labels=labels,\n data_fun=data_fun,\n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Generate noisy evoked data\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "picks = mne.pick_types(raw.info, meg=True, exclude=\"bads\")\niir_filter = fit_iir_model_raw(raw, order=5, picks=picks, tmin=60, tmax=180)[1]\nnave = 100 # simulate average of 100 epochs\nevoked = simulate_evoked(\n fwd, stc, info, cov, nave=nave, use_cps=True, iir_filter=iir_filter\n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plot\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "plot_sparse_source_estimates(\n fwd[\"src\"], stc, bgcolor=(1, 1, 1), opacity=0.5, high_resolution=True\n)\n\nplt.figure()\nplt.psd(evoked.data[0])\n\nevoked.plot(time_unit=\"s\")" ] } ], "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 }