[Numpy-discussion] MA bug or feature?

Michael Sorich michael.sorich at gmail.com
Mon Jul 3 23:44:56 CDT 2006


On 6/23/06, Paul Dubois <pfdubois at gmail.com> wrote:
> In the original MA in Numeric, I decided that to constantly check for masks
> that didn't actually mask anything was not a good idea. It punishes normal
> use with a very expensive check that is rarely going to be true.
>
> If you are in a setting where you do not want this behavior, but instead
> want masks removed whenever possible, you may wish to wrap or replace things
> like masked_array so that they call make_mask with flag = 1:
>
> y = masked_array(data, make_mask(maskdata, flag=1))
>
> y will have no mask if maskdata is all false.
>

Hi Paul. If this is purely for optimisation, is there perhaps a better
way to do so in which the optimisation is hidden? For example if the
optimisation is in regard to the case in which none of the elements is
masked, an alternative approach may be to make a subclass of ndarray
and cache the result of the any method. e.g.

import numpy as N

def asmask(data):
    if isinstance(data, Mask):
        return data
    else:
        return Mask(data)

class Mask(N.ndarray):
    __array_priority__ = 10.0
    def __new__(subtype, data):
        ret = N.array(data, N.bool_)
        return ret.view(Mask)

    def __init__(self, data):
        self._any = None

    def any(self):
        if self._any is None:
            self._any = N.ndarray.any(self)
        return self._any

    def __setitem__(self, index, value):
        self._any = None
        N.ndarray.__setitem__(self, index, value)

The biggest problem I have with the current setup is the inconsistency
between the behaviour of the array when the mask is nomask vs a
boolarray with all False. Another example of this is when one changes
the mask on an element. This is not possible when the mask is nomask

print N.version.version
ma1 = N.ma.array([1,2,3], mask=[False, False, False])
print ma1.mask
ma1.mask[2] = True
ma2 = N.ma.array([1,2,3], mask=False)
print ma2.mask
ma2.mask[2] = True

----- output
0.9.8
[False False False]
[False False True]
False
Traceback (most recent call last):
  File "D:\eclipse\Mask\src\mask\__init__.py", line 111, in ?
    ma2.mask[2] = True
TypeError: object does not support item assignment




More information about the Numpy-discussion mailing list