Skip to content

Commit c2250e2

Browse files
authored
Merge pull request #22 from irahulsonkar/master
Test on development
2 parents 78b75f9 + 7445d9f commit c2250e2

5 files changed

Lines changed: 282 additions & 34 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,4 @@ Tutorial/Source/*json
3535
/Tutorial/Source/*_brian.py
3636
*code.gen.*
3737
*_eden.py
38+
**/.ipynb_checkpoints/

Tutorial/Source/HodgkinHuxley.py

Lines changed: 64 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,45 @@
66
class HodgkinHuxley():
77
"""Full Hodgkin-Huxley Model implemented in Python"""
88

9-
C_m = 1.0
10-
"""membrane capacitance, in uF/cm^2"""
11-
12-
g_Na = 120.0
13-
"""Sodium (Na) maximum conductances, in mS/cm^2"""
14-
15-
g_K = 36.0
16-
"""Postassium (K) maximum conductances, in mS/cm^2"""
17-
18-
g_L = 0.3
19-
"""Leak maximum conductances, in mS/cm^2"""
20-
21-
E_Na = 50.0
22-
"""Sodium (Na) Nernst reversal potentials, in mV"""
23-
24-
E_K = -77.0
25-
"""Postassium (K) Nernst reversal potentials, in mV"""
26-
27-
E_L = -54.387
28-
"""Leak Nernst reversal potentials, in mV"""
29-
30-
t = np.arange(0.0, 450.0, 0.01)
31-
""" The time to integrate over """
9+
""" __init__ uses optional arguments """
10+
""" when no argument is passed default values are used """
11+
12+
def __init__(self, C_m=1, g_Na=120, g_K=36, g_L=0.3, E_Na=50, E_K=-77, E_L=-54.387, t_0=0, t_n=450, delta_t=0.01, I_inj_max=0, I_inj_width=0, I_inj_trans=0):
13+
14+
self.C_m = C_m
15+
""" membrane capacitance, in uF/cm^2 """
16+
17+
self.g_Na = g_Na
18+
""" Sodium (Na) maximum conductances, in mS/cm^2 """
19+
20+
self.g_K = g_K
21+
""" Postassium (K) maximum conductances, in mS/cm^2 """
22+
23+
self.g_L = g_L
24+
""" Leak maximum conductances, in mS/cm^2 """
25+
26+
self.E_Na = E_Na
27+
""" Sodium (Na) Nernst reversal potentials, in mV """
28+
29+
self.E_K = E_K
30+
""" Postassium (K) Nernst reversal potentials, in mV """
31+
32+
self.E_L = E_L
33+
""" Leak Nernst reversal potentials, in mV """
34+
35+
self.t = np.arange(t_0, t_n, delta_t)
36+
""" The time to integrate over """
37+
38+
""" Advanced input - injection current (single rectangular pulse only) """
39+
40+
self.I_inj_max = I_inj_max
41+
""" maximum value or amplitude of injection pulse """
42+
43+
self.I_inj_width = I_inj_width
44+
""" duration or width of injection pulse """
45+
46+
self.I_inj_trans = I_inj_trans
47+
""" strart time of injection pulse or tranlation about time axis """
3248

