Automatically resizing NumPy recarray

落爺英雄遲暮 提交于 2019-12-10 10:42:59

问题


I'd like to create a subclass of numpy.recarray that automatically resizes when data is added to a row outside of its current length.

The code below does most of what I want.

class autorecarray(numpy.recarray):

   def __init__(self,*args,**kwargs):
      self._increment = 1
      numpy.recarray.__init__(self,args,kwargs)

   def __setitem__(self,ind,y):
      try: 
         numpy.recarray.__setitem__(self,ind,y)
      except IndexError:
         self.resize((self.__len__()+self._increment,),refcheck=False)
         self.__setitem__(ind,y)

It works fine for this use case:

a = utils.autorecarray((1,),formats=['i4','i4'])
a[1] = (1,2) # len(a) will now be 2

However, this usage will raise an IndexError on numpy.core.records.recarray __getitem__ method:

a[2]['f1'] = 3

My initial attempt was to also override the __getitem__ method in my subclass, but this code does not work.

def __getitem__(self,ind):
      try:
         numpy.recarray.__getitem__(self,ind)
      except IndexError:
         self.resize((self.__len__() + self._increment,),refcheck=False)
         self.__getitem__(ind)

It does automatically expand the array, but now every item in the array is None and cannot be changed.

Can anyone tell me what I'm doing wrong?


回答1:


First of all you're missing the asterisks in the numpy.recarray.__init__ call:

def __init__(self, *args, **kwargs):
    self._increment = 1
    numpy.recarray.__init__(self, *args, **kwargs)

And second, you're missing return statements in the __getitem__:

def __getitem__(self,ind):
    try:
        return numpy.recarray.__getitem__(self,ind)
    except IndexError:
        self.resize((self.__len__() + self._increment,),refcheck=False)
        return self.__getitem__(ind)



回答2:


Your overridden __getitem__ doesn't return a value.

It took me a scarily long time to realize that.

Also, as Petr Viktorin points out, you've left out the * and ** operators in your __init__ call.



来源:https://stackoverflow.com/questions/6405342/automatically-resizing-numpy-recarray

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!