# non linear running time when simulating raw signal

**URL:** <https://mne.discourse.group/t/non-linear-running-time-when-simulating-raw-signal/11033>\
**Category:** Support & Discussions\
**Created:** [March 27, 2025, 9:43pm UTC](https://mne.discourse.group/t/non-linear-running-time-when-simulating-raw-signal/11033 "2025-03-27T21:43:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bburle](https://avatars.discourse-cdn.com/v4/letter/b/bbce88/32.png) [@bburle](https://mne.discourse.group/u/bburle)\
**Post date:** [March 27, 2025, 9:43pm UTC](https://mne.discourse.group/t/non-linear-running-time-when-simulating-raw-signal/11033/1 "2025-03-27T21:43:02Z")

</div>

Dear mne community,

- MNE version: e.g. 1.8.0
- operating system: Kubuntu 24.04

Context:  
I’m running simulations of raw signal. After loading the head model, determining the source (that is the cortex patch that will be used as the source), and defining the souce time course, I add the source time course to the source SourceSimulator:

```python

source_simulator = mne.simulation.SourceSimulator(src, tstep=tstep)
for t in np.arange(n_trials):
    source_simulator.add_data(vis_r, act_1[:,t], events)

```

Everything works fine!  
I then use the SourceSimulator to simulate the raw signal

```python

raw_vis = mne.simulation.simulate_raw(raw.info, source_simulator, forward=fwd)

```

No problem here neither.

The issue I have concerns the running time of this last line (simulate\_raw) as a function of the number of trials I want to simulate (i.e. the number of event in events, which corresponds to the number of trials n\_trials). I was expecting this running time to be linear, but it is far from being linear!

Here are some running time as a function of the number of events:

n\_trials running time (s)  
10 0.5  
20 2.8  
30 9  
40 21  
50 42

With such an exponential increase, it is unrealistic to run a “large” number of trials (I was targeting “only” 300…).

I quickly inspected the code of simulate\_raw, but I could not identify what could cause the exponential increase.

Not sure this is relevant, but I’m running the code in a jupyter notebook…

Any idea of what could be the cause of the phenomenon ?

Thanks very much in advance,  
B.

---

<div class="post-metadata">

**Author:** ![bburle](https://avatars.discourse-cdn.com/v4/letter/b/bbce88/32.png) [@bburle](https://mne.discourse.group/u/bburle)\
**Post date:** [April 2, 2025, 6:56am UTC](https://mne.discourse.group/t/non-linear-running-time-when-simulating-raw-signal/11033/2 "2025-04-02T06:56:24Z")

</div>

Dear community,

I guess I found out where the problem stems from…  
It is related to misunderstanding of the SourceSimulator object, and its add\_data method.  
To add ‘n\_time \* n\_trials’ data, one should **not** do

```python
for t in np.arange(n_trial):
   source_simulator.add_data(vis_r, act_1[:,t], events)

```

since, as far as I understand, we have ‘n\_trials’ events in ‘events’, the operation will be repeated ‘n\_trials \* n\_trials’ time; hence the exponential time.

One should instead simply run:

```python
source_simulator.add_data(vis_r, act_1, events)

```

which will directly import the n\_trials into source\_simulator, which saves a huge amount of time.

Funny enough, the error of importing in a loop was still producing the correct number of trials in the raw object though…

Hope this might be helpfull for other…  
Best
