{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Example of the tsodyks2_synapse in NEST\n\nThis synapse model implements synaptic short-term depression and short-term f\naccording to [1]_ and [2]_. It solves Eq (2) from [1]_ and modulates U according\n\nThis connection merely scales the synaptic weight, based on the spike history\nparameters of the kinetic model. Thus, it is suitable for any type of synapse\nthat is current or conductance based.\n\nThe parameter `A_se` from the publications is represented by the\nsynaptic weight. The variable `x` in the synapse properties is the\nfactor that scales the synaptic weight.\n\n## Parameters\n\nThe following parameters can be set in the status dictionary:\n\n* U           - probability of release increment (U1) [0,1], default=0.\n* u           - Maximum probability of release (U_se) [0,1], default=0.\n* x           - current scaling factor of the weight, default=U\n* tau_rec     - time constant for depression in ms, default=800 ms\n* tau_fac     - time constant for facilitation in ms, default=0 (off)\n\n## Notes\n\nUnder identical conditions, the ``tsodyks2_synapse`` produces slightly lower\npeak amplitudes than the ``tsodyks_synapse``. However, the qualitative behavior\nis identical.\n\nThis compares the two synapse models.\n\n## References\n\n.. [1] Tsodyks MV, and Markram H. (1997). The neural code between\n       neocortical depends on neurotransmitter release probability. PNAS,\n       94(2), 719-23.\n.. [2] Fuhrmann G, Segev I, Markram H, and Tsodyks MV. (2002). Coding of\n       temporal information by activity-dependent synapses. Journal of\n       Neurophysiology, 8. https://doi.org/10.1152/jn.00258.2001\n.. [3] Maass W, and Markram H. (2002). Synapses as dynamic memory buffers.\n       Neural Networks, 15(2), 155-161.\n       http://dx.doi.org/10.1016/S0893-6080(01)00144-7\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import nest\nimport nest.voltage_trace\n\nnest.ResetKernel()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Parameter set for depression\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "dep_params = {\"U\": 0.67, \"u\": 0.67, 'x': 1.0, \"tau_rec\": 450.0,\n              \"tau_fac\": 0.0, \"weight\": 250.}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Parameter set for facilitation\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fac_params = {\"U\": 0.1, \"u\": 0.1, 'x': 1.0, \"tau_fac\": 1000.,\n              \"tau_rec\": 100., \"weight\": 250.}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now we assign the parameter set to the synapse models.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "t1_params = fac_params       # for tsodyks_synapse\nt2_params = t1_params.copy()  # for tsodyks2_synapse\n\nnest.SetDefaults(\"tsodyks2_synapse\", t1_params)\nnest.SetDefaults(\"tsodyks_synapse\", t2_params)\nnest.SetDefaults(\"iaf_psc_exp\", {\"tau_syn_ex\": 3.})"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Create three neurons.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "neuron = nest.Create(\"iaf_psc_exp\", 3)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Neuron one produces spikes. Neurons 2 and 3 receive the spikes via the two\nsynapse models.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "nest.Connect(neuron[0], neuron[1], syn_spec=\"tsodyks_synapse\")\nnest.Connect(neuron[0], neuron[2], syn_spec=\"tsodyks2_synapse\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now create two voltmeters to record the responses.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "voltmeter = nest.Create(\"voltmeter\", 2)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Connect the voltmeters to the neurons.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "nest.Connect(voltmeter[0], neuron[1])\nnest.Connect(voltmeter[1], neuron[2])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now simulate the standard STP protocol: a burst of spikes, followed by a\npause and a recovery response.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "neuron[0].I_e = 376.0\n\nnest.Simulate(500.0)\nneuron[0].I_e = 0.0\nnest.Simulate(500.0)\nneuron[0].I_e = 376.0\nnest.Simulate(500.0)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Finally, generate voltage traces. Both are shown in the same plot and\nshould be almost completely overlapping.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "nest.voltage_trace.from_device(voltmeter[0])\nnest.voltage_trace.from_device(voltmeter[1])\nnest.voltage_trace.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.6.12"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}