Skip to content

Commit

Permalink
Make hypothesis respect floating point precision
Browse files Browse the repository at this point in the history
  • Loading branch information
Scienfitz committed May 29, 2024
1 parent b2a9863 commit 7946692
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 22 deletions.
10 changes: 4 additions & 6 deletions tests/hypothesis_strategies/acquisition.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
10 changes: 9 additions & 1 deletion tests/hypothesis_strategies/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
10 changes: 5 additions & 5 deletions tests/hypothesis_strategies/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`."""
Expand All @@ -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
Expand Down Expand Up @@ -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)


Expand Down
4 changes: 3 additions & 1 deletion tests/hypothesis_strategies/dataframes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
)
Expand Down
3 changes: 2 additions & 1 deletion tests/hypothesis_strategies/objectives.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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),
)
Expand Down
12 changes: 5 additions & 7 deletions tests/hypothesis_strategies/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

Expand Down Expand Up @@ -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,
)
Expand All @@ -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,
),
Expand All @@ -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,
)
)
Expand Down

0 comments on commit 7946692

Please sign in to comment.