Network class¶
(Shortest import: from brian2 import Network)
-
class
brian2.core.network.Network(*objs, name='network*')[source]¶ Bases:
brian2.core.names.NameableThe main simulation controller in Brian
Networkhandles the running of a simulation. It contains a set of Brian objects that are added withadd. Therunmethod 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 theNetworkare accesible via their names, e.g.net['neurongroup']would return theNeuronGroupwith this name.- Parameters
objs : (
BrianObject, container), optionalname : str, optional
An explicit name, if not specified gives an automatically generated name
See also
Notes
The main run loop performs the following steps:
Prepare the objects if necessary, see
prepare.Determine the end time of the simulation as
t`+``duration`.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. Settto the clock time.If the
tvalue of these clocks is past the end time of the simulation, stop running. If theNetwork.stopmethod or thestop()function have been called, stop running. Settto the end time of the simulation.For each object whose
clockis set to one of the clocks from the previous steps, call theupdatemethod. This method will not be called if theactiveflag is set toFalse. The order in which the objects are called is described below.Increase
Clock.tbyClock.dtfor 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.scheduleand the objectswhenandorderattributes. Thescheduleis a list of string names. Eachwhenattribute 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 withbefore_andafter_can be used. That means that all objects withwhen=='before_start'will be updated first, then those withwhen=='start',when=='after_start',when=='before_groups',when=='groups'and so forth. If several objects have the samewhenattribute, then the order is determined by theorderattribute (lower first).Attributes
Stored state of objects (store/restore)
The set of objects in the Network, should not normally be modified directly.
The time spent in executing the various
CodeObjects.List of
whenslots in the order they will be updated, can be modified.The sorted objects of this network in the order defined by the schedule.
Current simulation time in seconds (
Quantity)Current time as a float
Methods
add(*objs)Add objects to the
Networkbefore_run(namespace)Prepares the
Networkfor a run.get_profiling_info(**kwds)The only reason this is not directly implemented in
profiling_infois to allow devices (e.g.get_states([units, format, subexpressions, …])Return a copy of the current state variable values of objects in the network.
remove(*objs)Remove an object or sequence of objects from a
Network.restore([name, filename, restore_random_state])Retore the state of the network and all included objects.
run(duration[, report, report_period, …])Runs the simulation for the given duration.
Return a
SchedulingSummaryobject, representing the scheduling information for all objects included in the network.set_states(values[, units, format, level])Set the state variables of objects in the network.
stop()Stops the network from running, this is reset the next time
Network.runis called.store([name, filename])Store the state of the network and all included objects.
Details
-
_stored_state¶ Stored state of objects (store/restore)
-
objects¶ The set 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 inbefore_runand emptied inafter_run
-
profiling_info¶ The time spent in executing the various
CodeObjects.A list of
(name, time)tuples, containing the name of theCodeObjectand the total execution time for simulations of this object (as aQuantitywith unitsecond). The list is sorted descending with execution time.Profiling has to be activated using the
profilekeyword inrun()orNetwork.run.
-
schedule¶ List of
whenslots in the order they will be updated, can be modified.See notes on scheduling in
Network. Note that additionalwhenslots 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.
-
sorted_objects¶ The sorted objects of this network in the order defined by the schedule.
Objects are sorted first by their
whenattribute, and secondly by theorderattribute. The order of thewhenattribute is defined by theschedule. In addition to the slot names defined in the schedule, automatic slot names starting withbefore_andafter_can be used (e.g. the slots['groups', 'thresholds']allow to use['before_groups', 'groups', 'after_groups', 'before_thresholds', 'thresholds', 'after_thresholds']).Final ties are resolved using the objects’ names, leading to an arbitrary but deterministic sorting.
-
t_¶ Current time as a float
-
add(*objs)[source]¶ Add objects to the
Network- Parameters
objs : (
BrianObject, container)The
BrianObjector container of Brian objects to be added. Specify multiple objects, or lists (or other containers) of objects. Containers will be added recursively. If the container is adictthen it will add the values from the dictionary but not the keys. If you want to add the keys, doadd(objs.keys()).
-
after_run()¶
-
before_run(namespace)¶ Prepares the
Networkfor a run.Objects in the
Networkare sorted into the correct running order, and theirBrianObject.before_runmethods are called.- Parameters
run_namespace : dict-like, optional
A namespace in which objects which do not define their own namespace will be run.
-
get_profiling_info(**kwds)¶ The only reason this is not directly implemented in
profiling_infois to allow devices (e.g.CPPStandaloneDevice) to overwrite this.
-
get_states(units=True, format='dict', subexpressions=False, read_only_variables=True, level=0)[source]¶ Return a copy of the current state variable values of objects in the network.. The returned arrays are copies of the actual arrays that store the state variable values, therefore changing the values in the returned dictionary will not affect the state variables.
- Parameters
vars : list of str, optional
The names of the variables to extract. If not specified, extract all state variables (except for internal variables, i.e. names that start with
'_'). If thesubexpressionsargument isTrue, the current values of all subexpressions are returned as well.units : bool, optional
Whether to include the physical units in the return value. Defaults to
True.format : str, optional
The output format. Defaults to
'dict'.subexpressions: bool, optional :
Whether to return subexpressions when no list of variable names is given. Defaults to
False. This argument is ignored if an explicit list of variable names is given invars.read_only_variables : bool, optional
Whether to return read-only variables (e.g. the number of neurons, the time, etc.). Setting it to
Falsewill assure that the returned state can later be used withset_states. Defaults toTrue.level : int, optional
How much higher to go up the stack to resolve external variables. Only relevant if extracting subexpressions that refer to external variables.
- Returns
values : dict
A dictionary mapping object names to the state variables of that object, in the specified
format.
See also
-
remove(*objs)[source]¶ Remove an object or sequence of objects from a
Network.- Parameters
objs : (
BrianObject, container)The
BrianObjector 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', filename=None, restore_random_state=False)¶ 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'.filename : str, optional
The name of the file from where the state should be restored. If not specified, it is expected that the state exist in memory (i.e.
Network.storewas previously called without thefilenameargument).restore_random_state : bool, optional
Whether to restore the state of the random number generator. If set to
True, going back to an earlier state of the simulation will continue exactly where it left off, even if the simulation is stochastic. If set toFalse(the default), random numbers are independent between runs (except for explicitly set random seeds), regardless of whetherstore()/restore()has been used or not. Note that this also restores numpy’s random number generator (since it is used internally by Brian), but it does not restore Python’s builtin random number generator in therandommodule.
-
run(duration, report=None, report_period=60 * second, namespace=None, level=0)¶ Runs the simulation for the given duration.
- Parameters
duration :
QuantityThe 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 callbackcallable(elapsed, complete, duration)which will be passed the amount of time elapsed as aQuantity, 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 thereport_period.report_period :
QuantityHow 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 toFalse.level : int, optional
How deep to go up the stack frame to look for the locals/global (see
namespaceargument). Only used by run functions that call this run function, e.g.MagicNetwork.runto adjust for the additional nesting.
Notes
The simulation can be stopped by calling
Network.stopor the globalstop()function.
-
scheduling_summary()[source]¶ Return a
SchedulingSummaryobject, representing the scheduling information for all objects included in the network.- Returns
summary :
SchedulingSummaryObject representing the scheduling information.
-
set_states(values, units=True, format='dict', level=0)[source]¶ Set the state variables of objects in the network.
- Parameters
values : dict
A dictionary mapping object names to objects of
format, setting the states of this object.units : bool, optional
Whether the
valuesinclude physical units. Defaults toTrue.format : str, optional
The format of
values. Defaults to'dict'level : int, optional
How much higher to go up the stack to _resolve external variables. Only relevant when using string expressions to set values.
See also
Group.set_states
-
stop()¶ Stops the network from running, this is reset the next time
Network.runis called.
-
store(name='default', filename=None)¶ Store the state of the network and all included objects.
- Parameters
name : str, optional
A name for the snapshot, if not specified uses
'default'.filename : str, optional
A filename where the state should be stored. If not specified, the state will be stored in memory.
Notes
The state stored to disk can be restored with the
Network.restorefunction. Note that it will only restore the internal state of all the objects (including undelivered spikes) – the objects have to exist already and they need to have the same name as when they were stored. Equations, thresholds, etc. are not stored – this is therefore not a general mechanism for object serialization. Also, the format of the file is not guaranteed to work across platforms or versions. If you are interested in storing the state of a network for documentation or analysis purposes useNetwork.get_statesinstead.
Tutorials and examples using this¶
Tutorial 3-intro-to-brian-simulations
Example IF_curve_LIF
Example IF_curve_Hodgkin_Huxley
Example advanced/stochastic_odes
Example frompapers/Brunel_Wang_2001