8000 Platform-dependent behavior of either numpy.random.normal or numpy.abs · Issue #13189 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content
8000

Platform-dependent behavior of either numpy.random.normal or numpy.abs #13189

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
oceandatalab opened this issue Mar 26, 2019 · 2 comments
Open

Comments

@oceandatalab
Copy link
oceandatalab commented Mar 26, 2019

It seems the behavior of either numpy.random.normal or numpy.abs is not consistent across platforms (at least on Linux and macOS). We use the output of numpy.abs as the scale parameter passed to numpy.random.normal: a ValueError is raised when the array passed to numpy.abs contains a NaN value on some systems whereas the statement is executed without error on others.

Besides, when passing numpy.array([numpy.nan]) (which is the output of numpy.abs(numpy.array([numpy.nan]))) as the scale parameter, there is no error, so there might be a problem with the in-memory representation of the result returned by numpy.abs on some systems.

We tested our code on Linux and didn't check for NaNs at first: since we have been facing issues with wheels on multiple Python packages lately, our first move has been to reinstall NumPy from the source distribution with:

pip uninstall -y numpy
pip install --nobinary :all: numpy

and with this the ValueError went away, so we thought it was just another issue caused by the libraries linked in the wheel.

But then we tested on macOS and this workaround didn't work. Reinstalling both Cython and NumPy with --no-binary :all: had no effect.

Sorry if this is a bit confuse, the example code and the outputs provided below should make things clearer.

Reproducing code example:

import numpy
numpy.abs(numpy.array([numpy.nan]))
numpy.random.normal(0.0, numpy.array([numpy.nan]), 1)
numpy.random.normal(0.0, numpy.abs(numpy.array([numpy.nan])), 1)

Error message:

Traceback (most recent call last)
<ipython-input-5-26abddb224ea> in <module>
----> 1 numpy.random.normal(0, numpy.abs(numpy.array([numpy.nan])), 1)

mtrand.pyx in mtrand.RandomState.normal()

ValueError: scale < 0

On Linux with a NumPy package installed using the wheel:

Python 3.7.2 (default, Jan 10 2019, 23:51:51)
[GCC 8.2.1 20181127] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> numpy.abs(numpy.array([numpy.nan]))
array([nan])
>>> numpy.random.normal(0.0, numpy.array([numpy.nan]), 1)
array([nan])
>>> numpy.random.normal(0.0, numpy.abs(numpy.array([numpy.nan])), 1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "mtrand.pyx", line 1658, in mtrand.RandomState.normal
ValueError: scale < 0

On Linux with a NumPy package reinstalled from source:

Python 3.7.2 (default, Jan 10 2019, 23:51:51)
[GCC 8.2.1 20181127] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> numpy.abs(numpy.array([numpy.nan]))
array([nan])
>>> numpy.random.normal(0.0, numpy.array([numpy.nan]), 1)
array([nan])
>>> numpy.random.normal(0.0, numpy.abs(numpy.array([numpy.nan])), 1)
array([nan])

On macOS with a NumPy package reinstalled from source (the wheel version fails as well):

Python 3.7.2 (default, Feb 12 2019, 08:15:36)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.2.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import numpy

In [2]: numpy.abs(numpy.array([numpy.nan]))
Out[2]: array([nan])

In [3]: numpy.random.normal(0.0, numpy.array([numpy.nan]), 1)
Out[3]: array([nan])

In [4]: numpy.random.normal(0, numpy.abs(numpy.array([numpy.nan])), 1)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-3-26abddb224ea> in <module>
----> 1 numpy.random.normal(0, numpy.abs(numpy.array([numpy.nan])), 1)

mtrand.pyx in mtrand.RandomState.normal()

ValueError: scale < 0

Numpy/Python version information:

On ArchLinux:

import sys, numpy; print(numpy.__version__, sys.version)
1.16.2 3.7.2 (default, Jan 10 2019, 23:51:51)
[GCC 8.2.1 20181127]

On mac OS mojave (10.14.3):

import sys, numpy; print(numpy.__version__, sys.version)
1.16.2 3.7.2 (default, Feb 12 2019, 08:15:36)
[Clang 10.0.0 (clang-1000.11.45.5)]
@seberg
Copy link
Member
seberg commented Mar 26, 2019

Just to note for whoever looks at it next. This is related to np.signbit (on my arch I get the error as well), which seems to be a bit ill defined for NaN. I am not sure if this check was not even changed recently and one reason for the signbit may even have been NaN?

EDIT: I get False for fresh created NaN always, but abs(NaN) gives me a True signbit.

@bashtage
Copy link
Contributor
bashtage commented Apr 7, 2019

signbit is probably the wrong test, or at least is inconsistent with the documentation, which states that scale must be non-negative (and nan is not non-negative). The randomgen WIP #13163 uses not scale < 0 instead of signbit, although this may not be desirable in all cases.

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

No branches or pull requests

3 participants
0