{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Introduction to Brian part 1: Neurons"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "All Brian scripts start with the following. If you're trying this notebook out in IPython, you should start by running this cell."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from brian2 import *"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Later we'll do some plotting in the notebook, so we activate inline plotting in the IPython notebook by doing this:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Units system\n",
    "\n",
    "Brian has a system for using quantities with physical dimensions:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "print 20*volt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "All of the basic SI units can be used (volt, amp, etc.) along with all the standard prefixes (m=milli, p=pico, etc.), as well as a few special abbreviations like ``mV`` for millivolt, ``pF`` for picofarad, etc."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "print 1000*amp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "print 1e6*volt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "print 1000*namp"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Also note that combinations of units with work as expected:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "print 10*nA*5*Mohm"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And if you try to do something wrong like adding amps and volts, what happens?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "print 5*amp+10*volt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you haven't see an error message in Python before that can look a bit overwhelming, but it's actually quite simple and it's important to know how to read these because you'll probably see them quite often.\n",
    "\n",
    "You should start at the bottom and work up. The last line gives the error type ``DimensionMismatchError`` along with a more specific message (in this case, you were trying to add together two quantities with different SI units, which is impossible).\n",
    "\n",
    "Working upwards, each of the sections starts with a filename (e.g. ``C:\\Users\\Dan\\...``) with possibly the name of a function, and then a few lines surrounding the line where the error occurred (which is identified with an arrow).\n",
    "\n",
    "The last of these sections shows the place in the function where the error actually happened. The section above it shows the function that called that function, and so on until the first section will be the script that you actually run. This sequence of sections is called a traceback, and is helpful in debugging.\n",
    "\n",
    "If you see a traceback, what you want to do is start at the bottom and scan up the sections until you find your own file because that's most likely where the problem is. (Of course, your code might be correct and Brian may have a bug in which case, please let us know on the email support list.)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## A simple model\n",
    "\n",
    "Let's start by defining a simple neuron model. In Brian, all models are defined by systems of differential equations. Here's a simple example of what that looks like:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "tau = 10*ms\n",
    "eqs = '''\n",
    "dv/dt = (1-v)/tau : 1\n",
    "'''"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In Python, the notation ``'''`` is used to begin and end a multi-line string. So the equations are just a string with one line per equation. The equations are formatted with standard mathematical notation, with one addition. At the end of a line you write ``: unit`` where ``unit`` is the SI unit of that variable.\n",
    "\n",
    "Now let's use this definition to create a neuron."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "G = NeuronGroup(1, eqs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In Brian, you only create groups of neurons, using the class ``NeuronGroup``. The first two arguments when you create one of these objects are the number of neurons (in this case, 1) and the defining differential equations.\n",
    "\n",
    "Let's see what happens if we didn't put the variable ``tau`` in the equation:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "eqs = '''\n",
    "dv/dt = 1-v : 1\n",
    "'''\n",
    "G = NeuronGroup(1, eqs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "An error is raised, but why? The reason is that the differential equation is now dimensionally inconsistent. The left hand side ``dv/dt`` has units of ``1/second`` but the right hand side ``1-v`` is dimensionless. People often find this behaviour of Brian confusing because this sort of equation is very common in mathematics. However, for quantities with physical dimensions it is incorrect because the results would change depending on the unit you measured it in. For time, if you measured it in seconds the same equation would behave differently to how it would if you measured time in milliseconds. To avoid this, we insist that you always specify dimensionally consistent equations.\n",
    "\n",
    "Now let's go back to the good equations and actually run the simulation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "start_scope()\n",
    "\n",
    "tau = 10*ms\n",
    "eqs = '''\n",
    "dv/dt = (1-v)/tau : 1\n",
    "'''\n",
    "\n",
    "G = NeuronGroup(1, eqs)\n",
    "run(100*ms)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "First off, ignore that ``start_scope()`` at the top of the cell. You'll see that in each cell in this tutorial where we run a simulation. All it does is make sure that any Brian objects created before the function is called aren't included in the next run of the simulation.\n",
    "\n",
    "So, what has happened here? Well, the command ``run(100*ms)`` runs the simulation for 100 ms. We can see that this has worked by printing the value of the variable ``v`` before and after the simulation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "start_scope()\n",
    "\n",
    "G = NeuronGroup(1, eqs)\n",
    "print 'Before v =', G.v[0]\n",
    "run(100*ms)\n",
    "print 'After v =', G.v[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "By default, all variables start with the value 0. Since the differential equation is ``dv/dt=(1-v)/tau`` we would expect after a while that ``v`` would tend towards the value 1, which is just what we see. Specifically, we'd expect ``v`` to have the value ``1-exp(-t/tau)``. Let's see if that's right."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "print 'Expected value of v =', 1-exp(-100*ms/tau)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Good news, the simulation gives the value we'd expect!\n",
    "\n",
    "Now let's take a look at a graph of how the variable ``v`` evolves over time."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "start_scope()\n",
    "\n",
    "G = NeuronGroup(1, eqs)\n",
    "M = StateMonitor(G, 'v', record=True)\n",
    "\n",
    "run(30*ms)\n",
    "\n",
    "plot(M.t/ms, M.v[0])\n",
    "xlabel('Time (ms)')\n",
    "ylabel('v')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "This time we only ran the simulation for 30 ms so that we can see the behaviour better. It looks like it's behaving as expected, but let's just check that analytically by plotting the expected behaviour on top."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "start_scope()\n",
    "\n",
    "G = NeuronGroup(1, eqs)\n",
    "M = StateMonitor(G, 'v', record=0)\n",
    "\n",
    "run(30*ms)\n",
    "\n",
    "plot(M.t/ms, M.v[0], '-b', lw=2, label='Brian')\n",
    "plot(M.t/ms, 1-exp(-M.t/tau), '--r', lw=2, label='Analytic')\n",
    "xlabel('Time (ms)')\n",
    "ylabel('v')\n",
    "legend(loc='best')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As you can see, the blue (Brian) and dashed red (analytic solution) lines coincide.\n",
    "\n",
    "In this example, we used the object ``StateMonitor`` object. This is used to record the values of a neuron variable while the simulation runs. The first two arguments are the group to record from, and the variable you want to record from. We also specify ``record=0``. This means that we record all values for neuron 0. We have to specify which neurons we want to record because in large simulations with many neurons it usually uses up too much RAM to record the values of all neurons.\n",
    "\n",
    "Now try modifying the equations and parameters and see what happens in the cell below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "start_scope()\n",
    "\n",
    "tau = 10*ms\n",
    "eqs = '''\n",
    "dv/dt = (sin(2*pi*100*Hz*t)-v)/tau : 1\n",
    "'''\n",
    "\n",
    "G = NeuronGroup(1, eqs, method='euler') # TODO: we shouldn't have to specify euler here\n",
    "M = StateMonitor(G, 'v', record=0)\n",
    "\n",
    "G.v = 5 # initial value\n",
    "\n",
    "run(60*ms)\n",
    "\n",
    "plot(M.t/ms, M.v[0])\n",
    "xlabel('Time (ms)')\n",
    "ylabel('v')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Adding spikes\n",
    "\n",
    "So far we haven't done anything neuronal, just played around with differential equations. Now let's start adding spiking behaviour."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "start_scope()\n",
    "\n",
    "tau = 10*ms\n",
    "eqs = '''\n",
    "dv/dt = (1-v)/tau : 1\n",
    "'''\n",
    "\n",
    "G = NeuronGroup(1, eqs, threshold='v>0.8', reset='v = 0')\n",
    "\n",
    "M = StateMonitor(G, 'v', record=0)\n",
    "run(50*ms)\n",
    "plot(M.t/ms, M.v[0])\n",
    "xlabel('Time (ms)')\n",
    "ylabel('v')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We've added two new keywords to the ``NeuronGroup`` declaration: ``threshold='v>0.8'`` and ``reset='v = 0'``. What this means is that when ``v>1`` we fire a spike, and immediately reset ``v = 0`` after the spike. We can put any expression and series of statements as these strings.\n",
    "\n",
    "As you can see, at the beginning the behaviour is the same as before until ``v`` crosses the threshold ``v>0.8`` at which point you see it reset to 0. You can't see it in this figure, but internally Brian has registered this event as a spike. Let's have a look at that."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "start_scope()\n",
    "\n",
    "G = NeuronGroup(1, eqs, threshold='v>0.8', reset='v = 0')\n",
    "\n",
    "spikemon = SpikeMonitor(G)\n",
    "\n",
    "run(50*ms)\n",
    "\n",
    "print 'Spike times:', spikemon.t[:]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The ``SpikeMonitor`` object takes the group whose spikes you want to record as its argument and stores the spike times in the variable ``t``. Let's plot those spikes on top of the other figure to see that it's getting it right."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "start_scope()\n",
    "\n",
    "G = NeuronGroup(1, eqs, threshold='v>0.8', reset='v = 0')\n",
    "\n",
    "statemon = StateMonitor(G, 'v', record=0)\n",
    "spikemon = SpikeMonitor(G)\n",
    "\n",
    "run(50*ms)\n",
    "\n",
    "plot(statemon.t/ms, statemon.v[0])\n",
    "for t in spikemon.t:\n",
    "    axvline(t/ms, ls='--', c='r', lw=3)\n",
    "xlabel('Time (ms)')\n",
    "ylabel('v')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here we've used the ``axvline`` command from ``matplotlib`` to draw a red, dashed vertical line at the time of each spike recorded by the ``SpikeMonitor``.\n",
    "\n",
    "Now try changing the strings for ``threshold`` and ``reset`` in the cell above to see what happens."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Refractoriness\n",
    "\n",
    "A common feature of neuron models is refractoriness. This means that after the neuron fires a spike it becomes refractory for a certain duration and cannot fire another spike until this period is over. Here's how we do that in Brian."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "start_scope()\n",
    "\n",
    "tau = 10*ms\n",
    "eqs = '''\n",
    "dv/dt = (1-v)/tau : 1 (unless refractory)\n",
    "'''\n",
    "\n",
    "G = NeuronGroup(1, eqs, threshold='v>0.8', reset='v = 0', refractory=5*ms)\n",
    "\n",
    "statemon = StateMonitor(G, 'v', record=0)\n",
    "spikemon = SpikeMonitor(G)\n",
    "\n",
    "run(50*ms)\n",
    "\n",
    "plot(statemon.t/ms, statemon.v[0])\n",
    "for t in spikemon.t:\n",
    "    axvline(t/ms, ls='--', c='r', lw=3)\n",
    "xlabel('Time (ms)')\n",
    "ylabel('v')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As you can see in this figure, after the first spike, ``v`` stays at 0 for around 5 ms before it resumes its normal behaviour. To do this, we've done two things. Firstly, we've added the keyword ``refractory=5*ms`` to the ``NeuronGroup`` declaration. On its own, this only means that the neuron cannot spike in this period (see below), but doesn't change how ``v`` behaves. In order to make ``v`` stay constant during the refractory period, we have to add ``(unless refractory)`` to the end of the definition of ``v`` in the differential equations. What this means is that the differential equation determines the behaviour of ``v`` unless it's refractory in which case it is switched off.\n",
    "\n",
    "Here's what would happen if we didn't include ``(unless refractory)``. Note that we've also decreased the value of ``tau`` and increased the length of the refractory period to make the behaviour clearer."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "start_scope()\n",
    "\n",
    "tau = 5*ms\n",
    "eqs = '''\n",
    "dv/dt = (1-v)/tau : 1\n",
    "'''\n",
    "\n",
    "G = NeuronGroup(1, eqs, threshold='v>0.8', reset='v = 0', refractory=15*ms)\n",
    "\n",
    "statemon = StateMonitor(G, 'v', record=0)\n",
    "spikemon = SpikeMonitor(G)\n",
    "\n",
    "run(50*ms)\n",
    "\n",
    "plot(statemon.t/ms, statemon.v[0])\n",
    "for t in spikemon.t:\n",
    "    axvline(t/ms, ls='--', c='r', lw=3)\n",
    "axhline(0.8, ls=':', c='g', lw=3)\n",
    "xlabel('Time (ms)')\n",
    "ylabel('v')\n",
    "print \"Spike times:\", spikemon.t[:]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "So what's going on here? The behaviour for the first spike is the same: ``v`` rises to 0.8 and then the neuron fires a spike at time 8 ms before immediately resetting to 0. Since the refractory period is now 15 ms this means that the neuron won't be able to spike again until time 8 + 15 = 23 ms. Immediately after the first spike, the value of ``v`` now instantly starts to rise because we didn't specify ``(unless refractory)`` in the definition of ``dv/dt``. However, once it reaches the value 0.8 (the dashed green line) at time roughly 8 ms it doesn't fire a spike even though the threshold is ``v>0.8``. This is because the neuron is still refractory until time 23 ms, at which point it fires a spike.\n",
    "\n",
    "Note that you can do more complicated and interesting things with refractoriness. See the full documentation for more details about how it works."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Multiple neurons\n",
    "\n",
    "So far we've only been working with a single neuron. Let's do something interesting with multiple neurons."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "start_scope()\n",
    "\n",
    "N = 100\n",
    "tau = 10*ms\n",
    "eqs = '''\n",
    "dv/dt = (2-v)/tau : 1\n",
    "'''\n",
    "\n",
    "G = NeuronGroup(N, eqs, threshold='v>1', reset='v=0')\n",
    "G.v = 'rand()'\n",
    "\n",
    "spikemon = SpikeMonitor(G)\n",
    "\n",
    "run(50*ms)\n",
    "\n",
    "plot(spikemon.t/ms, spikemon.i, '.k')\n",
    "xlabel('Time (ms)')\n",
    "ylabel('Neuron index')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This shows a few changes. Firstly, we've got a new variable ``N`` determining the number of neurons. Secondly, we added the statement ``G.v = 'rand()'`` before the run. What this does is initialise each neuron with a different uniform random value between 0 and 1. We've done this just so each neuron will do something a bit different. The other big change is how we plot the data in the end.\n",
    "\n",
    "As well as the variable ``spikemon.t`` with the times of all the spikes, we've also used the variable ``spikemon.i`` which gives the corresponding neuron index for each spike, and plotted a single black dot with time on the x-axis and neuron index on the y-value. This is the standard \"raster plot\" used in neuroscience."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Parameters\n",
    "\n",
    "To make these multiple neurons do something more interesting, let's introduce per-neuron parameters that don't have a differential equation attached to them."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "start_scope()\n",
    "\n",
    "N = 100\n",
    "tau = 10*ms\n",
    "v0_max = 3.\n",
    "duration = 1000*ms\n",
    "\n",
    "eqs = '''\n",
    "dv/dt = (v0-v)/tau : 1 (unless refractory)\n",
    "v0 : 1\n",
    "'''\n",
    "\n",
    "G = NeuronGroup(N, eqs, threshold='v>1', reset='v=0', refractory=5*ms)\n",
    "M = SpikeMonitor(G)\n",
    "\n",
    "G.v0 = 'i*v0_max/(N-1)'\n",
    "\n",
    "run(duration)\n",
    "\n",
    "figure(figsize=(12,4))\n",
    "subplot(121)\n",
    "plot(M.t/ms, M.i, '.k')\n",
    "xlabel('Time (ms)')\n",
    "ylabel('Neuron index')\n",
    "subplot(122)\n",
    "plot(G.v0, M.count/duration)\n",
    "xlabel('v0')\n",
    "ylabel('Firing rate (sp/s)')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The line ``v0 : 1`` declares a new per-neuron parameter ``v0`` with units ``1`` (i.e. dimensionless).\n",
    "\n",
    "The line ``G.v0 = 'i*v0_max/(N-1)'`` initialises the value of v0 for each neuron varying from 0 up to ``v0_max``. The symbol ``i`` when it appears in strings like this refers to the neuron index.\n",
    "\n",
    "So in this example, we're driving the neuron towards the value ``v0`` exponentially, but we fire spikes when ``v`` crosses ``v>1`` it fires a spike and resets. The effect is that the rate at which it fires spikes will be related to the value of ``v0``. For ``v0<1`` it will never fire a spike, and as ``v0`` gets larger it will fire spikes at a higher rate. The right hand plot shows the firing rate as a function of the value of ``v0``. This is the I-f curve of this neuron model.\n",
    "\n",
    "Note that in the plot we've used the ``count`` variable of the ``SpikeMonitor``: this is an array of the number of spikes each neuron in the group fired. Dividing this by the duration of the run gives the firing rate."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Stochastic neurons\n",
    "\n",
    "Often when making models of neurons, we include a random element to model the effect of various forms of neural noise. In Brian, we can do this by using the symbol ``xi`` in differential equations. Strictly speaking, this symbol is a \"stochastic differential\" but you can sort of thinking of it as just a Gaussian random variable with mean 0 and standard deviation 1. We do have to take into account the way stochastic differentials scale with time, which is why we multiply it by ``tau**-0.5`` in the equations below (see a textbook on stochastic differential equations for more details)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "start_scope()\n",
    "\n",
    "N = 100\n",
    "tau = 10*ms\n",
    "v0_max = 3.\n",
    "duration = 1000*ms\n",
    "sigma = 0.2\n",
    "\n",
    "eqs = '''\n",
    "dv/dt = (v0-v)/tau+sigma*xi*tau**-0.5 : 1 (unless refractory)\n",
    "v0 : 1\n",
    "'''\n",
    "\n",
    "G = NeuronGroup(N, eqs, threshold='v>1', reset='v=0', refractory=5*ms)\n",
    "M = SpikeMonitor(G)\n",
    "\n",
    "G.v0 = 'i*v0_max/(N-1)'\n",
    "\n",
    "run(duration)\n",
    "\n",
    "figure(figsize=(12,4))\n",
    "subplot(121)\n",
    "plot(M.t/ms, M.i, '.k')\n",
    "xlabel('Time (ms)')\n",
    "ylabel('Neuron index')\n",
    "subplot(122)\n",
    "plot(G.v0, M.count/duration)\n",
    "xlabel('v0')\n",
    "ylabel('Firing rate (sp/s)')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "That's the same figure as in the previous section but with some noise added. Note how the curve has changed shape: instead of a sharp jump from firing at rate 0 to firing at a positive rate, it now increases in a sigmoidal fashion. This is because no matter how small the driving force the randomness may cause it to fire a spike."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## End of tutorial\n",
    "\n",
    "That's the end of this part of the tutorial. The cell below has another example. See if you can work out what it is doing and why. Try adding a ``StateMonitor`` to record the values of the variables for one of the neurons to help you understand it.\n",
    "\n",
    "You could also try out the things you've learned in this cell.\n",
    "\n",
    "Once you're done with that you can move on to the next tutorial on Synapses."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "start_scope()\n",
    "\n",
    "N = 1000\n",
    "tau = 10*ms\n",
    "vr = -70*mV\n",
    "vt0 = -50*mV\n",
    "delta_vt0 = 5*mV\n",
    "tau_t = 100*ms\n",
    "sigma = 0.5*(vt0-vr)\n",
    "v_drive = 2*(vt0-vr)\n",
    "duration = 100*ms\n",
    "\n",
    "eqs = '''\n",
    "dv/dt = (v_drive+vr-v)/tau + sigma*xi*tau**-0.5 : volt\n",
    "dvt/dt = (vt0-vt)/tau_t : volt\n",
    "'''\n",
    "\n",
    "reset = '''\n",
    "v = vr\n",
    "vt += delta_vt0\n",
    "'''\n",
    "\n",
    "G = NeuronGroup(N, eqs, threshold='v>vt', reset=reset, refractory=5*ms)\n",
    "spikemon = SpikeMonitor(G)\n",
    "\n",
    "G.v = 'rand()*(vt0-vr)+vr'\n",
    "G.vt = vt0\n",
    "\n",
    "run(duration)\n",
    "\n",
    "_ = hist(spikemon.t/ms, 100, histtype='stepfilled', facecolor='k', weights=ones(len(spikemon))/(N*defaultclock.dt))\n",
    "xlabel('Time (ms)')\n",
    "ylabel('Instantaneous firing rate (sp/s)')"
   ]
  }
 ],
 "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.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
