| 1 |
Neural models (Brian 1 --> 2 conversion) |
| 2 |
======================================== |
| 3 |
.. sidebar:: Brian 2 documentation |
| 4 |
|
| 5 |
For the main documentation about defining neural models, see the document |
| 6 |
:doc:`../../user/models`. |
| 7 |
|
| 8 |
.. contents:: |
| 9 |
:local: |
| 10 |
:depth: 1 |
| 11 |
|
| 12 |
The syntax for specifying neuron models in a `NeuronGroup` changed in several |
| 13 |
details. In general, a string-based syntax (that was already optional in Brian 1) |
| 14 |
consistently replaces the use of classes (e.g. ``VariableThreshold``) or |
| 15 |
guessing (e.g. which variable does ``threshold=50*mV`` check). |
| 16 |
|
| 17 |
Threshold and Reset |
| 18 |
------------------- |
| 19 |
String-based thresholds are now the only possible option and replace all the |
| 20 |
methods of defining threshold/reset in Brian 1: |
| 21 |
|
| 22 |
+----------------------------------------------------------------------------+----------------------------------------------------------------------------+ |
| 23 |
| Brian 1 | Brian 2 | |
| 24 |
+============================================================================+============================================================================+ |
| 25 |
+ .. code:: | .. code:: | |
| 26 |
+ | | |
| 27 |
+ group = NeuronGroup(N, 'dv/dt = -v / tau : volt', | group = NeuronGroup(N, 'dv/dt = -v / tau : volt', | |
| 28 |
+ threshold=-50*mV, | threshold='v > -50*mV', | |
| 29 |
+ reset=-70*mV) | reset='v = -70*mV') | |
| 30 |
+ | | |
| 31 |
+----------------------------------------------------------------------------+----------------------------------------------------------------------------+ |
| 32 |
+ .. code:: | .. code:: | |
| 33 |
+ | | |
| 34 |
+ group = NeuronGroup(N, 'dv/dt = -v / tau : volt', | group = NeuronGroup(N, 'dv/dt = -v / tau : volt', | |
| 35 |
+ threshold=Threshold(-50*mV, state='v'), | threshold='v > -50*mV', | |
| 36 |
+ reset=Reset(-70*mV, state='w')) | reset='v = -70*mV') | |
| 37 |
+ | | |
| 38 |
+----------------------------------------------------------------------------+----------------------------------------------------------------------------+ |
| 39 |
+ .. code:: | .. code:: | |
| 40 |
+ | | |
| 41 |
+ group = NeuronGroup(N, '''dv/dt = -v / tau : volt | group = NeuronGroup(N, '''dv/dt = -v / tau : volt | |
| 42 |
+ dvt/dt = -vt / tau : volt | dvt/dt = -vt / tau : volt | |
| 43 |
+ vr : volt''', | vr : volt''', | |
| 44 |
+ threshold=VariableThreshold(state='v', | threshold='v > vt', | |
| 45 |
+ threshold_state='vt'), | reset='v = vr') | |
| 46 |
+ reset=VariableThreshold(state='v', | | |
| 47 |
+ resetvaluestate='vr')) | | |
| 48 |
+ | | |
| 49 |
+----------------------------------------------------------------------------+----------------------------------------------------------------------------+ |
| 50 |
+ .. code:: | .. code:: | |
| 51 |
+ | | |
| 52 |
+ group = NeuronGroup(N, 'rate : Hz', | group = NeuronGroup(N, 'rate : Hz', | |
| 53 |
+ threshold=PoissonThreshold(state='rate')) | threshold='rand()<rate*dt') | |
| 54 |
+ | | |
| 55 |
+----------------------------------------------------------------------------+----------------------------------------------------------------------------+ |
| 56 |
|
| 57 |
There's no direct equivalent for the "functional threshold/reset" mechanism from |
| 58 |
Brian 1. In simple cases, they can be implemented using the general string |
| 59 |
expression/statement mechanism (note that in Brian 1, ``reset=myreset`` is |
| 60 |
equivalent to ``reset=FunReset(myreset)``): |
| 61 |
|
| 62 |
+------------------------------------------------------------------+-----------------------------------------------------------------+ |
| 63 |
| Brian 1 | Brian 2 | |
| 64 |
+==================================================================+=================================================================+ |
| 65 |
+ .. code:: | .. code:: | |
| 66 |
+ | | |
| 67 |
+ def myreset(P,spikes): | group = NeuronGroup(N, 'dv/dt = -v / tau : volt', | |
| 68 |
+ P.v_[spikes] = -70*mV+rand(len(spikes))*5*mV | threshold='v > -50*mV', | |
| 69 |
+ | reset='-70*mV + rand()*5*mV') | |
| 70 |
+ group = NeuronGroup(N, 'dv/dt = -v / tau : volt', | | |
| 71 |
+ threshold=-50*mV, | | |
| 72 |
+ reset=myreset) | | |
| 73 |
+ | | |
| 74 |
+------------------------------------------------------------------+-----------------------------------------------------------------+ |
| 75 |
+ .. code:: | .. code:: | |
| 76 |
+ | | |
| 77 |
+ def mythreshold(v): | group = NeuronGroup(N, 'dv/dt = -v / tau : volt', | |
| 78 |
+ return (v > -50*mV) & (rand(N) > 0.5) | threshold='v > -50*mV and rand() > 0.5', | |
| 79 |
+ | reset='v = -70*mV') | |
| 80 |
+ group = NeuronGroup(N, 'dv/dt = -v / tau : volt', | | |
| 81 |
+ threshold=SimpleFunThreshold(mythreshold, | | |
| 82 |
+ state='v'), | | |
| 83 |
+ reset=-70*mV) | | |
| 84 |
+ | | |
| 85 |
+------------------------------------------------------------------+-----------------------------------------------------------------+ |
| 86 |
|
| 87 |
For more complicated cases, you can use the general mechanism for |
| 88 |
:ref:`user_functions` that Brian 2 provides. The only caveat is that you'd have |
| 89 |
to provide an implementation of the function in the code generation target |
| 90 |
language which is by default C++ or Cython. However, in the default |
| 91 |
:ref:`runtime` mode, you can chose different code generation targets for |
| 92 |
different parts of your simulation. You can thus switch the code generation |
| 93 |
target for the threshold/reset mechanism to ``numpy`` while leaving the default |
| 94 |
target for the rest of the simulation in place. The details of this process and |
| 95 |
the correct definition of the functions (e.g. ``global_reset`` needs a "dummy" |
| 96 |
return value) are somewhat cumbersome at the moment and we plan to make them |
| 97 |
more straightforward in the future. Also note that if you use this kind of |
| 98 |
mechanism extensively, you'll lose all the performance advantage that Brian 2's |
| 99 |
code generation mechanism provides (in addition to not being able to use |
| 100 |
:ref:`cpp_standalone` mode at all). |
| 101 |
|
| 102 |
+-------------------------------------------------------------------------+-----------------------------------------------------------------+ |
| 103 |
| Brian 1 | Brian 2 | |
| 104 |
+=========================================================================+=================================================================+ |
| 105 |
+ .. code:: | .. code:: | |
| 106 |
+ | | |
| 107 |
+ def single_threshold(v): | @check_units(v=volt, result=bool) | |
| 108 |
+ # Only let a single neuron spike | def single_threshold(v): | |
| 109 |
+ crossed_threshold = np.nonzero(v > -50*mV)[0] | pass # ... (identical to Brian 1) | |
| 110 |
+ should_spike = np.zeros(len(P), dtype=np.bool) | | |
| 111 |
+ if len(crossed_threshold): | @check_units(spikes=1, result=1) | |
| 112 |
+ choose = np.random.randint(len(crossed_threshold)) | def global_reset(spikes): | |
| 113 |
+ should_spike[crossed_threshold[choose]] = True | # Reset everything | |
| 114 |
+ return should_spike | if len(spikes): | |
| 115 |
+ | neurons.v_[:] = -0.070 | |
| 116 |
+ def global_reset(P, spikes): | | |
| 117 |
+ # Reset everything | neurons = NeuronGroup(N, 'dv/dt = -v / tau : volt', | |
| 118 |
+ if len(spikes): | threshold='single_threshold(v)', | |
| 119 |
+ P.v_[:] = -70*mV | reset='dummy = global_reset(i)') | |
| 120 |
+ | # Set the code generation target for threshold/reset only: | |
| 121 |
+ neurons = NeuronGroup(N, 'dv/dt = -v / tau : volt', | neuron.thresholder['spike'].codeobj_class = NumpyCodeObject | |
| 122 |
+ threshold=SimpleFunThreshold(single_threshold, | neuron.resetter['spike'].codeobj_class = NumpyCodeObject | |
| 123 |
+ state='v'), | | |
| 124 |
+ reset=global_reset) | | |
| 125 |
+ | | |
| 126 |
+-------------------------------------------------------------------------+-----------------------------------------------------------------+ |
| 127 |
|
| 128 |
For an example how to translate ``EmpiricalThreshold``, see the section on |
| 129 |
"Refractoriness" below. |
| 130 |
|
| 131 |
Refractoriness |
| 132 |
-------------- |
| 133 |
For a detailed description of Brian 2's refractoriness mechanism see |
| 134 |
:doc:`../../user/refractoriness`. |
| 135 |
|
| 136 |
In Brian 1, refractoriness was tightly linked with the reset mechanism and |
| 137 |
some combinations of refractoriness and reset were not allowed. The standard |
| 138 |
refractory mechanism had two effects during the refractoriness: it prevented the |
| 139 |
refractory cell from spiking and it clamped a state variable (normally the |
| 140 |
membrane potential of the cell). In Brian 2, refractoriness is independent of |
| 141 |
reset and the two effects are specified separately: the ``refractory`` keyword |
| 142 |
specifies the time (or an expression evaluating to a time) during which the |
| 143 |
cell does not spike, and the ``(unless refractory)`` flag marks one or more |
| 144 |
variables to be clamped during the refractory period. To correctly translate |
| 145 |
the standard refractory mechanism from Brian 1, you'll therefore need to |
| 146 |
specify both: |
| 147 |
|
| 148 |
+---------------------------------------------------------+-----------------------------------------------------------------------------+ |
| 149 |
| Brian 1 | Brian 2 | |
| 150 |
+=========================================================+=============================================================================+ |
| 151 |
+ .. code:: | .. code:: | |
| 152 |
+ | | |
| 153 |
+ group = NeuronGroup(N, 'dv/dt = (I - v)/tau : volt', | group = NeuronGroup(N, 'dv/dt = (I - v)/tau : volt (unless refractory)', | |
| 154 |
+ threshold=-50*mV, | threshold='v > -50*mV', | |
| 155 |
+ reset=-70*mV, | reset='v = -70*mV', | |
| 156 |
+ refractory=3*ms) | refractory=3*ms) | |
| 157 |
+ | | |
| 158 |
+---------------------------------------------------------+-----------------------------------------------------------------------------+ |
| 159 |
|
| 160 |
More complex refractoriness mechanisms based on ``SimpleCustomRefractoriness`` |
| 161 |
and ``CustomRefractoriness`` can be translatated using string expressions or |
| 162 |
user-defined functions, see the remarks in the preceding section on "Threshold |
| 163 |
and Reset". |
| 164 |
|
| 165 |
Brian 2 no longer has an equivalent to the ``EmpiricalThreshold`` class (which |
| 166 |
detects at the first threshold crossing but ignores all following threshold |
| 167 |
crossings for a certain time after that). However, the standard refractoriness |
| 168 |
mechanism can be used to implement the same behaviour, since it does not |
| 169 |
reset/clamp any value if not explicitly asked for it (which would be fatal for |
| 170 |
Hodgkin-Huxley type models): |
| 171 |
|
| 172 |
+----------------------------------------------------------------------+----------------------------------------------------------------------+ |
| 173 |
| Brian 1 | Brian 2 | |
| 174 |
+======================================================================+======================================================================+ |
| 175 |
+ .. code:: | .. code:: | |
| 176 |
+ | | |
| 177 |
+ group = NeuronGroup(N,''' | group = NeuronGroup(N,''' | |
| 178 |
+ dv/dt = (I_L - I_Na - I_K + I)/Cm : volt | dv/dt = (I_L - I_Na - I_K + I)/Cm : volt | |
| 179 |
+ ...''', | ...''', | |
| 180 |
+ threshold=EmpiricalThreshold(threshold=20*mV, | threshold='v > -20*mV', | |
| 181 |
+ refractory=1*ms, | refractory=1*ms) | |
| 182 |
+ state='v')) | | |
| 183 |
+ | | |
| 184 |
+----------------------------------------------------------------------+----------------------------------------------------------------------+ |
| 185 |
|
| 186 |
Subgroups |
| 187 |
--------- |
| 188 |
The class `NeuronGroup` in Brian 2 does no longer provide a ``subgroup`` method, |
| 189 |
the only way to construct subgroups is therefore the slicing syntax (that works |
| 190 |
in the same way as in Brian 1): |
| 191 |
|
| 192 |
+-------------------------------------+-----------------------------------+ |
| 193 |
| Brian 1 | Brian 2 | |
| 194 |
+=====================================+===================================+ |
| 195 |
+ .. code:: | .. code:: | |
| 196 |
+ | | |
| 197 |
+ group = NeuronGroup(4000, ...) | group = NeuronGroup(4000, ...) | |
| 198 |
+ group_exc = group.subgroup(3200) | group_exc = group[:3200] | |
| 199 |
+ group_inh = group.subgroup(800) | group_inh = group[3200:] | |
| 200 |
+ | | |
| 201 |
+-------------------------------------+-----------------------------------+ |
| 202 |
|
| 203 |
Linked Variables |
| 204 |
---------------- |
| 205 |
For a description of Brian 2's mechanism to link variables between groups, see |
| 206 |
:ref:`linked_variables`. |
| 207 |
|
| 208 |
Linked variables need to be explicitly annotated with the ``(linked)`` flag in |
| 209 |
Brian 2: |
| 210 |
|
| 211 |
+----------------------------------------------------------+----------------------------------------------------------+ |
| 212 |
| Brian 1 | Brian 2 | |
| 213 |
+==========================================================+==========================================================+ |
| 214 |
+ .. code:: | .. code:: | |
| 215 |
+ | | |
| 216 |
+ group1 = NeuronGroup(N, | group1 = NeuronGroup(N, | |
| 217 |
+ 'dv/dt = -v / tau : volt') | 'dv/dt = -v / tau : volt') | |
| 218 |
+ group2 = NeuronGroup(N, | group2 = NeuronGroup(N, | |
| 219 |
+ '''dv/dt = (-v + w) / tau : volt | '''dv/dt = (-v + w) / tau : volt | |
| 220 |
+ w : volt''') | w : volt (linked)''') | |
| 221 |
+ group2.w = linked_var(group1, 'v') | group2.w = linked_var(group1, 'v') | |
| 222 |
+ | | |
| 223 |
+----------------------------------------------------------+----------------------------------------------------------+ |