# Network class¶

(Shortest import: from brian2 import Network)

class brian2.core.network.Network(*objs, name='network*')[source]

The main simulation controller in Brian

Network handles the running of a simulation. It contains a set of Brian objects that are added with add. The run method actually runs the simulation. The main run loop, determining which objects get called in what order is described in detail in the notes below. The objects in the Network are accesible via their names, e.g. net['neurongroup'] would return the NeuronGroup with this name.

Parameters: objs : (BrianObject, container), optional A list of objects to be added to the Network immediately, see add. name : str, optional An explicit name, if not specified gives an automatically generated name

Notes

The main run loop performs the following steps:

1. Prepare the objects if necessary, see prepare.
2. Determine the end time of the simulation as t+duration.
3. Determine which set of clocks to update. This will be the clock with the smallest value of t. If there are several with the same value, then all objects with these clocks will be updated simultaneously. Set t to the clock time.
4. If the t value of these clocks is past the end time of the simulation, stop running. If the Network.stop() method or the stop() function have been called, stop running. Set t to the end time of the simulation.
5. For each object whose clock is set to one of the clocks from the previous steps, call the update method. This method will not be called if the active flag is set to False. The order in which the objects are called is described below.
6. Increase Clock.t by Clock.dt for each of the clocks and return to step 2.

The order in which the objects are updated in step 4 is determined by the Network.schedule and the objects when and order attributes. The schedule is a list of string names. Each when attribute should be one of these strings, and the objects will be updated in the order determined by the schedule. The default schedule is ['start', 'groups', 'thresholds', 'synapses', 'resets', 'end']. In addition to the names provided in the schedule, automatic names starting with before_ and after_ can be used. That means that all objects with when=='before_start' will be updated first, then those with when=='start', when=='after_start', when=='before_groups', when=='groups' and so forth. If several objects have the same when attribute, then the order is determined by the order attribute (lower first).

Attributes

 _stored_t Stored time for the store/restore mechanism objects The list of objects in the Network, should not normally be modified directly. profiling_info The time spent in executing the various CodeObjects. schedule List of when slots in the order they will be updated, can be modified. t Current simulation time in seconds (Quantity) t_ Current time as a float

Methods

 add(*objs) Add objects to the Network check_dependencies() remove(*objs) Remove an object or sequence of objects from a Network.

Details

_stored_t

Stored time for the store/restore mechanism

objects

The list of objects in the Network, should not normally be modified directly. Note that in a MagicNetwork, this attribute only contains the objects during a run: it is filled in before_run and emptied in after_run

profiling_info

The time spent in executing the various CodeObjects.

A list of (name, time) tuples, containing the name of the CodeObject and the total execution time for simulations of this object (as a Quantity with unit second). The list is sorted descending with execution time.

Profiling has to be activated using the profile keyword in run() or Network.run().

schedule

List of when slots in the order they will be updated, can be modified.

See notes on scheduling in Network. Note that additional when slots can be added, but the schedule should contain at least all of the names in the default schedule: ['start', 'groups', 'thresholds', 'synapses', 'resets', 'end'].

The schedule can also be set to None, resetting it to the default schedule set by the core.network.default_schedule preference.

t

Current simulation time in seconds (Quantity)

t_

Current time as a float

add(*objs)[source]

Add objects to the Network

Parameters: objs : (BrianObject, container) The BrianObject or container of Brian objects to be added. Specify multiple objects, or lists (or other containers) of objects. Containers will be added recursively.
after_run()
before_run(namespace)

Prepares the Network for a run.

Objects in the Network are sorted into the correct running order, and their BrianObject.before_run() methods are called.

Parameters: namespace : dict-like, optional A namespace in which objects which do not define their own namespace will be run.
check_dependencies()[source]
get_profiling_info(*args, **kwds)

The only reason this is not directly implemented in profiling_info is to allow devices (e.g. CPPStandaloneDevice) to overwrite this.

remove(*objs)[source]

Remove an object or sequence of objects from a Network.

Parameters: objs : (BrianObject, container) The BrianObject or container of Brian objects to be removed. Specify multiple objects, or lists (or other containers) of objects. Containers will be removed recursively.
restore(name='default')

Retore the state of the network and all included objects.

Parameters: name : str, optional The name of the snapshot to restore, if not specified uses 'default'.
run(duration, report=None, report_period=60*second, namespace=None, level=0)

Runs the simulation for the given duration.

Parameters: duration : Quantity The amount of simulation time to run for. report : {None, ‘text’, ‘stdout’, ‘stderr’, function}, optional How to report the progress of the simulation. If None, do not report progress. If 'text' or 'stdout' is specified, print the progress to stdout. If 'stderr' is specified, print the progress to stderr. Alternatively, you can specify a callback callable(elapsed, complete, duration) which will be passed the amount of time elapsed as a Quantity, the fraction complete from 0.0 to 1.0 and the total duration of the simulation (in biological time). The function will always be called at the beginning and the end (i.e. for fractions 0.0 and 1.0), regardless of the report_period. report_period : Quantity How frequently (in real time) to report progress. namespace : dict-like, optional A namespace that will be used in addition to the group-specific namespaces (if defined). If not specified, the locals and globals around the run function will be used. profile : bool, optional Whether to record profiling information (see Network.profiling_info). Defaults to True. level : int, optional How deep to go up the stack frame to look for the locals/global (see namespace argument). Only used by run functions that call this run function, e.g. MagicNetwork.run() to adjust for the additional nesting.

Notes

The simulation can be stopped by calling Network.stop() or the global stop() function.

stop()

Stops the network from running, this is reset the next time Network.run() is called.

store(name='default')

Store the state of the network and all included objects.

Parameters: name : str, optional A name for the snapshot, if not specified uses 'default'.