Skip to content

Commit

Permalink
replace np.random.random with new Generator api
Browse files Browse the repository at this point in the history
  • Loading branch information
chrishavlin committed Apr 2, 2024
1 parent 03850ad commit 1d9d0c0
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 15 deletions.
3 changes: 2 additions & 1 deletion src/yt_napari/_special_loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/yt_napari/_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 13 additions & 10 deletions src/yt_napari/_tests/test_model_ingestor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -270,17 +271,18 @@ 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
le = unyt.unyt_array([0.1, 0.1, 0.1], "m")
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")
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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))

Expand All @@ -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))
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions src/yt_napari/_tests/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion src/yt_napari/_tests/test_widget_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 1d9d0c0

Please sign in to comment.