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

Hotfix for match mode simulation #208

Merged
merged 5 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `n_task_params` now evaluates to 1 if `task_idx == 0`
- Simulation no longer fails in `ignore` mode when lookup dataframe contains duplicate
parameter configurations
- Simulation no longer fails for targets in `MATCH` mode
- `closest_element` now works for array-like input of all kinds

### Deprecations
- The former `baybe.objective.Objective` class has been replaced with
Expand Down
2 changes: 1 addition & 1 deletion baybe/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ def simulate_experiment(
agg_fun = np.min
cum_fun = np.minimum.accumulate
elif target.mode is TargetMode.MATCH:
match_val = np.mean(target.bounds)
match_val = target.bounds.center
agg_fun = partial(closest_element, target=match_val)
cum_fun = lambda x: np.array( # noqa: E731
np.frompyfunc(
Expand Down
10 changes: 7 additions & 3 deletions baybe/utils/numerical.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from collections.abc import Sequence

import numpy as np
import numpy.typing as npt

DTypeFloatNumpy = np.float64
"""Floating point data type used for numpy arrays."""
Expand Down Expand Up @@ -33,16 +34,19 @@ def geom_mean(arr: np.ndarray, weights: Sequence[float]) -> np.ndarray:
return np.prod(np.power(arr, np.atleast_2d(weights) / np.sum(weights)), axis=1)


def closest_element(array: np.ndarray, target: float) -> float:
def closest_element(array: npt.ArrayLike, target: float) -> float:
"""Find the element of an array that is closest to a target value.

Args:
array: The array in which the closest value should be found.
array: The array in which the closest value is to be found.
target: The target value.

Returns:
The closes element.
The closest element.
"""
if np.ndim(array) == 0:
return np.asarray(array).item()
array = np.ravel(array)
return array[np.abs(array - target).argmin()]


Expand Down
26 changes: 26 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""Tests for utilities."""

import numpy as np
import pytest
from pytest import param

from baybe.utils.numerical import closest_element


@pytest.mark.parametrize(
"as_ndarray", [param(False, id="list"), param(True, id="array")]
)
@pytest.mark.parametrize(
("array", "target", "expected"),
[
param(0, 0.1, 0, id="0D"),
AdrianSosic marked this conversation as resolved.
Show resolved Hide resolved
param([0, 1], 0.1, 0, id="1D"),
param([[2, 3], [0, 1]], 0.1, 0, id="2D"),
],
)
def test_closest_element(as_ndarray, array, target, expected):
"""The closest element can be found irrespective of the input type."""
if as_ndarray:
array = np.asarray(array)
actual = closest_element(array, target)
assert actual == expected, (actual, expected)