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

Pierre GM pgmdevlist@gmail....
Wed Sep 24 11:37:21 CDT 2008


Vincent,

You should really consider putting an example next time. I must admit that I'm 
not sure what you're trying to do, and where/why it fails.

Yes, by default, the mask of a new MaskedArray is set to the value 'nomask', 
which is the boolean False. Directly setting an element of the mask in that 
condition fails of course. The reasons behind using this behavior are (1) 
backward compatibility and (2) speed, as you can bypass a lot of operations 
on the mask when it is empty.

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`:

>>>a = ma.array(np.arange(6).reshape(3,2))
masked_array(data =
 [[0 1]
 [2 3]
 [4 5]],
      mask =
 False,
      fill_value=999999)
>>> # Mask the first element.
>>> a[0,0] = ma.masked
>>> a
masked_array(data =
 [[-- 1]
 [2 3]
 [4 5]],
      mask =
 [[ True False]
 [False False]
 [False False]],
      fill_value=999999)

This value, `masked`, is also useful to check whether one particular element 
is masked:
>>> a[0,0] is ma.masked
True
>>> a[0,1] is ma.masked
False

You can also force the mask to be full of False with the proper shape by that 
way:
>>> a = ma.array(np.arange(6).reshape(3,2)
>>> # Force the mask to have the proper shape and be full of False:
>>> a.mask = False
masked_array(data =
 [[0 1]
 [2 3]
 [4 5]],
      mask =
 [[False False]
 [False False]
 [False False]],
      fill_value=999999)


The shrink argument of ma.array collapses amask full of False to nomask, once 
again for speed reasons. So no, it won't do what you look like to want.
 
I agree that having to deal with nomask is not completely intuitive. However, 
it is required for backward compatibility. One day, the class will be ported 
to C, and then I'll push to have the mask set to the proper shape ab initio, 
because then speed will be less of an issue.

In the meantime, I hope I answered your question.


On Wednesday 24 September 2008 06:25:57 Vincent Schut wrote:
> Probably I'm just overlooking something obvious, but I'm having problems
> with maskedarrays (numpy.ma from svn: '1.3.0.dev5861'), the mask by
> default being a single bool value ('False') instead of a properly sized
> bool array. If I then try to mask one value by assigning values to
> certain mask positions (a.mask[0,0]=True) I get an error, logically. I
> know I can use mask_where, but I like the mask[...] idiom. And I have to
> expand the mask anyway, as I'm gonna write it to a file at the end.
>
> 1) Is there a way to have ma always use properly expanded masks (bool
> arrays instead of single bool values)? I tried the shrink=False keyword,
> but that does not do what I want, and is not available for
> numpy.ma.zeros, which I conveniently use a lot.
>
> 2) Is there a method/function to request the mask, be it a single bool
> value or an array, as a properly sized array? I found shrink_mask but no
> opposite method, and shrink_mask seems to do something subtly different
> even.
>
> Regards,
> Vincent.
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion@scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion




More information about the Numpy-discussion mailing list