[Numpy-discussion] Help on subclassing numpy.ma: __array_wrap__

josef.pktd at gmail.com josef.pktd at gmail.com
Sun Mar 1 11:30:42 EST 2009


On Sun, Mar 1, 2009 at 10:37 AM, Kevin Dunn <kgdunn at gmail.com> wrote:
> Hi everyone,
>
> I'm subclassing Numpy's MaskedArray to create a data class that handles
> missing data, but adds some extra info I need to carrry around. However I've
> been having problems keeping this extra info attached to the subclass
> instances after performing operations on them.
>
> The bare-bones script that I've copied here shows the basic issue:
> http://pastebin.com/f69b979b8  There are 2 classes: one where I am able to
> subclass numpy (with help from the great description at
> http://www.scipy.org/Subclasses), and the other where I subclass numpy.ma,
> using the same ideas again.
>
> When stepping through the code in a debugger, lines 76 to 96, I can see that
> the numpy subclass, called DT, calls DT.__array_wrap__() after it completes
> unary and binary operations. But the numpy.ma subclass, called DTMA, does
> not seem to call DTMA.__array_wrap__(), especially line 111.
>
> Just to test this idea, I overrode the __mul__ function in my DTMA subclass
> to call DTMA.__array_wrap__() and it returns my extra attributes, in the
> same way that Numpy did.
>
> My questions are:
>
> (a) Is MA intended to be subclassed?
>
> (b) If so, perhaps I'm missing something to make this work.  Any pointers
> will be appreciated.
>
> So far it seems the only way for me to sub-class numpy.ma is to override all
> numpy.ma functions of interest for my class and add a DTMA.__array_wrap()
> call to the end of them.  Hopefully there is an easier way.
>
> Related to this question, was there are particular outcome from this
> archived discussion (I only joined the list recently):
> http://article.gmane.org/gmane.comp.python.numeric.general/24315  because
> that dictionary object would be exactly what I'm after here.
>
> Thanks,
>
> Kevin
>

 timeseries in the scikits are subclassing MaskedArray and might
provide some examples

http://scipy.org/scipy/scikits/browser/trunk/timeseries/scikits/timeseries/tseries.py#L446

Josef



More information about the NumPy-Discussion mailing list