Confused about accessing segments in Python

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

Moderator: hines

Post Reply
lcampagn
Posts: 22
Joined: Mon Jul 07, 2014 1:12 pm

Confused about accessing segments in Python

Post by lcampagn » Thu Oct 23, 2014 3:09 pm

Take the following session example:

Code: Select all

    >>> s = h.Section()
    >>> s.nseg
    1
    >>> s.allseg()
    <nrn.Section object at 0x7f50c483fd50>
    >>> list(s)
    [<nrn.Segment object at 0x7f50bc8ab058>, <nrn.Segment object at 0x7f50bc8ab8c8>, <nrn.Segment object at 0x7f50bc8ab7d8>]
    >>> list(s.allseg())
    [<nrn.Segment object at 0x7f50bc8ab738>, <nrn.Segment object at 0x7f50bc8ab800>, <nrn.Segment object at 0x7f50bc8ab850>]
    >>> list(s)
    [<nrn.Segment object at 0x7f50bc8ab058>]
    >>> list(s.allseg())
    [<nrn.Segment object at 0x7f50bc8ab738>, <nrn.Segment object at 0x7f50bc8ab800>, <nrn.Segment object at 0x7f50bc8ab850>]
1. Calling s.allseg() just returns s again. I think I understand why, but it is confusing for anyone trying to learn the API.
2. Why does list(s) return lists of different length for the first and second invocation?

I suspect that by calling s.allseg(), some internal flag of the Section is altered such that iterating over the Section will return the complete set of nodes expected from allseg() (http://www.neuron.yale.edu/phpbb/viewto ... f=2&t=2150), but obviously this results in some strange behavior if I don't immediately request all items in the iteration. A better approach might be for h.allseg() to return a dedicated iterator without changing the state of the Section.

hines
Site Admin
Posts: 1577
Joined: Wed May 18, 2005 3:32 pm

Re: Confused about accessing segments in Python

Post by hines » Fri Oct 24, 2014 5:30 am

changset http://www.neuron.yale.edu/hg/neuron/nr ... 17d227a57b fixes the problem in 'Release 7.3' and that has been merged to the trunk.

You were correct in surmising that there was a flag in nrn.Section that altered the interation over the nrn.Segments.
The flag has been remove and nrn.Section.allseg() now returns a nrn.AllSegIter iterator which does not interfere with the default behavior of
lis([nrn.Section).

lcampagn
Posts: 22
Joined: Mon Jul 07, 2014 1:12 pm

Re: Confused about accessing segments in Python

Post by lcampagn » Fri Oct 24, 2014 11:17 am

Thanks, Michael!

Post Reply