# Source code for brainmodels.synapses.AMPA

# -*- coding: utf-8 -*-

import brainpy as bp
import brainpy.math as bm

from .base import Synapse

__all__ = [
'AMPA',
]

[docs]class AMPA(Synapse):
r"""AMPA conductance-based synapse model.

**Model Descriptions**

AMPA receptor is an ionotropic receptor, which is an ion channel.
When it is bound by neurotransmitters, it will immediately open the
ion channel, causing the change of membrane potential of postsynaptic neurons.

A classical model is to use the Markov process to model ion channel switch.
Here :math:g represents the probability of channel opening, :math:1-g
represents the probability of ion channel closing, and :math:\alpha and
:math:\beta are the transition probability. Because neurotransmitters can
open ion channels, the transfer probability from :math:1-g to :math:g
is affected by the concentration of neurotransmitters. We denote the concentration
of neurotransmitters as :math:[T] and get the following Markov process.

.. image:: ../../images/synapse_markov.png
:align: center

We obtained the following formula when describing the process by a differential equation.

.. math::

\frac{dg}{dt} =\alpha[T](1-g)-\beta g

where :math:\alpha [T] denotes the transition probability from state :math:(1-g)
to state :math:(g); and :math:\beta represents the transition probability of
the other direction. :math:\alpha is the binding constant. :math:\beta is the
unbinding constant. :math:[T] is the neurotransmitter concentration, and
has the duration of 0.5 ms.

Moreover, the post-synaptic current on the post-synaptic neuron is formulated as

.. math::

I_{syn} = g_{max} g (V-E)

where :math:g_{max} is the maximum conductance, and E is the reverse potential.

**Model Examples**

.. plot::
:include-source: True

>>> import brainpy as bp
>>> import brainmodels
>>> import matplotlib.pyplot as plt
>>>
>>> neu1 = brainmodels.neurons.HH(1)
>>> neu2 = brainmodels.neurons.HH(1)
>>> syn1 = brainmodels.synapses.AMPA(neu1, neu2, bp.connect.All2All())
>>> net = bp.Network(pre=neu1, syn=syn1, post=neu2)
>>>
>>> runner = bp.StructRunner(net, inputs=[('pre.input', 5.)], monitors=['pre.V', 'post.V', 'syn.g'])
>>> runner.run(150.)
>>>
>>> fig, gs = bp.visualize.get_figure(2, 1, 3, 8)
>>> plt.plot(runner.mon.ts, runner.mon['pre.V'], label='pre-V')
>>> plt.plot(runner.mon.ts, runner.mon['post.V'], label='post-V')
>>> plt.legend()
>>>
>>> plt.plot(runner.mon.ts, runner.mon['syn.g'], label='g')
>>> plt.legend()
>>> plt.show()

**Model Parameters**

============= ============== ======== ================================================
**Parameter** **Init Value** **Unit** **Explanation**
------------- -------------- -------- ------------------------------------------------
delay         0              ms       The decay length of the pre-synaptic spikes.
g_max         .42            µmho(µS) Maximum conductance.
E             0              mV       The reversal potential for the synaptic current.
alpha         .98            \        Binding constant.
beta          .18            \        Unbinding constant.
T             .5             mM       Neurotransmitter concentration.
T_duration    .5             ms       Duration of the neurotransmitter concentration.
============= ============== ======== ================================================

**Model Variables**

================== ================== ==================================================
**Member name**    **Initial values** **Explanation**
------------------ ------------------ --------------------------------------------------
g                  0                  Synapse gating variable.
pre_spike          False              The history of pre-synaptic neuron spikes.
spike_arrival_time -1e7               The arrival time of the pre-synaptic neuron spike.
================== ================== ==================================================

**References**

.. [1] Vijayan S, Kopell N J. Thalamic model of awake alpha oscillations
and implications for stimulus processing[J]. Proceedings of the
National Academy of Sciences, 2012, 109(45): 18553-18558.
"""

[docs]  def __init__(self, pre, post, conn, delay=0., g_max=0.42, E=0., alpha=0.98,
beta=0.18, T=0.5, T_duration=0.5, method='exp_auto', name=None):
super(AMPA, self).__init__(pre=pre, post=post, conn=conn, method=method, name=name)
self.check_pre_attrs('t_last_spike')
self.check_post_attrs('input', 'V')

# parameters
self.delay = delay
self.g_max = g_max
self.E = E
self.alpha = alpha
self.beta = beta
self.T = T
self.T_duration = T_duration

# connections
self.pre_ids, self.post_ids = self.conn.require('pre_ids', 'post_ids')

# variables
self.g = bm.Variable(bm.zeros(len(self.pre_ids)))
self.pre_spike = bp.ConstantDelay(self.pre.num, delay, dtype=pre.spike.dtype)
self.spike_arrival_time = bm.Variable(bm.ones(self.pre.num) * -1e7)

def derivative(self, g, t, TT):
dg = self.alpha * TT * (1 - g) - self.beta * g
return dg

[docs]  def update(self, _t, _dt):
self.pre_spike.push(self.pre.spike)
self.spike_arrival_time.value = bm.where(self.pre_spike.pull(), _t, self.spike_arrival_time)
syn_st = bm.pre2syn(self.spike_arrival_time, self.pre_ids)
TT = ((_t - syn_st) < self.T_duration) * self.T
self.g.value = self.integral(self.g, _t, TT, dt=_dt)
g_post = bm.syn2post(self.g, self.post_ids, self.post.num)
self.post.input -= self.g_max * g_post * (self.post.V - self.E)