-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcalibration.py
500 lines (395 loc) · 14.5 KB
/
calibration.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
498
499
500
from numpy import * #ones, cumsum, sum, isscalar
from matplotlib.pylab import *
import pickle
import lib as lb
import storage as st
import tools as tools
from bootstrap_shcr import *
from data_Fullset import SCxL23 as exppar
from tools import *
import tools
gtrG = exppar.gtrG
gtrA = exppar.gtrA
gtrN = exppar.gtrN
# Parameter definitions
# Data is stored here
data = st.dataStorage() # some default parameters defined.
data.dt = 0.05
data.NMDA = False
# Definition of the model.
lb.h.dt = data.dt
NMDA = data.NMDA
model = lb.loadNeuron("ball-2sticks.hoc",axon=False)
# Adding piece of dendritic branch with spine
model.addDend(name="dendShaft",locus="dendA1",L=4.0,D=1.5,ilocus=1)
model.addSpne(locus="dendShaft",ilocus=0.5,L=1.0,D=1.0,Lneck=1.0,Dneck=0.15)
# Temperature of the neuron
lb.h.celsius = model.temperature
model.addDend(name="dendA2",locus=3,L=10.0,D=0.5,ilocus=1)
data.model = model.__dict__
model.soma.L = 20
#model.comp['B1'].L = 200
#model.comp['B1'].diam = 10
print("Neuron Topology:")
print(lb.h.topology())
nsp = 500
dis = btset_cd(cd='DiS',size=nsp)
sis = btset_cd(cd='SiS',size=nsp)
sps = btset_cd(cd='Sp',size=nsp)
dis['Rneck'] = dis['Rneck']#*2
sis['Rneck'] = sis['Rneck']#*2
sps['Rneck'] = sps['Rneck']#*2
iPSDsh = btShInh(size=nsp)
sp = model.spne[0]
sp.L = dis["L"].mean()
sp.diam = dis["D"].mean()
model.AMPAlist = []
model.ncAMPAlist = []
AMPA = lb.h.Exp2Syn(1,sec = model.spne[0])
tau1 = exppar.AMPAtaus[0]
tau2 = exppar.AMPAtaus[1]
AMPA.tau1 = tau1
AMPA.tau2 = tau2
gmax = gtrA*sps['A1'].mean()
stimE=lb.h.NetStim();stimE.number = 1;
NC = lb.h.NetCon(stimE,AMPA,0,0,gmax)
model.AMPAlist.append(AMPA)
model.ncAMPAlist.append(NC)
NC.delay = 10
# NMDA part
gmaxN = gtrN*sps['A1'].mean()
lb.add_NMDAsyns(model, locs=[[0,0.5]], gmax=gmaxN,tau2=20.0)
NMDA = model.NMDAlist[0]
NCN = model.ncNMDAlist[0]
stimN=lb.h.NetStim();stimN.number = 1;
NCN = lb.h.NetCon(stimN,NMDA,0,0,gmaxN)
model.ncNMDAlist[0] = NCN
NMDA.tau1 = exppar.NMDAtaus[0]
NMDA.tau2 = exppar.NMDAtaus[1]
gmaxG = gtrG*dis['A2'].mean()
GABA, NCG = lb.add_GABAsyns(model, locs=[[0,1]], spne=True, gmax=gmaxG,tau1=exppar.GABAtaus[0],tau2=exppar.GABAtaus[1], rev = -80)
inhOutside = exppar.inhOutside
if inhOutside:
gmaxG = gtrG*iPSDsh.mean()
GABA2, NCG2 = lb.add_GABAsynscomp(model, model.comp['dendShaft'], loc = 0.1, gmax=gmaxG,tau1=exppar.GABAtaus[0],tau2=exppar.GABAtaus[1], rev = -80)
trec, vrec = lb.h.Vector(), lb.h.Vector()
gRec, iRec, vspneRec = [], [], []
gNMDA_rec, iNMDA_rec = [], []
trec.record(lb.h._ref_t)
vrec.record(model.soma(0.5)._ref_v)
#n=0
vDendRec = []
vRecn = {}
#For all dendrites
for dend in model.dend:
#Adding vectors for Voltage, and Calcium
vDendRec.append(lb.h.Vector())
# Placing recording at mid-point in the dendritic branch
vDendRec[-1].record(dend(0.5)._ref_v)
vRecn[dend.name()] = vDendRec[-1]
# NO CALCIUM!?!?!
#Probably better to organize them in a dictionary
vDendRec[1].record(model.dend[1](1.0)._ref_v)
# Spine voltage recording stuff
vspneRec.append(lb.h.Vector())
vspneRec.append(lb.h.Vector())
sp = model.spne[0]
vspneRec[0].record(sp(0.5)._ref_v)
sp = model.neck[0]
vspneRec[1].record(sp(0.5)._ref_v)
# Calcim
caDendRec = []
caRecn = {}
sp = model.spne[0]
caDendRec.append(lb.h.Vector())
caDendRec.append(lb.h.Vector())
caDendRec.append(lb.h.Vector())
caDendRec[0].record(sp(0.5)._ref_ica)
caDendRec[1].record(model.NMDAlist[0]._ref_i)
caDendRec[2].record(sp(1.0)._ref_cai)
caRecn['sp ica'] = caDendRec[0]
caRecn['sp iNMDA'] = caDendRec[1]
caRecn['sp Ca'] = caDendRec[2]
vDendEL = []
dend = model.dend[1]
for i in range(10):
vDendEL.append(lb.h.Vector())
vDendEL[-1].record(dend((i+1.0)/10.0)._ref_v)
vDendEL2 = []
dend = model.dend[-1]
for i in range(10):
vDendEL2.append(lb.h.Vector())
vDendEL2[-1].record(dend((i+1.0)/10.0)._ref_v)
iRec.append(lb.h.Vector())
iRec.append(lb.h.Vector())
currentAMPA = iRec[0]
currentGABA = iRec[1]
iRec.append(lb.h.Vector())
currentNMDA = iRec[-1]
currentNMDA.record(NMDA._ref_i)
currentAMPA.record(AMPA._ref_i)
currentGABA.record(GABA._ref_i)
iRec.append(lb.h.Vector())
currentGABA2 = iRec[-1]
currentGABA2.record(GABA2._ref_i)
dendsizeL0 = exppar.dendsizeL0
dendsizeL = max(max(max(dis['Dss'])+5,max(sis['Dss'])+5),150)
neck = model.neck[0]
Rneck = neck.L*1e-6/(neck.diam*1e-6/2.0)**2/pi*neck.Ra/100
dend = model.comp['dendA1prox']
dend.L = exppar.dendsizeL0
dend.diam = exppar.denddiam0
dendA2 = model.comp['dendA2']
dendA1 = model.comp['dendA1']
dendsh = model.comp['dendShaft']
dendA2.L = 1.0
dendsh.diam = exppar.dendDiam
dendA1.diam = exppar.dendDiam
dendA2.diam = exppar.dendDiam
model.RM = exppar.rm
for sec in lb.h.allsec():
sec.Ra = exppar.ra
sec.cm = 1.0
sec.g_pas = 1.0/model.RM
factor = exppar.factor2nddend
model.comp['dendB1'].cm = factor
model.comp['dendB1'].g_pas = factor/model.RM
model.comp['dendB1'].L = exppar.len2nddend
model.comp['dendB1'].diam = 1
rhosp = 1.3
diamneck = sps['meanDneck'].mean()/1e3
dendareaperL = rhosp*(mean(sps['Ah'])+(pi*diamneck))
factor = dendareaperL/(pi*dendA1.diam)
#print(factor)
factorsp = factor
factordd = (pi*dendA1.diam-(pi*diamneck**2/4.0)*rhosp)/(pi*dendA1.diam)
#print(factordd,factordd+factorsp)
factor = factordd+factorsp
factor = exppar.factorspinesdend
dendA2.cm = factor
dendA1.cm = factor
dendA2.g_pas = factor/model.RM
dendA1.g_pas = factor/model.RM
dendA1.nseg = int(dendA1.L//10)+1
dendA2.nseg = int(dendA2.L//10)+1
dendA1.nseg = 10
dendA2.nseg = 10
#print(dendA1.nseg,dendA2.nseg)
lb.init_active(model, axon=False, soma=False, dend=False, dendNa=False,
dendCa=False,spne=True)
diam0 = 0.15
Rneck0 = neck.Ra*sps['Lneck']/(diam0)**2*0.04/pi
# voltage measure is at dend[1](1), so for resistance we take the whole cylinder
Rdendprox = model.comp['dendA1prox'].L*1e-6*model.comp['dendA1prox'].Ra/100/(pi*(model.comp['dendA1prox'].diam*1e-6/2.0)**2)
def simulateSet(model,spn,tG = 500,ton = 50,
toffset = 50,t_stop = 250, EL = -65,
btsr = None,VDCC = array([0.,0,0,0]),
dendrec = True,inhOutside= False, iPSDsh = None):
if btsr is None:
btsr = ones(9)==0
model.E_PAS = EL
model.soma.e_pas = model.E_PAS
for dendp in model.dend:
dendp.e_pas = model.E_PAS
for sp in model.spne:
sp.e_pas = model.E_PAS
dendsh = model.comp['dendShaft']
dendA1 = model.comp['dendA1']
dendA2 = model.comp['dendA2']
neck = model.neck[0]
sp = model.spne[0]
dshL = dendsh.L
dendsizeL = dendsh.L+dendA1.L+dendA2.L
data = column_stack((spn["A1"],spn["A1"],spn["A2"],spn["Rneck"],spn["Dss"],spn["L"],spn["D"],spn["Ln"],spn["AhA0"], spn["meanDneck"]/1e3))
if isnan(data).sum()>0:
raise "There are nans do something about it"
for i in range(9):
if ~btsr[i]:
data[:,i] = data[:,i].mean()
nsp = data.shape[0]
if iPSDsh is None:
iPSDsh = zeros(nsp)
mes = zeros((nsp,9))
me2 = zeros((nsp,11))
me3 = zeros((nsp,10))
iad = 2
vavg = zeros((int(t_stop/lb.h.dt)+iad,7))
vtracs = zeros((int(t_stop/lb.h.dt)+iad,nsp))
Ctracs = zeros((int(t_stop/lb.h.dt)+iad,nsp))
vtracsD = zeros((int(t_stop/lb.h.dt)+iad,nsp))
vtracsS = zeros((int(t_stop/lb.h.dt)+iad,nsp))
for i in arange(nsp):
NC.weight[0] = data[i,0] *gtrA#/2
NCN.weight[0] = data[i,1] *gtrN#*0#*0
NCG.weight[0] = data[i,2] *gtrG#*0
NCG2.weight[0] = iPSDsh[i] *gtrG#*0
# print(NCG2.weight[0], iPSDsh[i],gtrG,i)
if inhOutside:
NCG.weight[0] = 0.0
else:
NCG2.weight[0] = 0.0
neck.L = data[i,7]
Rneck0 = neck.Ra*neck.L/(diam0)**2*0.04/pi
#print(Rneck0,data[i,3])
neck.diam = diam0*sqrt(Rneck0/data[i,3])
# neck.diam = data[i,9]
# Rneck = neck.Ra*neck.L/(neck.diam)**2*0.04/pi
# neck.Ra = data[i,3]*(neck.diam)**2*pi/0.04/neck.L
posD = data[i,4]
dendA1.L = posD-dshL/2
dendA2.L = dendsizeL-posD-dshL/2
# print(NCG.weight[0],NCG2.weight[0])
# A = pi*D**2
sp.L = data[i,5]
sp.diam = data[i,6]
sp.cm = data[i,8]
sp.g_pas = data[i,8]/model.RM
spvol = sp(0.5).volume()
spineArea = sp(0.5).area()#sp.L*sp.diam+1.8*sp.diam**2/4 # um^2
CaTcond = 1e-4# to take pS/um^2 to S/cm^2
sp.pbar_caL13_alt = VDCC[0]*CaTcond#/spineArea
sp.pbar_caL_alt = VDCC[1]*CaTcond#/spineArea
sp.pbar_can_alt = VDCC[2]*CaTcond#/spineArea
sp.pcaqbar_caq_alt = VDCC[3]*CaTcond#/spineArea
NC.delay = toffset+ton-50
NCN.delay = toffset+ton-50
NCG.delay = toffset+tG#toffset+tG#-50
NCG2.delay = toffset+tG#toffset+tG#-50
for il in range(10):
f = 1-7.5*il/dendA1.L
#f = 0.5
if f>=0:
vDendEL[il].record(dendA1(f)._ref_v)
else:
vDendEL[il].record(dendA1(0.0)._ref_v)
for il in range(10):
f = 7.5*il/dendA2.L
#f = 0.5
if f<=1:
vDendEL2[il].record(dendA2(f)._ref_v)
else:
vDendEL2[il].record(dendA2(1.0)._ref_v)
lb.h.finitialize(model.E_PAS)
lb.neuron.run(t_stop)
current = abs((array(vDendRec[0])-array(vrec[0]))/Rdendprox)
vtracs[:,i] = array(vspneRec[0])
if dendrec:
vtracsD[:,i] = array(vDendRec[1])
else:
vtracsD[:,i] = array(vDendEL[2])
vtracsS[:,i] = array(vrec)
vavg[:,0] += array(vspneRec[0])
vavg[:,1] += array(vspneRec[0])**2
vavg[:,2] += array(vDendRec[1])
vavg[:,3] += array(vDendRec[1])**2
vavg[:,4] += array(vrec)
vavg[:,5] += array(vrec)**2
vavg[:,6] += 1
cat = array(caDendRec[-1])/1e-3
Ctracs[:,i] = cat-cat[0]
if inhOutside:
aG = abs(array(currentGABA2)).argmax()
maxGABA = array(currentGABA2)[aG]
else:
aG = abs(array(currentGABA)).argmax()
maxGABA = array(currentGABA)[aG]
#aG = abs(array(currentGABA)).argmax()
aA = abs(array(currentAMPA)).argmax()
aN = abs(array(currentNMDA)).argmax()
mes[i,:] = [data[i,3],max(vspneRec[0])-EL,
max(vDendRec[3])-EL,max(vrec)-EL,
max(cat)-cat[0],maxGABA,
array(currentAMPA)[aA],max(abs(current)),
array(currentNMDA)[aN]]
me2[i,0] = max(vDendRec[3])
me2[i,1:] = [max(vD) for vD in vDendEL]
me3[i,:] = [max(vD) for vD in vDendEL2]
#plot(trec,array(caDendRec[-1])/1e-3)
#ylabel("[Ca] (uM)")
#figure()
#plot(trec,vspneRec[0])
#break
vavg[:,:5] = vavg[:,:5]/vavg[0,6]
vavg[:,1] = sqrt(vavg[:,1]-vavg[:,0]**2)#/sqrt(vavg[0,6])
vavg[:,3] = sqrt(vavg[:,3]-vavg[:,2]**2)#/sqrt(vavg[0,6])
vavg[:,5] = sqrt(vavg[:,5]-vavg[:,4]**2)#/sqrt(vavg[0,6])
return(vavg,mes,vtracs,vtracsD,vtracsS,
Ctracs,me2,me3,array(trec))
VDCCa = array([25., 25., 20., 20., 20.])
EL0 = -70
with open(datasetfile,"rb") as f:
data = pickle.load(f)
spdata = dataset(data,noise=0.0)
#print(spdata['A1'].shape)
spdata['A2'][isnan(spdata['A2'])] = 0.0
#spdata['Rneck'] = spdata['Rneck']
#print(spdata['A2'].shape)
sel = ones(spdata['A2'].shape[0]) == 1
keys = list(spdata.keys())
print(keys)
keys.pop(12)
keys.pop(11)
keys.pop(7)
keys.pop(3)
keys.pop(9)
keys.pop(3)
#print(keys)
for key in keys:
sel = sel*isfinite(spdata[key])
print(key, isfinite(spdata[key]).sum())
for key in spdata.keys():
spdata[key] = spdata[key][sel]
#print(key,':',isfinite(spdata[key]).sum())
dendsizeL = max(max(spdata['Dss'])+5,150)
#print(spdata['A1'].shape)
gtrA0 = gtrA
dendA2 = model.comp['dendA2']
dendA1 = model.comp['dendA1']
spdata['Rmorph'] = spdata['Rneck'].copy()
spdata['Rneck'] = spdata['Rmorph']*2.5
td = 500
gtrA = exppar.gtrA*.96
gtrN = exppar.gtrN*0
res = simulateSet(model,spdata,tG = td,ton = 50,
toffset = 70,t_stop = 500,
EL = EL0,btsr = ones(9)==1,VDCC = VDCCa,
dendrec=False)
vavg,mes,vtracs,vtracsD,vtracsS,Ctracs,me2,me3,_ = res
#with open("../SpineModel/All_baseline_datasetx2.pickle","wb") as f:
# pickle.dump([vavg,mes,vtracs,vtracsD,vtracsS,Ctracs,me2,me3,spdata],f)
gtrN = exppar.gtrN
#plot(spdata['A1'],-mes[:,-3]*1000,'.')
sel = spdata['nPSD']==2.0
#plot(spdata['A1'][sel],-mes[sel,-3]*1000,'.')
i = tools.getintp(-mes[:,-3]*1000,0.68)
print('Spines <uEPSC> = %.2f, (med,cf int 0.68) = (%.2f,%.2f,%.2f)' % (-mes[:,-3].mean()*1e3,i[0],i[1],i[2]))
i = tools.getintp(-mes[sel,-3]*1000,0.68)
print('DiS <uEPSC> = %.2f, (med,cf int 0.68) = (%.2f,%.2f,%.2f)' % (-mes[sel,-3].mean()*1e3,i[0],i[1],i[2]))
i = tools.getintp(mes[:,3],0.68)
print('<uEPSP> = %.2f, (med,cf int 0.68) = (%.2f,%.2f,%.2f)' % (mes[:,3].mean(),i[0],i[1],i[2]))
print('CV and conductance', mes[:,3].std()/mes[:,3].mean(), spdata['A1'].mean()*gtrA*1e3)
print('Spines <uEPSC> = %.2f, (med,cf int 0.68) = (%.2f,%.2f,%.2f)' % (-mes[:,-2].mean()*1e9,i[0],i[1],i[2]))
print('Compare it with measured uEPSCs 58pA divided by 2.8 contacts = {:.3f} '.format(58/2.8))
def obtain_uEPSC(gtrAvalue):
gtrA = gtrAvalue
gtrN = exppar.gtrN*0
res = simulateSet(model,spdata,tG = td,ton = 50,
toffset = 70,t_stop = 500,
EL = EL0,btsr = ones(9)==1,VDCC = VDCCa,
dendrec=False)
vavg,mes,vtracs,vtracsD,vtracsS,Ctracs,me2,me3,_ = res
gtrA = exppar.gtrN
gtrN = exppar.gtrN
#i = getintp(-mes[:,-3]*1000,0.68)
#print('Spines <uEPSC> = %.2f, (med,cf int 0.68) = (%.2f,%.2f,%.2f)' % (-mes[:,-3].mean()*1e3,i[0],i[1],i[2]))
#i = getintp(-mes[sel,-3]*1000,0.68)
#print('DiS <uEPSC> = %.2f, (med,cf int 0.68) = (%.2f,%.2f,%.2f)' % (-mes[sel,-3].mean()*1e3,i[0],i[1],i[2]))
i = tools.getintp(mes[:,3],0.68)
#print('<uEPSP> = %.2f, (med,cf int 0.68) = (%.2f,%.2f,%.2f)' % (mes[:,3].mean(),i[0],i[1],i[2]))
#print('CV and conductance', mes[:,3].std()/mes[:,3].mean(), spdata['A1'].mean()*gtrA*1e3)
#print('Spines <uEPSC> = %.2f, (med,cf int 0.68) = (%.2f,%.2f,%.2f)' % (-mes[:,-2].mean()*1e9,i[0],i[1],i[2]))
#print('Compare it with measured uEPSCs 58pA divided by 2.8 contacts = {:.3f} '.format(58/2.8))
avuEPSCs = mes[:,-2].mean()*1e9
return avuEPSCs
target = 58.0/2.8 #58pA div by 2.8 contacts
print("Minimizacion:", obtain_uEPSC(exppar.gtrA), target)