Correct use of the network constructor

11 views
Skip to first unread message

Emma

unread,
Jul 30, 2024, 12:10:04 PM7/30/24
to ANNarchy

Dear Helge and Julien

 

I recently started using ANNarchy. Thank you for the great work. It is very easy to get started, and the documentation is fantastic. 


There is one beginner's question I am stuck on, and I hope you can set me on the right path: 

I would like to create my network object inside a function, return it and use it outside the function. I used the following to construct the network and assumed this would work. However, it seems the network objects are not contained within net as expected. 

 

net = Network(everything=True)

 

Below is a simple example of what I would like to do. It fails because the monitor I am trying to access in the last line is unknown (NameError: name 'm' is not defined). 

 

def build_model():
    input_pop = Population(
1Neuron(parameters="r=10.0"))
    pop1 = Population(
1LeakyIntegrator)
    proj = Projection(input_pop
pop1'exc')
    proj.connect_all_to_all(
1.0)

    
= Monitor(pop1'r')

    net = Network(
everything=True)

    
return net # return net, input_pop, pop1, proj, m

net = build_model() # net, input_pop, pop1, proj, m = build_model()

net.compile()
net.simulate(
10.0)
print(net.get(m).get('r'))

 

The solution I found is commented out. I am not sure if maybe I'm overlooking a better solution. I am using the constructor wrong? 

 

Thanks for the heads up 

 

Emma 

julien...@gmail.com

unread,
Jul 30, 2024, 12:24:25 PM7/30/24
to ANNarchy
Dear Emma,

thanks for the nice words!

Unfortunately, there is currently no way to access a monitor in a network if you do not have its original reference (it is deleted at the end of the function, as Python's garbage collector thinks it will not be used again). I am working on a better interface for the Network class, but it can take a while. 

Meanwhile, if you do not plan to run the network in parallel, I would suggest using a class instead:
 

class Model:

def __init__(self):
self.input_pop = Population(1, Neuron(parameters="r=10.0"))
self.pop1 = Population(1, LeakyIntegrator)
self.proj = Projection(self.input_pop, self.pop1, 'exc')
self.proj.connect_all_to_all(1.0)

self.m = Monitor(self.pop1, 'r')

net = Model()

compile()
simulate(10.0)

print(net.m.get('r'))

That way, you keep a reference to m in the object. If you plan to create the model multiple times, make sure to call clear() at the beginning of the constructor.

Best regards
Julien

Emma

unread,
Jul 31, 2024, 2:55:00 AM7/31/24
to ANNarchy
Dear Julien, 

thank you so much for the super quick help :-) 

Best, 
Emma 

Reply all
Reply to author
Forward
0 new messages