Skip to content
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

Implementing Property Based Tesing using Hypothesis #4724

Open
wants to merge 27 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
1c87e48
Added hypothesis test to two unit testing
RohitP2005 Dec 30, 2024
06ec7f8
hypothesis added in dependencies
RohitP2005 Dec 31, 2024
603dfc0
Update tests/unit/test_expression_tree/test_averages.py
RohitP2005 Dec 31, 2024
7fc931a
Update tests/unit/test_expression_tree/test_averages.py
RohitP2005 Dec 31, 2024
e0e4b81
Update tests/unit/test_expression_tree/test_averages.py
RohitP2005 Dec 31, 2024
f959fe6
Update tests/unit/test_expression_tree/test_averages.py
RohitP2005 Dec 31, 2024
6a894d8
Update tests/unit/test_expression_tree/test_averages.py
RohitP2005 Dec 31, 2024
cc79028
Update tests/unit/test_expression_tree/test_averages.py
RohitP2005 Dec 31, 2024
06b8981
Update tests/unit/test_expression_tree/test_averages.py
RohitP2005 Dec 31, 2024
46b713e
Update tests/unit/test_expression_tree/test_averages.py
RohitP2005 Dec 31, 2024
e33d3b4
Update tests/unit/test_expression_tree/test_averages.py
RohitP2005 Dec 31, 2024
5526029
Update tests/unit/test_expression_tree/test_averages.py
RohitP2005 Dec 31, 2024
007045e
Updated test_average.py
RohitP2005 Dec 31, 2024
aa68741
Update .gitignore
RohitP2005 Jan 8, 2025
17a716a
Update pyproject.toml
RohitP2005 Jan 8, 2025
e8b0571
Update tests/unit/test_expression_tree/test_unary_operators.py
RohitP2005 Jan 8, 2025
fdd61ae
Update tests/unit/test_expression_tree/test_unary_operators.py
RohitP2005 Jan 8, 2025
d59e382
Update tests/unit/test_expression_tree/test_unary_operators.py
RohitP2005 Jan 8, 2025
182e6a9
Update tests/unit/test_expression_tree/test_unary_operators.py
RohitP2005 Jan 8, 2025
465e0b9
Update tests/unit/test_expression_tree/test_unary_operators.py
RohitP2005 Jan 8, 2025
44c6fd4
Update tests/unit/test_expression_tree/test_unary_operators.py
RohitP2005 Jan 8, 2025
393b67a
Merge branch 'develop' into issue#4703
RohitP2005 Jan 8, 2025
750d812
Merge branch 'pybamm-team:develop' into issue#4703
RohitP2005 Jan 10, 2025
f5ff0b3
Merge branch 'develop' into issue#4703
kratman Jan 10, 2025
bee6556
Added missing dependecies hypothesis
RohitP2005 Jan 11, 2025
80d4e9b
Merge branch 'pybamm-team:develop' into issue#4703
RohitP2005 Feb 3, 2025
aac7338
increased the floatsize for test_sign
RohitP2005 Feb 3, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions tests/unit/test_expression_tree/test_averages.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,32 @@
import numpy as np
import pybamm
from tests import assert_domain_equal
from hypothesis import strategies as st
from hypothesis import given


class TestUnaryOperators:
def test_x_average(self):
a = pybamm.Scalar(4)
@given(st.integers())
def test_x_average(self, random_value):
RohitP2005 marked this conversation as resolved.
Show resolved Hide resolved
a = pybamm.Scalar(random_value)
RohitP2005 marked this conversation as resolved.
Show resolved Hide resolved
average_a = pybamm.x_average(a)
assert average_a == a

# average of a broadcast is the child
average_broad_a = pybamm.x_average(
pybamm.PrimaryBroadcast(a, ["negative electrode"])
)
assert average_broad_a == pybamm.Scalar(4)
assert average_broad_a == pybamm.Scalar(random_value)
RohitP2005 marked this conversation as resolved.
Show resolved Hide resolved

