From 7946692c093b7f4d2dc021bc8d0b1c9df1908fe5 Mon Sep 17 00:00:00 2001 From: Martin Fitzner Date: Wed, 29 May 2024 13:28:23 +0200 Subject: [PATCH] Make hypothesis respect floating point precision --- tests/hypothesis_strategies/acquisition.py | 10 ++++------ .../alternative_creation/test_searchspace.py | 2 +- tests/hypothesis_strategies/basic.py | 10 +++++++++- tests/hypothesis_strategies/constraints.py | 10 +++++----- tests/hypothesis_strategies/dataframes.py | 4 +++- tests/hypothesis_strategies/objectives.py | 3 ++- tests/hypothesis_strategies/parameters.py | 12 +++++------- 7 files changed, 29 insertions(+), 22 deletions(-) diff --git a/tests/hypothesis_strategies/acquisition.py b/tests/hypothesis_strategies/acquisition.py index 78322e6435..3c22b8964b 100644 --- a/tests/hypothesis_strategies/acquisition.py +++ b/tests/hypothesis_strategies/acquisition.py @@ -17,20 +17,18 @@ qUpperConfidenceBound, ) +from ..hypothesis_strategies.basic import finite_floats + # These acqfs are ordered roughly according to increasing complexity acquisition_functions = st.one_of( st.builds(ExpectedImprovement), st.builds(ProbabilityOfImprovement), - st.builds( - UpperConfidenceBound, beta=st.floats(min_value=0.0, allow_infinity=False) - ), + st.builds(UpperConfidenceBound, beta=finite_floats(min_value=0.0)), st.builds(PosteriorMean), st.builds(LogExpectedImprovement), st.builds(qExpectedImprovement), st.builds(qProbabilityOfImprovement), - st.builds( - qUpperConfidenceBound, beta=st.floats(min_value=0.0, allow_infinity=False) - ), + st.builds(qUpperConfidenceBound, beta=finite_floats(min_value=0.0)), st.builds(qSimpleRegret), st.builds(qLogExpectedImprovement), st.builds(qNoisyExpectedImprovement), diff --git a/tests/hypothesis_strategies/alternative_creation/test_searchspace.py b/tests/hypothesis_strategies/alternative_creation/test_searchspace.py index a0f6ab606b..3aeb5bb2c1 100644 --- a/tests/hypothesis_strategies/alternative_creation/test_searchspace.py +++ b/tests/hypothesis_strategies/alternative_creation/test_searchspace.py @@ -115,7 +115,7 @@ def test_searchspace_creation_from_dataframe(df, parameters, expected): ) def test_discrete_space_creation_from_simplex_inner(parameters, boundary_only): """Candidates from a simplex space satisfy the simplex constraint.""" - tolerance = 1e-6 + tolerance = 1e-2 max_possible = sum(max(p.values) for p in parameters) min_possible = sum(min(p.values) for p in parameters) diff --git a/tests/hypothesis_strategies/basic.py b/tests/hypothesis_strategies/basic.py index 3df4bbfd2d..ca8ed95c2c 100644 --- a/tests/hypothesis_strategies/basic.py +++ b/tests/hypothesis_strategies/basic.py @@ -3,6 +3,14 @@ from functools import partial import hypothesis.strategies as st +import numpy as np -finite_floats = partial(st.floats, allow_infinity=False, allow_nan=False) +from baybe.utils.numerical import DTypeFloatNumpy + +finite_floats = partial( + st.floats, + allow_infinity=False, + allow_nan=False, + width=32 if DTypeFloatNumpy == np.float32 else 64, +) """A strategy producing finite (i.e., non-nan and non-infinite) floats.""" diff --git a/tests/hypothesis_strategies/constraints.py b/tests/hypothesis_strategies/constraints.py index d286763f90..18a42cd77d 100644 --- a/tests/hypothesis_strategies/constraints.py +++ b/tests/hypothesis_strategies/constraints.py @@ -27,6 +27,8 @@ from baybe.parameters.base import DiscreteParameter from baybe.parameters.numerical import NumericalDiscreteParameter +from ..hypothesis_strategies.basic import finite_floats + def sub_selection_conditions(superset: Optional[list[Any]] = None): """Generate :class:`baybe.constraints.conditions.SubSelectionCondition`.""" @@ -39,9 +41,7 @@ def sub_selection_conditions(superset: Optional[list[Any]] = None): def threshold_conditions(): """Generate :class:`baybe.constraints.conditions.ThresholdCondition`.""" - return st.builds( - ThresholdCondition, threshold=st.floats(allow_infinity=False, allow_nan=False) - ) + return st.builds(ThresholdCondition, threshold=finite_floats()) @st.composite @@ -232,12 +232,12 @@ def _continuous_linear_constraints( coefficients = draw( st.lists( - st.floats(allow_nan=False), + finite_floats(), min_size=len(parameter_names), max_size=len(parameter_names), ) ) - rhs = draw(st.floats(allow_nan=False)) + rhs = draw(finite_floats()) return constraint_type(parameter_names, coefficients, rhs) diff --git a/tests/hypothesis_strategies/dataframes.py b/tests/hypothesis_strategies/dataframes.py index 100f67a621..66999003e9 100644 --- a/tests/hypothesis_strategies/dataframes.py +++ b/tests/hypothesis_strategies/dataframes.py @@ -3,11 +3,13 @@ from hypothesis import strategies as st from hypothesis.extra.pandas import column, data_frames, indexes +from ..hypothesis_strategies.basic import finite_floats + @st.composite def random_dataframes(draw: st.DrawFn): """Generate pandas dataframes of random shape and content.""" - index_elements = st.one_of(st.text(), st.integers(), st.floats()) + index_elements = st.one_of(st.text(), st.integers(), finite_floats()) cols = st.builds( column, name=index_elements, dtype=st.sampled_from([int, float, str]) ) diff --git a/tests/hypothesis_strategies/objectives.py b/tests/hypothesis_strategies/objectives.py index f0c1e5e745..94dc034a14 100644 --- a/tests/hypothesis_strategies/objectives.py +++ b/tests/hypothesis_strategies/objectives.py @@ -6,6 +6,7 @@ from baybe.objectives.enum import Scalarizer from baybe.objectives.single import SingleTargetObjective +from ..hypothesis_strategies.basic import finite_floats from ..hypothesis_strategies.targets import numerical_targets from ..hypothesis_strategies.utils import intervals as st_intervals @@ -24,7 +25,7 @@ def desirability_objectives(draw: st.DrawFn): ) weights = draw( st.lists( - st.floats(min_value=0.0, exclude_min=True), + finite_floats(min_value=0.0, exclude_min=True), min_size=len(targets), max_size=len(targets), ) diff --git a/tests/hypothesis_strategies/parameters.py b/tests/hypothesis_strategies/parameters.py index a01003fb8b..767e719e98 100644 --- a/tests/hypothesis_strategies/parameters.py +++ b/tests/hypothesis_strategies/parameters.py @@ -19,11 +19,12 @@ from baybe.parameters.substance import SubstanceEncoding, SubstanceParameter from baybe.utils.numerical import DTypeFloatNumpy +from ..hypothesis_strategies.basic import finite_floats from .utils import intervals decorrelations = st.one_of( st.booleans(), - st.floats(min_value=0.0, max_value=1.0, exclude_min=True, exclude_max=True), + finite_floats(min_value=0.0, max_value=1.0, exclude_min=True, exclude_max=True), ) """A strategy that generates decorrelation settings.""" @@ -68,7 +69,7 @@ def custom_descriptors(draw: st.DrawFn): index = st.lists(st.text(min_size=1), min_size=2, max_size=10, unique=True) cols = columns( names_or_number=10, - elements=st.floats(allow_nan=False, allow_infinity=False), + elements=finite_floats(), unique=True, dtype=DTypeFloatNumpy, ) @@ -85,9 +86,7 @@ def numerical_discrete_parameters( name = draw(parameter_names) values = draw( st.lists( - st.floats( - allow_infinity=False, - allow_nan=False, + finite_floats( min_value=min_value, max_value=max_value, ), @@ -100,10 +99,9 @@ def numerical_discrete_parameters( tolerance = 0.0 else: tolerance = draw( - st.floats( + finite_floats( min_value=0.0, max_value=max_tolerance, - allow_nan=False, exclude_max=True, ) )