Modeling neurons in NESTML

Writing the NESTML model

The top-level element of the model is model, followed by a name. All other blocks appear inside of here.

model hodkin_huxley_neuron:
    # [...]

Neuronal interactions


A neuron model written in NESTML can be configured to receive two distinct types of input: spikes and continuous-time values. This can be indicated using the following syntax:

    AMPA_spikes <- spike
    I_stim pA <- continuous

The general syntax is:

port_name <- inputQualifier spike
port_name dataType <- continuous

The spiking input ports are declared without a data type, whereas the continuous input ports must have a data type. For spiking input ports, the qualifier keywords decide whether inhibitory and excitatory inputs are lumped together into a single named input port, or if they are separated into differently named input ports based on their sign. When processing a spike event, some simulators (including NEST) use the sign of the amplitude (or weight) property in the spike event to indicate whether it should be considered an excitatory or inhibitory spike. By using the qualifier keywords, a single spike handler can route each incoming spike event to the correct input buffer (excitatory or inhibitory). Compare:

    # [...]
    all_spikes <- spike

In this case, all spike events will be processed through the all_spikes input port. A spike weight could be positive or negative, and the occurrences of all_spikes in the model should be considered a signed quantity.

    # [...]
    AMPA_spikes <- excitatory spike
    GABA_spikes <- inhibitory spike

In this case, spike events that have a negative weight are routed to the GABA_spikes input port, and those that have a positive weight to the AMPA_spikes port.

It is equivalent if either both inhibitory and excitatory are given, or neither: an unmarked port will by default handle all incoming presynaptic spikes.


The incoming weight \(w\)

none, or excitatory and inhibitory

… may be positive or negative. It is added to the buffer with signed value \(w\) (positive or negative).


… should not be negative. It is added to the buffer with non-negative magnitude \(w\).


… should be negative. It is added to the buffer with non-negative magnitude \(-w\).

Integrating current input

The current port symbol (here, I_stim) is available as a variable and can be used in expressions, e.g.:

    V_m' = -V_m/tau_m + ... + I_stim

    I_stim pA <- continuous
Integrating spiking input

Spikes arriving at the input port of a neuron can be written as a spike train \(s(t)\):

\[\large s(t) = \sum_{i=1}^N w_i \cdot \delta(t - t_i)\]

where \(w_i\) is the weight of spike \(i\).

To model the effect that an arriving spike has on the state of the neuron, a convolution with a kernel can be used. The kernel defines the postsynaptic response kernel, for example, an alpha (bi-exponential) function, decaying exponential, or a delta function. (See Kernel functions for how to define a kernel.) The convolution of the kernel with the spike train is defined as follows:

\[\begin{split}\begin{align*} \large (f \ast s)(t) &= \int s(u) f(t-u) du \\ &= \sum_{i=1}^N \int w_i \cdot \delta(u-t_i) f(t-u) du \\ &= \sum_{i=1}^N w_i \cdot f(t - t_i) \end{align*}\end{split}\]

For example, say there is a spiking input port defined named spikes. A decaying exponential with time constant tau_syn is defined as postsynaptic kernel G. Their convolution is expressed using the convolve() function, which takes a kernel and input port, respectively, as its arguments:

    kernel G = exp(-t / tau_syn)
    inline I_syn pA = convolve(G, spikes) * pA
    V_m' = -V_m / tau_m + I_syn / C_m

Note that in this example, the intended physical unit (pA) was assigned by multiplying the scalar convolution result with the unit literal. By the definition of convolution, convolve(G, spikes) will have the unit of kernel G multiplied by the unit of spikes and unit of time, i.e., [G] * [spikes] * s. Kernel functions in NESTML are always untyped and the unit of spikes is \(1/s\) as discussed above. As a result, the unit of convolution is \((1/s) * s\), a scalar quantity without a unit.

The incoming spikes could have been equivalently handled with an onReceive event handler block:

    I_syn pA = 0 pA

    I_syn' = -I_syn / tau_syn
    V_m' = -V_m / tau_m + I_syn / C_m

    I_syn += spikes * pA * s

Note that in this example, the intended physical unit (pA) was assigned by multiplying the type of the input port spikes (which is 1/s) by pA·s, resulting in a unit of pA for I_syn.

(Re)setting synaptic integration state

When convolutions are used, additional state variables are required for each pair (shape, spike input port) that appears as the parameters in a convolution. These variables track the dynamical state of that kernel, for that input port. The number of variables created corresponds to the dimensionality of the kernel. For example, in the code block above, the one-dimensional kernel G is used in a convolution with spiking input port spikes. During code generation, a new state variable called G__conv__spikes is created for this combination, by joining together the name of the kernel with the name of the spike buffer using (by default) the string “__conv__”. If the same kernel is used later in a convolution with another spiking input port, say spikes_GABA, then the resulting generated variable would be called G__conv__spikes_GABA, allowing independent synaptic integration between input ports but allowing the same kernel to be used more than once.

