[Numpy-discussion] returning recarray records as plain arrays

Pierre GM pgmdevlist at gmail.com
Thu Jan 4 00:40:54 CST 2007


On Wednesday 03 January 2007 23:57, Matthew Koichi Grimes wrote:
> Pierre GM wrote:
> > On Wednesday 03 January 2007 15:39, Matthew Koichi Grimes wrote:
> >> As per Stefan's help, I've made a subclass of recarray called nnvalue.
> >> It just fixes the dtype to [('x', 'f8'), ('dx', 'f8'), ('delta', 'f8)],
> >> and adds a few member functions. I basically treat nnvalue as a struct
> >> with three equal-shaped array fields: x, dx, and delta.

Matthew,
It seems that declaring your default dtype as a class argument and naming it 
dtype is the reason why things are messed up: you're basically overwriting 
the original ndarray.dtype

You have 2 options:
1. Put the definition of your base dtype inside __new__: it's no longer a 
class argument, so it's not overwriting anything

class nnvalue(N.recarray):
    def __new__(subtype,shape):
	_dtype = N.dtype([('x','f8'),('dx','f8'),('deltas','f8')])
        data = N.zeros(shape,subtype.dtype)
        return data.view(subtype)

2. Call it something else:
class nnvalue(N.recarray):
    __defaultdtype = N.dtype([('x','f8'),('dx','f8'),('deltas','f8')])
    def __new__(subtype,shape):
        data = N.zeros(shape,subtype.__defaultdtype )
        return data.view(subtype)


More information about the Numpy-discussion mailing list