{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n\n# Generate simulated raw data\n\nThis example generates raw data by repeating a desired source activation\nmultiple times.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Authors: Yousra Bekhti \n# Mark Wronkiewicz \n# 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 matplotlib.pyplot as plt\nimport numpy as np\n\nimport mne\nfrom mne import Epochs, compute_covariance, find_events, make_ad_hoc_cov\nfrom mne.datasets import sample\nfrom mne.simulation import (\n add_ecg,\n add_eog,\n add_noise,\n simulate_raw,\n simulate_sparse_stc,\n)\n\nprint(__doc__)\n\ndata_path = sample.data_path()\nmeg_path = data_path / \"MEG\" / \"sample\"\nraw_fname = meg_path / \"sample_audvis_raw.fif\"\nfwd_fname = meg_path / \"sample_audvis-meg-eeg-oct-6-fwd.fif\"\n\n# Load real data as the template\nraw = mne.io.read_raw_fif(raw_fname)\nraw.set_eeg_reference(projection=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Generate dipole time series\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "n_dipoles = 4 # number of dipoles to create\nepoch_duration = 2.0 # duration of each epoch/event\nn = 0 # harmonic number\nrng = np.random.RandomState(0) # random state (make reproducible)\n\n\ndef data_fun(times):\n \"\"\"Generate time-staggered sinusoids at harmonics of 10Hz.\"\"\"\n global n\n n_samp = len(times)\n window = np.zeros(n_samp)\n start, stop = (\n int(ii * float(n_samp) / (2 * n_dipoles)) for ii in (2 * n, 2 * n + 1)\n )\n window[start:stop] = 1.0\n n += 1\n data = 25e-9 * np.sin(2.0 * np.pi * 10.0 * n * times)\n data *= window\n return data\n\n\ntimes = raw.times[: int(raw.info[\"sfreq\"] * epoch_duration)]\nfwd = mne.read_forward_solution(fwd_fname)\nsrc = fwd[\"src\"]\nstc = simulate_sparse_stc(\n src, n_dipoles=n_dipoles, times=times, data_fun=data_fun, random_state=rng\n)\n# look at our source data\nfig, ax = plt.subplots(1)\nax.plot(times, 1e9 * stc.data.T)\nax.set(ylabel=\"Amplitude (nAm)\", xlabel=\"Time (s)\")\nmne.viz.utils.plt_show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Simulate raw data\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "raw_sim = simulate_raw(raw.info, [stc] * 10, forward=fwd, verbose=True)\ncov = make_ad_hoc_cov(raw_sim.info)\nadd_noise(raw_sim, cov, iir_filter=[0.2, -0.2, 0.04], random_state=rng)\nadd_ecg(raw_sim, random_state=rng)\nadd_eog(raw_sim, random_state=rng)\nraw_sim.plot()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plot evoked data\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "events = find_events(raw_sim) # only 1 pos, so event number == 1\nepochs = Epochs(raw_sim, events, 1, tmin=-0.2, tmax=epoch_duration)\ncov = compute_covariance(\n epochs, tmax=0.0, method=\"empirical\", verbose=\"error\"\n) # quick calc\nevoked = epochs.average()\nevoked.plot_white(cov, 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 }