| 1 |
Synapses (Brian 1 --> 2 conversion) |
| 2 |
=================================== |
| 3 |
.. sidebar:: Brian 2 documentation |
| 4 |
|
| 5 |
For the main documentation about defining and creating synapses, see the |
| 6 |
document :doc:`../../user/synapses`. |
| 7 |
|
| 8 |
.. contents:: |
| 9 |
:local: |
| 10 |
:depth: 1 |
| 11 |
|
| 12 |
Converting Brian 1's ``Connection`` class |
| 13 |
----------------------------------------- |
| 14 |
In Brian 2, the `Synapses` class is the only class to model synaptic |
| 15 |
connections, you will therefore have to convert all uses of Brian 1's |
| 16 |
``Connection`` class. The ``Connection`` class increases a post-synaptic |
| 17 |
variable by a certain amount (the "synaptic weight") each time a pre-synaptic |
| 18 |
spike arrives. This has to be explicitly specified when using the `Synapses` |
| 19 |
class, the equivalent to the basic ``Connection`` usage is: |
| 20 |
|
| 21 |
+----------------------------------------------+---------------------------------------------------+ |
| 22 |
| Brian 1 | Brian 2 | |
| 23 |
+==============================================+===================================================+ |
| 24 |
+ .. code:: | .. code:: | |
| 25 |
+ | | |
| 26 |
+ conn = Connection(source, target, 'ge') | conn = Synapses(source, target, 'w : siemens', | |
| 27 |
+ | on_pre='ge += w') | |
| 28 |
+ | | |
| 29 |
+----------------------------------------------+---------------------------------------------------+ |
| 30 |
|
| 31 |
Note that he variable ``w``, which stores the synaptic weight, has to have the |
| 32 |
same units as the post-synaptic variable (in this case: ``ge``) that it |
| 33 |
increases. |
| 34 |
|
| 35 |
Creating synapses and setting weights |
| 36 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 37 |
|
| 38 |
With the ``Connection`` class, creating a synapse and setting its weight is a |
| 39 |
single process whereas with the `Synapses` class those two steps are separate. |
| 40 |
There is no direct equivalent to the convenience functions ``connect_full``, |
| 41 |
``connect_random`` and ``connect_one_to_one``, but you can easily implement |
| 42 |
the same functionality with the general mechanism of `Synapses.connect`: |
| 43 |
|
| 44 |
+----------------------------------------------+---------------------------------------------------+ |
| 45 |
| Brian 1 | Brian 2 | |
| 46 |
+==============================================+===================================================+ |
| 47 |
+ .. code:: | .. code:: | |
| 48 |
+ | | |
| 49 |
+ conn1 = Connection(source, target, 'ge') | conn1 = Synapses(source, target, 'w: siemens', | |
| 50 |
+ conn1[3, 5] = 3*nS | on_pre='ge += w') | |
| 51 |
+ | conn1.connect(i=3, j=5) | |
| 52 |
+ | conn1.w[3, 5] = 3*nS # (or conn1.w = 3*nS) | |
| 53 |
+ | | |
| 54 |
+----------------------------------------------+---------------------------------------------------+ |
| 55 |
+ .. code:: | .. code:: | |
| 56 |
+ | | |
| 57 |
+ conn2 = Connection(source, target, 'ge') | conn2 = ... # see above | |
| 58 |
+ conn2.connect_full(source, target, 5*nS) | conn2.connect() | |
| 59 |
+ | conn2.w = 5*nS | |
| 60 |
+ | | |
| 61 |
+----------------------------------------------+---------------------------------------------------+ |
| 62 |
+ .. code:: | .. code:: | |
| 63 |
+ | | |
| 64 |
+ conn3 = Connection(source, target, 'ge') | conn3 = ... # see above | |
| 65 |
+ conn3.connect_random(source, target, | conn3.connect(p=0.02) | |
| 66 |
+ sparseness=0.02, | conn3.w = 2*nS | |
| 67 |
+ weight=2*ns) | | |
| 68 |
+ | | |
| 69 |
+----------------------------------------------+---------------------------------------------------+ |
| 70 |
+ .. code:: | .. code:: | |
| 71 |
+ | | |
| 72 |
+ conn4 = Connection(source, target, 'ge') | conn4 = ... # see above | |
| 73 |
+ conn4.connect_one_to_one(source, target, | conn4.connect(j='i') | |
| 74 |
+ weight=4*nS) | conn4.w = 4*nS | |
| 75 |
+ | | |
| 76 |
+----------------------------------------------+---------------------------------------------------+ |
| 77 |
+ .. code:: | .. code:: | |
| 78 |
+ | | |
| 79 |
+ conn5 = IdentityConnection(source, target,| conn5 = Synapses(source, target, | |
| 80 |
+ weight=3*nS) | 'w : siemens (shared)') | |
| 81 |
+ | conn5.w = 3*nS | |
| 82 |
+ | | |
| 83 |
+----------------------------------------------+---------------------------------------------------+ |
| 84 |
|
| 85 |
Weight matrices |
| 86 |
~~~~~~~~~~~~~~~ |
| 87 |
|
| 88 |
Brian 2's `Synapses` class does not support setting the weights of a neuron with |
| 89 |
a weight matrix. However, `Synapses.connect` creates the synapses in a |
| 90 |
predictable order (first all synapses for the first pre-synaptic cell, then all |
| 91 |
synapses for the second pre-synaptic cell, etc.), so a reshaped "flat" weight |
| 92 |
matrix can be used: |
| 93 |
|
| 94 |
+----------------------------------------------+---------------------------------------------------+ |
| 95 |
| Brian 1 | Brian 2 | |
| 96 |
+==============================================+===================================================+ |
| 97 |
+ .. code:: | .. code:: | |
| 98 |
+ | | |
| 99 |
+ # len(source) == 20, len(target) == 30 | # len(source) == 20, len(target) == 30 | |
| 100 |
+ conn6 = Connection(source, target, 'ge') | conn6 = Synapses(source, target, 'w: siemens', | |
| 101 |
+ W = rand(20, 30)*nS | on_pre='ge += w') | |
| 102 |
+ conn6.connect(source, target, weight=W) | W = rand(20, 30)*nS | |
| 103 |
+ | conn6.connect() | |
| 104 |
+ | conn6.w = W.flatten() | |
| 105 |
+ | | |
| 106 |
+----------------------------------------------+---------------------------------------------------+ |
| 107 |
|
| 108 |
However note that if your weight matrix can be described mathematically (e.g. |
| 109 |
random as in the example above), then you should not create a weight matrix in |
| 110 |
the first place but use Brian 2's mechanism to set variables based on |
| 111 |
mathematical expressions (in the above case: ``conn5.w = 'rand()'``). Especially |
| 112 |
for big connection matrices this will have better performance, since it will be |
| 113 |
executed in generated code. You should only resort to explicit weight matrices |
| 114 |
when there is no alternative (e.g. to load weights from previous simulations). |
| 115 |
|
| 116 |
In Brian 1, you can restrict the functions ``connect``, ``connect_random``, etc. |
| 117 |
to subgroups. Again, there is no direct equivalent to this in Brian 2, but the |
| 118 |
general string syntax allows you to make connections conditional on logical |
| 119 |
statements that refer to pre-/post-synaptic indices and can therefore also used |
| 120 |
to restrict the connection to a subgroup of cells. When you set the synaptic |
| 121 |
weights, you *can* however use subgroups to restrict the subset of weights you |
| 122 |
want to set. |
| 123 |
|
| 124 |
+--------------------------------------------------------+---------------------------------------------------+ |
| 125 |
| Brian 1 | Brian 2 | |
| 126 |
+========================================================+===================================================+ |
| 127 |
+ .. code:: | .. code:: | |
| 128 |
+ | | |
| 129 |
+ conn7 = Connection(source, target, 'ge') | conn7 = Synapses(source, target, 'w: siemens', | |
| 130 |
+ conn7.connect_full(source[:5], target[5:10], 5*nS) | on_pre='ge += w') | |
| 131 |
+ | conn7.connect('i < 5 and j >=5 and j <10') | |
| 132 |
+ | # Alternative (more efficient): | |
| 133 |
+ | # conn7.connect(j='k in range(5, 10) if i < 5')| |
| 134 |
+ | conn7.w[source[:5], target[5:10]] = 5*nS | |
| 135 |
+ | | |
| 136 |
+--------------------------------------------------------+---------------------------------------------------+ |
| 137 |
|
| 138 |
Connections defined by functions |
| 139 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 140 |
|
| 141 |
Brian 1 allowed you to pass in a function as the value for the weight |
| 142 |
argument in a ``connect`` call (and also for the sparseness argument in |
| 143 |
``connect_random``). You should be able to replace such use cases by the the |
| 144 |
general, string-expression based method: |
| 145 |
|
| 146 |
+------------------------------------------------------------------+---------------------------------------------------+ |
| 147 |
| Brian 1 | Brian 2 | |
| 148 |
+==================================================================+===================================================+ |
| 149 |
+ .. code:: | .. code:: | |
| 150 |
+ | | |
| 151 |
+ conn8 = Connection(source, target, 'ge') | conn8 = Synapses(source, target, 'w: siemens', | |
| 152 |
+ conn8.connect_full(source, target, | on_pre='ge += w') | |
| 153 |
+ weight=lambda i,j:(1+cos(i-j))*2*nS) | conn8.connect() | |
| 154 |
+ | conn8.w = '(1 + cos(i - j))*2*nS' | |
| 155 |
+ | | |
| 156 |
+------------------------------------------------------------------+---------------------------------------------------+ |
| 157 |
+ .. code:: | .. code:: | |
| 158 |
+ | | |
| 159 |
+ conn9 = Connection(source, target, 'ge') | conn9 = ... # see above | |
| 160 |
+ conn9.connect_random(source, target, | conn9.connect(p=0.02) | |
| 161 |
+ sparseness=0.02, | conn9.w = 'rand()*nS' | |
| 162 |
+ weight=lambda:rand()*nS) | | |
| 163 |
+ | | |
| 164 |
+------------------------------------------------------------------+---------------------------------------------------+ |
| 165 |
+ .. code:: | .. code:: | |
| 166 |
+ | | |
| 167 |
+ conn10 = Connection(source, target, 'ge') | conn10 = ... # see above | |
| 168 |
+ conn10.connect_random(source, target, | conn10.connect(p='exp(-abs(i - j)*.1)') | |
| 169 |
+ sparseness=lambda i,j:exp(-abs(i-j)*.1),| conn10.w = 2*nS | |
| 170 |
+ weight=2*ns) | | |
| 171 |
+ | | |
| 172 |
+------------------------------------------------------------------+---------------------------------------------------+ |
| 173 |
|
| 174 |
Delays |
| 175 |
~~~~~~ |
| 176 |
The specification of delays changed in several aspects from Brian 1 to Brian 2: |
| 177 |
In Brian 1, delays where homogeneous by default, and heterogeneous delays had |
| 178 |
to be marked by ``delay=True``, together with the specification of the maximum |
| 179 |
delay. In Brian 2, heterogeneous delays are the default and you do not have to |
| 180 |
state the maximum delay. Brian 1's syntax of specifying a pair of values to get |
| 181 |
randomly distributed delays in that range is no longer supported, instead use |
| 182 |
Brian 2's standard string syntax: |
| 183 |
|
| 184 |
+----------------------------------------------------------+-----------------------------------------------------+ |
| 185 |
| Brian 1 | Brian 2 | |
| 186 |
+==========================================================+=====================================================+ |
| 187 |
+ .. code:: | .. code:: | |
| 188 |
+ | | |
| 189 |
+ conn11 = Connection(source, target, 'ge', delay=True, | conn11 = Synapses(source, target, 'w : siemens', | |
| 190 |
+ max_delay=5*ms) | on_pre='ge += w') | |
| 191 |
+ conn11.connect_full(source, target, weight=3*nS, | conn11.connect() | |
| 192 |
+ delay=(0*ms, 5*ms)) | conn11.w = 3*nS | |
| 193 |
+ | conn11.delay = 'rand()*5*ms' | |
| 194 |
+ | | |
| 195 |
+----------------------------------------------------------+-----------------------------------------------------+ |
| 196 |
|
| 197 |
Modulation |
| 198 |
~~~~~~~~~~ |
| 199 |
In Brian 2, there's no need for the ``modulation`` keyword that Brian 1 offered, |
| 200 |
you can describe the modulation as part of the ``on_pre`` action: |
| 201 |
|
| 202 |
+----------------------------------------------------------+-----------------------------------------------------+ |
| 203 |
| Brian 1 | Brian 2 | |
| 204 |
+==========================================================+=====================================================+ |
| 205 |
+ .. code:: | .. code:: | |
| 206 |
+ | | |
| 207 |
+ conn12 = Connection(source, target, 'ge', | conn12 = Synapses(source, target, 'w : siemens', | |
| 208 |
+ modulation='u') | on_pre='ge += w * u_pre') | |
| 209 |
+ | | |
| 210 |
+----------------------------------------------------------+-----------------------------------------------------+ |
| 211 |
|
| 212 |
Structure |
| 213 |
~~~~~~~~~ |
| 214 |
There's no equivalen for Brian 1's ``structure`` keyword in Brian 2, synapses |
| 215 |
are always stored in a sparse data structure. There is currently no support for |
| 216 |
changing synapses at run time (i.e. the "dynamic" structure of Brian 1). |
| 217 |
|
| 218 |
|
| 219 |
Converting Brian 1's ``Synapses`` class |
| 220 |
--------------------------------------- |
| 221 |
Brian 2's `Synapses` class works for the most part like the class of the same |
| 222 |
name in Brian 1. There are however some differences in details, listed below: |
| 223 |
|
| 224 |
Synaptic models |
| 225 |
~~~~~~~~~~~~~~~ |
| 226 |
The basic syntax to define a synaptic model is unchanged, but the keywords |
| 227 |
``pre`` and ``post`` have been renamed to ``on_pre`` and ``on_post``, |
| 228 |
respectively. |
| 229 |
|
| 230 |
+----------------------------------------------------------------------------+----------------------------------------------------------------------------+ |
| 231 |
| Brian 1 | Brian 2 | |
| 232 |
+============================================================================+============================================================================+ |
| 233 |
| .. code:: | .. code:: | |
| 234 |
| | | |
| 235 |
| stdp_syn = Synapses(inputs, neurons, model=''' | stdp_syn = Synapses(inputs, neurons, model=''' | |
| 236 |
| w:1 | w:1 | |
| 237 |
| dApre/dt = -Apre/taupre : 1 (event-driven) | dApre/dt = -Apre/taupre : 1 (event-driven) | |
| 238 |
| dApost/dt = -Apost/taupost : 1 (event-driven)''', | dApost/dt = -Apost/taupost : 1 (event-driven)''', | |
| 239 |
| pre='''ge + =w | on_pre='''ge + =w | |
| 240 |
| Apre += delta_Apre | Apre += delta_Apre | |
| 241 |
| w = clip(w + Apost, 0, gmax)''', | w = clip(w + Apost, 0, gmax)''', | |
| 242 |
| post='''Apost += delta_Apost | on_post='''Apost += delta_Apost | |
| 243 |
| w = clip(w + Apre, 0, gmax)''') | w = clip(w + Apre, 0, gmax)''') | |
| 244 |
| | | |
| 245 |
+----------------------------------------------------------------------------+----------------------------------------------------------------------------+ |
| 246 |
|
| 247 |
Lumped variables (summed variables) |
| 248 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 249 |
The syntax to define lumped variables (we use the term "summed variables" in |
| 250 |
Brian 2) has been changed: instead of assigning the synaptic variable to the |
| 251 |
neuronal variable you'll have to include the summed variable in the synaptic |
| 252 |
equations with the flag ``(summed)``: |
| 253 |
|
| 254 |
+------------------------------------------------------------+------------------------------------------------------------+ |
| 255 |
| Brian 1 | Brian 2 | |
| 256 |
+============================================================+============================================================+ |
| 257 |
| .. code:: | .. code:: | |
| 258 |
| | | |
| 259 |
| # a non-linear synapse (e.g. NMDA) | # a non-linear synapse (e.g. NMDA) | |
| 260 |
| neurons = NeuronGroup(1, model=''' | neurons = NeuronGroup(1, model=''' | |
| 261 |
| dv/dt = (gtot - v)/(10*ms) : 1 | dv/dt = (gtot - v)/(10*ms) : 1 | |
| 262 |
| gtot : 1''') | gtot : 1''') | |
| 263 |
| syn = Synapses(inputs, neurons, | syn = Synapses(inputs, neurons, | |
| 264 |
| model=''' | model=''' | |
| 265 |
| dg/dt = -a*g+b*x*(1-g) : 1 | dg/dt = -a*g+b*x*(1-g) : 1 | |
| 266 |
| dx/dt = -c*x : 1 | dx/dt = -c*x : 1 | |
| 267 |
| w : 1 # synaptic weight''', | w : 1 # synaptic weight | |
| 268 |
| pre='x += w') | gtot_post = g : 1 (summed)''', | |
| 269 |
| neurons.gtot=S.g | on_pre='x += w') | |
| 270 |
| | | |
| 271 |
+------------------------------------------------------------+------------------------------------------------------------+ |
| 272 |
|
| 273 |
Creating synapses |
| 274 |
~~~~~~~~~~~~~~~~~ |
| 275 |
In Brian 1, synapses were created by assigning ``True`` or an integer (the |
| 276 |
number of synapses) to an indexed `Synapses` object. In Brian 2, all synapse |
| 277 |
creation goes through the `Synapses.connect` function. For examples how to |
| 278 |
create more complex connection patterns, see the section on translating |
| 279 |
``Connections`` objects above. |
| 280 |
|
| 281 |
+-------------------------------+-------------------------------+ |
| 282 |
| Brian 1 | Brian 2 | |
| 283 |
+===============================+===============================+ |
| 284 |
| .. code:: | .. code:: | |
| 285 |
| | | |
| 286 |
| syn = Synapses(...) | syn = Synapses(...) | |
| 287 |
| # single synapse | # single synapse | |
| 288 |
| syn[3, 5] = True | syn.connect(i=3, j=5) | |
| 289 |
| | | |
| 290 |
+-------------------------------+-------------------------------+ |
| 291 |
| .. code:: | .. code:: | |
| 292 |
| | | |
| 293 |
| # all-to-all connections | # all-to-all connections | |
| 294 |
| syn[:, :] = True | syn.connect() | |
| 295 |
| | | |
| 296 |
+-------------------------------+-------------------------------+ |
| 297 |
| .. code:: | .. code:: | |
| 298 |
| | | |
| 299 |
| # all to neuron number 1 | # all to neuron number 1 | |
| 300 |
| syn[:, 1] = True | syn.connect(j='1') | |
| 301 |
| | | |
| 302 |
+-------------------------------+-------------------------------+ |
| 303 |
| .. code:: | .. code:: | |
| 304 |
| | | |
| 305 |
| # multiple synapses | # multiple synapses | |
| 306 |
| syn[4, 7] = 3 | syn.connect(i=4, j=7, n=3) | |
| 307 |
| | | |
| 308 |
+-------------------------------+-------------------------------+ |
| 309 |
| .. code:: | .. code:: | |
| 310 |
| | | |
| 311 |
| # connection probability 2%| # connection probability 2%| |
| 312 |
| syn[:, :] = 0.02 | syn.connect(p=0.02) | |
| 313 |
| | | |
| 314 |
+-------------------------------+-------------------------------+ |
| 315 |
|
| 316 |
Multiple pathways |
| 317 |
~~~~~~~~~~~~~~~~~ |
| 318 |
As Brian 1, Brian 2 supports multiple pre- or post-synaptic pathways, with |
| 319 |
separate pre-/post-codes and delays. In Brian 1, you have to specify the |
| 320 |
pathways as tuples and can then later access them individually by using their |
| 321 |
index. In Brian 2, you specify the pathways as a dictionary, i.e. by giving |
| 322 |
them individual names which you can then later use to access them (the default |
| 323 |
pathways are called ``pre`` and ``post``): |
| 324 |
|
| 325 |
+----------------------------------------------------------+----------------------------------------------------------+ |
| 326 |
| Brian 1 | Brian 2 | |
| 327 |
+==========================================================+==========================================================+ |
| 328 |
| .. code:: | .. code:: | |
| 329 |
| | | |
| 330 |
| S = Synapses(..., | S = Synapses(..., | |
| 331 |
| pre=('ge + =w', | pre={'pre_transmission': | |
| 332 |
| '''w = clip(w + Apost, 0, inf) | 'ge += w', | |
| 333 |
| Apre += delta_Apre'''), | 'pre_plasticity': | |
| 334 |
| post='''Apost += delta_Apost | '''w = clip(w + Apost, 0, inf) | |
| 335 |
| w = clip(w + Apre, 0, inf)''')| Apre += delta_Apre'''}, | |
| 336 |
| | post='''Apost += delta_Apost | |
| 337 |
| S[:, :] = True | w = clip(w + Apre, 0, inf)''')| |
| 338 |
| S.delay[1][:, :] = 3*ms # delayed trace | | |
| 339 |
| | S.connect() | |
| 340 |
| | S.pre_plasticity.delay[:, :] = 3*ms # delayed trace| |
| 341 |
| | | |
| 342 |
+----------------------------------------------------------+----------------------------------------------------------+ |
| 343 |
|
| 344 |
Monitoring synaptic variables |
| 345 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 346 |
Both in Brian 1 and Brian 2, you can record the values of synaptic variables |
| 347 |
with a `StateMonitor`. You no longer have to call an explicit indexing function, |
| 348 |
but you can directly provide an appropriately indexed `Synapses` object. You |
| 349 |
can now also use the same technique to index the `StateMonitor` object to get |
| 350 |
the recorded values, see the respective section in the |
| 351 |
:doc:`../../user/synapses` documentation for details. |
| 352 |
|
| 353 |
+-------------------------------------------------+----------------------------------------------+ |
| 354 |
| Brian 1 | Brian 2 | |
| 355 |
+=================================================+==============================================+ |
| 356 |
| .. code:: | .. code:: | |
| 357 |
| | | |
| 358 |
| syn = Synapses(...) | syn = Synapses(...) | |
| 359 |
| # record all synapse targetting neuron 3 | # record all synapse targetting neuron 3 | |
| 360 |
| indices = syn.synapse_index((slice(None), 3))| mon = StateMonitor(S, 'w', record=S[:, 3])| |
| 361 |
| mon = StateMonitor(S, 'w', record=indices) | | |
| 362 |
| | | |
| 363 |
+-------------------------------------------------+----------------------------------------------+ |