Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: masked array division broken with np.seterr(under="raise") #25810

Closed
tacaswell opened this issue Feb 12, 2024 · 3 comments
Closed

BUG: masked array division broken with np.seterr(under="raise") #25810

tacaswell opened this issue Feb 12, 2024 · 3 comments
Labels
00 - Bug component: numpy.ma masked arrays sprintable Issue fits the time-frame and setting of a sprint

Comments

@tacaswell
Copy link
Contributor

Describe the issue:

When np.seterr(under="raise") is set, division does not work with masked arrays due to an internal multiplication underflow.

Reproduce the code example:

np.seterr(under="raise")
x=np.arange(0, 3, 0.1)
X = np.ma.array(x)
x2 = x / 2.0  # <- works
X2 = X / 2.0  # <- fails

Error message:

File /usr/lib/python3.11/site-packages/numpy/ma/core.py:4275, in MaskedArray.__truediv__(self, other)
   4273 if self._delegate_binop(other):
   4274     return NotImplemented
-> 4275 return true_divide(self, other)

File /usr/lib/python3.11/site-packages/numpy/ma/core.py:1171, in _DomainedBinaryOperation.__call__(self, a, b, *args, **kwargs)
   1169 domain = ufunc_domain.get(self.f, None)
   1170 if domain is not None:
-> 1171     m |= domain(da, db)
   1172 # Take care of the scalar case first
   1173 if not m.ndim:

File /usr/lib/python3.11/site-packages/numpy/ma/core.py:858, in _DomainSafeDivide.__call__(self, a, b)
    856 a, b = np.asarray(a), np.asarray(b)
    857 with np.errstate(invalid='ignore'):
--> 858     return umath.absolute(a) * self.tolerance >= umath.absolute(b)

FloatingPointError: underflow encountered in multiply

Python and NumPy Versions:

Python 3.11, numpy 1.26.3

Runtime Environment:

[{'numpy_version': '1.26.3',
  'python': '3.11.6 (main, Nov 14 2023, 09:36:21) [GCC 13.2.1 20230801]',
  'uname': uname_result(system='Linux', node='belanna', release='6.7.4-arch1-1', version='#1 SMP PREEMPT_DYNAMIC Mon, 05 Feb 2024 22:07:49 +0000', machine='x86_64')},
 {'simd_extensions': {'baseline': ['SSE', 'SSE2', 'SSE3'],
                      'found': ['SSSE3',
                                'SSE41',
                                'POPCNT',
                                'SSE42',
                                'AVX',
                                'F16C',
                                'FMA3',
                                'AVX2'],
                      'not_found': ['AVX512F',
                                    'AVX512CD',
                                    'AVX512_KNL',
                                    'AVX512_KNM',
                                    'AVX512_SKX',
                                    'AVX512_CLX',
                                    'AVX512_CNL',
                                    'AVX512_ICL',
                                    'AVX512_SPR']}}]

Context for the issue:

This was reported against Matplotlib in matplotlib/matplotlib#27770

@2sn will have to provide any additional context.

@tacaswell tacaswell changed the title BUG: <Please write a comprehensive title after the 'BUG: ' prefix> BUG: masked array division broken with np.seterr(under="raise") Feb 12, 2024
@tacaswell
Copy link
Contributor Author

This can be worked around by using X2 = X*0.5 instead.

@seberg
Copy link
Member

seberg commented Feb 12, 2024

This domain stuff is fatally flawed, but I guess np.errstate(all="ignore") makes sense in any domain related calculation especially.

@seberg seberg added component: numpy.ma masked arrays sprintable Issue fits the time-frame and setting of a sprint labels Feb 12, 2024
@carlosilva10260
Copy link
Contributor

I'm working on this issue

@seberg seberg closed this as completed in 9704767 Apr 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
00 - Bug component: numpy.ma masked arrays sprintable Issue fits the time-frame and setting of a sprint
Projects
None yet
Development

No branches or pull requests

3 participants