Skip to content

Commit 7445012

Browse files
authored
Fix seed for random test data. (#9844)
* Fix seed for random test data. Also switch to using default_rng instead of RandomState. * Fixes * one more fix. * more fixes * last one? * one more
1 parent 05f24f7 commit 7445012

16 files changed

+63
-59
lines changed

asv_bench/benchmarks/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ def requires_sparse():
3030

3131

3232
def randn(shape, frac_nan=None, chunks=None, seed=0):
33-
rng = np.random.RandomState(seed)
33+
rng = np.random.default_rng(seed)
3434
if chunks is None:
3535
x = rng.standard_normal(shape)
3636
else:
3737
import dask.array as da
3838

39-
rng = da.random.RandomState(seed)
39+
rng = da.random.default_rng(seed)
4040
x = rng.standard_normal(shape, chunks=chunks)
4141

4242
if frac_nan is not None:
@@ -47,7 +47,7 @@ def randn(shape, frac_nan=None, chunks=None, seed=0):
4747

4848

4949
def randint(low, high=None, size=None, frac_minus=None, seed=0):
50-
rng = np.random.RandomState(seed)
50+
rng = np.random.default_rng(seed)
5151
x = rng.randint(low, high, size)
5252
if frac_minus is not None:
5353
inds = rng.choice(range(x.size), int(x.size * frac_minus))

asv_bench/benchmarks/reindexing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
class Reindex:
1313
def setup(self):
14-
data = np.random.RandomState(0).randn(ntime, nx, ny)
14+
data = np.random.default_rng(0).random((ntime, nx, ny))
1515
self.ds = xr.Dataset(
1616
{"temperature": (("time", "x", "y"), data)},
1717
coords={"time": np.arange(ntime), "x": np.arange(nx), "y": np.arange(ny)},

asv_bench/benchmarks/unstacking.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
class Unstacking:
1010
def setup(self):
11-
data = np.random.RandomState(0).randn(250, 500)
11+
data = np.random.default_rng(0).random((250, 500))
1212
self.da_full = xr.DataArray(data, dims=list("ab")).stack(flat_dim=[...])
1313
self.da_missing = self.da_full[:-1]
1414
self.df_missing = self.da_missing.to_pandas()

doc/user-guide/computation.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ numpy) over all array values:
3030
.. ipython:: python
3131
3232
arr = xr.DataArray(
33-
np.random.RandomState(0).randn(2, 3), [("x", ["a", "b"]), ("y", [10, 20, 30])]
33+
np.random.default_rng(0).random((2, 3)),
34+
[("x", ["a", "b"]), ("y", [10, 20, 30])],
3435
)
3536
arr - 3
3637
abs(arr)

doc/user-guide/dask.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ work as a streaming operation, when run on arrays loaded from disk:
292292
.. ipython::
293293
:verbatim:
294294

295-
In [56]: rs = np.random.RandomState(0)
295+
In [56]: rs = np.random.default_rng(0)
296296

297297
In [57]: array1 = xr.DataArray(rs.randn(1000, 100000), dims=["place", "time"]) # 800MB
298298

doc/user-guide/pandas.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ Let's take a look:
202202

203203
.. ipython:: python
204204
205-
data = np.random.RandomState(0).rand(2, 3, 4)
205+
data = np.random.default_rng(0).rand(2, 3, 4)
206206
items = list("ab")
207207
major_axis = list("mno")
208208
minor_axis = pd.date_range(start="2000", periods=4, name="date")

xarray/tests/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,12 +298,12 @@ def assert_allclose(a, b, check_default_indexes=True, **kwargs):
298298

299299

300300
def create_test_data(
301-
seed: int | None = None,
301+
seed: int = 12345,
302302
add_attrs: bool = True,
303303
dim_sizes: tuple[int, int, int] = _DEFAULT_TEST_DIM_SIZES,
304304
use_extension_array: bool = False,
305305
) -> Dataset:
306-
rs = np.random.RandomState(seed)
306+
rs = np.random.default_rng(seed)
307307
_vars = {
308308
"var1": ["dim1", "dim2"],
309309
"var2": ["dim1", "dim2"],
@@ -329,15 +329,15 @@ def create_test_data(
329329
"dim1",
330330
pd.Categorical(
331331
rs.choice(
332-
list(string.ascii_lowercase[: rs.randint(1, 5)]),
332+
list(string.ascii_lowercase[: rs.integers(1, 5)]),
333333
size=dim_sizes[0],
334334
)
335335
),
336336
)
337337
if dim_sizes == _DEFAULT_TEST_DIM_SIZES:
338338
numbers_values = np.array([0, 1, 2, 0, 0, 1, 1, 2, 2, 3], dtype="int64")
339339
else:
340-
numbers_values = rs.randint(0, 3, _dims["dim3"], dtype="int64")
340+
numbers_values = rs.integers(0, 3, _dims["dim3"], dtype="int64")
341341
obj.coords["numbers"] = ("dim3", numbers_values)
342342
obj.encoding = {"foo": "bar"}
343343
assert_writeable(obj)

xarray/tests/test_backends.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2868,8 +2868,11 @@ def test_append_with_new_variable(self) -> None:
28682868

28692869
# check append mode for new variable
28702870
with self.create_zarr_target() as store_target:
2871-
xr.concat([ds, ds_to_append], dim="time").to_zarr(
2872-
store_target, mode="w", **self.version_kwargs
2871+
combined = xr.concat([ds, ds_to_append], dim="time")
2872+
combined.to_zarr(store_target, mode="w", **self.version_kwargs)
2873+
assert_identical(
2874+
combined,
2875+
xr.open_dataset(store_target, engine="zarr", **self.version_kwargs),
28732876
)
28742877
ds_with_new_var.to_zarr(store_target, mode="a", **self.version_kwargs)
28752878
combined = xr.concat([ds, ds_to_append], dim="time")
@@ -6494,7 +6497,7 @@ def test_zarr_safe_chunk_region(tmp_path):
64946497
arr.isel(a=slice(5, -1)).chunk(a=5).to_zarr(store, region="auto", mode="r+")
64956498

64966499
# Test if the code is detecting the last chunk correctly
6497-
data = np.random.RandomState(0).randn(2920, 25, 53)
6500+
data = np.random.default_rng(0).random((2920, 25, 53))
64986501
ds = xr.Dataset({"temperature": (("time", "lat", "lon"), data)})
64996502
chunks = {"time": 1000, "lat": 25, "lon": 53}
65006503
ds.chunk(chunks).to_zarr(store, compute=False, mode="w")

xarray/tests/test_computation.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,9 +1293,9 @@ def covariance(x, y):
12931293
(x - x.mean(axis=-1, keepdims=True)) * (y - y.mean(axis=-1, keepdims=True))
12941294
).mean(axis=-1)
12951295

1296-
rs = np.random.RandomState(42)
1297-
array1 = da.from_array(rs.randn(4, 4), chunks=(2, 4))
1298-
array2 = da.from_array(rs.randn(4, 4), chunks=(2, 4))
1296+
rs = np.random.default_rng(42)
1297+
array1 = da.from_array(rs.random((4, 4)), chunks=(2, 4))
1298+
array2 = da.from_array(rs.random((4, 4)), chunks=(2, 4))
12991299
data_array_1 = xr.DataArray(array1, dims=("x", "z"))
13001300
data_array_2 = xr.DataArray(array2, dims=("y", "z"))
13011301

xarray/tests/test_dask.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939

4040
def test_raise_if_dask_computes():
41-
data = da.from_array(np.random.RandomState(0).randn(4, 6), chunks=(2, 2))
41+
data = da.from_array(np.random.default_rng(0).random((4, 6)), chunks=(2, 2))
4242
with pytest.raises(RuntimeError, match=r"Too many computes"):
4343
with raise_if_dask_computes():
4444
data.compute()
@@ -77,7 +77,7 @@ def assertLazyAndAllClose(self, expected, actual):
7777

7878
@pytest.fixture(autouse=True)
7979
def setUp(self):
80-
self.values = np.random.RandomState(0).randn(4, 6)
80+
self.values = np.random.default_rng(0).random((4, 6))
8181
self.data = da.from_array(self.values, chunks=(2, 2))
8282

8383
self.eager_var = Variable(("x", "y"), self.values)

0 commit comments

Comments
 (0)