{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Gap Junctions: Two neuron example\n\nThis script simulates two Hodgkin-Huxley neurons of type ``hh_psc_alpha_gap``\nconnected by a gap junction. Both neurons receive a constant current of\n100.0 pA. The neurons are initialized with different membrane potentials and\nsynchronize over time due to the gap-junction connection.\n\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import nest\nimport matplotlib.pyplot as plt\nimport numpy\n\nnest.ResetKernel()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "First we set the resolution of the simulation, create two neurons and\ncreate a ``voltmeter`` for recording.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "nest.SetKernelStatus({'resolution': 0.05})\n\nneuron = nest.Create('hh_psc_alpha_gap', 2)\n\nvm = nest.Create('voltmeter', params={'interval': 0.1})"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Then we set the constant current input, modify the inital membrane\npotential of one of the neurons and connect the neurons to the ``voltmeter``.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "nest.SetStatus(neuron, {'I_e': 100.})\nnest.SetStatus(neuron[0], {'V_m': -10.})\n\nnest.Connect(vm, neuron, 'all_to_all')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "In order to create the ``gap_junction`` connection we employ the\n``all_to_all`` connection rule: Gap junctions are bidirectional connections,\ntherefore we need to connect `neuron[0]` to `neuron[1]` and `neuron[1]` to\n`neuron[0]`:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "nest.Connect(neuron, neuron,\n             {'rule': 'all_to_all', 'allow_autapses': False},\n             {'synapse_model': 'gap_junction', 'weight': 0.5})"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Finally we start the simulation and plot the membrane potentials of both\nneurons.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "nest.Simulate(351.)\n\nsenders = nest.GetStatus(vm, 'events')[0]['senders']\ntimes = nest.GetStatus(vm, 'events')[0]['times']\nV = nest.GetStatus(vm, 'events')[0]['V_m']\n\nplt.figure(1)\nplt.plot(times[numpy.where(senders == 1)],\n         V[numpy.where(senders == 1)], 'r-')\nplt.plot(times[numpy.where(senders == 2)],\n         V[numpy.where(senders == 2)], 'g-')\nplt.xlabel('time (ms)')\nplt.ylabel('membrane potential (mV)')\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.6.12"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}