| 1 |
Synapses |
| 2 |
======== |
| 3 |
|
| 4 |
.. sidebar:: For Brian 1 users |
| 5 |
|
| 6 |
`Synapses` is now the only class for defining synaptic interactions, |
| 7 |
it replaces *Connection*, *STDP*, etc. See the document |
| 8 |
:doc:`../introduction/brian1_to_2/synapses` for details how to convert |
| 9 |
Brian 1 code. |
| 10 |
|
| 11 |
.. contents:: |
| 12 |
:local: |
| 13 |
:depth: 1 |
| 14 |
|
| 15 |
Defining synaptic models |
| 16 |
------------------------ |
| 17 |
|
| 18 |
The most simple synapse (adding a fixed amount to the target membrane potential |
| 19 |
on every spike) is described as follows:: |
| 20 |
|
| 21 |
w = 1*mV |
| 22 |
S = Synapses(P, Q, on_pre='v += w') |
| 23 |
|
| 24 |
This defines a set of synapses between `NeuronGroup` P and `NeuronGroup` Q. |
| 25 |
If the target group is not specified, it is identical to the source group by default. |
| 26 |
The ``on_pre`` keyword defines what happens when a presynaptic spike arrives at |
| 27 |
a synapse. In this case, the constant ``w`` is added to variable ``v``. |
| 28 |
Because ``v`` is not defined as a synaptic variable, it is assumed by default |
| 29 |
that it is a postsynaptic variable, defined in the target `NeuronGroup` Q. |
| 30 |
Note that this does not create synapses (see `Creating Synapses`_), only the |
| 31 |
synaptic models. |
| 32 |
|
| 33 |
To define more complex models, models can be described as string equations, |
| 34 |
similar to the models specified in `NeuronGroup`:: |
| 35 |
|
| 36 |
S = Synapses(P, Q, model='w : volt', on_pre='v += w') |
| 37 |
|
| 38 |
The above specifies a parameter ``w``, i.e. a synapse-specific weight. Note that |
| 39 |
to avoid confusion, synaptic variables cannot have the same name as a pre- |
| 40 |
or post-synaptic variables. |
| 41 |
|
| 42 |
Synapses can also specify code that should be executed whenever a postsynaptic |
| 43 |
spike occurs (keyword ``on_post``) and a fixed (pre-synaptic) delay for all |
| 44 |
synapses (keyword ``delay``). |
| 45 |
|
| 46 |
As shown above, variable names that are not referring to a synaptic variable |
| 47 |
are automatically understood to be post-synaptic variables. To explicitly |
| 48 |
specify that a variable should be from a pre- or post-synaptic neuron, append |
| 49 |
the suffix ``_pre`` or ``_post``. An alternative but equivalent formulation of |
| 50 |
the ``on_pre`` statement above would therefore be ``v_post += w``. |
| 51 |
|
| 52 |
.. _synapse_model_syntax: |
| 53 |
|
| 54 |
Model syntax |
| 55 |
~~~~~~~~~~~~ |
| 56 |
|
| 57 |
The model follows exactly the same syntax as for `NeuronGroup`. There can be parameters |
| 58 |
(e.g. synaptic variable ``w`` above), but there can also be named |
| 59 |
subexpressions and differential equations, describing the dynamics of synaptic |
| 60 |
variables. In all cases, synaptic variables are created, one value per synapse. |
| 61 |
|
| 62 |
Brian also automatically defines a number of synaptic variables that can be |
| 63 |
used in equations, ``on_pre`` and ``on_post`` statements, as well as when |
| 64 |
:ref:`assigning to other synaptic variables <accessing_synaptic_variables>`: |
| 65 |
|
| 66 |
``i`` |
| 67 |
The index of the pre-synaptic source of a synapse. |
| 68 |
|
| 69 |
``j`` |
| 70 |
The index of the post-synaptic target of a synapse. |
| 71 |
|
| 72 |
``N`` |
| 73 |
The total number of synapses. |
| 74 |
|
| 75 |
``N_incoming`` |
| 76 |
The total number of synapses connected to the post-synaptic target of a |
| 77 |
synapse. |
| 78 |
|
| 79 |
``N_outgoing`` |
| 80 |
The total number of synapses outgoing from the pre-synaptic source of a |
| 81 |
synapse. |
| 82 |
|
| 83 |
``lastupdate`` |
| 84 |
The last time this synapse has applied an ``on_pre`` or ``on_post`` |
| 85 |
statement. There is normally no need to refer to this variable explicitly, |
| 86 |
it is used to implement :ref:`event_driven_updates` (see below). It is only |
| 87 |
defined when event-driven equations are used. |
| 88 |
|
| 89 |
.. _event_driven_updates: |
| 90 |
|
| 91 |
Event-driven updates |
| 92 |
~~~~~~~~~~~~~~~~~~~~ |
| 93 |
By default, differential equations are integrated in a clock-driven fashion, as for a |
| 94 |
`NeuronGroup`. This is potentially very time consuming, because all synapses are updated at every |
| 95 |
timestep and Brian will therefore emit a warning. If you are sure about integrating the equations at |
| 96 |
every timestep (e.g. because you want to record the values continuously), then you should specify |
| 97 |
the flag ``(clock-driven)``, which will silence the warning. To ask Brian 2 to simulate differential |
| 98 |
equations in an event-driven fashion use the flag ``(event-driven)``. A typical example is pre- and |
| 99 |
postsynaptic traces in STDP:: |
| 100 |
|
| 101 |
model='''w:1 |
| 102 |
dApre/dt=-Apre/taupre : 1 (event-driven) |
| 103 |
dApost/dt=-Apost/taupost : 1 (event-driven)''' |
| 104 |
|
| 105 |
Here, Brian updates the value of ``Apre`` for a given synapse only when this synapse receives a spike, |
| 106 |
whether it is presynaptic or postsynaptic. More precisely, the variables are updated every time either |
| 107 |
the ``on_pre`` or ``on_post`` code is called for the synapse, so that the values are always up to date when |
| 108 |
these codes are executed. |
| 109 |
|
| 110 |
Automatic event-driven updates are only possible for a subset of equations, in particular for |
| 111 |
one-dimensional linear equations. These equations must also be independent of the other ones, |
| 112 |
that is, a differential equation that is not event-driven cannot |
| 113 |
depend on an event-driven equation (since the values are not continuously updated). |
| 114 |
In other cases, the user can write event-driven code explicitly in the update codes (see below). |
| 115 |
|
| 116 |
Pre and post codes |
| 117 |
~~~~~~~~~~~~~~~~~~ |
| 118 |
The ``on_pre`` code is executed at each synapse receiving a presynaptic spike. For example:: |
| 119 |
|
| 120 |
on_pre='v+=w' |
| 121 |
|
| 122 |
adds the value of synaptic variable ``w`` to postsynaptic variable ``v``. |
| 123 |
Any sort of code can be executed. For example, the following code defines |
| 124 |
stochastic synapses, with a synaptic weight ``w`` and transmission probability ``p``:: |
| 125 |
|
| 126 |
S=Synapses(neuron_input,neurons,model="""w : 1 |
| 127 |
p : 1""", |
| 128 |
on_pre="v+=w*(rand()<p)") |
| 129 |
|
| 130 |
The code means that ``w`` is added to ``v`` with probability ``p``. |
| 131 |
The code may also include multiple lines. |
| 132 |
|
| 133 |
Similarly, the ``on_post`` code is executed at each synapse where the postsynaptic neuron |
| 134 |
has fired a spike. |
| 135 |
|
| 136 |
.. _creating_synapses: |
| 137 |
|
| 138 |
Creating synapses |
| 139 |
----------------- |
| 140 |
Creating a `Synapses` instance does not create synapses, it only specifies their dynamics. |
| 141 |
The following command creates a synapse between neuron ``5`` in the source group and |
| 142 |
neuron ``10`` in the target group:: |
| 143 |
|
| 144 |
S.connect(i=5, j=10) |
| 145 |
|
| 146 |
Multiple synaptic connections can be created in a single statement:: |
| 147 |
|
| 148 |
S.connect() |
| 149 |
S.connect(i=[1, 2], j=[3, 4]) |
| 150 |
S.connect(i=numpy.arange(10), j=1) |
| 151 |
|
| 152 |
The first statement connects all neuron pairs. |
| 153 |
The second statement creates synapses between neurons 1 and 3, and between neurons 2 and 4. |
| 154 |
The third statement creates synapses between the first ten neurons in the source group and neuron 1 |
| 155 |
in the target group. |
| 156 |
|
| 157 |
Conditional |
| 158 |
~~~~~~~~~~~ |
| 159 |
|
| 160 |
One can also create synapses by giving (as a string) the condition for a pair |
| 161 |
of neurons i and j to be connected by a synapse, e.g. you could |
| 162 |
connect neurons that are not very far apart with:: |
| 163 |
|
| 164 |
S.connect(condition='abs(i-j)<=5') |
| 165 |
|
| 166 |
|
| 167 |
The string expressions can also refer to pre- or postsynaptic variables. This |
| 168 |
can be useful for example for spatial connectivity: assuming that the pre- and |
| 169 |
postsynaptic groups have user-defined parameters ``x`` and ``y``, storing their |
| 170 |
location, the following statement connects all cells in a 250 um radius:: |
| 171 |
|
| 172 |
S.connect(condition='sqrt((x_pre-x_post)**2 + (y_pre-y_post)**2) < 250*umeter') |
| 173 |
|
| 174 |
Probabilistic |
| 175 |
~~~~~~~~~~~~~ |
| 176 |
|
| 177 |
Synapse creation can also be probabilistic by providing a ``p`` argument, |
| 178 |
providing the connection probability for each pair of synapses:: |
| 179 |
|
| 180 |
S.connect(p=0.1) |
| 181 |
|
| 182 |
This connects all neuron pairs with a probability of 10%. Probabilities can |
| 183 |
also be given as expressions, for example to implement a connection probability |
| 184 |
that depends on distance:: |
| 185 |
|
| 186 |
S.connect(condition='i != j', |
| 187 |
p='p_max*exp(-(x_pre-x_post)**2+(y_pre-y_post)**2 / (2*(125*umeter)**2))') |
| 188 |
|
| 189 |
If this statement is applied to a `Synapses` object that connects a group to |
| 190 |
itself, it prevents self-connections (``i != j``) and connects cells with a |
| 191 |
probability that is modulated according to a 2-dimensional Gaussian of the |
| 192 |
distance between the cells computed from the user-defined parameters ``x`` |
| 193 |
and ``y``, storing their location. |
| 194 |
|
| 195 |
One-to-one |
| 196 |
~~~~~~~~~~ |
| 197 |
|
| 198 |
You can specify a mapping from i to any function f(i), e.g. the |
| 199 |
simplest way to give a 1-to-1 connection would be:: |
| 200 |
|
| 201 |
S.connect(j='i') |
| 202 |
|
| 203 |
This mapping can also use a restricting condition with ``if``, e.g. to connect |
| 204 |
neurons 0, 2, 4, 6, ... to neurons 0, 1, 2, 3, ... you could write:: |
| 205 |
|
| 206 |
S.connect(j='int(i/2) if i % 2 == 0') |
| 207 |
|
| 208 |
The connections above describe the target indices ``j`` as a function of the source indices ``i``. |
| 209 |
You can also apply the syntax in the other direction, i.e. describe source indices ``i`` as a function |
| 210 |
of target indices ``j``. For a 1-to-1 connection, this does not change anything in most cases:: |
| 211 |
|
| 212 |
S.connect(i='j') |
| 213 |
|
| 214 |
Note that there is a subtle difference between the two descriptions if the two groups do not have the same size: |
| 215 |
if the source group has fewer neurons than the target group, then using `j='i'` is possible (there is a target |
| 216 |
neuron for each source neuron), but `i='j'` would raise an error; the opposite is true if the source group is |
| 217 |
bigger than the target group. |
| 218 |
|
| 219 |
The second example from above (neurons 0, 2, 4, ... to neurons 0, 1, 2, ...) can be adapted for the other |
| 220 |
direction, as well, and is possibly more intuitive in this case:: |
| 221 |
|
| 222 |
S.connect(i='j*2') |
| 223 |
|
| 224 |
.. _accessing_synaptic_variables: |
| 225 |
|
| 226 |
Accessing synaptic variables |
| 227 |
---------------------------- |
| 228 |
Synaptic variables can be accessed in a similar way as `NeuronGroup` variables. They can be indexed |
| 229 |
with two indexes, corresponding to the indexes of pre and postsynaptic neurons, or with string expressions (referring |
| 230 |
to ``i`` and ``j`` as the pre-/post-synaptic indices, or to other state variables of the synapse or the connected neurons). |
| 231 |
Note that setting a synaptic variable always refers to the synapses that *currently exist*, i.e. you have to set them |
| 232 |
*after* the relevant `Synapses.connect` call. |
| 233 |
|
| 234 |
Here are a few examples:: |
| 235 |
|
| 236 |
S.w[2, 5] = 1*nS |
| 237 |
S.w[1, :] = 2*nS |
| 238 |
S.w = 1*nS # all synapses assigned |
| 239 |
S.w[2, 3] = (1*nS, 2*nS) |
| 240 |
S.w[group1, group2] = "(1+cos(i-j))*2*nS" |
| 241 |
S.w[:, :] = 'rand()*nS' |
| 242 |
S.w['abs(x_pre-x_post) < 250*umetre'] = 1*nS |
| 243 |
|
| 244 |
Assignments can also refer to :ref:`pre-defined variables <synapse_model_syntax>`, |
| 245 |
e.g. to normalize synaptic weights. For example, after the following assignment |
| 246 |
the sum of weights of all synapses that a neuron receives is identical to 1, |
| 247 |
regardless of the number of synapses it receives:: |
| 248 |
|
| 249 |
syn.w = '1.0/N_incoming' |
| 250 |
|
| 251 |
Note that it is also possible to index synaptic variables with a single index |
| 252 |
(integer, slice, or array), but in this case synaptic indices have to be |
| 253 |
provided. |
| 254 |
|
| 255 |
The ``N_incoming`` and ``N_outgoing`` variables give access to the |
| 256 |
total number of incoming/outgoing synapses for a neuron, but this access is given |
| 257 |
for each *synapse*. This is necessary to apply it to individual synapses as in |
| 258 |
the statement to normalize synaptic weights mentioned above. To access these |
| 259 |
values per *neuron* instead, `~.Synapses.N_incoming_post` and |
| 260 |
`~.Synapses.N_outgoing_pre` can be used. Note that synaptic equations or |
| 261 |
``on_pre``/``on_post`` statements should always refer to ``N_incoming`` and |
| 262 |
``N_outgoing`` without ``pre``/``post`` suffix. |
| 263 |
|
| 264 |
Here's a little example illustrating the use of these variables:: |
| 265 |
|
| 266 |
>>> group1 = NeuronGroup(3, '') |
| 267 |
>>> group2 = NeuronGroup(3, '') |
| 268 |
>>> syn = Synapses(group1, group2) |
| 269 |
>>> syn.connect(i=[0, 0, 1, 2], j=[1, 2, 2, 2]) |
| 270 |
>>> print(syn.N_outgoing_pre) # for each presynaptic neuron |
| 271 |
[2 1 1] |
| 272 |
>>> print(syn.N_outgoing[:]) # same numbers, but indexed by synapse |
| 273 |
[2 2 1 1] |
| 274 |
>>> print(syn.N_incoming_post) |
| 275 |
[0 1 3] |
| 276 |
>>> print(syn.N_incoming[:]) |
| 277 |
[1 3 3 3] |
| 278 |
|
| 279 |
Note that `~.Synapses.N_incoming_post` and `~.Synapses.N_outgoing_pre` can contain zeros for neurons |
| 280 |
that do not have any incoming respectively outgoing synapses. In contrast, `~.Synapses.N_incoming` |
| 281 |
and `~.Synapses.N_outgoing` will never contain zeros, because unconnected neurons are not represented |
| 282 |
in the list of synapses. |
| 283 |
|
| 284 |
Delays |
| 285 |
------ |
| 286 |
There is a special synaptic variable that is automatically created: ``delay``. It is the propagation delay |
| 287 |
from the presynaptic neuron to the synapse, i.e., the presynaptic delay. This |
| 288 |
is just a convenience syntax for accessing the delay stored in the presynaptic |
| 289 |
pathway: ``pre.delay``. When there is a postsynaptic code (keyword ``post``), |
| 290 |
the delay of the postsynaptic pathway can be accessed as ``post.delay``. |
| 291 |
|
| 292 |
The delay variable(s) can be set and accessed in the same way as other synaptic |
| 293 |
variables. The same semantics as for other synaptic variables apply, which means |
| 294 |
in particular that the delay is only set for the synapses that have been already |
| 295 |
created with `Synapses.connect`. If you want to set a global delay for all |
| 296 |
synapses of a `Synapses` object, you can directly specify that delay as part |
| 297 |
of the `Synapses` initializer:: |
| 298 |
|
| 299 |
synapses = Synapses(sources, targets, '...', on_pre='...', delay=1*ms) |
| 300 |
|
| 301 |
When you use this syntax, you can still change the delay afterwards by setting |
| 302 |
``synapses.delay``, but you can only set it to another scalar value. If you need |
| 303 |
different delays across synapses, do not use this syntax but instead set the |
| 304 |
delay variable as any other synaptic variable (see above). |
| 305 |
|
| 306 |
Monitoring synaptic variables |
| 307 |
----------------------------- |
| 308 |
A `StateMonitor` object can be used to monitor synaptic variables. For example, the following statement |
| 309 |
creates a monitor for variable ``w`` for the synapses 0 and 1:: |
| 310 |
|
| 311 |
M = StateMonitor(S, 'w', record=[0,1]) |
| 312 |
|
| 313 |
Note that these are *synapse* indices, not neuron indices. More convenient is |
| 314 |
to directly index the `Synapses` object, Brian will automatically calculate the |
| 315 |
indices for you in this case:: |
| 316 |
|
| 317 |
M = StateMonitor(S, 'w', record=S[0, :]) # all synapses originating from neuron 0 |
| 318 |
M = StateMonitor(S, 'w', record=S['i!=j']) # all synapses excluding autapses |
| 319 |
M = StateMonitor(S, 'w', record=S['w>0']) # all synapses with non-zero weights (at this time) |
| 320 |
|
| 321 |
You can also record a synaptic variable for all synapses by passing ``record=True``. |
| 322 |
|
| 323 |
The recorded traces can then be accessed in the usual way, again with the |
| 324 |
possibility to index the `Synapses` object:: |
| 325 |
|
| 326 |
plot(M.t / ms, M[S[0]].w / nS) # first synapse |
| 327 |
plot(M.t / ms, M[S[0, :]].w / nS) # all synapses originating from neuron 0 |
| 328 |
plot(M.t / ms, M[S['w>0*nS']].w / nS) # all synapses with non-zero weights (at this time) |
| 329 |
|
| 330 |
Note (for users of Brian's advanced standalone mode only): |
| 331 |
the use of the `Synapses` object for indexing and ``record=True`` only |
| 332 |
work in the default runtime modes. In standalone mode (see :ref:`cpp_standalone`), |
| 333 |
the synapses have not yet been created at this point, so Brian cannot calculate |
| 334 |
the indices. |
| 335 |
|
| 336 |
.. admonition:: The following topics are not essential for beginners. |
| 337 |
|
| 338 |
| |
| 339 |
|
| 340 |
Synaptic connection/weight matrices |
| 341 |
----------------------------------- |
| 342 |
|
| 343 |
Brian does not directly support specifying synapses by using a |
| 344 |
matrix, you always have to use a "sparse" format, where each |
| 345 |
connection is defined by its source and target indices. However, |
| 346 |
you can easily convert between the two formats. Assuming you have |
| 347 |
a connection matrix :math:`C` of size :math:`N \times M`, where |
| 348 |
:math:`N` is the number of presynaptic cells, and :math:`M` the |
| 349 |
number of postsynaptic cells, with each entry being 1 for a |
| 350 |
connection, and 0 otherwise. You can convert this matrix to arrays of |
| 351 |
source and target indices, which you can then provide to Brian's |
| 352 |
`~.Synapses.connect` function:: |
| 353 |
|
| 354 |
C = ... # The connection matrix as a numpy array of 0's and 1's |
| 355 |
sources, targets = C.nonzero() |
| 356 |
synapses = Synapses(...) |
| 357 |
synapses.connect(i=sources, j=targets) |
| 358 |
|
| 359 |
Similarly, you can transform the flat array of values stored in a |
| 360 |
synapse into a matrix form. For example, to get a matrix with all |
| 361 |
the weight values ``w``, with ``NaN`` values where no synapse |
| 362 |
exists:: |
| 363 |
|
| 364 |
synapses = Synapses(source_group, target_group, |
| 365 |
'''... |
| 366 |
w : 1 # synaptic weight''', ...) |
| 367 |
# ... |
| 368 |
# Run e.g. a simulation with plasticity that changes the weights |
| 369 |
run(...) |
| 370 |
# Create a matrix to store the weights and fill it with NaN |
| 371 |
W = np.full((len(source_group), len(target_group)), np.nan) |
| 372 |
# Insert the values from the Synapses object |
| 373 |
W[synapses.i[:], synapses.j[:]] = synapses.w[:] |
| 374 |
|
| 375 |
You can also set synapses given a fully connected weight matrix |
| 376 |
(as a 2D ``numpy`` array ``W``):: |
| 377 |
|
| 378 |
synapses.w[:] = W.flatten() |
| 379 |
|
| 380 |
This works because the internal ordering of synapses is exactly the |
| 381 |
same as for a flattened matrix. |
| 382 |
|
| 383 |
.. _generator_syntax: |
| 384 |
|
| 385 |
Creating synapses with the generator syntax |
| 386 |
------------------------------------------- |
| 387 |
|
| 388 |
The most general way of specifying a connection is using the |
| 389 |
generator syntax, e.g. to connect neuron i to all neurons j with |
| 390 |
0<=j<=i:: |
| 391 |
|
| 392 |
S.connect(j='k for k in range(0, i+1)') |
| 393 |
|
| 394 |
There are several parts to this syntax. The general form is:: |
| 395 |
|
| 396 |
j='EXPR for VAR in RANGE if COND' |
| 397 |
|
| 398 |
or:: |
| 399 |
|
| 400 |
i='EXPR for VAR in RANGE if COND' |
| 401 |
|
| 402 |
Here ``EXPR`` can be any integer-valued expression. VAR is the name |
| 403 |
of the iteration variable (any name you like can be specified |
| 404 |
here). The ``if COND`` part is optional and lets you give an |
| 405 |
additional condition that has to be true for the synapse to be |
| 406 |
created. Finally, ``RANGE`` can be either: |
| 407 |
|
| 408 |
1. a Python ``range``, e.g. ``range(N)`` is the integers from |
| 409 |
0 to N-1, ``range(A, B)`` is the integers from A to B-1, |
| 410 |
``range(low, high, step)`` is the integers from ``low`` to |
| 411 |
``high-1`` with steps of size ``step``; |
| 412 |
2. a random sample ``sample(N, p=0.1)`` gives a |
| 413 |
random sample of integers from 0 to N-1 with 10% probability |
| 414 |
of each integer appearing in the sample. This can have extra |
| 415 |
arguments like range, e.g. ``sample(low, high, step, p=0.1)`` |
| 416 |
will give each integer in ``range(low, high, step)`` with |
| 417 |
probability 10%; |
| 418 |
3. a random sample ``sample(N, size=10)`` with a fixed size, |
| 419 |
in this example 10 values chosen (without replacement) from |
| 420 |
the integers from 0 to N-1. As for the random sample based on |
| 421 |
a probability, the ``sample`` expression can take additional |
| 422 |
arguments to sample from a restricted range. |
| 423 |
|
| 424 |
If you try to create an invalid synapse (i.e. connecting |
| 425 |
neurons that are outside the correct range) then you will get |
| 426 |
an error, e.g. you might like to try to do this to connect |
| 427 |
each neuron to its neighbours:: |
| 428 |
|
| 429 |
S.connect(j='i+(-1)**k for k in range(2)') |
| 430 |
|
| 431 |
However this won't work at for ``i=0`` it gives ``j=-1`` which |
| 432 |
is invalid. There is an option to just skip any synapses |
| 433 |
that are outside the valid range:: |
| 434 |
|
| 435 |
S.connect(j='i+(-1)**k for k in range(2)', skip_if_invalid=True) |
| 436 |
|
| 437 |
You can also use this argument to deal with random samples of |
| 438 |
incorrect size, i.e. a negative size or a size bigger than the |
| 439 |
total population size. With ``skip_if_invalid=True``, no error will |
| 440 |
be raised and a size of 0 or the population size will be used. |
| 441 |
|
| 442 |
.. _summed_variables: |
| 443 |
|
| 444 |
Summed variables |
| 445 |
---------------- |
| 446 |
In many cases, the postsynaptic neuron has a variable that represents a sum of variables over all |
| 447 |
its synapses. This is called a "summed variable". An example is nonlinear synapses (e.g. NMDA):: |
| 448 |
|
| 449 |
neurons = NeuronGroup(1, model='''dv/dt=(gtot-v)/(10*ms) : 1 |
| 450 |
gtot : 1''') |
| 451 |
S = Synapses(neuron_input, neurons, |
| 452 |
model='''dg/dt=-a*g+b*x*(1-g) : 1 |
| 453 |
gtot_post = g : 1 (summed) |
| 454 |
dx/dt=-c*x : 1 |
| 455 |
w : 1 # synaptic weight''', on_pre='x+=w') |
| 456 |
|
| 457 |
Here, each synapse has a conductance ``g`` with nonlinear dynamics. The neuron's total conductance |
| 458 |
is ``gtot``. The line stating ``gtot_post = g : 1 (summed)`` specifies the link |
| 459 |
between the two: ``gtot`` in the postsynaptic group is the summer over all |
| 460 |
variables ``g`` of the corresponding synapses. What happens during the |
| 461 |
simulation is that at each time step, presynaptic conductances are summed for each neuron and the |
| 462 |
result is copied to the variable ``gtot``. Another example is gap junctions:: |
| 463 |
|
| 464 |
neurons = NeuronGroup(N, model='''dv/dt=(v0-v+Igap)/tau : 1 |
| 465 |
Igap : 1''') |
| 466 |
S=Synapses(neurons,model='''w:1 # gap junction conductance |
| 467 |
Igap_post = w*(v_pre-v_post): 1 (summed)''') |
| 468 |
|
| 469 |
Here, ``Igap`` is the total gap junction current received by the postsynaptic neuron. |
| 470 |
|
| 471 |
Note that you cannot target the same post-synaptic variable from more than one |
| 472 |
`Synapses` object. To work around this restriction, use multiple post-synaptic |
| 473 |
variables that ar then summed up:: |
| 474 |
|
| 475 |
neurons = NeuronGroup(1, model='''dv/dt=(gtot-v)/(10*ms) : 1 |
| 476 |
gtot = gtot1 + gtot2: 1 |
| 477 |
gtot1 : 1 |
| 478 |
gtot2 : 1''') |
| 479 |
S1 = Synapses(neuron_input, neurons, |
| 480 |
model='''dg/dt=-a1*g+b1*x*(1-g) : 1 |
| 481 |
gtot1_post = g : 1 (summed) |
| 482 |
dx/dt=-c1*x : 1 |
| 483 |
w : 1 # synaptic weight |
| 484 |
''', on_pre='x+=w') |
| 485 |
S2 = Synapses(neuron_input, neurons, |
| 486 |
model='''dg/dt=-a2*g+b2*x*(1-g) : 1 |
| 487 |
gtot2_post = g : 1 (summed) |
| 488 |
dx/dt=-c2*x : 1 |
| 489 |
w : 1 # synaptic weight |
| 490 |
''', on_pre='x+=w') |
| 491 |
|
| 492 |
Creating multi-synapses |
| 493 |
----------------------- |
| 494 |
|
| 495 |
It is also possible to create several synapses for a given pair of neurons:: |
| 496 |
|
| 497 |
S.connect(i=numpy.arange(10), j=1, n=3) |
| 498 |
|
| 499 |
This is useful for example if one wants to have multiple synapses with different delays. To |
| 500 |
distinguish multiple variables connecting the same pair of neurons in synaptic expressions and |
| 501 |
statements, you can create a variable storing the synapse index with the ``multisynaptic_index`` |
| 502 |
keyword:: |
| 503 |
|
| 504 |
syn = Synapses(source_group, target_group, model='w : 1', on_pre='v += w', |
| 505 |
multisynaptic_index='synapse_number') |
| 506 |
syn.connect(i=numpy.arange(10), j=1, n=3) |
| 507 |
syn.delay = '1*ms + synapse_number*2*ms' |
| 508 |
|
| 509 |
This index can then be used to set/get synapse-specific values:: |
| 510 |
|
| 511 |
S.delay = '(synapse_number + 1)*ms)' # Set delays between 1 and 10ms |
| 512 |
S.w['synapse_number<5'] = 0.5 |
| 513 |
S.w['synapse_number>=5'] = 1 |
| 514 |
|
| 515 |
It also enables three-dimensional indexing, the following statement has the same effect as the last one above:: |
| 516 |
|
| 517 |
S.w[:, :, 5:] = 1 |
| 518 |
|
| 519 |
Multiple pathways |
| 520 |
----------------- |
| 521 |
|
| 522 |
It is possible to have multiple pathways with different update codes from the same presynaptic neuron group. |
| 523 |
This may be interesting in cases when different operations must be applied at different times for the same |
| 524 |
presynaptic spike, e.g. for a STDP rule that shifted in time. To do this, specify a dictionary of pathway names and codes:: |
| 525 |
|
| 526 |
on_pre={'pre_transmission': 'ge+=w', |
| 527 |
'pre_plasticity': '''w=clip(w+Apost,0,inf) |
| 528 |
Apre+=dApre'''} |
| 529 |
|
| 530 |
This creates two pathways with the given names (in fact, specifying ``on_pre=code`` |
| 531 |
is just a shorter syntax for ``on_pre={'pre': code}``) through which the delay |
| 532 |
variables can be accessed. |
| 533 |
The following statement, for example, sets the delay of the synapse between the first neurons |
| 534 |
of the source and target groups in the ``pre_plasticity`` pathway:: |
| 535 |
|
| 536 |
S.pre_plasticity.delay[0,0] = 3*ms |
| 537 |
|
| 538 |
As mentioned above, ``pre`` pathways are generally executed before ``post`` |
| 539 |
pathways. The order of execution of several ``pre`` (or ``post``) pathways with the |
| 540 |
same delay is however arbitrary, and simply based on the alphabetical ordering of their names |
| 541 |
(i.e. ``pre_plasticity`` will be executed before ``pre_transmission``). To |
| 542 |
explicitly specify the order, set the ``order`` attribute of the pathway, e.g.:: |
| 543 |
|
| 544 |
S.pre_transmission.order = -2 |
| 545 |
|
| 546 |
will make sure that the ``pre_transmission`` code is executed before the |
| 547 |
``pre_plasticity`` code in each time step. |
| 548 |
|
| 549 |
Multiple pathways can also be useful for abstract models of synaptic currents, e.g. |
| 550 |
modelling them as rectangular currents:: |
| 551 |
|
| 552 |
synapses = Synapses(..., |
| 553 |
on_pre={'up': 'I_syn_post += 1*nA', |
| 554 |
'down': 'I_syn_post -= 1*nA'}, |
| 555 |
delay={'up': 0*ms, 'down': 5*ms} # 5ms-wide rectangular current |
| 556 |
) |
| 557 |
|
| 558 |
Numerical integration |
| 559 |
--------------------- |
| 560 |
|
| 561 |
Differential equation flags |
| 562 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 563 |
|
| 564 |
For the integration of differential equations, one can use the same keywords as |
| 565 |
for `NeuronGroup`. |
| 566 |
|
| 567 |
.. note:: Declaring a subexpression as ``(constant over dt)`` means that it will |
| 568 |
be evaluated each timestep for all synapses, potentially a very costly |
| 569 |
operation. |
| 570 |
|
| 571 |
Explicit event-driven updates |
| 572 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 573 |
|
| 574 |
As mentioned above, it is possible to write event-driven update code for the synaptic variables. |
| 575 |
This can also be done manually, by defining the variable ``lastupdate`` and |
| 576 |
referring to the predefined variable ``t`` (current time). |
| 577 |
Here's an example for short-term plasticity:: |
| 578 |
|
| 579 |
S=Synapses(neuron_input,neuron, |
| 580 |
model='''x : 1 |
| 581 |
u : 1 |
| 582 |
w : 1 |
| 583 |
lastupdate : second''', |
| 584 |
on_pre='''u=U+(u-U)*exp(-(t-lastupdate)/tauf) |
| 585 |
x=1+(x-1)*exp(-(t-lastupdate)/taud) |
| 586 |
i+=w*u*x |
| 587 |
x*=(1-u) |
| 588 |
u+=U*(1-u) |
| 589 |
lastupdate = t''') |
| 590 |
|
| 591 |
By default, the ``pre`` pathway is executed before the ``post`` pathway (both |
| 592 |
are executed in the ``'synapses'`` scheduling slot, but the ``pre`` pathway has |
| 593 |
the ``order`` attribute -1, wheras the ``post`` pathway has ``order`` 1. See |
| 594 |
:ref:`scheduling` for more details). |
| 595 |
|
| 596 |
Note that using the automatic ``event-driven`` approach from above is usually preferable, |
| 597 |
see :doc:`../examples/frompapers.Stimberg_et_al_2018.example_1_COBA` for an ``event-driven`` |
| 598 |
implementation of short-term plasticity. |
| 599 |
|
| 600 |
Technical notes |
| 601 |
--------------- |
| 602 |
|
| 603 |
How connection arguments are interpreted |
| 604 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 605 |
|
| 606 |
If conditions for connecting neurons are combined with both the ``n`` (number of |
| 607 |
synapses to create) and the ``p`` (probability of a synapse) keywords, they are |
| 608 |
interpreted in the following way: |
| 609 |
|
| 610 |
| For every pair i, j: |
| 611 |
| if condition(i, j) is fulfilled: |
| 612 |
| Evaluate p(i, j) |
| 613 |
| If uniform random number between 0 and 1 < p(i, j): |
| 614 |
| Create n(i, j) synapses for (i, j) |
| 615 |
|
| 616 |
With the generator syntax ``j='EXPR for VAR in RANGE if COND'`` (where the |
| 617 |
``RANGE`` can be a full range or a random sample as described above), the interpretation |
| 618 |
is: |
| 619 |
|
| 620 |
| For every i: |
| 621 |
| for every VAR in RANGE: |
| 622 |
| j = EXPR |
| 623 |
| if COND: |
| 624 |
| Create n(i, j) synapses for (i, j) |
| 625 |
|
| 626 |
Note that the arguments in ``RANGE`` can only depend on ``i`` and the values of |
| 627 |
presynaptic variables. Similarly, the expression for ``j``, ``EXPR`` can depend |
| 628 |
on ``i``, presynaptic variables, and on the iteration variable ``VAR``. The |
| 629 |
condition ``COND`` can depend on anything (presynaptic and postsynaptic variables). |
| 630 |
|
| 631 |
The generator syntax expressing ``i`` as a function of ``j`` is interpreted |
| 632 |
in the same way: |
| 633 |
|
| 634 |
| For every j: |
| 635 |
| for every VAR in RANGE: |
| 636 |
| i = EXPR |
| 637 |
| if COND: |
| 638 |
| Create n(i, j) synapses for (i, j) |
| 639 |
|
| 640 |
Here, ``RANGE`` can only depend on ``j`` and postsynaptic variables, and ``EXPR`` |
| 641 |
can only depend on ``j``, postsynaptic variables, and on the iteration variable |
| 642 |
``VAR``. |
| 643 |
|
| 644 |
With the 1-to-1 mapping syntax ``j='EXPR'`` the interpretation is: |
| 645 |
|
| 646 |
| For every i: |
| 647 |
| j = EXPR |
| 648 |
| Create n(i, j) synapses for (i, j) |
| 649 |
|
| 650 |
And finally, ``i='EXPR'`` is interpreted as: |
| 651 |
|
| 652 |
| For every j: |
| 653 |
| i = EXPR |
| 654 |
| Create n(i, j) synapses for (i, j) |
| 655 |
|
| 656 |
Efficiency considerations |
| 657 |
~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 658 |
|
| 659 |
If you are connecting a single pair of neurons, the direct form ``connect(i=5, j=10)`` |
| 660 |
is the most efficient. However, if you are connecting a number of neurons, it |
| 661 |
will usually be more efficient to construct an array of ``i`` and ``j`` values |
| 662 |
and have a single ``connect(i=i, j=j)`` call. |
| 663 |
|
| 664 |
For large connections, you |
| 665 |
should use one of the string based syntaxes where possible as this will |
| 666 |
generate compiled low-level code that will be typically much faster than |
| 667 |
equivalent Python code. |
| 668 |
|
| 669 |
If you are expecting a majority of pairs of neurons to be connected, then using the |
| 670 |
condition-based syntax is optimal, e.g. ``connect(condition='i!=j')``. However, |
| 671 |
if relatively few neurons are being connected then the 1-to-1 mapping or generator syntax |
| 672 |
will be better. For 1-to-1, ``connect(j='i')`` will always be faster than |
| 673 |
``connect(condition='i==j')`` because the latter has to evaluate all ``N**2`` pairs |
| 674 |
``(i, j)`` and check if the condition is true, whereas the former only has to do O(N) |
| 675 |
operations. |
| 676 |
|
| 677 |
One tricky problem is how to efficiently generate connectivity with a probability |
| 678 |
``p(i, j)`` that depends on both i and j, since this requires ``N*N`` computations |
| 679 |
even if the expected number of synapses is proportional to N. Some tricks for getting |
| 680 |
around this are shown in :doc:`../examples/synapses.efficient_gaussian_connectivity`. |