-
Notifications
You must be signed in to change notification settings - Fork 8
/
initialize.py
498 lines (329 loc) · 11.8 KB
/
initialize.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
# coding: utf-8
import time
import os
import numpy as np
import scipy.io as sio
import eventlet #0.26.0
eventlet.monkey_patch()
from scipy import integrate, signal, sparse, linalg
from threading import Thread
from flask import Flask, render_template, session, request
from flask_socketio import SocketIO, emit, join_room, leave_room, close_room, disconnect
__author__ = 'Jimin Kim'
__authoremail__ = '[email protected]'
__version__ = '3.0.0-beta'
""" Number of Neurons """
N = 279
""" Cell membrane conductance (pS) """
Gc = 0.1
""" Cell Membrane Capacitance """
C = 0.015
""" Gap Junctions (Electrical, 279*279) """
ggap = 1.0
Gg_Static = np.load('Gg.npy')
""" Synaptic connections (Chemical, 279*279) """
gsyn = 1.0
Gs_Static = np.load('Gs.npy')
""" Leakage potential (mV) """
Ec = -35.0
""" Directionality (279*1) """
E = np.load('emask.npy')
E = -48.0 * E
EMat = np.tile(np.reshape(E, N), (N, 1))
""" Synaptic Activity Parameters """
ar = 1.0/1.5 # Synaptic activity's rise time
ad = 5.0/1.5 # Synaptic activity's decay time
B = 0.125 # Width of the sigmoid (mv^-1)
""" Input_Mask/Continuous Transtion """
transit_Mat = np.zeros((2, N))
t_Tracker = 0
Iext = 100000
rate = 0.025
offset = 0.15
""" Connectome Arrays """
Gg_Dynamic = Gg_Static.copy()
Gs_Dynamic = Gs_Static.copy()
""" Data matrix stack size """
stack_Size = 5
init_data_Mat = np.zeros((stack_Size + 50, N))
data_Mat = np.zeros((stack_Size, N))
""" Directory paths for presets """
default_Dir = os.getcwd()
preset_Dir = default_Dir + '/presets'
save_Dir = default_Dir + '/saved_dynamics'
""" Mask transition """
def transit_Mask(input_Array):
global t_Switch, oldMask, newMask, transit_End, Vth_Static
transit_Mat[0,:] = transit_Mat[1,:]
t_Switch = t_Tracker
transit_Mat[1,:] = input_Array
oldMask = transit_Mat[0,:]
newMask = transit_Mat[1,:]
Vth_Static = EffVth_rhs(Iext, newMask)
transit_End = t_Switch + 0.3
print(oldMask, newMask, t_Switch, transit_End)
def update_Mask(old, new, t, tSwitch):
return np.multiply(old, 0.5-0.5*np.tanh((t-tSwitch)/rate)) + np.multiply(new, 0.5+0.5*np.tanh((t-tSwitch)/rate))
""" Ablation """
def modify_Connectome(ablation_Array):
global Vth_Static, Gg_Dynamic, Gs_Dynamic
apply_Col = np.tile(ablation_Array, (N, 1))
apply_Row = np.transpose(apply_Col)
apply_Mat = np.multiply(apply_Col, apply_Row)
Gg_Dynamic = np.multiply(Gg_Static, apply_Mat)
Gs_Dynamic = np.multiply(Gs_Static, apply_Mat)
try:
newMask
except NameError:
EffVth(Gg_Dynamic, Gs_Dynamic)
if np.sum(ablation_Array) != N:
print("Neurons " + str(np.where(ablation_Array == False)[0]) + " are ablated")
else:
print("All Neurons healthy")
print("EffVth Recalculated")
else:
EffVth(Gg_Dynamic, Gs_Dynamic)
Vth_Static = EffVth_rhs(Iext, newMask)
if np.sum(ablation_Array) != N:
print("Neurons " + str(np.where(ablation_Array == False)[0]) + " are ablated")
else:
print("All Neurons healthy")
print("EffVth Recalculated")
print("Vth Recalculated")
""" Efficient V-threshold computation """
def EffVth(Gg, Gs):
Gcmat = np.multiply(Gc, np.eye(N))
EcVec = np.multiply(Ec, np.ones((N, 1)))
M1 = -Gcmat
b1 = np.multiply(Gc, EcVec)
Ggap = np.multiply(ggap, Gg)
Ggapdiag = np.subtract(Ggap, np.diag(np.diag(Ggap)))
Ggapsum = Ggapdiag.sum(axis = 1)
Ggapsummat = sparse.spdiags(Ggapsum, 0, N, N).toarray()
M2 = -np.subtract(Ggapsummat, Ggapdiag)
Gs_ij = np.multiply(gsyn, Gs)
s_eq = round((ar/(ar + 2 * ad)), 4)
sjmat = np.multiply(s_eq, np.ones((N, N)))
S_eq = np.multiply(s_eq, np.ones((N, 1)))
Gsyn = np.multiply(sjmat, Gs_ij)
Gsyndiag = np.subtract(Gsyn, np.diag(np.diag(Gsyn)))
Gsynsum = Gsyndiag.sum(axis = 1)
M3 = -sparse.spdiags(Gsynsum, 0, N, N).toarray()
b3 = np.dot(Gs_ij, np.multiply(s_eq, E))
M = M1 + M2 + M3
global LL, UU, bb
(P, LL, UU) = linalg.lu(M)
bbb = -b1 - b3
bb = np.reshape(bbb, N)
def EffVth_rhs(Iext, InMask):
InputMask = np.multiply(Iext, InMask)
b = np.subtract(bb, InputMask)
Vth = linalg.solve_triangular(UU, linalg.solve_triangular(LL, b, lower = True, check_finite=False), check_finite=False)
return Vth
def voltage_filter(v_vec, vmax, scaler):
filtered = vmax * np.tanh(scaler * np.divide(v_vec, vmax))
return filtered
""" Right hand side """
def membrane_voltageRHS(t, y):
""" Split the incoming values """
Vvec, SVec = np.split(y, 2)
""" Gc(Vi - Ec) """
VsubEc = np.multiply(Gc, (Vvec - Ec))
""" Gg(Vi - Vj) Computation """
Vrep = np.tile(Vvec, (N, 1))
GapCon = np.multiply(Gg_Dynamic, np.subtract(np.transpose(Vrep), Vrep)).sum(axis = 1)
""" Gs*S*(Vi - Ej) Computation """
VsubEj = np.subtract(np.transpose(Vrep), EMat)
SynapCon = np.multiply(np.multiply(Gs_Dynamic, np.tile(SVec, (N, 1))), VsubEj).sum(axis = 1)
global InMask, Vth
if t >= t_Switch and t <= transit_End:
InMask = update_Mask(oldMask, newMask, t, t_Switch + offset)
Vth = EffVth_rhs(Iext, InMask)
else:
InMask = newMask
Vth = Vth_Static
""" ar*(1-Si)*Sigmoid Computation """
SynRise = np.multiply(np.multiply(ar, (np.subtract(1.0, SVec))),
np.reciprocal(1.0 + np.exp(-B*(np.subtract(Vvec, Vth)))))
SynDrop = np.multiply(ad, SVec)
""" Input Mask """
Input = np.multiply(Iext, InMask)
""" dV and dS and merge them back to dydt """
dV = (-(VsubEc + GapCon + SynapCon) + Input)/C
dS = np.subtract(SynRise, SynDrop)
return np.concatenate((dV, dS))
def compute_jacobian(t, y):
Vvec, SVec = np.split(y, 2)
Vrep = np.tile(Vvec, (N, 1))
J1_M1 = -np.multiply(Gc, np.eye(N))
Ggap = np.multiply(ggap, Gg_Dynamic)
Ggapsumdiag = -np.diag(Ggap.sum(axis = 1))
J1_M2 = np.add(Ggap, Ggapsumdiag)
Gsyn = np.multiply(gsyn, Gs_Dynamic)
J1_M3 = np.diag(np.dot(-Gsyn, SVec))
J1 = (J1_M1 + J1_M2 + J1_M3) / C
J2_M4_2 = np.subtract(EMat, np.transpose(Vrep))
J2 = np.multiply(Gsyn, J2_M4_2) / C
global InMask, Vth
if t >= t_Switch and t <= transit_End:
InMask = update_Mask(oldMask, newMask, t, t_Switch + offset)
Vth = EffVth_rhs(Iext, InMask)
else:
InMask = newMask
Vth = Vth_Static
sigmoid_V = np.reciprocal(1.0 + np.exp(-B*(np.subtract(Vvec, Vth))))
J3_1 = np.multiply(ar, 1 - SVec)
J3_2 = np.multiply(B, sigmoid_V)
J3_3 = 1 - sigmoid_V
J3 = np.diag(np.multiply(np.multiply(J3_1, J3_2), J3_3))
J4 = np.diag(np.subtract(np.multiply(-ar, sigmoid_V), ad))
J_row1 = np.hstack((J1, J2))
J_row2 = np.hstack((J3, J4))
J = np.vstack((J_row1, J_row2))
return J
""" Simulation initiator """
def run_Network(t_Delta, atol):
dt = t_Delta
InitCond = 10**(-4)*np.random.normal(0, 0.94, 2*N)
""" Configuring the ODE Solver """
r = integrate.ode(membrane_voltageRHS, compute_jacobian).set_integrator('vode', atol = atol, min_step = dt*1e-6, method = 'bdf')
r.set_initial_value(InitCond, 0)
init_data_Mat[0, :] = InitCond[:N]
global session_Data, oldMask, t_Switch, t_Tracker, transit_End
session_Data = []
try:
newMask
except NameError:
transit_Mask(np.zeros(N))
else:
oldMask = newMask.copy()
t_Switch = 0
transit_End = 0.3
k = 1
while r.successful() and k < stack_Size + 50:
r.integrate(r.t + dt)
data = np.subtract(r.y[:N], Vth)
init_data_Mat[k, :] = voltage_filter(data, 500, 1)
t_Tracker = r.t
k += 1
@socketio.on("continue run", namespace='/test')
def continueRun():
global t_Tracker
k = 0
while r.successful() and k < stack_Size:
r.integrate(r.t + dt)
data = np.subtract(r.y[:N], Vth)
data_Mat[k, :] = voltage_filter(data, 500, 1)
t_Tracker = r.t
k += 1
emit('new data', data_Mat.tolist())
session_Data.append(np.asarray(data_Mat.tolist()))
emit('new data', init_data_Mat[50:, :].tolist())
session_Data.append(np.asarray(init_data_Mat[50:, :].tolist()))
EffVth(Gg_Static, Gs_Static)
class CustomFlask(Flask):
jinja_options = Flask.jinja_options.copy()
jinja_options.update(dict(
block_start_string='<%',
block_end_string='%>',
variable_start_string='%%',
variable_end_string='%%',
comment_start_string='<#',
comment_end_string='#>',
))
app = CustomFlask(__name__)
app.debug = True
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)
thread = None
def background_thread():
while True:
time.sleep(10)
@app.route('/')
def index():
global thread
if thread is None:
thread = Thread(target=background_thread)
thread.start()
return render_template('index.html')
@socketio.on('connect', namespace='/test')
def test_connect():
emit('data loaded', {'chem': open("chem.json").read(), 'gap': open("gap.json").read(), 'count': 0})
emit('list presets', os.listdir(preset_Dir))
@socketio.on('disconnect', namespace='/test')
def test_disconnect():
global t_Tracker, transit_Mat, Gg_Dynamic, Gs_Dynamic, newMask, oldMask, Vth_Static
t_Tracker = 0
transit_Mat = np.zeros((2, N))
oldMask = transit_Mat[0,:]
newMask = transit_Mat[1,:]
Gg_Dynamic = Gg_Static.copy()
Gs_Dynamic = Gs_Static.copy()
EffVth(Gg_Dynamic, Gs_Dynamic)
Vth_Static = EffVth_rhs(Iext, newMask)
if 'session_Data' in globals():
os.chdir(save_Dir)
np.save('saved_dynamics.npy', np.vstack(session_Data))
os.chdir(default_Dir)
print("Session Voltage Dynamics Saved")
print("EffVth Recalculated")
print("Simulation Resetted")
print("Client disconnected")
@socketio.on('startRun', namespace='/test')
def startRun(t_Delta, atol):
run_Network(t_Delta, atol)
@socketio.on('update', namespace='/test')
def update(input_Array):
transit_Mask(np.asarray(input_Array))
@socketio.on('modify connectome', namespace='/test')
def config_connectome(ablation_Array):
modify_Connectome(np.asarray(ablation_Array))
@socketio.on("stop", namespace="/test")
def stopit():
global t_Tracker
t_Tracker = 0
print("Simulation stopped")
@socketio.on("reset", namespace="/test")
def resetit():
global t_Tracker, transit_Mat, Gg_Dynamic, Gs_Dynamic, newMask, oldMask, Vth_Static
t_Tracker = 0
transit_Mat = np.zeros((2, N))
oldMask = transit_Mat[0,:]
newMask = transit_Mat[1,:]
Gg_Dynamic = Gg_Static.copy()
Gs_Dynamic = Gs_Static.copy()
EffVth(Gg_Dynamic, Gs_Dynamic)
Vth_Static = EffVth_rhs(Iext, newMask)
if 'session_Data' in globals():
os.chdir(save_Dir)
np.save('saved_dynamics.npy', np.vstack(session_Data))
os.chdir(default_Dir)
print("Session Voltage Dynamics Saved")
print("EffVth Recalculated")
print("Simulation Resetted")
@socketio.on("save", namespace="/test")
def save(name, json):
os.chdir(preset_Dir)
preset_file = open(name + '.json', "w")
preset_file.write(json)
preset_file.close()
emit('list presets', os.listdir(preset_Dir))
print("preset " + str(name) + " saved")
os.chdir(default_Dir)
@socketio.on("load", namespace="/test")
def load(name):
os.chdir(preset_Dir)
with open(name + '.json', 'r') as preset:
data = preset.read()
emit('file loaded', data)
print("preset " + str(name) + " loaded")
os.chdir(default_Dir)
@socketio.on("delete", namespace="/test")
def delete(name):
os.chdir(preset_Dir)
os.remove(name + '.json')
emit('list presets', os.listdir(preset_Dir))
print("preset " + str(name) + " deleted")
os.chdir(default_Dir)
if __name__ == '__main__':
socketio.run(app, host = '0.0.0.0', port = 5000, use_reloader = False)