# average of a number times a broadcast is the number times the child
average_two_broad_a = pybamm.x_average(
2 * pybamm.PrimaryBroadcast(a, ["negative electrode"])
)
assert average_two_broad_a == pybamm.Scalar(8)
assert average_two_broad_a == pybamm.Scalar(2 * random_value)
RohitP2005 marked this conversation as resolved.
Show resolved Hide resolved
average_t_broad_a = pybamm.x_average(
pybamm.t * pybamm.PrimaryBroadcast(a, ["negative electrode"])
)
assert average_t_broad_a == (pybamm.t * pybamm.Scalar(4))
assert average_t_broad_a == (pybamm.t * random_value)
RohitP2005 marked this conversation as resolved.
Show resolved Hide resolved

# full broadcasts
average_broad_a = pybamm.x_average(
Expand Down Expand Up @@ -174,9 +177,10 @@ def test_x_average(self):
assert pybamm.x_average(a + b) == pybamm.x_average(a) + pybamm.x_average(b)
assert pybamm.x_average(a - b) == pybamm.x_average(a) - pybamm.x_average(b)

def test_size_average(self):
@given(st.integers(min_value=-(2**63), max_value=2**63 - 1))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to provide such wide upper and lower bounds(size of int itself)? Can we not just use st.integers()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

they are used to made sure that the random values generated are strictly a signed 64-bit integer .

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Having large-sized integers causes buffer overflow (probably because of different ways int is handled by pybamm and numpy ). Yes it's good to restrict the range here.

def test_size_average(self, random_value):
RohitP2005 marked this conversation as resolved.
Show resolved Hide resolved
# no domain
a = pybamm.Scalar(1)
a = pybamm.Scalar(random_value)
RohitP2005 marked this conversation as resolved.
Show resolved Hide resolved
average_a = pybamm.size_average(a)
assert average_a == a

Expand All @@ -193,7 +197,7 @@ def test_size_average(self):
average_a = pybamm.size_average(
pybamm.PrimaryBroadcast(a, "negative particle size")
)
assert average_a.evaluate() == np.array([1])
assert average_a.evaluate() == np.array([random_value])
RohitP2005 marked this conversation as resolved.
Show resolved Hide resolved

a = pybamm.Symbol("a", domain="negative particle")
average_a = pybamm.size_average(
Expand Down Expand Up @@ -267,8 +271,9 @@ def test_r_average(self):
assert pybamm.r_average(a + b) == pybamm.r_average(a) + pybamm.r_average(b)
assert pybamm.r_average(a - b) == pybamm.r_average(a) - pybamm.r_average(b)

def test_yz_average(self):
a = pybamm.Scalar(1)
@given(st.integers(min_value=-(2**63), max_value=2**63 - 1))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

explained here at #4724 (comment)

def test_yz_average(self, random_value):
a = pybamm.Scalar(random_value)
z_average_a = pybamm.z_average(a)
yz_average_a = pybamm.yz_average(a)
assert z_average_a == a
Expand All @@ -280,8 +285,8 @@ def test_yz_average(self):
yz_average_broad_a = pybamm.yz_average(
pybamm.PrimaryBroadcast(a, ["current collector"])
)
assert z_average_broad_a.evaluate() == np.array([1])
assert yz_average_broad_a.evaluate() == np.array([1])
assert z_average_broad_a.evaluate() == np.array([random_value])
RohitP2005 marked this conversation as resolved.
Show resolved Hide resolved
assert yz_average_broad_a.evaluate() == np.array([random_value])
RohitP2005 marked this conversation as resolved.
Show resolved Hide resolved

a = pybamm.Variable("a", domain=["current collector"])
y = pybamm.SpatialVariable("y", ["current collector"])
Expand Down
80 changes: 52 additions & 28 deletions tests/unit/test_expression_tree/test_unary_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from sympy.vector.operators import Divergence as sympy_Divergence
from sympy.vector.operators import Gradient as sympy_Gradient
from tests import assert_domain_equal
from hypothesis import strategies as st
from hypothesis import given, settings, HealthCheck

import pybamm

Expand All @@ -25,15 +27,17 @@ def test_unary_operator(self):
absval = pybamm.AbsoluteValue(-a)
assert absval.evaluate(inputs={"a": 10}) == 10

def test_negation(self, mocker):
@settings(suppress_health_check=[HealthCheck.function_scoped_fixture])
@given(random_value=st.floats(min_value=-1e3, max_value=1e3))
RohitP2005 marked this conversation as resolved.
Show resolved Hide resolved
def test_negation(self, mocker, random_value):
a = pybamm.Symbol("a")
nega = pybamm.Negate(a)
assert nega.name == "-"
assert nega.children[0].name == a.name

b = pybamm.Scalar(4)
b = pybamm.Scalar(random_value)
negb = pybamm.Negate(b)
assert negb.evaluate() == -4
assert negb.evaluate() == -random_value

# Test broadcast gets switched
broad_a = pybamm.PrimaryBroadcast(a, "test")
Expand Down Expand Up @@ -65,15 +69,17 @@ def test_negation(self, mocker):
}
assert pybamm.Negate._from_json(input_json) == nega

def test_absolute(self, mocker):
@settings(suppress_health_check=[HealthCheck.function_scoped_fixture])
@given(random_value=st.floats(min_value=-1e3, max_value=1e3))
RohitP2005 marked this conversation as resolved.
Show resolved Hide resolved
def test_absolute(self, mocker, random_value):
a = pybamm.Symbol("a")
absa = pybamm.AbsoluteValue(a)
assert absa.name == "abs"
assert absa.children[0].name == a.name

b = pybamm.Scalar(-4)
b = pybamm.Scalar(-random_value)
RohitP2005 marked this conversation as resolved.
Show resolved Hide resolved
absb = pybamm.AbsoluteValue(b)
assert absb.evaluate() == 4
assert absb.evaluate() == abs(random_value)
RohitP2005 marked this conversation as resolved.
Show resolved Hide resolved

# Test broadcast gets switched
broad_a = pybamm.PrimaryBroadcast(a, "test")
Expand Down Expand Up @@ -116,17 +122,23 @@ def test_smooth_absolute_value(self):
"/ (exp(10.0 * y[0:1]) + exp(-10.0 * y[0:1]))"
)

