BUG: np.min does not always propagate NaNs
This investigation arises from scipy CI starting to fail on appveyor in the last few days, i.e. after 1.14 being released.
On my home computer (macOS, conda python 3.6.2, conda numpy):
>>> import numpy as np
>>> np.version.version
'1.14.0'
>>> np.min([1.0, 2.0, np.nan])
nan
>>> np.min([1.0, np.nan, 2.0])
nan
>>> np.min([np.nan, 1.0, 2.0])
nan
>>> np.min([np.nan, 1.0])
/Users/andrew/miniconda3/envs/dev3/lib/python3.6/site-packages/numpy/core/_methods.py:29: RuntimeWarning: invalid value encountered in reduce
return umr_minimum(a, axis, None, out, keepdims)
nan
On my work computer (macOS, conda python 3.6.2, numpy installed via pip in a clean env):
>>> import numpy as np
>>> np.version.version
'1.14.0'
>>> np.min([1., 2., 3., 4., np.nan])
nan
>>> np.min([1., 2., 3., np.nan, 4.])
nan
>>> np.min([1., 2., np.nan, 3., 4.])
nan
>>> np.min([1., np.nan, 2., 3., 4.])
nan
>>> np.min([np.nan, 1., 2., 3., 4.])
nan
>>> np.min([np.nan, 1.])
nan
>>> np.min([np.nan, 1., np.nan])
nan
>>> np.min([1., np.nan])
nan
>>> np.seterr(all='raise')
{'divide': 'warn', 'over': 'warn', 'under': 'ignore', 'invalid': 'warn'}
>>> np.min([1., np.nan])
nan
>>> np.min([np.nan, 1.])
nan
>>> np.min([np.nan, 1., 2., 3., 4.])
nan
>>> np.min([np.nan, 1., 2., 3., 4.])
nan
With the first set of code examples why don't the first three examples result in a warning, only the last? With the second set of examples no warning is emitted at all.
xref scipy/scipy#8282, scipy/scipy#8279
I think another point here is that it might be different code paths that trigger different (not really numpy I think) behaviour, e.g. due to using AVX or such, I think I saw sometimes that vector stuff failed to set the error cpu flags, and since that can appear a bit random if they are used or not....
Dunno if that has much to do with it, but I expect it, numpy does not do too much about floating point errors normally I think. Except checking whether something happened.
And as Chuck said, a lot of this floating point error flag is different on different systems unfortunately.
Ok, understood about why the error only occurs on some installs and not other. The reason why scipy is using np.min on an array that possibly contains NaN is because it's a quick way of checking for the presence of them. The numpy documentation suggests that this is allowable:
NaN values are propagated, that is if at least one item is NaN, the corresponding min value will be NaN as well.
Given that this is one of the allowed use cases for np.min, from a usage point of view I wouldn't expect any warnings/errors to be emitted at all.
(There are obviously other ways of achieving this, e.g. np.isnan(arr).any(), but that doesn't change my reasoning above)
Not at all is a plausible change I guess.
+1 for no warnings at all
Hi,
I stumbled upon a similar inconsistency:
>>> import numpy
>>> import warnings
>>> warnings.simplefilter("always", category=RuntimeWarning)
>>> numpy.min(numpy.full((7,), numpy.nan, dtype=numpy.float64))
nan
>>> numpy.min(numpy.full((8,), numpy.nan, dtype=numpy.float64))
/home/user/env/lib/python3.6/site-packages/numpy/core/_methods.py:29: RuntimeWarning: invalid value encountered in reduce
return umr_minimum(a, axis, None, out, keepdims)
nan
Why a shape of more than 8 raises that RuntimeWarning ?
@NotSqrt @seberg I'm seeing similar behaviour where min is not propagating NaNs correctly at all, when the size of the input array gets to size 8:
> cat np-min-wat.py
import numpy as np
print "numpy version:", np.__version__
print ""
def show_min(input):
print ""
arr = np.array(input)
print arr.dtype, arr
print "this should be nan as per docs:", arr.min()
arr = np.array
input = [31., 487., np.nan, 10000., 10000., 19., 101., 22., 1000., 300., 10.]
for x in xrange(3, len(input) + 1):
show_min(input[0:x])
Shows this rather odd behaviour on OSX & Windows, but not Linux.. fresh virtualenv using python 2.7.13 and numpy 1.14.2:
numpy version: 1.14.2
/Users/kip/ac/Environments/test/lib/python2.7/site-packages/numpy/core/_methods.py:29: RuntimeWarning: invalid value encountered in reduce
return umr_minimum(a, axis, None, out, keepdims)
float64 [ 31. 487. nan]
this should be nan as per docs: nan
float64 [ 31. 487. nan 10000.]
this should be nan as per docs: nan
float64 [ 31. 487. nan 10000. 10000.]
this should be nan as per docs: nan
float64 [ 31. 487. nan 10000. 10000. 19.]
this should be nan as per docs: nan
float64 [ 31. 487. nan 10000. 10000. 19. 101.]
this should be nan as per docs: nan
float64 [ 31. 487. nan 10000. 10000. 19. 101. 22.]
this should be nan as per docs: 19.0
float64 [ 31. 487. nan 10000. 10000. 19. 101. 22. 1000.]
this should be nan as per docs: 19.0
float64 [ 31. 487. nan 10000. 10000. 19. 101. 22. 1000. 300.]
this should be nan as per docs: nan
float64 [ 31. 487. nan 10000. 10000. 19. 101. 22. 1000. 300.
10.]
this should be nan as per docs: nan
See the two "this should be nan as per docs: 19.0" lines in there?
Also n.b. the warning does not appear on numpy 1.13.1 (which is where I first observed this behaviour.)
@kippr Where did you get NumPy?
I'm going to guess the eight element boundary may be related to AVX512 and the problem is some combination of compiler and cpu. What cpus/compiler are you seeing the problem on?
@juliantaylor Thoughts?
Alignment might also be having an effect.
Hi @charris
Thanks for taking a look at my post.. to answer your questions:
CPU: ❃ sysctl -n machdep.cpu.brand_string Intel(R) Core(TM) i7-6700K CPU @ 4.00GHz
NumPy was installed through pip on my mac in a fresh virtualenv (python 2.7.13 installed through homebrew), so I guess it uses all the default compiler flags etc?
I just recreated the virtual env and reran pip install, and here is what looks pertinent to me, but there are a lot of messages that come by on install.. (full log attached.) Please let me know if there is other info I can fish out of the build directory or whatnot, or if there are compile flags to try.
[..]
Downloading numpy-1.14.2.zip (4.9MB):
Downloading from URL https://pypi.python.org/packages/0b/66/86185402ee2d55865c675c06a5cfef742e39f4635a4ce1b1aefd20711c13/numpy-1.14.2.zip#md5=080f01a19707cf467393e426382c7619 (from https://pypi.python.org/simple/numpy/)
...Downloading numpy-1.14.2.zip (4.9MB): 4.9MB downloaded
[..]
building library "npymath" sources
get_default_fcompiler: matching types: '['gnu95', 'nag', 'absoft', 'ibm', 'intel', 'gnu', 'g95', 'pg']'
customize Gnu95FCompiler
Found executable /usr/local/bin/gfortran
customize Gnu95FCompiler
customize Gnu95FCompiler using config
C compiler: clang -fno-strict-aliasing -fno-common -dynamic -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/System/Library/Frameworks/Tk.framework/Versions/8.5/Headers -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes
compile options: '-Inumpy/core/src/private -Inumpy/core/src -Inumpy/core -Inumpy/core/src/npymath -Inumpy/core/src/multiarray -Inumpy/core/src/umath -Inumpy/core/src/npysort -I/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c'
clang: _configtest.c
clang _configtest.o -o _configtest
success!
removing: _configtest.c _configtest.o _configtest
Thanks Kris
P.s. probably no surprise, but the same behaviour appears with max:
numpy version: 1.14.2
arr.dtype, arr: float64 [ 31. 487. nan]
/Users/kip/ac/Environments/meh/lib/python2.7/site-packages/numpy/core/_methods.py:29: RuntimeWarning: invalid value encountered in reduce
return umr_minimum(a, axis, None, out, keepdims)
arr.min(): nan
/Users/kip/ac/Environments/meh/lib/python2.7/site-packages/numpy/core/_methods.py:26: RuntimeWarning: invalid value encountered in reduce
return umr_maximum(a, axis, None, out, keepdims)
arr.max(): nan
np.amin(arr): nan
np.nanmin(arr): 31.0
arr.dtype, arr: float64 [ 31. 487. nan 10000.]
arr.min(): nan
arr.max(): nan
np.amin(arr): nan
np.nanmin(arr): 31.0
arr.dtype, arr: float64 [ 31. 487. nan 10000. 10000.]
arr.min(): nan
arr.max(): nan
np.amin(arr): nan
np.nanmin(arr): 31.0
arr.dtype, arr: float64 [ 31. 487. nan 10000. 10000. 19.]
arr.min(): nan
arr.max(): nan
np.amin(arr): nan
np.nanmin(arr): 19.0
arr.dtype, arr: float64 [ 31. 487. nan 10000. 10000. 19. 101.]
arr.min(): nan
arr.max(): nan
np.amin(arr): nan
np.nanmin(arr): 19.0
arr.dtype, arr: float64 [ 31. 487. nan 10000. 10000. 19. 101. 22.]
arr.min(): 19.0
arr.max(): 10000.0
np.amin(arr): 19.0
np.nanmin(arr): 19.0
arr.dtype, arr: float64 [ 31. 487. nan 10000. 10000. 19. 101. 22. 1000.]
arr.min(): 19.0
arr.max(): 10000.0
np.amin(arr): 19.0
np.nanmin(arr): 19.0
arr.dtype, arr: float64 [ 31. 487. nan 10000. 10000. 19. 101. 22. 1000. 300.]
arr.min(): nan
arr.max(): nan
np.amin(arr): nan
np.nanmin(arr): 19.0
arr.dtype, arr: float64 [ 31. 487. nan 10000. 10000. 19. 101. 22. 1000. 300.
10.]
arr.min(): nan
arr.max(): nan
np.amin(arr): nan
np.nanmin(arr): 10.0
I am using the same numpy version (on arch) with a CPU which also has AVX2 and sse4.2 capabilities and not seeing it on linux. Maybe it has something to do with mac/clang?
EDIT/Ammendment: (I had tried around a bit with provoking different alignments, did not check the warning part, but the wrong result part I do not see)
I'm guessing that there is a cpu flag that is not getting set correctly, probably compiler related.
We should have a test that turns up this problem, if only to track it. @kippr What does
np.min(np.diagflat([np.nan]*10), axis=0)
do on your installation?
Hi @charris
That seems ok:
In [1]: import numpy as np
In [2]: np.min(np.diagflat([np.nan]*10), axis=0)
Out[2]: array([ nan, nan, nan, nan, nan, nan, nan, nan, nan, nan])
But I tried a few more combos and found the following (see last one, 8 values and axis=1):
In [3]: np.min(np.diagflat([np.nan]*10), axis=1)
Out[3]: array([ nan, nan, nan, nan, nan, nan, nan, nan, nan, nan])
In [4]: np.min(np.diagflat([np.nan]*8), axis=0)
Out[4]: array([ nan, nan, nan, nan, nan, nan, nan, nan])
In [5]: np.min(np.diagflat([np.nan]*8), axis=1)
Out[5]: array([ nan, nan, 0., 0., nan, 0., nan, nan])
Quite mysterious.. not sure if that helps understand cause at all.
@seberg - yes that's right, I observed this behaviour on my macs and also on windows, but linux was fine.
Thanks Kris
@kippr This is a really disturbing bug, as it affects a basic operation and the problem probably doesn't seem to originate with NumPy. We can try turning off vectorization on MAC and Windows and see if that helps. Do you only see this problem on Python 2.7?
I cannot see anything in NumPy that would have changed this behavior for 1.14. Maybe OpenBLAS is fiddling with the controls ...