Skip to content

Masked arrays don't properly edit views #5558

Closed

Description

@jakirkham

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)

Activity

  1. stuarteberg commented on Feb 13, 2015

    @stuarteberg
    Contributor

    @jakirkham, In your example code, I don't understand why you expect a.mask to be changed. The masks for all three arrays a, b, and c are False everywhere. Is the example missing a line such as c.mask[:] = True?

    FWIW, If I insert that line, then I do indeed see the issue reported here. If I assign to a view of a masked array, the data values are updated in both the view and the original array (of course). But the mask is only properly updated in the view, not the original array.

    System details:
    numpy 1.9.1
    python 2.7.9 (installed via Anaconda)
    Mac OS 10.7.5

  2. jakirkham commented on Feb 13, 2015

    @jakirkham
    ContributorAuthor

    @stuarteberg Yes, sorry. You are correct I was missing such a line.

    Generally, the documentation recommends one to assign as so c[:] = np.ma.masked. However, as we have ensured the mask is simply an array, it is also mentioned that you can do c.mask[:] = True as you have.

  3. jakirkham commented on Feb 14, 2015

    @jakirkham
    ContributorAuthor

    This code seems to stop propagation of the mask and it is not clear to me why this is necessary (

    numpy/numpy/ma/core.py

    Lines 3129 to 3132 in 4bfcf48

    # Unshare the mask if necessary to avoid propagation
    if not self._isfield:
    self.unshare_mask()
    _mask = ndarray.__getattribute__(self, '_mask')
    ). As the mask is not hard here, it should not be a problem to have propagation.

  4. jakirkham commented on Feb 14, 2015

    @jakirkham
    ContributorAuthor

    Same situation here (

    numpy/numpy/ma/core.py

    Lines 3112 to 3113 in 4bfcf48

    if not self._isfield:
    self._sharedmask = False
    ). I don't understand why only fields of masked record arrays can share masks, but nothing else. Is there something special we want to avoid in record arrays? If so, couldn't we accomplish this without ruining the possibility of editing views on an ordinary masked array?

  5. 13 remaining items

  6. jjhelmus commented on Oct 21, 2015

    @jjhelmus
    Contributor

    This issue was also reported in #1903.

  7. added a commit that references this issue on Jun 9, 2017
  8. jakirkham commented on Dec 4, 2017

    @jakirkham
    ContributorAuthor

    Should this issue be added to 1.14.0 milestone (as that is where the fix landed)?

  9. charris commented on Dec 4, 2017

    @charris
    Member

    Once it is in it doesn't matter. We don't rely on the milestone tag except to mark something as a potential release blocker.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions