1
1
from __future__ import division , absolute_import , print_function
2
2
3
- import numpy as np
4
- from numpy .testing import (
5
- TestCase , run_module_suite , assert_ , assert_raises , assert_equal ,
6
- assert_warns , assert_no_warnings , assert_array_equal ,
7
- assert_array_almost_equal )
8
3
import sys
9
4
import warnings
5
+
6
+ import numpy as np
7
+ from numpy .testing import (
8
+ TestCase , run_module_suite , assert_ , assert_raises , assert_equal ,
9
+ assert_warns , assert_no_warnings , assert_array_equal ,
10
+ assert_array_almost_equal )
11
+
10
12
import randomstate as random
11
13
from randomstate .compat import suppress_warnings
12
14
from randomstate .prng .mt19937 import mt19937
13
15
16
+
14
17
class TestSeed (TestCase ):
15
18
def test_scalar (self ):
16
19
s = mt19937 .RandomState (0 )
@@ -90,7 +93,7 @@ def setUp(self):
90
93
self .seed = 1234567890
91
94
self .prng = random .RandomState (self .seed )
92
95
self .state = self .prng .get_state ()
93
- self .legacy_state = self .prng .get_state (legacy = True ) # Use legacy to get old NumPy state
96
+ self .legacy_state = self .prng .get_state (legacy = True ) # Use legacy to get old NumPy state
94
97
95
98
def test_basic (self ):
96
99
old = self .prng .tomaxint (16 )
@@ -135,7 +138,6 @@ def test_negative_binomial(self):
135
138
136
139
137
140
class TestRandint (TestCase ):
138
-
139
141
rfunc = random .randint
140
142
141
143
# valid integer/boolean types
@@ -185,10 +187,10 @@ def test_full_range(self):
185
187
def test_in_bounds_fuzz (self ):
186
188
# Don't use fixed seed
187
189
mt19937 .seed ()
188
-
190
+
189
191
for dt in self .itype [1 :]:
190
192
for ubnd in [4 , 8 , 16 ]:
191
- vals = self .rfunc (2 , ubnd , size = 2 ** 16 , dtype = dt )
193
+ vals = self .rfunc (2 , ubnd , size = 2 ** 16 , dtype = dt )
192
194
assert_ (vals .max () < ubnd )
193
195
assert_ (vals .min () >= 2 )
194
196
@@ -480,9 +482,9 @@ def test_beta(self):
480
482
mt19937 .seed (self .seed )
481
483
actual = mt19937 .beta (.1 , .9 , size = (3 , 2 ))
482
484
desired = np .array (
483
- [[1.45341850513746058e-02 , 5.31297615662868145e-04 ],
484
- [1.85366619058432324e-06 , 4.19214516800110563e-03 ],
485
- [1.58405155108498093e-04 , 1.26252891949397652e-04 ]])
485
+ [[1.45341850513746058e-02 , 5.31297615662868145e-04 ],
486
+ [1.85366619058432324e-06 , 4.19214516800110563e-03 ],
487
+ [1.58405155108498093e-04 , 1.26252891949397652e-04 ]])
486
488
assert_array_almost_equal (actual , desired , decimal = 15 )
487
489
488
490
def test_binomial (self ):
@@ -512,6 +514,8 @@ def test_dirichlet(self):
512
514
[[0.59266909280647828 , 0.40733090719352177 ],
513
515
[0.56974431743975207 , 0.43025568256024799 ]]])
514
516
assert_array_almost_equal (actual , desired , decimal = 15 )
517
+ bad_alpha = np .array ([5.4e-01 , - 1.0e-16 ])
518
+ assert_raises (ValueError , mt19937 .dirichlet , bad_alpha )
515
519
516
520
def test_dirichlet_size (self ):
517
521
# gh-3173
@@ -660,7 +664,7 @@ def test_multivariate_normal(self):
660
664
cov = [[1 , 0 ], [0 , 1 ]]
661
665
size = (3 , 2 )
662
666
actual = mt19937 .multivariate_normal (mean , cov , size )
663
- desired = np .array ([[[1.463620246718631 , 11.73759122771936 ],
667
+ desired = np .array ([[[1.463620246718631 , 11.73759122771936 ],
664
668
[1.622445133300628 , 9.771356667546383 ]],
665
669
[[2.154490787682787 , 12.170324946056553 ],
666
670
[1.719909438201865 , 9.230548443648306 ]],
@@ -720,7 +724,7 @@ def test_noncentral_chisquare(self):
720
724
def test_noncentral_f (self ):
721
725
mt19937 .seed (self .seed )
722
726
actual = mt19937 .noncentral_f (dfnum = 5 , dfden = 2 , nonc = 1 ,
723
- size = (3 , 2 ))
727
+ size = (3 , 2 ))
724
728
desired = np .array ([[1.40598099674926669 , 0.34207973179285761 ],
725
729
[3.57715069265772545 , 7.92632662577829805 ],
726
730
[0.43741599463544162 , 1.1774208752428319 ]])
@@ -742,9 +746,9 @@ def test_pareto(self):
742
746
mt19937 .seed (self .seed )
743
747
actual = mt19937 .pareto (a = .123456789 , size = (3 , 2 ))
744
748
desired = np .array (
745
- [[2.46852460439034849e+03 , 1.41286880810518346e+03 ],
746
- [5.28287797029485181e+07 , 6.57720981047328785e+07 ],
747
- [1.40840323350391515e+02 , 1.98390255135251704e+05 ]])
749
+ [[2.46852460439034849e+03 , 1.41286880810518346e+03 ],
750
+ [5.28287797029485181e+07 , 6.57720981047328785e+07 ],
751
+ [1.40840323350391515e+02 , 1.98390255135251704e+05 ]])
748
752
# For some reason on 32-bit x86 Ubuntu 12.10 the [1, 0] entry in this
749
753
# matrix differs by 24 nulps. Discussion:
750
754
# http://mail.scipy.org/pipermail/numpy-discussion/2012-September/063801.html
@@ -765,9 +769,9 @@ def test_poisson_exceptions(self):
765
769
lambig = np .iinfo ('l' ).max
766
770
lamneg = - 1
767
771
assert_raises (ValueError , mt19937 .poisson , lamneg )
768
- assert_raises (ValueError , mt19937 .poisson , [lamneg ]* 10 )
772
+ assert_raises (ValueError , mt19937 .poisson , [lamneg ] * 10 )
769
773
assert_raises (ValueError , mt19937 .poisson , lambig )
770
- assert_raises (ValueError , mt19937 .poisson , [lambig ]* 10 )
774
+ assert_raises (ValueError , mt19937 .poisson , [lambig ] * 10 )
771
775
772
776
def test_power (self ):
773
777
mt19937 .seed (self .seed )
@@ -856,8 +860,8 @@ def test_uniform_range_bounds(self):
856
860
857
861
func = mt19937 .uniform
858
862
assert_raises (OverflowError , func , - np .inf , 0 )
859
- assert_raises (OverflowError , func , 0 , np .inf )
860
- assert_raises (OverflowError , func , fmin , fmax )
863
+ assert_raises (OverflowError , func , 0 , np .inf )
864
+ assert_raises (OverflowError , func , fmin , fmax )
861
865
assert_raises (OverflowError , func , [- np .inf ], [0 ])
862
866
assert_raises (OverflowError , func , [0 ], [np .inf ])
863
867
@@ -886,7 +890,7 @@ def __int__(self):
886
890
887
891
throwing_int = np .array (1 ).view (ThrowingInteger )
888
892
assert_raises (TypeError , mt19937 .hypergeometric , throwing_int , 1 , 1 )
889
-
893
+
890
894
def test_vonmises (self ):
891
895
mt19937 .seed (self .seed )
892
896
actual = mt19937 .vonmises (mu = 1.23 , kappa = 1.54 , size = (3 , 2 ))
@@ -1485,6 +1489,7 @@ def test_logseries(self):
1485
1489
assert_raises (ValueError , logseries , bad_p_one * 3 )
1486
1490
assert_raises (ValueError , logseries , bad_p_two * 3 )
1487
1491
1492
+
1488
1493
class TestThread (TestCase ):
1489
1494
# make sure each state produces the same sequence even in threads
1490
1495
def setUp (self ):
@@ -1515,18 +1520,22 @@ def check_function(self, function, sz):
1515
1520
def test_normal (self ):
1516
1521
def gen_random (state , out ):
1517
1522
out [...] = state .normal (size = 10000 )
1523
+
1518
1524
self .check_function (gen_random , sz = (10000 ,))
1519
1525
1520
1526
def test_exp (self ):
1521
1527
def gen_random (state , out ):
1522
1528
out [...] = state .exponential (scale = np .ones ((100 , 1000 )))
1529
+
1523
1530
self .check_function (gen_random , sz = (100 , 1000 ))
1524
1531
1525
1532
def test_multinomial (self ):
1526
1533
def gen_random (state , out ):
1527
- out [...] = state .multinomial (10 , [1 / 6. ]* 6 , size = 10000 )
1534
+ out [...] = state .multinomial (10 , [1 / 6. ] * 6 , size = 10000 )
1535
+
1528
1536
self .check_function (gen_random , sz = (10000 , 6 ))
1529
1537
1538
+
1530
1539
# See Issue #4263
1531
1540
class TestSingleEltArrayInput (TestCase ):
1532
1541
def setUp (self ):
@@ -1581,23 +1590,23 @@ def test_two_arg_funcs(self):
1581
1590
out = func (self .argOne , argTwo [0 ])
1582
1591
self .assertEqual (out .shape , self .tgtShape )
1583
1592
1584
- # TODO: Uncomment once randint can broadcast arguments
1585
- # def test_randint(self):
1586
- # itype = [np.bool, np.int8, np.uint8, np.int16, np.uint16,
1587
- # np.int32, np.uint32, np.int64, np.uint64]
1588
- # func = mt19937.randint
1589
- # high = np.array([1])
1590
- # low = np.array([0])
1591
- #
1592
- # for dt in itype:
1593
- # out = func(low, high, dtype=dt)
1594
- # self.assert_equal(out.shape, self.tgtShape)
1595
- #
1596
- # out = func(low[0], high, dtype=dt)
1597
- # self.assert_equal(out.shape, self.tgtShape)
1598
- #
1599
- # out = func(low, high[0], dtype=dt)
1600
- # self.assert_equal(out.shape, self.tgtShape)
1593
+ # TODO: Uncomment once randint can broadcast arguments
1594
+ # def test_randint(self):
1595
+ # itype = [np.bool, np.int8, np.uint8, np.int16, np.uint16,
1596
+ # np.int32, np.uint32, np.int64, np.uint64]
1597
+ # func = mt19937.randint
1598
+ # high = np.array([1])
1599
+ # low = np.array([0])
1600
+ #
1601
+ # for dt in itype:
1602
+ # out = func(low, high, dtype=dt)
1603
+ # self.assert_equal(out.shape, self.tgtShape)
1604
+ #
1605
+ # out = func(low[0], high, dtype=dt)
1606
+ # self.assert_equal(out.shape, self.tgtShape)
1607
+ #
1608
+ # out = func(low, high[0], dtype=dt)
1609
+ # self.assert_equal(out.shape, self.tgtShape)
1601
1610
1602
1611
def test_three_arg_funcs (self ):
1603
1612
funcs = [mt19937 .noncentral_f , mt19937 .triangular ,
@@ -1613,5 +1622,6 @@ def test_three_arg_funcs(self):
1613
1622
out = func (self .argOne , self .argTwo [0 ], self .argThree )
1614
1623
self .assertEqual (out .shape , self .tgtShape )
1615
1624
1625
+
1616
1626
if __name__ == "__main__" :
1617
1627
run_module_suite ()
0 commit comments