MagicNetwork class
(Shortest import: from brian2 import MagicNetwork)
- class brian2.core.magic.MagicNetwork(*args, **kw)[source]
Bases:
Network
Network
that automatically adds all Brian objectsIn order to avoid bugs, this class will occasionally raise
MagicError
when the intent of the user is not clear. See the notes below for more details on this point. If you persistently see this error, then Brian is not able to safely guess what you intend to do, and you should use aNetwork
object and callNetwork.run
explicitly.Note that this class cannot be instantiated by the user, there can be only one instance
magic_network
ofMagicNetwork
.Notes
All Brian objects that are visible at the point of the
run()
call will be included in the network. This class is designed to work in the following two major use cases:You create a collection of Brian objects, and call
run()
to run the simulation. Subsequently, you may callrun()
again to run it again for a further duration. In this case, theNetwork.t
time will start at 0 and for the second call torun()
will continue from the end of the previous run.You have a loop in which at each iteration, you create some Brian objects and run a simulation using them. In this case, time is reset to 0 for each call to
run()
.
In any other case, you will have to explicitly create a
Network
object yourself and callNetwork.run
on this object. Brian has a built in system to guess which of the cases above applies and behave correctly. When it is not possible to safely guess which case you are in, it raisesMagicError
. The rules for this guessing system are explained below.If a simulation consists only of objects that have not been run, it will assume that you want to start a new simulation. If a simulation only consists of objects that have been simulated in the previous
run()
call, it will continue that simulation at the previous time.If neither of these two situations apply, i.e., the network consists of a mix of previously run objects and new objects, an error will be raised.
In these checks, “non-invalidating” objects (i.e. objects that have
BrianObject.invalidates_magic_network
set toFalse
) are ignored, e.g. creating new monitors is always possible.Methods
add
(*objs)You cannot add objects directly to
MagicNetwork
get_states
([units, format, subexpressions, ...])See
Network.get_states
.remove
(*objs)You cannot remove objects directly from
MagicNetwork
restore
([name, filename, ...])See
Network.restore
.run
(duration[, report, report_period, ...])Runs the simulation for the given duration.
set_states
(values[, units, format, level])See
Network.set_states
.store
([name, filename, level])See
Network.store
.Details
- add(*objs)[source]
You cannot add objects directly to
MagicNetwork
- get_states(units=True, format='dict', subexpressions=False, level=0)[source]
See
Network.get_states
.
- remove(*objs)[source]
You cannot remove objects directly from
MagicNetwork
- restore(name='default', filename=None, restore_random_state=False, level=0)[source]
See
Network.restore
.
- run(duration, report=None, report_period=60 * second, namespace=None, level=0)[source]
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 callbackcallable(elapsed, completed, start, duration)
which will be passed the amount of time elapsed as aQuantity
, the fractioncompleted
from 0.0 to 1.0, thestart
time of the simulation as aQuantity
and the total duration of the simulation (in biological time) as aQuantity
. 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 :
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 toNone
(which will use the value set byset_device
, if any).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 globalstop()
function.
- set_states(values, units=True, format='dict', level=0)[source]
See
Network.set_states
.
- store(name='default', filename=None, level=0)[source]
See
Network.store
.