Minimal example#

Below you can find an example script that contains all the ideas showcased in the Getting Started tutorial. It may serve as a good starting point for your own simulation.

import numpy as np
import mne

from mne.datasets import sample

from meegsim.coupling import ppc_von_mises
from meegsim.location import select_random
from meegsim.simulate import SourceSimulator
from meegsim.waveform import narrowband_oscillation

First, we need to load all the prerequisites for our simulation:

  • src - the mne.SourceSpaces object that describes all candidate source locations

  • fwd - the mne.Forward object that describes the forward model

  • info - the mne.Info object that describes the channel layout

In this simulation, we only use the EEG channels.

# Paths
data_path = sample.data_path() / "MEG" / "sample"
fwd_path = data_path / "sample_audvis-meg-eeg-oct-6-fwd.fif"
raw_path = data_path / "sample_audvis_raw.fif"

# Load the prerequisites: fwd, src, and info
fwd = mne.read_forward_solution(fwd_path)
fwd = mne.convert_forward_solution(fwd, force_fixed=True)
raw = mne.io.read_raw(raw_path)
src = fwd["src"]
info = raw.info

# Pick EEG channels only
eeg_idx = mne.pick_types(info, eeg=True)
info_eeg = mne.pick_info(info, eeg_idx)
fwd_eeg = fwd.pick_channels(info_eeg.ch_names)
Using default location ~/mne_data for sample...
Creating /home/docs/mne_data
Downloading file 'MNE-sample-data-processed.tar.gz' from 'https://osf.io/86qa2/download?version=6' to '/home/docs/mne_data'.

  0%|                                              | 0.00/1.65G [00:00<?, ?B/s]
  0%|                                    | 36.9k/1.65G [00:00<1:28:26, 311kB/s]
  0%|▏                                    | 5.87M/1.65G [00:00<00:51, 32.0MB/s]
  1%|▎                                    | 14.2M/1.65G [00:00<00:29, 54.6MB/s]
  1%|▍                                    | 22.2M/1.65G [00:00<00:25, 64.3MB/s]
  2%|▋                                    | 30.0M/1.65G [00:00<00:23, 69.2MB/s]
  2%|▊                                    | 37.0M/1.65G [00:00<00:25, 63.6MB/s]
  3%|▉                                    | 43.5M/1.65G [00:00<00:38, 41.4MB/s]
  3%|█▏                                   | 52.0M/1.65G [00:01<00:31, 50.8MB/s]
  4%|█▎                                   | 60.4M/1.65G [00:01<00:27, 58.8MB/s]
  4%|█▌                                   | 68.8M/1.65G [00:01<00:24, 65.4MB/s]
  5%|█▋                                   | 76.2M/1.65G [00:01<00:23, 67.0MB/s]
  5%|█▉                                   | 84.8M/1.65G [00:01<00:21, 72.0MB/s]
  6%|██                                   | 92.5M/1.65G [00:01<00:21, 73.6MB/s]
  6%|██▎                                   | 101M/1.65G [00:01<00:20, 77.0MB/s]
  7%|██▌                                   | 110M/1.65G [00:01<00:19, 79.5MB/s]
  7%|██▋                                   | 118M/1.65G [00:01<00:18, 81.2MB/s]
  8%|██▉                                   | 127M/1.65G [00:01<00:18, 82.6MB/s]
  8%|███                                   | 135M/1.65G [00:02<00:18, 83.5MB/s]
  9%|███▎                                  | 144M/1.65G [00:02<00:17, 84.0MB/s]
  9%|███▌                                  | 152M/1.65G [00:02<00:18, 80.8MB/s]
 10%|███▋                                  | 160M/1.65G [00:02<00:21, 69.0MB/s]
 10%|███▊                                  | 168M/1.65G [00:02<00:35, 41.5MB/s]
 11%|████                                  | 175M/1.65G [00:02<00:30, 47.7MB/s]
 11%|████▏                                 | 183M/1.65G [00:02<00:26, 54.7MB/s]
 12%|████▍                                 | 192M/1.65G [00:03<00:23, 60.9MB/s]
 12%|████▌                                 | 200M/1.65G [00:03<00:21, 66.5MB/s]
 13%|████▊                                 | 208M/1.65G [00:03<00:24, 58.2MB/s]
 13%|████▉                                 | 214M/1.65G [00:03<00:35, 40.5MB/s]
 13%|█████                                 | 220M/1.65G [00:03<00:32, 44.1MB/s]
 14%|█████▏                                | 226M/1.65G [00:03<00:31, 45.6MB/s]
 14%|█████▎                                | 231M/1.65G [00:03<00:30, 47.3MB/s]
 14%|█████▍                                | 237M/1.65G [00:04<00:27, 51.3MB/s]
 15%|█████▌                                | 243M/1.65G [00:04<00:26, 53.7MB/s]
 15%|█████▋                                | 249M/1.65G [00:04<00:25, 54.5MB/s]
 15%|█████▊                                | 255M/1.65G [00:04<00:28, 49.1MB/s]
 16%|█████▉                                | 260M/1.65G [00:04<00:30, 45.1MB/s]
 16%|██████                                | 266M/1.65G [00:04<00:29, 47.6MB/s]
 16%|██████▏                               | 271M/1.65G [00:04<00:27, 50.2MB/s]
 17%|██████▍                               | 277M/1.65G [00:04<00:26, 52.6MB/s]
 17%|██████▌                               | 283M/1.65G [00:04<00:26, 51.0MB/s]
 18%|██████▋                               | 290M/1.65G [00:05<00:26, 52.1MB/s]
 18%|██████▊                               | 296M/1.65G [00:05<00:28, 46.9MB/s]
 18%|██████▉                               | 301M/1.65G [00:05<00:27, 48.3MB/s]
 19%|███████                               | 306M/1.65G [00:05<00:26, 49.9MB/s]
 19%|███████▏                              | 312M/1.65G [00:05<00:31, 42.5MB/s]
 19%|███████▎                              | 316M/1.65G [00:05<00:32, 41.3MB/s]
 19%|███████▎                              | 320M/1.65G [00:05<00:32, 41.4MB/s]
 20%|███████▍                              | 325M/1.65G [00:05<00:32, 41.1MB/s]
 20%|███████▌                              | 329M/1.65G [00:06<00:33, 40.1MB/s]
 20%|███████▊                              | 337M/1.65G [00:06<00:25, 52.0MB/s]
 21%|███████▊                              | 342M/1.65G [00:06<00:28, 46.7MB/s]
 21%|███████▉                              | 347M/1.65G [00:06<00:28, 45.3MB/s]
 21%|████████                              | 352M/1.65G [00:06<00:31, 40.7MB/s]
 22%|████████▏                             | 356M/1.65G [00:06<00:31, 41.7MB/s]
 22%|████████▎                             | 361M/1.65G [00:06<00:29, 43.1MB/s]
 22%|████████▍                             | 366M/1.65G [00:06<00:37, 34.3MB/s]
 22%|████████▍                             | 369M/1.65G [00:07<00:36, 35.6MB/s]
 23%|████████▌                             | 373M/1.65G [00:07<01:10, 18.2MB/s]
 23%|████████▊                             | 382M/1.65G [00:07<00:45, 28.2MB/s]
 24%|████████▉                             | 390M/1.65G [00:07<00:32, 38.5MB/s]
 24%|█████████▏                            | 399M/1.65G [00:07<00:26, 48.0MB/s]
 25%|█████████▎                            | 407M/1.65G [00:07<00:22, 56.6MB/s]
 25%|█████████▌                            | 414M/1.65G [00:08<00:21, 57.8MB/s]
 26%|█████████▋                            | 423M/1.65G [00:08<00:20, 59.0MB/s]
 26%|█████████▊                            | 429M/1.65G [00:08<00:25, 47.3MB/s]
 26%|██████████                            | 435M/1.65G [00:08<00:26, 45.5MB/s]
 27%|██████████▏                           | 441M/1.65G [00:08<00:30, 40.3MB/s]
 27%|██████████▏                           | 446M/1.65G [00:08<00:32, 37.3MB/s]
 27%|██████████▎                           | 450M/1.65G [00:09<00:37, 31.8MB/s]
 27%|██████████▍                           | 453M/1.65G [00:09<00:49, 24.4MB/s]
 28%|██████████▌                           | 457M/1.65G [00:09<00:45, 26.3MB/s]
 28%|██████████▌                           | 460M/1.65G [00:09<00:44, 26.9MB/s]
 28%|██████████▋                           | 463M/1.65G [00:09<00:41, 28.9MB/s]
 28%|██████████▋                           | 467M/1.65G [00:09<00:38, 31.2MB/s]
 29%|██████████▊                           | 472M/1.65G [00:09<00:33, 35.5MB/s]
 29%|██████████▉                           | 478M/1.65G [00:09<00:27, 42.5MB/s]
 29%|███████████                           | 483M/1.65G [00:10<00:28, 40.6MB/s]
 30%|███████████▎                          | 491M/1.65G [00:10<00:24, 47.1MB/s]
 30%|███████████▍                          | 496M/1.65G [00:10<00:36, 31.9MB/s]
 30%|███████████▍                          | 500M/1.65G [00:10<00:40, 28.3MB/s]
 30%|███████████▌                          | 503M/1.65G [00:10<00:46, 24.6MB/s]
 31%|███████████▋                          | 506M/1.65G [00:11<00:45, 24.9MB/s]
 31%|███████████▋                          | 509M/1.65G [00:11<00:48, 23.5MB/s]
 31%|███████████▊                          | 512M/1.65G [00:11<00:47, 24.0MB/s]
 31%|███████████▊                          | 516M/1.65G [00:11<00:41, 27.7MB/s]
 31%|███████████▉                          | 519M/1.65G [00:11<00:41, 27.5MB/s]
 32%|████████████                          | 527M/1.65G [00:11<00:28, 39.5MB/s]
 32%|████████████▏                         | 531M/1.65G [00:11<00:35, 31.9MB/s]
 32%|████████████▎                         | 536M/1.65G [00:11<00:33, 33.8MB/s]
 33%|████████████▍                         | 539M/1.65G [00:12<01:11, 15.6MB/s]
 33%|████████████▌                         | 548M/1.65G [00:12<00:44, 24.8MB/s]
 34%|████████████▊                         | 556M/1.65G [00:12<00:31, 34.7MB/s]
 34%|████████████▉                         | 565M/1.65G [00:12<00:24, 44.2MB/s]
 35%|█████████████▏                        | 572M/1.65G [00:12<00:21, 49.6MB/s]
 35%|█████████████▎                        | 580M/1.65G [00:13<00:18, 58.0MB/s]
 36%|█████████████▌                        | 588M/1.65G [00:13<00:32, 33.2MB/s]
 36%|█████████████▋                        | 596M/1.65G [00:13<00:25, 40.8MB/s]
 37%|█████████████▉                        | 604M/1.65G [00:13<00:21, 48.7MB/s]
 37%|██████████████                        | 612M/1.65G [00:13<00:19, 54.2MB/s]
 37%|██████████████▏                       | 619M/1.65G [00:13<00:17, 58.9MB/s]
 38%|██████████████▍                       | 628M/1.65G [00:13<00:15, 65.7MB/s]
 38%|██████████████▌                       | 635M/1.65G [00:14<00:18, 53.9MB/s]
 39%|██████████████▊                       | 642M/1.65G [00:14<00:18, 55.5MB/s]
 39%|██████████████▉                       | 648M/1.65G [00:14<00:23, 43.0MB/s]
 40%|███████████████                       | 657M/1.65G [00:14<00:19, 51.7MB/s]
 40%|███████████████▎                      | 665M/1.65G [00:14<00:16, 59.3MB/s]
 41%|███████████████▍                      | 673M/1.65G [00:14<00:15, 65.1MB/s]
 41%|███████████████▋                      | 682M/1.65G [00:14<00:13, 70.3MB/s]
 42%|███████████████▉                      | 691M/1.65G [00:15<00:12, 74.4MB/s]
 42%|████████████████                      | 698M/1.65G [00:15<00:27, 35.1MB/s]
 43%|████████████████▏                     | 705M/1.65G [00:15<00:23, 39.8MB/s]
 43%|████████████████▍                     | 713M/1.65G [00:15<00:19, 47.0MB/s]
 44%|████████████████▌                     | 722M/1.65G [00:15<00:16, 55.1MB/s]
 44%|████████████████▊                     | 730M/1.65G [00:15<00:14, 62.1MB/s]
 45%|████████████████▉                     | 739M/1.65G [00:16<00:13, 67.7MB/s]
 45%|█████████████████▏                    | 747M/1.65G [00:16<00:13, 68.7MB/s]
 46%|█████████████████▎                    | 755M/1.65G [00:16<00:12, 73.1MB/s]
 46%|█████████████████▌                    | 764M/1.65G [00:16<00:11, 76.6MB/s]
 47%|█████████████████▊                    | 772M/1.65G [00:16<00:11, 79.0MB/s]
 47%|█████████████████▉                    | 780M/1.65G [00:16<00:11, 76.5MB/s]
 48%|██████████████████▏                   | 788M/1.65G [00:16<00:12, 67.4MB/s]
 48%|██████████████████▎                   | 795M/1.65G [00:16<00:16, 52.9MB/s]
 48%|██████████████████▍                   | 801M/1.65G [00:17<00:22, 37.6MB/s]
 49%|██████████████████▌                   | 806M/1.65G [00:17<00:22, 37.9MB/s]
 49%|██████████████████▋                   | 811M/1.65G [00:17<00:22, 37.3MB/s]
 49%|██████████████████▊                   | 817M/1.65G [00:17<00:19, 42.4MB/s]
 50%|██████████████████▉                   | 824M/1.65G [00:17<00:16, 49.1MB/s]
 50%|███████████████████                   | 829M/1.65G [00:17<00:17, 47.7MB/s]
 51%|███████████████████▏                  | 835M/1.65G [00:17<00:16, 48.5MB/s]
 51%|███████████████████▎                  | 840M/1.65G [00:18<00:18, 44.2MB/s]
 51%|███████████████████▍                  | 848M/1.65G [00:18<00:15, 52.5MB/s]
 52%|███████████████████▌                  | 853M/1.65G [00:18<00:15, 53.1MB/s]
 52%|███████████████████▊                  | 859M/1.65G [00:18<00:14, 55.0MB/s]
 52%|███████████████████▉                  | 868M/1.65G [00:18<00:12, 63.2MB/s]
 53%|████████████████████▏                 | 876M/1.65G [00:18<00:11, 69.6MB/s]
 53%|████████████████████▎                 | 883M/1.65G [00:18<00:14, 51.5MB/s]
 54%|████████████████████▌                 | 892M/1.65G [00:18<00:12, 59.3MB/s]
 54%|████████████████████▋                 | 898M/1.65G [00:18<00:12, 59.2MB/s]
 55%|████████████████████▊                 | 905M/1.65G [00:19<00:13, 57.5MB/s]
 55%|████████████████████▉                 | 911M/1.65G [00:19<00:13, 53.2MB/s]
 55%|█████████████████████                 | 917M/1.65G [00:19<00:14, 49.3MB/s]
 56%|█████████████████████▏                | 922M/1.65G [00:19<00:17, 41.4MB/s]
 56%|█████████████████████▎                | 926M/1.65G [00:19<00:17, 41.2MB/s]
 56%|█████████████████████▍                | 932M/1.65G [00:19<00:16, 44.0MB/s]
 57%|█████████████████████▌                | 940M/1.65G [00:19<00:13, 53.9MB/s]
 57%|█████████████████████▊                | 946M/1.65G [00:20<00:24, 29.3MB/s]
 58%|█████████████████████▉                | 955M/1.65G [00:20<00:18, 38.3MB/s]
 58%|██████████████████████▏               | 963M/1.65G [00:20<00:14, 46.4MB/s]
 59%|██████████████████████▎               | 971M/1.65G [00:20<00:12, 54.8MB/s]
 59%|██████████████████████▌               | 980M/1.65G [00:20<00:10, 61.9MB/s]
 60%|██████████████████████▋               | 987M/1.65G [00:20<00:10, 63.2MB/s]
 60%|██████████████████████▊               | 994M/1.65G [00:21<00:16, 40.0MB/s]
 61%|██████████████████████▍              | 1.00G/1.65G [00:21<00:13, 47.8MB/s]
 61%|██████████████████████▋              | 1.01G/1.65G [00:21<00:11, 55.6MB/s]
 62%|██████████████████████▊              | 1.02G/1.65G [00:21<00:10, 62.2MB/s]
 62%|███████████████████████              | 1.03G/1.65G [00:21<00:09, 67.9MB/s]
 63%|███████████████████████▏             | 1.04G/1.65G [00:21<00:08, 72.1MB/s]
 63%|███████████████████████▍             | 1.04G/1.65G [00:21<00:09, 66.3MB/s]
 64%|███████████████████████▌             | 1.05G/1.65G [00:21<00:08, 71.2MB/s]
 64%|███████████████████████▊             | 1.06G/1.65G [00:22<00:07, 75.1MB/s]
 65%|███████████████████████▉             | 1.07G/1.65G [00:22<00:07, 77.9MB/s]
 65%|████████████████████████▏            | 1.08G/1.65G [00:22<00:07, 79.8MB/s]
 66%|████████████████████████▎            | 1.09G/1.65G [00:22<00:06, 81.2MB/s]
 66%|████████████████████████▌            | 1.10G/1.65G [00:22<00:06, 81.9MB/s]
 67%|████████████████████████▋            | 1.10G/1.65G [00:22<00:06, 81.0MB/s]
 67%|████████████████████████▉            | 1.11G/1.65G [00:22<00:06, 80.0MB/s]
 68%|█████████████████████████            | 1.12G/1.65G [00:22<00:06, 78.2MB/s]
 68%|█████████████████████████▎           | 1.13G/1.65G [00:22<00:06, 80.0MB/s]
 69%|█████████████████████████▍           | 1.14G/1.65G [00:22<00:06, 80.5MB/s]
 69%|█████████████████████████▋           | 1.14G/1.65G [00:23<00:09, 52.4MB/s]
 70%|█████████████████████████▊           | 1.15G/1.65G [00:23<00:10, 46.2MB/s]
 70%|█████████████████████████▉           | 1.16G/1.65G [00:23<00:16, 29.6MB/s]
 70%|██████████████████████████           | 1.16G/1.65G [00:23<00:14, 32.8MB/s]
 71%|██████████████████████████▏          | 1.17G/1.65G [00:24<00:12, 38.0MB/s]
 71%|██████████████████████████▎          | 1.17G/1.65G [00:24<00:12, 39.4MB/s]
 71%|██████████████████████████▍          | 1.18G/1.65G [00:24<00:11, 39.9MB/s]
 72%|██████████████████████████▌          | 1.19G/1.65G [00:24<00:09, 49.6MB/s]
 72%|██████████████████████████▋          | 1.19G/1.65G [00:24<00:12, 37.2MB/s]
 73%|██████████████████████████▊          | 1.20G/1.65G [00:24<00:10, 42.8MB/s]
 73%|██████████████████████████▉          | 1.20G/1.65G [00:24<00:10, 43.0MB/s]
 73%|███████████████████████████          | 1.21G/1.65G [00:25<00:15, 28.2MB/s]
 73%|███████████████████████████▏         | 1.21G/1.65G [00:25<00:15, 29.3MB/s]
 74%|███████████████████████████▏         | 1.22G/1.65G [00:25<00:14, 31.0MB/s]
 74%|███████████████████████████▎         | 1.22G/1.65G [00:25<00:14, 29.1MB/s]
 74%|███████████████████████████▍         | 1.23G/1.65G [00:25<00:12, 33.0MB/s]
 74%|███████████████████████████▌         | 1.23G/1.65G [00:26<00:20, 20.7MB/s]
 75%|███████████████████████████▋         | 1.24G/1.65G [00:26<00:13, 31.1MB/s]
 75%|███████████████████████████▉         | 1.25G/1.65G [00:26<00:10, 40.5MB/s]
 76%|████████████████████████████         | 1.25G/1.65G [00:26<00:08, 49.5MB/s]
 76%|████████████████████████████▏        | 1.26G/1.65G [00:26<00:07, 51.6MB/s]
 77%|████████████████████████████▍        | 1.27G/1.65G [00:26<00:06, 59.5MB/s]
 77%|████████████████████████████▌        | 1.28G/1.65G [00:27<00:14, 26.4MB/s]
 77%|████████████████████████████▋        | 1.28G/1.65G [00:28<00:28, 13.3MB/s]
 78%|████████████████████████████▊        | 1.28G/1.65G [00:29<00:39, 9.39MB/s]
 78%|████████████████████████████▊        | 1.29G/1.65G [00:29<00:44, 8.25MB/s]
 78%|████████████████████████████▊        | 1.29G/1.65G [00:29<00:46, 7.74MB/s]
 78%|████████████████████████████▉        | 1.29G/1.65G [00:30<00:54, 6.64MB/s]
 78%|████████████████████████████▉        | 1.29G/1.65G [00:30<00:55, 6.53MB/s]
 78%|████████████████████████████▉        | 1.29G/1.65G [00:30<01:00, 5.99MB/s]
 78%|████████████████████████████▉        | 1.29G/1.65G [00:31<01:04, 5.59MB/s]
 78%|████████████████████████████▉        | 1.29G/1.65G [00:31<01:16, 4.65MB/s]
 78%|█████████████████████████████        | 1.30G/1.65G [00:31<01:13, 4.87MB/s]
 78%|█████████████████████████████        | 1.30G/1.65G [00:31<01:07, 5.25MB/s]
 79%|█████████████████████████████        | 1.30G/1.65G [00:31<00:55, 6.44MB/s]
 79%|█████████████████████████████▏       | 1.31G/1.65G [00:31<00:17, 19.9MB/s]
 79%|█████████████████████████████▍       | 1.31G/1.65G [00:31<00:10, 33.5MB/s]
 80%|█████████████████████████████▌       | 1.32G/1.65G [00:31<00:07, 42.9MB/s]
 80%|█████████████████████████████▊       | 1.33G/1.65G [00:32<00:06, 53.3MB/s]
 81%|█████████████████████████████▉       | 1.34G/1.65G [00:32<00:05, 61.6MB/s]
 81%|██████████████████████████████▏      | 1.35G/1.65G [00:32<00:04, 67.9MB/s]
 82%|██████████████████████████████▎      | 1.35G/1.65G [00:32<00:04, 72.6MB/s]
 82%|██████████████████████████████▌      | 1.36G/1.65G [00:32<00:03, 76.0MB/s]
 83%|██████████████████████████████▋      | 1.37G/1.65G [00:32<00:07, 36.7MB/s]
 83%|██████████████████████████████▊      | 1.38G/1.65G [00:33<00:08, 31.1MB/s]
 84%|██████████████████████████████▉      | 1.38G/1.65G [00:33<00:08, 30.9MB/s]
 84%|███████████████████████████████      | 1.39G/1.65G [00:33<00:08, 32.5MB/s]
 84%|███████████████████████████████▏     | 1.39G/1.65G [00:33<00:07, 34.0MB/s]
 84%|███████████████████████████████▏     | 1.40G/1.65G [00:33<00:07, 36.5MB/s]
 85%|███████████████████████████████▎     | 1.40G/1.65G [00:33<00:06, 40.8MB/s]
 85%|███████████████████████████████▌     | 1.41G/1.65G [00:33<00:04, 50.4MB/s]
 86%|███████████████████████████████▋     | 1.42G/1.65G [00:34<00:03, 59.2MB/s]
 86%|███████████████████████████████▉     | 1.43G/1.65G [00:34<00:03, 65.4MB/s]
 87%|████████████████████████████████     | 1.43G/1.65G [00:34<00:03, 69.5MB/s]
 87%|████████████████████████████████▎    | 1.44G/1.65G [00:34<00:03, 56.6MB/s]
 88%|████████████████████████████████▍    | 1.45G/1.65G [00:34<00:03, 63.6MB/s]
 88%|████████████████████████████████▌    | 1.46G/1.65G [00:34<00:04, 41.9MB/s]
 88%|████████████████████████████████▋    | 1.46G/1.65G [00:35<00:05, 32.2MB/s]
 89%|████████████████████████████████▊    | 1.47G/1.65G [00:35<00:05, 33.2MB/s]
 89%|████████████████████████████████▉    | 1.47G/1.65G [00:35<00:05, 35.7MB/s]
 89%|█████████████████████████████████    | 1.48G/1.65G [00:35<00:04, 41.2MB/s]
 90%|█████████████████████████████████▏   | 1.48G/1.65G [00:35<00:05, 29.3MB/s]
 90%|█████████████████████████████████▍   | 1.49G/1.65G [00:35<00:04, 38.7MB/s]
 91%|█████████████████████████████████▌   | 1.50G/1.65G [00:35<00:03, 48.2MB/s]
 91%|█████████████████████████████████▋   | 1.51G/1.65G [00:36<00:02, 53.8MB/s]
 92%|█████████████████████████████████▉   | 1.52G/1.65G [00:36<00:02, 61.7MB/s]
 92%|██████████████████████████████████   | 1.52G/1.65G [00:36<00:01, 65.7MB/s]
 93%|██████████████████████████████████▎  | 1.53G/1.65G [00:36<00:01, 62.9MB/s]
 93%|██████████████████████████████████▍  | 1.54G/1.65G [00:36<00:02, 41.1MB/s]
 93%|██████████████████████████████████▌  | 1.55G/1.65G [00:36<00:02, 48.9MB/s]
 94%|██████████████████████████████████▋  | 1.55G/1.65G [00:36<00:02, 46.5MB/s]
 94%|██████████████████████████████████▉  | 1.56G/1.65G [00:37<00:01, 53.7MB/s]
 95%|███████████████████████████████████  | 1.57G/1.65G [00:37<00:01, 60.5MB/s]
 95%|███████████████████████████████████▎ | 1.58G/1.65G [00:37<00:01, 66.1MB/s]
 96%|███████████████████████████████████▍ | 1.59G/1.65G [00:37<00:00, 70.9MB/s]
 96%|███████████████████████████████████▋ | 1.59G/1.65G [00:37<00:00, 74.7MB/s]
 97%|███████████████████████████████████▊ | 1.60G/1.65G [00:37<00:00, 77.3MB/s]
 97%|████████████████████████████████████ | 1.61G/1.65G [00:37<00:00, 78.0MB/s]
 98%|████████████████████████████████████▏| 1.62G/1.65G [00:37<00:00, 79.9MB/s]
 98%|████████████████████████████████████▍| 1.63G/1.65G [00:37<00:00, 77.9MB/s]
 99%|████████████████████████████████████▌| 1.64G/1.65G [00:37<00:00, 79.1MB/s]
 99%|████████████████████████████████████▊| 1.64G/1.65G [00:38<00:00, 76.6MB/s]
100%|████████████████████████████████████▉| 1.65G/1.65G [00:38<00:00, 79.3MB/s]
  0%|                                              | 0.00/1.65G [00:00<?, ?B/s]
100%|█████████████████████████████████████| 1.65G/1.65G [00:00<00:00, 9.16TB/s]
Untarring contents of '/home/docs/mne_data/MNE-sample-data-processed.tar.gz' to '/home/docs/mne_data'
Attempting to create new mne-python configuration file:
/home/docs/.mne/mne-python.json
Could not read the /home/docs/.mne/mne-python.json json file during the writing. Assuming it is empty. Got: Expecting value: line 1 column 1 (char 0)
Download complete in 01m11s (1576.2 MB)
Reading forward solution from /home/docs/mne_data/MNE-sample-data/MEG/sample/sample_audvis-meg-eeg-oct-6-fwd.fif...
    Reading a source space...
    Computing patch statistics...
    Patch information added...
    Distance information added...
    [done]
    Reading a source space...
    Computing patch statistics...
    Patch information added...
    Distance information added...
    [done]
    2 source spaces read
    Desired named matrix (kind = 3523 (FIFF_MNE_FORWARD_SOLUTION_GRAD)) not available
    Read MEG forward solution (7498 sources, 306 channels, free orientations)
    Desired named matrix (kind = 3523 (FIFF_MNE_FORWARD_SOLUTION_GRAD)) not available
    Read EEG forward solution (7498 sources, 60 channels, free orientations)
    Forward solutions combined: MEG, EEG
    Source spaces transformed to the forward solution coordinate frame
    Average patch normals will be employed in the rotation to the local surface coordinates....
    Converting to surface-based source orientations...
    [done]
