Skip to content

Commit fd6c16e

Browse files
committed
Merge pull request #19 from bashtage/update-to-latest-numpy
UPD: Update code to latest NumPy changes
2 parents 2a2a979 + 08c527d commit fd6c16e

File tree

2 files changed

+72
-37
lines changed

2 files changed

+72
-37
lines changed

randomstate/interface.pyx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#cython: wraparound=False, nonecheck=False, boundscheck=False, cdivision=True
33
import sys
44
import operator
5+
import warnings
56
try:
67
from threading import Lock
78
except:
@@ -1166,6 +1167,10 @@ cdef class RandomState:
11661167
type translates to the C long type used by Python 2 for "short"
11671168
integers and its precision is platform dependent.
11681169
1170+
This function has been deprecated. Use randint instead.
1171+
1172+
.. deprecated:: 1.11.0
1173+
11691174
Parameters
11701175
----------
11711176
low : int
@@ -1231,9 +1236,17 @@ cdef class RandomState:
12311236
12321237
"""
12331238
if high is None:
1239+
warnings.warn(("This function is deprecated. Please call "
1240+
"randint(1, {low} + 1) instead".format(low=low)),
1241+
DeprecationWarning)
12341242
high = low
12351243
low = 1
12361244

1245+
else:
1246+
warnings.warn(("This function is deprecated. Please call "
1247+
"randint({low}, {high} + 1) instead".format(
1248+
low=low, high=high)), DeprecationWarning)
1249+
12371250
return self.randint(low, high + 1, size=size, dtype='l')
12381251

12391252

@@ -3789,7 +3802,6 @@ cdef class RandomState:
37893802
neg = (np.sum(u.T * v, axis=1) < 0) & (s > 0)
37903803
if np.any(neg):
37913804
s[neg] = 0.
3792-
import warnings
37933805
warnings.warn("covariance is not positive-semidefinite.",
37943806
RuntimeWarning)
37953807

randomstate/tests/tests_numpy_mt19937.py

Lines changed: 59 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
from __future__ import division, absolute_import, print_function
22

3+
import sys
4+
import warnings
5+
36
import numpy as np
4-
from numpy.testing import (
5-
TestCase, run_module_suite, assert_, assert_raises, assert_equal,
6-
assert_warns, decorators)
77
from numpy import random
88
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+
913
from randomstate.prng.mt19937 import mt19937
10-
import sys
14+
1115

1216
class TestSeed(TestCase):
1317
def test_scalar(self):
@@ -39,6 +43,7 @@ def test_invalid_array(self):
3943
assert_raises(ValueError, mt19937.RandomState, [1, 2, 4294967296])
4044
assert_raises(ValueError, mt19937.RandomState, [1, -2, 4294967296])
4145

46+
4247
class TestBinomial(TestCase):
4348
def test_n_zero(self):
4449
# Tests the corner case of n == 0 for the binomial distribution.
@@ -129,8 +134,8 @@ def test_negative_binomial(self):
129134
# arguments without truncation.
130135
self.prng.negative_binomial(0.5, 0.5)
131136

132-
class TestRandint(TestCase):
133137

138+
class TestRandint(TestCase):
134139
rfunc = mt19937.randint
135140

136141
# valid integer/boolean types
@@ -144,10 +149,10 @@ def test_bounds_checking(self):
144149
for dt in self.itype:
145150
lbnd = 0 if dt is np.bool else np.iinfo(dt).min
146151
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)
151156

152157
def test_rng_zero_and_extremes(self):
153158
for dt in self.itype:
@@ -157,18 +162,18 @@ def test_rng_zero_and_extremes(self):
157162
assert_equal(self.rfunc(tgt, tgt + 1, size=1000, dtype=dt), tgt)
158163
tgt = lbnd
159164
assert_equal(self.rfunc(tgt, tgt + 1, size=1000, dtype=dt), tgt)
160-
tgt = (lbnd + ubnd)//2
165+
tgt = (lbnd + ubnd) // 2
161166
assert_equal(self.rfunc(tgt, tgt + 1, size=1000, dtype=dt), tgt)
162167

163168
def test_in_bounds_fuzz(self):
164169
# Don't use fixed seed
165170
mt19937.seed()
166171
for dt in self.itype[1:]:
167172
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)
169174
assert_(vals.max() < ubnd)
170175
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)
172177
assert_(vals.max() < 2)
173178
assert_(vals.min() >= 0)
174179

@@ -205,6 +210,7 @@ def test_repeatability(self):
205210
res = hashlib.md5(val).hexdigest()
206211
assert_(tgt[np.dtype(np.bool).name] == res)
207212

213+
208214
class TestRandomDist(TestCase):
209215
# Make sure the random distribution returns the correct value for a
210216
# given seed
@@ -224,8 +230,8 @@ def test_randn(self):
224230
mt19937.seed(self.seed)
225231
actual = mt19937.randn(3, 2)
226232
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]])
229235
np.testing.assert_array_almost_equal(actual, desired, decimal=15)
230236

231237
def test_randint(self):
@@ -251,10 +257,24 @@ def test_random_integers_max_int(self):
251257
# method have thrown an OverflowError when attemping
252258
# to generate this integer.
253259
actual = mt19937.random_integers(np.iinfo('l').max,
254-
np.iinfo('l').max)
260+
np.iinfo('l').max)
255261
desired = np.iinfo('l').max
256262
np.testing.assert_equal(actual, desired)
257263

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+
258278
def test_random_sample(self):
259279
mt19937.seed(self.seed)
260280
actual = mt19937.random_sample((3, 2))
@@ -284,7 +304,7 @@ def test_choice_uniform_noreplace(self):
284304
def test_choice_nonuniform_noreplace(self):
285305
mt19937.seed(self.seed)
286306
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])
288308
desired = np.array([2, 3, 1])
289309
np.testing.assert_array_equal(actual, desired)
290310

@@ -301,13 +321,13 @@ def test_choice_exceptions(self):
301321
assert_raises(ValueError, sample, [[1, 2], [3, 4]], 3)
302322
assert_raises(ValueError, sample, [], 3)
303323
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]])
305325
assert_raises(ValueError, sample, [1, 2], 3, p=[0.4, 0.4, 0.2])
306326
assert_raises(ValueError, sample, [1, 2], 3, p=[1.1, -0.1])
307327
assert_raises(ValueError, sample, [1, 2], 3, p=[0.4, 0.4])
308328
assert_raises(ValueError, sample, [1, 2, 3], 4, replace=False)
309329
assert_raises(ValueError, sample, [1, 2, 3], 2, replace=False,
310-
p=[1, 0, 0])
330+
p=[1, 0, 0])
311331

312332
def test_choice_return_shape(self):
313333
p = [0.1, 0.9]
@@ -377,7 +397,7 @@ def test_shuffle_flexible(self):
377397

378398
def test_shuffle_masked(self):
379399
# 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)
381401
b = np.ma.masked_values(np.arange(20) % 3 - 1, -1)
382402
ma = np.ma.count_masked(a)
383403
mb = np.ma.count_masked(b)
@@ -400,8 +420,8 @@ def test_binomial(self):
400420
mt19937.seed(self.seed)
401421
actual = mt19937.binomial(100.123, .456, size=(3, 2))
402422
desired = np.array([[37, 43],
403-
[42, 48],
404-
[46, 45]])
423+
[42, 48],
424+
[46, 45]])
405425
np.testing.assert_array_equal(actual, desired)
406426

407427
def test_chisquare(self):
@@ -536,7 +556,7 @@ def test_logseries(self):
536556

537557
def test_multinomial(self):
538558
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))
540560
desired = np.array([[[4, 3, 5, 4, 2, 2],
541561
[5, 2, 8, 2, 2, 1]],
542562
[[3, 4, 3, 6, 0, 4],
@@ -587,9 +607,9 @@ def test_noncentral_chisquare(self):
587607
np.testing.assert_array_almost_equal(actual, desired, decimal=14)
588608

589609
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]])
593613
np.testing.assert_array_almost_equal(actual, desired, decimal=14)
594614

595615
mt19937.seed(self.seed)
@@ -602,7 +622,7 @@ def test_noncentral_chisquare(self):
602622
def test_noncentral_f(self):
603623
mt19937.seed(self.seed)
604624
actual = mt19937.noncentral_f(dfnum=5, dfden=2, nonc=1,
605-
size=(3, 2))
625+
size=(3, 2))
606626
desired = np.array([[1.40598099674926669, 0.34207973179285761],
607627
[3.57715069265772545, 7.92632662577829805],
608628
[0.43741599463544162, 1.1774208752428319]])
@@ -635,17 +655,17 @@ def test_poisson(self):
635655
mt19937.seed(self.seed)
636656
actual = mt19937.poisson(lam=.123456789, size=(3, 2))
637657
desired = np.array([[0, 0],
638-
[1, 0],
639-
[0, 0]])
658+
[1, 0],
659+
[0, 0]])
640660
np.testing.assert_array_equal(actual, desired)
641661

642662
def test_poisson_exceptions(self):
643663
lambig = np.iinfo('l').max
644664
lamneg = -1
645665
assert_raises(ValueError, mt19937.poisson, lamneg)
646-
assert_raises(ValueError, mt19937.poisson, [lamneg]*10)
666+
assert_raises(ValueError, mt19937.poisson, [lamneg] * 10)
647667
assert_raises(ValueError, mt19937.poisson, lambig)
648-
assert_raises(ValueError, mt19937.poisson, [lambig]*10)
668+
assert_raises(ValueError, mt19937.poisson, [lambig] * 10)
649669

650670
def test_power(self):
651671
mt19937.seed(self.seed)
@@ -706,7 +726,7 @@ def test_standard_t(self):
706726
def test_triangular(self):
707727
mt19937.seed(self.seed)
708728
actual = mt19937.triangular(left=5.12, mode=10.23, right=20.34,
709-
size=(3, 2))
729+
size=(3, 2))
710730
desired = np.array([[12.68117178949215784, 12.4129206149193152],
711731
[16.20131377335158263, 16.25692138747600524],
712732
[11.20400690911820263, 14.4978144835829923]])
@@ -726,8 +746,8 @@ def test_uniform_range_bounds(self):
726746

727747
func = mt19937.uniform
728748
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)
731751

732752
# (fmax / 1e17) - fmin is within range, so this should not throw
733753
mt19937.uniform(low=fmin, high=fmax / 1e17)
@@ -743,7 +763,7 @@ def test_vonmises(self):
743763
def test_vonmises_small(self):
744764
# check infinite loop, gh-4720
745765
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)
747767
np.testing.assert_(np.isfinite(r).all())
748768

749769
def test_wald(self):
@@ -801,17 +821,20 @@ def check_function(self, function, sz):
801821
def test_normal(self):
802822
def gen_random(state, out):
803823
out[...] = state.normal(size=10000)
824+
804825
self.check_function(gen_random, sz=(10000,))
805826

806827
def test_exp(self):
807828
def gen_random(state, out):
808829
out[...] = state.exponential(scale=np.ones((100, 1000)))
830+
809831
self.check_function(gen_random, sz=(100, 1000))
810832

811833
def test_multinomial(self):
812834
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))
815838

816839

817840
if __name__ == "__main__":

0 commit comments

Comments
 (0)