8000 NaN comparison allowed on slices of N-D array with floating-point invlaid error set to 'raise' · Issue #12935 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

NaN comparison allowed on slices of N-D array with floating-point invlaid error set to 'raise' #12935

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

Open
davcrom opened this issue Feb 5, 2019 · 5 comments
Labels

Comments

@davcrom
Copy link
davcrom commented Feb 5, 2019

It appears that comparison with NaN is allowed on certain array slices even when the floating-point error for invalid comparisons is set to 'raise'.

(may be related to #10370 and ultimately #11043)

Reproducing code example:

import numpy as np
np.seterr(invalid='raise')

np.full(2, np.nan)[0] > 0
np.full((2,2),  np.nan)[:,0] > 0
np.full((2,2), np.nan)[0,0] > 0
np.full((2,2,2), np.nan)[:,:,0] > 0
np.full((2,2,2), np.nan)[:,0,0] > 0
np.full((2,2,2), np.nan)[0,0,0] > 0

and

np.full((2,2,2), np.nan)[:,0,0].copy()[0] > 0

all return False (or arrays of False). However...

np.full((2,2), np.nan) > 0
np.full((2,2), np.nan)[0,:] > 0
np.full((2,2,2), np.nan)[0,:,:] > 0
np.full((2,2,2), np.nan)[0,0,:] > 0

and

np.full((2,2,2), np.nan)[:,:,0].copy() > 0

all raise the appropriate floating-point error.

Error message:

Traceback (most recent call last):
File "<stdin>", line 1, in
FloatingPointError: invalid value encountered in greater

Numpy/Python version information:

Numpy: 1.15.1
Python: 2.7.12 and 3.5.2
System: Ubuntu 16.04.5 LTS

@mhvk
Copy link
Contributor
mhvk commented Feb 5, 2019

I cannot reproduce all of the above. E.g.,

np.full((2,2,2), np.nan)[:,0,0] > 0
# FloatingPointError: invalid value encountered in greater

From my quick tests, it seems the only ones that fail are those in which the indexing led to a scalar.
(numpy 1.16.0rc2, Debian/testing)

@seberg
Copy link
Member
seberg commented Feb 11, 2019

My guess would have been that it fails fails for the contiguous fast path, which should be triggered on contiguous arrays, however, possibly it is also triggered on 1-D arrays in general and there may be a small chance that the fast path even broke there (leading to the difference in the last example).

Of course it could also be SIMD related for similar reasons.

@seberg
Copy link
Member
seberg commented Feb 11, 2019

Whoops, sorry, those are the scalar cases that fail.

@eric-wieser
Copy link
Member
eric-wieser commented Mar 7, 2019

A slightly easier to run test:

import numpy as np

to_try = [
    "np.full(2, np.nan)[0]",
    "np.full((2,2), np.nan)[:,0]",
    "np.full((2,2), np.nan)[0,0]",
    "np.full((2,2,2), np.nan)[:,:,0]",
    "np.full((2,2,2), np.nan)[:,0,0]",
    "np.full((2,2,2), np.nan)[0,0,0]",
    "np.full((2,2,2), np.nan)[:,0,0].copy()[0]",
    "np.full((2,2), np.nan)",
    "np.full((2,2), np.nan)[0,:]",
    "np.full((2,2,2), np.nan)[0,:,:]",
    "np.full((2,2,2), np.nan)[0,0,:]",
    "np.full((2,2,2), np.nan)[:,:,0].copy()",
]

np.seterr(invalid='raise')

raised = []
silent = []

for t in sorted(to_try):
    try:
        eval(t) < 0
    except FloatingPointError:
        raised.append(t)
    else:
        silent.append(t)

print("raised:")
for t in raised:
    print("  {}".format(t))
print("silent:")
for t in silent:
    print("  {}".format(t))

Which on my machine (windows 10 x64) gives:

raised:
  np.full((2,2), np.nan)
  np.full((2,2), np.nan)[0,:]
  np.full((2,2), np.nan)[:,0]
  np.full((2,2,2), np.nan)[0,0,:]
  np.full((2,2,2), np.nan)[0,:,:]
  np.full((2,2,2), np.nan)[:,0,0]
  np.full((2,2,2), np.nan)[:,:,0]
  np.full((2,2,2), np.nan)[:,:,0].copy()
silent:
  np.full((2,2), np.nan)[0,0]
  np.full((2,2,2), np.nan)[0,0,0]
  np.full((2,2,2), np.nan)[:,0,0].copy()[0]
  np.full(2, np.nan)[0]

The pattern there is that only the scalar cases are silent, which I think might be deliberate.

Can you run the code above on your machine, @davcrom?

@mspacek
Copy link
Contributor
mspacek commented Mar 7, 2019

@eric-wieser, the above code on my 64 bit Xubuntu 16.04 (Python 3.5.2, numpy 1.15.4) Thinkpad 6th gen i7 gives:

raised:
  np.full((2,2), np.nan)
  np.full((2,2), np.nan)[0,:]
  np.full((2,2,2), np.nan)[0,0,:]
  np.full((2,2,2), np.nan)[0,:,:]
  np.full((2,2,2), np.nan)[:,:,0].copy()
silent:
  np.full((2,2), np.nan)[0,0]
  np.full((2,2), np.nan)[:,0]
  np.full((2,2,2), np.nan)[0,0,0]
  np.full((2,2,2), np.nan)[:,0,0]
  np.full((2,2,2), np.nan)[:,0,0].copy()[0]
  np.full((2,2,2), np.nan)[:,:,0]
  np.full(2, np.nan)[0]

I can't pretend to understand what's going on, but it's clearly a different result from your windows machine.

In [6]: np.geterr()                                                                                     
Out[6]: {'divide': 'warn', 'invalid': 'raise', 'over': 'warn', 'under': 'ignore'}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants
0