Opening raw data file /home/docs/mne_data/MNE-sample-data/MEG/sample/sample_audvis_raw.fif...
    Read a total of 3 projection items:
        PCA-v1 (1 x 102)  idle
        PCA-v2 (1 x 102)  idle
        PCA-v3 (1 x 102)  idle
    Range : 25800 ... 192599 =     42.956 ...   320.670 secs
Ready.

Below we define the parameters of the simulation and the simulation itself. In this case, we place 50 noise (1/f) sources randomly and add two phase-coupled sources of alpha (8-12 Hz) activity in arbitrary locations:

# Simulation parameters
sfreq = 100  # in Hz
duration = 60  # in seconds

# Initialize
sim = SourceSimulator(src)

# Add 50 noise sources with random locations
sim.add_noise_sources(location=select_random, location_params=dict(n=50))

# Add two point sources with fixed locations, vertex indices are chosen
# arbitrarily to have one source in each hemisphere
lh_vertno = src[0]["vertno"][0]
rh_vertno = src[1]["vertno"][0]
sim.add_point_sources(
    location=[(0, lh_vertno), (1, rh_vertno)],
    waveform=narrowband_oscillation,
    waveform_params=dict(fmin=8, fmax=12),
    names=["s1", "s2"],
)

# Set the coupling between point sources
sim.set_coupling(
    ("s1", "s2"), method=ppc_von_mises, kappa=1, phase_lag=np.pi / 2, fmin=8, fmax=12
)

