"seg in sec" is not the same as "seg in sec.allseg()"

When Python is the interpreter, what is a good
design for the interface to the basic NEURON
concepts.

Moderator: hines

Post Reply
vellamike

"seg in sec" is not the same as "seg in sec.allseg()"

Post by vellamike »

I just realised there is some kind of subtle difference here in that "seg in sec.allseg()" seems to iterate over the edges of the section also, or something like that, I'm still trying to understand it.

so...

Code: Select all

      for seg in sec.allseg():
            seg.diam=seg.diam*coefficient
will produce a different result to

Code: Select all

        for seg in sec:
            seg.diam=seg.diam*coefficient
Can someone explain to me exactly what the difference is here? I thought that each section had X number of segments, each of which could be set.

I think that aside from the nodes at segment centres there are also the nodes at the edges, which are not accessed when using "for seg in sec" but are if "seg in sec.allseg()" is used, is this correct?
ted
Site Admin
Posts: 6287
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: "seg in sec" is not the same as "seg in sec.allseg()"

Post by ted »

vellamike wrote:

Code: Select all

      for seg in sec.allseg():
            seg.diam=seg.diam*coefficient
will produce a different result to

Code: Select all

        for seg in sec:
            seg.diam=seg.diam*coefficient
Which begs a question . . . can you please provide a specific example using a section that has, say, nseg == 3?
vellamike

Re: "seg in sec" is not the same as "seg in sec.allseg()"

Post by vellamike »

Sure, so here is some code which changes the diameters of the sections through some basic algorithm and the output of the code. The sections look different when using "for seg in sec" as opposed to "for seg in sec.allseg()":

Code: Select all

from neuron import h

testsection1 = h.Section()
testsection2 = h.Section()

testsection1.nseg=3
testsection2.nseg=3

i=0
j=0 

print 'testsection1:'

for seg in testsection1:
	seg.diam=300+i
	i+=20
	print seg.diam

print 'testsection2:'
for seg in testsection2.allseg():
	seg.diam=300+j
	j+=20
	print seg.diam
the output is:

Code: Select all

testsection1:
300.0
320.0
340.0
testsection2:
300.0
320.0
340.0
360.0
380.0
Which seems to imply that section.allseg() means all nodes rather than all segments and produces the same result as for(x) in hoc?
ted
Site Admin
Posts: 6287
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: "seg in sec" is not the same as "seg in sec.allseg()"

Post by ted »

section.allseg() means all nodes rather than all segments and produces the same result as for(x) in hoc?
True.
for seg in sec
iterates over the internal nodes of sec, i.e. nodes with range >0 and <1, just like hoc's
secname for (x,0)
for seg in sec.allseg()
iterates over all nodes of sec, including the nodes at 0 and 1, just like hoc's
secname for (x)

All that your example needs to prove the point beyond question is to print seg.x as well as seg.diam.

Iterating over all nodes, whether in hoc or Python, can produce identically misleading results, as is the case with your own example:

Code: Select all

testsection2:
300.0
320.0
340.0
360.0
380.0
After exiting the loop, 300 will not be the diam at 0, nor will 360 be the diam at 0.833 (i.e. at the last internal node. Check the diameters with

Code: Select all

for seg in sec.allseg():
    print sec.x, sec.diam
and you will discover that the actual outcome is

Code: Select all

0.0  320.0
0.166...  320.0
0.5  340.0
0.833...  380.0
1.0  380.0
Exactly the same results are generated by the hoc equivalent

Code: Select all

dend for (x) {
  diam(x) = 300+j
  j += 20
  print diam(x)
}
dend for (x) print diam(x)
The rule is that attempting to access a range variable at the 0 or 1 location actually accesses the range variable at the adjacent internal node.
vellamike

Re: "seg in sec" is not the same as "seg in sec.allseg()"

Post by vellamike »

Thanks for the clarifications!
ted
Site Admin
Posts: 6287
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: "seg in sec" is not the same as "seg in sec.allseg()"

Post by ted »

Well, you pretty much figured it out. I just wanted to spell out the implications as clearly as possible for anyone else who might read this thread.
Post Reply