Loading BOA from JSON and Plotting Results#

This notebook demonstrates how to:

Loading a Scheduler from JSON from a previous run (If you want to see the Experiment that this is from, see Running BOA Optimization Directly in Python. We will look at the output and plot some exploratory data analysis.

 1import pathlib
 2import os
 3
 4import numpy as np
 5from ax.utils.notebook.plotting import init_notebook_plotting
 6from ax.plot.trace import optimization_trace_single_method_plotly
 7from ax.service.utils.report_utils import get_standard_plots, exp_to_df
 8import boa
 9from botorch.test_functions.synthetic import Cosine8
10
11init_notebook_plotting()
[WARNING 08-09 19:04:36] ax.service.utils.with_db_settings_base: Ax currently requires a sqlalchemy version below 2.0. This will be addressed in a future release. Disabling SQL storage in Ax for now, if you would like to use SQL storage please install Ax with mysql extras via `pip install ax-platform[mysql]`.
[INFO 08-09 19:04:37] ax.utils.notebook.plotting: Injecting Plotly library into cell. Do not overwrite or delete cell.

Loading the Scheduler#

1# setup stuff just because this gets reused from the latest run for the case of the docs
2try:
3    run = list(pathlib.Path().resolve().glob("boa_runs*"))[-1]
4except IndexError:
5    print("No run to load. Make sure you run optimization_run.ipynb first")
1# Filepath to the scheduler.json
2
3scheduler_fp = run / "scheduler.json"
1scheduler = boa.scheduler_from_json_file(scheduler_fp)
1scheduler
Scheduler(experiment=Experiment(boa_runs), generation_strategy=GenerationStrategy(name='Sobol+GPEI', steps=[Sobol for 16 trials, GPEI for subsequent trials]), options=SchedulerOptions(max_pending_trials=10, trial_type=<TrialType.TRIAL: 0>, batch_size=None, total_trials=None, tolerated_trial_failure_rate=0.5, min_failed_trials_for_failure_rate_check=5, log_filepath=None, logging_level=20, ttl_seconds_for_trials=None, init_seconds_between_polls=1, min_seconds_before_poll=1.0, seconds_between_polls_backoff_factor=1.5, timeout_hours=None, run_trials_in_batches=False, debug_log_run_metadata=False, early_stopping_strategy=None, global_stopping_strategy=None, suppress_storage_errors_after_retries=False))

Show the Best Fitted Trial and Raw Trial#

best_fitted_trials uses the data to do a fitting from all trials and with the noise levels you provided (or if no noise levels was provided, it assumed an unknown level of noise and inferred the noise level from the trial runs)

1trial = scheduler.best_fitted_trials()
2trial
{49: {'params': {'x0': 0.0,
   'x1': 0.37206440392558554,
   'x2': 0.37332223273644144,
   'x3': 0.3660092554325016,
   'x4': 0.3672361370154477,
   'x5': 0.3614313634497034,
   'x6': 0.0,
   'x7': 0.0},
  'means': {'Cosine8': -0.059883372839546256},
  'cov_matrix': {'Cosine8': {'Cosine8': 1.5097094613552903e-06}}}}

if you need the exact points of the best trial, maybe because you need the trial number of the best trial to plot results, or for any other reason, best_raw_trails does not do any fitting

1trial = scheduler.best_raw_trials()
2trial
{44: {'params': {'x0': 0.0,
   'x1': 0.37206440392558554,
   'x2': 0.37332223273644144,
   'x3': 0.3660092554325016,
   'x4': 0.3672361370154477,
   'x5': 0.3614313634497034,
   'x6': 0.0,
   'x7': 0.0},
  'means': {'Cosine8': -0.059944627300000004},
  'cov_matrix': {'Cosine8': {'Cosine8': 0.0}}}}
1boa.scheduler_to_df(scheduler)
trial_index arm_name trial_status generation_method Cosine8 x0 x1 x2 x3 x4 x5 x6 x7
0 0 0_0 COMPLETED Sobol 2.663157 0.312017 0.375450 0.518184 0.255694 0.707143 0.977300 0.627417 0.424752
1 1 1_0 COMPLETED Sobol 3.472775 0.811599 0.102800 0.826156 0.586577 0.981025 0.461695 0.791734 0.264965
2 2 2_0 COMPLETED Sobol 2.599138 0.513995 0.750892 0.365383 0.176387 0.993027 0.377566 0.549260 0.210662
3 3 3_0 COMPLETED Sobol 1.135110 0.020953 0.352685 0.384740 0.598315 0.675668 0.320246 0.389470 0.386341
4 4 4_0 COMPLETED Sobol 1.293105 0.049371 0.654408 0.467794 0.322839 0.333161 0.513854 0.134319 0.485225
5 5 5_0 COMPLETED Sobol 1.457474 0.779954 0.080537 0.271937 0.845372 0.035978 0.263293 0.257189 0.118739
6 6 6_0 COMPLETED Sobol 4.251037 0.869940 0.920247 0.434625 0.701204 0.696775 0.943400 0.606757 0.462032
7 7 7_0 COMPLETED Sobol 1.981109 0.600472 0.356166 0.415501 0.840716 0.145596 0.444465 0.601179 0.432331
8 8 8_0 COMPLETED Sobol 2.561738 0.507137 0.699582 0.326667 0.639318 0.389713 0.358486 0.544911 0.891967
9 9 9_0 COMPLETED Sobol 4.313421 0.646849 0.827967 0.732573 0.994397 0.221591 0.797337 0.615998 0.755372
10 10 10_0 COMPLETED Sobol 4.327585 0.508235 0.655068 0.893461 0.930283 0.624175 0.886251 0.679031 0.264030
11 11 11_0 COMPLETED Sobol 1.960524 0.245657 0.174974 0.354753 0.424672 0.133220 0.951068 0.439949 0.566493
12 12 12_0 COMPLETED Sobol 1.423228 0.252244 0.236526 0.581198 0.018938 0.773188 0.382626 0.166053 0.225524
13 13 13_0 COMPLETED Sobol 1.470841 0.430965 0.535480 0.319732 0.225324 0.358769 0.215507 0.827910 0.112318
14 14 14_0 COMPLETED Sobol 2.047706 0.825881 0.154236 0.244455 0.487318 0.748788 0.788026 0.309836 0.027160
15 15 15_0 COMPLETED Sobol 3.829676 0.896006 0.323754 0.970113 0.600032 0.488128 0.843276 0.184945 0.638443
16 16 16_0 COMPLETED GPEI 0.831562 0.036738 0.472569 0.397035 0.506163 0.458229 0.376407 0.258182 0.421778
17 17 17_0 COMPLETED GPEI 1.411013 0.000000 0.522502 0.458266 0.426792 0.721061 0.422221 0.266576 0.507974
18 18 18_0 COMPLETED GPEI 1.143500 0.212341 0.268985 0.330559 0.679346 0.434590 0.297060 0.349851 0.314136
19 19 19_0 COMPLETED GPEI 0.463539 0.086164 0.361035 0.370711 0.297997 0.358246 0.348047 0.298517 0.293590
20 20 20_0 COMPLETED GPEI 0.383194 0.034869 0.334433 0.375754 0.461653 0.441267 0.412602 0.036355 0.292011
21 21 21_0 COMPLETED GPEI 0.688874 0.040416 0.495560 0.429553 0.433630 0.297753 0.193928 0.385047 0.349413
22 22 22_0 COMPLETED GPEI 0.298763 0.000000 0.335533 0.382258 0.345978 0.332896 0.287420 0.047099 0.217264
23 23 23_0 COMPLETED GPEI 0.283036 0.000000 0.211597 0.399096 0.342443 0.303916 0.345610 0.051221 0.387174
24 24 24_0 COMPLETED GPEI 0.224499 0.000000 0.383919 0.361917 0.395714 0.365952 0.413164 0.139015 0.077381
25 25 25_0 COMPLETED GPEI 0.473313 0.000000 0.213475 0.389053 0.377933 0.248060 0.433936 0.111913 0.129031
26 26 26_0 COMPLETED GPEI 0.506551 0.000000 0.248434 0.243786 0.325343 0.303443 0.453504 0.044787 0.169293
27 27 27_0 COMPLETED GPEI 0.606441 0.000000 0.246060 0.493610 0.393148 0.308101 0.375003 0.163087 0.117784
28 28 28_0 COMPLETED GPEI 0.537491 0.000000 0.492178 0.306430 0.412458 0.239091 0.350907 0.177723 0.053241
29 29 29_0 COMPLETED GPEI 0.350213 0.000000 0.385204 0.345878 0.297388 0.501700 0.405405 0.090237 0.057207
30 30 30_0 COMPLETED GPEI 0.384968 0.000000 0.412547 0.370486 0.323259 0.277802 0.447566 0.046090 0.184052
31 31 31_0 COMPLETED GPEI 0.399310 0.000000 0.326253 0.350303 0.384816 0.406117 0.384198 0.151564 0.170865
32 32 32_0 COMPLETED GPEI 0.651089 0.000000 0.162394 0.357030 0.254357 0.404179 0.177300 0.038917 0.541337
33 33 33_0 COMPLETED GPEI 0.133918 0.000000 0.367831 0.348879 0.466264 0.424930 0.415589 0.015454 0.000000
34 34 34_0 COMPLETED GPEI 0.160010 0.134682 0.383295 0.376547 0.416497 0.363486 0.407027 0.000000 0.000000
35 35 35_0 COMPLETED GPEI 0.607355 0.207752 0.261156 0.403849 0.292222 0.151493 0.273163 0.000000 0.408476
36 36 36_0 COMPLETED GPEI 0.241903 0.000000 0.445157 0.420915 0.474798 0.422200 0.406699 0.000000 0.000000
37 37 37_0 COMPLETED GPEI 0.132668 0.034296 0.369506 0.351920 0.429270 0.365676 0.470187 0.000000 0.000000
38 38 38_0 COMPLETED GPEI 0.583685 0.085351 0.385241 0.324978 0.500865 0.407612 0.538656 0.033266 0.000000
39 39 39_0 COMPLETED GPEI -0.023691 0.026875 0.366678 0.363851 0.414838 0.363992 0.364331 0.000000 0.000000
40 40 40_0 COMPLETED GPEI -0.040379 0.000000 0.357151 0.374060 0.370617 0.328077 0.357097 0.000000 0.000000
41 41 41_0 COMPLETED GPEI 0.115861 0.000000 0.344771 0.370551 0.454516 0.278565 0.331861 0.000000 0.000000
42 42 42_0 COMPLETED GPEI 0.036092 0.047530 0.337711 0.337497 0.333073 0.329439 0.327415 0.000000 0.000000
43 43 43_0 COMPLETED GPEI 0.601410 0.195682 0.091939 0.364800 0.288180 0.435749 0.463267 0.000000 0.459962
44 44 44_0 COMPLETED GPEI -0.059945 0.000000 0.372064 0.373322 0.366009 0.367236 0.361431 0.000000 0.000000
45 45 45_0 COMPLETED GPEI -0.036262 0.000000 0.407358 0.361476 0.360987 0.357513 0.352213 0.000000 0.000000
46 46 46_0 COMPLETED GPEI -0.030902 0.000000 0.368343 0.386212 0.362645 0.380164 0.319139 0.000000 0.000000
47 47 47_0 COMPLETED GPEI 2.587291 0.453197 0.682408 0.350890 0.612169 0.674319 0.550328 0.038512 0.847392
48 48 48_0 COMPLETED GPEI 2.134270 0.492481 0.245022 0.685515 0.889154 0.622527 0.033786 0.109576 0.312552
49 49 49_0 COMPLETED GPEI 1.029291 0.000000 0.134440 0.300564 0.563262 0.233928 0.387781 0.000000 0.615784

EDA Plots#

1boa.plot_metrics_trace(scheduler)
1boa.plot_contours(scheduler)
1boa.plot_slice(scheduler)

We can also directly pass in our scheduler file path instead of having to reload it ourselves

1boa.plot_metrics_trace(scheduler_fp)