[Numpy-discussion] maskedarray: how to force mask to expand
Pierre GM
pgmdevlist at gmail.com
Fri Sep 26 11:45:27 EDT 2008
Vincent,
> I think the
> idea that when a.mask returns False, that actually means nomask instead
> of the False I'm used to, is what caused a major part of my confusion.
Indeed.
> It might actually be nice to give you some context of why I asked this:
> during my (satellite image) processing, I use maskedarrays by default
> for each step in the processing chain
Ah. You'll probably notice it might be faster to process the array and its
mask in parallel, and then recreate a masked array at the end. If you need to
save a mask that hasn't been set yet, you could use the `ma.getmaskarray`
command instead of trying to access the mask as an attribute, that is, use:
>>> mask=ma.getmaskarray(a)
instead of
>>> mask=a.mask
or
>>> mask=ma.getmask(a)
`ma.getmaskarray(a)` always return an array with the same shape as `a`, even
if full of False.
If you need to create a new mask from the shape and dtype of `a`, you can use
`ma.make_mask_none(a)`.
Both functions are documented.
> force expanding a mask, or e.g. an ma.mask.as_full_shaped_mask() method
> that returns either the mask, or (if nomask) a new array of Falses. I
> just supposed it existed and I could not find it, but now I understand
> it does not exist.
Actually, it does, that's the ma.getmaskarray() I was just telling you about.
> Just for completeness (in case someone else is reading this and
> wondering how to *unmask* values): just setting ma[idx] to some valid
> number will unset the mask for that index. No need to do ma[idx] =
> ma.unmask or whatever, just ma[idx] = v.
Exactly. Except that for the sake of completeness, there's a little known
attribute (_hardmask) that prevents you to unmask some data by mistake.
By default, `_hardmask` is False, which means that if you set a masked value
to a non-masked one, you update the mask as well. If you set a._hardmask to
True or use the `harden_mask` method, you won't be able to unmask the data
that way, you'll have to modify the mask directly. It's useful when the mask
cannot or should not be changed.
> Btw, in future versions, would it be an idea to separate 'nomask' and
> 'False' a little more? I always assumed (correctly?) that in python,
> True and False are singletons (as far as that is possible in python),
> just like None. 'False is False' should always compare to True, then. In
> this case (a.mask is False) it at least *seems* to break that 'rule'...
You mean, returning something that says 'nomask' instead of 'False' ? I'm not
sure how I can do that. Sure, it'd be nice.
> > 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.
>
> Exactly. It might not be inconsistent, but imho it does a lot of effort
> to /feel/ inconsistent to Joe Average.
It's not intentional.
I guess most of your problems would never have come if we had a proper
documentation. As you're starting to use MaskedArrays, you could take note
and that would provide us with a basis we could improve on...
More information about the NumPy-Discussion
mailing list