neuron.h.Vector() size argument has no effect

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

Moderator: hines

Post Reply
selfdestructo
Posts: 32
Joined: Wed Oct 14, 2009 11:12 am

neuron.h.Vector() size argument has no effect

Post by selfdestructo » Fri Apr 29, 2011 7:44 am

Hi,

Consider the following Python function, looping over all sections of a morphology, creating membrane current recorders for all segments;

Code: Select all

    def set_imem_recorders(self, tstopms):
        '''record membrane currents for all compartments'''
        self.memireclist = neuron.h.List()
        for sec in neuron.h.allseclist:
            for seg in sec:
                memirec = neuron.h.Vector(size=int(tstopms/self.timeres_python+1))
                memirec.record(seg._ref_i_membrane, self.timeres_python)
                self.memireclist.append(memirec)
For some reason, the size argument does not seem to have any effect. This causes a lot of memory consumption running long simulations since NEURON double the memory allocation every now and then.
I found that using the following fix work, but it cant be so that the size argument should not be honored?

Code: Select all

                ....
                memirec = neuron.h.Vector()
                memirec.resize(int(tstopms/self.timeres_python+1))
                ....

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

Re: neuron.h.Vector() size argument has no effect

Post by hines » Tue May 03, 2011 9:46 am

It is generally the case that Hoc does not take keyword arguments. The exceptions are
sec=...
to specify the currently accessed section during the scope of the function, and several optional named arguments to the
Section constructor. Those named arguments must follow the unnamed arguments.
So use
neuron.h.Vector(int(...))

selfdestructo
Posts: 32
Joined: Wed Oct 14, 2009 11:12 am

Re: neuron.h.Vector() size argument has no effect

Post by selfdestructo » Thu May 05, 2011 3:52 am

hines wrote:It is generally the case that Hoc does not take keyword arguments. The exceptions are
sec=...
to specify the currently accessed section during the scope of the function, and several optional named arguments to the
Section constructor. Those named arguments must follow the unnamed arguments.
So use
neuron.h.Vector(int(...))
Thanks, I didn't realize this.

Are there also any way to delete this memireclist object properly from Python so that it would actually free the memory afterwards? I tried setting size and buffersize to zero for all elements after creating my numpy.array to no avail. It's no longer needed.

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

Re: neuron.h.Vector() size argument has no effect

Post by hines » Thu May 05, 2011 7:07 am

The Hoc List instance wil be deleted when its reference count goes to 0.
If memireclist is the only Python reference to that hoc object then it should
be sufficient to set it to None or del it or cause object that owns memireclist to
be destroyed. If some other object also holds a reference to the List then you would
need to hunt it down and cause it to reference something else.
Note that the object whose reference count must go to 0 is
self.memireclist.hname()

Code: Select all

>>> from neuron import h
>>> l = h.List()
>>> l.hname()
'List[8]'
>>> sf = h.StringFunctions()
>>> sf.references(l)
List[8] has 2 references
  found 0 of them
0.0
>>> 

It is hard not to be off by one when determining the reference count. In the above case
l holds a reference, and  also the List[8] reference count is temporariy incremented when
it is passed as an object to sf.references. StringFunctions only knows about references
held in the HOC world.



selfdestructo
Posts: 32
Joined: Wed Oct 14, 2009 11:12 am

Re: neuron.h.Vector() size argument has no effect

Post by selfdestructo » Fri May 06, 2011 7:47 am

Thanks, I was able to resolve our memory consumption problems.

Best,
Espen

Post Reply