# MagicNetwork class

(Shortest import: `from brian2 import MagicNetwork)`

class brian2.core.magic.MagicNetwork(*args, **kw)

  Bases: `Network`

  `Network` that automatically adds all Brian objects

  In 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 a `Network` object and call `Network.run` explicitly.

  Note that this class cannot be instantiated by the user, there can be only one instance `magic_network` of `MagicNetwork`.

  See also

  `Network`, `collect()`, `run()`, `stop()`, `store()`, `restore()`

  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:

  1. You create a collection of Brian objects, and call `run()` to run the simulation. Subsequently, you may call `run()` again to run it again for a further duration. In this case, the `Network.t` time will start at 0 and for the second call to `run()` will continue from the end of the previous run.

  2. 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 call `Network.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 raises `MagicError`. 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 to `False`) are ignored, e.g. creating new monitors is always possible.

  Methods

  `add`(*objs) | You cannot add objects directly to `MagicNetwork`
  `after_run`() |
  `check_dependencies`() |
  `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)

    You cannot add objects directly to `MagicNetwork`

  after_run()
  check_dependencies()
  get_states(units=True, format='dict', subexpressions=False, level=0)

    See `Network.get_states`.

  remove(*objs)

    You cannot remove objects directly from `MagicNetwork`

  restore(name='default', filename=None, restore_random_state=False, level=0)

    See `Network.restore`.

  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, completed, start, duration)` which will be passed the amount of time elapsed as a `Quantity`, the fraction `completed` from 0.0 to 1.0, the `start` time of the simulation as a `Quantity` and the total duration of the simulation (in biological time) as a `Quantity`. 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 `None` (which will use the value set by `set_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.

    Raises

    `ValueError`

      Error raised when duration passed in run function is negative.

    Notes

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

  set_states(values, units=True, format='dict', level=0)

    See `Network.set_states`.

  store(name='default', filename=None, level=0)

    See `Network.store`.