{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": false,
    "level": 1
   },
   "source": [
    "# Introduction to Brian part 2: Synapses"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you haven't yet read part 1: Neurons, go read that now.\n",
    "\n",
    "As before we start by importing the Brian package and setting up matplotlib for IPython:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from brian2 import *\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": false,
    "level": 2
   },
   "source": [
    "## The simplest Synapse\n",
    "\n",
    "Once you have some neurons, the next step is to connect them up via synapses. We'll start out with doing the simplest possible type of synapse that causes an instantaneous change in a variable after a spike."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "start_scope()\n",
    "\n",
    "eqs = '''\n",
    "dv/dt = (I-v)/tau : 1\n",
    "I : 1\n",
    "tau : second\n",
    "'''\n",
    "G = NeuronGroup(2, eqs, threshold='v>1', reset='v = 0')\n",
    "G.I = [2, 0]\n",
    "G.tau = [10, 100]*ms\n",
    "\n",
    "# Comment these two lines out to see what happens without Synapses\n",
    "S = Synapses(G, G, on_pre='v_post += 0.2')\n",
    "S.connect(i=0, j=1)\n",
    "\n",
    "M = StateMonitor(G, 'v', record=True)\n",
    "\n",
    "run(100*ms)\n",
    "\n",
    "plot(M.t/ms, M.v[0], '-b', label='Neuron 0')\n",
    "plot(M.t/ms, M.v[1], '-g', lw=2, label='Neuron 1')\n",
    "xlabel('Time (ms)')\n",
    "ylabel('v')\n",
    "legend(loc='best')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "level": 7
   },
   "source": [
    "There are a few things going on here. First of all, let's recap what is going on with the ``NeuronGroup``. We've created two neurons, each of which has the same differential equation but different values for parameters I and tau. Neuron 0 has ``I=2`` and ``tau=10*ms`` which means that is driven to repeatedly spike at a fairly high rate. Neuron 1 has ``I=0`` and ``tau=100*ms`` which means that on its own - without the synapses - it won't spike at all (the driving current I is 0). You can prove this to yourself by commenting out the two lines that define the synapse.\n",
    "\n",
    "Next we define the synapses: ``Synapses(source, target, ...)`` means that we are defining a synaptic model that goes from ``source`` to ``target``. In this case, the source and target are both the same, the group ``G``. The syntax ``on_pre='v_post += 0.2'`` means that when a spike occurs in the presynaptic neuron (hence ``on_pre``) it causes an instantaneous change to happen ``v_post += 0.2``. The ``_post`` means that the value of ``v`` referred to is the post-synaptic value, and it is increased by 0.2. So in total, what this model says is that whenever two neurons in G are connected by a synapse, when the source neuron fires a spike the target neuron will have its value of ``v`` increased by 0.2.\n",
    "\n",
    "However, at this point we have only defined the synapse model, we haven't actually created any synapses. The next line ``S.connect(i=0, j=1)`` creates a synapse from neuron 0 to neuron 1."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": false,
    "level": 2
   },
   "source": [
    "## Adding a weight\n",
    "\n",
    "In the previous section, we hard coded the weight of the synapse to be the value 0.2, but often we would to allow this to be different for different synapses. We do that by introducing synapse equations."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "start_scope()\n",
    "\n",
    "eqs = '''\n",
    "dv/dt = (I-v)/tau : 1\n",
    "I : 1\n",
    "tau : second\n",
    "'''\n",
    "G = NeuronGroup(3, eqs, threshold='v>1', reset='v = 0')\n",
    "G.I = [2, 0, 0]\n",
    "G.tau = [10, 100, 100]*ms\n",
    "\n",
    "# Comment these two lines out to see what happens without Synapses\n",
    "S = Synapses(G, G, 'w : 1', on_pre='v_post += w')\n",
    "S.connect(i=0, j=[1, 2])\n",
    "S.w = 'j*0.2'\n",
    "\n",
    "M = StateMonitor(G, 'v', record=True)\n",
    "\n",
    "run(50*ms)\n",
    "\n",
    "plot(M.t/ms, M.v[0], '-b', label='Neuron 0')\n",
    "plot(M.t/ms, M.v[1], '-g', lw=2, label='Neuron 1')\n",
    "plot(M.t/ms, M.v[2], '-r', lw=2, label='Neuron 1')\n",
    "xlabel('Time (ms)')\n",
    "ylabel('v')\n",
    "legend(loc='best')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This example behaves very similarly to the previous example, but now there's a synaptic weight variable ``w``. The string ``'w : 1'`` is an equation string, precisely the same as for neurons, that defines a single dimensionless parameter ``w``. We changed the behaviour on a spike to ``on_pre='v_post += w'`` now, so that each synapse can behave differently depending on the value of ``w``. To illustrate this, we've made a third neuron which behaves precisely the same as the second neuron, and connected neuron 0 to both neurons 1 and 2. We've also set the weights via ``S.w = 'j*0.2'``. When ``i`` and ``j`` occur in the context of synapses, ``i`` refers to the source neuron index, and ``j`` to the target neuron index. So this will give a synaptic connection from 0 to 1 with weight ``0.2=0.2*1`` and from 0 to 2 with weight ``0.4=0.2*2``."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": false,
    "level": 2
   },
   "source": [
    "## Introducing a delay\n",
    "\n",
    "So far, the synapses have been instantaneous, but we can also make them act with a certain delay."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "start_scope()\n",
    "\n",
    "eqs = '''\n",
    "dv/dt = (I-v)/tau : 1\n",
    "I : 1\n",
    "tau : second\n",
    "'''\n",
    "G = NeuronGroup(3, eqs, threshold='v>1', reset='v = 0')\n",
    "G.I = [2, 0, 0]\n",
    "G.tau = [10, 100, 100]*ms\n",
    "\n",
    "S = Synapses(G, G, 'w : 1', on_pre='v_post += w')\n",
    "S.connect(i=0, j=[1, 2])\n",
    "S.w = 'j*0.2'\n",
    "S.delay = 'j*2*ms'\n",
    "\n",
    "M = StateMonitor(G, 'v', record=True)\n",
    "\n",
    "run(50*ms)\n",
    "\n",
    "plot(M.t/ms, M.v[0], '-b', label='Neuron 0')\n",
    "plot(M.t/ms, M.v[1], '-g', lw=2, label='Neuron 1')\n",
    "plot(M.t/ms, M.v[2], '-r', lw=2, label='Neuron 1')\n",
    "xlabel('Time (ms)')\n",
    "ylabel('v')\n",
    "legend(loc='best')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As you can see, that's as simple as adding a line ``S.delay = 'j*2*ms'`` so that the synapse from 0 to 1 has a delay of 2 ms, and from 0 to 2 has a delay of 4 ms."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": false,
    "level": 2
   },
   "source": [
    "## More complex connectivity\n",
    "\n",
    "So far, we specified the synaptic connectivity explicitly, but for larger networks this isn't usually possible. For that, we usually want to specify some condition."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "start_scope()\n",
    "\n",
    "N = 10\n",
    "G = NeuronGroup(N, 'v:1')\n",
    "S = Synapses(G, G)\n",
    "S.connect(condition='i!=j', p=0.2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "level": 7
   },
   "source": [
    "Here we've created a dummy neuron group of N neurons and a dummy synapses model that doens't actually do anything just to demonstrate the connectivity. The line ``S.connect(condition='i!=j', p=0.2)`` will connect all pairs of neurons ``i`` and ``j`` with probability 0.2 as long as the condition ``i!=j`` holds. So, how can we see that connectivity? Here's a little function that will let us visualise it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def visualise_connectivity(S):\n",
    "    Ns = len(S.source)\n",
    "    Nt = len(S.target)\n",
    "    figure(figsize=(10, 4))\n",
    "    subplot(121)\n",
    "    plot(zeros(Ns), arange(Ns), 'ok', ms=10)\n",
    "    plot(ones(Nt), arange(Nt), 'ok', ms=10)\n",
    "    for i, j in zip(S.i, S.j):\n",
    "        plot([0, 1], [i, j], '-k')\n",
    "    xticks([0, 1], ['Source', 'Target'])\n",
    "    ylabel('Neuron index')\n",
    "    xlim(-0.1, 1.1)\n",
    "    ylim(-1, max(Ns, Nt))\n",
    "    subplot(122)\n",
    "    plot(S.i, S.j, 'ok')\n",
    "    xlim(-1, Ns)\n",
    "    ylim(-1, Nt)\n",
    "    xlabel('Source neuron index')\n",
    "    ylabel('Target neuron index')\n",
    "    \n",
    "visualise_connectivity(S)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There are two plots here. On the left hand side, you see a vertical line of circles indicating source neurons on the left, and a vertical line indicating target neurons on the right, and a line between two neurons that have a synapse. On the right hand side is another way of visualising the same thing. Here each black dot is a synapse, with x value the source neuron index, and y value the target neuron index.\n",
    "\n",
    "Let's see how these figures change as we change the probability of a connection:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "start_scope()\n",
    "\n",
    "N = 10\n",
    "G = NeuronGroup(N, 'v:1')\n",
    "\n",
    "for p in [0.1, 0.5, 1.0]:\n",
    "    S = Synapses(G, G)\n",
    "    S.connect(condition='i!=j', p=p)\n",
    "    visualise_connectivity(S)\n",
    "    suptitle('p = '+str(p))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And let's see what another connectivity condition looks like. This one will only connect neighbouring neurons."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "start_scope()\n",
    "\n",
    "N = 10\n",
    "G = NeuronGroup(N, 'v:1')\n",
    "\n",
    "S = Synapses(G, G)\n",
    "S.connect(condition='abs(i-j)<4 and i!=j')\n",
    "visualise_connectivity(S)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Try using that cell to see how other connectivity conditions look like."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "level": 7
   },
   "source": [
    "You can also use the generator syntax to create connections like this more efficiently. In small examples like this, it doesn't matter, but for large numbers of neurons it can be much more efficient to specify directly which neurons should be connected than to specify just a condition."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "start_scope()\n",
    "\n",
    "N = 10\n",
    "G = NeuronGroup(N, 'v:1')\n",
    "\n",
    "S = Synapses(G, G)\n",
    "S.connect(j='k for k in range(clip(i-3, 0, N_post), clip(i+4, 0, N_post)) if i!=k')\n",
    "visualise_connectivity(S)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "level": 7
   },
   "source": [
    "If each source neuron is connected to precisely one target neuron, there is a special syntax that is extremely efficient. For example, 1-to-1 connectivity looks like this:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "start_scope()\n",
    "\n",
    "N = 10\n",
    "G = NeuronGroup(N, 'v:1')\n",
    "\n",
    "S = Synapses(G, G)\n",
    "S.connect(j='i')\n",
    "visualise_connectivity(S)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "level": 7
   },
   "source": [
    "You can also do things like specifying the value of weights with a string. Let's see an example where we assign each neuron a spatial location and have a distance-dependent connectivity function. We visualise the weight of a synapse by the size of the marker."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "start_scope()\n",
    "\n",
    "N = 30\n",
    "neuron_spacing = 50*umetre\n",
    "width = N/4.0*neuron_spacing\n",
    "\n",
    "# Neuron has one variable x, its position\n",
    "G = NeuronGroup(N, 'x : metre')\n",
    "G.x = 'i*neuron_spacing'\n",
    "\n",
    "# All synapses are connected (excluding self-connections)\n",
    "S = Synapses(G, G, 'w : 1')\n",
    "S.connect(condition='i!=j')\n",
    "# Weight varies with distance\n",
    "S.w = 'exp(-(x_pre-x_post)**2/(2*width**2))'\n",
    "\n",
    "scatter(G.x[S.i]/um, G.x[S.j]/um, S.w*20)\n",
    "xlabel('Source neuron position (um)')\n",
    "ylabel('Target neuron position (um)')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now try changing that function and seeing how the plot changes.\n",
    "\n",
    "## More complex synapse models: STDP\n",
    "\n",
    "Brian's synapse framework is very general and can do things like short-term plasticity (STP) or spike-timing dependent plasticity (STDP). Let's see how that works for STDP.\n",
    "\n",
    "STDP is normally defined by an equation something like this:\n",
    "\n",
    "$$\\Delta w = \\sum_{t_{pre}} \\sum_{t_{post}} W(t_{post}-t_{pre})$$\n",
    "\n",
    "That is, the change in synaptic weight w is the sum over all presynaptic spike times $t_{pre}$ and postsynaptic spike times $t_{post}$ of some function $W$ of the difference in these spike times. A commonly used function $W$ is:\n",
    "\n",
    "$$W(\\Delta t) = \\begin{cases}\n",
    "A_{pre} e^{-\\Delta t/\\tau_{pre}} & \\Delta t>0 \\\\\n",
    "A_{post}- e^{\\Delta t/\\tau_{pre}} & \\Delta t<0\n",
    "\\end{cases}$$\n",
    "\n",
    "This function looks like this:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "tau_pre = tau_post = 20*ms\n",
    "A_pre = 0.01\n",
    "A_post = -A_pre*1.05\n",
    "delta_t = linspace(-50, 50, 100)*ms\n",
    "W = where(delta_t<0, A_pre*exp(delta_t/tau_pre), A_post*exp(-delta_t/tau_post))\n",
    "plot(delta_t/ms, W)\n",
    "xlabel(r'$\\Delta t$ (ms)')\n",
    "ylabel('W')\n",
    "ylim(-A_post, A_post)\n",
    "axhline(0, ls='-', c='k')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Simulating it directly using this equation though would be very inefficient, because we would have to sum over all pairs of spikes. That would also be physiologically unrealistic because the neuron cannot remember all its previous spike times. It turns out there is a more efficient and physiologically more plausible way to get the same effect.\n",
    "\n",
    "We define two new variables $a_{pre}$ and $a_{post}$ which are \"traces\" of pre- and post-synaptic activity, governed by the differential equations:\n",
    "\n",
    "$$\\begin{eqnarray}\n",
    "\\tau_{pre}\\frac{\\mathrm{d}}{\\mathrm{d}t} a_{pre} &=& -a_{pre}\\\\\n",
    "\\tau_{post}\\frac{\\mathrm{d}}{\\mathrm{d}t} a_{post} &=& -a_{post}\\\\\n",
    "\\end{eqnarray}$$\n",
    "\n",
    "When a presynaptic spike occurs, the presynaptic trace is updated and the weight is modified according to the rule:\n",
    "\n",
    "$$\\begin{eqnarray}\n",
    "a_{pre} &\\rightarrow& a_{pre}+A_{pre}\\\\\n",
    "w &\\rightarrow& w+a_{post}\n",
    "\\end{eqnarray}$$\n",
    "\n",
    "When a postsynaptic spike occurs:\n",
    "\n",
    "$$\\begin{eqnarray}\n",
    "a_{post} &\\rightarrow& a_{post}+A_{post}\\\\\n",
    "w &\\rightarrow& w+a_{pre}\n",
    "\\end{eqnarray}$$\n",
    "\n",
    "To see that this formulation is equivalent, you just have to check that the equations sum linearly, and consider two cases: what happens if the presynaptic spike occurs before the postsynaptic spike, and vice versa. Try drawing a picture of it.\n",
    "\n",
    "Now that we have a formulation that relies only on differential equations and spike events, we can turn that into Brian code."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "start_scope()\n",
    "\n",
    "taupre = taupost = 20*ms\n",
    "wmax = 0.01\n",
    "Apre = 0.01\n",
    "Apost = -Apre*taupre/taupost*1.05\n",
    "\n",
    "G = NeuronGroup(1, 'v:1', threshold='v>1')\n",
    "\n",
    "S = Synapses(G, G,\n",
    "             '''\n",
    "             w : 1\n",
    "             dapre/dt = -apre/taupre : 1 (event-driven)\n",
    "             dapost/dt = -apost/taupost : 1 (event-driven)\n",
    "             ''',\n",
    "             on_pre='''\n",
    "             v_post += w\n",
    "             apre += Apre\n",
    "             w = clip(w+apost, 0, wmax)\n",
    "             ''',\n",
    "             on_post='''\n",
    "             apost += Apost\n",
    "             w = clip(w+apre, 0, wmax)\n",
    "             ''')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There are a few things to see there. Firstly, when defining the synapses we've given a more complicated multi-line string defining three synaptic variables (``w``, ``apre`` and ``apost``). We've also got a new bit of syntax there, ``(event-driven)`` after the definitions of ``apre`` and ``apost``. What this means is that although these two variables evolve continuously over time, Brian should only update them at the time of an event (a spike). This is because we don't need the values of ``apre`` and ``apost`` except at spike times, and it is more efficient to only update them when needed.\n",
    "\n",
    "Next we have a ``on_pre=...`` argument. The first line is ``v_post += w``: this is the line that actually applies the synaptic weight to the target neuron. The second line is ``apre += Apre`` which encodes the rule above. In the third line, we're also encoding the rule above but we've added one extra feature: we've clamped the synaptic weights between a minimum of 0 and a maximum of ``wmax`` so that the weights can't get too large or negative. The function ``clip(x, low, high)`` does this.\n",
    "\n",
    "Finally, we have a ``on_post=...`` argument. This gives the statements to calculate when a post-synaptic neuron fires. Note that we do not modify ``v`` in this case, only the synaptic variables.\n",
    "\n",
    "Now let's see how all the variables behave when a presynaptic spike arrives some time before a postsynaptic spike."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "start_scope()\n",
    "\n",
    "taupre = taupost = 20*ms\n",
    "wmax = 0.01\n",
    "Apre = 0.01\n",
    "Apost = -Apre*taupre/taupost*1.05\n",
    "\n",
    "G = NeuronGroup(2, 'v:1', threshold='t>(1+i)*10*ms', refractory=100*ms)\n",
    "\n",
    "S = Synapses(G, G,\n",
    "             '''\n",
    "             w : 1\n",
    "             dapre/dt = -apre/taupre : 1 (clock-driven)\n",
    "             dapost/dt = -apost/taupost : 1 (clock-driven)\n",
    "             ''',\n",
    "             on_pre='''\n",
    "             v_post += w\n",
    "             apre += Apre\n",
    "             w = clip(w+apost, 0, wmax)\n",
    "             ''',\n",
    "             on_post='''\n",
    "             apost += Apost\n",
    "             w = clip(w+apre, 0, wmax)\n",
    "             ''')\n",
    "S.connect(i=0, j=1)\n",
    "M = StateMonitor(S, ['w', 'apre', 'apost'], record=True)\n",
    "\n",
    "run(30*ms)\n",
    "\n",
    "figure(figsize=(4, 8))\n",
    "subplot(211)\n",
    "plot(M.t/ms, M.apre[0], label='apre')\n",
    "plot(M.t/ms, M.apost[0], label='apost')\n",
    "legend(loc='best')\n",
    "subplot(212)\n",
    "plot(M.t/ms, M.w[0], label='w')\n",
    "legend(loc='best')\n",
    "xlabel('Time (ms)')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A couple of things to note here. First of all, we've used a trick to make neuron 0 fire a spike at time 10 ms, and neuron 1 at time 20 ms. Can you see how that works?\n",
    "\n",
    "Secondly, we've replaced the ``(event-driven)`` by ``(clock-driven)`` so you can see how ``apre`` and ``apost`` evolve over time. Try reverting this change and see what happens.\n",
    "\n",
    "Try changing the times of the spikes to see what happens.\n",
    "\n",
    "Finally, let's verify that this formulation is equivalent to the original one."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "start_scope()\n",
    "\n",
    "taupre = taupost = 20*ms\n",
    "Apre = 0.01\n",
    "Apost = -Apre*taupre/taupost*1.05\n",
    "tmax = 50*ms\n",
    "N = 100\n",
    "\n",
    "# Presynaptic neurons G spike at times from 0 to tmax\n",
    "# Postsynaptic neurons G spike at times from tmax to 0\n",
    "# So difference in spike times will vary from -tmax to +tmax\n",
    "G = NeuronGroup(N, 'tspike:second', threshold='t>tspike', refractory=100*ms)\n",
    "H = NeuronGroup(N, 'tspike:second', threshold='t>tspike', refractory=100*ms)\n",
    "G.tspike = 'i*tmax/(N-1)'\n",
    "H.tspike = '(N-1-i)*tmax/(N-1)'\n",
    "\n",
    "S = Synapses(G, H,\n",
    "             '''\n",
    "             w : 1\n",
    "             dapre/dt = -apre/taupre : 1 (event-driven)\n",
    "             dapost/dt = -apost/taupost : 1 (event-driven)\n",
    "             ''',\n",
    "             on_pre='''\n",
    "             apre += Apre\n",
    "             w = w+apost\n",
    "             ''',\n",
    "             on_post='''\n",
    "             apost += Apost\n",
    "             w = w+apre\n",
    "             ''')\n",
    "S.connect(j='i')\n",
    "\n",
    "run(tmax+1*ms)\n",
    "\n",
    "plot((H.tspike-G.tspike)/ms, S.w)\n",
    "xlabel(r'$\\Delta t$ (ms)')\n",
    "ylabel(r'$\\Delta w$')\n",
    "ylim(-Apost, Apost)\n",
    "axhline(0, ls='-', c='k')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Can you see how this works?\n",
    "\n",
    "## End of tutorial"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
