Skip to content

Commit 2878daa

Browse files
authored
Merge pull request scipy#23739 from mdhaber/remove_seed
MAINT: stats: remove use of `np.random.seed`
2 parents 9d3be49 + 2269f77 commit 2878daa

18 files changed

+352
-335
lines changed

scipy/stats/tests/common_tests.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -197,12 +197,8 @@ def check_named_args(distfn, x, shape_args, defaults, meths):
197197
def check_random_state_property(distfn, args):
198198
# check the random_state attribute of a distribution *instance*
199199

200-
# This test fiddles with distfn.random_state. This breaks other tests,
201-
# hence need to save it and then restore.
202-
rndm = distfn.random_state
203-
204200
# baseline: this relies on the global state
205-
np.random.seed(1234)
201+
np.random.seed(1234) # valid use of np.random.seed
206202
distfn.random_state = None
207203
r0 = distfn.rvs(*args, size=8)
208204

@@ -231,9 +227,6 @@ def check_random_state_property(distfn, args):
231227
# ... and that does not alter the instance-level random_state!
232228
npt.assert_equal(distfn.random_state.get_state(), orig_state)
233229

234-
# finally, restore the random_state
235-
distfn.random_state = rndm
236-
237230

238231
def check_meth_dtype(distfn, arg, meths):
239232
q0 = [0.25, 0.5, 0.75]

