1
1
from __future__ import division , absolute_import , print_function
2
2
3
+ import sys
4
+ import warnings
5
+
3
6
import numpy as np
4
- from numpy .testing import (
5
- TestCase , run_module_suite , assert_ , assert_raises , assert_equal ,
6
- assert_warns , decorators )
7
7
from numpy import random
8
8
from numpy .compat import asbytes
9
+ from numpy .testing import (
10
+ TestCase , run_module_suite , assert_ , assert_raises , assert_equal ,
11
+ assert_warns , decorators )
12
+
9
13
from randomstate .prng .mt19937 import mt19937
10
- import sys
14
+
11
15
12
16
class TestSeed (TestCase ):
13
17
def test_scalar (self ):
@@ -39,6 +43,7 @@ def test_invalid_array(self):
39
43
assert_raises (ValueError , mt19937 .RandomState , [1 , 2 , 4294967296 ])
40
44
assert_raises (ValueError , mt19937 .RandomState , [1 , - 2 , 4294967296 ])
41
45
46
+
42
47
class TestBinomial (TestCase ):
43
48
def test_n_zero (self ):
44
49
# Tests the corner case of n == 0 for the binomial distribution.
@@ -129,8 +134,8 @@ def test_negative_binomial(self):
129
134
# arguments without truncation.
130
135
self .prng .negative_binomial (0.5 , 0.5 )
131
136
132
- class TestRandint (TestCase ):
133
137
138
+ class TestRandint (TestCase ):
134
139
rfunc = mt19937 .randint
135
140
136
141
# valid integer/boolean types
@@ -144,10 +149,10 @@ def test_bounds_checking(self):
144
149
for dt in self .itype :
145
150
lbnd = 0 if dt is np .bool else np .iinfo (dt ).min
146
151
ubnd = 2 if dt is np .bool else np .iinfo (dt ).max + 1
147
- assert_raises (ValueError , self .rfunc , lbnd - 1 , ubnd , dtype = dt )
148
- assert_raises (ValueError , self .rfunc , lbnd , ubnd + 1 , dtype = dt )
149
- assert_raises (ValueError , self .rfunc , ubnd , lbnd , dtype = dt )
150
- assert_raises (ValueError , self .rfunc , 1 , 0 , dtype = dt )
152
+ assert_raises (ValueError , self .rfunc , lbnd - 1 , ubnd , dtype = dt )
153
+ assert_raises (ValueError , self .rfunc , lbnd , ubnd + 1 , dtype = dt )
154
+ assert_raises (ValueError , self .rfunc , ubnd , lbnd , dtype = dt )
155
+ assert_raises (ValueError , self .rfunc , 1 , 0 , dtype = dt )
151
156
152
157
def test_rng_zero_and_extremes (self ):
153
158
for dt in self .itype :
@@ -157,18 +162,18 @@ def test_rng_zero_and_extremes(self):
157
162
assert_equal (self .rfunc (tgt , tgt + 1 , size = 1000 , dtype = dt ), tgt )
158
163
tgt = lbnd
159
164
assert_equal (self .rfunc (tgt , tgt + 1 , size = 1000 , dtype = dt ), tgt )
160
- tgt = (lbnd + ubnd )// 2
165
+ tgt = (lbnd + ubnd ) // 2
161
166
assert_equal (self .rfunc (tgt , tgt + 1 , size = 1000 , dtype = dt ), tgt )
162
167
163
168
def test_in_bounds_fuzz (self ):
164
169
# Don't use fixed seed
165
170
mt19937 .seed ()
166
171
for dt in self .itype [1 :]:
167
172
for ubnd in [4 , 8 , 16 ]:
168
- vals = self .rfunc (2 , ubnd , size = 2 ** 16 , dtype = dt )
173
+ vals = self .rfunc (2 , ubnd , size = 2 ** 16 , dtype = dt )
169
174
assert_ (vals .max () < ubnd )
170
175
assert_ (vals .min () >= 2 )
171
- vals = self .rfunc (0 , 2 , size = 2 ** 16 , dtype = np .bool )
176
+ vals = self .rfunc (0 , 2 , size = 2 ** 16 , dtype = np .bool )
172
177
assert_ (vals .max () < 2 )
173
178
assert_ (vals .min () >= 0 )
174
179
@@ -205,6 +210,7 @@ def test_repeatability(self):
205
210
res = hashlib .md5 (val ).hexdigest ()
206
211
assert_ (tgt [np .dtype (np .bool ).name ] == res )
207
212
213
+
208
214
class TestRandomDist (TestCase ):
209
215
# Make sure the random distribution returns the correct value for a
210
216
# given seed
@@ -224,8 +230,8 @@ def test_randn(self):
224
230
mt19937 .seed (self .seed )
225
231
actual = mt19937 .randn (3 , 2 )
226
232
desired = np .array ([[1.34016345771863121 , 1.73759122771936081 ],
227
- [1.498988344300628 , - 0.2286433324536169 ],
228
- [2.031033998682787 , 2.17032494605655257 ]])
233
+ [1.498988344300628 , - 0.2286433324536169 ],
234
+ [2.031033998682787 , 2.17032494605655257 ]])
229
235
np .testing .assert_array_almost_equal (actual , desired , decimal = 15 )
230
236
231
237
def test_randint (self ):
@@ -251,10 +257,24 @@ def test_random_integers_max_int(self):
251
257
# method have thrown an OverflowError when attemping
252
258
# to generate this integer.
253
259
actual = mt19937 .random_integers (np .iinfo ('l' ).max ,
254
- np .iinfo ('l' ).max )
260
+ np .iinfo ('l' ).max )
255
261
desired = np .iinfo ('l' ).max
256
262
np .testing .assert_equal (actual , desired )
257
263
264
+ def test_random_integers_deprecated (self ):
265
+ with warnings .catch_warnings ():
266
+ warnings .simplefilter ("error" , DeprecationWarning )
267
+
268
+ # DeprecationWarning raised with high == None
269
+ assert_raises (DeprecationWarning ,
270
+ mt19937 .random_integers ,
271
+ np .iinfo ('l' ).max )
272
+
273
+ # DeprecationWarning raised with high != None
274
+ assert_raises (DeprecationWarning ,
275
+ mt19937 .random_integers ,
276
+ np .iinfo ('l' ).max , np .iinfo ('l' ).max )
277
+
258
278
def test_random_sample (self ):
259
279
mt19937 .seed (self .seed )
260
280
actual = mt19937 .random_sample ((3 , 2 ))
@@ -284,7 +304,7 @@ def test_choice_uniform_noreplace(self):
284
304
def test_choice_nonuniform_noreplace (self ):
285
305
mt19937 .seed (self .seed )
286
306
actual = mt19937 .choice (4 , 3 , replace = False ,
287
- p = [0.1 , 0.3 , 0.5 , 0.1 ])
307
+ p = [0.1 , 0.3 , 0.5 , 0.1 ])
288
308
desired = np .array ([2 , 3 , 1 ])
289
309
np .testing .assert_array_equal (actual , desired )
290
310
@@ -301,13 +321,13 @@ def test_choice_exceptions(self):
301
321
assert_raises (ValueError , sample , [[1 , 2 ], [3 , 4 ]], 3 )
302
322
assert_raises (ValueError , sample , [], 3 )
303
323
assert_raises (ValueError , sample , [1 , 2 , 3 , 4 ], 3 ,
304
- p = [[0.25 , 0.25 ], [0.25 , 0.25 ]])
324
+ p = [[0.25 , 0.25 ], [0.25 , 0.25 ]])
305
325
assert_raises (ValueError , sample , [1 , 2 ], 3 , p = [0.4 , 0.4 , 0.2 ])
306
326
assert_raises (ValueError , sample , [1 , 2 ], 3 , p = [1.1 , - 0.1 ])
307
327
assert_raises (ValueError , sample , [1 , 2 ], 3 , p = [0.4 , 0.4 ])
308
328
assert_raises (ValueError , sample , [1 , 2 , 3 ], 4 , replace = False )
309
329
assert_raises (ValueError , sample , [1 , 2 , 3 ], 2 , replace = False ,
310
- p = [1 , 0 , 0 ])
330
+ p = [1 , 0 , 0 ])
311
331
312
332
def test_choice_return_shape (self ):
313
333
p = [0.1 , 0.9 ]
@@ -377,7 +397,7 @@ def test_shuffle_flexible(self):
377
397
378
398
def test_shuffle_masked (self ):
379
399
# gh-3263
380
- a = np .ma .masked_values (np .reshape (range (20 ), (5 ,4 )) % 3 - 1 , - 1 )
400
+ a = np .ma .masked_values (np .reshape (range (20 ), (5 , 4 )) % 3 - 1 , - 1 )
381
401
b = np .ma .masked_values (np .arange (20 ) % 3 - 1 , - 1 )
382
402
ma = np .ma .count_masked (a )
383
403
mb = np .ma .count_masked (b )
@@ -400,8 +420,8 @@ def test_binomial(self):
400
420
mt19937 .seed (self .seed )
401
421
actual = mt19937 .binomial (100.123 , .456 , size = (3 , 2 ))
402
422
desired = np .array ([[37 , 43 ],
403
- [42 , 48 ],
404
- [46 , 45 ]])
423
+ [42 , 48 ],
424
+ [46 , 45 ]])
405
425
np .testing .assert_array_equal (actual , desired )
406
426
407
427
def test_chisquare (self ):
@@ -536,7 +556,7 @@ def test_logseries(self):
536
556
537
557
def test_multinomial (self ):
538
558
mt19937 .seed (self .seed )
539
- actual = mt19937 .multinomial (20 , [1 / 6. ]* 6 , size = (3 , 2 ))
559
+ actual = mt19937 .multinomial (20 , [1 / 6. ] * 6 , size = (3 , 2 ))
540
560
desired = np .array ([[[4 , 3 , 5 , 4 , 2 , 2 ],
541
561
[5 , 2 , 8 , 2 , 2 , 1 ]],
542
562
[[3 , 4 , 3 , 6 , 0 , 4 ],
@@ -587,9 +607,9 @@ def test_noncentral_chisquare(self):
587
607
np .testing .assert_array_almost_equal (actual , desired , decimal = 14 )
588
608
589
609
actual = mt19937 .noncentral_chisquare (df = .5 , nonc = .2 , size = (3 , 2 ))
590
- desired = np .array ([[ 1.47145377828516666 , 0.15052899268012659 ],
591
- [ 0.00943803056963588 , 1.02647251615666169 ],
592
- [ 0.332334982684171 , 0.15451287602753125 ]])
610
+ desired = np .array ([[1.47145377828516666 , 0.15052899268012659 ],
611
+ [0.00943803056963588 , 1.02647251615666169 ],
612
+ [0.332334982684171 , 0.15451287602753125 ]])
593
613
np .testing .assert_array_almost_equal (actual , desired , decimal = 14 )
594
614
595
615
mt19937 .seed (self .seed )
@@ -602,7 +622,7 @@ def test_noncentral_chisquare(self):
602
622
def test_noncentral_f (self ):
603
623
mt19937 .seed (self .seed )
604
624
actual = mt19937 .noncentral_f (dfnum = 5 , dfden = 2 , nonc = 1 ,
605
- size = (3 , 2 ))
625
+ size = (3 , 2 ))
606
626
desired = np .array ([[1.40598099674926669 , 0.34207973179285761 ],
607
627
[3.57715069265772545 , 7.92632662577829805 ],
608
628
[0.43741599463544162 , 1.1774208752428319 ]])
@@ -635,17 +655,17 @@ def test_poisson(self):
635
655
mt19937 .seed (self .seed )
636
656
actual = mt19937 .poisson (lam = .123456789 , size = (3 , 2 ))
637
657
desired = np .array ([[0 , 0 ],
638
- [1 , 0 ],
639
- [0 , 0 ]])
658
+ [1 , 0 ],
659
+ [0 , 0 ]])
640
660
np .testing .assert_array_equal (actual , desired )
641
661
642
662
def test_poisson_exceptions (self ):
643
663
lambig = np .iinfo ('l' ).max
644
664
lamneg = - 1
645
665
assert_raises (ValueError , mt19937 .poisson , lamneg )
646
- assert_raises (ValueError , mt19937 .poisson , [lamneg ]* 10 )
666
+ assert_raises (ValueError , mt19937 .poisson , [lamneg ] * 10 )
647
667
assert_raises (ValueError , mt19937 .poisson , lambig )
648
- assert_raises (ValueError , mt19937 .poisson , [lambig ]* 10 )
668
+ assert_raises (ValueError , mt19937 .poisson , [lambig ] * 10 )
649
669
650
670
def test_power (self ):
651
671
mt19937 .seed (self .seed )
@@ -706,7 +726,7 @@ def test_standard_t(self):
706
726
def test_triangular (self ):
707
727
mt19937 .seed (self .seed )
708
728
actual = mt19937 .triangular (left = 5.12 , mode = 10.23 , right = 20.34 ,
709
- size = (3 , 2 ))
729
+ size = (3 , 2 ))
710
730
desired = np .array ([[12.68117178949215784 , 12.4129206149193152 ],
711
731
[16.20131377335158263 , 16.25692138747600524 ],
712
732
[11.20400690911820263 , 14.4978144835829923 ]])
@@ -726,8 +746,8 @@ def test_uniform_range_bounds(self):
726
746
727
747
func = mt19937 .uniform
728
748
np .testing .assert_raises (OverflowError , func , - np .inf , 0 )
729
- np .testing .assert_raises (OverflowError , func , 0 , np .inf )
730
- np .testing .assert_raises (OverflowError , func , fmin , fmax )
749
+ np .testing .assert_raises (OverflowError , func , 0 , np .inf )
750
+ np .testing .assert_raises (OverflowError , func , fmin , fmax )
731
751
732
752
# (fmax / 1e17) - fmin is within range, so this should not throw
733
753
mt19937 .uniform (low = fmin , high = fmax / 1e17 )
@@ -743,7 +763,7 @@ def test_vonmises(self):
743
763
def test_vonmises_small (self ):
744
764
# check infinite loop, gh-4720
745
765
mt19937 .seed (self .seed )
746
- r = mt19937 .vonmises (mu = 0. , kappa = 1.1e-8 , size = 10 ** 6 )
766
+ r = mt19937 .vonmises (mu = 0. , kappa = 1.1e-8 , size = 10 ** 6 )
747
767
np .testing .assert_ (np .isfinite (r ).all ())
748
768
749
769
def test_wald (self ):
@@ -801,17 +821,20 @@ def check_function(self, function, sz):
801
821
def test_normal (self ):
802
822
def gen_random (state , out ):
803
823
out [...] = state .normal (size = 10000 )
824
+
804
825
self .check_function (gen_random , sz = (10000 ,))
805
826
806
827
def test_exp (self ):
807
828
def gen_random (state , out ):
808
829
out [...] = state .exponential (scale = np .ones ((100 , 1000 )))
830
+
809
831
self .check_function (gen_random , sz = (100 , 1000 ))
810
832
811
833
def test_multinomial (self ):
812
834
def gen_random (state , out ):
813
- out [...] = state .multinomial (10 , [1 / 6. ]* 6 , size = 10000 )
814
- self .check_function (gen_random , sz = (10000 ,6 ))
835
+ out [...] = state .multinomial (10 , [1 / 6. ] * 6 , size = 10000 )
836
+
837
+ self .check_function (gen_random , sz = (10000 , 6 ))
815
838
816
839
817
840
if __name__ == "__main__" :
0 commit comments