| 1 |
Inputs (Brian 1 --> 2 conversion) |
| 2 |
================================= |
| 3 |
.. sidebar:: Brian 2 documentation |
| 4 |
|
| 5 |
For the main documentation about adding external stimulation to a network, |
| 6 |
see the document :doc:`../../user/input`. |
| 7 |
|
| 8 |
.. contents:: |
| 9 |
:local: |
| 10 |
:depth: 1 |
| 11 |
|
| 12 |
Poisson Input |
| 13 |
------------- |
| 14 |
Brian 2 provides the same two groups that Brian 1 provided: `PoissonGroup` and |
| 15 |
`PoissonInput`. The mechanism for inhomogoneous Poisson processes has changed: |
| 16 |
instead of providing a Python function of time, you'll now have to provide a |
| 17 |
string expression that is evaluated at every time step. For most use cases, this |
| 18 |
should allow a direct translation: |
| 19 |
|
| 20 |
+-------------------------------------------------+------------------------------------------+ |
| 21 |
| Brian 1 | Brian 2 | |
| 22 |
+=================================================+==========================================+ |
| 23 |
+ .. code:: | .. code:: | |
| 24 |
+ | | |
| 25 |
+ rates = lambda t:(1+cos(2*pi*t*1*Hz))*10*Hz | rates = '(1 + cos(2*pi*t*1*Hz)*10*Hz)' | |
| 26 |
+ group = PoissonGroup(100, rates=rates) | group = PoissonGroup(100, rates=rates) | |
| 27 |
+ | | |
| 28 |
+-------------------------------------------------+------------------------------------------+ |
| 29 |
|
| 30 |
For more complex rate modulations, the expression can refer to |
| 31 |
:ref:`user_functions` and/or you can replace the `PoissonGroup` by a general |
| 32 |
`NeuronGroup` with a threshold condition ``rand()<rates*dt`` (which allows you |
| 33 |
to store per-neuron attributes). |
| 34 |
|
| 35 |
There is currently no direct replacement for the more advanced features of |
| 36 |
`PoissonInput` (``record``, ``freeze``, ``copies``, ``jitter``, and |
| 37 |
``reliability`` keywords), but various workarounds are possible, e.g. by |
| 38 |
directly using a `BinomialFunction` in the equations. For example, you can get |
| 39 |
the functionality of the ``freeze`` keyword (identical Poisson events for all |
| 40 |
neurons) by storing the input in a shared variable and then distribute the input |
| 41 |
to all neurons: |
| 42 |
|
| 43 |
+---------------------------------------------------+-------------------------------------------------------------+ |
| 44 |
| Brian 1 | Brian 2 | |
| 45 |
+===================================================+=============================================================+ |
| 46 |
+ .. code:: | .. code:: | |
| 47 |
+ | | |
| 48 |
+ group = NeuronGroup(10, | group = NeuronGroup(10, '''dv/dt = -v / (10*ms) : 1 | |
| 49 |
+ 'dv/dt = -v/(10*ms) : 1') | shared_input : 1 (shared)''') | |
| 50 |
+ input = PoissonInput(group, N=1000, rate=1*Hz, | poisson_input = BinomialFunction(n=1000, p=1*Hz*group.dt) | |
| 51 |
+ weight=0.1, state='v', | group.run_regularly('''shared_input = poisson_input()*0.1 | |
| 52 |
+ freeze=True) | v += shared_input''') | |
| 53 |
+ | | |
| 54 |
+---------------------------------------------------+-------------------------------------------------------------+ |
| 55 |
|
| 56 |
Spike generation |
| 57 |
---------------- |
| 58 |
`SpikeGeneratorGroup` provides mostly the same functionality as in Brian 1. In |
| 59 |
contrast to Brian 1, there is only one way to specify which neurons spike and |
| 60 |
when -- you have to provide the index array and the times array as separate |
| 61 |
arguments: |
| 62 |
|
| 63 |
+----------------------------------------------------------+----------------------------------------------------+ |
| 64 |
| Brian 1 | Brian 2 | |
| 65 |
+==========================================================+====================================================+ |
| 66 |
| .. code:: | .. code:: | |
| 67 |
| | | |
| 68 |
| gen1 = SpikeGeneratorGroup(2, [(0, 0*ms), (1, 1*ms)]) | gen1 = SpikeGeneratorGroup(2, [0, 1], [0, 1]*ms) | |
| 69 |
| gen2 = SpikeGeneratorGroup(2, [(array([0, 1]), 0*ms), | gen2 = SpikeGeneratorGroup(2, [0, 1, 0, 1], | |
| 70 |
| (array([0, 1]), 1*ms)] | [0, 0, 1, 1]*ms) | |
| 71 |
| gen3 = SpikeGeneratorGroup(2, (array([0, 1]), | gen3 = SpikeGeneratorGroup(2, [0, 1], [0, 1]*ms) | |
| 72 |
| array([0, 1])*ms)) | | |
| 73 |
| gen4 = SpikeGeneratorGroup(2, array([[0, 0.0], | gen4 = SpikeGeneratorGroup(2, [0, 1], [0, 1]*ms) | |
| 74 |
| [1, 0.001]]) | | |
| 75 |
+----------------------------------------------------------+----------------------------------------------------+ |
| 76 |
|
| 77 |
.. note:: |
| 78 |
|
| 79 |
For large arrays, make sure to provide a `Quantity` array (e.g. |
| 80 |
``[0, 1, 2]*ms``) and not a list of `Quantity` values (e.g. |
| 81 |
``[0*ms, 1*ms, 2*ms]``). A list has first to be translated into an array |
| 82 |
which can take a considerable amount of time for a list with many elements. |
| 83 |
|
| 84 |
There is no direct equivalent of the Brian 1 option to use a generator that |
| 85 |
updates spike times online. The easiest alternative in Brian 2 is to |
| 86 |
pre-calculate the spikes and then use a standard `SpikeGeneratorGroup`. If this |
| 87 |
is not possible (e.g. there are two many spikes to fit in memory), then you can |
| 88 |
workaround the restriction by using custom code (see :ref:`user_functions` and |
| 89 |
:ref:`network_operation`). |
| 90 |
|
| 91 |
Arbitrary time-dependent input (``TimedArray``) |
| 92 |
----------------------------------------------- |
| 93 |
For a detailed description of the `TimedArray` mechanism in Brian 2, see |
| 94 |
:ref:`timed_arrays`. |
| 95 |
|
| 96 |
In Brian 1, timed arrays where special objects that could be assigned to a |
| 97 |
state variable and would then be used to update this state variable at every |
| 98 |
time step. In Brian 2, a timed array is implemented using the standard |
| 99 |
:doc:`../../advanced/functions` mechanism which has the advantage that more |
| 100 |
complex access patterns can be implemented (e.g. by not using ``t`` as an |
| 101 |
argument, but something like ``t - delay``). This syntax was possible in Brian 1 |
| 102 |
as well, but was disadvantageous for performance and had other limits (e.g. no |
| 103 |
unit support, no linear integration). In Brian 2, these disadvantages no longer |
| 104 |
apply and the function syntax is therefore the only available syntax. You can |
| 105 |
convert the old-style Brian 1 syntax to Brian 2 as follows: |
| 106 |
|
| 107 |
.. warning:: |
| 108 |
The example below does not correctly translate the changed semantics of |
| 109 |
`TimedArray` related to the time. In Brian 1, |
| 110 |
``TimedArray([0, 1, 2], dt=10*ms)`` will return ``0`` for ``t<5*ms``, ``1`` |
| 111 |
for ``5*ms<=t<15*ms``, and ``2`` for ``t>=15*ms``. Brian 2 will return ``0`` |
| 112 |
for ``t<10*ms``, ``1`` for ``10*ms<=t<20*ms``, and ``2`` for ``t>=20*ms``. |
| 113 |
|
| 114 |
+-----------------------------------------------------------+----------------------------------------------------+ |
| 115 |
| Brian 1 | Brian 2 | |
| 116 |
+===========================================================+====================================================+ |
| 117 |
| .. code:: | .. code:: | |
| 118 |
| | | |
| 119 |
| # same input for all neurons | # same input for all neurons | |
| 120 |
| eqs = ''' | I = TimedArray(linspace(0*mV, 20*mV, 100), | |
| 121 |
| dv/dt = (I - v)/tau : volt | dt=10*ms) | |
| 122 |
| I : volt | eqs = ''' | |
| 123 |
| ''' | dv/dt = (I(t) - v)/tau : volt | |
| 124 |
| group = NeuronGroup(1, model=eqs, | ''' | |
| 125 |
| reset=0*mV, threshold=15*mV) | group = NeuronGroup(1, model=eqs, | |
| 126 |
| group.I = TimedArray(linspace(0*mV, 20*mV, 100), | reset='v = 0*mV', | |
| 127 |
| dt=10*ms) | threshold='v > 15*mV') | |
| 128 |
| | | |
| 129 |
+-----------------------------------------------------------+----------------------------------------------------+ |
| 130 |
| .. code:: | .. code:: | |
| 131 |
| | | |
| 132 |
| # neuron-specific input | # neuron-specific input | |
| 133 |
| eqs = ''' | values = (linspace(0*mV, 20*mV, 100)[:, None] * | |
| 134 |
| dv/dt = (I - v)/tau : volt | linspace(0, 1, 5)) | |
| 135 |
| I : volt | I = TimedArray(values, dt=10*ms) | |
| 136 |
| ''' | eqs = ''' | |
| 137 |
| group = NeuronGroup(5, model=eqs, | dv/dt = (I(t, i) - v)/tau : volt | |
| 138 |
| reset=0*mV, threshold=15*mV) | ''' | |
| 139 |
| values = (linspace(0*mV, 20*mV, 100)[:, None] * | group = NeuronGroup(5, model=eqs, | |
| 140 |
| linspace(0, 1, 5)) | reset='v = 0*mV', | |
| 141 |
| group.I = TimedArray(values, dt=10*ms) | threshold='v > 15*mV') | |
| 142 |
| | | |
| 143 |
+-----------------------------------------------------------+----------------------------------------------------+ |