Skip to content

Commit

Permalink
BUG: ma.make_mask should always return nomask for nomask argument.
Browse files Browse the repository at this point in the history
When the mask argument was nomask, commit 8da9c71 changed the behavior
to depend on shrink=True as well, resulting in array(False) false being
returned when shrink=True, which in turn led to bugs because nomask is a
singleton and is detected by `mask is nomask`. That dectection fails
when nomask is replaced by array(False).

Closes numpy#6667.
  • Loading branch information
charris committed Nov 11, 2015
1 parent 1d51142 commit 59fbef0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
9 changes: 5 additions & 4 deletions numpy/ma/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1495,9 +1495,10 @@ def make_mask(m, copy=False, shrink=True, dtype=MaskType):
shrink : bool, optional
Whether to shrink `m` to ``nomask`` if all its values are False.
dtype : dtype, optional
Data-type of the output mask. By default, the output mask has
a dtype of MaskType (bool). If the dtype is flexible, each field
has a boolean dtype.
Data-type of the output mask. By default, the output mask has a
dtype of MaskType (bool). If the dtype is flexible, each field has
a boolean dtype. This is ignored when `m` is ``nomask``, in which
case ``nomask`` is always returned.
Returns
-------
Expand Down Expand Up @@ -1547,7 +1548,7 @@ def make_mask(m, copy=False, shrink=True, dtype=MaskType):
dtype=[('man', '|b1'), ('mouse', '|b1')])
"""
if m is nomask and shrink:
if m is nomask:
return nomask
elif isinstance(m, ndarray):
# We won't return after this point to make sure we can shrink the mask
Expand Down
11 changes: 11 additions & 0 deletions numpy/ma/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
import warnings
import pickle
import operator
import itertools
from functools import reduce


import numpy as np
import numpy.ma.core
import numpy.core.fromnumeric as fromnumeric
Expand Down Expand Up @@ -3816,6 +3818,15 @@ def test_make_mask(self):
assert_equal(test.dtype, bdtype)
assert_equal(test, np.array([(0, 0), (0, 1)], dtype=bdtype))

# test that nomask is returned when m is nomask.
bools = [True, False]
dtypes = [MaskType, np.float]
msgformat = 'copy=%s, shrink=%s, dtype=%s'
for cpy, shr, dt in itertools.product(bools, bools, dtypes):
res = make_mask(nomask, copy=cpy, shrink=shr, dtype=dt)
assert_(res is nomask, msgformat % (cpy, shr, dt))


def test_mask_or(self):
# Initialize
mtype = [('a', np.bool), ('b', np.bool)]
Expand Down

0 comments on commit 59fbef0

Please sign in to comment.