# 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`](./../Basics/campaign.md) for a basic example. ## Necessary imports ```python import numpy as np ``` ```python 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 ```python 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 ```python 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 ```python print("Original object") print(campaign, end="\n" * 3) ``` Original object Campaign Meta Data Batches Done: 0 Fits Done: 0 Search Space Search Space Type: DISCRETE Discrete Search Space Discrete Parameters Name Type Num_Values Encoding 0 Granularity CategoricalParameter 3 CategoricalEncoding.OHE 1 Pressure[bar] NumericalDiscreteParameter 3 None 2 Temperature[degree_C] NumericalDiscreteParameter 10 None Experimental Representation 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] Metadata: was_recommended: 0/90 was_measured: 0/90 dont_recommend: 0/90 Constraints Empty DataFrame Columns: [] Index: [] Computational Representation 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] Objective Type: SingleTargetObjective Targets  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. ```python string = campaign.to_json() ``` Deserialize the JSON string back to an object. ```python 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 Search Space Search Space Type: DISCRETE Discrete Search Space Discrete Parameters Name Type Num_Values Encoding 0 Granularity CategoricalParameter 3 CategoricalEncoding.OHE 1 Pressure[bar] NumericalDiscreteParameter 3 None 2 Temperature[degree_C] NumericalDiscreteParameter 10 None Experimental Representation 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] Metadata: was_recommended: 0/90 was_measured: 0/90 dont_recommend: 0/90 Constraints Empty DataFrame Columns: [] Index: [] Computational Representation 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] Objective Type: SingleTargetObjective Targets  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) ```python # 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. ```python recommendation_orig = campaign.recommend(batch_size=2) recommendation_recreate = campaign_recreate.recommend(batch_size=2) ``` ```python 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 ```python 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