Skip to content

Commit

Permalink
Add hypothesis strategy for Partition
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianSosic committed Jan 16, 2025
1 parent 0af5c2c commit e7b929b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
2 changes: 2 additions & 0 deletions baybe/utils/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ class Partition:

@thresholds.validator
def _validate_thresholds(self, _, value):
if not np.all(np.isfinite(value)):
raise ValueError("Thresholds must be finite.")
if not all(x < y for x, y in pairwise(value)):
raise ValueError("Thresholds must be strictly monotonically increasing.")

Expand Down
12 changes: 11 additions & 1 deletion tests/hypothesis_strategies/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

import hypothesis.extra.numpy as hnp
import hypothesis.strategies as st
import numpy as np

from baybe.utils.interval import Interval
from baybe.utils.interval import Interval, Partition

from .basic import finite_floats

Expand Down Expand Up @@ -65,3 +66,12 @@ def intervals(
raise RuntimeError("This line should be unreachable.")

return Interval.create(bounds)


partitions = st.builds(
Partition,
thresholds=st.lists(finite_floats(), min_size=1)
.map(sorted)
.filter(lambda x: np.all(np.diff(x) > 0)),
)
"""Generate :class:`baybe.utils.interval.Partition`."""
2 changes: 2 additions & 0 deletions tests/validation/utils/test_partition_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
@pytest.mark.parametrize(
("thresholds", "error", "match"),
[
param([float("nan")], ValueError, "must be finite", id="nan"),
param([float("inf")], ValueError, "must be finite", id="inf"),
param(1, TypeError, None, id="not_iterable"),
param([], ValueError, None, id="too_short"),
param([1, 0], ValueError, "monotonically increasing", id="decreasing"),
Expand Down

0 comments on commit e7b929b

Please sign in to comment.