Skip to content

Commit

Permalink
ENH: Warn on integers/randint endainess settings
Browse files Browse the repository at this point in the history
Lets uers know this is not supported
  • Loading branch information
bashtage committed May 28, 2019
1 parent 5920ca5 commit b2a041d
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 10 deletions.
29 changes: 22 additions & 7 deletions randomgen/generator.pyx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!python
#cython: wraparound=False, nonecheck=False, boundscheck=False, cdivision=True, language_level=3
import operator
import warnings

import numpy as np

Expand Down Expand Up @@ -611,9 +610,16 @@ cdef class Generator:
high = low
low = 0

key = np.dtype(dtype).name
dt = np.dtype(dtype)
key = dt.name
if key not in _integers_types:
raise TypeError('Unsupported dtype "%s" for integers' % key)
if dt.byteorder != '=' and dt.byteorder != '|':
import warnings
warnings.warn('Byteorder is not supported. If you require '
'platform-independent byteorder, call byteswap when '
'required.\n\nIn future version, specifying '
'byteorder will raise a ValueError', FutureWarning)

if key == 'int32':
ret = _rand_int32(low, high, size, _use_masked, endpoint, &self._bitgen, self.lock)
Expand Down Expand Up @@ -1199,13 +1205,15 @@ cdef class Generator:
"""
if high is None:
import warnings
warnings.warn(("This function is deprecated. Please call "
"integers(1, {low} + 1) instead".format(low=low)),
DeprecationWarning)
high = low
low = 1

else:
import warnings
warnings.warn(("This function is deprecated. Please call "
"integers({low}, {high} + 1)"
"instead".format(low=low, high=high)),
Expand Down Expand Up @@ -3726,8 +3734,10 @@ cdef class Generator:
Diagonal covariance means that points are oriented along x or y-axis:
>>> from randomgen import Generator
>>> rg = Generator()
>>> import matplotlib.pyplot as plt
>>> x, y = randomgen.generator.multivariate_normal(mean, cov, 5000).T
>>> x, y = rg.multivariate_normal(mean, cov, 5000).T
>>> plt.plot(x, y, 'x')
>>> plt.axis('equal')
>>> plt.show()
Expand All @@ -3745,9 +3755,11 @@ cdef class Generator:
Examples
--------
>>> from randomgen import Generator
>>> rg = Generator()
>>> mean = (1, 2)
>>> cov = [[1, 0], [0, 1]]
>>> x = randomgen.generator.multivariate_normal(mean, cov, (3, 3))
>>> x = rg.multivariate_normal(mean, cov, (3, 3))
>>> x.shape
(3, 3, 2)
Expand Down Expand Up @@ -3810,6 +3822,7 @@ cdef class Generator:
psd = np.allclose(np.dot(v.T * s, v), cov, rtol=tol, atol=tol)
if not psd:
if check_valid == 'warn':
import warnings
warnings.warn("covariance is not positive-semidefinite.",
RuntimeWarning)
else:
Expand Down Expand Up @@ -4237,14 +4250,16 @@ cdef class Generator:
Examples
--------
>>> randomgen.generator.permutation(10)
>>> from randomgen import Generator
>>> gen = Generator()
>>> gen.permutation(10)
array([1, 7, 4, 3, 0, 9, 2, 5, 8, 6]) # random
>>> randomgen.generator.permutation([1, 4, 9, 12, 15])
>>> gen.permutation([1, 4, 9, 12, 15])
array([15, 1, 9, 4, 12]) # random
>>> arr = np.arange(9).reshape((3, 3))
>>> randomgen.generator.permutation(arr)
>>> gen.permutation(arr)
array([[6, 7, 8], # random
[0, 1, 2],
[3, 4, 5]])
Expand Down
10 changes: 8 additions & 2 deletions randomgen/mtrand.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -597,10 +597,16 @@ cdef class RandomState:
if high is None:
high = low
low = 0

key = np.dtype(dtype).name
dt = np.dtype(dtype)
key = dt.name
if key not in _integers_types:
raise TypeError('Unsupported dtype "%s" for randint' % key)
if dt.byteorder != '=' and dt.byteorder != '|':
import warnings
warnings.warn('Byteorder is not supported. If you require '
'platform-independent byteorder, call byteswap when '
'required.\n\nIn future version, specifying '
'byteorder will raise a ValueError', FutureWarning)

if key == 'int32':
ret = _legacy_rand_int32(low, high, size, &self._aug_state, self.lock)
Expand Down
2 changes: 1 addition & 1 deletion randomgen/tests/test_against_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ def test_dir(self):
'division', 'get_state', 'set_state', 'seed',
'ranf', 'random', 'sample', 'absolute_import',
'print_function', 'RandomState', 'Lock',
'randint']
'randint', 'warnings']
mod += known_exlcuded
diff = set(npmod).difference(mod)
print(diff)
Expand Down
5 changes: 5 additions & 0 deletions randomgen/tests/test_generator_mt19937.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,11 @@ def test_zero_size(self, endpoint):
assert_equal(random.integers(0, -10, size=0).shape, (0,))
assert_equal(random.integers(10, 10, size=0).shape, (0,))

def test_warns_byteorder(self):
other_byteord_dt = '<i4' if sys.byteorder == 'big' else '>i4'
with pytest.warns(FutureWarning):
random.integers(0, 200, size=10, dtype=other_byteord_dt)


class TestRandomDist(object):
# Make sure the random distribution returns the correct value for a
Expand Down
7 changes: 7 additions & 0 deletions randomgen/tests/test_randomstate_regression.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import sys

import pytest

import numpy as np
from numpy.compat import long
from numpy.testing import assert_, assert_array_equal, assert_raises
Expand Down Expand Up @@ -154,3 +156,8 @@ def __array__(self):
perm = random.permutation(m)
assert_array_equal(perm, np.array([2, 1, 4, 0, 3]))
assert_array_equal(m.__array__(), np.arange(5))

def test_warns_byteorder(self):
other_byteord_dt = '<i4' if sys.byteorder == 'big' else '>i4'
with pytest.warns(FutureWarning):
random.randint(0, 200, size=10, dtype=other_byteord_dt)

0 comments on commit b2a041d

Please sign in to comment.