.. currentmodule:: brian .. _usermanual-synapses: Synapses ======== Starting from Brian 1.4, there is a new class, :class:`Synapses`, in which everything synaptic can be defined. The :class:`Synapses` is similar to the :class:`Connection` class, but it is more general and flexible. In particular, synaptic plasticity can be defined in the same object. Defining synaptic models ------------------------ The basic syntax is as follows:: S=Synapses(P,Q,model='w:1',pre='v+=w') This defines a set of synapses between :class:`NeuronGroup` P and :class:`NeuronGroup` Q. If the target group is not specified, it is identical to the source group by default. The ``model`` keyword is similar as in :class:`NeuronGroup`: it defines synaptic variables and possibly their dynamics (with differential equations, as in :class:`NeuronGroup`). Here, synaptic variable ``w`` is created: there is one value for each synapse. The ``pre`` keyword defines what happens when a presynaptic spike arrives at a synapse. In this case, variable ``w`` is added to variable ``v``. Because ``v`` is not defined as a synaptic variable, it is assumed by default that it is a postsynaptic variable, defined in the target :class:`NeuronGroup` Q. Note that this does not does create synapses (see next section), only the synaptic models. The more general syntax is:: S=Synapses(P,Q,model=model_string,pre=pre_code,post=post_code) Model syntax ^^^^^^^^^^^^ The model follows exactly the same syntax as for :class:`NeuronGroup`. There can be parameters (e.g. synaptic variable ``w`` above), but there can also be static equations and differential equations, describing the dynamics of synaptic variables. In all cases, synaptic variables are created, one value per synapse. Internally, these are stored as arrays. There are a few specificities: * A variable with the ``_post`` suffix is looked up in the postsynaptic (target) neuron. That is, ``v_post`` means variable ``v`` in the postsynaptic neuron. * A variable with the ``_pre`` suffix is looked up in the presynaptic (source) neuron. * A variable not defined as a synaptic variable is considered to be postsynaptic. * A variable not defined as a synaptic variable and not defined in the postsynaptic neuron is considered external. For the integration of differential equations, one can use the same keywords as for :class:`NeuronGroup`. Event-driven updates ^^^^^^^^^^^^^^^^^^^^ By default, differential equations are integrated in a clock-driven fashion, as for a :class:`NeuronGroup`. This is potentially very time consuming, because all synapses are updated at every timestep. It is possible to ask Brian to simulate differential equations in an event-driven fashion, for one-dimensional linear equations, using the keyword ``(event-driven)``. A typical example is pre and postsynaptic traces in STDP:: model='''w:1 dApre/dt=-Apre/taupre : 1 (event-driven) dApost/dt=-Apost/taupost : 1 (event-driven)''' Here, Brian updates the value of ``Apre`` for a given synapse only when this synapse receives a spike, whether it is presynaptic or postsynaptic. More precisely, the variables are updated every time either the ``pre`` or ``post`` code is called for the synapse, so that the values are always up to date when these codes are executed. Automatic event-driven updates are only possible for one-dimensional linear equations. These equations must also be independent of the other ones, that is, a differential equation that is not event-driven cannot depend on an event-driven equation (since the values are not continuously updated). In other cases, the user can write event-driven code explicitly in the update codes (see below). Pre and post codes ^^^^^^^^^^^^^^^^^^ The ``pre`` (``post``) code is executed at each synapse receiving a presynaptic spike. For example: pre='v+=w' adds the value of synaptic variable ``w`` to postsynaptic variable ``v``. As for the model equations, the ``_post`` (``_pre``) suffix indicates a postsynaptic (presynaptic) variable, and variables not found in the synaptic variables are considered postsynaptic by default. Internally, the execution of the code is vectorized (simultaneously executed) for all synapses receiving presynaptic spikes during the current timestep. Therefore, the code should be understood as acting on arrays rather than single values. Any sort of code can be executed. For example, the following code defines stochastic synapses, with a synaptic weight ``w`` and transmission probability ``p``:: S=Synapses(input,neurons,model="""w : 1 p : 1""", pre="v+=w*(rand()3 S.w[2,3]=(1*nS,2*nS) S.w[group1,group2]="(1+cos(i-j))*2*nS" S.w[:,:]='rand()*nS' Delays ------ There is a special synaptic variable that is automatically created: ``delay``. It is the propagation delay from the presynaptic neuron to the synapse, i.e., the presynaptic delay. An alias is ``delay_pre``. When there is a postsynaptic code (keyword ``post``), the variable ``delay_post`` is created. These can be accessed and modified in the same way as other synaptic variables. If delays can change during the simulation, one should specify the maximum allowed delay with the keyword ``max_delay``:: synapses = Synapses(P,Q,model='w:1',pre='v+=w',max_delay=1*ms) Otherwise, this maximum delay is automatically calculated the first time the model is run. Multiple pathways ----------------- It is possible to have multiple pathways with different update codes from the same presynaptic neuron group. This may be interesting in cases when different operations must be applied at different times for the same presynaptic spike. To do this, simply specify a tuple or list of ``pre`` codes:: pre=('ge+=w', '''w=clip(w+Apost,0,inf) Apre+=dApre''') This creates two sets of delay variables, one for each pathway. They can be accessed by first indexing with the pathway number. The following statement, for example, sets the delay of the synapse between the first neurons of the source and target groups, in the second pathway:: S.delay[1][0,0]=3*ms Monitoring synaptic variables ----------------------------- A :class:`StateMonitor` object can be used to monitor synaptic variables. For example, the following statement creates a monitor for variable ``w`` for the synapses 0 and 1:: M = StateMonitor(S,'w',record=[0,1]) Note that these are synapse indexes, not neuron indexes. These can be obtained with the :meth:`~Synapses.synapse_index` method:: s=S.synapse_index((i,j)) where ``i`` and ``j`` may be integers, arrays or slices. A third index can also be given. The recorded traces can then be accessed in the usual way, for example:: plot(M.times,M[0])