Skip to content

TST: Add tests for nan guards #6

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

Merged
merged 1 commit into from
Apr 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions numpy/random/randomgen/common.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,9 @@ cdef int check_constraint(double val, object name, constraint_type cons) except
elif cons == CONS_BOUNDED_0_1:
if not (val >= 0) or not (val <= 1):
raise ValueError("{0} < 0 , {0} > 1 or {0} is NaN".format(name))
elif cons == CONS_BOUNDED_GT_0_1:
if not val >0 or not val <= 1:
raise ValueError("{0} <= 0 , {0} > 1 or {0} contains NaNs".format(name))
elif cons == CONS_GT_1:
if not (val > 1):
raise ValueError("{0} <= 1 or {0} is NaN".format(name))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ double legacy_gauss(aug_brng_t *aug_state) {
return temp;
} else {
double f, x1, x2, r2;
double f, x1, x2, r2;

do {
x1 = 2.0 * legacy_double(aug_state) - 1.0;
Expand Down
6 changes: 3 additions & 3 deletions numpy/random/randomgen/tests/test_against_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,9 +570,9 @@ def test_multivariate_normal(self):
self._is_state_common_legacy()


funcs = [randomgen.generator.zipf,
randomgen.generator.logseries,
randomgen.generator.poisson]
funcs = [generator.zipf,
generator.logseries,
generator.poisson]
ids = [f.__name__ for f in funcs]


Expand Down
6 changes: 3 additions & 3 deletions numpy/random/randomgen/tests/test_direct.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,14 +233,14 @@ def test_seed_out_of_range_array(self):

def test_repr(self):
rs = RandomGenerator(self.brng(*self.data1['seed']))
assert 'RandomGenerator' in rs.__repr__()
assert str(hex(id(rs)))[2:].upper() in rs.__repr__()
assert 'RandomGenerator' in repr(rs)
assert '{:#x}'.format(id(rs)).upper().replace('X', 'x') in repr(rs)

def test_str(self):
rs = RandomGenerator(self.brng(*self.data1['seed']))
assert 'RandomGenerator' in str(rs)
assert str(self.brng.__name__) in str(rs)
assert str(hex(id(rs)))[2:].upper() not in str(rs)
assert '{:#x}'.format(id(rs)).upper().replace('X', 'x') not in str(rs)

def test_generator(self):
brng = self.brng(*self.data1['seed'])
Expand Down
127 changes: 84 additions & 43 deletions numpy/random/randomgen/tests/test_generator_mt19937.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ def test_n_zero(self):
# This test addresses issue #3480.
zeros = np.zeros(2, dtype='int')
for p in [0, .5, 1]:
val = random.binomial(0, p)
assert val == 0
assert_(random.binomial(0, p) == 0)
assert_array_equal(random.binomial(zeros, p), zeros)

def test_p_is_nan(self):
Expand Down Expand Up @@ -96,40 +95,40 @@ def test_invalid_prob(self):
class TestSetState(object):
def setup(self):
self.seed = 1234567890
self.brng = RandomGenerator(MT19937(self.seed))
self.state = self.brng.state
self.rg = RandomGenerator(MT19937(self.seed))
self.state = self.rg.state
self.legacy_state = (self.state['brng'],
self.state['state']['key'],
self.state['state']['pos'])

def test_basic(self):
old = self.brng.tomaxint(16)
self.brng.state = self.state
new = self.brng.tomaxint(16)
old = self.rg.tomaxint(16)
self.rg.state = self.state
new = self.rg.tomaxint(16)
assert_(np.all(old == new))

def test_gaussian_reset(self):
# Make sure the cached every-other-Gaussian is reset.
old = self.brng.standard_normal(size=3)
self.brng.state = self.state
new = self.brng.standard_normal(size=3)
old = self.rg.standard_normal(size=3)
self.rg.state = self.state
new = self.rg.standard_normal(size=3)
assert_(np.all(old == new))

def test_gaussian_reset_in_media_res(self):
# When the state is saved with a cached Gaussian, make sure the
# cached Gaussian is restored.

self.brng.standard_normal()
state = self.brng.state
old = self.brng.standard_normal(size=3)
self.brng.state = state
new = self.brng.standard_normal(size=3)
self.rg.standard_normal()
state = self.rg.state
old = self.rg.standard_normal(size=3)
self.rg.state = state
new = self.rg.standard_normal(size=3)
assert_(np.all(old == new))

def test_negative_binomial(self):
# Ensure that the negative binomial results take floating point
# arguments without truncation.
self.brng.negative_binomial(0.5, 0.5)
self.rg.negative_binomial(0.5, 0.5)


class TestRandint(object):
Expand Down Expand Up @@ -554,8 +553,7 @@ def test_choice_uniform_noreplace(self):

def test_choice_nonuniform_noreplace(self):
random.seed(self.seed)
actual = random.choice(4, 3, replace=False,
p=[0.1, 0.3, 0.5, 0.1])
actual = random.choice(4, 3, replace=False, p=[0.1, 0.3, 0.5, 0.1])
desired = np.array([2, 3, 1])
assert_array_equal(actual, desired)

Expand Down Expand Up @@ -614,11 +612,11 @@ def test_choice_return_shape(self):
# Check multi dimensional array
s = (2, 3)
p = [0.1, 0.1, 0.1, 0.1, 0.4, 0.2]
assert_(random.choice(6, s, replace=True).shape, s)
assert_(random.choice(6, s, replace=False).shape, s)
assert_(random.choice(6, s, replace=True, p=p).shape, s)
assert_(random.choice(6, s, replace=False, p=p).shape, s)
assert_(random.choice(np.arange(6), s, replace=True).shape, s)
assert_equal(random.choice(6, s, replace=True).shape, s)
assert_equal(random.choice(6, s, replace=False).shape, s)
assert_equal(random.choice(6, s, replace=True, p=p).shape, s)
assert_equal(random.choice(6, s, replace=False, p=p).shape, s)
assert_equal(random.choice(np.arange(6), s, replace=True).shape, s)

# Check zero-size
assert_equal(random.randint(0, 0, size=(3, 0, 4)).shape, (3, 0, 4))
Expand Down Expand Up @@ -711,6 +709,11 @@ def test_binomial(self):
[46, 45]])
assert_array_equal(actual, desired)

