-
Notifications
You must be signed in to change notification settings - Fork 176
Fixing OutOfBounds errors on field.eval (attempt 2) #2681
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
0e2acde
f8a7794
07f6f72
c93133b
a5751ab
fabb42c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,8 +5,11 @@ | |
|
|
||
| from parcels import Field, UxGrid, VectorField, XGrid | ||
| from parcels._core.fieldset import FieldSet | ||
| from parcels._core.warnings import FieldEvalWarning | ||
| from parcels._datasets.structured.generated import simple_UV_dataset | ||
| from parcels._datasets.structured.generic import T as T_structured | ||
| from parcels._datasets.structured.generic import datasets as datasets_structured | ||
| from parcels._datasets.unstructured.generic import _ux_constant_flow_face_centered_2D | ||
| from parcels._datasets.unstructured.generic import datasets as datasets_unstructured | ||
| from parcels.interpolators import ( | ||
| UxConstantFaceConstantZC, | ||
|
|
@@ -270,6 +273,54 @@ def test_field_constant_in_time(): | |
| assert np.isclose(P1, P2) | ||
|
|
||
|
|
||
| def test_field_eval_out_of_bounds_structured(): | ||
| """Test that Field.eval returns IndexError when queried outside the grid boundaries.""" | ||
| ds = simple_UV_dataset(mesh="flat") | ||
| fieldset = FieldSet.from_sgrid_conventions(ds, mesh="flat") | ||
| fieldset.U.data[:] = 1.0 | ||
| fieldset.V.data[:] = 2.0 | ||
|
|
||
| # eval inside bounds should work | ||
| np.testing.assert_allclose(fieldset.U.eval(0.0, 0.0, 0.0, 0.0), 1.0) | ||
| np.testing.assert_allclose(fieldset.UV.eval(0.0, 0.0, 0.0, 0.0), [[1.0], [2.0]]) | ||
|
Comment on lines
+283
to
+285
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see that in future we would (by default) have the test suite fail if any unexpected warnings are printed. This includes FieldEval warnings like this. As such - I think this "eval inside bounds should work" test should be removed as the whole rest of the test suite already tests it. |
||
|
|
||
| # eval outside of bounds should return 0.0 | ||
| with pytest.warns( | ||
| FieldEvalWarning, | ||
| match="Some interpolated values are out-of-bounds. These values are set to 0. Treat carefully.", | ||
| ): | ||
|
Comment on lines
+288
to
+291
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This block only tests that the warning is raised at some point during the evaluations below - not that it's being raised each time. I think we can refactor the following test to something like the following @pytest.fixture(scope="session")
def fieldset_1_2():
ds = simple_UV_dataset(mesh="flat")
fieldset = FieldSet.from_sgrid_conventions(ds, mesh="flat")
fieldset.U.data[:] = 1.0
fieldset.V.data[:] = 2.0
return fieldset
@pytest.mark.parametrize(
"field_name, location, expected",
[
("U", (0.0, 0.0, 0.0, 5e6), 0.0),
("UV", (0.0, 0.0, 0.0, 5e6), [[0.0], [0.0]]),
("U", (0.0, 0.0, 5e6, 0.0), 0.0),
("UV", (0.0, 0.0, 5e6, 0.0), [[0.0], [0.0]]),
("U", (0.0, 5e6, 0.0, 0.0), 0.0),
("UV", (0.0, 5e6, 0.0, 0.0), [[0.0], [0.0]]),
],
)
def test_field_eval_out_of_bounds_structured(fieldset_1_2, field_name, location, expected):
"""Test that Field.eval returns IndexError when queried outside the grid boundaries."""
# eval outside of bounds should return 0.0
field = getattr(fieldset_1_2, field_name)
with pytest.warns(
FieldEvalWarning,
match="Some interpolated values are out-of-bounds. These values are set to 0. Treat carefully.",
):
np.testing.assert_allclose(field.eval(*location), expected) |
||
| np.testing.assert_allclose(fieldset.U.eval(0.0, 0.0, 0.0, 5e6), 0.0) | ||
| np.testing.assert_allclose(fieldset.UV.eval(0.0, 0.0, 0.0, 5e6), [[0.0], [0.0]]) | ||
| np.testing.assert_allclose(fieldset.U.eval(0.0, 0.0, 5e6, 0.0), 0.0) | ||
| np.testing.assert_allclose(fieldset.UV.eval(0.0, 0.0, 5e6, 0.0), [[0.0], [0.0]]) | ||
| np.testing.assert_allclose(fieldset.U.eval(0.0, 5e6, 0.0, 0.0), 0.0) | ||
| np.testing.assert_allclose(fieldset.UV.eval(0.0, 5e6, 0.0, 0.0), [[0.0], [0.0]]) | ||
|
|
||
|
|
||
| def test_field_eval_out_of_bounds_unstructured(): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above |
||
| """Test that Field.eval returns IndexError when queried outside the grid boundaries.""" | ||
| ds = _ux_constant_flow_face_centered_2D() | ||
| fieldset = FieldSet.from_ugrid_conventions(ds, mesh="flat") | ||
| fieldset.U.data[:] = 1.0 | ||
| fieldset.V.data[:] = 2.0 | ||
|
|
||
| # eval inside bounds should work | ||
| np.testing.assert_allclose(fieldset.U.eval(0.0, 0.0, 0.0, 0.0), 1.0) | ||
| np.testing.assert_allclose(fieldset.UV.eval(0.0, 0.0, 0.0, 0.0), [[1.0], [2.0]]) | ||
|
|
||
| # eval outside of bounds should return 0.0 | ||
| with pytest.warns( | ||
| FieldEvalWarning, | ||
| match="Some interpolated values are out-of-bounds. These values are set to 0. Treat carefully.", | ||
| ): | ||
| np.testing.assert_allclose(fieldset.U.eval(0.0, 0.0, 0.0, 5e6), 0.0) | ||
| np.testing.assert_allclose(fieldset.UV.eval(0.0, 0.0, 0.0, 5e6), [[0.0], [0.0]]) | ||
| np.testing.assert_allclose(fieldset.U.eval(0.0, 0.0, 5e6, 0.0), 0.0) | ||
| np.testing.assert_allclose(fieldset.UV.eval(0.0, 0.0, 5e6, 0.0), [[0.0], [0.0]]) | ||
| np.testing.assert_allclose(fieldset.U.eval(0.0, 5e6, 0.0, 0.0), 0.0) | ||
| np.testing.assert_allclose(fieldset.UV.eval(0.0, 5e6, 0.0, 0.0), [[0.0], [0.0]]) | ||
|
|
||
|
|
||
|
fluidnumericsJoe marked this conversation as resolved.
|
||
| def test_field_unstructured_grid_creation(): ... | ||
|
|
||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I wonder whether we should have a
parcels.warningsnamespace.I'm not strongly for or against - just a convention some packages adopt.
Similarly for
parcels.errors.