# Error with restore() and run()

**URL:** <https://brian.discourse.group/t/error-with-restore-and-run/475>\
**Category:** Support\
**Created:** [13 September 2021 13:26 UTC](https://brian.discourse.group/t/error-with-restore-and-run/475 "2021-09-13T13:26:32Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![SIbanez](https://avatars.discourse-cdn.com/v4/letter/s/50afbb/32.png) [@SIbanez](https://brian.discourse.group/u/SIbanez)\
**Post date:** [13 September 2021 13:26 UTC](https://brian.discourse.group/t/error-with-restore-and-run/475/1 "2021-09-13T13:26:32Z")

</div>

# Description of problem

Hi, I want to use store() / restore() in brian2 to save the connectivity and reloaded in a different simulation, but I get an error when I try to run simulations with the restored connectivity.  
If I don’t run a simulation and just store the connectivity and restore it after, then the restore function works well. But when I try to restore the connectivity while I also use run() to run a simulation I get the error below. I hope someone can help me with this. I can copy part of the code too if that is helpful to understand the problem.

I have been having problems with Cython that seemed to be solved now, I am not sure up to what point this error could also be related with Cython.

Error:

File “/Users/saraibanez/opt/anaconda3/envs/brian2\_good/lib/python3.9/site-packages/brian2/codegen/runtime/cython\_rt/cython\_rt.py”, line 160, in run\_block  
return compiled\_code.main(self.namespace)

File “\_cython\_magic\_ccb3d62751e600698487f83f290437ab.pyx”, line 57, in \_cython\_magic\_ccb3d62751e600698487f83f290437ab.main

File “/Users/saraibanez/opt/anaconda3/envs/brian2\_good/lib/python3.9/site-packages/brian2/synapses/synapses.py”, line 347, in initialise\_queue  
raise TypeError(("Synapses object ‘%s’ does not do anything, since "

TypeError: Synapses object ‘C1’ does not do anything, since it has not created synapses with ‘connect’. Set its active attribute to False if you intend to do only do this for a subsequent run.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):

File “/Users/saraibanez/opt/anaconda3/envs/brian2\_good/lib/python3.9/site-packages/brian2/core/network.py”, line 901, in before\_run  
obj.before\_run(run\_namespace)

File “/Users/saraibanez/opt/anaconda3/envs/brian2\_good/lib/python3.9/site-packages/brian2/core/base.py”, line 279, in device\_override\_decorated\_function  
return func(\*args, \*\*kwds)

File “/Users/saraibanez/opt/anaconda3/envs/brian2\_good/lib/python3.9/site-packages/brian2/synapses/synapses.py”, line 319, in before\_run  
super(SynapticPathway, self).before\_run(run\_namespace)

File “/Users/saraibanez/opt/anaconda3/envs/brian2\_good/lib/python3.9/site-packages/brian2/groups/group.py”, line 1143, in before\_run  
super(CodeRunner, self).before\_run(run\_namespace)

File “/Users/saraibanez/opt/anaconda3/envs/brian2\_good/lib/python3.9/site-packages/brian2/core/base.py”, line 169, in before\_run  
codeobj.before\_run()

File “/Users/saraibanez/opt/anaconda3/envs/brian2\_good/lib/python3.9/site-packages/brian2/codegen/codeobject.py”, line 123, in before\_run  
return self.run\_block(‘before\_run’)

File “/Users/saraibanez/opt/anaconda3/envs/brian2\_good/lib/python3.9/site-packages/brian2/codegen/runtime/cython\_rt/cython\_rt.py”, line 164, in run\_block  
raise BrianObjectException(message, self.owner) from exc

BrianObjectException: Error encountered with object named “C1\_pre”.  
Object was created here (most recent call only, full details in debug log):  
File “/Users/saraibanez/Desktop/Brian\_Hansel&Mato/Brian2/balancedRing\_b2\_new\_copia\_copia.py”, line 201, in run\_sims  
C1 = Synapses(networkE, networkE,

An exception occured during the execution of the before\_run block of code object C1\_pre\_push\_spikes.  
(See above for original error message and traceback.)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):

File “/Users/saraibanez/Desktop/Brian\_Hansel&Mato/Brian2/balancedRing\_b2\_new\_copia\_copia.py”, line 339, in   
run\_sims()

File “/Users/saraibanez/Desktop/Brian\_Hansel&Mato/Brian2/balancedRing\_b2\_new\_copia\_copia.py”, line 296, in run\_sims  
run(stim\_on,report=‘text’)

File “/Users/saraibanez/opt/anaconda3/envs/brian2\_good/lib/python3.9/site-packages/brian2/units/fundamentalunits.py”, line 2430, in new\_f  
result = f(\*args, \*\*kwds)

File “/Users/saraibanez/opt/anaconda3/envs/brian2\_good/lib/python3.9/site-packages/brian2/core/magic.py”, line 374, in run  
return magic\_network.run(duration, report=report, report\_period=report\_period,

File “/Users/saraibanez/opt/anaconda3/envs/brian2\_good/lib/python3.9/site-packages/brian2/core/magic.py”, line 232, in run  
Network.run(self, duration, report=report, report\_period=report\_period,

File “/Users/saraibanez/opt/anaconda3/envs/brian2\_good/lib/python3.9/site-packages/brian2/core/base.py”, line 279, in device\_override\_decorated\_function  
return func(\*args, \*\*kwds)

File “/Users/saraibanez/opt/anaconda3/envs/brian2\_good/lib/python3.9/site-packages/brian2/units/fundamentalunits.py”, line 2430, in new\_f  
result = f(\*args, \*\*kwds)

File “/Users/saraibanez/opt/anaconda3/envs/brian2\_good/lib/python3.9/site-packages/brian2/core/network.py”, line 1012, in run  
self.before\_run(namespace)

File “/Users/saraibanez/opt/anaconda3/envs/brian2\_good/lib/python3.9/site-packages/brian2/core/base.py”, line 279, in device\_override\_decorated\_function  
return func(\*args, \*\*kwds)

File “/Users/saraibanez/opt/anaconda3/envs/brian2\_good/lib/python3.9/site-packages/brian2/core/network.py”, line 903, in before\_run  
raise BrianObjectException(“An error occurred when preparing an object.”, obj) from ex

BrianObjectException: Error encountered with object named “C1\_pre”.  
Object was created here (most recent call only, full details in debug log):  
File “/Users/saraibanez/Desktop/Brian\_Hansel&Mato/Brian2/balancedRing\_b2\_new\_copia\_copia.py”, line 201, in run\_sims  
C1 = Synapses(networkE, networkE,

An error occurred when preparing an object. (See above for original error message and traceback.)

---

<div class="post-metadata">

**Author:** ![adam](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/adam/32/197_2.png) [@adam](https://brian.discourse.group/u/adam)\
**Post date:** [13 September 2021 14:24 UTC](https://brian.discourse.group/t/error-with-restore-and-run/475/2 "2021-09-13T14:24:25Z")

</div>

Does the code run successfully without `store()` and `restore()`?  
I’d try that first with only one parameter setting (meaning comment out the loop you’ve set up to sweep across multiple parameters, and try running the network once)

And yeah the part of the code at least between line 201:  
`C1 = Synapses( ...`  
and the `run()` command might be helpful to see here

---

<div class="post-metadata">

**Author:** ![SIbanez](https://avatars.discourse-cdn.com/v4/letter/s/50afbb/32.png) [@SIbanez](https://brian.discourse.group/u/SIbanez)\
**Post date:** [13 September 2021 14:48 UTC](https://brian.discourse.group/t/error-with-restore-and-run/475/3 "2021-09-13T14:48:42Z")

</div>

Hi Adam,  
see below a short example. set make\_network = True to store. then, you should get the error when setting it to False.

without store / restore it works fine.

####### CODE:

from brian2 import \*  
start\_time = time.time()  
make\_network = True

NE = 500  
fE = 1  
sigma = 36.0 # sigma in [deg]  
sigma = sigma / 360.0 #\* NE # convert sigma

G = NeuronGroup(NE, ‘v:1’, threshold=‘v\>1’, name=‘G’)  
S = Synapses(G, G, on\_pre=‘v += 1’, name=‘S’)

if make\_network:  
S.connect(p = 'fE \* exp(-(i/NE-j/NE)**2 / (2\*sigma**2)) \* (int( (i/NE-j/NE)_sign(i/NE-j/NE) \<= 0.5)) + fE \* exp(-(abs(i/NE-j/NE)-1)\*\*2 / (2_sigma\*\*2)) \* (int( (i/NE-j/NE)\*sign(i/NE-j/NE) \> 0.5)) ')  
store(filename = ‘my\_network’)

else:  
restore(filename = ‘my\_network’,restore\_random\_state=False)

###### # visualize resulting connectivity matrix

C=zeros((NE,NE))  
C[S.i,S.j] = 1  
imshow(C)

run(100\*ms,report=‘text’)

print(‘sim finished’)  
print(time.time() - start\_time)

---

<div class="post-metadata">

**Author:** ![adam](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/adam/32/197_2.png) [@adam](https://brian.discourse.group/u/adam)\
**Post date:** [13 September 2021 18:12 UTC](https://brian.discourse.group/t/error-with-restore-and-run/475/4 "2021-09-13T18:12:13Z")

</div>

> [@SIbanez](#):
>
> ```auto
> from brian2 import *
> start_time = time.time()
> make_network = True
> 
> NE = 500
> fE = 1
> sigma = 36.0 # sigma in [deg]
> sigma = sigma / 360.0 #* NE # convert sigma
> 
> G = NeuronGroup(NE, ‘v:1’, threshold=‘v>1’, name=‘G’)
> S = Synapses(G, G, on_pre=‘v += 1’, name=‘S’)
> 
> if make_network:
> S.connect(p = 'fE * exp(-(i/NE-j/NE) **2 / (2*sigma** 2)) * (int( (i/NE-j/NE) *sign(i/NE-j/NE) <= 0.5)) + fE * exp(-(abs(i/NE-j/NE)-1)**2 / (2* sigma**2)) * (int( (i/NE-j/NE)*sign(i/NE-j/NE) > 0.5)) ')
> store(filename = ‘my_network’)
> else:
> restore(filename = ‘my_network’,restore_random_state=False)
> 
> ###### # visualize resulting connectivity matrix
> 
> C=zeros((NE,NE))
> C[S.i,S.j] = 1
> imshow(C)
> 
> run(100*ms,report=‘text’)
> 
> print(‘sim finished’)
> print(time.time() - start_time)
> 
> ```

so this runs for me if I set `make_network = True`

 ![Screen Shot 2021-09-13 at 2.11.02 PM](https://global.discourse-cdn.com/free1/uploads/brian/original/1X/099340ab1a37ece236a00b5e5598b8c59e8e9704.jpeg)

debugging what happens with `make_network=False` now …

---

<div class="post-metadata">

**Author:** ![adam](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/adam/32/197_2.png) [@adam](https://brian.discourse.group/u/adam)\
**Post date:** [13 September 2021 18:17 UTC](https://brian.discourse.group/t/error-with-restore-and-run/475/5 "2021-09-13T18:17:51Z")

</div>

Okay. I think the issue is you need to unconditionally connect the synapses.

> Note that the [`store()`](https://brian2.readthedocs.io/en/stable/reference/brian2.core.magic.store.html#brian2.core.magic.store)/[`restore()`](https://brian2.readthedocs.io/en/stable/reference/brian2.core.magic.restore.html#brian2.core.magic.restore) mechanism does not re-create the network as such, you still need to construct all the [`NeuronGroup`](https://brian2.readthedocs.io/en/stable/reference/brian2.groups.neurongroup.NeuronGroup.html#brian2.groups.neurongroup.NeuronGroup), [`Synapses`](https://brian2.readthedocs.io/en/stable/reference/brian2.synapses.synapses.Synapses.html#brian2.synapses.synapses.Synapses), [`StateMonitor`](https://brian2.readthedocs.io/en/stable/reference/brian2.monitors.statemonitor.StateMonitor.html#brian2.monitors.statemonitor.StateMonitor), … objects, restoring will only restore all the state variable values (membrane potential, conductances, synaptic connections/weights/delays, …).

from [Running a simulation](https://brian2.readthedocs.io/en/stable/user/running.html)

so try this code:

```auto
from brian2 import *
start_time = time.time()
make_network = False

NE = 500
fE = 1
sigma = 36.0 # sigma in [deg]
sigma = sigma / 360.0 #* NE # convert sigma

G = NeuronGroup(NE, 'v:1', threshold='v>1', name='G')
S = Synapses(G, G, on_pre='v += 1', name='S')
S.connect(p = 'fE * exp(-(i/NE-j/NE) **2 / (2*sigma** 2)) * (int( (i/NE-j/NE) *sign(i/NE-j/NE) <= 0.5)) + fE * exp(-(abs(i/NE-j/NE)-1)**2 / (2* sigma**2)) * (int( (i/NE-j/NE)*sign(i/NE-j/NE) > 0.5)) ')

if make_network:
    store(filename = 'my_network')
else:
    restore(filename = 'my_network',restore_random_state=False)

###### # visualize resulting connectivity matrix

C=zeros((NE,NE))
C[S.i,S.j] = 1
imshow(C)

run(100*ms,report='text')

print('sim finished')
print(time.time() - start_time)

```

---

<div class="post-metadata">

**Author:** ![SIbanez](https://avatars.discourse-cdn.com/v4/letter/s/50afbb/32.png) [@SIbanez](https://brian.discourse.group/u/SIbanez)\
**Post date:** [14 September 2021 10:25 UTC](https://brian.discourse.group/t/error-with-restore-and-run/475/6 "2021-09-14T10:25:11Z")

</div>

Hi Adam,

this works fine, thanks!

---

<div class="post-metadata">

**Author:** ![mstimberg](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/mstimberg/32/11_2.png) [@mstimberg](https://brian.discourse.group/u/mstimberg)\
**Post date:** [15 September 2021 11:51 UTC](https://brian.discourse.group/t/error-with-restore-and-run/475/7 "2021-09-15T11:51:03Z")

</div>

Hi @SIbanez , hi @adam . Actually, it turns out that you found a bug in Brian!  
Your error message states that `Synapses object ‘C1’ does not do anything, since it has not created synapses with ‘connect’.` This is a check that Brian does to give a useful error message in case a user created a `Synapses` object, but forgot to call `Synapses.connect` (which means that the `Synapses` object does not do anything). Brian thinks that this is the case in your original example, since for `make_network=False` you never call `Synapses.connect`. However, your synapses _do_ get restored correctly, Brian just fails to realize that the synapses have been created this way instead of via a `Synapses.connect` call. As a workaround, you can add a dummy `Synapses.connect(False)` after your `restore` call, which will add 0 synapses but will make Brian think that all is good since you called `Synapses.connect`.

Regarding the code proposed by @adam in the last comment: this works, but note that it will regenerate the synapses independently in each simulation. If I understood correctly, you want to have the exact same connection pattern in both simulations, this is not the case here.

Final comment: if you only want to store the synaptic connections, you do not necessarily have to use `store`/`restore` which also store everything else (all the state variables, parameters, etc.). Another option would be to use the standard numpy methods to save/load data, e.g.:

```auto
if make_network:
    S.connect(p = '...')
    np.savez('my_network.npz', i=S.i[:], j=S.j[:])
else:
    connections = np.load('my_network.npz')
    S.connect(i=connections['i'], j=connections['j'])

```

Best,  
Marcel

---

<div class="post-metadata">

**Author:** ![SIbanez](https://avatars.discourse-cdn.com/v4/letter/s/50afbb/32.png) [@SIbanez](https://brian.discourse.group/u/SIbanez)\
**Post date:** [15 September 2021 14:28 UTC](https://brian.discourse.group/t/error-with-restore-and-run/475/8 "2021-09-15T14:28:27Z")

</div>

Hi Marcel,

interesting that we found this!

I need to use store/restore because I will later need to store other variables like delays, etc.

Regarding Adam’s solution, I think you are right that it is not what I need, so thanks for point it out. however it is not clear to me how restore would be working here. I would be regenerating new connectivities (this is clear), and then, does ‘‘restore’’ restore anything?

Thanks for your solution as well.

---

<div class="post-metadata">

**Author:** ![mstimberg](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/mstimberg/32/11_2.png) [@mstimberg](https://brian.discourse.group/u/mstimberg)\
**Post date:** [15 September 2021 14:47 UTC](https://brian.discourse.group/t/error-with-restore-and-run/475/9 "2021-09-15T14:47:42Z")

</div>

> [@SIbanez](#):
>
> Regarding Adam’s solution, I think you are right that it is not what I need, so thanks for point it out. however it is not clear to me how restore would be working here. I would be regenerating new connectivities (this is clear), and then, does ‘‘restore’’ restore anything?

Um, I think I have to apologize, this actually might do what you wanted, only in a slightly inefficient way. In @adam’s solution, the `Synapses.connect(...)` will generate two different connection patterns in the two calls, but with the `restore` call, this connection pattern will be overwritten with the original one, so you should actually end up with everything working as expected. The only downside is that for `make_network = True`, it will unnecessarily generate connections that will not be used, and generating connections can take quite a while for big networks.

---

<div class="post-metadata">

**Author:** ![mstimberg](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/mstimberg/32/11_2.png) [@mstimberg](https://brian.discourse.group/u/mstimberg)\
**Post date:** [15 September 2021 14:54 UTC](https://brian.discourse.group/t/error-with-restore-and-run/475/10 "2021-09-15T14:54:24Z")

</div>

Just to confirm what I said in my previous post, here’s a simplified example:

```auto
from brian2 import *
make_network = True

G = NeuronGroup(5, '')
S = Synapses(G, G)
S.connect(p=0.5)
print('Connections after S.connect: ')
print(list(zip(S.i, S.j)))

if make_network:
    store(filename='my_network')
else:
    restore(filename='my_network')

print('Connections after store/restore:')
print(list(zip(S.i, S.j)))

```

After the first run with `make_network = True` I get (of course this will be different for you):

```nohighlight
Connections after S.connect
[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (1, 3), (2, 0), (2, 1), (3, 0), (3, 2), (3, 3), (3, 4), (4, 0), (4, 4)]
Connections after store/restore:
[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (1, 3), (2, 0), (2, 1), (3, 0), (3, 2), (3, 3), (3, 4), (4, 0), (4, 4)]

```

After running it again with `make_network = False`:

```nohighlight
Connections after S.connect: 
[(0, 0), (0, 2), (0, 4), (1, 0), (1, 1), (1, 2), (1, 3), (3, 0), (4, 3), (4, 4)]
Connections after store/restore:
[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (1, 3), (2, 0), (2, 1), (3, 0), (3, 2), (3, 3), (3, 4), (4, 0), (4, 4)]

```

As you can see, the second run first generates a different connection pattern but then restores the original one.

---

<div class="post-metadata">

**Author:** ![SIbanez](https://avatars.discourse-cdn.com/v4/letter/s/50afbb/32.png) [@SIbanez](https://brian.discourse.group/u/SIbanez)\
**Post date:** [15 September 2021 15:04 UTC](https://brian.discourse.group/t/error-with-restore-and-run/475/11 "2021-09-15T15:04:55Z")

</div>

Thank you Marcel.  
I will stick then to using " Synapses.connect(False)".

---

<div class="post-metadata">

**Author:** ![mstimberg](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/mstimberg/32/11_2.png) [@mstimberg](https://brian.discourse.group/u/mstimberg)\
**Post date:** [8 October 2021 15:15 UTC](https://brian.discourse.group/t/error-with-restore-and-run/475/12 "2021-10-08T15:15:33Z")

</div>

This has now been fixed on github via [Do not raise an error if synapses use restore instead of Synapses.connect by mstimberg · Pull Request #1359 · brian-team/brian2 · GitHub](https://github.com/brian-team/brian2/pull/1359)
