Skip to content

BUG: sqrt(ma.masked) returns non-masked array #25635

Closed

Description

@crusaderky

Describe the issue:

In numpy 2.0 nightly, applying sqrt() to a fully masked scalar array undoes the mask.
numpy 1.26.3 behaves correctly.
Other ufuncs seem to behave correctly (but I haven't performed a thorough test for all of them).

Reproduce the code example:

>>> import numpy as np
>>> a = np.ma.masked_array(1, mask=True)
>>> type(a)
numpy.ma.core.MaskedArray

>>> np.sqrt(a)
# numpy 2.0
masked_array(data=1.,
             mask=False,
             fill_value=1e+20)
# numpy 1.26.3
masked_array(data=--,
             mask=True,
       fill_value=1e+20,
            dtype=float64)

>>> np.sqrt(np.ma.masked)
# numpy 2.0
masked_array(data=0.,
             mask=False,
             fill_value=1e+20)
# numpy 1.26.3
masked_array(data=--,
             mask=True,
       fill_value=1e+20,
            dtype=float64)

Error message:

No response

Python and NumPy Versions:

CPython 3.12.1 x86_64
numpy 2.0.0.dev0+git20240113.d2f60ff

from
python -m pip install --no-deps --pre -i https://pypi.anaconda.org/scientific-python-nightly-wheels/simple numpy
as of 2024-01-19

Runtime Environment:

No response

Context for the issue:

No response

Activity

  1. seberg commented on Jan 19, 2024

    @seberg
    Member

    Weird, I think the reason why this is so cryptic, is that something fails with a TypeError while dealing with the context, and then it succeeds when not passing it.

    (Shows how try/except clauses to be forgiving are not helpful sometimes...)

  2. seberg commented on Jan 19, 2024

    @seberg
    Member

    The error seems to be:

    TypeError                                 Traceback (most recent call last)
    Cell In[1], line 1
    ----> 1 np.sqrt(np.ma.masked)
    
    File ~/forks/numpy/build-install/usr/lib/python3.10/site-packages/numpy/ma/core.py:6686, in MaskedConstant.__array_wrap__(self, obj, context, return_scalar)
       6685 def __array_wrap__(self, obj, context=None, return_scalar=False):
    -> 6686     return self.view(MaskedArray).__array_wrap__(obj, context)
    
    File ~/forks/numpy/build-install/usr/lib/python3.10/site-packages/numpy/ma/core.py:3131, in MaskedArray.__array_wrap__(self, obj, context, return_scalar)
       3127 except KeyError:
       3128     # Domain not recognized, use fill_value instead
       3129     fill_value = self.fill_value
    -> 3131 np.copyto(result, fill_value, where=d)
       3133 # Update the mask
       3134 if m is nomask:
    
    TypeError: Cannot cast scalar from dtype('float64') to dtype('bool') according to the rule 'safe'
    

    (Although now I have to look at my other code, I didn't want to see it at this stage ;)).

  3. seberg commented on Jan 19, 2024

    @seberg
    Member

    OK, the problem there is the d, which is not a valid mask (it is the float value). That seems right (not sure when/why it changed).

  4. Kai-Striega commented on Mar 8, 2024

    @Kai-Striega
    Member

    @seberg do you think this should be possible to fix in a weekend? If so I can take a look at it this weekend :)

  5. added a commit that references this issue on Nov 20, 2024
    294d745
  6. added a commit that references this issue on Nov 20, 2024
    785fceb
  7. added a commit that references this issue on Jan 8, 2025
    3ec6fa8
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