| 1 |
Networks and clocks (Brian 1 --> 2 conversion) |
| 2 |
============================================== |
| 3 |
.. sidebar:: Brian 2 documentation |
| 4 |
|
| 5 |
For the main documentation about running simulations, controling the |
| 6 |
simulation timestep, etc., see the document :doc:`../../user/running`. |
| 7 |
|
| 8 |
.. contents:: |
| 9 |
:local: |
| 10 |
:depth: 1 |
| 11 |
|
| 12 |
Clocks and timesteps |
| 13 |
-------------------- |
| 14 |
Brian's system of handling clocks has substantially changed. For details about |
| 15 |
the new system in place see :ref:`time_steps`. The main differences to Brian 1 |
| 16 |
are: |
| 17 |
|
| 18 |
* There is no more "clock guessing" -- objects either use the `defaultclock` or |
| 19 |
a ``dt``/``clock`` value that was explicitly specified during their |
| 20 |
construction. |
| 21 |
* In Brian 2, the time step is allowed to change after the creation of an object |
| 22 |
and between runs -- the relevant value is the value in place at the point of |
| 23 |
the `run` call. |
| 24 |
* It is rarely necessary to create an explicit `Clock` object, most of the time |
| 25 |
you should use the `defaultclock` or provide a ``dt`` argument during the |
| 26 |
construction of the object. |
| 27 |
* There's only one `Clock` class, the (deprecated) ``FloatClock``, |
| 28 |
``RegularClock``, etc. classes that Brian 1 provided no longer exist. |
| 29 |
* It is no longer possible to (re-)set the time of a clock explicitly, there is |
| 30 |
no direct equivalent of ``Clock.reinit`` and ``reinit_default_clock``. To |
| 31 |
start a completely new simulation after you have finished a previous one, |
| 32 |
either create a new `Network` or use the `start_scope` mechanism. To "rewind" |
| 33 |
a simulation to a previous point, use the new `store`/`restore` mechanism. For |
| 34 |
more details, see below and :doc:`../../user/running`. |
| 35 |
|
| 36 |
Networks |
| 37 |
-------- |
| 38 |
Both Brian 1 and Brian 2 offer two ways to run a simulation: either by |
| 39 |
explicitly creating a `Network` object, or by using a `MagicNetwork`, i.e. a |
| 40 |
simple `run` statement. |
| 41 |
|
| 42 |
Explicit network |
| 43 |
~~~~~~~~~~~~~~~~ |
| 44 |
The mechanism to create explicit `Network` objects has not changed significantly |
| 45 |
from Brian 1 to Brian 2. However, creating a new `Network` will now also |
| 46 |
automatically reset the clock back to 0s, and stricter checks no longer allow |
| 47 |
the inclusion of the same object in multiple networks. |
| 48 |
|
| 49 |
+------------------------------+------------------------------+ |
| 50 |
+ Brian 1 | Brian 2 | |
| 51 |
+==============================+==============================+ |
| 52 |
| .. code:: | .. code:: | |
| 53 |
| | | |
| 54 |
| group = ... | group = ... | |
| 55 |
| mon = ... | mon = ... | |
| 56 |
| net = Network(group, mon) | net = Network(group, mon) | |
| 57 |
| net.run(1*ms) | net.run(1*ms) | |
| 58 |
| | | |
| 59 |
| reinit() | # new network starts at 0s| |
| 60 |
| group = ... | group = ... | |
| 61 |
| mon = ... | mon = ... | |
| 62 |
| net = Network(group, mon) | net = Network(group, mon) | |
| 63 |
| net.run(1*ms) | net.run(1*ms) | |
| 64 |
| | | |
| 65 |
+------------------------------+------------------------------+ |
| 66 |
|
| 67 |
"Magic" network |
| 68 |
~~~~~~~~~~~~~~~ |
| 69 |
For most simple, "flat", scripts (see e.g. the :doc:`../../examples/index`), |
| 70 |
the `run` statement in Brian 2 automatically collects all the Brian objects |
| 71 |
(`NeuronGroup`, etc.) into a "magic" network in the same way as Brian 1 did. |
| 72 |
The logic behind this collection has changed, though, with important |
| 73 |
consequences for more complex simulation scripts: in Brian 1, the magic network |
| 74 |
includes all Brian objects that have been *created* in the same execution frame |
| 75 |
as the `run` call. Objects that are created in other functions could be added |
| 76 |
using ``magic_return`` and ``magic_register``. In Brian 2, the magic network |
| 77 |
contains all Brian objects that are *visible* in the same execution frame as the |
| 78 |
`run` call. The advantage of the new system is that it is clearer what will be |
| 79 |
included in the network and there is no danger of including previously created, |
| 80 |
but no longer needed, objects in a simulation. E.g. in the following example, |
| 81 |
a common mistake in Brian 1 was to not include the `clear()`, which meant that |
| 82 |
each run not only simulated the current objects, but also all objects from |
| 83 |
previous loop iterations. Also, without the ``reinit_default_clock()``, |
| 84 |
each run would start at the end time of the previous run. In Brian 2, this loop |
| 85 |
does not need any explicit clearing up, each `run` will only simulate the |
| 86 |
object that it "sees" (``group1``, ``group2``, ``syn``, and ``mon``) and start |
| 87 |
each simulation at 0s: |
| 88 |
|
| 89 |
+--------------------------------------------+--------------------------------------------+ |
| 90 |
| Brian 1 | Brian 2 | |
| 91 |
+============================================+============================================+ |
| 92 |
| .. code:: | .. code:: | |
| 93 |
| | | |
| 94 |
| for r in range(100): | for r in range(100): | |
| 95 |
| reinit_default_clock() | | |
| 96 |
| clear() | | |
| 97 |
| group1 = NeuronGroup(...) | group1 = NeuronGroup(...) | |
| 98 |
| group2 = NeuronGroup(...) | group2 = NeuronGroup(...) | |
| 99 |
| syn = Synapses(group1, group2, ...)| syn = Synapses(group1, group2, ...)| |
| 100 |
| mon = SpikeMonitor(group2) | mon = SpikeMonitor(group2) | |
| 101 |
| run(1*second) | run(1*second) | |
| 102 |
| | | |
| 103 |
+--------------------------------------------+--------------------------------------------+ |
| 104 |
|
| 105 |
There is no replacement for the ``magic_return`` and ``magic_register`` |
| 106 |
functions. If the returned object is stored in a variable at the level of |
| 107 |
the `run` call, then it is no longer necessary to use ``magic_return``, as the |
| 108 |
returned object is "visible" at the level of the `run` call: |
| 109 |
|
| 110 |
+-----------------------------------------------+-------------------------------------------------+ |
| 111 |
| Brian 1 | Brian 2 | |
| 112 |
+===============================================+=================================================+ |
| 113 |
| .. code:: | .. code:: | |
| 114 |
| | | |
| 115 |
| @magic_return | | |
| 116 |
| def f(): | def f(): | |
| 117 |
| return PoissonGroup(100, rates=100*Hz)| return PoissonGroup(100, rates=100*Hz) | |
| 118 |
| | | |
| 119 |
| pg = f() # needs magic_return | pg = f() # is "visible" and will be included| |
| 120 |
| mon = SpikeMonitor(pg) | mon = SpikeMonitor(pg) | |
| 121 |
| run(100*ms) | run(100*ms) | |
| 122 |
| | | |
| 123 |
+-----------------------------------------------+-------------------------------------------------+ |
| 124 |
|
| 125 |
The general recommendation is however: if your script is complex (multiple |
| 126 |
functions/files/classes) and you are not sure whether some objects will be |
| 127 |
included in the magic network, use an explicit `Network` object. |
| 128 |
|
| 129 |
Note that one consequence of the "is visible" approach is that objects stored |
| 130 |
in containers (lists, dictionaries, ...) will not be automatically included in |
| 131 |
Brian 2. Use an explicit `Network` object to get around this restriction: |
| 132 |
|
| 133 |
+----------------------------------------+----------------------------------------+ |
| 134 |
| Brian 1 | Brian 2 | |
| 135 |
+========================================+========================================+ |
| 136 |
| .. code:: | .. code:: | |
| 137 |
| | | |
| 138 |
| groups = {'exc': NeuronGroup(...), | groups = {'exc': NeuronGroup(...), | |
| 139 |
| 'inh': NeuronGroup(...)} | 'inh': NeuronGroup(...)} | |
| 140 |
| ... | ... | |
| 141 |
| | net = Network(groups) | |
| 142 |
| run(5*ms) | net.run(5*ms) | |
| 143 |
| | | |
| 144 |
+----------------------------------------+----------------------------------------+ |
| 145 |
|
| 146 |
External constants |
| 147 |
~~~~~~~~~~~~~~~~~~ |
| 148 |
In Brian 2, external constants are taken from the surrounding namespace at |
| 149 |
the point of the `run` call and not when the object is defined (for other ways |
| 150 |
to define the namespace, see :ref:`external-variables`). This allows to easily |
| 151 |
change external constants between runs, in contrast to Brian 1 where the whether |
| 152 |
this worked or not depended on details of the model (e.g. whether linear |
| 153 |
integration was used): |
| 154 |
|
| 155 |
+----------------------------------------------------------+-----------------------------------------------------------+ |
| 156 |
| Brian 1 | Brian 2 | |
| 157 |
+==========================================================+===========================================================+ |
| 158 |
| .. code:: | .. code:: | |
| 159 |
| | | |
| 160 |
| tau = 10*ms | tau = 10*ms | |
| 161 |
| # to be sure that changes between runs are taken into | | |
| 162 |
| # account, define "I" as a neuronal parameter | # The value for I will be updated at each run | |
| 163 |
| group = NeuronGroup(10, '''dv/dt = (-v + I) / tau : 1 | group = NeuronGroup(10, 'dv/dt = (-v + I) / tau : 1') | |
| 164 |
| I : 1''') | | |
| 165 |
| group.v = linspace(0, 1, 10) | group.v = linspace(0, 1, 10) | |
| 166 |
| group.I = 0.0 | I = 0.0 | |
| 167 |
| mon = StateMonitor(group, 'v', record=True) | mon = StateMonitor(group, 'v', record=True) | |
| 168 |
| run(5*ms) | run(5*ms) | |
| 169 |
| group.I = 0.5 | I = 0.5 | |
| 170 |
| run(5*ms) | run(5*ms) | |
| 171 |
| group.I = 0.0 | I = 0.0 | |
| 172 |
| run(5*ms) | run(5*ms) | |
| 173 |
| | | |
| 174 |
+----------------------------------------------------------+-----------------------------------------------------------+ |