The process of generating extra state variables for keeping track of convolution state is normally hidden from the user. For some models, however, it might be required to set or reset the state of synaptic integration, which is stored in these internally generated variables. For example, we might want to set the synaptic current (and its rate of change) to 0 when firing a dendritic action potential. Although we would like to set the generated variable G__conv__spikes to 0 in the running example, a variable by this name is only generated during code generation, and does not exist in the namespace of the NESTML model to begin with. To still allow referring to this state in the context of the model, it is recommended to use an inline expression, with only a convolution on the right-hand side.

For example, suppose we define:

inline g_dend pA = convolve(G, spikes)

Then the name g_dend can be used as a target for assignment:

    g_dend = 42 pA

This also works for higher-order kernels, e.g. for the second-order alpha kernel \(H(t)\):

kernel H'' = (-2/tau_syn) * H' - 1/tau_syn**2) * H

We can define an inline expression with the same port as before, spikes:

inline h_dend pA = convolve(H, spikes)

The name h_dend now acts as an alias for this particular convolution. We can now assign to the inline defined variable up to the order of the kernel:

    h_dend = 42 pA
    h_dend' = 10 pA/ms

For more information, see the Active dendrite tutorial.

Multiple input ports

If there is more than one line specifying a spike or continuous port with the same sign, a neuron with multiple receptor types is created. For example, say that we define three spiking input ports as follows:

    spikes1 <- spike
    spikes2 <- spike
    spikes3 <- spike

For the sake of keeping the example simple, we assign a decaying exponential-kernel postsynaptic response to each input port, each with a different time constant:

    kernel I_kernel1 = exp(-t / tau_syn1)
    kernel I_kernel2 = exp(-t / tau_syn2)
    kernel I_kernel3 = -exp(-t / tau_syn3)
    inline I_syn pA = (convolve(I_kernel1, spikes1) - convolve(I_kernel2, spikes2) + convolve(I_kernel3, spikes3)) * pA
    V_m' = -(V_m - E_L) / tau_m + I_syn / C_m
Multiple input ports with vectors

The input ports can also be defined as vectors. For example,

neuron multi_synapse_vectors:
        AMPA_spikes <- excitatory spike
        GABA_spikes <- inhibitory spike
        NMDA_spikes <- spike
        foo[2] <- spike
        exc_spikes[3] <- excitatory spike
        inh_spikes[3] <- inhibitory spike

        kernel I_kernel_exc = exp(-1 / tau_syn_exc * t)
        kernel I_kernel_inh = exp(-1 / tau_syn_inh * t)
        inline I_syn_exc pA = convolve(I_kernel_exc, exc_spikes[1]) * pA
        inline I_syn_inh pA = convolve(I_kernel_inh, inh_spikes[1]) * pA

In this example, the spiking input ports foo, exc_spikes, and inh_spikes are defined as vectors. The integer surrounded by [ and ] determines the size of the vector. The size of the input port must always be a positive-valued integer.

They could also be used in differential equations defined in the equations block as shown for exc_spikes[1] and inh_spikes[1] in the example above.


emit_spike: calling this function in the update block results in firing a spike to all target neurons and devices time stamped with the current simulation time.

Implementing refractoriness

In order to model an absolute refractory state, in which the neuron cannot fire action potentials, an extra parameter (say, refr_T) can be introduced, that defines the duration of the refractory period. A new state variable, refr_t, then specifies the time of the refractory period that has already elapsed, and a second boolean state variable is_refactory identifies whether or not we are in the refractory state. In the initial state, the neuron is not refractory and the timer is set to zero. When a spike is emitted, the boolean flag is set to true and the timer is set to refr_T. Using a separate flag allows us to freely formulate a condition on ending the timer without having to worry about special (for instance, negative) values representing a non-refactory condition. This is hazardous because of an imprecise floating point representation of real numbers. The check against \(\Delta t/2\) instead of checking against 0 additionally guards against accumulated discretization errors. Integrating the ODE for \(V_\text{m}\) is disabled while the flag is set to true. When the timer reaches zero, the flag is set to false. In the update block, the timer is decremented each timestep. An onCondition is formulated on ending the refractory period, which allows the time at which the condition becomes true to be determined precisely (whereas it would be aliased to the nearest simulation timestep interval if the condition had been checked in update).

    refr_T ms = 5 ms

    refr_t ms = 0 ms    # Refractory period timer
    is_refractory boolean = false

    I_syn' = ...
    V_m' = ...

    if is_refractory:
        # neuron is absolute refractory, do not evolve V_m
        refr_t -= resolution()
        # neuron not refractory, so evolve all ODEs
        integrate_odes(V_m, I_syn)

onCondition(is_refractory and refr_t <= resolution() / 2):
    # end of refractory period
    refr_t = 0 ms
    is_refractory = false