[Numpy-discussion] Fix masked arrays to properly edit views

Eric Firing efiring at hawaii.edu
Sat Mar 14 20:01:04 EDT 2015


On 2015/03/14 1:02 PM, John Kirkham wrote:
> The sample case of the issue (
> https://github.com/numpy/numpy/issues/5558 ) is shown below. A proposal
> to address this behavior can be found here (
> https://github.com/numpy/numpy/pull/5580 ). Please give me your feedback.
>
>
> I tried to change the mask of `a` through a subindexed view, but was
> unable. Using this setup I can reproduce this in the 1.9.1 version of NumPy.
>
>      import numpy as np
>
>      a = np.arange(6).reshape(2,3)
>      a = np.ma.masked_array(a, mask=np.ma.getmaskarray(a), shrink=False)
>
>      b = a[1:2,1:2]
>
>      c = np.zeros(b.shape, b.dtype)
>      c = np.ma.masked_array(c, mask=np.ma.getmaskarray(c), shrink=False)
>      c[:] = np.ma.masked
>
> This yields what one would expect for `a`, `b`, and `c` (seen below).
>
>       masked_array(data =
>         [[0 1 2]
>          [3 4 5]],
>                    mask =
>         [[False False False]
>          [False False False]],
>               fill_value = 999999)
>
>       masked_array(data =
>         [[4]],
>                    mask =
>         [[False]],
>               fill_value = 999999)
>
>       masked_array(data =
>         [[--]],
>                    mask =
>         [[ True]],
>               fill_value = 999999)
>
> Now, it would seem reasonable that to copy data into `b` from `c` one
> can use `__setitem__` (seen below).
>
>       b[:] = c
>
> This results in new data and mask for `b`.
>
>       masked_array(data =
>         [[--]],
>                    mask =
>         [[ True]],
>               fill_value = 999999)
>
> This should, in turn, change `a`. However, the mask of `a` remains
> unchanged (seen below).
>
>       masked_array(data =
>         [[0 1 2]
>          [3 0 5]],
>                    mask =
>         [[False False False]
>          [False False False]],
>               fill_value = 999999)
>
>

I agree that this behavior is wrong.  A related oddity is this:

In [24]: a = np.arange(6).reshape(2,3)
In [25]: a = np.ma.array(a, mask=np.ma.getmaskarray(a), shrink=False)
In [27]: a.sharedmask
True
In [28]: a.unshare_mask()
In [30]: b = a[1:2, 1:2]
In [31]: b[:] = np.ma.masked
In [32]: b.sharedmask
False
In [33]: a
masked_array(data =
  [[0 1 2]
  [3 -- 5]],
              mask =
  [[False False False]
  [False  True False]],
        fill_value = 999999)

It looks like the sharedmask property simply is not being set and 
interpreted correctly--a freshly initialized array has sharedmask True; 
and after setting it to False, changing the mask of a new view *does* 
change the mask in the original.

Eric

>
> Best,
> John
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>




More information about the NumPy-Discussion mailing list