Thanks Michael, however I am still not able to make it work. Below is the code for my Cell class which basically only has functions for setting and getting i_membrane: getMemCurArrSlow() is a slow implementation where I loop through the tree at each time step. Following your suggestion I used the PtrVector and created two functions: setMemCurArrEff() to set pointers to seg.i_membrane and another one getMemCurArrEff() to gather vector of seg.i_membrane at each time step. As you can see the setMemCurArrEff() is called only in constructor.
Code: Select all
class Cell(object):
'''
Class for cell initialized from hoc Template
'''
def __init__(self,hocTemplate):
'''
Constructor
'''
self.hoc = hocTemplate
self.getNseg()
self.iMemSeg = zeros((self.nseg,1))
self.pVec = h.PtrVector(self.nseg) # pointer vector
self.setMemCurArrEff() # this should be called once only during the constructor
def getNseg(self):
self.nseg = 0
for sec in self.hoc.all:
self.nseg += sec.nseg # get the total # of segments in the cell
def getMemCurArrSlow(self): # slow because it loops over dendrites on each time step
jseg = 0
for sec in self.hoc.all:
for seg in sec:
self.iMemSeg[jseg] = seg.i_membrane
jseg += 1 # increment cell's segment index
return self.iMemSeg
def setMemCurArrEff(self):
jseg = 0
for sec in self.hoc.all:
for seg in sec:
self.pVec.pset(jseg,seg._ref_i_membrane)
jseg += 1
def getMemCurArrEff(self): # get membrane currents at each time step from PtrVector (no loop)
iMemSegfromPtr = h.Vector(self.nseg) # place here values from pointer to i_membrane
self.pVec.gather(iMemSegfromPtr)
return iMemSegfromPtr.as_numpy()
I call get funcions from calcCSD() belloning to the Population class. calcCSD() is called on each time step from proc advance() after proc fadvance(). I stripped my calcCSD() from much detail just to see whether using PtrVecor works:
Code: Select all
def calcCSD(self): # calculate CSD:
iMemSeg = cell.getMemCurArrSlow() # get membrane currents
iMemArr = cell.getMemCurArrEff() # must be efficient but does not work
print ' iMemSeg[67]',iMemSeg[67],' iMemArr[67]',iMemArr[67]
the print statement results in iMemArr being all constant (it is a small number ~1E-316), i.e. the vector does not update, whereas iMemSeg is caluclated correctly, but very inefficiently. Do you have any idea for what is wrong with my implementation of the code?
At this point I wanted to ask you about your last comment:
Of course, you still need to execute setMemCurArray every time step so that that can execute PtrVec.gather(destvec)
This is confusing, if I still need to execute setMemCurArray (which is now actually called getMemCurArrSlow()), then what would be the point of using PtrVector? I realize that I do need to perform a PtrVec.gather(destvec). But can I avoid looping through dendritic tree at each time step?