[Numpy-discussion] maskedarray: how to force mask to expand

Pierre GM pgmdevlist at gmail.com
Thu Sep 25 13:02:51 EDT 2008


Vincent,

The argument of speed (having a default mask of nomask) applies only to the 
computations inside MaskedArray. Of course, it is still far faster not to use 
masks but only ndarrays.

> Just for clarity, to rephrase my question: how do I force ma to give me
> (always/by default/by some method on a maskedarray) a full shaped mask
> instead of 'False' or nomask? Because I am sure from the beginning that
> I'll need this mask in full shape, I want it, and I want to be able to
> treat it like any normal bool array :-)

Easy:
>>> a = ma.array([1,2,3,4], mask=False)
masked_array(data = [1 2 3 4],
      mask = [False False False False],
      fill_value=999999)

Puzzling ? Not so much. See, by default, the value of the mask parameter is 
`nomask`. nomask is in fact a 0-size boolean ndarray with value 0 (False). At 
the creation of the masked array, we check whether a value was given to the 
mask parameter. If no value is given, we default to `nomask`, and we end up 
with `a.mask is nomask`. If you force the mask parameter to the boolean 
False, you're not using `nomask`: in that case, the full mask is created.

That won't work with ma.zeros or ma.ones. I could add an extra keyword to deal 
witht that, but is it really needed when you can simply do a.mask=False ?

Note: 
>>> a=ma.array([1,2,3,])
>>> a.mask is False
False
>>> a.mask is ma.nomask
True
>>> a.mask == False
True

> > If you need to mask one or several elements, the easiest is not to modify
> > the mask itself, but to use the the special value `masked`:

> Ah, I did not know that one. Does that always work, I mean, with slices,
> fancy indexing, etc.? Like 'a[a<0 | a>100] = ma.masked'? It's kind of
> clean to fiddle with the mask of the array without really interacting
> with the mask itself, if you understand what I mean... :)

It does work, try it for yourself. It's actually *the* recommended way to set 
values to the mask.

> And is there also a complement, like ma.unmasked? I could not find it
> (very quick search, I admit)... Or can I use !ma.masked?

No, there isn't and no you can't.  ma.masked is actually a constant defined 
module-wide and independent of any array. That way, you can test whether an 
element is masked with `a[..] is masked`. 

> Ah, now the magic starts... (normal user cap on head, beware):
>
> In [9]: am.mask
> Out[9]: False
>
> In [10]: am.mask = False
>
> In [11]: am.mask
> Out[11]:
> array([[False, False],
>         [False, False]], dtype=bool)
>
> while (with the same am as before [9], with am.mask == False):
>
> In [15]: am.mask = am.mask
>
> In [16]: am.mask
> Out[16]: False
>
> Do you see (and agree with me about) the inconsistency?

No. I'm afraid you're confusing `nomask` and `False`. Once again, nomask is 
NOT the same thing as False. It's the same value, but not the same object.




More information about the NumPy-Discussion mailing list