Debian Codegraph @

examples/frompapers/Stimberg_et_al_2019/example_3_bisection_standalone.py

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