Section-relative location of a segment

The basics of how to develop, test, and use models.
Post Reply
opdalex
Posts: 9
Joined: Tue Sep 26, 2023 9:16 am

Section-relative location of a segment

Post 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!
ted
Site Admin
Posts: 6314
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Section-relative location of a segment

Post 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
opdalex
Posts: 9
Joined: Tue Sep 26, 2023 9:16 am

Re: Section-relative location of a segment

Post by opdalex »

I am a bit embarassed to recognize that I have definitely used this before. Thanks for digging this up!
ted
Site Admin
Posts: 6314
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Section-relative location of a segment

Post by ted »

Around here we all have egg on our faces.
Post Reply