def test_sign(self):
b = pybamm.Scalar(-4)
@given(
random_value=st.integers(),
random_matrix=st.lists(
st.floats(min_value=-10, max_value=10), min_size=5, max_size=5
),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we increase the size here? i'll look into this bit later

Copy link
Contributor Author

@RohitP2005 RohitP2005 Jan 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay , i guess 10 will be good a number

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey @prady0t shall i go with min_size=10 also i thought of creating a new PR by closing this one, cause it sflooded with too much of commits

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this PR is fine; it preserves the review comments in one place. Once the PR and the reviews are completed, please feel free to squash unnecessary commits as an exercise, or we can do it as we squash-merge PRs anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @agriyakhetarpal i will look into the float size and fix those failing CI tests and notify you.

)
def test_sign(self, random_value, random_matrix):
b = pybamm.Scalar(random_value)
signb = pybamm.sign(b)
assert signb.evaluate() == -1
assert signb.evaluate() == -1 if random_value < 0 else 1

A = diags(np.linspace(-1, 1, 5))
A = diags(random_matrix)
b = pybamm.Matrix(A)
signb = pybamm.sign(b)
np.testing.assert_array_equal(
np.diag(signb.evaluate().toarray()), [-1, -1, 0, 1, 1]
)
evaluated_sign = signb.evaluate().toarray()
expected_sign = np.sign(np.diag(A.toarray()))
np.testing.assert_array_equal(np.diag(evaluated_sign), expected_sign)

broad = pybamm.PrimaryBroadcast(-4, "test domain")
assert pybamm.sign(broad) == pybamm.PrimaryBroadcast(-1, "test domain")
Expand All @@ -138,7 +150,7 @@ def test_sign(self):
)

