NEURON+Python hacks
Posted: Sun Jun 03, 2012 4:31 pm
here's a rewrite of forall and forsec in python
forall is maybe not very useful since easy enough to just call the hoc version from h()
but forsec is useful if you have your sectionlist object-reference in python since otherwise you need to access it via an embedded python interpreter
there are also a couple other aliases provided
note that this assumes a header of from neuron import h
forall is maybe not very useful since easy enough to just call the hoc version from h()
but forsec is useful if you have your sectionlist object-reference in python since otherwise you need to access it via an embedded python interpreter
there are also a couple other aliases provided
note that this assumes a header of from neuron import h
Code: Select all
def htype (obj): st=obj.hname(); sv=st.split('['); return sv[0]
def secname (obj): obj.push(); print h.secname() ; h.pop_section()
def psection (obj): obj.push(); print h.psection() ; h.pop_section()
allsecs=None #global list containing all NEURON sections, initialized via mkallsecs
# still need to generate a full allsecs
def mkallsecs ():
""" mkallsecs - make the global allsecs variable, containing
all the NEURON sections.
"""
global allsecs
allsecs=h.SectionList() # no .clear() command
roots=h.SectionList()
roots.allroots()
for s in roots:
s.push()
allsecs.wholetree()
return allsecs
#forall syntax - c gets executed, allsecs has Sections
def forall (c):
""" NEURON forall syntax - iterates through all the sections available
note that there's a dummy loop variable called s used in this function,
so any command that needs to access a section should be via s.
example: forall('print s.name()') , will print all the section names.
Also note that this function uses a global list, 'allsecs', which may
need to get re-initialized when new sections are created, via the mkallsecs
function above.
"""
global allsecs
if (type(allsecs)==types.NoneType): mkallsecs()
for s in allsecs: exec(c)
#forsec syntax - executes command for each section who's name
# contains secname as a substring
def forsec (secref="soma",command=""):
""" NEURON forsec syntax - iterates over all sections which have a substring
in their names matching secref argument. command is executed if match found.
this function also utilizes the allsecs global variable.
"""
global allsecs
if (type(allsecs)==types.NoneType): mkallsecs()
if (type(secref)==types.StringTypes[0]):
for s in allsecs:
if s.name().count(secref) > 0:
exec(command)
else:
for s in secref: exec(command)