Skip to content

Commit

Permalink
Control seed of random number generator in test
Browse files Browse the repository at this point in the history
  • Loading branch information
david-zwicker committed Sep 23, 2023
1 parent 9c628e4 commit 6504a79
Show file tree
Hide file tree
Showing 37 changed files with 468 additions and 459 deletions.
5 changes: 3 additions & 2 deletions tests/fields/fixtures/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def iter_grids():

def get_cartesian_grid(dim=2, periodic=True):
"""return a random Cartesian grid of given dimension"""
bounds = [[0, 1 + np.random.random()] for _ in range(dim)]
shape = np.random.randint(32, 64, size=dim)
rng = np.random.default_rng(0)
bounds = [[0, 1 + rng.random()] for _ in range(dim)]
shape = rng.integers(32, 64, size=dim)
return CartesianGrid(bounds, shape, periodic=periodic)
38 changes: 19 additions & 19 deletions tests/fields/test_field_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@


@pytest.mark.parametrize("grid", iter_grids())
def test_shapes_nfields(grid):
def test_shapes_nfields(grid, rng):
"""test single component field"""
for num in [1, 3]:
fields = [ScalarField.random_uniform(grid) for _ in range(num)]
fields = [ScalarField.random_uniform(grid, rng=rng) for _ in range(num)]
field = FieldCollection(fields)
data_shape = (num,) + grid.shape
np.testing.assert_equal(field.data.shape, data_shape)
Expand All @@ -27,12 +27,12 @@ def test_shapes_nfields(grid):
assert field.grid == field_c.grid


def test_collections():
def test_collections(rng):
"""test field collections"""
grid = UnitGrid([3, 4])
sf = ScalarField.random_uniform(grid, label="sf")
vf = VectorField.random_uniform(grid, label="vf")
tf = Tensor2Field.random_uniform(grid, label="tf")
sf = ScalarField.random_uniform(grid, label="sf", rng=rng)
vf = VectorField.random_uniform(grid, label="vf", rng=rng)
tf = Tensor2Field.random_uniform(grid, label="tf", rng=rng)
fields = FieldCollection([sf, vf, tf])
assert fields.data.shape == (7, 3, 4)
assert isinstance(str(fields), str)
Expand Down Expand Up @@ -176,14 +176,14 @@ def test_collections_operators():
np.testing.assert_allclose(fields.data, 4)


def test_smoothing_collection():
def test_smoothing_collection(rng):
"""test smoothing of a FieldCollection"""
grid = UnitGrid([3, 4], periodic=[True, False])
sf = ScalarField.random_uniform(grid)
vf = VectorField.random_uniform(grid)
tf = Tensor2Field.random_uniform(grid)
sf = ScalarField.random_uniform(grid, rng=rng)
vf = VectorField.random_uniform(grid, rng=rng)
tf = Tensor2Field.random_uniform(grid, rng=rng)
fields = FieldCollection([sf, vf, tf])
sgm = 0.5 + np.random.random()
sgm = 0.5 + rng.random()

out = fields.smooth(sigma=sgm)
for i in range(3):
Expand Down Expand Up @@ -225,11 +225,11 @@ def test_from_scalar_expressions():

@skipUnlessModule("napari")
@pytest.mark.interactive
def test_interactive_collection_plotting():
def test_interactive_collection_plotting(rng):
"""test the interactive plotting"""
grid = UnitGrid([3, 3])
sf = ScalarField.random_uniform(grid, 0.1, 0.9)
vf = VectorField.random_uniform(grid, 0.1, 0.9)
sf = ScalarField.random_uniform(grid, 0.1, 0.9, rng=rng)
vf = VectorField.random_uniform(grid, 0.1, 0.9, rng=rng)
field = FieldCollection([sf, vf])
field.plot_interactive(viewer_args={"show": False, "close": True})

Expand Down Expand Up @@ -306,11 +306,11 @@ def test_collection_plotting():
fc.plot(arrangement="vertical")


def test_from_data():
def test_from_data(rng):
"""test the `from_data` method"""
grid = UnitGrid([3, 5])
s = ScalarField.random_uniform(grid, label="s1")
v = VectorField.random_uniform(grid, label="v2")
s = ScalarField.random_uniform(grid, label="s1", rng=rng)
v = VectorField.random_uniform(grid, label="v2", rng=rng)
f1 = FieldCollection([s, v])

f2 = FieldCollection.from_data(
Expand All @@ -334,11 +334,11 @@ def test_from_data():
np.testing.assert_allclose(f1.data, f2.data)


def test_collection_apply():
def test_collection_apply(rng):
"""test the `apply` method"""
grid = UnitGrid([3, 5])
s = ScalarField(grid, 2, label="s1")
v = VectorField.random_uniform(grid, label="v2")
v = VectorField.random_uniform(grid, label="v2", rng=rng)
f1 = FieldCollection([s, v])

np.testing.assert_allclose(f1.apply("s1 * v2").data, v.data * 2)
Loading

0 comments on commit 6504a79

Please sign in to comment.