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
Campaign
Meta Data
Batches done: 0
Fits done: 0
SearchSpace
Search Space Type: DISCRETE
SubspaceDiscrete
Discrete Parameters
Name Type Num_Values Encoding
0 Granularity CategoricalP... 3 CategoricalE...
1 Pressure[bar] NumericalDis... 3 None
2 Temperature[... NumericalDis... 10 None
Experimental Representation
Granularity Pressure[bar] Temperature[degr
0 coarse 1.0 100.000
1 coarse 1.0 111.111
2 fine 1.0 100.000
.. ... ... ...
87 medium 10.0 177.778
88 medium 10.0 188.889
89 medium 10.0 200.000
[90 rows x 3 columns]
Meta Data
was_recommended: 0/90
was_measured: 0/90
dont_recommend: 0/90
Constraints
Empty DataFrame
Columns: []
Index: []
Computational Representation
Granularity_coar Granularity_fine ... Pressure[bar]
Temperature[degr 0 1.0 0.0 … 1.0 100.000 1 1.0 0.0 … 1.0 111.111 2 0.0 1.0 … 1.0 100.000 .. … … … … … 87 0.0 0.0 … 10.0 177.778 88 0.0 0.0 … 10.0 188.889 89 0.0 0.0 … 10.0 200.000
[90 rows x 5 columns]
Objective
Type: SingleTargetObjective
Targets
Type Name ... Upper_Bound Transformation
0 NumericalTarget Yield ... inf None
[1 rows x 6 columns]
TwoPhaseMetaRecommender
Initial recommender
FPSRecommender
Compatibility: SearchSpaceType.DISCRETE
Recommender
BotorchRecommender
Surrogate
GaussianProcessSurrogate
Supports Transfer Learning: True
Kernel factory: DefaultKernelFactory()
Acquisition function: qLogExpectedImprovement()
Compatibility: SearchSpaceType.HYBRID
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
Campaign
Meta Data
Batches done: 0
Fits done: 0
SearchSpace
Search Space Type: DISCRETE
SubspaceDiscrete
Discrete Parameters
Name Type Num_Values Encoding
0 Granularity CategoricalP... 3 CategoricalE...
1 Pressure[bar] NumericalDis... 3 None
2 Temperature[... NumericalDis... 10 None
Experimental Representation
Granularity Pressure[bar] Temperature[degr
0 coarse 1.0 100.000
1 coarse 1.0 111.111
2 fine 1.0 100.000
.. ... ... ...
87 medium 10.0 177.778
88 medium 10.0 188.889
89 medium 10.0 200.000
[90 rows x 3 columns]
Meta Data
was_recommended: 0/90
was_measured: 0/90
dont_recommend: 0/90
Constraints
Empty DataFrame
Columns: []
Index: []
Computational Representation
Granularity_coar Granularity_fine ... Pressure[bar]
Temperature[degr 0 1.0 0.0 … 1.0 100.000 1 1.0 0.0 … 1.0 111.111 2 0.0 1.0 … 1.0 100.000 .. … … … … … 87 0.0 0.0 … 10.0 177.778 88 0.0 0.0 … 10.0 188.889 89 0.0 0.0 … 10.0 200.000
[90 rows x 5 columns]
Objective
Type: SingleTargetObjective
Targets
Type Name ... Upper_Bound Transformation
0 NumericalTarget Yield ... inf None
[1 rows x 6 columns]
TwoPhaseMetaRecommender
Initial recommender
FPSRecommender
Compatibility: SearchSpaceType.DISCRETE
Recommender
BotorchRecommender
Surrogate
GaussianProcessSurrogate
Supports Transfer Learning: True
Kernel factory: DefaultKernelFactory()
Acquisition function: qLogExpectedImprovement()
Compatibility: SearchSpaceType.HYBRID
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
85 fine 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
85 fine 10.0 200.0