3349
def alpha_m(self, V):
3450
"""Channel gating kinetics. Functions of membrane voltage"""
@@ -98,7 +114,19 @@ def I_inj(self, t):
98114
| step up to 35 uA/cm^2 at t>300
99115
| step down to 0 uA/cm^2 at t>400
100116
"""
101-
return 10*(t>100) - 10*(t>200) + 35*(t>300) - 35*(t>400)
117+
118+
""" running standalone python script """
119+
if __name__ == '__main__':
120+
return 10*(t>100) - 10*(t>200) + 35*(t>300) - 35*(t>400)
121+
122+
#""" running jupyterLab notebook """
123+
#advanced input (if checkbox selected)
124+
elif self.I_inj_width>0:
125+
return self.I_inj_max*(t>self.I_inj_trans) - self.I_inj_max*(t>self.I_inj_trans+self.I_inj_width)
126+
127+
#basic input
128+
else:
129+
return 10*(t>100) - 10*(t>200) + 35*(t>300) - 35*(t>400)
102130

103131
@staticmethod
104132
def dALLdt(X, t, self):
@@ -131,32 +159,34 @@ def Main(self):
131159
ik = self.I_K(V, n)
132160
il = self.I_L(V)
133161

134-
plt.figure()
135-
162+
plt.figure(figsize=[15,10])
163+
136164
ax1 = plt.subplot(4,1,1)
137-
plt.title('Hodgkin-Huxley Neuron')
165+
plt.xlim([np.min(self.t),np.max(self.t)]) #for all subplots
166+
plt.title('Hodgkin-Huxley Neuron', fontsize = 20)
138167
plt.plot(self.t, V, 'k')
139-
plt.ylabel('V (mV)')
168+
plt.ylabel('V (mV)', fontsize = 15)
169+
140170

141171
plt.subplot(4,1,2, sharex = ax1)
142172
plt.plot(self.t, ina, 'c', label='$I_{Na}$')
143173
plt.plot(self.t, ik, 'y', label='$I_{K}$')
144174
plt.plot(self.t, il, 'm', label='$I_{L}$')
145-
plt.ylabel('Current')
146-
plt.legend()
175+
plt.ylabel('Current', fontsize = 15)
176+
plt.legend(bbox_to_anchor=(1.1, 0.5),loc='center right', fontsize = 15, borderaxespad=0)
147177

148178
plt.subplot(4,1,3, sharex = ax1)
149179
plt.plot(self.t, m, 'r', label='m')
150180
plt.plot(self.t, h, 'g', label='h')
151181
plt.plot(self.t, n, 'b', label='n')
152-
plt.ylabel('Gating Value')
153-
plt.legend()
182+
plt.ylabel('Gating Value', fontsize = 15)
183+
plt.legend(bbox_to_anchor=(1.1, 0.5),loc='center right', fontsize = 15, borderaxespad=0)
154184

155185
plt.subplot(4,1,4, sharex = ax1)
156186
i_inj_values = [self.I_inj(t) for t in self.t]
157187
plt.plot(self.t, i_inj_values, 'k')
158-
plt.xlabel('t (ms)')
159-
plt.ylabel('$I_{inj}$ ($\\mu{A}/cm^2$)')
188+
plt.xlabel('t (ms)', fontsize = 15)
189+
plt.ylabel('$I_{inj}$ ($\\mu{A}/cm^2$)', fontsize = 15)
160190
plt.ylim(-1, 40)
161191

162192
plt.tight_layout()

notebook/equivalentCircuit.PNG

18.9 KB
Loading

notebook/notebook.ipynb

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {
6+
"tags": []
7+
},
8+
"source": [
9+
"# Interactive Tutorial for Hodgkin Huxley Model on Juypter Lab"
10+
]
11+
},
12+
{
13+
"cell_type": "markdown",
14+
"metadata": {},
15+
"source": [
16+
"<font size=\"5\">HH Model for Single Neuron</font>"
17+
]
18+
},
19+
{
20+
"cell_type": "markdown",
21+
"metadata": {},
22+
"source": [
23+
"This model relies on a basic equivalence between a biological membrane plus embedded ion channels, and an electrical circuit.\n",
24+
"\n",
25+
"<font size=\"5\">Basic inputs to the Model</font>\n",
26+
"\n",
27+
"1) Membrane capacitance, $\\mu{A}/cm^2$\n",
28+
"2) Maximum Conductances, $mS/cm^2$\n",
29+
"3) Nernst Reverasal Potentials, $mV$\n",
30+
"4) Simulation Parameters (time), $ms$\n",
31+
"\n",
32+
"<img src=\"equivalentCircuit.PNG\" width=\"500\"/>\n"
33+
]
34+
},
35+
{
36+
"cell_type": "markdown",
37+
"metadata": {},
38+
"source": [
39+
"<font size=\"5\">Execute Hodgkin Huxley Model</font>"
40+
]
41+
},
42+
{
43+
"cell_type": "code",
44+
"execution_count": 1,
45+
"metadata": {},
46+
"outputs": [
47+
{
48+
"data": {
49+
"application/vnd.jupyter.widget-view+json": {
50+
"model_id": "0069befeceae4a93a70e238bd4db6770",
51+
"version_major": 2,
52+
"version_minor": 0
53+
},
54+
"text/plain": [
55+
"VBox(children=(HBox(children=(HTML(value=\"<b><font color='blue'>Membrane Capacitance, uF/cm^2</b>\"),)), HBox(c…"
56+
]
57+
},
58+
"metadata": {},
59+
"output_type": "display_data"
60+
},
61+
{
62+
"data": {
63+
"application/vnd.jupyter.widget-view+json": {
64+
"model_id": "f778c8dc95a342e3a889af3c1ce47463",
65+
"version_major": 2,
66+
"version_minor": 0
67+
},
68+
"text/plain": [
69+
"interactive(children=(Checkbox(value=False, description=\"<b><font color='blue'>Advanced Input - Incjection Cur…"
70+
]
71+
},
72+
"metadata": {},
73+
"output_type": "display_data"
74+
}
75+
],
76+
"source": [
77+
"import ipywidgets\n",
78+
"import ui_widget\n",
79+
"from importlib.machinery import SourceFileLoader\n",
80+
"import numpy as np\n",
81+
"import matplotlib.pyplot as plt\n",
82+
" \n",
83+
"# imports the module from the given path\n",
84+
"HHmodel = SourceFileLoader(\"HodgkinHuxley.py\",\"../Tutorial/Source/HodgkinHuxley.py\").load_module()\n",
85+
"\n",
86+
"#function to call python script as a module\n",
87+
"def runHH(C_m, g_Na, g_K, g_L, E_Na, E_K, E_L, t_0, t_n, delta_t, I_inj_max, I_inj_width, I_inj_trans):\n",
88+
" runner = HHmodel.HodgkinHuxley(C_m, g_Na, g_K, g_L, E_Na, E_K, E_L, t_0, t_n, delta_t, I_inj_max, I_inj_width, I_inj_trans)\n",
89+
" runner.Main()\n",
90+
"\n",
91+
"#function to handle checkbox event\n",
92+
"def checkboxEvent(checkboxStatus):\n",
93+
" if checkboxStatus:\n",
94+
" wid_inj=ipywidgets.interact(ui_widget.injectorCurrent,amplidute=ui_widget.slider_amplitude,t_width=ui_widget.slider_width,t_translation=ui_widget.slider_translation);\n",
95+
" display(wid_plotArea)\n",
96+
" else:\n",
97+
" display(wid_plotArea)\n",
98+
"\n",
99+
"#create checkbox widget\n",
100+
"wid_checkbox=ipywidgets.interactive(checkboxEvent,checkboxStatus=ui_widget.cb);\n",
101+
"\n",
102+
"#create plot area widget and interact with HHmodel\n",
103+
"wid_plotArea=ipywidgets.interactive_output(runHH,{'C_m':ui_widget.slider_capacitance,\n",
104+
" 'g_Na':ui_widget.slider_cond_Na, 'g_K':ui_widget.slider_cond_K, 'g_L':ui_widget.slider_cond_L, \n",
105+
" 'E_Na':ui_widget.slider_pot_Na, 'E_K':ui_widget.slider_pot_K, 'E_L':ui_widget.slider_pot_L,\n",
106+
" 't_0':ui_widget.time_start, 't_n':ui_widget.time_end, 'delta_t':ui_widget.time_step, \n",
107+
" 'I_inj_max':ui_widget.slider_amplitude,'I_inj_width':ui_widget.slider_width,'I_inj_trans':ui_widget.slider_translation})\n",
108+
" \n",
109+
"display(ui_widget.basicInputs,wid_checkbox)"
110+
]
111+
},
112+
{
113+
"cell_type": "markdown",
114+
"metadata": {},
115+
"source": [
116+
"<font size=\"5\">Description of the Plots</font>"
117+
]
118+
},
119+
{
120+
"cell_type": "markdown",
121+
"metadata": {},
122+
"source": [
123+
"1) Starting from the bottom, the first (bottom-most) plot shows two currents injected into the cell membrane at times 100ms and 300ms.\n",
124+
"2) The second plot from the bottom shows the activation/inactivation parameters of the ion channels in the neuron. \n",
125+
"3) Third plot from the bottom (the current/time plot) makes this more concrete, showing the influx (negative y-axis) and outflux (positive y-axis) of ions passing through each type of ion channel being modeled.\n",
126+
"4) The top plot, which shows neural membrane voltage activity. The spikes here are called “action potentials” and correspond directly to the current/time plot. "
127+
]
128+
}
129+
],
130+
"metadata": {
131+
"kernelspec": {
132+
"display_name": "Python 3 (ipykernel)",
133+
"language": "python",
134+
"name": "python3"
135+
},
136+
"language_info": {
137+
"codemirror_mode": {
138+
"name": "ipython",
139+
"version": 3
140+
},
141+
"file_extension": ".py",
142+
"mimetype": "text/x-python",
143+
"name": "python",
144+
"nbconvert_exporter": "python",
145+
"pygments_lexer": "ipython3",
146+
"version": "3.9.7"
147+
}
148+
},
149+
"nbformat": 4,
150+
"nbformat_minor": 4
151+
}

notebook/ui_widget.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import ipywidgets
2+
import numpy as np
3+
import pylab as plt
4+
5+
def resetTodefault(_):
6+
slider_capacitance.value = 1
7+
slider_cond_Na.value = 120
8+
slider_cond_K.value = 36
9+
slider_cond_L.value = 0.3
10+
slider_pot_Na.value = 50
11+
slider_pot_K.value = -77
12+
slider_pot_L.value = -54.387
13+
time_start.value = 0
14+
time_end.value = 450
15+
time_step.value = 0.01
16+
slider_amplitude.value = 50
17+
slider_width.value = 0
18+
slider_translation.value = 50
19+
cb.value = False
20+
21+
#function to define injector current parameters and plot
22+
def injectorCurrent(amplidute=10, t_width=100, t_translation=100):
23+
t = np.arange(0, 450, 5) #range for plotting only not used in simulation
24+
for i in t:
25+
I_inj = amplidute*(t>t_translation) - amplidute*(t>t_translation+t_width)
26+
plt.figure()
27+
plt.plot(t, I_inj)
28+
plt.xlabel('t (ms)', fontsize = 15)
29+
plt.ylabel('$I_{inj}$ ($\\mu{A}/cm^2$)')
30+
plt.show()
31+
#display(plt.show(),wid_plotArea)
32+
33+
header_capacitance = ipywidgets.HTML(value=f"<b><font color='blue'>Membrane Capacitance, uF/cm^2</b>")
34+
header_conductance = ipywidgets.HTML(value=f"<b><font color='blue'>Maximum Conductances, mS/cm^2</b>")
35+
header_potential = ipywidgets.HTML(value=f"<b><font color='blue'>Nernst Reverasal Potentials, mV</b>")
36+
header_simTime = ipywidgets.HTML(value=f"<b><font color='blue'>Simulation Time, ms</b>")
37+
slider_capacitance = ipywidgets.FloatSlider(value=1,min=0,max=3,step=0.1,description='Capacitance',readout_format='.1f',continuous_update=False)
38+
slider_cond_Na = ipywidgets.FloatSlider(value=120,min=80,max=160,step=0.1,description='Sodium',readout_format='.1f',continuous_update=False)
39+
slider_cond_K = ipywidgets.FloatSlider(value=36,min=0,max=80,step=0.1,description='Potassium',readout_format='.1f',continuous_update=False)
40+
slider_cond_L = ipywidgets.FloatSlider(value=0.3,min=0,max=1,step=0.1,description='Leak',readout_format='.1f',continuous_update=False)
41+
slider_pot_Na = ipywidgets.FloatSlider(value=50,min=-100,max=100,step=0.1,description='Sodium',readout_format='.1f',continuous_update=False)
42+
slider_pot_K = ipywidgets.FloatSlider(value=-77,min=-100,max=100,step=0.1,description='Potassium',readout_format='.1f',continuous_update=False)
43+
slider_pot_L = ipywidgets.FloatSlider(value=-54.387,min=-100,max=100,step=0.1,description='Leak',readout_format='.1f',continuous_update=False)
44+
time_start = ipywidgets.FloatText(value=0,description='Start Time',disabled=True)
45+
time_end = ipywidgets.FloatText(value=450,description='Total Time',disabled=False)
46+
time_step = ipywidgets.FloatText(value=0.01,description='Time Step',disabled=False)
47+
48+
reset_button = ipywidgets.Button(description="Reset All")
49+
reset_button.on_click(resetTodefault)
50+
51+
h1=ipywidgets.HBox([header_capacitance])
52+
h2=ipywidgets.HBox([slider_capacitance])
53+
h3=ipywidgets.HBox([header_conductance])
54+
h4=ipywidgets.HBox([slider_cond_Na,slider_cond_K,slider_cond_L])
55+
h5=ipywidgets.HBox([header_potential])
56+
h6=ipywidgets.HBox([slider_pot_Na,slider_pot_K,slider_pot_L])
57+
h7=ipywidgets.HBox([header_simTime])
58+
h8=ipywidgets.HBox([time_start,time_end,time_step])
59+
h9=ipywidgets.HBox([reset_button])
60+
basicInputs=ipywidgets.VBox([h1,h2,h3,h4,h5,h6,h7,h8,h9])
61+
62+
#Advanced Inputs
63+
cb=ipywidgets.Checkbox(value=False,description=f"<b><font color='blue'>Advanced Input - Incjection Current</b>",indent=False)
64+
slider_amplitude = ipywidgets.FloatSlider(value=50,min=-100,max=100,step=1,description='Amplitude',readout_format='d',continuous_update=False)
65+
slider_width = ipywidgets.FloatSlider(value=0,min=0,max=500,step=1,description='Width',readout_format='d',continuous_update=False)
66+
slider_translation = ipywidgets.FloatSlider(value=50,min=0,max=250,step=1,description='Start at',readout_format='d',continuous_update=False)

0 commit comments

Comments
 (0)