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-11 16:42:27] 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-11 16:42:28] 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.0,
   'x2': 0.0,
   'x3': 0.0,
   'x4': 0.0,
   'x5': 0.06930601680758236,
   'x6': 0.0,
   'x7': 0.0},
  'means': {'Cosine8': -0.7415530059101249},
  'cov_matrix': {'Cosine8': {'Cosine8': 1.994921857584542e-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
{49: {'params': {'x0': 0.0,
   'x1': 0.0,
   'x2': 0.0,
   'x3': 0.0,
   'x4': 0.0,
   'x5': 0.06930601680758236,
   'x6': 0.0,
   'x7': 0.0},
  'means': {'Cosine8': -0.7415643011},
  '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.312574 0.641198 0.509851 0.643574 0.342344 0.184574 0.425460 0.147585 0.862826
1 1 1_0 COMPLETED Sobol 4.114923 0.641329 0.760731 0.480517 0.655057 0.684032 0.827076 0.544816 0.953904
2 2 2_0 COMPLETED Sobol 2.743053 0.551332 0.774817 0.270002 0.838295 0.303479 0.398621 0.623924 0.686079
3 3 3_0 COMPLETED Sobol 3.850876 0.762454 0.626652 0.244818 0.945087 0.476822 0.015980 0.912887 0.904618
4 4 4_0 COMPLETED Sobol 2.608688 0.667364 0.069686 0.309966 0.428322 0.674479 0.065950 0.976063 0.709459
5 5 5_0 COMPLETED Sobol 1.974493 0.370491 0.162647 0.443520 0.874803 0.776729 0.574515 0.193187 0.012911
6 6 6_0 COMPLETED Sobol 2.845052 0.826540 0.836006 0.405795 0.615118 0.402983 0.928199 0.394292 0.315527
7 7 7_0 COMPLETED Sobol 3.698982 0.998682 0.450401 0.476324 0.747421 0.426451 0.997526 0.474534 0.571115
8 8 8_0 COMPLETED Sobol 2.648915 0.238294 0.848735 0.798250 0.370319 0.317516 0.411909 0.985842 0.402288
9 9 9_0 COMPLETED Sobol 2.944921 0.721314 0.492542 0.058900 0.811311 0.603035 0.736251 0.510693 0.658074
10 10 10_0 COMPLETED Sobol 2.757180 0.592044 0.611893 0.585272 0.314436 0.294956 0.513611 0.711743 0.592482
11 11 11_0 COMPLETED Sobol 0.439704 0.829978 0.281443 0.033115 0.072682 0.062373 0.297407 0.004131 0.031631
12 12 12_0 COMPLETED Sobol 2.410136 0.482622 0.527160 0.647361 0.622111 0.506918 0.286399 0.813753 0.080078
13 13 13_0 COMPLETED Sobol 2.120711 0.279927 0.069855 0.252951 0.039143 0.706080 0.626794 0.952211 0.365904
14 14 14_0 COMPLETED Sobol 3.345619 0.802037 0.765988 0.656114 0.131441 0.004006 0.914584 0.668150 0.705632
15 15 15_0 COMPLETED Sobol 2.464124 0.119684 0.800477 0.793525 0.912781 0.103097 0.050017 0.673922 0.391205
16 16 16_0 COMPLETED GPEI 1.604220 0.306006 0.236585 0.311612 0.850725 0.684008 0.484447 0.106861 0.041178
17 17 17_0 COMPLETED GPEI 2.551606 0.399276 0.061787 0.569153 0.908082 0.890039 0.639548 0.257491 0.000000
18 18 18_0 COMPLETED GPEI 2.065649 0.426924 0.350156 0.524015 0.825132 0.697300 0.648393 0.198878 0.000000
19 19 19_0 COMPLETED GPEI 0.698885 0.728773 0.278508 0.075984 0.179814 0.151707 0.305612 0.020328 0.038081
20 20 20_0 COMPLETED GPEI 0.667193 0.922598 0.296312 0.001208 0.000000 0.000000 0.235847 0.000000 0.061872
21 21 21_0 COMPLETED GPEI 0.286559 0.794509 0.244258 0.015352 0.020665 0.031567 0.430551 0.000000 0.000000
22 22 22_0 COMPLETED GPEI 0.187087 0.770096 0.397701 0.000000 0.018507 0.000000 0.444187 0.000000 0.000000
23 23 23_0 COMPLETED GPEI 0.245326 0.839955 0.097454 0.030576 0.025661 0.050534 0.402272 0.000000 0.000000
24 24 24_0 COMPLETED GPEI 0.783128 0.889544 0.274802 0.000000 0.000000 0.167345 0.471483 0.000000 0.000000
25 25 25_0 COMPLETED GPEI 0.287372 0.831902 0.029093 0.000000 0.168752 0.000000 0.393299 0.000000 0.000000
26 26 26_0 COMPLETED GPEI 0.262830 0.795698 0.071508 0.171566 0.000000 0.000000 0.373140 0.000000 0.000000
27 27 27_0 COMPLETED GPEI 0.198092 0.610418 0.469162 0.000000 0.000000 0.000000 0.389891 0.000000 0.000000
28 28 28_0 COMPLETED GPEI -0.055638 0.726465 0.012555 0.001103 0.000000 0.000000 0.343452 0.000000 0.000000
29 29 29_0 COMPLETED GPEI 0.535152 0.702919 0.514704 0.102717 0.067066 0.000000 0.424814 0.000000 0.000000
30 30 30_0 COMPLETED GPEI 0.302196 0.703841 0.412734 0.000000 0.006987 0.000000 0.408757 0.135834 0.000000
31 31 31_0 COMPLETED GPEI 0.057380 0.636298 0.000000 0.000000 0.000000 0.000000 0.353137 0.000000 0.104355
32 32 32_0 COMPLETED GPEI 0.031672 0.609046 0.069241 0.000000 0.000000 0.000000 0.286211 0.000000 0.000000
33 33 33_0 COMPLETED GPEI 0.120032 0.713692 0.000000 0.000000 0.000000 0.000000 0.310501 0.123906 0.000000
34 34 34_0 COMPLETED GPEI -0.078771 0.611300 0.000000 0.000000 0.000000 0.000000 0.379317 0.000000 0.000000
35 35 35_0 COMPLETED GPEI -0.390599 0.424837 0.000000 0.000000 0.000000 0.000000 0.444694 0.000000 0.000000
36 36 36_0 COMPLETED GPEI -0.007368 0.743013 0.000000 0.016614 0.000000 0.000000 0.300016 0.000000 0.026812
37 37 37_0 COMPLETED GPEI -0.216913 0.293198 0.000000 0.000000 0.000000 0.000000 0.514175 0.000000 0.000000
38 38 38_0 COMPLETED GPEI -0.413375 0.259757 0.000000 0.000000 0.000000 0.000000 0.400080 0.000000 0.000000
39 39 39_0 COMPLETED GPEI -0.039577 0.370922 0.082804 0.000000 0.000000 0.000000 0.580555 0.000000 0.000000
40 40 40_0 COMPLETED GPEI -0.487677 0.338707 0.000000 0.000000 0.000000 0.000000 0.392457 0.000000 0.000000
41 41 41_0 COMPLETED GPEI -0.307592 0.315678 0.000000 0.000000 0.000000 0.132048 0.386122 0.000000 0.000000
42 42 42_0 COMPLETED GPEI -0.331300 0.318212 0.000000 0.119249 0.000000 0.000000 0.386589 0.000000 0.000000
43 43 43_0 COMPLETED GPEI -0.322832 0.325220 0.000000 0.000000 0.129034 0.000000 0.380001 0.000000 0.000000
44 44 44_0 COMPLETED GPEI -0.335872 0.321640 0.000000 0.000000 0.000000 0.000000 0.382723 0.119181 0.000000
45 45 45_0 COMPLETED GPEI -0.349076 0.330138 0.111965 0.000000 0.000000 0.000000 0.394956 0.000000 0.000000
46 46 46_0 COMPLETED GPEI -0.382313 0.316948 0.000000 0.000000 0.000000 0.000000 0.372418 0.000000 0.091813
47 47 47_0 COMPLETED GPEI -0.453965 0.313889 0.000000 0.000000 0.000000 0.000000 0.322792 0.000000 0.000000
48 48 48_0 COMPLETED GPEI -0.170774 0.257477 0.000000 0.000000 0.000000 0.000000 0.332580 0.000000 0.198456
49 49 49_0 COMPLETED GPEI -0.741564 0.000000 0.000000 0.000000 0.000000 0.000000 0.069306 0.000000 0.000000

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)