random.seed(self.seed)
actual = random.binomial(100.123, .456)
desired = 37
assert_array_equal(actual, desired)

def test_chisquare(self):
random.seed(self.seed)
actual = random.chisquare(50, size=(3, 2))
Expand Down Expand Up @@ -795,6 +798,16 @@ def test_geometric(self):
[5, 12]])
assert_array_equal(actual, desired)

def test_geometric_exceptions(self):
assert_raises(ValueError, random.geometric, 1.1)
assert_raises(ValueError, random.geometric, [1.1] * 10)
assert_raises(ValueError, random.geometric, -0.1)
assert_raises(ValueError, random.geometric, [-0.1] * 10)
with suppress_warnings() as sup:
sup.record(RuntimeWarning)
assert_raises(ValueError, random.geometric, np.nan)
assert_raises(ValueError, random.geometric, [np.nan] * 10)

def test_gumbel(self):
random.seed(self.seed)
actual = random.gumbel(loc=.123456789, scale=2.0, size=(3, 2))
Expand Down Expand Up @@ -873,6 +886,12 @@ def test_logseries(self):
[3, 6]])
assert_array_equal(actual, desired)

def test_logseries_exceptions(self):
with suppress_warnings() as sup:
sup.record(RuntimeWarning)
assert_raises(ValueError, random.logseries, np.nan)
assert_raises(ValueError, random.logseries, [np.nan] * 10)

def test_multinomial(self):
random.seed(self.seed)
actual = random.multinomial(20, [1 / 6.] * 6, size=(3, 2))
Expand Down Expand Up @@ -943,6 +962,13 @@ def test_negative_binomial(self):
[723, 751]])
assert_array_equal(actual, desired)

