Skip to content

Commit

Permalink
Rename decorator utility
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianSosic committed Dec 20, 2024
1 parent d7d5a79 commit 4c36257
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 19 deletions.
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `benchmarks` subpackage for defining and running performance tests
`Campaign.toggle_discrete_candidates` to dynamically in-/exclude discrete candidates
- `filter_df` utility for filtering dataframe content
- `label_input_and_output_columns` decorator to create lookups from array-based
callables
- `arrays_to_dataframes` decorator to create lookups from array-based callables
- `DiscreteConstraint.get_valid` to conveniently access valid candidates
- Functionality for persisting benchmarking results on S3 from a manual pipeline run

Expand Down
10 changes: 7 additions & 3 deletions baybe/utils/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -683,13 +683,17 @@ def filter_df(
return out


def label_input_and_output_columns(
input_labels: Sequence[str], output_labels: Sequence[str], use_torch: bool = False
def arrays_to_dataframes(
input_labels: Sequence[str],
output_labels: Sequence[str],
/,
use_torch: bool = False,
) -> Callable[
[Callable[[_ArrayLike], _ArrayLike]], Callable[[pd.DataFrame], pd.DataFrame]
]:
"""Create a decorator for labeling the inputs and outputs of array-based callables.
"""Make a decorator for labeling the input/output columns of array-based callables.
Useful for creating parameter-to-target lookups from array-based logic.
The decorator transforms a callable designed to work with unlabelled arrays such
that it can operate with dataframes instead. The original callable is expected to
accept and return two-dimensional arrays. When decorated, the callable accepts and
Expand Down
4 changes: 2 additions & 2 deletions examples/Constraints_Continuous/hybrid_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from baybe.parameters import NumericalContinuousParameter, NumericalDiscreteParameter
from baybe.searchspace import SearchSpace
from baybe.targets import NumericalTarget
from baybe.utils.dataframe import label_input_and_output_columns
from baybe.utils.dataframe import arrays_to_dataframes

### Defining the test function

Expand Down Expand Up @@ -93,7 +93,7 @@

### Wrap the test function as a dataframe-based lookup callable

lookup = label_input_and_output_columns(
lookup = arrays_to_dataframes(
[p.name for p in parameters], [target.name], use_torch=True
)(TestFunction)

Expand Down
4 changes: 2 additions & 2 deletions examples/Constraints_Continuous/linear_constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from baybe.parameters import NumericalContinuousParameter
from baybe.searchspace import SearchSpace
from baybe.targets import NumericalTarget
from baybe.utils.dataframe import label_input_and_output_columns
from baybe.utils.dataframe import arrays_to_dataframes

### Defining the test function

Expand Down Expand Up @@ -79,7 +79,7 @@

### Wrap the test function as a dataframe-based lookup callable

lookup = label_input_and_output_columns(
lookup = arrays_to_dataframes(
[p.name for p in parameters], [target.name], use_torch=True
)(TestFunction)

Expand Down
4 changes: 2 additions & 2 deletions examples/Custom_Hooks/probability_of_improvement.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from baybe.surrogates import GaussianProcessSurrogate
from baybe.targets import NumericalTarget
from baybe.utils.basic import register_hooks
from baybe.utils.dataframe import label_input_and_output_columns, to_tensor
from baybe.utils.dataframe import arrays_to_dataframes, to_tensor
from baybe.utils.plotting import create_example_plots
from baybe.utils.random import set_random_seed

Expand Down Expand Up @@ -141,7 +141,7 @@ def extract_pi(

# Now, we perform a couple of experimental iterations with the active hook:

lookup = label_input_and_output_columns(
lookup = arrays_to_dataframes(
[p.name for p in discrete_params], [target.name], use_torch=True
)(test_function)

Expand Down
4 changes: 2 additions & 2 deletions examples/Searchspaces/discrete_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from baybe.parameters import NumericalDiscreteParameter
from baybe.searchspace import SearchSpace
from baybe.targets import NumericalTarget
from baybe.utils.dataframe import label_input_and_output_columns
from baybe.utils.dataframe import arrays_to_dataframes

### Defining the test function

Expand Down Expand Up @@ -89,7 +89,7 @@

# Evaluate the test function.

lookup = label_input_and_output_columns(
lookup = arrays_to_dataframes(
[p.name for p in parameters], [target.name], use_torch=True
)(TestFunction)

Expand Down
4 changes: 2 additions & 2 deletions examples/Searchspaces/hybrid_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from baybe.recommenders import NaiveHybridSpaceRecommender, TwoPhaseMetaRecommender
from baybe.searchspace import SearchSpace
from baybe.targets import NumericalTarget
from baybe.utils.dataframe import label_input_and_output_columns
from baybe.utils.dataframe import arrays_to_dataframes

### Defining the test function and the hybrid dimensions

Expand Down Expand Up @@ -104,7 +104,7 @@

### Wrap the test function as a dataframe-based lookup callable

lookup = label_input_and_output_columns(
lookup = arrays_to_dataframes(
searchspace.parameter_names, [target.name], use_torch=True
)(TestFunction)

Expand Down
4 changes: 2 additions & 2 deletions examples/Transfer_Learning/backtesting.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from baybe.searchspace import SearchSpace
from baybe.simulation import simulate_scenarios, simulate_transfer_learning
from baybe.targets import NumericalTarget
from baybe.utils.dataframe import label_input_and_output_columns
from baybe.utils.dataframe import arrays_to_dataframes
from baybe.utils.plotting import create_example_plots

### Settings
Expand Down Expand Up @@ -92,7 +92,7 @@
# and vice versa. The used model is of course not aware of this relationship but
# needs to infer it from the data gathered during the optimization process.

wrapper = label_input_and_output_columns(
wrapper = arrays_to_dataframes(
[p.name for p in discrete_params], [target.name], use_torch=True
)

Expand Down
4 changes: 2 additions & 2 deletions examples/Transfer_Learning/basic_transfer_learning.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from baybe.searchspace import SearchSpace
from baybe.simulation import simulate_scenarios
from baybe.targets import NumericalTarget
from baybe.utils.dataframe import label_input_and_output_columns
from baybe.utils.dataframe import arrays_to_dataframes
from baybe.utils.plotting import create_example_plots

### Settings
Expand Down Expand Up @@ -96,7 +96,7 @@
# noise. The used model is of course not aware of this relationship but needs to infer
# it from the data gathered during the optimization process.

wrapper = label_input_and_output_columns(
wrapper = arrays_to_dataframes(
[p.name for p in discrete_params], [target.name], use_torch=True
)

Expand Down

0 comments on commit 4c36257

Please sign in to comment.