Debian Codegraph @

examples/frompapers/Stimberg_et_al_2019/example_3_bisection.py

raw
1 #!/usr/bin/env python3
2 """
3 Reproduces Figure 4B from Stimberg et al. (2019).
4
5 Same code as in the https://github.com/brian-team/brian2_paper_examples repository, but using matplotlib instead of
6 Plotly for plotting.
7
8 Marcel Stimberg, Romain Brette, Dan FM Goodman (2019)
9 Brian 2, an intuitive and efficient neural simulator eLife 8:e47314
10
11 https://doi.org/10.7554/eLife.47314
12 """
13 from brian2 import *
14
15 defaultclock.dt = 0.01*ms # small time step for stiff equations
16
17 # Our model of the neuron is based on the classical model of from Hodgkin and Huxley (1952). Note that this is not
18 # actually a model of a neuron, but rather of a (space-clamped) axon. However, to avoid confusion with spatially
19 # extended models, we simply use the term "neuron" here. In this model, the membrane potential is shifted, i.e. the
20 # resting potential is at 0mV:
21 El = 10.613*mV
22 ENa = 115*mV
23 EK = -12*mV
24 gl = 0.3*msiemens/cm**2
25 gK = 36*msiemens/cm**2
26 gNa_max = 100*msiemens/cm**2
27 gNa_min = 15*msiemens/cm**2
28 C = 1*uF/cm**2
29
30 eqs = """
31 dv/dt = (gl * (El-v) + gNa * m**3 * h * (ENa-v) + gK * n**4 * (EK-v)) / C : volt
32 gNa : siemens/meter**2 (constant)
33 dm/dt = alpham * (1-m) - betam * m : 1
34 dn/dt = alphan * (1-n) - betan * n : 1
35 dh/dt = alphah * (1-h) - betah * h : 1
36 alpham = (0.1/mV) * 10*mV / exprel((-v+25*mV) / (10*mV))/ms : Hz
37 betam = 4 * exp(-v/(18*mV))/ms : Hz
38 alphah = 0.07 * exp(-v/(20*mV))/ms : Hz
39 betah = 1/(exp((-v+30*mV) / (10*mV)) + 1)/ms : Hz
40 alphan = (0.01/mV) * 10*mV / exprel((-v+10*mV) / (10*mV))/ms : Hz
41 betan = 0.125*exp(-v/(80*mV))/ms : Hz
42 """
43
44 # We simulate 100 neurons at the same time, each of them having a density of sodium channels between 15 and 100 mS/cm²:
45 neurons = NeuronGroup(100, eqs, method="rk4", threshold="v>50*mV", reset="")
46 neurons.gNa = "gNa_min + (gNa_max - gNa_min)*1.0*i/N"
47
48 # We initialize the state variables to their resting state values, note that the values for $m$, $n$, $h$ depend on the
49 # values of $\alpha_m$, $\beta_m$, etc. which themselves depend on $v$. The order of the assignments ($v$ is
50 # initialized before $m$, $n$, and $h$) therefore matters, something that is naturally expressed by stating initial
51 # values as sequential assignments to the state variables. In a declarative approach, this would be potentially
52 # ambiguous.
53
54 neurons.v = 0*mV
55 neurons.m = "1/(1 + betam/alpham)"
56 neurons.n = "1/(1 + betan/alphan)"
57 neurons.h = "1/(1 + betah/alphah)"
58
59 # We record the spiking activity of the neurons and store the current network state so that we can later restore it and
60 # run another iteration of our experiment:
61
62 S = SpikeMonitor(neurons)
63 store()
64
65 # The algorithm we use here to find the voltage threshold is a simple bisection: we try to find the threshold voltage of
66 # a neuron by repeatedly testing values and increasing or decreasing these values depending on whether we observe a
67 # spike or not. By continously halving the size of the correction, we quickly converge to a precise estimate.
68 #
69 # We start with the same initial estimate for all segments, 25mV above the resting potential, and the same value for
70 # the size of the "correction step":
71
72 v0 = 25*mV*np.ones(len(neurons))
73 step = 25*mV
74
75 # For later visualization of how the estimates converged towards their final values, we also store the intermediate values of the estimates:
76 estimates = np.full((11, len(neurons)), np.nan)*mV
77 estimates[0, :] = v0
78
79 # We now run 10 iterations of our algorithm:
80
81 for i in range(10):
82 print(".", end="")
83 # Reset to the initial state
84 restore()
85 # Set the membrane potential to our threshold estimate and update the initial values of the gating variables
86 neurons.v = v0
87 # Note that we do not update the initial values of the gating variables to their steady state values with
88 # respect to v, but rather to the resting potential.
89 # Run the simulation for 20ms
90 run(20*ms)
91 print()
92 # Decrease the estimates for neurons that spiked
93 v0[S.count > 0] -= step
94 # Increase the estimate for neurons that did not spike
95 v0[S.count == 0] += step
96 # Reduce step size and store current estimate
97 step /= 2.0
98 estimates[i + 1, :] = v0
99 print()
100
101 # After the 10 iteration steps, we plot the results:
102 plt.rcParams.update({'axes.spines.top': False,
103 'axes.spines.right': False})
104 fig, (ax1, ax2) = plt.subplots(1, 2, sharey=True, figsize=(8, 3), layout="constrained")
105
106 colors = ["#1f77b4", "#ff7f03", "#2ca02c"]
107 examples = [10, 50, 90]
108 for example, color in zip(examples, colors):
109 ax1.plot(np.arange(11), estimates[:, example] / mV,
110 "o-", mec='white', lw=2, color=color, clip_on=False, ms=10, zorder=100,
111 label=f"gNA = {(neurons.gNa[example]/(mS/cm**2)):.1f}mS/cm$^2$")
112
113 ax2.plot(neurons.gNa/(mS/cm**2), v0/mV, color="gray", lw=2)
114
115 for idx, (example, color) in enumerate(zip(examples, colors)):
116 ax2.plot([neurons.gNa[example]/(mS/cm**2)], [estimates[-1, example]/mV], "o", color=color, mec="white", ms=10,
117 label=f"gNA = {(neurons.gNa[example]/(mS/cm**2)):.1f}mS/cm$^2$")
118
119 ax1.set(title="iteration", ylim=(0, 45), ylabel="threshold estimate (mV)")
120 ax2.set(title="gNA (mS/cm²)")
121
122 plt.show()