/
PySim.py
68 lines (56 loc) · 2.07 KB
/
PySim.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
# Interpreter prefix added automatically by target-side NeuronPythonPrep.m
# since it is machine-dependent.
# Produces time and voltage data files that are processed externally.
def pythonsim(simid, rundir, inputdir, modeldir, outputdir, currentstr, vinitstr, delaystr, stimdurationstr, timestepstr, simstopstr, recordintervalstr):
import neuron
from neuron import h
Section = h.Section
# All the mods are in the simulator directory
# Apparently this is already done by the import, if we use the "-dll"
# option in the call to nrniv. Since the import behavior seems to be
# inconsistent without the "-dll" option, we do it this way.
#neuron.load_mechanisms(".") # SO NOT USING LOAD_MECHANISMS HERE
# local auto-modified version for no gui and control over certain ion
# channels is in the run dir
h.xopen(rundir + '/' + simid + '.hoc')
timefilepath = outputdir + '/' + 'timedata.txt'
timefile = h.File()
timefile.wopen(timefilepath, 'w')
voltagefilepath = outputdir + '/' + 'voltagedata.txt'
voltagefile = h.File()
voltagefile.wopen(voltagefilepath, 'w')
current = float(currentstr)
vinit = float(vinitstr)
delay = float(delaystr)
stimduration = float(stimdurationstr)
timestep = float(timestepstr)
simstop = float(simstopstr)
recordinterval = float(recordintervalstr)
# ----- Current Injection
stim = h.IClamp(0.5, sec=h.soma)
stim.amp = current # nA
stim.delay = delay # msec
stim.dur = stimduration # msec
# ----- Simulation Control (now mostly done through input arguments)
h.dt = timestep
# Preallocate record vectors for speed
# Requires recordinterval to be an exact multiple of tstep.
recordlength = simstop/recordinterval + 1
testt = h.Vector(recordlength)
testv = h.Vector(recordlength)
# Recording at the soma
testt.record(h._ref_t, recordinterval)
testv.record(h.soma(0.5)._ref_v, recordinterval)
# Initialize
h.finitialize(vinit)
h.fcurrent()
# Integrate
while h.t <= simstop:
v = h.soma.v
h.fadvance()
# Shutdown
testt.printf(timefile, '%f\n')
testv.printf(voltagefile, '%f\n')
timefile.close()
voltagefile.close()
return(0)