Now let’s simulate the configuration with a desired level of global SNR:

sc = sim.simulate(
    sfreq,
    duration,
    fwd=fwd_eeg,
    snr_global=3,
    snr_params=dict(fmin=8, fmax=12),
    random_state=0,
)

We can double-check that the defined sources were successfully added (for noise sources, we only print the total count):

print(f"Point sources: {sc._sources}")
print(f"The number of noise sources: {len(sc._noise_sources)}")
Point sources: {'s1': <PointSource | s1 | lh | 841>, 's2': <PointSource | s2 | rh | 1492>}
The number of noise sources: 50

Finally, we can obtain the simulated source activity and/or project it to sensor space while adding 1% of sensor noise:

stc = sc.to_stc()
raw = sc.to_raw(fwd_eeg, info_eeg, sensor_noise_level=0.01)
Projecting source estimate to sensor space...
[done]
Creating RawArray with float64 data, n_channels=59, n_times=6000
    Range : 0 ... 5999 =      0.000 ...    59.990 secs
Ready.

We can now plot the power spectra of simulated sensor-space data to verify that it has a mixture of 1/f and alpha activity as defined in the simulation:

spec = raw.compute_psd(
    method="welch", n_fft=2 * sfreq, n_overlap=sfreq, n_per_seg=2 * sfreq
)
spec.plot()
EEG
Effective window size : 2.000 (s)
Plotting power spectral density (dB=True).

<MNELineFigure size 1000x350 with 2 Axes>

Total running time of the script: (1 minutes 13.237 seconds)

Gallery generated by Sphinx-Gallery