# Test from_json
c = pybamm.Multiplication(pybamm.Variable("a"), pybamm.Scalar(3))
c = pybamm.Multiplication(pybamm.Variable("a"), pybamm.Scalar(random_value))
sign_json = {
"name": "sign",
"id": 5341515228900508018,
Expand All @@ -153,19 +165,24 @@ def test_sign(self):

assert pybamm.sign(c) == pybamm.Sign._from_json(sign_json)

def test_floor(self, mocker):
@settings(suppress_health_check=[HealthCheck.function_scoped_fixture])
@given(
random_value1=st.floats(min_value=-1e3, max_value=1e3),
random_value2=st.floats(min_value=-1e3, max_value=1e3),
)
RohitP2005 marked this conversation as resolved.
Show resolved Hide resolved
def test_floor(self, mocker, random_value1, random_value2):
a = pybamm.Symbol("a")
floora = pybamm.Floor(a)
assert floora.name == "floor"
assert floora.children[0].name == a.name

b = pybamm.Scalar(3.5)
b = pybamm.Scalar(random_value1)
floorb = pybamm.Floor(b)
assert floorb.evaluate() == 3
assert floorb.evaluate() == np.floor(random_value1)

c = pybamm.Scalar(-3.2)
c = pybamm.Scalar(random_value2)
floorc = pybamm.Floor(c)
assert floorc.evaluate() == -4
assert floorc.evaluate() == np.floor(random_value2)

# Test from_json
input_json = {
Expand All @@ -181,19 +198,24 @@ def test_floor(self, mocker):
}
assert pybamm.Floor._from_json(input_json) == floora

def test_ceiling(self, mocker):
@settings(suppress_health_check=[HealthCheck.function_scoped_fixture])
@given(
random_value1=st.floats(min_value=-1e3, max_value=1e3),
random_value2=st.floats(min_value=-1e3, max_value=1e3),
RohitP2005 marked this conversation as resolved.
Show resolved Hide resolved
)
def test_ceiling(self, mocker, random_value1, random_value2):
a = pybamm.Symbol("a")
ceila = pybamm.Ceiling(a)
assert ceila.name == "ceil"
assert ceila.children[0].name == a.name

b = pybamm.Scalar(3.5)
b = pybamm.Scalar(random_value1)
ceilb = pybamm.Ceiling(b)
assert ceilb.evaluate() == 4
assert ceilb.evaluate() == np.ceil(random_value1)

c = pybamm.Scalar(-3.2)
c = pybamm.Scalar(random_value2)
ceilc = pybamm.Ceiling(c)
assert ceilc.evaluate() == -3
assert ceilc.evaluate() == np.ceil(random_value2)

# Test from_json
input_json = {
Expand All @@ -209,7 +231,8 @@ def test_ceiling(self, mocker):
}
assert pybamm.Ceiling._from_json(input_json) == ceila

def test_gradient(self):
@given(st.integers())
def test_gradient(self, random_value):
# gradient of scalar symbol should fail
a = pybamm.Symbol("a")
with pytest.raises(
Expand All @@ -219,7 +242,7 @@ def test_gradient(self):
pybamm.Gradient(a)

# gradient of variable evaluating on edges should fail
a = pybamm.PrimaryBroadcastToEdges(pybamm.Scalar(1), "test")
a = pybamm.PrimaryBroadcastToEdges(pybamm.Scalar(random_value), "test")
with pytest.raises(TypeError, match="evaluates on edges"):
pybamm.Gradient(a)

Expand All @@ -240,7 +263,8 @@ def test_gradient(self):
assert grad.children[0].name == a.name
assert grad.domain == a.domain

def test_div(self):
@given(st.integers())
def test_div(self, random_value):
# divergence of scalar symbol should fail
a = pybamm.Symbol("a")
with pytest.raises(
Expand All @@ -250,7 +274,7 @@ def test_div(self):
pybamm.Divergence(a)

# divergence of variable evaluating on edges should fail
a = pybamm.PrimaryBroadcast(pybamm.Scalar(1), "test")
a = pybamm.PrimaryBroadcast(pybamm.Scalar(random_value), "test")
with pytest.raises(TypeError, match="evaluate on edges"):
pybamm.Divergence(a)

Expand Down
Loading