-
Notifications
You must be signed in to change notification settings - Fork 2
/
run.py
149 lines (133 loc) · 4.03 KB
/
run.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
# $Id: run.py,v 1.56 2012/09/20 14:06:03 samn Exp $
from pyinit import *
from geom import *
# from network import *
# from params import *
import sys
try:
import filt
except:
print("Couldn't import filt routines used in gethilbnqs")
# sets up external inputs
if net.noise:
net.set_noise_inputs(h.tstop) #h.tstop sets duration of inpus for make noise case
# handler for printing out time during simulation run
def fi():
for i in range(0,int(h.tstop),100):
h.cvode.event(i, "print " + str(i))
fih = h.FInitializeHandler(1, fi)
# initialize random # generators of NetStims - forces it at beginning of each sim
def myInitNetStims():
net.init_NetStims()
fihns = h.FInitializeHandler(0, myInitNetStims)
# save LFP with current pyramidal cell voltages
_svNUM = 0
def saveLFPInterm (fbase):
global _svNUM
fout = fbase + "_svNUM_" + str(_svNUM) + "_lfp.vec"
print("time is " , h.t, " saving LFP to " , fout)
net.calc_lfp()
mysvvec(fout,net.vlfp)
net.clear_mem()
_svNUM += 1
# setup events to save LFP intermittently
_svFBase = "./tmp_"
_svINC = 1000
def setSaveLFPEvents ():
global _svNUM
_svNUM = 0
stre = "nrnpython(\"saveLFPInterm(_svFBase)\")"
for tt in range(_svINC,int(h.tstop),_svINC):
h.cvode.event(tt,stre)
h.cvode.event(h.tstop,stre)
# example to save LFP intermittently:
# fisv = h.FInitializeHandler(0, setSaveLFPEvents)
#save vec to fn (fn is path)
def mysvvec(fn,vec):
fp = h.File()
fp.wopen(fn)
if fp.isopen():
vec.vwrite(fp)
fp.close()
else:
print("savevec ERR: couldn't open " + fn)
#run a sim and save data
def minrunsv (simstr,tstop=1200,dt=0.1,savevolt=False):
h.tstop=tstop
h.dt=dt
h.run()
print("saving output data")
net.calc_lfp()
fn = "./data/"+simstr+"_lfp.vec"
mysvvec(fn,net.vlfp)
net.setsnq() # make NQS with spike times
fn = "./data/"+simstr+"_snq.nqs"
net.snq.sv(fn)
if savevolt:
nqv = net.getnqvolt()
nqv.sv('./data/'+simstr+'_nqvolt.nqs')
#read a Vector from file, fn is file-path, vec is a Vector
def myrdvec(fn,vec):
fp=h.File()
fp.ropen(fn)
if not fp.isopen():
print("myrdvec ERRA: Couldn't open " + fn)
return False
vec.vread(fp)
fp.close()
return True
# concat a series of LFPs - fbase is base of filename
def catlfp (fbase,svn):
vlfp, vtmp = h.Vector(), h.Vector()
for i in range(svn):
fin = fbase + "_svNUM_" + str(i) + "_lfp.vec"
if myrdvec(fin,vtmp): vlfp.append(vtmp)
return vlfp
#load data from minrunsv into net.vlfp,net.snq
def loadminrundat(simstr,datadir="./data/",rdvolt=False):
fs = datadir+simstr+"_lfp.vec"
try:
net.vlfp.resize(0)
except:
net.vlfp = h.Vector()
myrdvec(fs,net.vlfp)
fs = datadir+simstr+"_snq.nqs"
try:
h.nqsdel(net.snq)
except:
pass
try:
net.snq=h.NQS(fs)
except:
print("loadminrundat ERRB: couldn't read snq from " + fs)
net.snq.verbose=0 # next, copy snq into vectors so can plot with net.rasterplot
for po in net.cells:
for i in range(len(po.lidvec)):
ID = po.cell[i].id
po.lidvec[i].resize(0)
po.ltimevec[i].resize(0)
if net.snq.select("id",ID):
po.lidvec[i].copy(net.snq.getcol("id"))
po.ltimevec[i].copy(net.snq.getcol("t"))
net.snq.verbose=1
if rdvolt:
try:
h.nqsdel(net.nqv)
except:
pass
fs = datadir+simstr+'_nqvolt.nqs'
try:
net.nqv=h.NQS(fs)
except:
print("loadminrundat ERRC: couldn't read nqvolt from " + fs)
def myrast(spikes,times,sz=12):
if h.g[0] == None:
h.gg()
spikes.mark(h.g[0],times,"O",sz,1,1)
h.g[0].exec_menu("View = plot")
############################
# setup multithreading #
pc = h.ParallelContext() #
pc.nthread(32) #
############################
####################################################################################################