[Numpy-discussion] subclassing recarray

Matthew Koichi Grimes mkg at cs.nyu.edu
Tue Jan 2 02:30:34 EST 2007

Is it hard to subclass recarrays? I'm currently working on a subclass 
that  doesn't add much; just some methods that sets all the values to 
zero and other similarly trivial things. It'd be nice to make it work, 
but if I have to use external functions on a plain recarray instead of 
this subclass, I won't be found weeping in the corner.

The constructor of this subclass just calls recarray's __init__ method, 
but when it does this, I get an error. The class is currently just the 

class nnvalue(N.recarray):

    def __init__(self, shape):
        N.recarray.__init__(self, shape, dtype=N.dtype([('x', 'f8'), 
('dx', 'f8'), ('delta', 'f8')]))

    def clearGradients(self):
        """Fills all members other than x with zeros."""
        self.dx[:] = 0.
        self.deltas[:] = 0.

The error message reads:

Traceback (most recent call last):
  File "/home/mkg/projects/physlearn/src/pl/modules.py", line 287, in ?
    mod = DynModule(states.shape[1:], controls.shape[1:], states.shape[0])
  File "/home/mkg/projects/physlearn/src/pl/modules.py", line 62, in 
    self.params = nnvalue(pSize)
  File "/usr/lib/python2.4/site-packages/numpy/core/records.py", line 
175, in __new__
    descr = format_parser(formats, names, titles, aligned, byteorder)._descr
  File "/usr/lib/python2.4/site-packages/numpy/core/records.py", line 
44, in __init__
    self._parseFormats(formats, aligned)
  File "/usr/lib/python2.4/site-packages/numpy/core/records.py", line 
52, in _parseFormats
    raise ValueError, "Need formats argument"
ValueError: Need formats argument

Any help would be much appreciated.
-- Matt

More information about the NumPy-Discussion mailing list