def test_negative_binomial_exceptions(self):
with suppress_warnings() as sup:
sup.record(RuntimeWarning)
assert_raises(ValueError, random.negative_binomial, 100, np.nan)
assert_raises(ValueError, random.negative_binomial, 100,
[np.nan] * 10)

def test_noncentral_chisquare(self):
random.seed(self.seed)
actual = random.noncentral_chisquare(df=5, nonc=5, size=(3, 2))
Expand Down Expand Up @@ -973,6 +999,11 @@ def test_noncentral_f(self):
[1.16362730891403, 2.54104276581491]])
assert_array_almost_equal(actual, desired, decimal=14)

def test_noncentral_f_nan(self):
random.seed(self.seed)
actual = random.noncentral_f(dfnum=5, dfden=2, nonc=np.nan)
assert np.isnan(actual)

def test_normal(self):
random.seed(self.seed)
actual = random.normal(loc=.123456789, scale=2.0, size=(3, 2))
Expand All @@ -993,7 +1024,7 @@ def test_pareto(self):
[1.1281132447159091e+01, 3.1895968171107006e+08]])
# For some reason on 32-bit x86 Ubuntu 12.10 the [1, 0] entry in this
# matrix differs by 24 nulps. Discussion:
# http://mail.scipy.org/pipermail/numpy-discussion/2012-September/063801.html
# https://mail.python.org/pipermail/numpy-discussion/2012-September/063801.html
# Consensus is that this is probably some gcc quirk that affects
# rounding but not in any important way, so we just use a looser
# tolerance on this test:
Expand All @@ -1014,6 +1045,10 @@ def test_poisson_exceptions(self):
assert_raises(ValueError, random.poisson, [lamneg] * 10)
assert_raises(ValueError, random.poisson, lambig)
assert_raises(ValueError, random.poisson, [lambig] * 10)
with suppress_warnings() as sup:
sup.record(RuntimeWarning)
assert_raises(ValueError, random.poisson, np.nan)
assert_raises(ValueError, random.poisson, [np.nan] * 10)

def test_power(self):
random.seed(self.seed)
Expand Down Expand Up @@ -1168,8 +1203,8 @@ def __float__(self):
raise TypeError

throwing_float = np.array(1.0).view(ThrowingFloat)
assert_raises(TypeError, random.uniform,
throwing_float, throwing_float)
assert_raises(TypeError, random.uniform, throwing_float,
throwing_float)

class ThrowingInteger(np.ndarray):
def __int__(self):
Expand All @@ -1189,9 +1224,14 @@ def test_vonmises(self):
def test_vonmises_small(self):
# check infinite loop, gh-4720
random.seed(self.seed)
r = random.vonmises(mu=0., kappa=1.1e-8, size=10 ** 6)
r = random.vonmises(mu=0., kappa=1.1e-8, size=10**6)
assert_(np.isfinite(r).all())

def test_vonmises_nan(self):
random.seed(self.seed)
r = random.vonmises(mu=0., kappa=np.nan)
assert_(np.isnan(r))

def test_wald(self):
random.seed(self.seed)
actual = random.wald(mean=1.23, scale=1.54, size=(3, 2))
Expand Down Expand Up @@ -1372,8 +1412,9 @@ def test_noncentral_f(self):

self.set_seed()
actual = nonc_f(dfnum * 3, dfden, nonc)
mt_nonc_f = random.noncentral_f
assert_array_almost_equal(actual, desired, decimal=14)
assert np.all(np.isnan(nonc_f(dfnum, dfden, [np.nan] * 3)))

assert_raises(ValueError, nonc_f, bad_dfnum * 3, dfden, nonc)
assert_raises(ValueError, nonc_f, dfnum * 3, bad_dfden, nonc)
assert_raises(ValueError, nonc_f, dfnum * 3, dfden, bad_nonc)
Expand All @@ -1391,9 +1432,12 @@ def test_noncentral_f(self):
assert_raises(ValueError, nonc_f, bad_dfnum, dfden, nonc * 3)
assert_raises(ValueError, nonc_f, dfnum, bad_dfden, nonc * 3)
assert_raises(ValueError, nonc_f, dfnum, dfden, bad_nonc * 3)
assert_raises(ValueError, mt_nonc_f, bad_dfnum, dfden, nonc * 3)
assert_raises(ValueError, mt_nonc_f, dfnum, bad_dfden, nonc * 3)
assert_raises(ValueError, mt_nonc_f, dfnum, dfden, bad_nonc * 3)