scipy/stats/tests/test_axis_nan_policy.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -989,9 +989,9 @@ def test_non_broadcastable(hypotest, args, kwds, n_samples, n_outputs, paired,
989989

990990
def test_masked_array_2_sentinel_array():
991991
# prepare arrays
992-
np.random.seed(0)
993-
A = np.random.rand(10, 11, 12)
994-
B = np.random.rand(12)
992+
rng = np.random.default_rng(805715284)
993+
A = rng.random((10, 11, 12))
994+
B = rng.random(12)
995995
mask = A < 0.5
996996
A = np.ma.masked_array(A, mask)
997997

@@ -1122,10 +1122,10 @@ def test_masked_stat_1d():
11221122
@pytest.mark.parametrize(("axis"), range(-3, 3))
11231123
def test_masked_stat_3d(axis):
11241124
# basic test of _axis_nan_policy_factory with 3D masked sample
1125-
np.random.seed(0)
1126-
a = np.random.rand(3, 4, 5)
1127-
b = np.random.rand(4, 5)
1128-
c = np.random.rand(4, 1)
1125+
rng = np.random.default_rng(3679428403)
1126+
a = rng.random((3, 4, 5))
1127+
b = rng.random((4, 5))
1128+
c = rng.random((4, 1))
11291129

11301130
mask_a = a < 0.1
11311131
mask_c = [False, False, False, True]

scipy/stats/tests/test_binned_statistic.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,9 @@ def test_1d_bincode(self):
159159

160160
def test_1d_range_keyword(self):
161161
# Regression test for gh-3063, range can be (min, max) or [(min, max)]
162-
np.random.seed(9865)
162+
rng = np.random.default_rng(6823616729)
163163
x = np.arange(30)
164-
data = np.random.random(30)
164+
data = rng.random(30)
165165

166166
mean, bins, _ = binned_statistic(x[:15], data[:15])
167167
mean_range, bins_range, _ = binned_statistic(x, data, range=[(0, 14)])
@@ -479,8 +479,9 @@ def test_dd_binnumbers_unraveled(self):
479479

480480
def test_dd_binned_statistic_result(self):
481481
# NOTE: tests the reuse of bin_edges from previous call
482-
x = np.random.random((10000, 3))
483-
v = np.random.random(10000)
482+
rng = np.random.default_rng(8111360615)
483+
x = rng.random((10000, 3))
484+
v = rng.random(10000)
484485
bins = np.linspace(0, 1, 10)
485486
bins = (bins, bins, bins)
486487

@@ -494,8 +495,9 @@ def test_dd_binned_statistic_result(self):
494495
assert_allclose(stat, stat2)
495496

496497
def test_dd_zero_dedges(self):
497-
x = np.random.random((10000, 3))
498-
v = np.random.random(10000)
498+
rng = np.random.default_rng(1132724173)
499+
x = rng.random((10000, 3))
500+
v = rng.random(10000)
499501
bins = np.linspace(0, 1, 10)
500502
bins = np.append(bins, 1)
501503
bins = (bins, bins, bins)

scipy/stats/tests/test_continuous_basic.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,6 @@ def test_gh1320_regression():
587587

588588
def test_method_of_moments():
589589
# example from https://en.wikipedia.org/wiki/Method_of_moments_(statistics)
590-
np.random.seed(1234)
591590
x = [0, 0, 0, 0, 1]
592591
a = 1/5 - 2*np.sqrt(3)/5
593592
b = 1/5 + 2*np.sqrt(3)/5

scipy/stats/tests/test_discrete_basic.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,9 @@ def test_ppf_with_loc(dist, args):
168168
except TypeError:
169169
distfn = dist
170170
#check with a negative, no and positive relocation.
171-
np.random.seed(1942349)
172-
re_locs = [np.random.randint(-10, -1), 0, np.random.randint(1, 10)]
171+
rng = np.random.default_rng(5108587887)
172+
173+
re_locs = [rng.integers(-10, -1), 0, rng.integers(1, 10)]
173174
_a, _b = distfn.support(*args)
174175
for loc in re_locs:
175176
npt.assert_array_equal(
@@ -185,17 +186,17 @@ def test_isf_with_loc(dist, args):
185186
except TypeError:
186187
distfn = dist
187188
# check with a negative, no and positive relocation.
188-
np.random.seed(1942349)
189-
re_locs = [np.random.randint(-10, -1), 0, np.random.randint(1, 10)]
189+
rng = np.random.default_rng(4030503535)
190+
re_locs = [rng.integers(-10, -1), 0, rng.integers(1, 10)]
190191
_a, _b = distfn.support(*args)
191192
for loc in re_locs:
192193
expected = _b + loc, _a - 1 + loc
193194
res = distfn.isf(0., *args, loc=loc), distfn.isf(1., *args, loc=loc)
194195
npt.assert_array_equal(expected, res)
195196
# test broadcasting behaviour
196-
re_locs = [np.random.randint(-10, -1, size=(5, 3)),
197+
re_locs = [rng.integers(-10, -1, size=(5, 3)),
197198
np.zeros((5, 3)),
198-
np.random.randint(1, 10, size=(5, 3))]
199+
rng.integers(1, 10, size=(5, 3))]
199200
_a, _b = distfn.support(*args)
200201
for loc in re_locs:
201202
expected = _b + loc, _a - 1 + loc

scipy/stats/tests/test_discrete_distns.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -177,17 +177,18 @@ def test_issue_11134():
177177

178178

179179
def test_issue_7406():
180-
np.random.seed(0)
181-
assert_equal(binom.ppf(np.random.rand(10), 0, 0.5), 0)
180+
rng = np.random.default_rng(4763112764)
181+
assert_equal(binom.ppf(rng.random(10), 0, 0.5), 0)
182182

183183
# Also check that endpoints (q=0, q=1) are correct
184184
assert_equal(binom.ppf(0, 0, 0.5), -1)
185185
assert_equal(binom.ppf(1, 0, 0.5), 0)
186186

187187

188188
def test_issue_5122():
189+
rng = np.random.default_rng(8312492117)
189190
p = 0
190-
n = np.random.randint(100, size=10)
191+
n = rng.integers(100, size=10)
191192

192193
x = 0
193194
ppf = binom.ppf(x, n, p)
@@ -397,17 +398,19 @@ def test_mean_against_mpmath(self, a, n, ref):
397398

398399

399400
class TestNCH:
400-
np.random.seed(2) # seeds 0 and 1 had some xl = xu; randint failed
401-
shape = (2, 4, 3)
402-
max_m = 100
403-
m1 = np.random.randint(1, max_m, size=shape) # red balls
404-
m2 = np.random.randint(1, max_m, size=shape) # white balls
405-
N = m1 + m2 # total balls
406-
n = randint.rvs(0, N, size=N.shape) # number of draws
407-
xl = np.maximum(0, n-m2) # lower bound of support
408-
xu = np.minimum(n, m1) # upper bound of support
409-
x = randint.rvs(xl, xu, size=xl.shape)
410-
odds = np.random.rand(*x.shape)*2
401+
def setup_method(self):
402+
rng = np.random.default_rng(7162434334)
403+
shape = (2, 4, 3)
404+
max_m = 100
405+
m1 = rng.integers(1, max_m, size=shape) # red balls
406+
m2 = rng.integers(1, max_m, size=shape) # white balls
407+
N = m1 + m2 # total balls
408+
n = randint.rvs(0, N, size=N.shape, random_state=rng) # number of draws
409+
xl = np.maximum(0, n-m2) # lower bound of support
410+
xu = np.minimum(n, m1) # upper bound of support
411+
x = randint.rvs(xl, xu, size=xl.shape, random_state=rng)
412+
odds = rng.random(x.shape)*2
413+
self.x, self.N, self.m1, self.n, self.odds = x, N, m1, n, odds
411414

412415
# test output is more readable when function names (strings) are passed
413416
@pytest.mark.parametrize('dist_name',

0 commit comments

Comments
 (0)