Page 1 of 1

Section-relative location of a segment

Posted: Sat Apr 13, 2024 7:56 pm
by opdalex
Hi.

I have not been able to find a simple, pythonic way of extracting the section-relative location of a segment. I find it quite surprising that I have not found it considering the location is available from the segment object's string representation. Good thing that it is, because that is what my current solution relies on. However, I believe there must be better way, which is why I came here.

Code: Select all

section = h.Section("sec")
section.nseg = 3
segment = section(0.5)

#print(segment) will be sec(0.5), therefore split at parentheses
location_string = str(segment)
parts = location_string.split("(")
location = float(parts[1].rstrip(")"))
Hope to hear from someone soon!

Re: Section-relative location of a segment

Posted: Mon Apr 15, 2024 9:43 am
by ted
Given all that beautiful documentation at nrn.readthedocs.io I find it even more surprising that it is so difficult to discover the answer to your question. One would expect it to be with the other documentation about Section, but it isn't. But wait, there's a Search feature . . . which isn't very helpful for this particular problem.
what my current solution relies on
is clever but horrible.

The answer should be, but isn't, in the Index. However it is implicit in the Scripting NEURON basics text under the heading Aside 6: Accessing segment variables (aside 6?? who would have guessed???), which you will find here https://nrn.readthedocs.io/en/latest/tu ... -variables--until somebody redoes the documentation [again] and, not recognizing the importance of this bit, tosses it down the memory hole.

"What", you may well ask, "am I supposed to glean from this?"

If foo refers to a segment in some section bah, then foo.x returns the range of the node at the center of foo (i.e. normalized distance of that node from bah's 0 end). Example:

dend = h.Section('dend')
dend.nseg = 5
print(dend(0.5).x) # will print 0.5
for seg in dend: print(seg.x) # will print 0.1 0.3 0.5 0.7 0.9

If you want to include the nodes at 0 and 1, the syntax is
for seg in dend.allseg(): print(seg.x) # will print 0.0 0.1 0.3 0.5 0.7 0.9 1.0

Re: Section-relative location of a segment

Posted: Mon Apr 15, 2024 10:57 am
by opdalex
I am a bit embarassed to recognize that I have definitely used this before. Thanks for digging this up!

Re: Section-relative location of a segment

Posted: Mon Apr 15, 2024 11:59 am
by ted
Around here we all have egg on our faces.