About Network class

Hello! I am trying to use the Network class to perform multiple simulations. I created a function Model() in which i defined NeuronGroups and Synapses. Model() return Network(NeuronGroups, Synapses). Reading the Network source code I found that if I want to access to an object inside the Network class i need to call (if net = Model()) net['NeuronGroup'] but it is not working.

def Model():
   thalamus = NeuronGroup()
   crtx = NeuronGroup()
   S = Synapses(thalamus, crtx)
   return(Network(collect()))

net = Model()
input=PoissonGroup()
S2 = Synapses(input, net['thalamus'])

and here i get the error KeyError: ‘No object with name “thalamus” found’

I initially thought was a namespace problem (it could be right?) but it doesn’t work even if i define a dummy neuron, add it to the net and try to access it.

dummy_neuron = NeuronGroup(1,....)
net.add(dummy_neuron)
net['dummy_neuron']

KeyError: ‘No object with name “dummy_neuron” found’

So i would like to know: How can i access to the Network objects and if i can use a single instance of Network() in multiple functions and saving step by step the state of the network

Hi @caccola816. The basic idea is correct, but you need to use the name of the object, which isn’t the name of the variable you used when you created the object. In your example, the Network does not know that your NeuronGroup was stored in a variable thalamus, and it could not have been stored in any variable at all, but instead in a list or something similar.
This is why each object in Brian also has an internal name that is used in a few places. By default, in both of your example, this name would be neurongroup (if there is more than one object, it would be neurongroup_2, etc.). But if you want to refer for it, and for easier debugging, I’d strongly recommend that you specify the name manually:

thalamus = NeuronGroup(..., name='thalamus')

You should then be able to access the object via net['thalamus'].

Hope that clears things up!

1 Like

Oh ok, perfect! Thank you!

1 Like