| 1 |
Input stimuli |
| 2 |
============= |
| 3 |
.. sidebar:: For Brian 1 users |
| 4 |
|
| 5 |
See the document :doc:`../introduction/brian1_to_2/inputs` for details how |
| 6 |
to convert Brian 1 code. |
| 7 |
|
| 8 |
.. contents:: |
| 9 |
:local: |
| 10 |
:depth: 1 |
| 11 |
|
| 12 |
There are various ways of providing "external" input to a network. |
| 13 |
|
| 14 |
Poisson inputs |
| 15 |
-------------- |
| 16 |
For generating spikes according to a Poisson point process, `PoissonGroup` can |
| 17 |
be used, e.g.:: |
| 18 |
|
| 19 |
P = PoissonGroup(100, np.arange(100)*Hz + 10*Hz) |
| 20 |
G = NeuronGroup(100, 'dv/dt = -v / (10*ms) : 1') |
| 21 |
S = Synapses(P, G, on_pre='v+=0.1') |
| 22 |
S.connect(j='i') |
| 23 |
|
| 24 |
See `More on Poisson inputs`_ below for further information. |
| 25 |
|
| 26 |
For simulations where the individually generated spikes are just used as a |
| 27 |
source of input to a neuron, the `PoissonInput` class provides a more efficient |
| 28 |
alternative: see :ref:`poisson_input_class` below for details. |
| 29 |
|
| 30 |
Spike generation |
| 31 |
---------------- |
| 32 |
You can also generate an explicit list of spikes given via arrays using |
| 33 |
`SpikeGeneratorGroup`. This object behaves just like a `NeuronGroup` in that |
| 34 |
you can connect it to other groups via a `Synapses` object, but you specify |
| 35 |
three bits of information: ``N`` the number of neurons in the group; |
| 36 |
``indices`` an array of the indices of the neurons that will fire; and |
| 37 |
``times`` an array of the same length as ``indices`` with the times that the |
| 38 |
neurons will fire a spike. The ``indices`` and ``times`` arrays are matching, |
| 39 |
so for example ``indices=[0,2,1]`` and ``times=[1*ms,2*ms,3*ms]`` means that |
| 40 |
neuron 0 fires at time 1 ms, neuron 2 fires at 2 ms and neuron 1 fires at 3 ms. |
| 41 |
Example use:: |
| 42 |
|
| 43 |
indices = array([0, 2, 1]) |
| 44 |
times = array([1, 2, 3])*ms |
| 45 |
G = SpikeGeneratorGroup(3, indices, times) |
| 46 |
|
| 47 |
The spikes that will be generated by `SpikeGeneratorGroup` can be changed |
| 48 |
between runs with the |
| 49 |
`~brian2.input.spikegeneratorgroup.SpikeGeneratorGroup.set_spikes` method. This |
| 50 |
can be useful if the input to a system should depend on its previous output or |
| 51 |
when running multiple trials with different input:: |
| 52 |
|
| 53 |
inp = SpikeGeneratorGroup(N, indices, times) |
| 54 |
G = NeuronGroup(N, '...') |
| 55 |
feedforward = Synapses(inp, G, '...', on_pre='...') |
| 56 |
feedforward.connect(j='i') |
| 57 |
recurrent = Synapses(G, G, '...', on_pre='...') |
| 58 |
recurrent.connect('i!=j') |
| 59 |
spike_mon = SpikeMonitor(G) |
| 60 |
# ... |
| 61 |
run(runtime) |
| 62 |
# Replay the previous output of group G as input into the group |
| 63 |
inp.set_spikes(spike_mon.i, spike_mon.t + runtime) |
| 64 |
run(runtime) |
| 65 |
|
| 66 |
Explicit equations |
| 67 |
------------------ |
| 68 |
If the input can be explicitly expressed as a function of time (e.g. a |
| 69 |
sinusoidal input current), then its description can be directly included in |
| 70 |
the equations of the respective group:: |
| 71 |
|
| 72 |
G = NeuronGroup(100, '''dv/dt = (-v + I)/(10*ms) : 1 |
| 73 |
rates : Hz # each neuron's input has a different rate |
| 74 |
size : 1 # and a different amplitude |
| 75 |
I = size*sin(2*pi*rates*t) : 1''') |
| 76 |
G.rates = '10*Hz + i*Hz' |
| 77 |
G.size = '(100-i)/100. + 0.1' |
| 78 |
|
| 79 |
.. _timed_arrays: |
| 80 |
|
| 81 |
Timed arrays |
| 82 |
------------ |
| 83 |
If the time dependence of the input cannot be expressed in the equations in the |
| 84 |
way shown above, it is possible to create a `TimedArray`. This acts |
| 85 |
as a function of time where the values at given time points are given |
| 86 |
explicitly. This can be especially useful to describe non-continuous |
| 87 |
stimulation. For example, the following code defines a `TimedArray` where |
| 88 |
stimulus blocks consist of a constant current of random strength for 30ms, |
| 89 |
followed by no stimulus for 20ms. Note that in this particular example, |
| 90 |
numerical integration can use exact methods, since it can assume that the |
| 91 |
`TimedArray` is a constant function of time during a single integration time |
| 92 |
step. |
| 93 |
|
| 94 |
.. note:: |
| 95 |
The semantics of `TimedArray` changed slightly compared |
| 96 |
to Brian 1: for ``TimedArray([x1, x2, ...], dt=my_dt)``, the value ``x1`` will be |
| 97 |
returned for all ``0<=t<my_dt``, ``x2`` for ``my_dt<=t<2*my_dt`` etc., whereas |
| 98 |
Brian1 returned ``x1`` for ``0<=t<0.5*my_dt``, |
| 99 |
``x2`` for ``0.5*my_dt<=t<1.5*my_dt``, etc. |
| 100 |
|
| 101 |
:: |
| 102 |
|
| 103 |
stimulus = TimedArray(np.hstack([[c, c, c, 0, 0] |
| 104 |
for c in np.random.rand(1000)]), |
| 105 |
dt=10*ms) |
| 106 |
G = NeuronGroup(100, 'dv/dt = (-v + stimulus(t))/(10*ms) : 1', |
| 107 |
threshold='v>1', reset='v=0') |
| 108 |
G.v = '0.5*rand()' # different initial values for the neurons |
| 109 |
|
| 110 |
`TimedArray` can take a one-dimensional value array (as above) and therefore |
| 111 |
return the same value for all neurons or it can take a two-dimensional array |
| 112 |
with time as the first and (neuron/synapse/...-)index as the second dimension. |
| 113 |
|
| 114 |
In the following, this is used to implement shared noise between neurons, all |
| 115 |
the "even neurons" get the first noise instantiation, all the "odd neurons" get |
| 116 |
the second:: |
| 117 |
|
| 118 |
runtime = 1*second |
| 119 |
stimulus = TimedArray(np.random.rand(int(runtime/defaultclock.dt), 2), |
| 120 |
dt=defaultclock.dt) |
| 121 |
G = NeuronGroup(100, 'dv/dt = (-v + stimulus(t, i % 2))/(10*ms) : 1', |
| 122 |
threshold='v>1', reset='v=0') |
| 123 |
|
| 124 |
.. _regular_operations: |
| 125 |
|
| 126 |
Regular operations |
| 127 |
------------------ |
| 128 |
An alternative to specifying a stimulus in advance is to run explicitly |
| 129 |
specified code at certain points during a simulation. This can be |
| 130 |
achieved with :meth:`~brian2.groups.group.Group.run_regularly`. |
| 131 |
One can think of these statements as |
| 132 |
equivalent to reset statements but executed unconditionally (i.e. for all |
| 133 |
neurons) and possibly on a different clock than the rest of the group. The |
| 134 |
following code changes the stimulus strength of half of the neurons (randomly |
| 135 |
chosen) to a new random value every 50ms. Note that the statement uses logical |
| 136 |
expressions to have the values only updated for the chosen subset of neurons |
| 137 |
(where the newly introduced auxiliary variable ``change`` equals 1):: |
| 138 |
|
| 139 |
G = NeuronGroup(100, '''dv/dt = (-v + I)/(10*ms) : 1 |
| 140 |
I : 1 # one stimulus per neuron''') |
| 141 |
G.run_regularly('''change = int(rand() < 0.5) |
| 142 |
I = change*(rand()*2) + (1-change)*I''', |
| 143 |
dt=50*ms) |
| 144 |
|
| 145 |
Operations run at pre-defined times |
| 146 |
----------------------------------- |
| 147 |
Instead of using regular intervals, you can also execute code at pre-defined |
| 148 |
time points. The following code has the same effect as the code above, but uses |
| 149 |
:meth:`~brian2.groups.group.Group.run_at` to only switch to new values at time |
| 150 |
points 50ms, 100ms, and 500ms:: |
| 151 |
|
| 152 |
G = NeuronGroup(100, '''dv/dt = (-v + I)/(10*ms) : 1 |
| 153 |
I : 1 # one stimulus per neuron''') |
| 154 |
G.run_at('''change = int(rand() < 0.5) |
| 155 |
I = change*(rand()*2) + (1-change)*I''', |
| 156 |
times=[50, 100, 500]*ms) |
| 157 |
|
| 158 |
|
| 159 |
.. admonition:: The following topics are not essential for beginners. |
| 160 |
|
| 161 |
| |
| 162 |
|
| 163 |
More on Poisson inputs |
| 164 |
---------------------- |
| 165 |
|
| 166 |
Setting rates for Poisson inputs |
| 167 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 168 |
|
| 169 |
``PoissonGroup`` takes either a constant rate, an array of rates (one rate per |
| 170 |
neuron, as in the example above), or a string expression evaluating to a rate |
| 171 |
as an argument. |
| 172 |
|
| 173 |
If the given value for ``rates`` is a constant, then using |
| 174 |
``PoissonGroup(N, rates)`` is equivalent to:: |
| 175 |
|
| 176 |
NeuronGroup(N, 'rates : Hz', threshold='rand()<rates*dt') |
| 177 |
|
| 178 |
and setting the group's ``rates`` attribute. |
| 179 |
|
| 180 |
If ``rates`` is a string, then this is equivalent to:: |
| 181 |
|
| 182 |
NeuronGroup(N, 'rates = ... : Hz', threshold='rand()<rates*dt') |
| 183 |
|
| 184 |
with the respective expression for the rates. This expression will be evaluated |
| 185 |
at every time step and therefore allows the use of time-dependent rates, i.e. |
| 186 |
inhomogeneous Poisson processes. For example, the following code |
| 187 |
(see also `Timed arrays`_) uses a `TimedArray` to define the rates of a |
| 188 |
`PoissonGroup` as a function of time, resulting in five 100ms blocks of 100 Hz |
| 189 |
stimulation, followed by 100ms of silence:: |
| 190 |
|
| 191 |
stimulus = TimedArray(np.tile([100., 0.], 5)*Hz, dt=100.*ms) |
| 192 |
P = PoissonGroup(1, rates='stimulus(t)') |
| 193 |
|
| 194 |
Note that, as can be seen in its equivalent `NeuronGroup` formulation, a |
| 195 |
`PoissonGroup` does not work for high rates where more than one spike might |
| 196 |
fall into a single timestep. Use several units with lower rates in this case |
| 197 |
(e.g. use ``PoissonGroup(10, 1000*Hz)`` instead of |
| 198 |
``PoissonGroup(1, 10000*Hz)``). |
| 199 |
|
| 200 |
.. _poisson_input_class: |
| 201 |
|
| 202 |
Efficient Poisson inputs via PoissonInput |
| 203 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 204 |
|
| 205 |
For simulations where the `PoissonGroup` is just used as a source of input to a |
| 206 |
neuron (i.e., the individually generated spikes are not important, just their |
| 207 |
impact on the target cell), the `PoissonInput` class provides a more efficient |
| 208 |
alternative: instead of generating spikes, `PoissonInput` directly updates |
| 209 |
a target variable based on the sum of independent Poisson processes:: |
| 210 |
|
| 211 |
G = NeuronGroup(100, 'dv/dt = -v / (10*ms) : 1') |
| 212 |
P = PoissonInput(G, 'v', 100, 100*Hz, weight=0.1) |
| 213 |
|
| 214 |
Each input of the `PoissonInput` is connected to all the neurons of the target |
| 215 |
`NeuronGroup` but each neuron receives independent realizations of the Poisson |
| 216 |
spike trains. Note that the `PoissonInput` class is however more restrictive than |
| 217 |
`PoissonGroup`, it only allows for a constant rate across all neurons (but |
| 218 |
you can create several `PoissonInput` objects, targeting different subgroups). |
| 219 |
It internally uses `BinomialFunction` which will draw a random number each time |
| 220 |
step, either from a binomial distribution or from a normal distribution as an |
| 221 |
approximation to the binomial distribution if :math:`n p > 5 \wedge n (1 - p) > 5` |
| 222 |
, where :math:`n` is the number of inputs and :math:`p = dt \cdot rate` the |
| 223 |
spiking probability for a single input. |
| 224 |
|
| 225 |
.. _network_operation: |
| 226 |
|
| 227 |
Arbitrary Python code (network operations) |
| 228 |
------------------------------------------ |
| 229 |
If none of the above techniques is general enough to fulfill the requirements |
| 230 |
of a simulation, Brian allows you to write a `NetworkOperation`, an arbitrary |
| 231 |
Python function that is executed every time step (possible on a different clock |
| 232 |
than the rest of the simulation). This function can do arbitrary operations, |
| 233 |
use conditional statements etc. and it will be executed as it is (i.e. as pure |
| 234 |
Python code even if cython code generation is active). Note that one cannot use |
| 235 |
network operations in combination with the C++ standalone mode. Network |
| 236 |
operations are particularly useful when some condition or calculation depends |
| 237 |
on operations across neurons, which is currently not possible to express in |
| 238 |
abstract code. The following code switches input on for a randomly chosen single |
| 239 |
neuron every 50 ms:: |
| 240 |
|
| 241 |
G = NeuronGroup(10, '''dv/dt = (-v + active*I)/(10*ms) : 1 |
| 242 |
I = sin(2*pi*100*Hz*t) : 1 (shared) #single input |
| 243 |
active : 1 # will be set in the network operation''') |
| 244 |
@network_operation(dt=50*ms) |
| 245 |
def update_active(): |
| 246 |
index = np.random.randint(10) # index for the active neuron |
| 247 |
G.active_ = 0 # the underscore switches off unit checking |
| 248 |
G.active_[index] = 1 |
| 249 |
|
| 250 |
Note that the network operation (in the above example: ``update_active``) has |
| 251 |
to be included in the `Network` object if one is constructed explicitly. |
| 252 |
|
| 253 |
Only functions with zero or one arguments can be used as a `NetworkOperation`. |
| 254 |
If the function has one argument then it will be passed the current time ``t``:: |
| 255 |
|
| 256 |
@network_operation(dt=1*ms) |
| 257 |
def update_input(t): |
| 258 |
if t>50*ms and t<100*ms: |
| 259 |
pass # do something |
| 260 |
|
| 261 |
Note that this is preferable to accessing ``defaultclock.t`` from within the |
| 262 |
function -- if the network operation is not running on the `defaultclock` |
| 263 |
itself, then that value is not guaranteed to be correct. |
| 264 |
|
| 265 |
Instance methods can be used as network operations as well, however in this case |
| 266 |
they have to be constructed explicitly, the `network_operation` decorator |
| 267 |
cannot be used:: |
| 268 |
|
| 269 |
class Simulation(object): |
| 270 |
def __init__(self, data): |
| 271 |
self.data = data |
| 272 |
self.group = NeuronGroup(...) |
| 273 |
self.network_op = NetworkOperation(self.update_func, dt=10*ms) |
| 274 |
self.network = Network(self.group, self.network_op) |
| 275 |
|
| 276 |
def update_func(self): |
| 277 |
pass # do something |
| 278 |
|
| 279 |
def run(self, runtime): |
| 280 |
self.network.run(runtime) |