Example for the serialization of a campaign¶
This example shows how to serialize and also de-serialize a campaign. It demonstrates and shows that the “original” and “new” objects behave the same.
This example assumes some basic familiarity with using BayBE.
We thus refer to campaign
for a basic example.
Necessary imports¶
import numpy as np
from baybe import Campaign
from baybe.objectives import SingleTargetObjective
from baybe.parameters import (
CategoricalParameter,
NumericalDiscreteParameter,
)
from baybe.recommenders import (
BotorchRecommender,
FPSRecommender,
TwoPhaseMetaRecommender,
)
from baybe.searchspace import SearchSpace
from baybe.targets import NumericalTarget
Experiment setup¶
parameters = [
CategoricalParameter(
name="Granularity",
values=["coarse", "medium", "fine"],
encoding="OHE",
),
NumericalDiscreteParameter(
name="Pressure[bar]",
values=[1, 5, 10],
tolerance=0.2,
),
NumericalDiscreteParameter(
name="Temperature[degree_C]",
values=np.linspace(100, 200, 10),
),
]
Creating the campaign¶
campaign = Campaign(
searchspace=SearchSpace.from_product(parameters=parameters, constraints=None),
objective=SingleTargetObjective(target=NumericalTarget(name="Yield", mode="MAX")),
recommender=TwoPhaseMetaRecommender(
recommender=BotorchRecommender(),
initial_recommender=FPSRecommender(),
),
)
Serialization and de-serialization¶
We begin by printing the original campaign
print("Original object")
print(campaign, end="\n" * 3)
Original object
[1mCampaign[0m
[1mMeta Data[0m
Batches Done: 0
Fits Done: 0
[1mSearch Space[0m
[1mSearch Space Type: [0mDISCRETE
[1mDiscrete Search Space[0m
[1mDiscrete Parameters[0m
Name Type Num_Values
Encoding 0 Granularity CategoricalParameter 3 CategoricalEncoding.OHE 1 Pressure[bar] NumericalDiscreteParameter 3 None 2 Temperature[degree_C] NumericalDiscreteParameter 10 None
[1mExperimental Representation[0m
Granularity Pressure[bar] Temperature[degree_C]
0 coarse 1.0 100.000000
1 coarse 1.0 111.111111
2 coarse 1.0 122.222222
.. ... ... ...
87 fine 10.0 177.777778
88 fine 10.0 188.888889
89 fine 10.0 200.000000
[90 rows x 3 columns]
[1mMetadata:[0m
was_recommended: 0/90
was_measured: 0/90
dont_recommend: 0/90
[1mConstraints[0m
Empty DataFrame
Columns: []
Index: []
[1mComputational Representation[0m
Granularity_coarse Granularity_medium ... Pressure[bar] Temperature[degree_C]
0 1.0 0.0 ... 1.0
100.000000 1 1.0 0.0 … 1.0 111.111111 2 1.0 0.0 … 1.0 122.222222 .. … … … … … 87 0.0 0.0 … 10.0 177.777778 88 0.0 0.0 … 10.0 188.888889 89 0.0 0.0 … 10.0 200.000000
[90 rows x 5 columns]
[1mObjective[0m
[1mType: [0mSingleTargetObjective
[1mTargets [0m
Type Name Mode Lower_Bound Upper_Bound Transformation
0 NumericalTarget Yield MAX -inf inf None
TwoPhaseMetaRecommender(initial_recommender=FPSRecommender(allow_repeated_recommend
ations=False, allow_recommending_already_measured=True), recommender=BotorchRecommender(allow_repeated_recommendations=False, allow_recommending_already_measured=True, surrogate_model=GaussianProcessSurrogate(kernel_factory=DefaultKernelFactory(), _model=None), acquisition_function=qLogExpectedImprovement(), _botorch_acqf=None, acquisition_function_cls=None, sequential_continuous=False, hybrid_sampler=None, sampling_percentage=1.0), switch_after=1)
We next serialize the campaign to JSON. This yields a JSON representation in string format. Since it is rather complex, we do not print this string here. Note: Dataframes are binary-encoded and are hence not human-readable.
string = campaign.to_json()
Deserialize the JSON string back to an object.
print("Deserialized object")
campaign_recreate = Campaign.from_json(string)
print(campaign_recreate, end="\n" * 3)
Deserialized object
[1mCampaign[0m
[1mMeta Data[0m
Batches Done: 0
Fits Done: 0
[1mSearch Space[0m
[1mSearch Space Type: [0mDISCRETE
[1mDiscrete Search Space[0m
[1mDiscrete Parameters[0m
Name Type Num_Values
Encoding 0 Granularity CategoricalParameter 3 CategoricalEncoding.OHE 1 Pressure[bar] NumericalDiscreteParameter 3 None 2 Temperature[degree_C] NumericalDiscreteParameter 10 None
[1mExperimental Representation[0m
Granularity Pressure[bar] Temperature[degree_C]
0 coarse 1.0 100.000000
1 coarse 1.0 111.111111
2 coarse 1.0 122.222222
.. ... ... ...
87 fine 10.0 177.777778
88 fine 10.0 188.888889
89 fine 10.0 200.000000
[90 rows x 3 columns]
[1mMetadata:[0m
was_recommended: 0/90
was_measured: 0/90
dont_recommend: 0/90
[1mConstraints[0m
Empty DataFrame
Columns: []
Index: []
[1mComputational Representation[0m
Granularity_coarse Granularity_medium ... Pressure[bar] Temperature[degree_C]
0 1.0 0.0 ... 1.0
100.000000 1 1.0 0.0 … 1.0 111.111111 2 1.0 0.0 … 1.0 122.222222 .. … … … … … 87 0.0 0.0 … 10.0 177.777778 88 0.0 0.0 … 10.0 188.888889 89 0.0 0.0 … 10.0 200.000000
[90 rows x 5 columns]
[1mObjective[0m
[1mType: [0mSingleTargetObjective
[1mTargets [0m
Type Name Mode Lower_Bound Upper_Bound Transformation
0 NumericalTarget Yield MAX -inf inf None
TwoPhaseMetaRecommender(initial_recommender=FPSRecommender(allow_repeated_recommend
ations=False, allow_recommending_already_measured=True), recommender=BotorchRecommender(allow_repeated_recommendations=False, allow_recommending_already_measured=True, surrogate_model=GaussianProcessSurrogate(kernel_factory=DefaultKernelFactory(), _model=None), acquisition_function=qLogExpectedImprovement(), _botorch_acqf=None, acquisition_function_cls=None, sequential_continuous=False, hybrid_sampler=None, sampling_percentage=1.0), switch_after=1)
# Verify that both objects are equal.
assert campaign == campaign_recreate
print("Passed basic assertion check!")
Passed basic assertion check!
Comparing recommendations in both objects¶
To further show how serialization affects working with campaigns, we will now create and compare some recommendations in both campaigns.
recommendation_orig = campaign.recommend(batch_size=2)
recommendation_recreate = campaign_recreate.recommend(batch_size=2)
print("Recommendation from original object:")
print(recommendation_orig)
Recommendation from original object:
Granularity Pressure[bar] Temperature[degree_C]
0 coarse 1.0 100.0
59 medium 10.0 200.0
print("Recommendation from recreated object:")
print(recommendation_recreate)
Recommendation from recreated object:
Granularity Pressure[bar] Temperature[degree_C]
0 coarse 1.0 100.0
59 medium 10.0 200.0