Skip to content

Commit

Permalink
TST: introduce testing for ND linear and rbf interpolations.
Browse files Browse the repository at this point in the history
  • Loading branch information
phmbressan committed Sep 14, 2024
1 parent 2b44723 commit 66e9b8e
Showing 1 changed file with 104 additions and 20 deletions.
124 changes: 104 additions & 20 deletions tests/unit/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,110 @@ def test_3d_shepard_interpolation(x, y, z, w_expected):
assert np.isclose(w_expected, w, atol=1e-8).all()


@pytest.mark.parametrize(
"x,y,z_expected",
[
(1, 0, 0),
(0, 1, 0),
(0, 0, 1),
(0.5, 0.5, 0),
(0.25, 0.25, 0.5),
([0, 0.5], [0, 0.5], [1, 0]),
],
)
def test_2d_rbf_interpolation(x, y, z_expected):
"""Test the rbf interpolation method of the Function class."""
# Test plane x + y + z = 1
source = [(1, 0, 0), (0, 1, 0), (0, 0, 1)]
func = Function(
source=source, inputs=["x", "y"], outputs=["z"], interpolation="rbf"
)
z = func(x, y)
z_opt = func.get_value_opt(x, y)
assert np.isclose(z, z_opt, atol=1e-8).all()
assert np.isclose(z_expected, z, atol=1e-8).all()


@pytest.mark.parametrize(
"x,y,z,w_expected",
[
(0, 0, 0, 1),
(1, 0, 0, 0),
(0, 1, 0, 0),
(0, 0, 1, 0),
(0.5, 0.5, 0.5, -0.5),
(0.25, 0.25, 0.25, 0.25),
([0, 0.5], [0, 0.5], [0, 0.5], [1, -0.5]),
],
)
def test_3d_rbf_interpolation(x, y, z, w_expected):
"""Test the rbf interpolation method of the Function class."""
# Test plane x + y + z + w = 1
source = [(1, 0, 0, 0), (0, 1, 0, 0), (0, 0, 1, 0), (0, 0, 0, 1)]
func = Function(
source=source, inputs=["x", "y", "z"], outputs=["w"], interpolation="rbf"
)
w = func(x, y, z)
w_opt = func.get_value_opt(x, y, z)
assert np.isclose(w, w_opt, atol=1e-8).all()
assert np.isclose(w_expected, w, atol=1e-8).all()


@pytest.mark.parametrize(
"x,y,z_expected",
[
(1, 0, 0),
(0, 1, 0),
(0, 0, 1),
(0.5, 0.5, 0),
(0.25, 0.25, 0.5),
([0, 0.5], [0, 0.5], [1, 0]),
],
)
def test_2d_linear_interpolation(x, y, z_expected):
"""Test the linear interpolation method of the Function class."""
# Test plane x + y + z = 1
source = [(1, 0, 0), (0, 1, 0), (0, 0, 1)]
func = Function(
source=source, inputs=["x", "y"], outputs=["z"], interpolation="linear"
)
z = func(x, y)
z_opt = func.get_value_opt(x, y)
assert np.isclose(z, z_opt, atol=1e-8).all()
assert np.isclose(z_expected, z, atol=1e-8).all()


@pytest.mark.parametrize(
"x,y,z,w_expected",
[
(1, 0, 0, 0),
(0, 1, 0, 0),
(0, 0, 1, 0),
(0, 0, 0, 1),
(0.5, 0.5, 0.5, -0.5),
(0.25, 0.25, 0.25, 0.25),
([0, 0.25], [0, 0.25], [0, 0.25], [1, 0.25]),
],
)
def test_3d_linear_interpolation(x, y, z, w_expected):
"""Test the linear interpolation method of the Function class."""
# Test plane x + y + z + w = 1
source = [
(1, 0, 0, 0),
(0, 1, 0, 0),
(0, 0, 1, 0),
(0, 0, 0, 1),
(0.5, 0.5, 0.5, -0.5),
]
func = Function(
source=source, inputs=["x", "y", "z"], outputs=["w"], interpolation="linear"
)
w = func(x, y, z)
w_opt = func.get_value_opt(x, y, z)
assert np.isclose(w, w_opt, atol=1e-8).all()
assert np.isclose(w_expected, w, atol=1e-8).all()


@pytest.mark.parametrize("a", [-1, -0.5, 0, 0.5, 1])
@pytest.mark.parametrize("b", [-1, -0.5, 0, 0.5, 1])
def test_multivariate_function(a, b):
Expand Down Expand Up @@ -565,26 +669,6 @@ def test_set_discrete_based_on_2d_model(func_2d_from_csv):
assert discretized_func.__extrapolation__ == func_2d_from_csv.__extrapolation__


@pytest.mark.parametrize(
"x,y,z_expected",
[
(1, 0, 0),
(0, 1, 0),
(0, 0, 1),
(0.5, 0.5, 1 / 3),
(0.25, 0.25, 25 / (25 + 2 * 5**0.5)),
([0, 0.5], [0, 0.5], [1, 1 / 3]),
],
)
def test_shepard_interpolation(x, y, z_expected):
"""Test the shepard interpolation method of the Function class."""
# Test plane x + y + z = 1
source = [(1, 0, 0), (0, 1, 0), (0, 0, 1)]
func = Function(source=source, inputs=["x", "y"], outputs=["z"])
z = func(x, y)
assert np.isclose(z, z_expected, atol=1e-8).all()


@pytest.mark.parametrize("other", [1, 0.1, np.int_(1), np.float64(0.1), np.array([1])])
def test_sum_arithmetic_priority(other):
"""Test the arithmetic priority of the add operation of the Function class,
Expand Down

0 comments on commit 66e9b8e

Please sign in to comment.