From 04e40fc5c21335f296009f40f4599b66dc099e6d Mon Sep 17 00:00:00 2001 From: Evgeni Burovski Date: Mon, 2 Nov 2015 13:25:28 +0000 Subject: MAINT: random: allow nonc==0 in noncentral_chisquare. Noncentral chi-square reduces to a central chi-square, so just defer to that. --- numpy/random/tests/test_random.py | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'numpy/random/tests/test_random.py') diff --git a/numpy/random/tests/test_random.py b/numpy/random/tests/test_random.py index 596c218a2..ab7f90d82 100644 --- a/numpy/random/tests/test_random.py +++ b/numpy/random/tests/test_random.py @@ -504,6 +504,13 @@ class TestRandomDist(TestCase): [ 0.332334982684171 , 0.15451287602753125]]) np.testing.assert_array_almost_equal(actual, desired, decimal=14) + np.random.seed(self.seed) + actual = np.random.noncentral_chisquare(df=5, nonc=0, size=(3, 2)) + desired = np.array([[9.597154162763948, 11.725484450296079], + [10.413711048138335, 3.694475922923986], + [13.484222138963087, 14.377255424602957]]) + np.testing.assert_array_almost_equal(actual, desired, decimal=14) + def test_noncentral_f(self): np.random.seed(self.seed) actual = np.random.noncentral_f(dfnum=5, dfden=2, nonc=1, -- cgit v1.2.1 From 084952500f388caed570b0961194a5b76bd8b6be Mon Sep 17 00:00:00 2001 From: gfyoung Date: Sun, 13 Dec 2015 18:11:14 -0800 Subject: MAINT: minor spelling and grammar corrections --- numpy/random/tests/test_random.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'numpy/random/tests/test_random.py') diff --git a/numpy/random/tests/test_random.py b/numpy/random/tests/test_random.py index ab7f90d82..193844030 100644 --- a/numpy/random/tests/test_random.py +++ b/numpy/random/tests/test_random.py @@ -26,12 +26,12 @@ class TestSeed(TestCase): assert_equal(s.randint(1000), 265) def test_invalid_scalar(self): - # seed must be a unsigned 32 bit integers + # seed must be an unsigned 32 bit integer assert_raises(TypeError, np.random.RandomState, -0.5) assert_raises(ValueError, np.random.RandomState, -1) def test_invalid_array(self): - # seed must be a unsigned 32 bit integers + # seed must be an unsigned 32 bit integer assert_raises(TypeError, np.random.RandomState, [-0.5]) assert_raises(ValueError, np.random.RandomState, [-1]) assert_raises(ValueError, np.random.RandomState, [4294967296]) @@ -129,7 +129,7 @@ class TestSetState(TestCase): self.prng.negative_binomial(0.5, 0.5) class TestRandomDist(TestCase): - # Make sure the random distrobution return the correct value for a + # Make sure the random distribution returns the correct value for a # given seed def setUp(self): -- cgit v1.2.1 From d7a68afd3dddb65bd8edad0d484e1333daafa631 Mon Sep 17 00:00:00 2001 From: gfyoung Date: Wed, 23 Dec 2015 21:45:38 -0800 Subject: ENH: Allow random_integers to include the maximum np.iinfo('l').max Redistributes the code between the randint and random_integers methods so that we can generate integers up to and including np.iinfo('l').max with random_integers, which previously would have caused an OverflowError. --- numpy/random/tests/test_random.py | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'numpy/random/tests/test_random.py') diff --git a/numpy/random/tests/test_random.py b/numpy/random/tests/test_random.py index 193844030..0ce341ead 100644 --- a/numpy/random/tests/test_random.py +++ b/numpy/random/tests/test_random.py @@ -167,6 +167,17 @@ class TestRandomDist(TestCase): [-48, -66]]) np.testing.assert_array_equal(actual, desired) + def test_random_integers_max_int(self): + # Tests whether random_integers can generate the + # maximum allowed Python int that can be converted + # into a C long. Previous implementations of this + # method have thrown an OverflowError when attemping + # to generate this integer. + actual = np.random.random_integers(np.iinfo('l').max, + np.iinfo('l').max) + desired = np.iinfo('l').max + np.testing.assert_equal(actual, desired) + def test_random_sample(self): np.random.seed(self.seed) actual = np.random.random_sample((3, 2)) -- cgit v1.2.1 From eebb304a0c91c9f52bc883a352b2520e3ca7c88e Mon Sep 17 00:00:00 2001 From: gfyoung Date: Tue, 29 Dec 2015 23:51:41 -0800 Subject: MAINT: Cleaned up unused variables and spelling mistakes in np.random modules --- numpy/random/tests/test_random.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'numpy/random/tests/test_random.py') diff --git a/numpy/random/tests/test_random.py b/numpy/random/tests/test_random.py index 0ce341ead..c3aa43f0e 100644 --- a/numpy/random/tests/test_random.py +++ b/numpy/random/tests/test_random.py @@ -171,7 +171,7 @@ class TestRandomDist(TestCase): # Tests whether random_integers can generate the # maximum allowed Python int that can be converted # into a C long. Previous implementations of this - # method have thrown an OverflowError when attemping + # method have thrown an OverflowError when attempting # to generate this integer. actual = np.random.random_integers(np.iinfo('l').max, np.iinfo('l').max) -- cgit v1.2.1 From 5aca879e18541eff3daf506786b07a950a075b37 Mon Sep 17 00:00:00 2001 From: Charles Harris Date: Fri, 1 Jan 2016 18:38:44 -0700 Subject: TST: Add tests for new randint functionality. * check exceptions * check extreme bounds are reachable * check that all values are in the specified bounds * check repeatability of sequences More exact statistical tests would be nice, but that is another project. --- numpy/random/tests/test_random.py | 77 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) (limited to 'numpy/random/tests/test_random.py') diff --git a/numpy/random/tests/test_random.py b/numpy/random/tests/test_random.py index 0ce341ead..c2e1adca3 100644 --- a/numpy/random/tests/test_random.py +++ b/numpy/random/tests/test_random.py @@ -128,6 +128,83 @@ class TestSetState(TestCase): # arguments without truncation. self.prng.negative_binomial(0.5, 0.5) +class TestRandint(TestCase): + + rfunc = np.random.randint + + # valid integer/boolean types + itype = [np.bool, np.int8, np.uint8, np.int16, np.uint16, + np.int32, np.uint32, np.int64, np.uint64] + + def test_unsupported_type(self): + assert_raises(TypeError, self.rfunc, 1, dtype=np.float) + + def test_bounds_checking(self): + for dt in self.itype: + 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 + assert_raises(ValueError, self.rfunc, lbnd - 1 , ubnd, dtype=dt) + assert_raises(ValueError, self.rfunc, lbnd , ubnd + 1, dtype=dt) + assert_raises(ValueError, self.rfunc, ubnd , lbnd, dtype=dt) + assert_raises(ValueError, self.rfunc, 1 , 0, dtype=dt) + + def test_rng_zero_and_extremes(self): + for dt in self.itype: + 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 + tgt = ubnd - 1 + assert_equal(self.rfunc(tgt, tgt + 1, size=1000, dtype=dt), tgt) + tgt = lbnd + assert_equal(self.rfunc(tgt, tgt + 1, size=1000, dtype=dt), tgt) + tgt = (lbnd + ubnd)//2 + assert_equal(self.rfunc(tgt, tgt + 1, size=1000, dtype=dt), tgt) + + def test_in_bounds_fuzz(self): + # Don't use fixed seed + np.random.seed() + for dt in self.itype[1:]: + for ubnd in [4, 8, 16]: + vals = self.rfunc(2, ubnd, size=2**16, dtype=dt) + assert_(vals.max() < ubnd) + assert_(vals.min() >= 2) + vals = self.rfunc(0, 2, size=2**16, dtype=np.bool) + assert_(vals.max() < 2) + assert_(vals.min() >= 0) + + 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 + # is [0, 2). Hashes are for little endian numbers. + tgt = {'bool': '7dd3170d7aa461d201a65f8bcf3944b0', + 'int16': '1b7741b80964bb190c50d541dca1cac1', + 'int32': '4dc9fcc2b395577ebb51793e58ed1a05', + 'int64': '17db902806f448331b5a758d7d2ee672', + 'int8': '27dd30c4e08a797063dffac2490b0be6', + 'uint16': '1b7741b80964bb190c50d541dca1cac1', + 'uint32': '4dc9fcc2b395577ebb51793e58ed1a05', + 'uint64': '17db902806f448331b5a758d7d2ee672', + 'uint8': '27dd30c4e08a797063dffac2490b0be6'} + + for dt in self.itype[1:]: + np.random.seed(1234) + + # view as little endian for hash + if sys.byteorder == 'little': + val = self.rfunc(0, 6, size=1000, dtype=dt) + else: + val = self.rfunc(0, 6, size=1000, dtype=dt).byteswap() + + res = hashlib.md5(val.view(np.int8)).hexdigest() + assert_(tgt[np.dtype(dt).name] == res) + + # bools do not depend on endianess + np.random.seed(1234) + val = self.rfunc(0, 2, size=1000, dtype=np.bool).view(np.int8) + res = hashlib.md5(val).hexdigest() + assert_(tgt[np.dtype(np.bool).name] == res) + + class TestRandomDist(TestCase): # Make sure the random distribution returns the correct value for a # given seed -- cgit v1.2.1 From b23ec897fcbf1a0279a599c1ba301a2b01c09c88 Mon Sep 17 00:00:00 2001 From: gfyoung Date: Sun, 3 Jan 2016 15:22:40 -0800 Subject: DEP: Deprecate random_integers --- numpy/random/tests/test_random.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'numpy/random/tests/test_random.py') diff --git a/numpy/random/tests/test_random.py b/numpy/random/tests/test_random.py index a6783fe8f..37c1876bf 100644 --- a/numpy/random/tests/test_random.py +++ b/numpy/random/tests/test_random.py @@ -7,6 +7,8 @@ from numpy.testing import ( from numpy import random from numpy.compat import asbytes import sys +import warnings + class TestSeed(TestCase): def test_scalar(self): @@ -255,6 +257,20 @@ class TestRandomDist(TestCase): desired = np.iinfo('l').max np.testing.assert_equal(actual, desired) + def test_random_integers_deprecated(self): + with warnings.catch_warnings(): + warnings.simplefilter("error", DeprecationWarning) + + # DeprecationWarning raised with high == None + assert_raises(DeprecationWarning, + np.random.random_integers, + np.iinfo('l').max) + + # DeprecationWarning raised with high != None + assert_raises(DeprecationWarning, + np.random.random_integers, + np.iinfo('l').max, np.iinfo('l').max) + def test_random_sample(self): np.random.seed(self.seed) actual = np.random.random_sample((3, 2)) -- cgit v1.2.1 From 5be93a2580a232705e897984d0f920bc6346990e Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Sat, 5 Dec 2015 20:46:09 -0800 Subject: MAINT: memcpy-based ~4x faster, typed shuffle. Only for 1d-ndarrays exactly, as subtypes (e.g. masked arrays) may not allow direct shuffle of the underlying buffer (in fact, the old implementation destroyed the underlying values of masked arrays while shuffling). Also handles struct-containing-object 1d ndarrays properly. See #6776 for an earlier, less general (but even faster: ~6x) improvement attempt, #5514 for the original issue. --- numpy/random/tests/test_random.py | 51 +++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 23 deletions(-) (limited to 'numpy/random/tests/test_random.py') diff --git a/numpy/random/tests/test_random.py b/numpy/random/tests/test_random.py index 37c1876bf..e3391a9a2 100644 --- a/numpy/random/tests/test_random.py +++ b/numpy/random/tests/test_random.py @@ -10,6 +10,7 @@ import sys import warnings + class TestSeed(TestCase): def test_scalar(self): s = np.random.RandomState(0) @@ -40,6 +41,7 @@ class TestSeed(TestCase): assert_raises(ValueError, np.random.RandomState, [1, 2, 4294967296]) assert_raises(ValueError, np.random.RandomState, [1, -2, 4294967296]) + class TestBinomial(TestCase): def test_n_zero(self): # Tests the corner case of n == 0 for the binomial distribution. @@ -130,6 +132,7 @@ class TestSetState(TestCase): # arguments without truncation. self.prng.negative_binomial(0.5, 0.5) + class TestRandint(TestCase): rfunc = np.random.randint @@ -207,12 +210,13 @@ class TestRandint(TestCase): assert_(tgt[np.dtype(np.bool).name] == res) -class TestRandomDist(TestCase): +class TestRandomDist: # Make sure the random distribution returns the correct value for a # given seed - def setUp(self): - self.seed = 1234567890 + @classmethod + def setup_class(cls): + cls.seed = 1234567890 def test_rand(self): np.random.seed(self.seed) @@ -368,40 +372,41 @@ class TestRandomDist(TestCase): np.testing.assert_equal(actual, desired) def test_shuffle(self): - # Test lists, arrays, and multidimensional versions of both: - for conv in [lambda x: x, - np.asarray, + # Test lists, arrays (of various dtypes), and multidimensional versions + # of both, c-contiguous or not: + for conv in [lambda x: np.array([]), + lambda x: x, + lambda x: np.asarray(x).astype(np.int8), + lambda x: np.asarray(x).astype(np.float32), + lambda x: np.asarray(x).astype(np.complex64), + lambda x: np.asarray(x).astype(object), lambda x: [(i, i) for i in x], - lambda x: np.asarray([(i, i) for i in x])]: + lambda x: np.asarray([[i, i] for i in x]), + lambda x: np.vstack([x, x]).T, + # gh-4270 + lambda x: np.asarray([(i, i) for i in x], + [("a", object, 1), + ("b", np.int32, 1)])]: np.random.seed(self.seed) alist = conv([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]) np.random.shuffle(alist) actual = alist desired = conv([0, 1, 9, 6, 2, 4, 5, 8, 7, 3]) - np.testing.assert_array_equal(actual, desired) - - def test_shuffle_flexible(self): - # gh-4270 - arr = [(0, 1), (2, 3)] - dt = np.dtype([('a', np.int32, 1), ('b', np.int32, 1)]) - nparr = np.array(arr, dtype=dt) - a, b = nparr[0].copy(), nparr[1].copy() - for i in range(50): - np.random.shuffle(nparr) - assert_(a in nparr) - assert_(b in nparr) + yield np.testing.assert_array_equal, actual, desired def test_shuffle_masked(self): # gh-3263 a = np.ma.masked_values(np.reshape(range(20), (5,4)) % 3 - 1, -1) b = np.ma.masked_values(np.arange(20) % 3 - 1, -1) - ma = np.ma.count_masked(a) - mb = np.ma.count_masked(b) + a_orig = a.copy() + b_orig = b.copy() for i in range(50): np.random.shuffle(a) - self.assertEqual(ma, np.ma.count_masked(a)) + assert_equal( + sorted(a.data[~a.mask]), sorted(a_orig.data[~a_orig.mask])) np.random.shuffle(b) - self.assertEqual(mb, np.ma.count_masked(b)) + assert_equal( + sorted(b.data[~b.mask]), sorted(b_orig.data[~b_orig.mask])) def test_beta(self): np.random.seed(self.seed) -- cgit v1.2.1 From 309fdd4cd1400fc392acfa418226bd7f8b10073d Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Tue, 12 Jan 2016 17:43:22 -0800 Subject: Revert to non-generative test. --- numpy/random/tests/test_random.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'numpy/random/tests/test_random.py') diff --git a/numpy/random/tests/test_random.py b/numpy/random/tests/test_random.py index e3391a9a2..96aa3790f 100644 --- a/numpy/random/tests/test_random.py +++ b/numpy/random/tests/test_random.py @@ -210,13 +210,12 @@ class TestRandint(TestCase): assert_(tgt[np.dtype(np.bool).name] == res) -class TestRandomDist: +class TestRandomDist(TestCase): # Make sure the random distribution returns the correct value for a # given seed - @classmethod - def setup_class(cls): - cls.seed = 1234567890 + def setUp(self): + self.seed = 1234567890 def test_rand(self): np.random.seed(self.seed) @@ -392,7 +391,7 @@ class TestRandomDist: np.random.shuffle(alist) actual = alist desired = conv([0, 1, 9, 6, 2, 4, 5, 8, 7, 3]) - yield np.testing.assert_array_equal, actual, desired + np.testing.assert_array_equal(actual, desired) def test_shuffle_masked(self): # gh-3263 -- cgit v1.2.1 From 091db7d35249935913c84bfff1bd78da3cb4f556 Mon Sep 17 00:00:00 2001 From: gfyoung Date: Thu, 21 Jan 2016 03:30:28 +0000 Subject: TST: Added broadcasting tests in test_random.py Added a whole new suite of tests to ensure that functions in mtrand.pyx which are broadcastable actually broadcast their arguments properly. --- numpy/random/tests/test_random.py | 716 +++++++++++++++++++++++++++++++++----- 1 file changed, 635 insertions(+), 81 deletions(-) (limited to 'numpy/random/tests/test_random.py') diff --git a/numpy/random/tests/test_random.py b/numpy/random/tests/test_random.py index 96aa3790f..7ec71e2e5 100644 --- a/numpy/random/tests/test_random.py +++ b/numpy/random/tests/test_random.py @@ -3,14 +3,13 @@ from __future__ import division, absolute_import, print_function import numpy as np from numpy.testing import ( TestCase, run_module_suite, assert_, assert_raises, assert_equal, - assert_warns) + assert_warns, assert_array_equal, assert_array_almost_equal) from numpy import random from numpy.compat import asbytes import sys import warnings - class TestSeed(TestCase): def test_scalar(self): s = np.random.RandomState(0) @@ -50,7 +49,7 @@ class TestBinomial(TestCase): zeros = np.zeros(2, dtype='int') for p in [0, .5, 1]: assert_(random.binomial(0, p) == 0) - np.testing.assert_array_equal(random.binomial(zeros, p), zeros) + assert_array_equal(random.binomial(zeros, p), zeros) def test_p_is_nan(self): # Issue #4571. @@ -148,10 +147,10 @@ class TestRandint(TestCase): for dt in self.itype: 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 - assert_raises(ValueError, self.rfunc, lbnd - 1 , ubnd, dtype=dt) - assert_raises(ValueError, self.rfunc, lbnd , ubnd + 1, dtype=dt) - assert_raises(ValueError, self.rfunc, ubnd , lbnd, dtype=dt) - assert_raises(ValueError, self.rfunc, 1 , 0, dtype=dt) + assert_raises(ValueError, self.rfunc, lbnd - 1, ubnd, dtype=dt) + assert_raises(ValueError, self.rfunc, lbnd, ubnd + 1, dtype=dt) + assert_raises(ValueError, self.rfunc, ubnd, lbnd, dtype=dt) + assert_raises(ValueError, self.rfunc, 1, 0, dtype=dt) def test_rng_zero_and_extremes(self): for dt in self.itype: @@ -223,7 +222,7 @@ class TestRandomDist(TestCase): desired = np.array([[0.61879477158567997, 0.59162362775974664], [0.88868358904449662, 0.89165480011560816], [0.4575674820298663, 0.7781880808593471]]) - np.testing.assert_array_almost_equal(actual, desired, decimal=15) + assert_array_almost_equal(actual, desired, decimal=15) def test_randn(self): np.random.seed(self.seed) @@ -231,7 +230,7 @@ class TestRandomDist(TestCase): desired = np.array([[1.34016345771863121, 1.73759122771936081], [1.498988344300628, -0.2286433324536169], [2.031033998682787, 2.17032494605655257]]) - np.testing.assert_array_almost_equal(actual, desired, decimal=15) + assert_array_almost_equal(actual, desired, decimal=15) def test_randint(self): np.random.seed(self.seed) @@ -239,7 +238,7 @@ class TestRandomDist(TestCase): desired = np.array([[31, 3], [-52, 41], [-48, -66]]) - np.testing.assert_array_equal(actual, desired) + assert_array_equal(actual, desired) def test_random_integers(self): np.random.seed(self.seed) @@ -247,7 +246,7 @@ class TestRandomDist(TestCase): desired = np.array([[31, 3], [-52, 41], [-48, -66]]) - np.testing.assert_array_equal(actual, desired) + assert_array_equal(actual, desired) def test_random_integers_max_int(self): # Tests whether random_integers can generate the @@ -258,7 +257,7 @@ class TestRandomDist(TestCase): actual = np.random.random_integers(np.iinfo('l').max, np.iinfo('l').max) desired = np.iinfo('l').max - np.testing.assert_equal(actual, desired) + assert_equal(actual, desired) def test_random_integers_deprecated(self): with warnings.catch_warnings(): @@ -280,38 +279,38 @@ class TestRandomDist(TestCase): desired = np.array([[0.61879477158567997, 0.59162362775974664], [0.88868358904449662, 0.89165480011560816], [0.4575674820298663, 0.7781880808593471]]) - np.testing.assert_array_almost_equal(actual, desired, decimal=15) + assert_array_almost_equal(actual, desired, decimal=15) def test_choice_uniform_replace(self): np.random.seed(self.seed) actual = np.random.choice(4, 4) desired = np.array([2, 3, 2, 3]) - np.testing.assert_array_equal(actual, desired) + assert_array_equal(actual, desired) def test_choice_nonuniform_replace(self): np.random.seed(self.seed) actual = np.random.choice(4, 4, p=[0.4, 0.4, 0.1, 0.1]) desired = np.array([1, 1, 2, 2]) - np.testing.assert_array_equal(actual, desired) + assert_array_equal(actual, desired) def test_choice_uniform_noreplace(self): np.random.seed(self.seed) actual = np.random.choice(4, 3, replace=False) desired = np.array([0, 1, 3]) - np.testing.assert_array_equal(actual, desired) + assert_array_equal(actual, desired) def test_choice_nonuniform_noreplace(self): np.random.seed(self.seed) actual = np.random.choice(4, 3, replace=False, p=[0.1, 0.3, 0.5, 0.1]) desired = np.array([2, 3, 1]) - np.testing.assert_array_equal(actual, desired) + assert_array_equal(actual, desired) def test_choice_noninteger(self): np.random.seed(self.seed) actual = np.random.choice(['a', 'b', 'c', 'd'], 4) desired = np.array(['c', 'd', 'c', 'd']) - np.testing.assert_array_equal(actual, desired) + assert_array_equal(actual, desired) def test_choice_exceptions(self): sample = np.random.choice @@ -320,13 +319,13 @@ class TestRandomDist(TestCase): assert_raises(ValueError, sample, [[1, 2], [3, 4]], 3) assert_raises(ValueError, sample, [], 3) assert_raises(ValueError, sample, [1, 2, 3, 4], 3, - p=[[0.25, 0.25], [0.25, 0.25]]) + p=[[0.25, 0.25], [0.25, 0.25]]) assert_raises(ValueError, sample, [1, 2], 3, p=[0.4, 0.4, 0.2]) assert_raises(ValueError, sample, [1, 2], 3, p=[1.1, -0.1]) assert_raises(ValueError, sample, [1, 2], 3, p=[0.4, 0.4]) assert_raises(ValueError, sample, [1, 2, 3], 4, replace=False) - assert_raises(ValueError, sample, [1, 2, 3], 2, replace=False, - p=[1, 0, 0]) + assert_raises(ValueError, sample, [1, 2, 3], 2, + replace=False, p=[1, 0, 0]) def test_choice_return_shape(self): p = [0.1, 0.9] @@ -368,7 +367,7 @@ class TestRandomDist(TestCase): np.random.seed(self.seed) actual = np.random.bytes(10) desired = asbytes('\x82Ui\x9e\xff\x97+Wf\xa5') - np.testing.assert_equal(actual, desired) + assert_equal(actual, desired) def test_shuffle(self): # Test lists, arrays (of various dtypes), and multidimensional versions @@ -391,11 +390,11 @@ class TestRandomDist(TestCase): np.random.shuffle(alist) actual = alist desired = conv([0, 1, 9, 6, 2, 4, 5, 8, 7, 3]) - np.testing.assert_array_equal(actual, desired) + assert_array_equal(actual, desired) def test_shuffle_masked(self): # gh-3263 - a = np.ma.masked_values(np.reshape(range(20), (5,4)) % 3 - 1, -1) + a = np.ma.masked_values(np.reshape(range(20), (5, 4)) % 3 - 1, -1) b = np.ma.masked_values(np.arange(20) % 3 - 1, -1) a_orig = a.copy() b_orig = b.copy() @@ -414,15 +413,15 @@ class TestRandomDist(TestCase): [[1.45341850513746058e-02, 5.31297615662868145e-04], [1.85366619058432324e-06, 4.19214516800110563e-03], [1.58405155108498093e-04, 1.26252891949397652e-04]]) - np.testing.assert_array_almost_equal(actual, desired, decimal=15) + assert_array_almost_equal(actual, desired, decimal=15) def test_binomial(self): np.random.seed(self.seed) actual = np.random.binomial(100.123, .456, size=(3, 2)) desired = np.array([[37, 43], - [42, 48], - [46, 45]]) - np.testing.assert_array_equal(actual, desired) + [42, 48], + [46, 45]]) + assert_array_equal(actual, desired) def test_chisquare(self): np.random.seed(self.seed) @@ -430,7 +429,7 @@ class TestRandomDist(TestCase): desired = np.array([[63.87858175501090585, 68.68407748911370447], [65.77116116901505904, 47.09686762438974483], [72.3828403199695174, 74.18408615260374006]]) - np.testing.assert_array_almost_equal(actual, desired, decimal=13) + assert_array_almost_equal(actual, desired, decimal=13) def test_dirichlet(self): np.random.seed(self.seed) @@ -442,7 +441,7 @@ class TestRandomDist(TestCase): [0.58964023305154301, 0.41035976694845688]], [[0.59266909280647828, 0.40733090719352177], [0.56974431743975207, 0.43025568256024799]]]) - np.testing.assert_array_almost_equal(actual, desired, decimal=15) + assert_array_almost_equal(actual, desired, decimal=15) def test_dirichlet_size(self): # gh-3173 @@ -462,7 +461,7 @@ class TestRandomDist(TestCase): desired = np.array([[1.08342649775011624, 1.00607889924557314], [2.46628830085216721, 2.49668106809923884], [0.68717433461363442, 1.69175666993575979]]) - np.testing.assert_array_almost_equal(actual, desired, decimal=15) + assert_array_almost_equal(actual, desired, decimal=15) def test_f(self): np.random.seed(self.seed) @@ -470,7 +469,7 @@ class TestRandomDist(TestCase): desired = np.array([[1.21975394418575878, 1.75135759791559775], [1.44803115017146489, 1.22108959480396262], [1.02176975757740629, 1.34431827623300415]]) - np.testing.assert_array_almost_equal(actual, desired, decimal=15) + assert_array_almost_equal(actual, desired, decimal=15) def test_gamma(self): np.random.seed(self.seed) @@ -478,7 +477,7 @@ class TestRandomDist(TestCase): desired = np.array([[24.60509188649287182, 28.54993563207210627], [26.13476110204064184, 12.56988482927716078], [31.71863275789960568, 33.30143302795922011]]) - np.testing.assert_array_almost_equal(actual, desired, decimal=14) + assert_array_almost_equal(actual, desired, decimal=14) def test_geometric(self): np.random.seed(self.seed) @@ -486,7 +485,7 @@ class TestRandomDist(TestCase): desired = np.array([[8, 7], [17, 17], [5, 12]]) - np.testing.assert_array_equal(actual, desired) + assert_array_equal(actual, desired) def test_gumbel(self): np.random.seed(self.seed) @@ -494,7 +493,7 @@ class TestRandomDist(TestCase): desired = np.array([[0.19591898743416816, 0.34405539668096674], [-1.4492522252274278, -1.47374816298446865], [1.10651090478803416, -0.69535848626236174]]) - np.testing.assert_array_almost_equal(actual, desired, decimal=15) + assert_array_almost_equal(actual, desired, decimal=15) def test_hypergeometric(self): np.random.seed(self.seed) @@ -502,25 +501,25 @@ class TestRandomDist(TestCase): desired = np.array([[10, 10], [10, 10], [9, 9]]) - np.testing.assert_array_equal(actual, desired) + assert_array_equal(actual, desired) # Test nbad = 0 actual = np.random.hypergeometric(5, 0, 3, size=4) desired = np.array([3, 3, 3, 3]) - np.testing.assert_array_equal(actual, desired) + assert_array_equal(actual, desired) actual = np.random.hypergeometric(15, 0, 12, size=4) desired = np.array([12, 12, 12, 12]) - np.testing.assert_array_equal(actual, desired) + assert_array_equal(actual, desired) # Test ngood = 0 actual = np.random.hypergeometric(0, 5, 3, size=4) desired = np.array([0, 0, 0, 0]) - np.testing.assert_array_equal(actual, desired) + assert_array_equal(actual, desired) actual = np.random.hypergeometric(0, 15, 12, size=4) desired = np.array([0, 0, 0, 0]) - np.testing.assert_array_equal(actual, desired) + assert_array_equal(actual, desired) def test_laplace(self): np.random.seed(self.seed) @@ -528,7 +527,7 @@ class TestRandomDist(TestCase): desired = np.array([[0.66599721112760157, 0.52829452552221945], [3.12791959514407125, 3.18202813572992005], [-0.05391065675859356, 1.74901336242837324]]) - np.testing.assert_array_almost_equal(actual, desired, decimal=15) + assert_array_almost_equal(actual, desired, decimal=15) def test_logistic(self): np.random.seed(self.seed) @@ -536,7 +535,7 @@ class TestRandomDist(TestCase): desired = np.array([[1.09232835305011444, 0.8648196662399954], [4.27818590694950185, 4.33897006346929714], [-0.21682183359214885, 2.63373365386060332]]) - np.testing.assert_array_almost_equal(actual, desired, decimal=15) + assert_array_almost_equal(actual, desired, decimal=15) def test_lognormal(self): np.random.seed(self.seed) @@ -544,7 +543,7 @@ class TestRandomDist(TestCase): desired = np.array([[16.50698631688883822, 36.54846706092654784], [22.67886599981281748, 0.71617561058995771], [65.72798501792723869, 86.84341601437161273]]) - np.testing.assert_array_almost_equal(actual, desired, decimal=13) + assert_array_almost_equal(actual, desired, decimal=13) def test_logseries(self): np.random.seed(self.seed) @@ -552,7 +551,7 @@ class TestRandomDist(TestCase): desired = np.array([[2, 2], [6, 17], [3, 6]]) - np.testing.assert_array_equal(actual, desired) + assert_array_equal(actual, desired) def test_multinomial(self): np.random.seed(self.seed) @@ -563,7 +562,7 @@ class TestRandomDist(TestCase): [2, 1, 4, 3, 6, 4]], [[4, 4, 2, 5, 2, 3], [4, 3, 4, 2, 3, 4]]]) - np.testing.assert_array_equal(actual, desired) + assert_array_equal(actual, desired) def test_multivariate_normal(self): np.random.seed(self.seed) @@ -578,12 +577,12 @@ class TestRandomDist(TestCase): [-1.77505606019580053, 10.]], [[-0.54970369430044119, 10.], [0.29768848031692957, 10.]]]) - np.testing.assert_array_almost_equal(actual, desired, decimal=15) + assert_array_almost_equal(actual, desired, decimal=15) # Check for default size, was raising deprecation warning actual = np.random.multivariate_normal(mean, cov) desired = np.array([-0.79441224511977482, 10.]) - np.testing.assert_array_almost_equal(actual, desired, decimal=15) + assert_array_almost_equal(actual, desired, decimal=15) # Check that non positive-semidefinite covariance raises warning mean = [0, 0] @@ -596,7 +595,7 @@ class TestRandomDist(TestCase): desired = np.array([[848, 841], [892, 611], [779, 647]]) - np.testing.assert_array_equal(actual, desired) + assert_array_equal(actual, desired) def test_noncentral_chisquare(self): np.random.seed(self.seed) @@ -604,20 +603,20 @@ class TestRandomDist(TestCase): desired = np.array([[23.91905354498517511, 13.35324692733826346], [31.22452661329736401, 16.60047399466177254], [5.03461598262724586, 17.94973089023519464]]) - np.testing.assert_array_almost_equal(actual, desired, decimal=14) + assert_array_almost_equal(actual, desired, decimal=14) actual = np.random.noncentral_chisquare(df=.5, nonc=.2, size=(3, 2)) - desired = np.array([[ 1.47145377828516666, 0.15052899268012659], - [ 0.00943803056963588, 1.02647251615666169], - [ 0.332334982684171 , 0.15451287602753125]]) - np.testing.assert_array_almost_equal(actual, desired, decimal=14) + desired = np.array([[1.47145377828516666, 0.15052899268012659], + [0.00943803056963588, 1.02647251615666169], + [0.332334982684171, 0.15451287602753125]]) + assert_array_almost_equal(actual, desired, decimal=14) np.random.seed(self.seed) actual = np.random.noncentral_chisquare(df=5, nonc=0, size=(3, 2)) desired = np.array([[9.597154162763948, 11.725484450296079], [10.413711048138335, 3.694475922923986], [13.484222138963087, 14.377255424602957]]) - np.testing.assert_array_almost_equal(actual, desired, decimal=14) + assert_array_almost_equal(actual, desired, decimal=14) def test_noncentral_f(self): np.random.seed(self.seed) @@ -626,7 +625,7 @@ class TestRandomDist(TestCase): desired = np.array([[1.40598099674926669, 0.34207973179285761], [3.57715069265772545, 7.92632662577829805], [0.43741599463544162, 1.1774208752428319]]) - np.testing.assert_array_almost_equal(actual, desired, decimal=14) + assert_array_almost_equal(actual, desired, decimal=14) def test_normal(self): np.random.seed(self.seed) @@ -634,7 +633,7 @@ class TestRandomDist(TestCase): desired = np.array([[2.80378370443726244, 3.59863924443872163], [3.121433477601256, -0.33382987590723379], [4.18552478636557357, 4.46410668111310471]]) - np.testing.assert_array_almost_equal(actual, desired, decimal=15) + assert_array_almost_equal(actual, desired, decimal=15) def test_pareto(self): np.random.seed(self.seed) @@ -655,9 +654,9 @@ class TestRandomDist(TestCase): np.random.seed(self.seed) actual = np.random.poisson(lam=.123456789, size=(3, 2)) desired = np.array([[0, 0], - [1, 0], - [0, 0]]) - np.testing.assert_array_equal(actual, desired) + [1, 0], + [0, 0]]) + assert_array_equal(actual, desired) def test_poisson_exceptions(self): lambig = np.iinfo('l').max @@ -673,7 +672,7 @@ class TestRandomDist(TestCase): desired = np.array([[0.02048932883240791, 0.01424192241128213], [0.38446073748535298, 0.39499689943484395], [0.00177699707563439, 0.13115505880863756]]) - np.testing.assert_array_almost_equal(actual, desired, decimal=15) + assert_array_almost_equal(actual, desired, decimal=15) def test_rayleigh(self): np.random.seed(self.seed) @@ -681,7 +680,7 @@ class TestRandomDist(TestCase): desired = np.array([[13.8882496494248393, 13.383318339044731], [20.95413364294492098, 21.08285015800712614], [11.06066537006854311, 17.35468505778271009]]) - np.testing.assert_array_almost_equal(actual, desired, decimal=14) + assert_array_almost_equal(actual, desired, decimal=14) def test_standard_cauchy(self): np.random.seed(self.seed) @@ -689,7 +688,7 @@ class TestRandomDist(TestCase): desired = np.array([[0.77127660196445336, -6.55601161955910605], [0.93582023391158309, -2.07479293013759447], [-4.74601644297011926, 0.18338989290760804]]) - np.testing.assert_array_almost_equal(actual, desired, decimal=15) + assert_array_almost_equal(actual, desired, decimal=15) def test_standard_exponential(self): np.random.seed(self.seed) @@ -697,7 +696,7 @@ class TestRandomDist(TestCase): desired = np.array([[0.96441739162374596, 0.89556604882105506], [2.1953785836319808, 2.22243285392490542], [0.6116915921431676, 1.50592546727413201]]) - np.testing.assert_array_almost_equal(actual, desired, decimal=15) + assert_array_almost_equal(actual, desired, decimal=15) def test_standard_gamma(self): np.random.seed(self.seed) @@ -705,7 +704,7 @@ class TestRandomDist(TestCase): desired = np.array([[5.50841531318455058, 6.62953470301903103], [5.93988484943779227, 2.31044849402133989], [7.54838614231317084, 8.012756093271868]]) - np.testing.assert_array_almost_equal(actual, desired, decimal=14) + assert_array_almost_equal(actual, desired, decimal=14) def test_standard_normal(self): np.random.seed(self.seed) @@ -713,7 +712,7 @@ class TestRandomDist(TestCase): desired = np.array([[1.34016345771863121, 1.73759122771936081], [1.498988344300628, -0.2286433324536169], [2.031033998682787, 2.17032494605655257]]) - np.testing.assert_array_almost_equal(actual, desired, decimal=15) + assert_array_almost_equal(actual, desired, decimal=15) def test_standard_t(self): np.random.seed(self.seed) @@ -721,7 +720,7 @@ class TestRandomDist(TestCase): desired = np.array([[0.97140611862659965, -0.08830486548450577], [1.36311143689505321, -0.55317463909867071], [-0.18473749069684214, 0.61181537341755321]]) - np.testing.assert_array_almost_equal(actual, desired, decimal=15) + assert_array_almost_equal(actual, desired, decimal=15) def test_triangular(self): np.random.seed(self.seed) @@ -730,7 +729,7 @@ class TestRandomDist(TestCase): desired = np.array([[12.68117178949215784, 12.4129206149193152], [16.20131377335158263, 16.25692138747600524], [11.20400690911820263, 14.4978144835829923]]) - np.testing.assert_array_almost_equal(actual, desired, decimal=14) + assert_array_almost_equal(actual, desired, decimal=14) def test_uniform(self): np.random.seed(self.seed) @@ -738,16 +737,16 @@ class TestRandomDist(TestCase): desired = np.array([[6.99097932346268003, 6.73801597444323974], [9.50364421400426274, 9.53130618907631089], [5.48995325769805476, 8.47493103280052118]]) - np.testing.assert_array_almost_equal(actual, desired, decimal=15) + assert_array_almost_equal(actual, desired, decimal=15) def test_uniform_range_bounds(self): fmin = np.finfo('float').min fmax = np.finfo('float').max func = np.random.uniform - np.testing.assert_raises(OverflowError, func, -np.inf, 0) - np.testing.assert_raises(OverflowError, func, 0, np.inf) - np.testing.assert_raises(OverflowError, func, fmin, fmax) + assert_raises(OverflowError, func, -np.inf, 0) + assert_raises(OverflowError, func, 0, np.inf) + assert_raises(OverflowError, func, fmin, fmax) # (fmax / 1e17) - fmin is within range, so this should not throw np.random.uniform(low=fmin, high=fmax / 1e17) @@ -758,7 +757,7 @@ class TestRandomDist(TestCase): desired = np.array([[2.28567572673902042, 2.89163838442285037], [0.38198375564286025, 2.57638023113890746], [1.19153771588353052, 1.83509849681825354]]) - np.testing.assert_array_almost_equal(actual, desired, decimal=15) + assert_array_almost_equal(actual, desired, decimal=15) def test_vonmises_small(self): # check infinite loop, gh-4720 @@ -772,7 +771,7 @@ class TestRandomDist(TestCase): desired = np.array([[3.82935265715889983, 5.13125249184285526], [0.35045403618358717, 1.50832396872003538], [0.24124319895843183, 0.22031101461955038]]) - np.testing.assert_array_almost_equal(actual, desired, decimal=14) + assert_array_almost_equal(actual, desired, decimal=14) def test_weibull(self): np.random.seed(self.seed) @@ -780,7 +779,7 @@ class TestRandomDist(TestCase): desired = np.array([[0.97097342648766727, 0.91422896443565516], [1.89517770034962929, 1.91414357960479564], [0.67057783752390987, 1.39494046635066793]]) - np.testing.assert_array_almost_equal(actual, desired, decimal=15) + assert_array_almost_equal(actual, desired, decimal=15) def test_zipf(self): np.random.seed(self.seed) @@ -788,10 +787,566 @@ class TestRandomDist(TestCase): desired = np.array([[66, 29], [1, 1], [3, 13]]) - np.testing.assert_array_equal(actual, desired) + assert_array_equal(actual, desired) + + +class TestBroadcast(TestCase): + # tests that functions that broadcast behave + # correctly when presented with non-scalar arguments + def setUp(self): + self.seed = 123456789 + + def setSeed(self): + np.random.seed(self.seed) + + # TODO: Include test for randint once it can broadcast + # Can steal the test written in PR #6938 + + def test_uniform(self): + low = [0] + high = [1] + uniform = np.random.uniform + desired = np.array([0.53283302478975902, + 0.53413660089041659, + 0.50955303552646702]) + + self.setSeed() + actual = uniform(low * 3, high) + assert_array_almost_equal(actual, desired, decimal=14) + + self.setSeed() + actual = uniform(low, high * 3) + assert_array_almost_equal(actual, desired, decimal=14) + + def test_normal(self): + loc = [0] + scale = [1] + bad_scale = [-1] + normal = np.random.normal + desired = np.array([2.2129019979039612, + 2.1283977976520019, + 1.8417114045748335]) + + self.setSeed() + actual = normal(loc * 3, scale) + assert_array_almost_equal(actual, desired, decimal=14) + assert_raises(ValueError, normal, loc * 3, bad_scale) + + self.setSeed() + actual = normal(loc, scale * 3) + assert_array_almost_equal(actual, desired, decimal=14) + assert_raises(ValueError, normal, loc, bad_scale * 3) + + def test_beta(self): + a = [1] + b = [2] + bad_a = [-1] + bad_b = [-2] + beta = np.random.beta + desired = np.array([0.19843558305989056, + 0.075230336409423643, + 0.24976865978980844]) + + self.setSeed() + actual = beta(a * 3, b) + assert_array_almost_equal(actual, desired, decimal=14) + assert_raises(ValueError, beta, bad_a * 3, b) + assert_raises(ValueError, beta, a * 3, bad_b) + + self.setSeed() + actual = beta(a, b * 3) + assert_array_almost_equal(actual, desired, decimal=14) + assert_raises(ValueError, beta, bad_a, b * 3) + assert_raises(ValueError, beta, a, bad_b * 3) + + def test_exponential(self): + scale = [1] + bad_scale = [-1] + exponential = np.random.exponential + desired = np.array([0.76106853658845242, + 0.76386282278691653, + 0.71243813125891797]) + + self.setSeed() + actual = exponential(scale * 3) + assert_array_almost_equal(actual, desired, decimal=14) + assert_raises(ValueError, exponential, bad_scale * 3) + + def test_standard_gamma(self): + shape = [1] + bad_shape = [-1] + std_gamma = np.random.standard_gamma + desired = np.array([0.76106853658845242, + 0.76386282278691653, + 0.71243813125891797]) + + self.setSeed() + actual = std_gamma(shape * 3) + assert_array_almost_equal(actual, desired, decimal=14) + assert_raises(ValueError, std_gamma, bad_shape * 3) + + def test_gamma(self): + shape = [1] + scale = [2] + bad_shape = [-1] + bad_scale = [-2] + gamma = np.random.gamma + desired = np.array([1.5221370731769048, + 1.5277256455738331, + 1.4248762625178359]) + + self.setSeed() + actual = gamma(shape * 3, scale) + assert_array_almost_equal(actual, desired, decimal=14) + assert_raises(ValueError, gamma, bad_shape * 3, scale) + assert_raises(ValueError, gamma, shape * 3, bad_scale) + + self.setSeed() + actual = gamma(shape, scale * 3) + assert_array_almost_equal(actual, desired, decimal=14) + assert_raises(ValueError, gamma, bad_shape, scale * 3) + assert_raises(ValueError, gamma, shape, bad_scale * 3) + + def test_f(self): + dfnum = [1] + dfden = [2] + bad_dfnum = [-1] + bad_dfden = [-2] + f = np.random.f + desired = np.array([0.80038951638264799, + 0.86768719635363512, + 2.7251095168386801]) + + self.setSeed() + actual = f(dfnum * 3, dfden) + assert_array_almost_equal(actual, desired, decimal=14) + assert_raises(ValueError, f, bad_dfnum * 3, dfden) + assert_raises(ValueError, f, dfnum * 3, bad_dfden) + + self.setSeed() + actual = f(dfnum, dfden * 3) + assert_array_almost_equal(actual, desired, decimal=14) + assert_raises(ValueError, f, bad_dfnum, dfden * 3) + assert_raises(ValueError, f, dfnum, bad_dfden * 3) + + def test_noncentral_f(self): + dfnum = [2] + dfden = [3] + nonc = [4] + bad_dfnum = [0] + bad_dfden = [-1] + bad_nonc = [-2] + nonc_f = np.random.noncentral_f + desired = np.array([9.1393943263705211, + 13.025456344595602, + 8.8018098359100545]) + + self.setSeed() + actual = nonc_f(dfnum * 3, dfden, nonc) + assert_array_almost_equal(actual, desired, decimal=14) + 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) + + self.setSeed() + actual = nonc_f(dfnum, dfden * 3, nonc) + assert_array_almost_equal(actual, desired, decimal=14) + assert_raises(ValueError, nonc_f, bad_dfnum, dfden * 3, nonc) + assert_raises(ValueError, nonc_f, dfnum, bad_dfden * 3, nonc) + assert_raises(ValueError, nonc_f, dfnum, dfden * 3, bad_nonc) + + self.setSeed() + actual = nonc_f(dfnum, dfden, nonc * 3) + assert_array_almost_equal(actual, desired, decimal=14) + 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_chisquare(self): + df = [1] + bad_df = [-1] + chisquare = np.random.chisquare + desired = np.array([0.57022801133088286, + 0.51947702108840776, + 0.1320969254923558]) + + self.setSeed() + actual = chisquare(df * 3) + assert_array_almost_equal(actual, desired, decimal=14) + assert_raises(ValueError, chisquare, bad_df * 3) + + def test_noncentral_chisquare(self): + df = [1] + nonc = [2] + bad_df = [-1] + bad_nonc = [-2] + nonc_chi = np.random.noncentral_chisquare + desired = np.array([9.0015599467913763, + 4.5804135049718742, + 6.0872302432834564]) + + self.setSeed() + actual = nonc_chi(df * 3, nonc) + 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) + + self.setSeed() + 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) + + def test_standard_t(self): + df = [1] + bad_df = [-1] + t = np.random.standard_t + desired = np.array([3.0702872575217643, + 5.8560725167361607, + 1.0274791436474273]) + + self.setSeed() + actual = t(df * 3) + assert_array_almost_equal(actual, desired, decimal=14) + assert_raises(ValueError, t, bad_df * 3) + + def test_vonmises(self): + mu = [2] + kappa = [1] + bad_kappa = [-1] + vonmises = np.random.vonmises + desired = np.array([2.9883443664201312, + -2.7064099483995943, + -1.8672476700665914]) + + self.setSeed() + actual = vonmises(mu * 3, kappa) + assert_array_almost_equal(actual, desired, decimal=14) + assert_raises(ValueError, vonmises, mu * 3, bad_kappa) + + self.setSeed() + actual = vonmises(mu, kappa * 3) + assert_array_almost_equal(actual, desired, decimal=14) + assert_raises(ValueError, vonmises, mu, bad_kappa * 3) + + def test_pareto(self): + a = [1] + bad_a = [-1] + pareto = np.random.pareto + desired = np.array([1.1405622680198362, + 1.1465519762044529, + 1.0389564467453547]) + + self.setSeed() + actual = pareto(a * 3) + assert_array_almost_equal(actual, desired, decimal=14) + assert_raises(ValueError, pareto, bad_a * 3) + + def test_weibull(self): + a = [1] + bad_a = [-1] + weibull = np.random.weibull + desired = np.array([0.76106853658845242, + 0.76386282278691653, + 0.71243813125891797]) + + self.setSeed() + actual = weibull(a * 3) + assert_array_almost_equal(actual, desired, decimal=14) + assert_raises(ValueError, weibull, bad_a * 3) + + def test_power(self): + a = [1] + bad_a = [-1] + power = np.random.power + desired = np.array([0.53283302478975902, + 0.53413660089041659, + 0.50955303552646702]) + + self.setSeed() + actual = power(a * 3) + assert_array_almost_equal(actual, desired, decimal=14) + assert_raises(ValueError, power, bad_a * 3) + + def test_laplace(self): + loc = [0] + scale = [1] + bad_scale = [-1] + laplace = np.random.laplace + desired = np.array([0.067921356028507157, + 0.070715642226971326, + 0.019290950698972624]) + + self.setSeed() + actual = laplace(loc * 3, scale) + assert_array_almost_equal(actual, desired, decimal=14) + assert_raises(ValueError, laplace, loc * 3, bad_scale) + + self.setSeed() + actual = laplace(loc, scale * 3) + assert_array_almost_equal(actual, desired, decimal=14) + assert_raises(ValueError, laplace, loc, bad_scale * 3) + + def test_gumbel(self): + loc = [0] + scale = [1] + bad_scale = [-1] + gumbel = np.random.gumbel + desired = np.array([0.2730318639556768, + 0.26936705726291116, + 0.33906220393037939]) + + self.setSeed() + actual = gumbel(loc * 3, scale) + assert_array_almost_equal(actual, desired, decimal=14) + assert_raises(ValueError, gumbel, loc * 3, bad_scale) + + self.setSeed() + actual = gumbel(loc, scale * 3) + assert_array_almost_equal(actual, desired, decimal=14) + assert_raises(ValueError, gumbel, loc, bad_scale * 3) + + def test_logistic(self): + loc = [0] + scale = [1] + bad_scale = [-1] + logistic = np.random.logistic + desired = np.array([0.13152135837586171, + 0.13675915696285773, + 0.038216792802833396]) + + self.setSeed() + actual = logistic(loc * 3, scale) + assert_array_almost_equal(actual, desired, decimal=14) + assert_raises(ValueError, logistic, loc * 3, bad_scale) + + self.setSeed() + actual = logistic(loc, scale * 3) + assert_array_almost_equal(actual, desired, decimal=14) + assert_raises(ValueError, logistic, loc, bad_scale * 3) + + def test_lognormal(self): + mean = [0] + sigma = [1] + bad_sigma = [-1] + lognormal = np.random.lognormal + desired = np.array([9.1422086044848427, + 8.4013952870126261, + 6.3073234116578671]) + + self.setSeed() + actual = lognormal(mean * 3, sigma) + assert_array_almost_equal(actual, desired, decimal=14) + assert_raises(ValueError, lognormal, mean * 3, bad_sigma) + + self.setSeed() + actual = lognormal(mean, sigma * 3) + assert_array_almost_equal(actual, desired, decimal=14) + assert_raises(ValueError, lognormal, mean, bad_sigma * 3) + + def test_rayleigh(self): + scale = [1] + bad_scale = [-1] + rayleigh = np.random.rayleigh + desired = np.array([1.2337491937897689, + 1.2360119924878694, + 1.1936818095781789]) + + self.setSeed() + actual = rayleigh(scale * 3) + assert_array_almost_equal(actual, desired, decimal=14) + assert_raises(ValueError, rayleigh, bad_scale * 3) + + def test_wald(self): + mean = [0.5] + scale = [1] + bad_mean = [0] + bad_scale = [-2] + wald = np.random.wald + desired = np.array([0.11873681120271318, + 0.12450084820795027, + 0.9096122728408238]) + + self.setSeed() + actual = wald(mean * 3, scale) + assert_array_almost_equal(actual, desired, decimal=14) + assert_raises(ValueError, wald, bad_mean * 3, scale) + assert_raises(ValueError, wald, mean * 3, bad_scale) + + self.setSeed() + actual = wald(mean, scale * 3) + assert_array_almost_equal(actual, desired, decimal=14) + assert_raises(ValueError, wald, bad_mean, scale * 3) + assert_raises(ValueError, wald, mean, bad_scale * 3) + + def test_triangular(self): + left = [1] + right = [3] + mode = [2] + bad_left_one = [3] + bad_mode_one = [4] + bad_left_two, bad_mode_two = right * 2 + triangular = np.random.triangular + desired = np.array([2.03339048710429, + 2.0347400359389356, + 2.0095991069536208]) + + self.setSeed() + actual = triangular(left * 3, mode, right) + 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) + + self.setSeed() + 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) + + self.setSeed() + 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) + + def test_binomial(self): + n = [1] + p = [0.5] + bad_n = [-1] + bad_p_one = [-1] + bad_p_two = [1.5] + binom = np.random.binomial + desired = np.array([1, 1, 1]) + + self.setSeed() + actual = binom(n * 3, p) + assert_array_equal(actual, desired) + assert_raises(ValueError, binom, bad_n * 3, p) + assert_raises(ValueError, binom, n * 3, bad_p_one) + assert_raises(ValueError, binom, n * 3, bad_p_two) + + self.setSeed() + actual = binom(n, p * 3) + assert_array_equal(actual, desired) + assert_raises(ValueError, binom, bad_n, p * 3) + assert_raises(ValueError, binom, n, bad_p_one * 3) + assert_raises(ValueError, binom, n, bad_p_two * 3) + + def test_negative_binomial(self): + n = [1] + p = [0.5] + bad_n = [-1] + bad_p_one = [-1] + bad_p_two = [1.5] + neg_binom = np.random.negative_binomial + desired = np.array([1, 0, 1]) + + self.setSeed() + actual = neg_binom(n * 3, p) + assert_array_equal(actual, desired) + assert_raises(ValueError, neg_binom, bad_n * 3, p) + assert_raises(ValueError, neg_binom, n * 3, bad_p_one) + assert_raises(ValueError, neg_binom, n * 3, bad_p_two) + + self.setSeed() + actual = neg_binom(n, p * 3) + assert_array_equal(actual, desired) + assert_raises(ValueError, neg_binom, bad_n, p * 3) + assert_raises(ValueError, neg_binom, n, bad_p_one * 3) + assert_raises(ValueError, neg_binom, n, bad_p_two * 3) + + def test_poisson(self): + max_lam = np.random.RandomState().poisson_lam_max + + lam = [1] + bad_lam_one = [-1] + bad_lam_two = [max_lam * 2] + poisson = np.random.poisson + desired = np.array([1, 1, 0]) + + self.setSeed() + actual = poisson(lam * 3) + assert_array_equal(actual, desired) + assert_raises(ValueError, poisson, bad_lam_one * 3) + assert_raises(ValueError, poisson, bad_lam_two * 3) + + def test_zipf(self): + a = [2] + bad_a = [0] + zipf = np.random.zipf + desired = np.array([2, 2, 1]) + self.setSeed() + actual = zipf(a * 3) + assert_array_equal(actual, desired) + assert_raises(ValueError, zipf, bad_a * 3) -class TestThread(object): + def test_geometric(self): + p = [0.5] + bad_p_one = [-1] + bad_p_two = [1.5] + geom = np.random.geometric + desired = np.array([2, 2, 2]) + + self.setSeed() + actual = geom(p * 3) + assert_array_equal(actual, desired) + assert_raises(ValueError, geom, bad_p_one * 3) + assert_raises(ValueError, geom, bad_p_two * 3) + + def test_hypergeometric(self): + ngood = [1] + nbad = [2] + nsample = [2] + bad_ngood = [-1] + bad_nbad = [-2] + bad_nsample_one = [0] + bad_nsample_two = [4] + hypergeom = np.random.hypergeometric + desired = np.array([1, 1, 1]) + + self.setSeed() + actual = hypergeom(ngood * 3, nbad, nsample) + assert_array_equal(actual, desired) + assert_raises(ValueError, hypergeom, bad_ngood * 3, nbad, nsample) + assert_raises(ValueError, hypergeom, ngood * 3, bad_nbad, nsample) + assert_raises(ValueError, hypergeom, ngood * 3, nbad, bad_nsample_one) + assert_raises(ValueError, hypergeom, ngood * 3, nbad, bad_nsample_two) + + self.setSeed() + actual = hypergeom(ngood, nbad * 3, nsample) + assert_array_equal(actual, desired) + assert_raises(ValueError, hypergeom, bad_ngood, nbad * 3, nsample) + assert_raises(ValueError, hypergeom, ngood, bad_nbad * 3, nsample) + assert_raises(ValueError, hypergeom, ngood, nbad * 3, bad_nsample_one) + assert_raises(ValueError, hypergeom, ngood, nbad * 3, bad_nsample_two) + + self.setSeed() + actual = hypergeom(ngood, nbad, nsample * 3) + assert_array_equal(actual, desired) + assert_raises(ValueError, hypergeom, bad_ngood, nbad, nsample * 3) + assert_raises(ValueError, hypergeom, ngood, bad_nbad, nsample * 3) + assert_raises(ValueError, hypergeom, ngood, nbad, bad_nsample_one * 3) + assert_raises(ValueError, hypergeom, ngood, nbad, bad_nsample_two * 3) + + def test_logseries(self): + p = [0.5] + bad_p_one = [2] + bad_p_two = [-1] + logseries = np.random.logseries + desired = np.array([1, 1, 1]) + + self.setSeed() + actual = logseries(p * 3) + assert_array_equal(actual, desired) + assert_raises(ValueError, logseries, bad_p_one * 3) + assert_raises(ValueError, logseries, bad_p_two * 3) + + +class TestThread(TestCase): # make sure each state produces the same sequence even in threads def setUp(self): self.seeds = range(4) @@ -813,10 +1368,10 @@ class TestThread(object): function(np.random.RandomState(s), o) # these platforms change x87 fpu precision mode in threads - if (np.intp().dtype.itemsize == 4 and sys.platform == "win32"): - np.testing.assert_array_almost_equal(out1, out2) + if np.intp().dtype.itemsize == 4 and sys.platform == "win32": + assert_array_almost_equal(out1, out2) else: - np.testing.assert_array_equal(out1, out2) + assert_array_equal(out1, out2) def test_normal(self): def gen_random(state, out): @@ -831,8 +1386,7 @@ class TestThread(object): def test_multinomial(self): def gen_random(state, out): out[...] = state.multinomial(10, [1/6.]*6, size=10000) - self.check_function(gen_random, sz=(10000,6)) - + self.check_function(gen_random, sz=(10000, 6)) if __name__ == "__main__": run_module_suite() -- cgit v1.2.1 From 61f872265b67b313058a07533eaed88f4170ff2c Mon Sep 17 00:00:00 2001 From: gfyoung Date: Mon, 18 Jan 2016 20:29:34 +0000 Subject: BUG: One element array inputs get one element arrays returned in np.random Fixes bug in np.random methods that would return scalars when passed one-element array inputs. This is because one-element ndarrays can be cast to integers / floats, which is what functions like PyFloat_AsDouble do before converting to the intended data type. This commit changes the check used to determine whether the inputs are purely scalar by converting all inputs to arrays and checking if the resulting shape is an empty tuple (scalar) or not (array). Closes gh-4263. --- numpy/random/tests/test_random.py | 87 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 86 insertions(+), 1 deletion(-) (limited to 'numpy/random/tests/test_random.py') diff --git a/numpy/random/tests/test_random.py b/numpy/random/tests/test_random.py index 7ec71e2e5..199509361 100644 --- a/numpy/random/tests/test_random.py +++ b/numpy/random/tests/test_random.py @@ -1345,7 +1345,6 @@ class TestBroadcast(TestCase): assert_raises(ValueError, logseries, bad_p_one * 3) assert_raises(ValueError, logseries, bad_p_two * 3) - class TestThread(TestCase): # make sure each state produces the same sequence even in threads def setUp(self): @@ -1388,5 +1387,91 @@ class TestThread(TestCase): out[...] = state.multinomial(10, [1/6.]*6, size=10000) self.check_function(gen_random, sz=(10000, 6)) +# See Issue #4263 +class TestSingleEltArrayInput(TestCase): + def setUp(self): + self.argOne = np.array([2]) + self.argTwo = np.array([3]) + self.argThree = np.array([4]) + self.tgtShape = (1,) + + def test_one_arg_funcs(self): + funcs = (np.random.exponential, np.random.standard_gamma, + np.random.chisquare, np.random.standard_t, + np.random.pareto, np.random.weibull, + np.random.power, np.random.rayleigh, + np.random.poisson, np.random.zipf, + np.random.geometric, np.random.logseries) + + probfuncs = (np.random.geometric, np.random.logseries) + + for func in funcs: + if func in probfuncs: # p < 1.0 + out = func(np.array([0.5])) + + else: + out = func(self.argOne) + + self.assertEqual(out.shape, self.tgtShape) + + def test_two_arg_funcs(self): + funcs = (np.random.uniform, np.random.normal, + np.random.beta, np.random.gamma, + np.random.f, np.random.noncentral_chisquare, + np.random.vonmises, np.random.laplace, + np.random.gumbel, np.random.logistic, + np.random.lognormal, np.random.wald, + np.random.binomial, np.random.negative_binomial) + + probfuncs = (np.random.binomial, np.random.negative_binomial) + + for func in funcs: + if func in probfuncs: # p <= 1 + argTwo = np.array([0.5]) + + else: + argTwo = self.argTwo + + out = func(self.argOne, argTwo) + self.assertEqual(out.shape, self.tgtShape) + + out = func(self.argOne[0], argTwo) + self.assertEqual(out.shape, self.tgtShape) + + out = func(self.argOne, argTwo[0]) + self.assertEqual(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, +# np.int32, np.uint32, np.int64, np.uint64] +# func = np.random.randint +# high = np.array([1]) +# low = np.array([0]) +# +# for dt in itype: +# out = func(low, high, dtype=dt) +# self.assert_equal(out.shape, self.tgtShape) +# +# out = func(low[0], high, dtype=dt) +# self.assert_equal(out.shape, self.tgtShape) +# +# out = func(low, high[0], dtype=dt) +# self.assert_equal(out.shape, self.tgtShape) + + def test_three_arg_funcs(self): + funcs = [np.random.noncentral_f, np.random.triangular, + np.random.hypergeometric] + + for func in funcs: + out = func(self.argOne, self.argTwo, self.argThree) + self.assertEqual(out.shape, self.tgtShape) + + out = func(self.argOne[0], self.argTwo, self.argThree) + self.assertEqual(out.shape, self.tgtShape) + + out = func(self.argOne, self.argTwo[0], self.argThree) + self.assertEqual(out.shape, self.tgtShape) + if __name__ == "__main__": run_module_suite() -- cgit v1.2.1 From de44e0fb8cf67594227e9770dc8e6f94285cd486 Mon Sep 17 00:00:00 2001 From: gfyoung Date: Sun, 7 Feb 2016 00:21:20 +0000 Subject: BUG: Enforce dtype for randint singletons Closes gh-7203. --- numpy/random/tests/test_random.py | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'numpy/random/tests/test_random.py') diff --git a/numpy/random/tests/test_random.py b/numpy/random/tests/test_random.py index 199509361..fac287b3f 100644 --- a/numpy/random/tests/test_random.py +++ b/numpy/random/tests/test_random.py @@ -208,6 +208,15 @@ class TestRandint(TestCase): res = hashlib.md5(val).hexdigest() assert_(tgt[np.dtype(np.bool).name] == res) + def test_respect_dtype_singleton(self): + # See gh-7203 + for dt in self.itype: + 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 + + sample = self.rfunc(lbnd, ubnd, dtype=dt) + self.assertEqual(sample.dtype, np.dtype(dt)) + class TestRandomDist(TestCase): # Make sure the random distribution returns the correct value for a -- cgit v1.2.1 From 4f91544af2a74bc2dcecfce5db05b015f26721e4 Mon Sep 17 00:00:00 2001 From: gfyoung Date: Fri, 19 Feb 2016 05:14:49 +0000 Subject: BUG: Make randint backwards compatible with pandas The 'pandas' library expects Python integers to be returned, so this commit changes the API so that the default is 'np.int' which converts to native Python integer types when a singleton is being generated with this function. Closes gh-7284. --- numpy/random/tests/test_random.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'numpy/random/tests/test_random.py') diff --git a/numpy/random/tests/test_random.py b/numpy/random/tests/test_random.py index fac287b3f..a07fa52f5 100644 --- a/numpy/random/tests/test_random.py +++ b/numpy/random/tests/test_random.py @@ -137,7 +137,7 @@ class TestRandint(TestCase): rfunc = np.random.randint # valid integer/boolean types - itype = [np.bool, np.int8, np.uint8, np.int16, np.uint16, + itype = [np.bool_, np.int8, np.uint8, np.int16, np.uint16, np.int32, np.uint32, np.int64, np.uint64] def test_unsupported_type(self): @@ -145,8 +145,8 @@ class TestRandint(TestCase): def test_bounds_checking(self): for dt in self.itype: - 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 + 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 assert_raises(ValueError, self.rfunc, lbnd - 1, ubnd, dtype=dt) assert_raises(ValueError, self.rfunc, lbnd, ubnd + 1, dtype=dt) assert_raises(ValueError, self.rfunc, ubnd, lbnd, dtype=dt) @@ -154,8 +154,8 @@ class TestRandint(TestCase): def test_rng_zero_and_extremes(self): for dt in self.itype: - 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 + 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 tgt = ubnd - 1 assert_equal(self.rfunc(tgt, tgt + 1, size=1000, dtype=dt), tgt) tgt = lbnd @@ -211,11 +211,20 @@ class TestRandint(TestCase): def test_respect_dtype_singleton(self): # See gh-7203 for dt in self.itype: + 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 + + sample = self.rfunc(lbnd, ubnd, dtype=dt) + self.assertEqual(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 + # gh-7284: Ensure that we get Python data types sample = self.rfunc(lbnd, ubnd, dtype=dt) - self.assertEqual(sample.dtype, np.dtype(dt)) + self.assertFalse(hasattr(sample, 'dtype')) + self.assertEqual(type(sample), dt) class TestRandomDist(TestCase): -- cgit v1.2.1 From bae9aebaa5aeedc3e1ecf69bef65f5fe721a3d37 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Fri, 1 May 2015 22:30:48 -0700 Subject: Allow many distributions to have a scale of 0. (in which case a stream of 0's is usually returned (or 1's)). See #5818. --- numpy/random/tests/test_random.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'numpy/random/tests/test_random.py') diff --git a/numpy/random/tests/test_random.py b/numpy/random/tests/test_random.py index a07fa52f5..3f4952d20 100644 --- a/numpy/random/tests/test_random.py +++ b/numpy/random/tests/test_random.py @@ -481,6 +481,9 @@ class TestRandomDist(TestCase): [0.68717433461363442, 1.69175666993575979]]) assert_array_almost_equal(actual, desired, decimal=15) + def test_exponential_0(self): + assert_equal(np.random.exponential(scale=0), 0) + def test_f(self): np.random.seed(self.seed) actual = np.random.f(12, 77, size=(3, 2)) @@ -497,6 +500,9 @@ class TestRandomDist(TestCase): [31.71863275789960568, 33.30143302795922011]]) assert_array_almost_equal(actual, desired, decimal=14) + def test_gamma_0(self): + assert_equal(np.random.gamma(shape=0, scale=0), 0) + def test_geometric(self): np.random.seed(self.seed) actual = np.random.geometric(.123456789, size=(3, 2)) @@ -513,6 +519,9 @@ class TestRandomDist(TestCase): [1.10651090478803416, -0.69535848626236174]]) assert_array_almost_equal(actual, desired, decimal=15) + def test_gumbel_0(self): + assert_equal(np.random.gumbel(scale=0), 0) + def test_hypergeometric(self): np.random.seed(self.seed) actual = np.random.hypergeometric(10.1, 5.5, 14, size=(3, 2)) @@ -547,6 +556,9 @@ class TestRandomDist(TestCase): [-0.05391065675859356, 1.74901336242837324]]) assert_array_almost_equal(actual, desired, decimal=15) + def test_laplace_0(self): + assert_equal(np.random.laplace(scale=0), 0) + def test_logistic(self): np.random.seed(self.seed) actual = np.random.logistic(loc=.123456789, scale=2.0, size=(3, 2)) @@ -555,6 +567,9 @@ class TestRandomDist(TestCase): [-0.21682183359214885, 2.63373365386060332]]) assert_array_almost_equal(actual, desired, decimal=15) + def test_laplace_0(self): + assert_(np.random.laplace(scale=0) in [0, 1]) + def test_lognormal(self): np.random.seed(self.seed) actual = np.random.lognormal(mean=.123456789, sigma=2.0, size=(3, 2)) @@ -563,6 +578,9 @@ class TestRandomDist(TestCase): [65.72798501792723869, 86.84341601437161273]]) assert_array_almost_equal(actual, desired, decimal=13) + def test_lognormal_0(self): + assert_equal(np.random.lognormal(sigma=0), 1) + def test_logseries(self): np.random.seed(self.seed) actual = np.random.logseries(p=.923456789, size=(3, 2)) @@ -653,6 +671,9 @@ class TestRandomDist(TestCase): [4.18552478636557357, 4.46410668111310471]]) assert_array_almost_equal(actual, desired, decimal=15) + def test_normal_0(self): + assert_equal(np.random.normal(scale=0), 0) + def test_pareto(self): np.random.seed(self.seed) actual = np.random.pareto(a=.123456789, size=(3, 2)) @@ -700,6 +721,9 @@ class TestRandomDist(TestCase): [11.06066537006854311, 17.35468505778271009]]) assert_array_almost_equal(actual, desired, decimal=14) + def test_rayleigh_0(self): + assert_equal(np.random.rayleigh(scale=0), 0) + def test_standard_cauchy(self): np.random.seed(self.seed) actual = np.random.standard_cauchy(size=(3, 2)) @@ -724,6 +748,9 @@ class TestRandomDist(TestCase): [7.54838614231317084, 8.012756093271868]]) assert_array_almost_equal(actual, desired, decimal=14) + def test_standard_gamma_0(self): + assert_equal(np.random.standard_gamma(shape=0), 0) + def test_standard_normal(self): np.random.seed(self.seed) actual = np.random.standard_normal(size=(3, 2)) @@ -799,6 +826,9 @@ class TestRandomDist(TestCase): [0.67057783752390987, 1.39494046635066793]]) assert_array_almost_equal(actual, desired, decimal=15) + def test_weibull_0(self): + assert_equal(np.random.weibull(a=0), 0) + def test_zipf(self): np.random.seed(self.seed) actual = np.random.zipf(a=1.23, size=(3, 2)) -- cgit v1.2.1 From a2c4c117478b34a0761a98a9e5c474ab5c4f5490 Mon Sep 17 00:00:00 2001 From: Charles Harris Date: Sun, 10 Apr 2016 10:43:58 -0600 Subject: MAINT: Ignore DeprecationWarning for random_integers in tests. The warning turned up when the numpy/randome/tests were run using $ python runtests.py -t numpy/random/tests/ It doesn't show when all the tests are run. --- numpy/random/tests/test_random.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) (limited to 'numpy/random/tests/test_random.py') diff --git a/numpy/random/tests/test_random.py b/numpy/random/tests/test_random.py index a07fa52f5..013205835 100644 --- a/numpy/random/tests/test_random.py +++ b/numpy/random/tests/test_random.py @@ -260,11 +260,13 @@ class TestRandomDist(TestCase): def test_random_integers(self): np.random.seed(self.seed) - actual = np.random.random_integers(-99, 99, size=(3, 2)) - desired = np.array([[31, 3], - [-52, 41], - [-48, -66]]) - assert_array_equal(actual, desired) + with warnings.catch_warnings(): + warnings.simplefilter("ignore", DeprecationWarning) + actual = np.random.random_integers(-99, 99, size=(3, 2)) + desired = np.array([[31, 3], + [-52, 41], + [-48, -66]]) + assert_array_equal(actual, desired) def test_random_integers_max_int(self): # Tests whether random_integers can generate the @@ -272,10 +274,12 @@ class TestRandomDist(TestCase): # into a C long. Previous implementations of this # method have thrown an OverflowError when attempting # to generate this integer. - actual = np.random.random_integers(np.iinfo('l').max, - np.iinfo('l').max) - desired = np.iinfo('l').max - assert_equal(actual, desired) + with warnings.catch_warnings(): + warnings.simplefilter("ignore", DeprecationWarning) + actual = np.random.random_integers(np.iinfo('l').max, + np.iinfo('l').max) + desired = np.iinfo('l').max + assert_equal(actual, desired) def test_random_integers_deprecated(self): with warnings.catch_warnings(): -- cgit v1.2.1 From 0319d0ce3436d39a739b2537e60c1eaced80a6ca Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Sat, 2 May 2015 23:51:04 -0700 Subject: Don't allow -0. as distribution shape parameter. At least the gamma generator doesn't support it. --- numpy/random/tests/test_random.py | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'numpy/random/tests/test_random.py') diff --git a/numpy/random/tests/test_random.py b/numpy/random/tests/test_random.py index 3f4952d20..08039cbbe 100644 --- a/numpy/random/tests/test_random.py +++ b/numpy/random/tests/test_random.py @@ -483,6 +483,7 @@ class TestRandomDist(TestCase): def test_exponential_0(self): assert_equal(np.random.exponential(scale=0), 0) + assert_raises(ValueError, np.random.exponential, scale=-0.) def test_f(self): np.random.seed(self.seed) @@ -502,6 +503,7 @@ class TestRandomDist(TestCase): def test_gamma_0(self): assert_equal(np.random.gamma(shape=0, scale=0), 0) + assert_raises(ValueError, np.random.gamma, shape=-0., scale=-0.) def test_geometric(self): np.random.seed(self.seed) @@ -521,6 +523,7 @@ class TestRandomDist(TestCase): def test_gumbel_0(self): assert_equal(np.random.gumbel(scale=0), 0) + assert_raises(ValueError, np.random.gumbel, scale=-0.) def test_hypergeometric(self): np.random.seed(self.seed) @@ -558,6 +561,7 @@ class TestRandomDist(TestCase): def test_laplace_0(self): assert_equal(np.random.laplace(scale=0), 0) + assert_raises(ValueError, np.random.laplace, scale=-0.) def test_logistic(self): np.random.seed(self.seed) @@ -569,6 +573,7 @@ class TestRandomDist(TestCase): def test_laplace_0(self): assert_(np.random.laplace(scale=0) in [0, 1]) + assert_raises(ValueError, np.random.laplace, scale=-0.) def test_lognormal(self): np.random.seed(self.seed) @@ -580,6 +585,7 @@ class TestRandomDist(TestCase): def test_lognormal_0(self): assert_equal(np.random.lognormal(sigma=0), 1) + assert_raises(ValueError, np.random.lognormal, sigma=-0.) def test_logseries(self): np.random.seed(self.seed) @@ -673,6 +679,7 @@ class TestRandomDist(TestCase): def test_normal_0(self): assert_equal(np.random.normal(scale=0), 0) + assert_raises(ValueError, np.random.normal, scale=-0.) def test_pareto(self): np.random.seed(self.seed) @@ -723,6 +730,7 @@ class TestRandomDist(TestCase): def test_rayleigh_0(self): assert_equal(np.random.rayleigh(scale=0), 0) + assert_raises(ValueError, np.random.rayleigh, scale=-0.) def test_standard_cauchy(self): np.random.seed(self.seed) @@ -750,6 +758,7 @@ class TestRandomDist(TestCase): def test_standard_gamma_0(self): assert_equal(np.random.standard_gamma(shape=0), 0) + assert_raises(ValueError, np.random.standard_gamma, shape=-0.) def test_standard_normal(self): np.random.seed(self.seed) @@ -828,6 +837,7 @@ class TestRandomDist(TestCase): def test_weibull_0(self): assert_equal(np.random.weibull(a=0), 0) + assert_raises(ValueError, np.random.weibull, a=-0.) def test_zipf(self): np.random.seed(self.seed) -- cgit v1.2.1 From 308161c80f4450f05f8399343034308bd18b4e1e Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Sun, 19 Jun 2016 14:18:35 +0200 Subject: TST: Use new warnings context manager in all tests In some places, just remove aparently unnecessary filters. After this, all cases of ignore filters should be removed from the tests, making testing (even multiple runs) normally fully predictable. --- numpy/random/tests/test_random.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) (limited to 'numpy/random/tests/test_random.py') diff --git a/numpy/random/tests/test_random.py b/numpy/random/tests/test_random.py index a06de58e3..012bf4826 100644 --- a/numpy/random/tests/test_random.py +++ b/numpy/random/tests/test_random.py @@ -3,7 +3,8 @@ from __future__ import division, absolute_import, print_function import numpy as np from numpy.testing import ( TestCase, run_module_suite, assert_, assert_raises, assert_equal, - assert_warns, assert_array_equal, assert_array_almost_equal) + assert_warns, assert_array_equal, assert_array_almost_equal, + suppress_warnings) from numpy import random from numpy.compat import asbytes import sys @@ -260,13 +261,14 @@ class TestRandomDist(TestCase): def test_random_integers(self): np.random.seed(self.seed) - with warnings.catch_warnings(): - warnings.simplefilter("ignore", DeprecationWarning) + with suppress_warnings() as sup: + w = sup.record(DeprecationWarning) actual = np.random.random_integers(-99, 99, size=(3, 2)) - desired = np.array([[31, 3], - [-52, 41], - [-48, -66]]) - assert_array_equal(actual, desired) + assert_(len(w) == 1) + desired = np.array([[31, 3], + [-52, 41], + [-48, -66]]) + assert_array_equal(actual, desired) def test_random_integers_max_int(self): # Tests whether random_integers can generate the @@ -274,12 +276,14 @@ class TestRandomDist(TestCase): # into a C long. Previous implementations of this # method have thrown an OverflowError when attempting # to generate this integer. - with warnings.catch_warnings(): - warnings.simplefilter("ignore", DeprecationWarning) + with suppress_warnings() as sup: + w = sup.record(DeprecationWarning) actual = np.random.random_integers(np.iinfo('l').max, np.iinfo('l').max) - desired = np.iinfo('l').max - assert_equal(actual, desired) + assert_(len(w) == 1) + + desired = np.iinfo('l').max + assert_equal(actual, desired) def test_random_integers_deprecated(self): with warnings.catch_warnings(): -- cgit v1.2.1 From f5bb42f0b02fa954f03bf4d96801877a4837c8cb Mon Sep 17 00:00:00 2001 From: Alistair Muldal Date: Sun, 30 Oct 2016 20:04:59 +0000 Subject: BUG: Better check for invalid bounds in np.random.uniform. Also check for invalid bounds when low= and high= are arraylike rather than scalar (closes #8226) --- numpy/random/tests/test_random.py | 2 ++ 1 file changed, 2 insertions(+) (limited to 'numpy/random/tests/test_random.py') diff --git a/numpy/random/tests/test_random.py b/numpy/random/tests/test_random.py index 012bf4826..47301a770 100644 --- a/numpy/random/tests/test_random.py +++ b/numpy/random/tests/test_random.py @@ -809,6 +809,8 @@ class TestRandomDist(TestCase): assert_raises(OverflowError, func, -np.inf, 0) assert_raises(OverflowError, func, 0, np.inf) assert_raises(OverflowError, func, fmin, fmax) + assert_raises(OverflowError, func, [-np.inf], [0]) + assert_raises(OverflowError, func, [0], [np.inf]) # (fmax / 1e17) - fmin is within range, so this should not throw np.random.uniform(low=fmin, high=fmax / 1e17) -- cgit v1.2.1