3.12. Network

A Network object holds copies of previously defined populations, projections or monitors in order to simulate them independently.

The parallel_run() method can be used to simulate different networks in parallel.

3.12.1. Parallel simulations

Network.parallel_run(method, networks=None, number=0, max_processes=-1, measure_time=False, sequential=False, same_seed=False, **args)

Allows to run multiple networks in parallel using multiprocessing.

If the networks argument is provided as a list of Network objects, the given method will be executed for each of these networks.

If number is given instead, the same number of networks will be created and the method is applied.

Returns:

  • a list of the values returned by method.

If number is used, the created networks are not returned, you should return what you need to analyse.

Parameters:

  • method: a Python method which will be executed for each network. This function must accept an integer as first argument (id of the simulation) and a Network object as second argument.
  • networks: a list of networks to simulate in parallel.
  • number: the number of odentical networks to run in parallel.
  • max_processes: maximal number of processes to start concurrently (default: the available number of cores on the machine).
  • measure_time: if the total simulation time should be printed out.
  • sequential: if True, runs the simulations sequentially instead of in parallel (default: False).
  • same_seed: if True, all networks will use the same seed. If not, the seed will be randomly initialized with time(0) for each network (default).
  • args: other named arguments you want to pass to the simulation method.

Example::

pop1 = PoissonPopulation(100, rates=10.0)
pop2 = Population(100, Izhikevich)
proj = Projection(pop1, pop2, 'exc')
proj.connect_fixed_probability(weights=5.0, probability=0.2)
m = Monitor(pop2, 'spike')

compile()

def simulation(idx, net):
    net.get(pop1).rates = 10. * idx
    net.simulate(1000.)
    return net.get(m).raster_plot()

results = parallel_run(method=simulation, number = 3)

t1, n1 = results[0]
t2, n2 = results[1]
t3, n3 = results[2]

3.12.2. Network class

class ANNarchy.Network(everything=False)[source]

A network gathers already defined populations, projections and monitors in order to run them independently.

This is particularly useful when varying single parameters of a network and comparing the results (see the parallel_run() method).

Only objects declared before the creation of the network can be used. Global methods such as simulate() must be used on the network object. The objects must be accessed through the get() method, as the original ones will not be part of the network (a copy is made).

Each network must be individually compiled, but it does not matter if the original objects were already compiled.

When passing everything=True to the constructor, all populations/projections/monitors already defined at the global level will be added to the network.

If not, you can select which object will be added to network with the add() method.

Example with everything=True:

pop = Population(100, Izhikevich)
proj = Projection(pop, pop, 'exc')
proj.connect_all_to_all(1.0)
m = Monitor(pop, 'spike')

compile() # Optional

net = Network(everything=True)
net.get(pop).a = 0.02
net.compile()
net.simulate(1000.)

net2 = Network(everything=True)
net2.get(pop).a = 0.05
net2.compile()
net2.simulate(1000.)

t, n = net.get(m).raster_plot()
t2, n2 = net2.get(m).raster_plot()

Example with everything=False (the default):

pop = Population(100, Izhikevich)
proj1 = Projection(pop, pop, 'exc')
proj1.connect_all_to_all(1.0)
proj2 = Projection(pop, pop, 'exc')
proj2.connect_all_to_all(2.0)
m = Monitor(pop, 'spike')

net = Network()
net.add([pop, proj1, m])
net.compile()
net.simulate(1000.)

net2 = Network()
net2.add([pop, proj2, m])
net2.compile()
net2.simulate(1000.)

t, n = net.get(m).raster_plot()
t2, n2 = net2.get(m).raster_plot()

Parameters:

  • everything: defines if all existing populations and projections should be automatically added (default: False).
add(objects)[source]

Adds a Population, Projection or Monitor to the network.

Parameters:

  • objects: A single object or a list to add to the network.
compile(directory='annarchy', clean=False, compiler='default', compiler_flags='-march=native -O2', cuda_config=None, silent=False)[source]

Compiles the network.

Parameters:

  • directory: name of the subdirectory where the code will be generated and compiled. Must be a relative path. Default: “annarchy/”.
  • clean: boolean to specifying if the library should be recompiled entirely or only the changes since last compilation (default: False).
  • compiler: C++ compiler to use. Default: g++ on GNU/Linux, clang++ on OS X. Valid compilers are [g++, clang++].
  • compiler_flags: platform-specific flags to pass to the compiler. Default: “-march=native -O2”. Warning: -O3 often generates slower code and can cause linking problems, so it is not recommended.
  • cuda_config: dictionary defining the CUDA configuration for each population and projection.
  • silent: defines if the “Compiling... OK” should be printed.
disable_learning(projections=None)[source]

Disables learning for all projections.

Parameter:

  • projections: the projections whose learning should be disabled. By default, all the existing projections are disabled.
enable_learning(projections=None)[source]

Enables learning for all projections.

Parameter:

  • projections: the projections whose learning should be enabled. By default, all the existing projections are disabled.
get(obj)[source]

Returns the local Population, Projection or Monitor identical to the provided argument.

Parameters:

  • obj: A single object or a list of objects.

Returns:

  • The corresponding object or list of objects.

Example:

pop = Population(100, Izhikevich)
net = Network()
net.add(pop)
net.compile()
net.simulate(100.)
print net.get(pop).v
get_current_step()[source]

Returns the current simulation step.

get_population(name)[source]

Returns the population with the given name.

Parameter:

  • name: name of the population

Returns:

  • The requested Population object if existing, None otherwise.
get_projection(name)[source]

Returns the projection with the given name.

Parameter:

  • name: name of the projection

Returns:

  • The requested Projection object if existing, None otherwise.
get_time()[source]

Returns the current time in ms.

reset(populations=True, projections=False, synapses=False)[source]

Reinitialises the network to its state before the call to compile.

Parameters:

  • populations: if True (default), the neural parameters and variables will be reset to their initial value.
  • projections: if True, the synaptic parameters and variables (except the connections) will be reset (default=False).
  • synapses: if True, the synaptic weights will be erased and recreated (default=False).
set_current_step(t)[source]

Sets the current simulation step.

Warning

Can be dangerous for some spiking models.

set_seed(seed)[source]

Sets the seed of the random number generators for this network.

set_time(t, net_id=0)[source]

Sets the current time in ms.

Warning

Can be dangerous for some spiking models.

simulate(duration, measure_time=False)[source]

Runs the network for the given duration in milliseconds. The number of simulation steps is computed relative to the discretization step dt declared in setup() (default: 1ms):

simulate(1000.0)

Parameters:

  • duration: the duration in milliseconds.
  • measure_time: defines whether the simulation time should be printed (default=False).
simulate_until(max_duration, population, operator='and', measure_time=False)[source]

Runs the network for the maximal duration in milliseconds. If the stop_condition defined in the population becomes true during the simulation, it is stopped.

One can specify several populations. If the stop condition is true for any of the populations, the simulation will stop (‘or’ function).

Example:

pop1 = Population( ..., stop_condition = "r > 1.0 : any")
compile()
simulate_until(max_duration=1000.0. population=pop1)

Parameters:

  • duration: the maximum duration of the simulation in milliseconds.
  • population: the (list of) population whose stop_condition should be checked to stop the simulation.
  • operator: operator to be used (‘and’ or ‘or’) when multiple populations are provided (default: ‘and’).
  • measure_time: defines whether the simulation time should be printed (default=False).

Returns:

  • the actual duration of the simulation in milliseconds.
step()[source]

Performs a single simulation step (duration = dt).