{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### Quickstart\n", "To run the code below:\n", "\n", "1. Click on the cell to select it.\n", "2. Press `SHIFT+ENTER` on your keyboard or press the play button\n", " () in the toolbar above.\n", "\n", "Feel free to create new cells using the plus button\n", "(), or pressing `SHIFT+ENTER` while this cell\n", "is selected." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Example 3 (Using bisection to find a neuron's voltage threshold)\n", "\n", "This example demonstrate how a control flow, where simulation parameters depend on the results of previous simulations, can be expressed by making use of standard control structures in Python. By having access to the full expressivity of a general purpose programming language, expressing such control flow is straight-forward; this would not be the case for a declarative model description.\n", "\n", "Our goal in this toy example is to find the threshold voltage of neuron as a function of the density of sodium channels.\n", "\n", "We start with the basic setup:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from brian2 import *\n", "\n", "defaultclock.dt = 0.01*ms # small time step for stiff equations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Our model of the neuron is based on the classical model of from Hodgkin and Huxley (1952). Note that this is not actually a model of a neuron, but rather of a (space-clamped) axon. However, to avoid confusion with spatially extended models, we simply use the term \"neuron\" here. In this model, the membrane potential is shifted, i.e. the resting potential is at 0mV:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "El = 10.613*mV\n", "ENa = 115*mV\n", "EK = -12*mV\n", "gl = 0.3*msiemens/cm**2\n", "gK = 36*msiemens/cm**2\n", "gNa_max = 100*msiemens/cm**2\n", "gNa_min = 15*msiemens/cm**2\n", "C = 1*uF/cm**2\n", "\n", "eqs = '''\n", "dv/dt = (gl * (El-v) + gNa * m**3 * h * (ENa-v) + gK * n**4 * (EK-v)) / C : volt\n", "gNa : siemens/meter**2\n", "dm/dt = alpham * (1-m) - betam * m : 1\n", "dn/dt = alphan * (1-n) - betan * n : 1\n", "dh/dt = alphah * (1-h) - betah * h : 1\n", "alpham = (0.1/mV) * (-v+25*mV) / (exp((-v+25*mV) / (10*mV)) - 1)/ms : Hz\n", "betam = 4 * exp(-v/(18*mV))/ms : Hz\n", "alphah = 0.07 * exp(-v/(20*mV))/ms : Hz\n", "betah = 1/(exp((-v+30*mV) / (10*mV)) + 1)/ms : Hz\n", "alphan = (0.01/mV) * (-v+10*mV) / (exp((-v+10*mV) / (10*mV)) - 1)/ms : Hz\n", "betan = 0.125*exp(-v/(80*mV))/ms : Hz\n", "'''" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We simulate 100 neurons at the same time, each of them having a density of sodium channels between 15 and 100 mS/cm²:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "neurons = NeuronGroup(100, eqs, method='rk4', threshold='v>50*mV')\n", "neurons.gNa = 'gNa_min + (gNa_max - gNa_min)*1.0*i/N'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We initialize the state variables to their resting state values, note that the values for $m$, $n$, $h$ depend on the values of $\\alpha_m$, $\\beta_m$, etc. which themselves depend on $v$. The order of the assignments ($v$ is initialized before $m$, $n$, and $h$) therefore matters, something that is naturally expressed by stating initial values as sequential assignments to the state variables. In a declarative approach, this would be potentially ambiguous." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "neurons.v = 0*mV\n", "neurons.m = '1/(1 + betam/alpham)'\n", "neurons.n = '1/(1 + betan/alphan)'\n", "neurons.h = '1/(1 + betah/alphah)'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We record the spiking activity of the neurons and store the current network state so that we can later restore it and run another iteration of our experiment:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "S = SpikeMonitor(neurons)\n", "store()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The algorithm we use here to find the voltage threshold is a simple bisection: we try to find the threshold voltage of a neuron by repeatedly testing values and increasing or decreasing these values depending on whether we observe a spike or not. By continously halving the size of the correction, we quickly converge to a precise estimate.\n", "\n", "We start with the same initial estimate for all segments, 25mV above the resting potential, and the same value for the size of the \"correction step\":" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "v0 = 25*mV*ones(len(neurons))\n", "step = 25*mV" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For later visualization of how the estimates converged towards their final values, we also store the intermediate values of the estimates:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "estimates = np.full((11, len(neurons)), np.nan)*mV\n", "estimates[0, :] = v0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We now run 10 iterations of our algorithm:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for i in range(10):\n", " # Reset to the initial state\n", " restore()\n", " # Set the membrane potential to our threshold estimate\n", " neurons.v = v0\n", " # Run the simulation for 20ms\n", " run(20*ms)\n", " # Decrease the estimates for neurons that spiked\n", " v0[S.count > 0] -= step\n", " # Increase the estimate for neurons that did not spike\n", " v0[S.count == 0] += step\n", " # Reduce step size and store current estimate\n", " step /= 2.0\n", " estimates[i + 1, :] = v0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "After the 10 iteration steps, we plot the results:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from plotly import tools\n", "from plotly.offline import iplot, init_notebook_mode\n", "import plotly.graph_objs as go\n", "\n", "init_notebook_mode(connected=True)\n", "\n", "fig = tools.make_subplots(2, 1, shared_yaxes=True, print_grid=False)\n", "colors = ['#1f77b4', '#ff7f03', '#2ca02c']\n", "examples = [10, 50, 90]\n", "for example, color in zip(examples, colors):\n", " trace = go.Scatter(x=np.arange(11),\n", " y=estimates[:, example] / mV, \n", " name='gNA = %.1fmS/cm$^2$' % (neurons.gNa[example]/(mS/cm**2)),\n", " marker={'color': color, 'size': 10},\n", " mode='markers+lines',\n", " showlegend=False)\n", " fig.append_trace(trace, 1, 1)\n", "\n", "trace = go.Scatter(x=neurons.gNa/(mS/cm**2),\n", " y=v0/mV,\n", " line={'color': 'gray'},\n", " mode='lines',\n", " name='threshold estimate',\n", " showlegend=False)\n", "fig.append_trace(trace, 2, 1)\n", "for idx, (example, color) in enumerate(zip(examples, colors)):\n", " trace = go.Scatter(x=[neurons.gNa[example]/(mS/cm**2)],\n", " y=[estimates[-1, example]/mV],\n", " mode='markers',\n", " marker={'color': color, 'symbol': 'circle', 'size': 15},\n", " showlegend=False,\n", " name='gNA = %.1fmS/cm$^2$' % (neurons.gNa[example]/(mS/cm**2)))\n", " fig.append_trace(trace, 2, 1)\n", "fig['layout'].update(xaxis1={'title': 'iteration'},\n", " xaxis2={'title': 'gNA (mS/cm²)'},\n", " yaxis1={'range': (0, 45),\n", " 'title': 'threshold estimate (mV)'},\n", " yaxis2={'range': (0, 45),\n", " 'title': 'threshold estimate (mV)'})\n", "\n", "iplot(fig)" ] } ], "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.7" } }, "nbformat": 4, "nbformat_minor": 2 }