-
-
Notifications
You must be signed in to change notification settings - Fork 154
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
Convert a test to use Hypothesis #1507
Open
etellman
wants to merge
3
commits into
aesara-devs:main
Choose a base branch
from
etellman:hypothesis
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.hypothesis | ||
*.pkl | ||
_build | ||
__pycache__ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
from sys import float_info | ||
|
||
import hypothesis.strategies as st | ||
import numpy as np | ||
import pytest | ||
from hypothesis import given, settings | ||
|
||
import aesara | ||
import tests.unittest_tools as utt | ||
from aesara.tensor.type import dscalar, zscalar | ||
|
||
|
||
pytestmark = pytest.mark.filterwarnings("error") | ||
|
||
# functions that accept either real or complex inputs | ||
# the second value in each tupple converts an arbitrary float value into a | ||
# value in the function's domain. | ||
COMPLEX_FUNCTIONS = [ | ||
(np.tanh, np.log), | ||
(np.cosh, np.log), | ||
(np.sinh, np.log), | ||
(np.arcsinh, lambda x: x), | ||
(np.log, lambda x: x), | ||
(np.log10, lambda x: x), | ||
(np.log1p, lambda x: x / 2), | ||
(np.log2, lambda x: x), | ||
(np.exp, np.log), | ||
(np.expm1, np.log), | ||
(np.exp2, lambda x: np.log2(x) - 1), | ||
(np.sqrt, abs), | ||
] | ||
|
||
# functions that only accept real inputs | ||
# the second value in each tupple converts an arbitrary float value into a | ||
# value in the function's domain | ||
REAL_FUNCTIONS = [ | ||
(np.deg2rad, lambda x: x), | ||
(np.rad2deg, np.deg2rad), | ||
(np.cos, lambda x: x), | ||
(np.sin, lambda x: x), | ||
(np.tan, lambda x: x), | ||
(np.arctan, lambda x: x), | ||
(np.arcsin, lambda x: x / float_info.max), | ||
(np.arccos, lambda x: x / float_info.max), | ||
] | ||
|
||
|
||
# tests calling a function with a real value | ||
def do_real_test(fct, value: float): | ||
# set up | ||
x = dscalar("x") | ||
y = fct(x) | ||
f = aesara.function([x], y) | ||
|
||
# exercise and verify | ||
utt.assert_allclose(fct(value), f(value)) | ||
|
||
|
||
# tests functions that can be invoked with either real or imaginary inputs | ||
# with real inputs | ||
@pytest.mark.parametrize( | ||
"fct, to_domain", | ||
REAL_FUNCTIONS + COMPLEX_FUNCTIONS, | ||
) | ||
@given(value=st.floats(float_info.min, float_info.max)) | ||
@settings(deadline=None) | ||
def test_real(fct, to_domain, value): | ||
do_real_test(fct, to_domain(value)) | ||
|
||
|
||
# arccosh has a domain that is awkward to derive from min/max float | ||
@given(value=st.floats(1, float_info.max)) | ||
@settings(deadline=None) | ||
def test_arccosh(value): | ||
do_real_test(np.arccosh, value) | ||
|
||
|
||
# arctanh has a domain that is awkward to derive from min/max float | ||
@given(value=st.floats(-1 + float_info.epsilon, 1 - float_info.epsilon)) | ||
@settings(deadline=None) | ||
def test_arctanh_real(value): | ||
do_real_test(np.arctanh, value) | ||
|
||
|
||
# tests functions with complex inputs | ||
# cscalar doesn't work for because it loses precision | ||
@pytest.mark.parametrize("fct, to_domain", COMPLEX_FUNCTIONS) | ||
@given( | ||
real_value=st.floats(float_info.min, float_info.max), | ||
imaginary_value=st.floats(float_info.min, float_info.max), | ||
) | ||
@settings(deadline=None) | ||
def test_complex(fct, to_domain, real_value, imaginary_value): | ||
# set up | ||
x = zscalar("x") | ||
y = fct(x) | ||
f = aesara.function([x], y) | ||
value = to_domain(real_value) + 1j * to_domain(imaginary_value) | ||
|
||
# exercise and verify | ||
utt.assert_allclose(fct(value), f(value)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
You might need to add this to the test set up script (i.e. here).
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.
I added it. I'm not sure how to test it, as I'm a little fuzzy on what this script is used for, but I guess we'll find out when something happens on GitHub. :^)