{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# The Hodgkin-Huxley Model Solver\nSolving a Hodgkin-Huxley system and visualizing the simulation results.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib as mpl\nimport matplotlib.pyplot as plt\nimport neuromodels as nm\nimport numpy as np\nimport seaborn as sns\nfrom matplotlib import gridspec\n\nsns.set(context=\"paper\", style='whitegrid', rc={\"axes.facecolor\": \"0.96\"})"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Initialize the Hodgkin-Huxley system; model parameters can either be\nset in the constructor or accessed as class attributes:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "hh = nm.HodgkinHuxley(V_rest=-65)\nhh.gbar_K = 36"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The simulation parameters needed are the simulation time ``T``, the time\nstep ``dt``, and the input ``stimulus``:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "T = 50.     # Simulation time [ms]\ndt = 0.01   # Time step"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The ``stimulus`` must be provided as either a scalar value;\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "stimulus = 10   # [mV]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "a callable, e.g. a function, with signature ``(t)``;\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "def stimulus(t):\n    return 10 if 10 <= t <= 40 else 0"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "or a ndarray with `shape=(int(T/dt)+1,)`;\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "def generate_stimulus(I_amp, T, dt, t_stim_on, t_stim_off):\n    time = np.arange(0, T + dt, dt)\n    I_stim = np.zeros_like(time)\n    stim_on_ind = int(np.round(t_stim_on / dt))\n    stim_off_ind = int(np.round(t_stim_off / dt))\n    I_stim[stim_on_ind:stim_off_ind] = I_amp\n    return I_stim\n\n\nI_amp = 10          # Input stimulus amplitude [mV]\nt_stim_on = 10      # Time when stimulus is turned on [ms]\nt_stim_off = 40     # Time when stimulus is turned off [ms]\nstimulus = generate_stimulus(I_amp, T, dt, t_stim_on, t_stim_off)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The system is solved by calling the class method ``solve``:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "hh.solve(stimulus, T, dt)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The solutions can be accessed as class attributes:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "t = hh.t\nV = hh.V\nn = hh.n\nm = hh.m\nh = hh.h"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The simulation results can then be plotted:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fig = plt.figure(figsize=(7, 5), tight_layout=True, dpi=300)\ngs = gridspec.GridSpec(3, 1, height_ratios=[4, 4, 1.5])\n\nax = plt.subplot(gs[0])\nplt.plot(t, V)\nplt.ylabel('Voltage (mV)')\nax.set_xticks([])\nax.set_yticks([-70, -20, 30])\n\nax = plt.subplot(gs[1])\nplt.plot(t, n, label='$n$')\nplt.plot(t, m, label='$m$')\nplt.plot(t, h, label='$h$')\nplt.ylabel(\"State\")\nplt.legend(loc='upper right')\nax.set_xticks([])\nax.set_yticks([0.0, 0.5, 1.0])\n\n\nax = plt.subplot(gs[2])\nplt.plot(t, stimulus, 'k')\nplt.xlabel('Time (ms)')\nplt.ylabel('Input (nA)')\nax.set_xticks([0, 10, 25, 40, np.max(t)])\nax.set_yticks([0, np.max(stimulus)])\n\nplt.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.8.6"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}