Debian Codegraph @

examples/advanced/COBAHH_approximated.py

raw
1 #!/usr/bin/env python3
2 """Hodgkin-Huxley neuron simulation with approximations for gating variable steady-states and time constants
3
4 Follows exercise 4, chapter 2 of Eugene M. Izhikevich: Dynamical Systems in Neuroscience
5
6 Sebastian Schmitt, 2021
7 """
8
9 import argparse
10 from functools import reduce
11 import operator
12
13 import matplotlib.pyplot as plt
14 from cycler import cycler
15 import numpy as np
16
17 from brian2 import run
18 from brian2 import mS, cmeter, ms, mV, uA, uF
19 from brian2 import Equations, NeuronGroup, StateMonitor, TimedArray, defaultclock
20
21
22 def construct_gating_variable_inf_equation(gating_variable):
23 """Construct the voltage-dependent steady-state gating variable equation.
24
25 Approximated by Boltzmann function.
26
27 gating_variable -- gating variable, typically one of "m", "n" and "h"
28 """
29
30 return Equations('xinf = 1/(1+exp((v_half-v)/k)) : 1',
31 xinf=f'{gating_variable}_inf',
32 v_half=f'v_{gating_variable}_half',
33 k=f'k_{gating_variable}')
34
35
36 def construct_gating_variable_tau_equation(gating_variable):
37 """Construct the voltage-dependent gating variable time constant equation.
38
39 Approximated by Gaussian function.
40
41 gating_variable -- gating variable, typically one of "m", "n" and "h"
42 """
43
44 return Equations('tau = c_base + c_amp*exp(-(v_max - v)**2/sigma**2) : second',
45 tau=f'tau_{gating_variable}',
46 c_base=f'c_{gating_variable}_base',
47 c_amp=f'c_{gating_variable}_amp',
48 v_max=f'v_{gating_variable}_max',
49 sigma=f'sigma_{gating_variable}')
50
51
52 def construct_gating_variable_ode(gating_variable):
53 """Construct the ordinary differential equation of the gating variable.
54
55 gating_variable -- gating variable, typically one of "m", "n" and "h"
56 """
57
58 return Equations('dx/dt = (xinf - x)/tau : 1',
59 x=gating_variable,
60 xinf=f'{gating_variable}_inf',
61 tau=f'tau_{gating_variable}')
62
63
64 def construct_neuron_ode():
65 """Construct the ordinary differential equation of the membrane."""
66
67 # conductances
68 g_K_eq = Equations('g_K = g_K_bar*n**4 : siemens/meter**2')
69 g_Na_eq = Equations('g_Na = g_Na_bar*m**3*h : siemens/meter**2')
70
71 # currents
72 I_K_eq = Equations('I_K = g_K*(v - e_K) : ampere/meter**2')
73 I_Na_eq = Equations('I_Na = g_Na*(v - e_Na) : ampere/meter**2')
74 I_L_eq = Equations('I_L = g_L*(v - e_L) : ampere/meter**2')
75
76 # external drive
77 I_ext_eq = Equations('I_ext = I_stim(t) : ampere/meter**2')
78
79 # membrane
80 membrane_eq = Equations('dv/dt = (I_ext - I_K - I_Na - I_L)/C_mem : volt')
81
82 return [g_K_eq, g_Na_eq, I_K_eq, I_Na_eq, I_L_eq, I_ext_eq, membrane_eq]
83
84
85 def plot_tau(ax, parameters):
86 """Plot gating variable time constants as function of membrane potential.
87
88 ax -- matplotlib axes to be plotted on
89 parameters -- dictionary of parameters for gating variable time constant equations
90 """
91
92 tau_group = NeuronGroup(100,
93 Equations('v : volt') +
94 reduce(operator.add, [construct_gating_variable_tau_equation(
95 gv) for gv in ['m', 'n', 'h']]),
96 method='euler', namespace=parameters)
97
98 min_v = -100
99 max_v = 100
100 tau_group.v = np.linspace(min_v, max_v, len(tau_group))*mV
101
102 ax.plot(tau_group.v/mV, tau_group.tau_m/ms, label=r'$\tau_m$')
103 ax.plot(tau_group.v/mV, tau_group.tau_n/ms, label=r'$\tau_n$')
104 ax.plot(tau_group.v/mV, tau_group.tau_h/ms, label=r'$\tau_h$')
105
106 ax.set_xlabel('$v$ (mV)')
107 ax.set_ylabel(r'$\tau$ (ms)')
108 ax.yaxis.set_label_position("right")
109 ax.yaxis.tick_right()
110 ax.legend()
111
112
113 def plot_inf(ax, parameters):
114 """Plot gating variable steady-state values as function of membrane potential.
115
116 ax -- matplotlib axes to be plotted on
117 parameters -- dictionary of parameters for gating variable steady-state equations
118 """
119
120 inf_group = NeuronGroup(100,
121 Equations('v : volt') +
122 reduce(operator.add, [construct_gating_variable_inf_equation(
123 gv) for gv in ['m', 'n', 'h']]),
124 method='euler', namespace=parameters)
125 inf_group.v = np.linspace(-100, 100, len(inf_group))*mV
126
127 ax.plot(inf_group.v/mV, inf_group.m_inf, label=r'$m_\infty$')
128 ax.plot(inf_group.v/mV, inf_group.n_inf, label=r'$n_\infty$')
129 ax.plot(inf_group.v/mV, inf_group.h_inf, label=r'$h_\infty$')
130 ax.set_xlabel('$v$ (mV)')
131 ax.set_ylabel('steady-state activation')
132 ax.yaxis.set_label_position("right")
133 ax.yaxis.tick_right()
134 ax.legend()
135
136
137 def plot_membrane_voltage(ax, statemon):
138 """Plot simulation result: membrane potential.
139
140 ax -- matplotlib axes to be plotted on
141 statemon -- StateMonitor (with v recorded)
142 """
143
144 ax.plot(statemon.t/ms, statemon.v[0]/mV, label='membrane voltage')
145 ax.set_xlabel('$t$ (ms)')
146 ax.set_ylabel('$v$ (mV)')
147 ax.axhline(0, linestyle='dashed')
148 ax.legend()
149
150
151 def plot_gating_variable_activations(ax, statemon):
152 """Plot simulation result: gating variables.
153
154 ax -- matplotlib axes to be plotted on
155 statemon -- StateMonitor (with m, n and h recorded)
156 """
157
158 ax.plot(statemon.t/ms, statemon.m[0], label='$m$')
159 ax.plot(statemon.t/ms, statemon.n[0], label='$n$')
160 ax.plot(statemon.t/ms, statemon.h[0], label='$h$')
161 ax.set_xlabel('$t$ (ms)')
162 ax.set_ylabel('activation')
163 ax.legend()
164
165
166 def plot_conductances(ax, statemon):
167 """Plot simulation result: conductances.
168
169 ax -- matplotlib axes to be plotted on
170 statemon -- StateMonitor (with g_K and g_Na recorded)
171 """
172
173 ax.plot(statemon.t/ms, statemon.g_K[0] / (mS/(cmeter**2)),
174 label=r'$g_\mathregular{K}$')
175
176 ax.plot(statemon.t/ms, statemon.g_Na[0] / (mS/(cmeter**2)),
177 label=r'$g_\mathregular{Na}$')
178
179 ax.set_xlabel('$t$ (ms)')
180 ax.set_ylabel('$g$ (mS/cm$^2$)')
181 ax.legend()
182
183
184 def plot_currents(ax, statemon):
185 """Plot simulation result: currents.
186
187 ax -- matplotlib axes to be plotted on
188 statemon -- StateMonitor (with I_K, I_Na and I_L recorded)
189 """
190
191 ax.plot(statemon.t/ms,
192 statemon.I_K[0] / (uA/(cmeter**2)),
193 label=r'$I_\mathregular{K}$')
194
195 ax.plot(statemon.t/ms, statemon.I_Na[0] / (uA/(cmeter**2)),
196 label=r'$I_\mathregular{Na}$')
197
198 ax.plot(statemon.t/ms, (statemon.I_Na[0] + statemon.I_K[0] +
199 statemon.I_L[0]) / (uA/(cmeter**2)),
200 label=r'$I_\mathregular{Na} + I_\mathregular{K} + I_\mathregular{L}$')
201
202 ax.set_xlabel('$t$ (ms)')
203 ax.set_ylabel(r'I ($\mu$A/cm$^2$)')
204 ax.legend()
205
206
207 def plot_current_stimulus(ax, statemon):
208 """Plot simulation result: external current stimulus.
209
210 ax -- matplotlib axes to be plotted on
211 statemon -- StateMonitor (with I_ext recorded)
212 """
213
214 ax.plot(statemon.t/ms, statemon.I_ext[0] /
215 (uA/(cmeter**2)), label=r'$I_\mathregular{ext}$')
216
217 ax.set_xlabel('$t$ (ms)')
218 ax.set_ylabel(r'I ($\mu$A/cm$^2$)')
219 ax.legend()
220
221
222 def plot_gating_variable_time_constants(ax, statemon):
223 """Plot simulation result: gating variable time constants.
224
225 ax -- matplotlib axes to be plotted on
226 statemon -- StateMonitor (with tau_m, tau_n and tau_h recorded)
227 """
228
229 ax.plot(statemon.t/ms, statemon.tau_m[0]/ms, label=r'$\tau_m$')
230 ax.plot(statemon.t/ms, statemon.tau_n[0]/ms, label=r'$\tau_n$')
231 ax.plot(statemon.t/ms, statemon.tau_h[0]/ms, label=r'$\tau_h$')
232
233 ax.set_xlabel('$t$ (ms)')
234 ax.set_ylabel(r'$\tau$ (ms)')
235 ax.legend()
236
237
238 def run_simulation(parameters):
239 """Run the simulation.
240
241 parameters -- dictionary with parameters
242 """
243
244 equations = []
245 for gating_variable in ["m", "n", "h"]:
246 equations.append(
247 construct_gating_variable_inf_equation(gating_variable))
248 equations.append(
249 construct_gating_variable_tau_equation(gating_variable))
250 equations.append(construct_gating_variable_ode(gating_variable))
251 equations += construct_neuron_ode()
252
253 eqs_HH = reduce(operator.add, equations)
254 group = NeuronGroup(1, eqs_HH, method='euler', namespace=parameters)
255
256 group.v = parameters["v_initial"]
257
258 group.m = parameters["m_initial"]
259 group.n = parameters["n_initial"]
260 group.h = parameters["h_initial"]
261
262 statemon = StateMonitor(group, ['v',
263 'I_ext',
264 'm', 'n', 'h',
265 'g_K', 'g_Na',
266 'I_K', 'I_Na', 'I_L',
267 'tau_m', 'tau_n', 'tau_h'],
268 record=True)
269
270 defaultclock.dt = parameters["defaultclock_dt"]
271 run(parameters["duration"])
272
273 return statemon
274
275
276 def main(parameters):
277 """Run simulation and return matplotlib figure.
278
279 parameters -- dictionary with parameters
280 """
281
282 statemon = run_simulation(parameters)
283
284 fig = plt.figure(figsize=(20, 15), constrained_layout=True)
285 gs = fig.add_gridspec(6, 2)
286
287 ax0 = fig.add_subplot(gs[0, 0])
288 ax1 = fig.add_subplot(gs[1, 0])
289 ax2 = fig.add_subplot(gs[2, 0])
290 ax3 = fig.add_subplot(gs[3, 0])
291 ax4 = fig.add_subplot(gs[4, 0])
292 ax5 = fig.add_subplot(gs[5, 0])
293 ax6 = fig.add_subplot(gs[:3, 1])
294 ax7 = fig.add_subplot(gs[3:, 1])
295
296 plot_membrane_voltage(ax0, statemon)
297 plot_gating_variable_activations(ax1, statemon)
298 plot_conductances(ax2, statemon)
299 plot_currents(ax3, statemon)
300 plot_current_stimulus(ax4, statemon)
301 plot_gating_variable_time_constants(ax5, statemon)
302
303 plot_tau(ax6, parameters)
304 plot_inf(ax7, parameters)
305
306 return fig
307
308
309 parameters = {
310
311 # Boltzmann function parameters
312 'v_n_half': 12*mV,
313 'v_m_half': 25*mV,
314 'v_h_half': 3*mV,
315
316 'k_n': 15*mV,
317 'k_m': 9*mV,
318 'k_h': -7*mV,
319
320 # Gaussian function parameters
321 'v_n_max': -14*mV,
322 'v_m_max': 27*mV,
323 'v_h_max': -2*mV,
324
325 'sigma_n': 50*mV,
326 'sigma_m': 30*mV,
327 'sigma_h': 20*mV,
328
329 'c_n_amp': 4.7*ms,
330 'c_m_amp': 0.46*ms,
331 'c_h_amp': 7.4*ms,
332
333 'c_n_base': 1.1*ms,
334 'c_m_base': 0.04*ms,
335 'c_h_base': 1.2*ms,
336
337 # conductances
338 'g_K_bar': 36*mS / (cmeter**2),
339 'g_Na_bar': 120*mS / (cmeter**2),
340 'g_L': 0.3*mS / (cmeter**2),
341
342 # reversal potentials
343 'e_K': -12*mV,
344 'e_Na': 120*mV,
345 'e_L': 10.6*mV,
346
347 # membrane capacitance
348 'C_mem': 1*uF / cmeter**2,
349
350 # initial membrane voltage
351 'v_initial': 0*mV,
352
353 # initial gating variable activations
354 'm_initial': 0.05,
355 'n_initial': 0.32,
356 'h_initial': 0.60,
357
358 # external stimulus at 2 ms with 4 uA/cm^2 and at 10 ms with 15 uA/cm^2
359 # for 0.5 ms each
360 'I_stim': TimedArray(values=([0]*4+[4]+[0]*15+[15]+[0])*uA/(cmeter**2),
361 dt=0.5*ms),
362
363 # simulation time step
364 'defaultclock_dt': 0.01*ms,
365
366 # simulation duration
367 'duration': 20*ms
368 }
369
370 linestyle_cycler = cycler('linestyle',['-','--',':','-.'])
371 plt.rc('axes', prop_cycle=linestyle_cycler)
372
373 fig = main(parameters)
374
375 plt.show()