def test_noncentral_f_small_df(self):
self.set_seed()
desired = np.array([21.57878070681719, 1.17110217503908])
actual = random.noncentral_f(0.9, 0.9, 2, size=2)
assert_array_almost_equal(actual, desired, decimal=14)

def test_chisquare(self):
df = [1]
Expand All @@ -1420,20 +1464,15 @@ def test_noncentral_chisquare(self):

self.set_seed()
actual = nonc_chi(df * 3, nonc)
mt_nonc_chi2 = random.noncentral_chisquare
assert_array_almost_equal(actual, desired, decimal=14)
assert_raises(ValueError, nonc_chi, bad_df * 3, nonc)
assert_raises(ValueError, nonc_chi, df * 3, bad_nonc)
assert_raises(ValueError, mt_nonc_chi2, bad_df * 3, nonc)
assert_raises(ValueError, mt_nonc_chi2, df * 3, bad_nonc)

self.set_seed()
actual = nonc_chi(df, nonc * 3)
assert_array_almost_equal(actual, desired, decimal=14)
assert_raises(ValueError, nonc_chi, bad_df, nonc * 3)
assert_raises(ValueError, nonc_chi, df, bad_nonc * 3)
assert_raises(ValueError, mt_nonc_chi2, bad_df, nonc * 3)
assert_raises(ValueError, mt_nonc_chi2, df, bad_nonc * 3)

def test_standard_t(self):
df = [1]
Expand Down Expand Up @@ -1645,24 +1684,24 @@ def test_triangular(self):
assert_array_almost_equal(actual, desired, decimal=14)
assert_raises(ValueError, triangular, bad_left_one * 3, mode, right)
assert_raises(ValueError, triangular, left * 3, bad_mode_one, right)
assert_raises(ValueError, triangular,
bad_left_two * 3, bad_mode_two, right)
assert_raises(ValueError, triangular, bad_left_two * 3, bad_mode_two,
right)

self.set_seed()
actual = triangular(left, mode * 3, right)
assert_array_almost_equal(actual, desired, decimal=14)
assert_raises(ValueError, triangular, bad_left_one, mode * 3, right)
assert_raises(ValueError, triangular, left, bad_mode_one * 3, right)
assert_raises(ValueError, triangular, bad_left_two,
bad_mode_two * 3, right)
assert_raises(ValueError, triangular, bad_left_two, bad_mode_two * 3,
right)

self.set_seed()
actual = triangular(left, mode, right * 3)
assert_array_almost_equal(actual, desired, decimal=14)
assert_raises(ValueError, triangular, bad_left_one, mode, right * 3)
assert_raises(ValueError, triangular, left, bad_mode_one, right * 3)
assert_raises(ValueError, triangular, bad_left_two,
bad_mode_two, right * 3)
assert_raises(ValueError, triangular, bad_left_two, bad_mode_two,
right * 3)

assert_raises(ValueError, triangular, 10., 0., 20.)
assert_raises(ValueError, triangular, 10., 25., 20.)
Expand Down Expand Up @@ -1739,6 +1778,9 @@ def test_zipf(self):
actual = zipf(a * 3)
assert_array_equal(actual, desired)
assert_raises(ValueError, zipf, bad_a * 3)
with np.errstate(invalid='ignore'):
assert_raises(ValueError, zipf, np.nan)
assert_raises(ValueError, zipf, [0, 0, np.nan])

def test_geometric(self):
p = [0.5]
Expand Down Expand Up @@ -1809,7 +1851,6 @@ def test_logseries(self):

class TestThread(object):
# make sure each state produces the same sequence even in threads

def setup(self):
self.seeds = range(4)

Expand Down
Loading