-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
## E2E Test for using callables | ||
|
||
# This example shows how to use the register_callable function to request internal data from different objects such as campaign, recommenders and objective. | ||
|
||
# To request internal data from the recommendation process this function can be used by giving it two arguments: | ||
# - The class method that contains the required data | ||
# - The callable function that will process the data | ||
# The function wraps both functions and returns the wrapped function | ||
|
||
# This examples assumes some basic familiarity with using BayBE. | ||
# We refer to [`campaign`](./campaign.md) for a more general and basic example. | ||
|
||
### Necessary imports for this example | ||
|
||
from typing import Optional | ||
|
||
import pandas as pd | ||
|
||
from baybe import Campaign | ||
from baybe.objectives import SingleTargetObjective | ||
from baybe.parameters import NumericalDiscreteParameter | ||
from baybe.recommenders import RandomRecommender | ||
from baybe.searchspace import SearchSpace | ||
from baybe.targets import NumericalTarget | ||
from baybe.utils.basic import register_hook | ||
from baybe.utils.dataframe import add_fake_results | ||
|
||
### Setup | ||
|
||
# Define three test functions to test the functionality of register_callable(). | ||
# Note that the callable function needs to have the same signature like the function from which the data are required. | ||
|
||
|
||
def recommend_test( | ||
self, | ||
searchspace: SearchSpace, | ||
batch_size: int = 1, | ||
train_x: Optional[pd.DataFrame] = None, | ||
train_y: Optional[pd.DataFrame] = None, | ||
): | ||
"""Print the searchspace and the batch size from the recommend call.""" | ||
print("start recommend_test") | ||
print(searchspace, batch_size) | ||
print("End recommend_test") | ||
|
||
|
||
### Example | ||
|
||
# We create a two phase meta recommender with default values. | ||
|
||
recommender = RandomRecommender() | ||
|
||
# We overwrite the original recommend method of the sequential greedy recommender class. | ||
|
||
RandomRecommender.recommend = register_hook(RandomRecommender.recommend, recommend_test) | ||
|
||
# We define all needen parameters for this example and collect them in a list. | ||
|
||
temperature = NumericalDiscreteParameter( | ||
"Temperature", values=[90, 105, 120], tolerance=2 | ||
) | ||
concentration = NumericalDiscreteParameter( | ||
"Concentration", values=[0.057, 0.1, 0.153], tolerance=0.005 | ||
) | ||
parameters = [temperature, concentration] | ||
|
||
# We create the searchspace and the objective. | ||
|
||
searchspace = SearchSpace.from_product(parameters=parameters) | ||
objective = SingleTargetObjective(target=NumericalTarget(name="yield", mode="MAX")) | ||
|
||
### Creating the campaign | ||
|
||
campaign = Campaign( | ||
searchspace=searchspace, | ||
recommender=recommender, | ||
objective=objective, | ||
) | ||
|
||
# This campaign can now be used to get recommendations and add measurements: | ||
# Note that a for loop is used to train the data of the second recommendation on the data from the first one. | ||
|
||
for _ in range(2): | ||
recommendation = campaign.recommend(batch_size=3) | ||
print("\n\nRecommended experiments: ") | ||
print(recommendation) | ||
|
||
add_fake_results(recommendation, campaign) | ||
print("\n\nRecommended experiments with fake measured values: ") | ||
print(recommendation) | ||
|
||
campaign.add_measurements(recommendation) |