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%|                                     | 3.73M/1.65G [00:00<00:44, 37.3MB/s]
  1%|▏                                    | 9.85M/1.65G [00:00<00:32, 51.3MB/s]
  1%|▎                                    | 16.0M/1.65G [00:00<00:29, 56.2MB/s]
  1%|▍                                    | 22.1M/1.65G [00:00<00:28, 57.7MB/s]
  2%|▋                                    | 28.2M/1.65G [00:00<00:27, 59.0MB/s]
  2%|▊                                    | 34.4M/1.65G [00:00<00:26, 60.0MB/s]
  2%|▉                                    | 40.6M/1.65G [00:00<00:26, 60.7MB/s]
  3%|█                                    | 46.8M/1.65G [00:00<00:26, 61.3MB/s]
  3%|█▏                                   | 53.0M/1.65G [00:00<00:26, 61.2MB/s]
  4%|█▎                                   | 59.1M/1.65G [00:01<00:26, 60.8MB/s]
  4%|█▍                                   | 65.2M/1.65G [00:01<00:26, 60.7MB/s]
  4%|█▌                                   | 71.3M/1.65G [00:01<00:26, 60.3MB/s]
  5%|█▋                                   | 77.4M/1.65G [00:01<00:25, 60.6MB/s]
  5%|█▊                                   | 83.5M/1.65G [00:01<00:25, 60.7MB/s]
  5%|██                                   | 89.6M/1.65G [00:01<00:25, 60.9MB/s]
  6%|██▏                                  | 95.7M/1.65G [00:01<00:25, 60.7MB/s]
  6%|██▎                                   | 102M/1.65G [00:01<00:25, 60.7MB/s]
  7%|██▍                                   | 108M/1.65G [00:01<00:25, 60.7MB/s]
  7%|██▌                                   | 114M/1.65G [00:01<00:25, 60.8MB/s]
  7%|██▊                                   | 120M/1.65G [00:02<00:25, 60.7MB/s]
  8%|██▉                                   | 126M/1.65G [00:02<00:25, 60.9MB/s]
  8%|███                                   | 132M/1.65G [00:02<00:24, 60.9MB/s]
  8%|███▏                                  | 138M/1.65G [00:02<00:24, 61.0MB/s]
  9%|███▎                                  | 144M/1.65G [00:02<00:25, 60.3MB/s]
  9%|███▍                                  | 150M/1.65G [00:02<00:24, 60.1MB/s]
  9%|███▌                                  | 157M/1.65G [00:02<00:24, 60.3MB/s]
 10%|███▋                                  | 163M/1.65G [00:02<00:24, 60.4MB/s]
 10%|███▉                                  | 169M/1.65G [00:02<00:24, 60.7MB/s]
 11%|████                                  | 175M/1.65G [00:02<00:24, 60.2MB/s]
 11%|████▏                                 | 181M/1.65G [00:03<00:24, 59.9MB/s]
 11%|████▎                                 | 187M/1.65G [00:03<00:24, 60.1MB/s]
 12%|████▍                                 | 193M/1.65G [00:03<00:24, 60.2MB/s]
 12%|████▌                                 | 199M/1.65G [00:03<00:24, 60.3MB/s]
 12%|████▋                                 | 205M/1.65G [00:03<00:24, 60.2MB/s]
 13%|████▊                                 | 211M/1.65G [00:03<00:24, 59.6MB/s]
 13%|████▉                                 | 217M/1.65G [00:03<00:24, 59.6MB/s]
 13%|█████▏                                | 223M/1.65G [00:03<00:23, 59.8MB/s]
 14%|█████▎                                | 229M/1.65G [00:03<00:23, 60.0MB/s]
 14%|█████▍                                | 235M/1.65G [00:03<00:24, 58.6MB/s]
 15%|█████▌                                | 241M/1.65G [00:04<00:23, 59.0MB/s]
 15%|█████▋                                | 247M/1.65G [00:04<00:23, 59.4MB/s]
 15%|█████▊                                | 253M/1.65G [00:04<00:23, 59.7MB/s]
 16%|█████▉                                | 259M/1.65G [00:04<00:23, 59.8MB/s]
 16%|██████                                | 265M/1.65G [00:04<00:23, 59.5MB/s]
 16%|██████▏                               | 271M/1.65G [00:04<00:23, 59.6MB/s]
 17%|██████▎                               | 277M/1.65G [00:04<00:22, 59.9MB/s]
 17%|██████▌                               | 283M/1.65G [00:04<00:22, 60.2MB/s]
 18%|██████▋                               | 289M/1.65G [00:04<00:22, 60.5MB/s]
 18%|██████▊                               | 295M/1.65G [00:04<00:22, 60.0MB/s]
 18%|██████▉                               | 301M/1.65G [00:05<00:23, 57.7MB/s]
 19%|███████                               | 308M/1.65G [00:05<00:22, 58.6MB/s]
 19%|███████▏                              | 314M/1.65G [00:05<00:22, 59.3MB/s]
 19%|███████▎                              | 320M/1.65G [00:05<00:22, 59.8MB/s]
 20%|███████▍                              | 326M/1.65G [00:05<00:22, 60.2MB/s]
 20%|███████▋                              | 332M/1.65G [00:05<00:21, 60.4MB/s]
 20%|███████▊                              | 338M/1.65G [00:05<00:21, 60.2MB/s]
 21%|███████▉                              | 344M/1.65G [00:05<00:21, 60.3MB/s]
 21%|████████                              | 350M/1.65G [00:05<00:21, 60.4MB/s]
 22%|████████▏                             | 356M/1.65G [00:05<00:21, 60.6MB/s]
 22%|████████▎                             | 362M/1.65G [00:06<00:22, 58.0MB/s]
 22%|████████▍                             | 368M/1.65G [00:06<00:23, 55.1MB/s]
 23%|████████▌                             | 374M/1.65G [00:06<00:22, 55.8MB/s]
 23%|████████▋                             | 379M/1.65G [00:06<00:22, 55.8MB/s]
 23%|████████▊                             | 385M/1.65G [00:06<00:22, 56.1MB/s]
 24%|████████▉                             | 391M/1.65G [00:06<00:22, 56.5MB/s]
 24%|█████████                             | 397M/1.65G [00:06<00:22, 55.6MB/s]
 24%|█████████▏                            | 402M/1.65G [00:06<00:22, 55.2MB/s]
 25%|█████████▎                            | 408M/1.65G [00:06<00:22, 54.9MB/s]
 25%|█████████▍                            | 413M/1.65G [00:06<00:22, 55.0MB/s]
 25%|█████████▋                            | 419M/1.65G [00:07<00:22, 55.9MB/s]
 26%|█████████▊                            | 425M/1.65G [00:07<00:21, 56.8MB/s]
 26%|█████████▉                            | 431M/1.65G [00:07<00:21, 57.4MB/s]
 26%|██████████                            | 437M/1.65G [00:07<00:21, 57.7MB/s]
 27%|██████████▏                           | 443M/1.65G [00:07<00:20, 58.1MB/s]
 27%|██████████▎                           | 448M/1.65G [00:07<00:20, 58.5MB/s]
 27%|██████████▍                           | 454M/1.65G [00:07<00:35, 34.2MB/s]
 28%|██████████▌                           | 460M/1.65G [00:08<00:31, 38.4MB/s]
 28%|██████████▋                           | 466M/1.65G [00:08<00:27, 42.9MB/s]
 29%|██████████▊                           | 472M/1.65G [00:08<00:25, 46.7MB/s]
 29%|██████████▉                           | 477M/1.65G [00:08<00:23, 49.6MB/s]
 29%|███████████                           | 483M/1.65G [00:08<00:22, 51.6MB/s]
 30%|███████████▏                          | 489M/1.65G [00:08<00:21, 53.3MB/s]
 30%|███████████▎                          | 495M/1.65G [00:08<00:21, 54.8MB/s]
 30%|███████████▌                          | 501M/1.65G [00:08<00:20, 56.3MB/s]
 31%|███████████▋                          | 507M/1.65G [00:08<00:20, 57.0MB/s]
 31%|███████████▊                          | 512M/1.65G [00:08<00:20, 56.9MB/s]
 31%|███████████▉                          | 518M/1.65G [00:09<00:19, 57.5MB/s]
 32%|████████████                          | 524M/1.65G [00:09<00:19, 58.4MB/s]
 32%|████████████▏                         | 530M/1.65G [00:09<00:20, 55.4MB/s]
 32%|████████████▎                         | 536M/1.65G [00:09<00:20, 55.5MB/s]
 33%|████████████▍                         | 542M/1.65G [00:09<00:19, 56.5MB/s]
 33%|████████████▌                         | 548M/1.65G [00:09<00:19, 57.3MB/s]
 33%|████████████▋                         | 553M/1.65G [00:09<00:19, 57.4MB/s]
 34%|████████████▊                         | 560M/1.65G [00:09<00:28, 38.7MB/s]
 34%|████████████▉                         | 565M/1.65G [00:10<00:26, 41.3MB/s]
 35%|█████████████                         | 570M/1.65G [00:10<00:24, 44.7MB/s]
 35%|█████████████▏                        | 575M/1.65G [00:10<00:23, 46.2MB/s]
 35%|█████████████▎                        | 580M/1.65G [00:10<00:27, 38.3MB/s]
 35%|█████████████▍                        | 585M/1.65G [00:10<00:28, 37.0MB/s]
 36%|█████████████▌                        | 590M/1.65G [00:10<00:26, 39.9MB/s]
 36%|█████████████▋                        | 594M/1.65G [00:10<00:25, 42.0MB/s]
 36%|█████████████▊                        | 600M/1.65G [00:10<00:23, 45.5MB/s]
 37%|█████████████▉                        | 605M/1.65G [00:10<00:22, 45.7MB/s]
 37%|██████████████                        | 610M/1.65G [00:11<00:21, 47.8MB/s]
 37%|██████████████▏                       | 615M/1.65G [00:11<00:20, 49.5MB/s]
 38%|██████████████▎                       | 621M/1.65G [00:11<00:20, 51.1MB/s]
 38%|██████████████▍                       | 626M/1.65G [00:11<00:20, 50.1MB/s]
 38%|██████████████▌                       | 631M/1.65G [00:11<00:20, 50.4MB/s]
 38%|██████████████▋                       | 636M/1.65G [00:11<00:20, 50.2MB/s]
 39%|██████████████▋                       | 641M/1.65G [00:11<00:20, 49.7MB/s]
 39%|██████████████▊                       | 646M/1.65G [00:11<00:20, 49.4MB/s]
 39%|██████████████▉                       | 652M/1.65G [00:11<00:19, 51.3MB/s]
 40%|███████████████                       | 658M/1.65G [00:11<00:18, 52.9MB/s]
 40%|███████████████▎                      | 664M/1.65G [00:12<00:17, 55.0MB/s]
 41%|███████████████▍                      | 670M/1.65G [00:12<00:17, 57.1MB/s]
 41%|███████████████▌                      | 676M/1.65G [00:12<00:16, 58.3MB/s]
 41%|███████████████▋                      | 682M/1.65G [00:12<00:16, 58.3MB/s]
 42%|███████████████▊                      | 688M/1.65G [00:12<00:16, 57.8MB/s]
 42%|███████████████▉                      | 693M/1.65G [00:12<00:18, 52.0MB/s]
 42%|████████████████                      | 699M/1.65G [00:12<00:19, 48.5MB/s]
 43%|████████████████▏                     | 704M/1.65G [00:12<00:20, 45.9MB/s]
 43%|████████████████▎                     | 708M/1.65G [00:13<00:23, 39.5MB/s]
 43%|████████████████▍                     | 714M/1.65G [00:13<00:21, 42.8MB/s]
 44%|████████████████▌                     | 719M/1.65G [00:13<00:20, 45.9MB/s]
 44%|████████████████▋                     | 725M/1.65G [00:13<00:19, 48.4MB/s]
 44%|████████████████▊                     | 730M/1.65G [00:13<00:20, 44.3MB/s]
 44%|████████████████▉                     | 734M/1.65G [00:13<00:20, 44.3MB/s]
 45%|████████████████▉                     | 739M/1.65G [00:13<00:25, 36.0MB/s]
 45%|█████████████████                     | 743M/1.65G [00:13<00:23, 38.1MB/s]
 45%|█████████████████▏                    | 747M/1.65G [00:13<00:24, 36.3MB/s]
 46%|█████████████████▎                    | 752M/1.65G [00:14<00:22, 39.7MB/s]
 46%|█████████████████▍                    | 758M/1.65G [00:14<00:19, 45.1MB/s]
 46%|█████████████████▌                    | 764M/1.65G [00:14<00:17, 49.4MB/s]
 47%|█████████████████▋                    | 770M/1.65G [00:14<00:16, 52.6MB/s]
 47%|█████████████████▊                    | 776M/1.65G [00:14<00:15, 54.8MB/s]
 47%|█████████████████▉                    | 783M/1.65G [00:14<00:15, 56.9MB/s]
 48%|██████████████████▏                   | 788M/1.65G [00:14<00:18, 47.7MB/s]
 48%|██████████████████▎                   | 794M/1.65G [00:14<00:17, 49.6MB/s]
 48%|██████████████████▍                   | 800M/1.65G [00:14<00:16, 52.0MB/s]
 49%|██████████████████▌                   | 806M/1.65G [00:15<00:15, 54.5MB/s]
 49%|██████████████████▋                   | 812M/1.65G [00:15<00:15, 53.0MB/s]
 49%|██████████████████▊                   | 817M/1.65G [00:15<00:24, 34.4MB/s]
 50%|██████████████████▉                   | 822M/1.65G [00:15<00:22, 37.3MB/s]
 50%|███████████████████                   | 827M/1.65G [00:15<00:20, 40.4MB/s]
 50%|███████████████████▏                  | 832M/1.65G [00:15<00:19, 41.5MB/s]
 51%|███████████████████▏                  | 837M/1.65G [00:16<00:32, 25.0MB/s]
 51%|███████████████████▎                  | 840M/1.65G [00:16<00:30, 26.3MB/s]
 51%|███████████████████▍                  | 844M/1.65G [00:16<00:29, 27.2MB/s]
 51%|███████████████████▍                  | 847M/1.65G [00:16<00:28, 28.6MB/s]
 51%|███████████████████▌                  | 851M/1.65G [00:16<00:25, 31.0MB/s]
 52%|███████████████████▋                  | 855M/1.65G [00:16<00:23, 34.5MB/s]
 52%|███████████████████▊                  | 861M/1.65G [00:16<00:19, 39.8MB/s]
 52%|███████████████████▉                  | 866M/1.65G [00:16<00:18, 41.9MB/s]
 53%|████████████████████                  | 871M/1.65G [00:16<00:18, 42.5MB/s]
 53%|████████████████████▏                 | 876M/1.65G [00:17<00:17, 45.3MB/s]
 53%|████████████████████▏                 | 881M/1.65G [00:17<00:24, 31.8MB/s]
 54%|████████████████████▎                 | 884M/1.65G [00:17<00:28, 27.3MB/s]
 54%|████████████████████▍                 | 888M/1.65G [00:17<00:32, 23.5MB/s]
 54%|████████████████████▍                 | 891M/1.65G [00:17<00:32, 23.6MB/s]
 54%|████████████████████▌                 | 893M/1.65G [00:17<00:32, 23.2MB/s]
 54%|████████████████████▌                 | 896M/1.65G [00:18<00:31, 23.8MB/s]
 54%|████████████████████▋                 | 900M/1.65G [00:18<00:28, 26.7MB/s]
 55%|████████████████████▊                 | 904M/1.65G [00:18<00:23, 31.3MB/s]
 55%|████████████████████▊                 | 908M/1.65G [00:18<00:27, 27.5MB/s]
 55%|████████████████████▉                 | 913M/1.65G [00:18<00:22, 33.4MB/s]
 55%|█████████████████████                 | 917M/1.65G [00:18<00:21, 34.9MB/s]
 56%|█████████████████████▏                | 920M/1.65G [00:18<00:21, 33.7MB/s]
 56%|█████████████████████▎                | 926M/1.65G [00:18<00:19, 38.1MB/s]
 56%|█████████████████████▍                | 931M/1.65G [00:18<00:17, 41.8MB/s]
 57%|█████████████████████▌                | 937M/1.65G [00:19<00:15, 47.0MB/s]
 57%|█████████████████████▋                | 943M/1.65G [00:19<00:13, 51.1MB/s]
 57%|█████████████████████▊                | 949M/1.65G [00:19<00:12, 54.2MB/s]
 58%|█████████████████████▉                | 955M/1.65G [00:19<00:12, 56.4MB/s]
 58%|██████████████████████                | 961M/1.65G [00:19<00:11, 57.9MB/s]
 59%|██████████████████████▎               | 968M/1.65G [00:19<00:11, 60.0MB/s]
 59%|██████████████████████▍               | 974M/1.65G [00:19<00:17, 39.8MB/s]
 59%|██████████████████████▌               | 980M/1.65G [00:19<00:15, 43.9MB/s]
 60%|██████████████████████▋               | 986M/1.65G [00:20<00:14, 47.6MB/s]
 60%|██████████████████████▊               | 992M/1.65G [00:20<00:12, 51.2MB/s]
 60%|██████████████████████▉               | 998M/1.65G [00:20<00:12, 54.1MB/s]
 61%|██████████████████████▍              | 1.00G/1.65G [00:20<00:11, 56.1MB/s]
 61%|██████████████████████▌              | 1.01G/1.65G [00:20<00:11, 57.3MB/s]
 61%|██████████████████████▋              | 1.02G/1.65G [00:20<00:13, 46.3MB/s]
 62%|██████████████████████▊              | 1.02G/1.65G [00:20<00:13, 48.2MB/s]
 62%|███████████████████████              | 1.03G/1.65G [00:20<00:12, 51.5MB/s]
 63%|███████████████████████▏             | 1.03G/1.65G [00:20<00:12, 50.3MB/s]
 63%|███████████████████████▏             | 1.04G/1.65G [00:21<00:14, 43.3MB/s]
 63%|███████████████████████▎             | 1.04G/1.65G [00:21<00:13, 44.8MB/s]
 63%|███████████████████████▍             | 1.05G/1.65G [00:21<00:27, 22.1MB/s]
 64%|███████████████████████▌             | 1.05G/1.65G [00:21<00:23, 25.6MB/s]
 64%|███████████████████████▋             | 1.06G/1.65G [00:21<00:18, 31.4MB/s]
 64%|███████████████████████▊             | 1.07G/1.65G [00:22<00:15, 37.0MB/s]
 65%|███████████████████████▉             | 1.07G/1.65G [00:22<00:13, 42.2MB/s]
 65%|████████████████████████▏            | 1.08G/1.65G [00:22<00:12, 46.7MB/s]
 66%|████████████████████████▎            | 1.08G/1.65G [00:22<00:11, 50.0MB/s]
 66%|████████████████████████▍            | 1.09G/1.65G [00:22<00:10, 52.5MB/s]
 66%|████████████████████████▌            | 1.10G/1.65G [00:22<00:10, 54.7MB/s]
 67%|████████████████████████▋            | 1.10G/1.65G [00:23<00:21, 25.7MB/s]
 67%|████████████████████████▊            | 1.11G/1.65G [00:23<00:18, 30.3MB/s]
 67%|████████████████████████▉            | 1.11G/1.65G [00:23<00:15, 35.5MB/s]
 68%|█████████████████████████            | 1.12G/1.65G [00:23<00:13, 40.3MB/s]
 68%|█████████████████████████▏           | 1.12G/1.65G [00:23<00:12, 43.0MB/s]
 68%|█████████████████████████▎           | 1.13G/1.65G [00:23<00:11, 45.5MB/s]
 69%|█████████████████████████▍           | 1.13G/1.65G [00:23<00:12, 40.9MB/s]
 69%|█████████████████████████▌           | 1.14G/1.65G [00:24<00:16, 30.6MB/s]
 69%|█████████████████████████▌           | 1.14G/1.65G [00:24<00:16, 31.4MB/s]
 69%|█████████████████████████▋           | 1.15G/1.65G [00:24<00:14, 34.3MB/s]
 70%|█████████████████████████▊           | 1.15G/1.65G [00:24<00:14, 33.7MB/s]
 70%|█████████████████████████▉           | 1.16G/1.65G [00:24<00:12, 38.6MB/s]
 70%|██████████████████████████           | 1.16G/1.65G [00:24<00:11, 42.4MB/s]
 71%|██████████████████████████▏          | 1.17G/1.65G [00:24<00:10, 45.0MB/s]
 71%|██████████████████████████▎          | 1.17G/1.65G [00:24<00:10, 47.8MB/s]
 71%|██████████████████████████▍          | 1.18G/1.65G [00:24<00:09, 50.8MB/s]
 72%|██████████████████████████▌          | 1.18G/1.65G [00:24<00:09, 48.8MB/s]
 72%|██████████████████████████▋          | 1.19G/1.65G [00:25<00:09, 49.9MB/s]
 72%|██████████████████████████▊          | 1.20G/1.65G [00:25<00:08, 51.5MB/s]
 73%|██████████████████████████▊          | 1.20G/1.65G [00:25<00:11, 40.4MB/s]
 73%|██████████████████████████▉          | 1.20G/1.65G [00:25<00:12, 35.1MB/s]
 73%|███████████████████████████          | 1.21G/1.65G [00:25<00:11, 38.8MB/s]
 74%|███████████████████████████▏         | 1.22G/1.65G [00:25<00:10, 42.6MB/s]
 74%|███████████████████████████▎         | 1.22G/1.65G [00:25<00:09, 45.8MB/s]
 74%|███████████████████████████▍         | 1.23G/1.65G [00:25<00:08, 47.9MB/s]
 75%|███████████████████████████▌         | 1.23G/1.65G [00:26<00:08, 49.1MB/s]
 75%|███████████████████████████▋         | 1.24G/1.65G [00:26<00:08, 50.6MB/s]
 75%|███████████████████████████▊         | 1.24G/1.65G [00:26<00:08, 47.5MB/s]
 75%|███████████████████████████▉         | 1.25G/1.65G [00:26<00:10, 40.5MB/s]
 76%|████████████████████████████         | 1.25G/1.65G [00:26<00:09, 40.3MB/s]
 76%|████████████████████████████         | 1.26G/1.65G [00:26<00:14, 27.0MB/s]
 76%|████████████████████████████▏        | 1.26G/1.65G [00:26<00:12, 31.8MB/s]
 77%|████████████████████████████▎        | 1.27G/1.65G [00:27<00:10, 37.0MB/s]
 77%|████████████████████████████▍        | 1.27G/1.65G [00:27<00:09, 41.4MB/s]
 77%|████████████████████████████▌        | 1.28G/1.65G [00:27<00:08, 46.3MB/s]
 78%|████████████████████████████▋        | 1.28G/1.65G [00:27<00:07, 50.2MB/s]
 78%|████████████████████████████▉        | 1.29G/1.65G [00:27<00:06, 54.2MB/s]
 78%|█████████████████████████████        | 1.30G/1.65G [00:27<00:08, 43.2MB/s]
 79%|█████████████████████████████▏       | 1.30G/1.65G [00:27<00:07, 47.1MB/s]
 79%|█████████████████████████████▎       | 1.31G/1.65G [00:27<00:06, 50.7MB/s]
 80%|█████████████████████████████▍       | 1.31G/1.65G [00:27<00:06, 53.4MB/s]
 80%|█████████████████████████████▌       | 1.32G/1.65G [00:28<00:05, 55.6MB/s]
 80%|█████████████████████████████▋       | 1.33G/1.65G [00:28<00:05, 57.3MB/s]
 81%|█████████████████████████████▊       | 1.33G/1.65G [00:28<00:05, 53.4MB/s]
 81%|█████████████████████████████▉       | 1.34G/1.65G [00:28<00:07, 39.9MB/s]
 81%|██████████████████████████████       | 1.34G/1.65G [00:28<00:07, 43.1MB/s]
 82%|██████████████████████████████▏      | 1.35G/1.65G [00:28<00:06, 46.8MB/s]
 82%|██████████████████████████████▎      | 1.35G/1.65G [00:29<00:21, 14.1MB/s]
 82%|██████████████████████████████▍      | 1.36G/1.65G [00:29<00:16, 18.0MB/s]
 83%|██████████████████████████████▌      | 1.37G/1.65G [00:29<00:12, 23.0MB/s]
 83%|██████████████████████████████▋      | 1.37G/1.65G [00:30<00:11, 24.4MB/s]
 83%|██████████████████████████████▊      | 1.37G/1.65G [00:30<00:10, 26.8MB/s]
 83%|██████████████████████████████▊      | 1.38G/1.65G [00:30<00:11, 24.3MB/s]
 84%|██████████████████████████████▉      | 1.38G/1.65G [00:30<00:13, 20.3MB/s]
 84%|███████████████████████████████      | 1.39G/1.65G [00:30<00:10, 25.1MB/s]
 84%|███████████████████████████████      | 1.39G/1.65G [00:30<00:09, 26.9MB/s]
 84%|███████████████████████████████▏     | 1.39G/1.65G [00:30<00:09, 27.3MB/s]
 85%|███████████████████████████████▎     | 1.40G/1.65G [00:31<00:08, 31.4MB/s]
 85%|███████████████████████████████▍     | 1.40G/1.65G [00:31<00:09, 26.6MB/s]
 85%|███████████████████████████████▌     | 1.41G/1.65G [00:31<00:07, 33.8MB/s]
 85%|███████████████████████████████▌     | 1.41G/1.65G [00:31<00:08, 28.3MB/s]
 86%|███████████████████████████████▋     | 1.42G/1.65G [00:31<00:07, 32.8MB/s]
 86%|███████████████████████████████▊     | 1.42G/1.65G [00:31<00:08, 26.1MB/s]
 86%|███████████████████████████████▊     | 1.42G/1.65G [00:32<00:09, 25.1MB/s]
 86%|███████████████████████████████▉     | 1.43G/1.65G [00:32<00:08, 27.2MB/s]
 87%|████████████████████████████████     | 1.43G/1.65G [00:32<00:07, 30.4MB/s]
 87%|████████████████████████████████     | 1.43G/1.65G [00:32<00:07, 28.2MB/s]
 87%|████████████████████████████████▏    | 1.44G/1.65G [00:32<00:06, 32.1MB/s]
 87%|████████████████████████████████▎    | 1.44G/1.65G [00:32<00:05, 35.3MB/s]
 88%|████████████████████████████████▍    | 1.45G/1.65G [00:32<00:05, 36.9MB/s]
 88%|████████████████████████████████▌    | 1.45G/1.65G [00:32<00:04, 42.4MB/s]
 88%|████████████████████████████████▋    | 1.46G/1.65G [00:32<00:04, 47.1MB/s]
 89%|████████████████████████████████▊    | 1.47G/1.65G [00:33<00:03, 50.7MB/s]
 89%|████████████████████████████████▉    | 1.47G/1.65G [00:33<00:03, 54.3MB/s]
 89%|█████████████████████████████████    | 1.48G/1.65G [00:33<00:05, 30.1MB/s]
 90%|█████████████████████████████████▏   | 1.48G/1.65G [00:33<00:04, 34.8MB/s]
 90%|█████████████████████████████████▎   | 1.49G/1.65G [00:34<00:09, 18.2MB/s]
 90%|█████████████████████████████████▍   | 1.49G/1.65G [00:34<00:12, 12.4MB/s]
 90%|█████████████████████████████████▍   | 1.50G/1.65G [00:35<00:19, 8.15MB/s]
 91%|█████████████████████████████████▌   | 1.50G/1.65G [00:35<00:19, 7.86MB/s]
 91%|█████████████████████████████████▌   | 1.50G/1.65G [00:36<00:20, 7.52MB/s]
 91%|█████████████████████████████████▌   | 1.50G/1.65G [00:36<00:22, 6.87MB/s]
 91%|█████████████████████████████████▌   | 1.50G/1.65G [00:36<00:21, 6.93MB/s]
 91%|█████████████████████████████████▌   | 1.50G/1.65G [00:36<00:24, 6.21MB/s]
 91%|█████████████████████████████████▋   | 1.50G/1.65G [00:37<00:29, 5.01MB/s]
 91%|█████████████████████████████████▋   | 1.50G/1.65G [00:37<00:28, 5.25MB/s]
 91%|█████████████████████████████████▋   | 1.50G/1.65G [00:37<00:27, 5.45MB/s]
 91%|█████████████████████████████████▋   | 1.50G/1.65G [00:37<00:27, 5.46MB/s]
 91%|█████████████████████████████████▋   | 1.51G/1.65G [00:37<00:28, 5.16MB/s]
 91%|█████████████████████████████████▋   | 1.51G/1.65G [00:37<00:30, 4.84MB/s]
 91%|█████████████████████████████████▋   | 1.51G/1.65G [00:37<00:32, 4.54MB/s]
 91%|█████████████████████████████████▋   | 1.51G/1.65G [00:38<00:41, 3.52MB/s]
 91%|█████████████████████████████████▋   | 1.51G/1.65G [00:38<00:41, 3.54MB/s]
 91%|█████████████████████████████████▊   | 1.51G/1.65G [00:38<00:33, 4.28MB/s]
 91%|█████████████████████████████████▊   | 1.51G/1.65G [00:38<00:22, 6.32MB/s]
 92%|█████████████████████████████████▉   | 1.51G/1.65G [00:38<00:08, 16.0MB/s]
 92%|█████████████████████████████████▉   | 1.52G/1.65G [00:38<00:13, 9.95MB/s]
 92%|██████████████████████████████████   | 1.52G/1.65G [00:39<00:08, 16.5MB/s]
 92%|██████████████████████████████████▏  | 1.53G/1.65G [00:39<00:04, 25.5MB/s]
 93%|██████████████████████████████████▎  | 1.53G/1.65G [00:39<00:05, 24.4MB/s]
 93%|██████████████████████████████████▎  | 1.53G/1.65G [00:39<00:06, 17.1MB/s]
 93%|██████████████████████████████████▍  | 1.54G/1.65G [00:39<00:06, 18.9MB/s]
 93%|██████████████████████████████████▍  | 1.54G/1.65G [00:39<00:05, 21.3MB/s]
 93%|██████████████████████████████████▌  | 1.54G/1.65G [00:39<00:04, 24.7MB/s]
 94%|██████████████████████████████████▋  | 1.55G/1.65G [00:40<00:05, 19.8MB/s]
 94%|██████████████████████████████████▋  | 1.55G/1.65G [00:40<00:04, 23.2MB/s]
 94%|██████████████████████████████████▊  | 1.55G/1.65G [00:40<00:03, 27.9MB/s]
 94%|██████████████████████████████████▉  | 1.56G/1.65G [00:40<00:03, 30.8MB/s]
 95%|███████████████████████████████████  | 1.56G/1.65G [00:40<00:02, 36.2MB/s]
 95%|███████████████████████████████████▏ | 1.57G/1.65G [00:40<00:02, 40.6MB/s]
 95%|███████████████████████████████████▏ | 1.57G/1.65G [00:41<00:03, 21.9MB/s]
 96%|███████████████████████████████████▎ | 1.58G/1.65G [00:41<00:02, 28.0MB/s]
 96%|███████████████████████████████████▍ | 1.59G/1.65G [00:41<00:01, 34.3MB/s]
 96%|███████████████████████████████████▋ | 1.59G/1.65G [00:41<00:01, 40.3MB/s]
 97%|███████████████████████████████████▊ | 1.60G/1.65G [00:41<00:01, 45.3MB/s]
 97%|███████████████████████████████████▉ | 1.60G/1.65G [00:41<00:01, 46.8MB/s]
 97%|████████████████████████████████████ | 1.61G/1.65G [00:41<00:01, 39.9MB/s]
 98%|████████████████████████████████████ | 1.61G/1.65G [00:42<00:01, 31.5MB/s]
 98%|████████████████████████████████████▏| 1.62G/1.65G [00:42<00:01, 32.4MB/s]
 98%|████████████████████████████████████▎| 1.62G/1.65G [00:42<00:00, 35.2MB/s]
 98%|████████████████████████████████████▍| 1.63G/1.65G [00:42<00:00, 32.2MB/s]
 99%|████████████████████████████████████▍| 1.63G/1.65G [00:42<00:00, 37.3MB/s]
 99%|████████████████████████████████████▋| 1.64G/1.65G [00:42<00:00, 42.6MB/s]
 99%|████████████████████████████████████▊| 1.64G/1.65G [00:42<00:00, 47.5MB/s]
100%|████████████████████████████████████▉| 1.65G/1.65G [00:42<00:00, 51.4MB/s]
  0%|                                              | 0.00/1.65G [00:00<?, ?B/s]
100%|█████████████████████████████████████| 1.65G/1.65G [00:00<00:00, 5.83TB/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 01m16s (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 18.738 seconds)

Gallery generated by Sphinx-Gallery