From 1d9d0c0cd9a6cdb9c43922a507489f130d04d7a1 Mon Sep 17 00:00:00 2001 From: chrishavlin Date: Tue, 2 Apr 2024 14:25:56 -0500 Subject: [PATCH] replace np.random.random with new Generator api --- src/yt_napari/_special_loaders.py | 3 ++- src/yt_napari/_tests/conftest.py | 3 ++- src/yt_napari/_tests/test_model_ingestor.py | 23 ++++++++++++--------- src/yt_napari/_tests/test_timeseries.py | 6 ++++-- src/yt_napari/_tests/test_widget_reader.py | 3 ++- 5 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/yt_napari/_special_loaders.py b/src/yt_napari/_special_loaders.py index 259c491..9620508 100644 --- a/src/yt_napari/_special_loaders.py +++ b/src/yt_napari/_special_loaders.py @@ -5,7 +5,8 @@ def _ytnapari_load_grid(): - arr = np.random.random(size=(64, 64, 64)) + rng = np.random.default_rng() + arr = rng.random(size=(64, 64, 64)) d = dict(density=(arr, "g/cm**3"), temperature=(arr, "K")) bbox = np.array([[-1.5, 1.5], [-1.5, 1.5], [-1.5, 1.5]]) shp = arr.shape diff --git a/src/yt_napari/_tests/conftest.py b/src/yt_napari/_tests/conftest.py index 6058bb3..0c5a153 100644 --- a/src/yt_napari/_tests/conftest.py +++ b/src/yt_napari/_tests/conftest.py @@ -7,7 +7,8 @@ def yt_ugrid_ds_fn(tmpdir_factory): # this fixture generates a random yt dataset saved to disk that can be # re-loaded and sampled. - arr = np.random.random(size=(64, 64, 64)) + rng = np.random.default_rng() + arr = rng.random(size=(64, 64, 64)) d = dict(density=(arr, "g/cm**3"), temperature=(arr, "K")) bbox = np.array([[-1.5, 1.5], [-1.5, 1.5], [-1.5, 1.5]]) shp = arr.shape diff --git a/src/yt_napari/_tests/test_model_ingestor.py b/src/yt_napari/_tests/test_model_ingestor.py index 403b4f2..35c86e0 100644 --- a/src/yt_napari/_tests/test_model_ingestor.py +++ b/src/yt_napari/_tests/test_model_ingestor.py @@ -211,9 +211,10 @@ def test_reference_layer(domains_to_test): im_type = "image" spatial_layer_list = [] + rng = np.random.default_rng() for d in domains_to_test.domain_sets: layer_domain = _mi.LayerDomain(d.left_edge, d.right_edge, d.resolution) - im = np.random.random(d.resolution) + im = rng.random(d.resolution) spatial_layer_list.append((im, {}, im_type, layer_domain)) layer_for_ref = spatial_layer_list[0][3] @@ -270,9 +271,10 @@ def test_ref_layer_selection(domains_to_test): im_type = "image" spatial_layer_list = [] + rng = np.random.default_rng() for d in domains_to_test.domain_sets: layer_domain = _mi.LayerDomain(d.left_edge, d.right_edge, d.resolution) - im = np.random.random(d.resolution) + im = rng.random(d.resolution) spatial_layer_list.append((im, {}, im_type, layer_domain)) # add another small volume layer @@ -280,7 +282,7 @@ def test_ref_layer_selection(domains_to_test): re = unyt.unyt_array([0.2, 0.2, 0.2], "m") res = (3, 4, 5) small_vol_domain = _mi.LayerDomain(le, re, resolution=res) - im = np.random.random(res) + im = rng.random(res) spatial_layer_list.append((im, {}, im_type, small_vol_domain)) first_ref = _mi._choose_ref_layer(spatial_layer_list, method="first_in_list") @@ -309,8 +311,8 @@ def test_2d_3d_mix(): layer_2d = _mi.LayerDomain( le, re, res, n_d=2, new_dim_value=unyt.unyt_quantity(1, "km") ) - - sp_layer = (np.random.random(res), {}, "testname", layer_2d) + rng = np.random.default_rng() + sp_layer = (rng.random(res), {}, "testname", layer_2d) new_layer_2d = ref.align_sanitize_layer(sp_layer) assert "scale" not in new_layer_2d[1] # no scale when it is all 1 @@ -392,9 +394,9 @@ def test_timeseries_container(selection_objs): domain = _mi.LayerDomain( unyt.unyt_array([0, 0, 0], "m"), unyt.unyt_array([1.0, 1.0, 1.0], "m"), shp ) - im = np.random.random(shp) + rng = np.random.default_rng() + im = rng.random(shp) - print("what what") for _ in range(3): tc.add(reg_1, ("enzo", "temperature"), (im, im_kwargs, "image", domain)) @@ -408,7 +410,7 @@ def test_timeseries_container(selection_objs): shp, n_d=2, ) - im = np.random.random(shp) + im = rng.random(shp) for _ in range(2): tc.add(slc_1, ("enzo", "temperature"), (im, im_kwargs, "image", domain)) @@ -494,7 +496,8 @@ def test_yt_data_dir_check(tmp_path): def test_linear_rescale(): - data = 10 * np.random.random((5, 5)) + rng = np.random.default_rng() + data = 10 * rng.random((5, 5)) rsc = _mi._linear_rescale(data) assert rsc.min() == 0.0 assert rsc.max() == 1.0 @@ -509,7 +512,7 @@ def test_linear_rescale(): assert np.nanmin(rsc) == 0.0 assert np.nanmax(rsc) == 1.0 - data = 10 * np.random.random((5, 5)) + data = 10 * rng.random((5, 5)) data[1, 1] = np.inf with pytest.warns(RuntimeWarning, match="invalid value"): rsc = _mi._linear_rescale(data, fill_inf=False) diff --git a/src/yt_napari/_tests/test_timeseries.py b/src/yt_napari/_tests/test_timeseries.py index ab4b5e0..993f437 100644 --- a/src/yt_napari/_tests/test_timeseries.py +++ b/src/yt_napari/_tests/test_timeseries.py @@ -14,7 +14,8 @@ def yt_ds_0(): # this fixture generates a random yt dataset saved to disk that can be # re-loaded and sampled. - arr = np.random.random(size=(16, 16, 16)) + rng = np.random.default_rng() + arr = rng.random(size=(16, 16, 16)) d = dict(density=(arr, "g/cm**3"), temperature=(arr, "K")) bbox = np.array([[-1.5, 1.5], [-1.5, 1.5], [-1.5, 1.5]]) shp = arr.shape @@ -179,7 +180,8 @@ def test_validate_scale(selection): assert kwargdict["scale"][0] == 10.0 # check that existing scale is not over-ridden - sc_scale = np.random.random((selection.nd,)) + rng = np.random.default_rng() + sc_scale = rng.random((selection.nd,)) kwargdict = {"scale": sc_scale} ts._validate_scale(selection, kwargdict, False, 1.0) assert np.all(kwargdict["scale"] == sc_scale) diff --git a/src/yt_napari/_tests/test_widget_reader.py b/src/yt_napari/_tests/test_widget_reader.py index 794d18d..51b24c5 100644 --- a/src/yt_napari/_tests/test_widget_reader.py +++ b/src/yt_napari/_tests/test_widget_reader.py @@ -46,7 +46,8 @@ def _rebuild_data(final_shape, data): # dataset will not have the full functionality of a ds. so here, we # inject a correctly shaped random array here. If we start using full # test datasets from yt in testing, this should be changed. - return np.random.random(final_shape) * data.mean() + rng = np.random.default_rng() + return rng.random(final_shape) * data.mean() def test_save_widget_reader(make_napari_viewer, yt_ugrid_ds_fn, tmp_path):