-
Notifications
You must be signed in to change notification settings - Fork 0
/
e4070_library.py
289 lines (240 loc) · 7.33 KB
/
e4070_library.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import numpy as np
import sys
import pandas as pd
import copy
import matplotlib.pyplot as plt
def LD_to_DL(LD):
return dict(zip(LD[0],zip(*[d.values() for d in LD])))
def zo(x):
return np.minimum(np.maximum(0., x), 1.)
""" I/O Conversion Functions
--------
The following functions handle the conversion of numpy arrays into lists and back.
This behavior is useful if you are going to be primarily using numpy in your code.
For behavior consistent with the docstring replace these with identity functions
as shown.
"""
def get_columns(x):
return [column.T for column in x.T]
# return x # For default docstring behavior
def return_columns(x):
return np.vstack(x).T
# return x # For default docstring behavior
""" State and Input Name Dictionaries
--------
The following dictionaries are helpful for building a simulator and keeping track of the ordering of the variables.
"""
SN = {} # State name dictionary
SN['HH_step'] = ['V', 'n', 'm', 'h', 'spike', 'Vprev1', 'Vprev2']
SN['CS_step'] = ['V', 'n', 'm', 'h', 'a', 'b', 'spike', 'Vprev1', 'Vprev2']
SN['ML_step'] = ['V', 'R', 'spike', 'Vprev1', 'Vprev2']
SN['IonotropicSynapse_step'] = ['g_syn', 'x1']
SN['MetabotropicSynapse_step'] = ['g_syn', 'x1', 'x2']
SN['Dendrite_step'] = ['I']
SN['ConstantCurrent_step'] = ['I']
IN = {} # Input name dictionary
IN['HH_step'] = ['I']
IN['CS_step'] = ['I']
IN['ML_step'] = ['I']
IN['IonotropicSynapse_step'] = ['spike']
IN['MetabotropicSynapse_step'] = ['spike']
IN['Dendrite_step'] = ['g_syn', 'V']
IN['ConstantCurrent_step'] = ['I']
""" Model Step Functions
--------
Step functions for various models.
"""
def HH_step(dt, I, x):
"""Step function for a Hodgkin-Huxley neuron
Parameters
--------
dt: float
Time step in seconds
I: float
Injected current
x: list
State list, containing:
V: float
Current voltage
n: float
Current state variable value
m: float
Current state variable value
h: float
Current state variable value
spike: float
Current binary spike state
Vprev1: float
Voltage 1 time step ago
Vprev2: float
Voltage 2 time steps ago
Returns
-------
x: list
Updated state list.
"""
dt = dt * 1000.
I = get_columns(I)[0]
V, n, m, h, spike, Vprev1, Vprev2 = get_columns(x)
n = zo(n)
m = zo(m)
h = zo(h)
E_K = -77
E_Na = 50
E_L = -54.387
gmax_K = 36
gmax_Na = 120
g_L = 0.3
an = 0.01*(V+55)/(1-np.exp(-0.1*(V+55)))
bn = 0.125*np.exp(-(V+65)/80)
am = 0.1*(V+40)/(1-np.exp(-0.1*(V+40)))
bm = 4*np.exp(-(V+65)/18)
ah = 0.07*np.exp(-0.05*(V+65))
bh = 1/(1+np.exp(-0.1*(V+35)))
# Update internal state variables
dn = (an*(1.-n)-bn*n)
dm = (am*(1.-m)-bm*m)
dh = (ah*(1.-h)-bh*h)
'''
dm[np.isnan(dm)] = 0.
dn[np.isnan(dn)] = 0.
dh[np.isnan(dh)] = 0.
'''
# Calculate the memconductances
g_K = gmax_K*(n**4.)
g_Na = gmax_Na*(m**3.)*h
# Update the ionic currents: and membrane voltage:
I_K = g_K*(V-E_K)
I_Na = g_Na*(V-E_Na)
I_L = g_L*(V-E_L)
# Calculate the gradients
dV = I-I_K-I_Na-I_L
dV[np.isnan(dV)] = 0.
spike = (Vprev2<=Vprev1) * (Vprev1 >= V) * (Vprev1 > -30)
x = [V+dt*dV, n+dt*dn, m+dt*dm, h+dt*dh, spike, V+dt*dV, Vprev1]
return return_columns(x)
def CS_step(dt, I, x):
"""Step function for a Connor-Stevens neuron
Parameters
--------
dt: float
Time step in seconds
I: float
Injected current
x: list
State list, containing:
V: float
Current voltage
n: float
Current state variable value
m: float
Current state variable value
h: float
Current state variable value
a: float
Current state variable value
b: float
Current state variable value
spike: float
Current binary spike state
Vprev1: float
Voltage 1 time step ago
Vprev2: float
Voltage 2 time steps ago
Returns
-------
x: list
Updated state list.
"""
dt = dt * 1000.
I = get_columns(I)[0]
V, n, m, h, a, b, spike, Vprev1, Vprev2 = get_columns(x)
n = zo(n)
m = zo(m)
h = zo(h)
a = zo(a)
b = zo(b)
E_A = -75
E_Na = 55
E_K = -72
E_L = -17
gmax_A = 47.7
gmax_Na = 120
gmax_K = 20
gmax_L = 0.3
m_s = -5.3
n_s = -4.3
h_s = -12
alpha_m = (0.1 * (V + 35. + m_s)) / (1. - np.exp(-(V + 35. + m_s) / 10.))
alpha_n = (0.01 * (V + 50. + n_s)) / (1. - np.exp(-0.1 * (V + 50. + n_s)))
alpha_h = 0.07 * np.exp(-0.05 * (V + 60. + h_s))
beta_m = 4. * np.exp(-(V + 60. + m_s) / 18.)
beta_n = 0.125 * np.exp(-(V + 60. + n_s) / 80.)
beta_h = 1. / (1. + np.exp(-(V + 30. + h_s / 10.)))
m_inf = alpha_m / (alpha_m + beta_m)
n_inf = alpha_n / (alpha_n + beta_n)
h_inf = alpha_h / (alpha_h + beta_h)
tau_m = 1. / (3.8 * (alpha_m + beta_m))
tau_n = 2. / (3.8 * (alpha_n + beta_n))
tau_h = 1. / (3.8 * (alpha_h + beta_h))
a_inf = (0.0761 * np.exp((V + 94.22) / 31.84)/(1. + np.exp((V + 1.17) / 28.93))) ** (1. / 3.)
b_inf = (1. / (1. + np.exp((V + 53.3) / 14.54))) ** 4.
tau_a = 0.3632 + 1.158 / (1. + np.exp((V + 55.96) / 20.12))
tau_b = 1.24 + 2.678 / (1. + np.exp((V + 50.) / 16.027))
# Update internal state variables
da = (a_inf - a) / tau_a
db = (b_inf - b) / tau_b
dm = (m_inf - m) / tau_m
dn = (n_inf - n) / tau_n
dh = (h_inf - h) / tau_h
'''
da[np.isnan(da)] = 0.
db[np.isnan(db)] = 0.
dm[np.isnan(dm)] = 0.
dn[np.isnan(dn)] = 0.
dh[np.isnan(dh)] = 0.
'''
g_A = gmax_A * (a ** 3.) * b
g_Na = gmax_Na * (m ** 3.) * h
g_K = gmax_K * (n ** 4.)
g_L = gmax_L
I_A = g_A * (V - E_A)
I_Na = g_Na * (V - E_Na)
I_K = g_K * (V - E_K)
I_L = g_L * (V - E_L)
dV = I - I_K - I_Na - I_L
dV[np.isnan(dV)] = 0.
spike = (Vprev2 <= Vprev1) * (Vprev1 >= V) * (Vprev1 > -30)
x = [V + dt * dV, n + dt * dn, m + dt * dm, h + dt * dh, a + dt * da, b + dt * db, spike, V + dt * dV, Vprev1]
return return_columns(x)
def ConstantCurrent_step(dt, I, x, value = 0.0):
"""Step function for a constant current injector
Parameters
--------
dt: float
Time step in seconds
I: empty list
x: empty list
value: float
Constant value
Returns
-------
x: list
Updated state list.
"""
I = get_columns(I)[0]
x = [value + I]
return return_columns(x)
n_steps = 10000
dt = 1e-5
time = np.zeros((n_steps))
output = np.zeros((n_steps))
CS = np.zeros(9)
for i in range(n_steps):
time[i] = i * dt
constant_current = ConstantCurrent_step(dt, np.zeros(n_steps), np.zeros(n_steps), value = 20)
CS = CS_step(dt, constant_current, CS)
output[i] = CS[0][0]
fig, ax = plt.subplots()
ax.plot(time, output)
plt.show()