diff options
Diffstat (limited to 'numpy/random/tests/test_random.py')
-rw-r--r-- | numpy/random/tests/test_random.py | 88 |
1 files changed, 50 insertions, 38 deletions
diff --git a/numpy/random/tests/test_random.py b/numpy/random/tests/test_random.py index 0e7396494..a530b9e13 100644 --- a/numpy/random/tests/test_random.py +++ b/numpy/random/tests/test_random.py @@ -3,15 +3,16 @@ import warnings import numpy as np from numpy.testing import ( - TestCase, run_module_suite, assert_, assert_raises, assert_equal, - assert_warns, assert_no_warnings, assert_array_equal, - assert_array_almost_equal, suppress_warnings) + run_module_suite, assert_, assert_raises, assert_equal, assert_warns, + assert_no_warnings, assert_array_equal, assert_array_almost_equal, + suppress_warnings + ) from numpy import random import sys import warnings -class TestSeed(TestCase): +class TestSeed(object): def test_scalar(self): s = np.random.RandomState(0) assert_equal(s.randint(1000), 684) @@ -42,7 +43,7 @@ class TestSeed(TestCase): assert_raises(ValueError, np.random.RandomState, [1, -2, 4294967296]) -class TestBinomial(TestCase): +class TestBinomial(object): def test_n_zero(self): # Tests the corner case of n == 0 for the binomial distribution. # binomial(0, p) should be zero for any p in [0, 1]. @@ -57,7 +58,7 @@ class TestBinomial(TestCase): assert_raises(ValueError, random.binomial, 1, np.nan) -class TestMultinomial(TestCase): +class TestMultinomial(object): def test_basic(self): random.multinomial(100, [0.2, 0.8]) @@ -82,11 +83,11 @@ class TestMultinomial(TestCase): (2, 2, 2)) assert_raises(TypeError, np.random.multinomial, 1, p, - np.float(1)) + float(1)) -class TestSetState(TestCase): - def setUp(self): +class TestSetState(object): + def setup(self): self.seed = 1234567890 self.prng = random.RandomState(self.seed) self.state = self.prng.get_state() @@ -133,7 +134,7 @@ class TestSetState(TestCase): self.prng.negative_binomial(0.5, 0.5) -class TestRandint(TestCase): +class TestRandint(object): rfunc = np.random.randint @@ -142,7 +143,7 @@ class TestRandint(TestCase): np.int32, np.uint32, np.int64, np.uint64] def test_unsupported_type(self): - assert_raises(TypeError, self.rfunc, 1, dtype=np.float) + assert_raises(TypeError, self.rfunc, 1, dtype=float) def test_bounds_checking(self): for dt in self.itype: @@ -199,7 +200,7 @@ class TestRandint(TestCase): def test_repeatability(self): import hashlib # We use a md5 hash of generated sequences of 1000 samples - # in the range [0, 6) for all but np.bool, where the range + # in the range [0, 6) for all but bool, where the range # is [0, 2). Hashes are for little endian numbers. tgt = {'bool': '7dd3170d7aa461d201a65f8bcf3944b0', 'int16': '1b7741b80964bb190c50d541dca1cac1', @@ -225,9 +226,9 @@ class TestRandint(TestCase): # bools do not depend on endianess np.random.seed(1234) - val = self.rfunc(0, 2, size=1000, dtype=np.bool).view(np.int8) + val = self.rfunc(0, 2, size=1000, dtype=bool).view(np.int8) res = hashlib.md5(val).hexdigest() - assert_(tgt[np.dtype(np.bool).name] == res) + assert_(tgt[np.dtype(bool).name] == res) def test_int64_uint64_corner_case(self): # When stored in Numpy arrays, `lbnd` is casted @@ -259,23 +260,23 @@ class TestRandint(TestCase): ubnd = 2 if dt is np.bool_ else np.iinfo(dt).max + 1 sample = self.rfunc(lbnd, ubnd, dtype=dt) - self.assertEqual(sample.dtype, np.dtype(dt)) + assert_equal(sample.dtype, np.dtype(dt)) - for dt in (np.bool, np.int, np.long): - lbnd = 0 if dt is np.bool else np.iinfo(dt).min - ubnd = 2 if dt is np.bool else np.iinfo(dt).max + 1 + for dt in (bool, int, np.long): + lbnd = 0 if dt is bool else np.iinfo(dt).min + ubnd = 2 if dt is bool else np.iinfo(dt).max + 1 # gh-7284: Ensure that we get Python data types sample = self.rfunc(lbnd, ubnd, dtype=dt) - self.assertFalse(hasattr(sample, 'dtype')) - self.assertEqual(type(sample), dt) + assert_(not hasattr(sample, 'dtype')) + assert_equal(type(sample), dt) -class TestRandomDist(TestCase): +class TestRandomDist(object): # Make sure the random distribution returns the correct value for a # given seed - def setUp(self): + def setup(self): self.seed = 1234567890 def test_rand(self): @@ -522,7 +523,12 @@ class TestRandomDist(TestCase): assert_equal(np.random.dirichlet(p, (2, 2)).shape, (2, 2, 2)) assert_equal(np.random.dirichlet(p, np.array((2, 2))).shape, (2, 2, 2)) - assert_raises(TypeError, np.random.dirichlet, p, np.float(1)) + assert_raises(TypeError, np.random.dirichlet, p, float(1)) + + def test_dirichlet_bad_alpha(self): + # gh-2089 + alpha = np.array([5.4e-01, -1.0e-16]) + assert_raises(ValueError, np.random.mtrand.dirichlet, alpha) def test_exponential(self): np.random.seed(self.seed) @@ -929,10 +935,10 @@ class TestRandomDist(TestCase): assert_array_equal(actual, desired) -class TestBroadcast(TestCase): +class TestBroadcast(object): # tests that functions that broadcast behave # correctly when presented with non-scalar arguments - def setUp(self): + def setup(self): self.seed = 123456789 def setSeed(self): @@ -1100,7 +1106,13 @@ class TestBroadcast(TestCase): 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) - + + def test_noncentral_f_small_df(self): + self.setSeed() + desired = np.array([6.869638627492048, 0.785880199263955]) + actual = np.random.noncentral_f(0.9, 0.9, 2, size=2) + assert_array_almost_equal(actual, desired, decimal=14) + def test_chisquare(self): df = [1] bad_df = [-1] @@ -1484,9 +1496,9 @@ class TestBroadcast(TestCase): assert_raises(ValueError, logseries, bad_p_one * 3) assert_raises(ValueError, logseries, bad_p_two * 3) -class TestThread(TestCase): +class TestThread(object): # make sure each state produces the same sequence even in threads - def setUp(self): + def setup(self): self.seeds = range(4) def check_function(self, function, sz): @@ -1527,8 +1539,8 @@ class TestThread(TestCase): self.check_function(gen_random, sz=(10000, 6)) # See Issue #4263 -class TestSingleEltArrayInput(TestCase): - def setUp(self): +class TestSingleEltArrayInput(object): + def setup(self): self.argOne = np.array([2]) self.argTwo = np.array([3]) self.argThree = np.array([4]) @@ -1551,7 +1563,7 @@ class TestSingleEltArrayInput(TestCase): else: out = func(self.argOne) - self.assertEqual(out.shape, self.tgtShape) + assert_equal(out.shape, self.tgtShape) def test_two_arg_funcs(self): funcs = (np.random.uniform, np.random.normal, @@ -1572,17 +1584,17 @@ class TestSingleEltArrayInput(TestCase): argTwo = self.argTwo out = func(self.argOne, argTwo) - self.assertEqual(out.shape, self.tgtShape) + assert_equal(out.shape, self.tgtShape) out = func(self.argOne[0], argTwo) - self.assertEqual(out.shape, self.tgtShape) + assert_equal(out.shape, self.tgtShape) out = func(self.argOne, argTwo[0]) - self.assertEqual(out.shape, self.tgtShape) + assert_equal(out.shape, self.tgtShape) # TODO: Uncomment once randint can broadcast arguments # def test_randint(self): -# itype = [np.bool, np.int8, np.uint8, np.int16, np.uint16, +# itype = [bool, np.int8, np.uint8, np.int16, np.uint16, # np.int32, np.uint32, np.int64, np.uint64] # func = np.random.randint # high = np.array([1]) @@ -1604,13 +1616,13 @@ class TestSingleEltArrayInput(TestCase): for func in funcs: out = func(self.argOne, self.argTwo, self.argThree) - self.assertEqual(out.shape, self.tgtShape) + assert_equal(out.shape, self.tgtShape) out = func(self.argOne[0], self.argTwo, self.argThree) - self.assertEqual(out.shape, self.tgtShape) + assert_equal(out.shape, self.tgtShape) out = func(self.argOne, self.argTwo[0], self.argThree) - self.assertEqual(out.shape, self.tgtShape) + assert_equal(out.shape, self.tgtShape) if __name__ == "__main__": run_module_suite() |