{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from neuron import h,gui\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "N_sections = 11\n",
    "h.celsius=22\n",
    "nlist=[]\n",
    "gcci = [h.Section() for i in range(N_sections)]\n",
    "nlist.append(gcci)\n",
    "gccd = [h.Section() for i in range(N_sections)]\n",
    "nlist.append(gccd)\n",
    "gci = [h.Section() for i in range(N_sections)]\n",
    "nlist.append(gci)\n",
    "gcd = [h.Section() for i in range(N_sections)]\n",
    "nlist.append(gcd)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "for i in range(N_sections-1):\n",
    "    gcci[i+1].connect(gcci[i],1,0)\n",
    "    gccd[i+1].connect(gccd[i],1,0)\n",
    "\n",
    "for i in range(N_sections-1):\n",
    "    gci[i+1].connect(gci[i],1,0)\n",
    "for i in range(N_sections-1):\n",
    "    gcd[i+1].connect(gcd[i],1,0) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "for i in nlist:\n",
    "    i[0].diam=10\n",
    "    i[0].L=10\n",
    "    i[0].nseg=3\n",
    "     \n",
    "for i in range(1,N_sections):\n",
    "    for j in nlist:\n",
    "        j[i].nseg=3\n",
    "        j[i].L=20\n",
    "        j[i].Ra=100\n",
    "    gcd[i].diam=2-(i-1)*0.088 #(2-1.2)/9\n",
    "    gci[i].diam=2.7-(i-1)*0.166 #(2.7-1.2)/9  \n",
    "    \n",
    "    gccd[i].diam=2-(i-1)*0.088 #(2-1.2)/9\n",
    "    gcci[i].diam=2.7-(i-1)*0.166 #(2.7-1.2)/9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "\n",
    "for i in range(N_sections):\n",
    "    for j in nlist:\n",
    "        j[i].insert('pas')\n",
    "        j[i].g_pas=0.0002\n",
    "        j[i].insert('kfast')\n",
    "        j[i].insert('nat')\n",
    "        j[i].insert('nadp')\n",
    "        j[i].TotalPump_nadp=0\n",
    " \n",
    "      \n",
    "gcd[0].gbar_nat=4.1\n",
    "gcd[0].gbar_kfast=4.1/80\n",
    "\n",
    "gcd[1].gbar_nat=4.1\n",
    "gcd[1].gbar_kfast=4.1/80\n",
    "\n",
    "gci[0].gbar_nat=2.0\n",
    "gci[0].gbar_kfast=2.0/80\n",
    "\n",
    "gci[1].gbar_nat=2.0\n",
    "gci[1].gbar_kfast=2.0/80\n",
    "\n",
    "gcina=[2,2]\n",
    "gcdna=[4.1,4.1]\n",
    "\n",
    "for i in range(2,N_sections):    \n",
    "    gci[i].gbar_nat=(gci[i-1].gbar_nat)+0.05*(gci[i-1].gbar_nat)\n",
    "    gci[i].gbar_kfast=(gci[i-1].gbar_kfast)+0.05*(gci[i-1].gbar_kfast)\n",
    "    gcina.append(round(gci[i].gbar_nat,5))\n",
    "    \n",
    "    \n",
    "    gcd[i].gbar_nat=(gcd[i-1].gbar_nat)-0.12*(gcd[i-1].gbar_nat)\n",
    "    gcd[i].gbar_kfast=(gcd[i-1].gbar_kfast)-0.12*(gcd[i-1].gbar_kfast)\n",
    "    gcdna.append(round(gcd[i].gbar_nat,5))\n",
    "    \n",
    "avnared=2.2*((2.7+2.8+2.2+4.2)/4) #mM\n",
    "avnablu=2.2*((6.5+6.3+7.2+11)/4) #mM\n",
    "\n",
    "avnaredn=2.2*((2.7+2.8)/2) #mM\n",
    "avnablun=2.2*((6.5+6.3+7.2)/3) #mM"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'hoc.HocObject' object has no attribute 'Ipulse1'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-13-1b9652c07819>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mpulscd\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mh\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mIpulse1\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0.5\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0msec\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mgccd\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      2\u001b[0m \u001b[0mpulscd\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mton\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      3\u001b[0m \u001b[0mpulscd\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtoff\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m15\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      4\u001b[0m \u001b[0mpulscd\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnum\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m20\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      5\u001b[0m \u001b[0mpulscd\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mamp\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;31m#na\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mAttributeError\u001b[0m: 'hoc.HocObject' object has no attribute 'Ipulse1'"
     ]
    }
   ],
   "source": [
    "pulscd=h.Ipulse1(0.5,sec=gccd[0])\n",
    "pulscd.ton=5\n",
    "pulscd.toff=15\n",
    "pulscd.num=20\n",
    "pulscd.amp=1#na\n",
    "\n",
    "pulsci=h.Ipulse1(0.5,sec=gcci[0])\n",
    "pulsci.ton=5\n",
    "pulsci.toff=15\n",
    "pulsci.num=20\n",
    "pulsci.amp=1#na\n",
    "\n",
    "pulsd=h.Ipulse1(0.5,sec=gcd[0])\n",
    "pulsd.ton=5\n",
    "pulsd.toff=15\n",
    "pulsd.num=20\n",
    "pulsd.amp=1 #na\n",
    "\n",
    "pulsi=h.Ipulse1(0.5,sec=gci[0])\n",
    "pulsi.ton=5\n",
    "pulsi.toff=15\n",
    "pulsi.num=20\n",
    "pulsi.amp=1 #na\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "\n",
    "time_h = h.Vector()\n",
    "time_h.record(h._ref_t)\n",
    "\n",
    "inareci=[h.Vector() for i in range(N_sections)]\n",
    "inarecd=[h.Vector() for i in range(N_sections)]\n",
    "\n",
    "vrec_0=h.Vector()\n",
    "vrec_1=h.Vector()\n",
    "vrec_2=h.Vector()\n",
    "vrec_3=h.Vector()\n",
    "vrec_4=h.Vector()\n",
    "vrec_5=h.Vector()\n",
    "\n",
    "vrec_p1=h.Vector()\n",
    "vrec_p2=h.Vector()\n",
    "vrec_p3=h.Vector()\n",
    "\n",
    "vrec_0.record(gcd[0](0.5)._ref_v)\n",
    "vrec_1.record(gci[0](0.5)._ref_v)\n",
    "\n",
    "vrec_p1.record(gccd[0](0.5)._ref_v)\n",
    "\n",
    "vrec_2.record(gcd[1](0.5)._ref_v)\n",
    "vrec_3.record(gci[1](0.5)._ref_v)\n",
    "\n",
    "vrec_p2.record(gccd[1](0.5)._ref_v)\n",
    "\n",
    "vrec_4.record(gcd[10](0.5)._ref_v)\n",
    "vrec_5.record(gci[10](0.5)._ref_v)\n",
    "\n",
    "vrec_p3.record(gccd[10](0.5)._ref_v)\n",
    "\n",
    "for i in range(N_sections):\n",
    "    inarecd[i].record(gcd[i](0.5)._ref_ina)\n",
    "    inareci[i].record(gci[i](0.5)._ref_ina)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "h.tstop =100\n",
    "h.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "\n",
    "plt.suptitle('Vm',fontsize=14)\n",
    "\n",
    "plt.subplot(321).set_title('decreasing pattern')\n",
    "plt.plot(time_h,vrec_0)\n",
    "plt.legend(['Soma'])\n",
    "\n",
    "\n",
    "plt.subplot(322).set_title('increasing pattern')\n",
    "plt.plot(time_h,vrec_1)\n",
    "\n",
    "plt.subplot(323)\n",
    "plt.plot(time_h,vrec_2)\n",
    "plt.legend(['Dend(0)'])\n",
    "\n",
    "plt.subplot(324)\n",
    "plt.plot(time_h,vrec_3)\n",
    "\n",
    "plt.subplot(325)\n",
    "plt.plot(time_h,vrec_4)\n",
    "plt.legend(['Dend(1)'])\n",
    "\n",
    "plt.subplot(326)\n",
    "plt.plot(time_h,vrec_5)\n",
    "\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "connai=[]\n",
    "for i in range(N_sections):\n",
    "   connai.append(round(gci[i](0.5).nai,3))\n",
    "\n",
    "connad=[]\n",
    "for i in range(N_sections):\n",
    "    connad.append(round(gcd[i](0.5).nai,3)) \n",
    "    \n",
    "connacd=[]\n",
    "for i in range(N_sections):\n",
    "    connacd.append(round(gccd[i](0.5).nai,3))\n",
    "    \n",
    "connaci=[]\n",
    "for i in range(N_sections):\n",
    "    connaci.append(round(gcci[i](0.5).nai,3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "plt.subplot(2,2,1).set_title('[Na] in decreasing density')\n",
    "plt.plot(connad,'b')\n",
    "\n",
    "plt.subplot(2,2,2).set_title('gnabar in decreasing pattern')\n",
    "plt.plot(gcdna,'b')\n",
    "\n",
    "plt.subplot(2,2,3).set_title('[Na] in increasing density')\n",
    "plt.plot(connai,'r')\n",
    "\n",
    "plt.subplot(2,2,4).set_title('gnabar in increasing pattern')\n",
    "plt.plot(gcina,'r')\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'inareci' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-7-a5f5685522c0>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mN_sections\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m     \u001b[0minareci\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0masarray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minareci\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      3\u001b[0m     \u001b[0minarecd\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0masarray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minarecd\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      4\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      5\u001b[0m \u001b[0mInabari\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mNameError\u001b[0m: name 'inareci' is not defined"
     ]
    }
   ],
   "source": [
    "for i in range(N_sections):\n",
    "    inareci[i]=np.asarray(inareci[i])\n",
    "    inarecd[i]=np.asarray(inarecd[i])\n",
    "\n",
    "Inabari=[]\n",
    "Inabard=[]\n",
    "for i in range(N_sections):\n",
    "    Inabarii=-1*round(np.mean(inareci[i]),3)\n",
    "    Inabari.append(Inabarii)\n",
    "    Inabardd=-1*round(np.mean(inarecd[i]),3)\n",
    "    Inabard.append(Inabardd)\n",
    "    \n",
    "plt.subplot(211).set_title('Ina,Increasing, mA/cm2')\n",
    "plt.plot(Inabari,'r')\n",
    "\n",
    "plt.subplot(212).set_title('Ina,Decreasing, mA/cm2')\n",
    "plt.plot(Inabard,'b')\n",
    "\n",
    "plt.show()"
   ]
  }
 ],
 "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.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}