bax-algorithms 0.1.2__tar.gz → 0.1.4__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {bax_algorithms-0.1.2 → bax_algorithms-0.1.4}/.github/workflows/tests.yml +1 -1
- {bax_algorithms-0.1.2 → bax_algorithms-0.1.4}/PKG-INFO +3 -4
- {bax_algorithms-0.1.2 → bax_algorithms-0.1.4}/bax_algorithms/_version.py +3 -3
- {bax_algorithms-0.1.2 → bax_algorithms-0.1.4}/bax_algorithms/emittance.py +149 -50
- {bax_algorithms-0.1.2 → bax_algorithms-0.1.4}/bax_algorithms/pathwise/base.py +15 -53
- {bax_algorithms-0.1.2 → bax_algorithms-0.1.4}/bax_algorithms/pathwise/optimize.py +3 -0
- {bax_algorithms-0.1.2 → bax_algorithms-0.1.4}/bax_algorithms/solenoid_alignment.py +65 -48
- {bax_algorithms-0.1.2 → bax_algorithms-0.1.4}/bax_algorithms/visualize.py +174 -3
- {bax_algorithms-0.1.2 → bax_algorithms-0.1.4}/bax_algorithms.egg-info/PKG-INFO +3 -4
- {bax_algorithms-0.1.2 → bax_algorithms-0.1.4}/bax_algorithms.egg-info/SOURCES.txt +2 -0
- {bax_algorithms-0.1.2 → bax_algorithms-0.1.4}/bax_algorithms.egg-info/requires.txt +1 -1
- bax_algorithms-0.1.4/bax_algorithms.egg-info/scm_file_list.json +26 -0
- bax_algorithms-0.1.4/bax_algorithms.egg-info/scm_version.json +8 -0
- {bax_algorithms-0.1.2 → bax_algorithms-0.1.4}/examples/emittance.ipynb +7 -39
- {bax_algorithms-0.1.2 → bax_algorithms-0.1.4}/examples/solenoid_alignment.ipynb +10 -10
- {bax_algorithms-0.1.2 → bax_algorithms-0.1.4}/pyproject.toml +2 -3
- {bax_algorithms-0.1.2 → bax_algorithms-0.1.4}/tests/test_emittance.py +2 -2
- {bax_algorithms-0.1.2 → bax_algorithms-0.1.4}/.gitattributes +0 -0
- {bax_algorithms-0.1.2 → bax_algorithms-0.1.4}/.github/workflows/lint.yml +0 -0
- {bax_algorithms-0.1.2 → bax_algorithms-0.1.4}/.github/workflows/pypi_upload.yml +0 -0
- {bax_algorithms-0.1.2 → bax_algorithms-0.1.4}/.gitignore +0 -0
- {bax_algorithms-0.1.2 → bax_algorithms-0.1.4}/.pre-commit-config.yaml +0 -0
- {bax_algorithms-0.1.2 → bax_algorithms-0.1.4}/LICENSE +0 -0
- {bax_algorithms-0.1.2 → bax_algorithms-0.1.4}/README.md +0 -0
- {bax_algorithms-0.1.2 → bax_algorithms-0.1.4}/bax_algorithms/__init__.py +0 -0
- {bax_algorithms-0.1.2 → bax_algorithms-0.1.4}/bax_algorithms/pathwise/sampling.py +0 -0
- {bax_algorithms-0.1.2 → bax_algorithms-0.1.4}/bax_algorithms/utils.py +0 -0
- {bax_algorithms-0.1.2 → bax_algorithms-0.1.4}/bax_algorithms.egg-info/dependency_links.txt +0 -0
- {bax_algorithms-0.1.2 → bax_algorithms-0.1.4}/bax_algorithms.egg-info/top_level.txt +0 -0
- {bax_algorithms-0.1.2 → bax_algorithms-0.1.4}/examples/pathwise_tutorial.ipynb +0 -0
- {bax_algorithms-0.1.2 → bax_algorithms-0.1.4}/setup.cfg +0 -0
- {bax_algorithms-0.1.2 → bax_algorithms-0.1.4}/tests/test_solenoid_alignment.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: bax_algorithms
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.4
|
|
4
4
|
Summary: Collection of algorithms that can be used in Bayesian Algorithm Execution (BAX) Xopt generators.
|
|
5
5
|
Author: Dylan Kennedy, Sean Gasiorowski, Samuel Klein
|
|
6
6
|
License: Apache License
|
|
@@ -209,13 +209,12 @@ Project-URL: Homepage, https://github.com/xopt-org/bax_algorithms
|
|
|
209
209
|
Keywords: machine learning,accelerator physics
|
|
210
210
|
Classifier: Natural Language :: English
|
|
211
211
|
Classifier: Programming Language :: Python :: 3 :: Only
|
|
212
|
-
Classifier: Programming Language :: Python :: 3.10
|
|
213
212
|
Classifier: Programming Language :: Python :: 3.11
|
|
214
213
|
Classifier: Programming Language :: Python :: 3.12
|
|
215
214
|
Classifier: Programming Language :: Python :: 3.13
|
|
216
215
|
Classifier: Intended Audience :: Science/Research
|
|
217
216
|
Classifier: Topic :: Scientific/Engineering
|
|
218
|
-
Requires-Python: >=3.
|
|
217
|
+
Requires-Python: >=3.11
|
|
219
218
|
Description-Content-Type: text/markdown
|
|
220
219
|
License-File: LICENSE
|
|
221
220
|
Requires-Dist: numpy
|
|
@@ -223,7 +222,7 @@ Requires-Dist: botorch>=0.8.5
|
|
|
223
222
|
Requires-Dist: gpytorch>=1.10
|
|
224
223
|
Requires-Dist: pydantic>=1.10.9
|
|
225
224
|
Requires-Dist: scipy>=1.10.1
|
|
226
|
-
Requires-Dist: xopt>=
|
|
225
|
+
Requires-Dist: xopt>=3.1.2
|
|
227
226
|
Requires-Dist: jupyterlab>=3
|
|
228
227
|
Provides-Extra: dev
|
|
229
228
|
Requires-Dist: pytest; extra == "dev"
|
|
@@ -18,7 +18,7 @@ version_tuple: tuple[int | str, ...]
|
|
|
18
18
|
commit_id: str | None
|
|
19
19
|
__commit_id__: str | None
|
|
20
20
|
|
|
21
|
-
__version__ = version = '0.1.
|
|
22
|
-
__version_tuple__ = version_tuple = (0, 1,
|
|
21
|
+
__version__ = version = '0.1.4'
|
|
22
|
+
__version_tuple__ = version_tuple = (0, 1, 4)
|
|
23
23
|
|
|
24
|
-
__commit_id__ = commit_id = '
|
|
24
|
+
__commit_id__ = commit_id = 'gf750fbf44'
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
from pydantic import Field
|
|
1
|
+
from pydantic import Field, PositiveInt, field_validator, field_serializer
|
|
2
|
+
from typing import Optional, Any
|
|
3
|
+
import ast
|
|
2
4
|
import torch
|
|
3
5
|
from torch import Tensor
|
|
4
6
|
|
|
@@ -7,7 +9,11 @@ from bax_algorithms.pathwise.base import PathwiseOptimization
|
|
|
7
9
|
from bax_algorithms.pathwise.sampling import draw_product_kernel_post_paths
|
|
8
10
|
from botorch.sampling.pathwise.posterior_samplers import draw_matheron_paths
|
|
9
11
|
from botorch.models.model import Model
|
|
10
|
-
from xopt.generators.bayesian.bax.algorithms import
|
|
12
|
+
from xopt.generators.bayesian.bax.algorithms import (
|
|
13
|
+
Algorithm,
|
|
14
|
+
OptimizationAlgorithmResult,
|
|
15
|
+
VirtualMeasurementResult,
|
|
16
|
+
)
|
|
11
17
|
|
|
12
18
|
import numpy as np
|
|
13
19
|
from scipy.optimize import minimize
|
|
@@ -438,39 +444,60 @@ def compute_emit_bmag_quad_scan(
|
|
|
438
444
|
return rv
|
|
439
445
|
|
|
440
446
|
|
|
447
|
+
class VirtualEmittanceMeasurementResult(VirtualMeasurementResult):
|
|
448
|
+
emittance_x: Optional[Tensor] = Field(
|
|
449
|
+
default=None,
|
|
450
|
+
description="The geometric emittance in the x transverse dimension.",
|
|
451
|
+
)
|
|
452
|
+
emittance_y: Optional[Tensor] = Field(
|
|
453
|
+
default=None,
|
|
454
|
+
description="The geometric emittance in the y transverse dimension.",
|
|
455
|
+
)
|
|
456
|
+
bmag_x: Optional[Tensor] = Field(
|
|
457
|
+
default=None, description="The Bmag in the x transverse dimension."
|
|
458
|
+
)
|
|
459
|
+
bmag_y: Optional[Tensor] = Field(
|
|
460
|
+
default=None, description="The Bmag in the y transverse dimension."
|
|
461
|
+
)
|
|
462
|
+
|
|
463
|
+
|
|
441
464
|
class EmittanceAlgorithm(Algorithm):
|
|
442
|
-
|
|
465
|
+
name: str = Field("minimize_emittance", frozen=True)
|
|
466
|
+
x_key: str | None = Field(
|
|
443
467
|
None,
|
|
444
468
|
description="key designating the beamsize squared output in x from evaluate function",
|
|
445
469
|
)
|
|
446
|
-
|
|
470
|
+
|
|
471
|
+
y_key: str | None = Field(
|
|
447
472
|
None,
|
|
448
473
|
description="key designating the beamsize squared output in y from evaluate function",
|
|
449
474
|
)
|
|
450
475
|
energy: float = Field(1.0, description="Beam energy in [eV]")
|
|
451
476
|
q_len: float = Field(
|
|
452
|
-
description="the longitudinal thickness of the measurement quadrupole"
|
|
477
|
+
0.08, description="the longitudinal thickness of the measurement quadrupole"
|
|
453
478
|
)
|
|
454
|
-
rmat_x: Tensor = Field(
|
|
455
|
-
|
|
479
|
+
rmat_x: Tensor | None = Field(
|
|
480
|
+
Tensor([[1.0, 1.0], [0.0, 1.0]]),
|
|
481
|
+
description="2x2 Tensor containing downstream rmat for x dimension",
|
|
456
482
|
)
|
|
457
|
-
rmat_y: Tensor = Field(
|
|
458
|
-
|
|
483
|
+
rmat_y: Tensor | None = Field(
|
|
484
|
+
Tensor([[1.0, 1.0], [0.0, 1.0]]),
|
|
485
|
+
description="2x2 Tensor containing downstream rmat for y dimension",
|
|
459
486
|
)
|
|
460
|
-
twiss0_x: Tensor = Field(
|
|
461
|
-
|
|
462
|
-
description="
|
|
487
|
+
twiss0_x: Tensor | None = Field(
|
|
488
|
+
Tensor([1.0, 0.0]),
|
|
489
|
+
description="List length 2 containing design x-twiss: [beta0_x, alpha0_x] (for bmag)",
|
|
463
490
|
)
|
|
464
|
-
twiss0_y: Tensor = Field(
|
|
465
|
-
|
|
466
|
-
description="
|
|
491
|
+
twiss0_y: Tensor | None = Field(
|
|
492
|
+
Tensor([1.0, 0.0]),
|
|
493
|
+
description="List length 2 containing design y-twiss: [beta0_y, alpha0_y] (for bmag)",
|
|
467
494
|
)
|
|
468
495
|
meas_dim: int = Field(
|
|
469
|
-
|
|
496
|
+
0,
|
|
470
497
|
description="index identifying the measurement quad dimension in the model",
|
|
471
498
|
)
|
|
472
499
|
n_steps_measurement_param: int = Field(
|
|
473
|
-
|
|
500
|
+
5, description="number of steps to use in the virtual measurement scans"
|
|
474
501
|
)
|
|
475
502
|
thin_lens: bool = Field(
|
|
476
503
|
False,
|
|
@@ -480,9 +507,6 @@ class EmittanceAlgorithm(Algorithm):
|
|
|
480
507
|
True,
|
|
481
508
|
description="Whether to multiply the emit by the bmag to get virtual objective.",
|
|
482
509
|
)
|
|
483
|
-
results: dict = Field(
|
|
484
|
-
{}, description="Dictionary to store results from emittance calculcation"
|
|
485
|
-
)
|
|
486
510
|
maxiter_fit: int = Field(
|
|
487
511
|
20, description="Maximum number of iterations in nonlinear emittance fitting."
|
|
488
512
|
)
|
|
@@ -491,6 +515,41 @@ class EmittanceAlgorithm(Algorithm):
|
|
|
491
515
|
description="Whether to retain beamsize values only around the minimum from each scan.",
|
|
492
516
|
)
|
|
493
517
|
|
|
518
|
+
@field_validator("rmat_x", "rmat_y", "twiss0_x", "twiss0_y", mode="before")
|
|
519
|
+
@classmethod
|
|
520
|
+
def validate_tensors(cls, v: Any) -> Tensor:
|
|
521
|
+
"""Accept tensors, (possibly nested) lists/tuples, or their string
|
|
522
|
+
representations (e.g. "[[1.0, 1.0], [0.0, 1.0]]" or "1.0, 0.0") and
|
|
523
|
+
convert them into a double-precision tensor."""
|
|
524
|
+
if isinstance(v, Tensor):
|
|
525
|
+
return v
|
|
526
|
+
if isinstance(v, str):
|
|
527
|
+
stripped = v.strip()
|
|
528
|
+
if stripped.startswith("[") or stripped.startswith("("):
|
|
529
|
+
v = ast.literal_eval(stripped)
|
|
530
|
+
else:
|
|
531
|
+
v = [item for item in stripped.split(",")]
|
|
532
|
+
if isinstance(v, (list, tuple)):
|
|
533
|
+
float_list = cls._to_nested_floats(v)
|
|
534
|
+
return torch.tensor(float_list, dtype=torch.double)
|
|
535
|
+
raise ValueError(f"Cannot convert {v} to a Tensor.")
|
|
536
|
+
|
|
537
|
+
@classmethod
|
|
538
|
+
def _to_nested_floats(cls, v: Any) -> Any:
|
|
539
|
+
"""Recursively convert a (possibly nested) list/tuple to floats,
|
|
540
|
+
preserving the nesting structure."""
|
|
541
|
+
if isinstance(v, (list, tuple)):
|
|
542
|
+
return [cls._to_nested_floats(item) for item in v]
|
|
543
|
+
return float(v)
|
|
544
|
+
|
|
545
|
+
@field_serializer("rmat_x", "rmat_y", "twiss0_x", "twiss0_y")
|
|
546
|
+
def serialize_tensor(self, v: Any) -> Any:
|
|
547
|
+
"""Serialize tensor fields to nested lists so they round-trip through
|
|
548
|
+
JSON/YAML (pydantic otherwise dumps unknown types as ``'torch.Tensor'``)."""
|
|
549
|
+
if isinstance(v, Tensor):
|
|
550
|
+
return v.tolist()
|
|
551
|
+
return v
|
|
552
|
+
|
|
494
553
|
@property
|
|
495
554
|
def x_idx(self) -> int:
|
|
496
555
|
"""
|
|
@@ -518,7 +577,7 @@ class EmittanceAlgorithm(Algorithm):
|
|
|
518
577
|
at which to evaluate the objective.
|
|
519
578
|
bounds: tensor shape (2, n_dim) specifying the upper and lower measurement bounds
|
|
520
579
|
returns:
|
|
521
|
-
|
|
580
|
+
VirtualEmittanceMeasurementResult
|
|
522
581
|
"""
|
|
523
582
|
tuning_idxs = torch.arange(bounds.shape[1])
|
|
524
583
|
tuning_idxs = tuning_idxs[
|
|
@@ -536,21 +595,26 @@ class EmittanceAlgorithm(Algorithm):
|
|
|
536
595
|
)
|
|
537
596
|
|
|
538
597
|
# store virtual measurement results
|
|
539
|
-
result = {
|
|
598
|
+
result = {
|
|
599
|
+
"emittance_x": None,
|
|
600
|
+
"emittance_y": None,
|
|
601
|
+
"bmag_x": None,
|
|
602
|
+
"bmag_y": None,
|
|
603
|
+
}
|
|
540
604
|
if self.x_key:
|
|
541
|
-
result["
|
|
605
|
+
result["emittance_x"] = emit[..., self.x_idx]
|
|
542
606
|
best_bmag_x = torch.min(bmag[..., self.x_idx], dim=-1, keepdim=True)[0]
|
|
543
607
|
result["bmag_x"] = best_bmag_x
|
|
544
|
-
objective = result["
|
|
608
|
+
objective = result["emittance_x"]
|
|
545
609
|
mean_bmag = result["bmag_x"]
|
|
546
610
|
if self.y_key:
|
|
547
|
-
result["
|
|
611
|
+
result["emittance_y"] = emit[..., self.y_idx]
|
|
548
612
|
best_bmag_y = torch.min(bmag[..., self.y_idx], dim=-1, keepdim=True)[0]
|
|
549
613
|
result["bmag_y"] = best_bmag_y
|
|
550
|
-
objective = result["
|
|
614
|
+
objective = result["emittance_y"]
|
|
551
615
|
mean_bmag = result["bmag_y"]
|
|
552
616
|
if self.x_key and self.y_key:
|
|
553
|
-
objective = (result["
|
|
617
|
+
objective = (result["emittance_x"] * result["emittance_y"]).sqrt()
|
|
554
618
|
best_bmag_idcs = torch.min(
|
|
555
619
|
(bmag[..., self.x_idx] * bmag[..., self.y_idx]), dim=-1, keepdim=True
|
|
556
620
|
)[1]
|
|
@@ -562,9 +626,14 @@ class EmittanceAlgorithm(Algorithm):
|
|
|
562
626
|
if self.use_bmag:
|
|
563
627
|
objective *= mean_bmag
|
|
564
628
|
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
629
|
+
algorithm_result = VirtualEmittanceMeasurementResult(
|
|
630
|
+
objective=objective,
|
|
631
|
+
emittance_x=result["emittance_x"],
|
|
632
|
+
emittance_y=result["emittance_y"],
|
|
633
|
+
bmag_x=result["bmag_x"],
|
|
634
|
+
bmag_y=result["bmag_y"],
|
|
635
|
+
)
|
|
636
|
+
return algorithm_result
|
|
568
637
|
|
|
569
638
|
def get_meas_scan_inputs(
|
|
570
639
|
self, x_tuning: Tensor, bounds: Tensor, tkwargs: dict = None
|
|
@@ -758,28 +827,58 @@ class EmittanceAlgorithm(Algorithm):
|
|
|
758
827
|
|
|
759
828
|
|
|
760
829
|
class PathwiseMinimizeEmittance(EmittanceAlgorithm, PathwiseOptimization):
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
sample_functions_list
|
|
830
|
+
name: str = Field("pathwise_minimize_emittance", frozen=True)
|
|
831
|
+
n_batch: PositiveInt = Field(
|
|
832
|
+
1,
|
|
833
|
+
description="Number of sample batches to optimize, with each batch containing self.n_samples",
|
|
834
|
+
)
|
|
835
|
+
|
|
836
|
+
def execute(self, model: Model, bounds: Tensor) -> Tensor:
|
|
837
|
+
best_tuning_inputs_list = []
|
|
838
|
+
best_objective_list = []
|
|
839
|
+
best_scan_inputs_list = []
|
|
840
|
+
best_scan_outputs_list = []
|
|
841
|
+
for i in range(self.n_batch):
|
|
842
|
+
# draw callable sample functions
|
|
843
|
+
sample_functions_list = self.draw_sample_functions_list(model)
|
|
844
|
+
|
|
845
|
+
best_tuning_inputs = self.optimize_samples_funcs_list(
|
|
846
|
+
sample_functions_list, bounds
|
|
847
|
+
)
|
|
848
|
+
best_meas_scan_inputs = self.get_meas_scan_inputs(
|
|
849
|
+
best_tuning_inputs, bounds
|
|
850
|
+
)
|
|
851
|
+
best_meas_scan_outputs = torch.vstack(
|
|
852
|
+
[
|
|
853
|
+
sample_func(best_meas_scan_inputs)
|
|
854
|
+
for sample_func in sample_functions_list
|
|
855
|
+
]
|
|
856
|
+
).T.unsqueeze(0)
|
|
857
|
+
best_result = self.perform_virtual_measurement(
|
|
858
|
+
sample_functions_list, best_meas_scan_inputs[:, :1, :], bounds
|
|
859
|
+
)
|
|
860
|
+
best_tuning_inputs_list += [best_tuning_inputs]
|
|
861
|
+
best_objective_list += [best_result.objective]
|
|
862
|
+
best_scan_inputs_list += [best_meas_scan_inputs]
|
|
863
|
+
best_scan_outputs_list += [best_meas_scan_outputs]
|
|
864
|
+
|
|
865
|
+
input_execution_paths = torch.cat(best_scan_inputs_list)
|
|
866
|
+
output_execution_paths = torch.cat(best_scan_outputs_list)
|
|
867
|
+
best_inputs = torch.cat(best_tuning_inputs_list)
|
|
868
|
+
best_objective = torch.cat(best_objective_list)
|
|
869
|
+
solution_center = best_inputs.mean(dim=0)
|
|
870
|
+
solution_entropy = float(torch.log(best_inputs.std(dim=0) ** 2).sum())
|
|
871
|
+
|
|
872
|
+
algorithm_result = OptimizationAlgorithmResult(
|
|
873
|
+
best_inputs=best_inputs.detach(),
|
|
874
|
+
best_objective=best_objective.detach(),
|
|
875
|
+
input_execution_paths=input_execution_paths.detach(),
|
|
876
|
+
output_execution_paths=output_execution_paths.detach(),
|
|
877
|
+
solution_center=solution_center.detach(),
|
|
878
|
+
solution_entropy=solution_entropy,
|
|
775
879
|
)
|
|
776
|
-
self.results = {}
|
|
777
|
-
self.results["best_inputs"] = best_inputs
|
|
778
|
-
self.results["best_emit"] = best_emit
|
|
779
|
-
self.results["best_bmag"] = best_bmag
|
|
780
|
-
self.results["sample_functions_list"] = sample_functions_list
|
|
781
880
|
|
|
782
|
-
return
|
|
881
|
+
return algorithm_result
|
|
783
882
|
|
|
784
883
|
def draw_sample_functions_list(self, model):
|
|
785
884
|
sample_funcs_list = []
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
# to be added to basic algorithms in Xopt
|
|
2
2
|
|
|
3
|
-
from
|
|
4
|
-
from bax_algorithms.pathwise.optimize import VirtualOptimizer, DifferentialEvolution
|
|
3
|
+
from bax_algorithms.pathwise.optimize import DifferentialEvolution
|
|
5
4
|
from botorch.models.model import Model, ModelList
|
|
6
5
|
from botorch.sampling.pathwise.posterior_samplers import draw_matheron_paths
|
|
7
6
|
from pydantic import Field
|
|
8
|
-
from xopt.generators.bayesian.bax.algorithms import
|
|
7
|
+
from xopt.generators.bayesian.bax.algorithms import (
|
|
8
|
+
Algorithm,
|
|
9
|
+
)
|
|
9
10
|
from torch import Tensor
|
|
10
11
|
import torch
|
|
11
12
|
from typing import List
|
|
@@ -39,54 +40,35 @@ class PathwiseOptimization(Algorithm):
|
|
|
39
40
|
Get the bounds for virtual optimization.
|
|
40
41
|
"""
|
|
41
42
|
|
|
42
|
-
name = "pathwise_optimization"
|
|
43
|
-
optimizer:
|
|
43
|
+
name: str = Field("pathwise_optimization", frozen=True)
|
|
44
|
+
optimizer: DifferentialEvolution = Field(
|
|
44
45
|
DifferentialEvolution(), description="Optimizer for virtual objective."
|
|
45
46
|
)
|
|
46
|
-
results: dict = Field(
|
|
47
|
-
default=None,
|
|
48
|
-
description="dictionary containing algorithm results",
|
|
49
|
-
)
|
|
50
47
|
observable_names_ordered: List[str] = Field(
|
|
51
|
-
default=None,
|
|
52
48
|
description="names of observable models used in this algorithm",
|
|
53
49
|
)
|
|
54
50
|
|
|
55
|
-
@abstractmethod
|
|
56
|
-
def perform_virtual_measurement(
|
|
57
|
-
self,
|
|
58
|
-
model: Model,
|
|
59
|
-
x: Tensor,
|
|
60
|
-
bounds: Tensor,
|
|
61
|
-
n_samples: int = None,
|
|
62
|
-
tkwargs: dict = None,
|
|
63
|
-
) -> dict:
|
|
64
|
-
"""
|
|
65
|
-
Defines how the measurement of the virtual objective should be performed.
|
|
66
|
-
Stores results in a dictionary.
|
|
67
|
-
Returned dictionary must contain key 'objective' containing the virtual objective results.
|
|
68
|
-
"""
|
|
69
|
-
return {"objective": None}
|
|
70
|
-
|
|
71
51
|
def evaluate_virtual_objective(
|
|
72
52
|
self,
|
|
73
53
|
model: Model,
|
|
74
54
|
x: Tensor,
|
|
75
55
|
bounds: Tensor,
|
|
76
|
-
n_samples: int = None,
|
|
77
|
-
tkwargs: dict = None,
|
|
56
|
+
n_samples: int | None = None,
|
|
78
57
|
) -> Tensor:
|
|
79
58
|
"""
|
|
80
59
|
Performs virtual measurement and extracts virtual objective value from resultant dictionary.
|
|
81
60
|
"""
|
|
82
61
|
|
|
83
62
|
measurement_result = self.perform_virtual_measurement(
|
|
84
|
-
model,
|
|
63
|
+
model,
|
|
64
|
+
x,
|
|
65
|
+
bounds,
|
|
66
|
+
n_samples,
|
|
85
67
|
)
|
|
86
68
|
|
|
87
|
-
return measurement_result
|
|
69
|
+
return measurement_result.objective
|
|
88
70
|
|
|
89
|
-
def
|
|
71
|
+
def optimize_samples_funcs_list(
|
|
90
72
|
self, sample_functions_list: List[Callable], bounds: Tensor
|
|
91
73
|
) -> Tensor:
|
|
92
74
|
"""
|
|
@@ -96,7 +78,7 @@ class PathwiseOptimization(Algorithm):
|
|
|
96
78
|
optimization_indeces = self._get_optimization_indeces(bounds)
|
|
97
79
|
|
|
98
80
|
# optimize sample functions
|
|
99
|
-
|
|
81
|
+
best_tuning_inputs = self.optimizer.optimize(
|
|
100
82
|
virtual_objective=self.evaluate_virtual_objective,
|
|
101
83
|
sample_functions_list=sample_functions_list,
|
|
102
84
|
bounds=bounds,
|
|
@@ -104,27 +86,7 @@ class PathwiseOptimization(Algorithm):
|
|
|
104
86
|
n_samples=self.n_samples,
|
|
105
87
|
)
|
|
106
88
|
|
|
107
|
-
return
|
|
108
|
-
|
|
109
|
-
def get_execution_paths(self, model: Model, bounds: Tensor) -> Tensor:
|
|
110
|
-
"""
|
|
111
|
-
Execute algorithm and get execution paths from optimization result.
|
|
112
|
-
"""
|
|
113
|
-
|
|
114
|
-
# draw callable sample functions
|
|
115
|
-
sample_functions_list = self.draw_sample_functions_list(model)
|
|
116
|
-
|
|
117
|
-
best_inputs = self.execute_algorithm(sample_functions_list, bounds)
|
|
118
|
-
best_objective = self.evaluate_virtual_objective(
|
|
119
|
-
sample_functions_list, best_inputs, bounds
|
|
120
|
-
)
|
|
121
|
-
|
|
122
|
-
self.results = {}
|
|
123
|
-
self.results["best_inputs"] = best_inputs
|
|
124
|
-
self.results["best_objective"] = best_objective
|
|
125
|
-
self.results["sample_functions_list"] = sample_functions_list
|
|
126
|
-
|
|
127
|
-
return best_inputs, best_objective, self.results
|
|
89
|
+
return best_tuning_inputs
|
|
128
90
|
|
|
129
91
|
def draw_sample_functions_list(self, model: Model) -> List:
|
|
130
92
|
"""
|
|
@@ -31,6 +31,7 @@ class VirtualOptimizer(XoptBaseModel, ABC):
|
|
|
31
31
|
"""
|
|
32
32
|
Minimizes virtual objective sample functions and returns optimal inputs.
|
|
33
33
|
"""
|
|
34
|
+
raise NotImplementedError("This method should be implemented in subclasses.")
|
|
34
35
|
|
|
35
36
|
@abstractmethod
|
|
36
37
|
def _wrap_virtual_objective(
|
|
@@ -44,6 +45,7 @@ class VirtualOptimizer(XoptBaseModel, ABC):
|
|
|
44
45
|
"""
|
|
45
46
|
Wraps virtual objective function so inputs/outputs are suitable for optimization method.
|
|
46
47
|
"""
|
|
48
|
+
raise NotImplementedError("This method should be implemented in subclasses.")
|
|
47
49
|
|
|
48
50
|
@abstractmethod
|
|
49
51
|
def _get_virtual_optimization_bounds(
|
|
@@ -52,6 +54,7 @@ class VirtualOptimizer(XoptBaseModel, ABC):
|
|
|
52
54
|
"""
|
|
53
55
|
Get bounds for virtual optimization (may not be the same as bounds passed to optimizer).
|
|
54
56
|
"""
|
|
57
|
+
raise NotImplementedError("This method should be implemented in subclasses.")
|
|
55
58
|
|
|
56
59
|
def _get_target_function(
|
|
57
60
|
self,
|
|
@@ -3,26 +3,37 @@ from torch import Tensor
|
|
|
3
3
|
from botorch.models.model import Model
|
|
4
4
|
from pydantic import Field, PositiveInt
|
|
5
5
|
from bax_algorithms.pathwise.base import PathwiseOptimization
|
|
6
|
+
from xopt.generators.bayesian.bax.algorithms import (
|
|
7
|
+
OptimizationAlgorithmResult,
|
|
8
|
+
VirtualMeasurementResult,
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class VirtualAlignmentMeasurementResult(VirtualMeasurementResult):
|
|
13
|
+
misalignment_x: Tensor = Field(
|
|
14
|
+
description="The misalignment in the x transverse dimension."
|
|
15
|
+
)
|
|
16
|
+
misalignment_y: Tensor = Field(
|
|
17
|
+
description="The misalignment in the y transverse dimension."
|
|
18
|
+
)
|
|
6
19
|
|
|
7
20
|
|
|
8
21
|
class PathwiseSolenoidAlignment(PathwiseOptimization):
|
|
9
|
-
|
|
22
|
+
name: str = Field("pathwise_solenoid_alignment", frozen=True)
|
|
23
|
+
x_key: str | None = Field(
|
|
10
24
|
None,
|
|
11
25
|
description="key designating the centroid position in x from evaluate function",
|
|
12
26
|
)
|
|
13
|
-
y_key: str = Field(
|
|
27
|
+
y_key: str | None = Field(
|
|
14
28
|
None,
|
|
15
29
|
description="key designating the centroid poisition in y from evaluate function",
|
|
16
30
|
)
|
|
17
|
-
meas_dim: int = Field(
|
|
31
|
+
meas_dim: int | None = Field(
|
|
18
32
|
None,
|
|
19
33
|
description="index identifying the measurement quad dimension in the model",
|
|
20
34
|
)
|
|
21
35
|
n_steps_measurement_param: int = Field(
|
|
22
|
-
|
|
23
|
-
)
|
|
24
|
-
results: dict = Field(
|
|
25
|
-
{}, description="Dictionary to store results from emittance calculcation"
|
|
36
|
+
5, description="number of steps to use in the virtual measurement scans"
|
|
26
37
|
)
|
|
27
38
|
n_batch: PositiveInt = Field(
|
|
28
39
|
1,
|
|
@@ -46,8 +57,8 @@ class PathwiseSolenoidAlignment(PathwiseOptimization):
|
|
|
46
57
|
return self.observable_names_ordered.index(self.y_key)
|
|
47
58
|
|
|
48
59
|
def perform_virtual_measurement(
|
|
49
|
-
self, model, x, bounds
|
|
50
|
-
):
|
|
60
|
+
self, model: Model, x: Tensor, bounds: Tensor, n_samples: int | None = None
|
|
61
|
+
) -> VirtualAlignmentMeasurementResult:
|
|
51
62
|
"""
|
|
52
63
|
inputs:
|
|
53
64
|
model: a botorch ModelListGP
|
|
@@ -56,7 +67,7 @@ class PathwiseSolenoidAlignment(PathwiseOptimization):
|
|
|
56
67
|
at which to evaluate the objective.
|
|
57
68
|
bounds: tensor shape (2, n_dim) specifying the upper and lower measurement bounds
|
|
58
69
|
returns:
|
|
59
|
-
|
|
70
|
+
VirtualAlignmentMeasurementResult
|
|
60
71
|
"""
|
|
61
72
|
tuning_idxs = torch.arange(bounds.shape[1])
|
|
62
73
|
tuning_idxs = tuning_idxs[
|
|
@@ -69,21 +80,22 @@ class PathwiseSolenoidAlignment(PathwiseOptimization):
|
|
|
69
80
|
model,
|
|
70
81
|
x_tuning,
|
|
71
82
|
bounds,
|
|
72
|
-
tkwargs,
|
|
73
83
|
n_samples,
|
|
74
84
|
)
|
|
75
85
|
|
|
76
86
|
# store virtual measurement results
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
87
|
+
misalignment_x = misalignment[..., self.x_idx]
|
|
88
|
+
misalignment_y = misalignment[..., self.y_idx]
|
|
89
|
+
objective = misalignment_x + misalignment_y
|
|
90
|
+
virtual_alignment_result = VirtualAlignmentMeasurementResult(
|
|
91
|
+
objective=objective,
|
|
92
|
+
misalignment_x=misalignment_x,
|
|
93
|
+
misalignment_y=misalignment_y,
|
|
94
|
+
)
|
|
81
95
|
|
|
82
|
-
return
|
|
96
|
+
return virtual_alignment_result
|
|
83
97
|
|
|
84
|
-
def get_meas_scan_inputs(
|
|
85
|
-
self, x_tuning: Tensor, bounds: Tensor, tkwargs: dict = None
|
|
86
|
-
):
|
|
98
|
+
def get_meas_scan_inputs(self, x_tuning: Tensor, bounds: Tensor) -> Tensor:
|
|
87
99
|
"""
|
|
88
100
|
A function that generates the inputs for virtual emittance measurement scans at the tuning
|
|
89
101
|
configurations specified by x_tuning.
|
|
@@ -104,10 +116,9 @@ class PathwiseSolenoidAlignment(PathwiseOptimization):
|
|
|
104
116
|
|
|
105
117
|
# expand the x tensor to represent quad measurement scans
|
|
106
118
|
# at the locations in tuning parameter space specified by X
|
|
107
|
-
tkwargs = tkwargs if tkwargs else {"dtype": torch.double, "device": "cpu"}
|
|
108
119
|
|
|
109
120
|
x_meas = torch.linspace(
|
|
110
|
-
*bounds.T[self.meas_dim], self.n_steps_measurement_param
|
|
121
|
+
*bounds.T[self.meas_dim], self.n_steps_measurement_param
|
|
111
122
|
)
|
|
112
123
|
|
|
113
124
|
# prepare column of measurement scans coordinates
|
|
@@ -132,7 +143,11 @@ class PathwiseSolenoidAlignment(PathwiseOptimization):
|
|
|
132
143
|
return x
|
|
133
144
|
|
|
134
145
|
def evaluate_posterior_misalignment(
|
|
135
|
-
self,
|
|
146
|
+
self,
|
|
147
|
+
model: Model,
|
|
148
|
+
x_tuning: Tensor,
|
|
149
|
+
bounds: Tensor,
|
|
150
|
+
n_samples: int | None = None,
|
|
136
151
|
):
|
|
137
152
|
"""
|
|
138
153
|
inputs:
|
|
@@ -143,10 +158,9 @@ class PathwiseSolenoidAlignment(PathwiseOptimization):
|
|
|
143
158
|
"""
|
|
144
159
|
assert len(x_tuning.shape) in [2, 3]
|
|
145
160
|
# x_tuning must be shape (n_tuning_configs, n_tuning_dims) or (n_samples, n_tuning_configs, ndim)
|
|
146
|
-
tkwargs = tkwargs if tkwargs else {"dtype": torch.double, "device": "cpu"}
|
|
147
161
|
|
|
148
162
|
x = self.get_meas_scan_inputs(
|
|
149
|
-
x_tuning, bounds
|
|
163
|
+
x_tuning, bounds
|
|
150
164
|
) # result shape n_tuning_configs*n_steps x ndim
|
|
151
165
|
centroid_position = self.evaluate_virtual_observables(model, x, n_samples)
|
|
152
166
|
|
|
@@ -173,19 +187,21 @@ class PathwiseSolenoidAlignment(PathwiseOptimization):
|
|
|
173
187
|
|
|
174
188
|
return misalignment
|
|
175
189
|
|
|
176
|
-
def
|
|
177
|
-
|
|
190
|
+
def execute(self, model: Model, bounds: Tensor) -> OptimizationAlgorithmResult:
|
|
191
|
+
best_tuning_inputs_list = []
|
|
178
192
|
best_objective_list = []
|
|
179
|
-
best_misalignment_list = []
|
|
180
193
|
best_scan_inputs_list = []
|
|
181
194
|
best_scan_outputs_list = []
|
|
182
|
-
|
|
183
195
|
for i in range(self.n_batch):
|
|
184
196
|
# draw callable sample functions
|
|
185
197
|
sample_functions_list = self.draw_sample_functions_list(model)
|
|
186
198
|
|
|
187
|
-
|
|
188
|
-
|
|
199
|
+
best_tuning_inputs = self.optimize_samples_funcs_list(
|
|
200
|
+
sample_functions_list, bounds
|
|
201
|
+
)
|
|
202
|
+
best_meas_scan_inputs = self.get_meas_scan_inputs(
|
|
203
|
+
best_tuning_inputs, bounds
|
|
204
|
+
)
|
|
189
205
|
scan_outputs = [
|
|
190
206
|
sample_func(best_meas_scan_inputs).unsqueeze(-1)
|
|
191
207
|
for sample_func in sample_functions_list
|
|
@@ -194,29 +210,30 @@ class PathwiseSolenoidAlignment(PathwiseOptimization):
|
|
|
194
210
|
best_result = self.perform_virtual_measurement(
|
|
195
211
|
sample_functions_list, best_meas_scan_inputs[:, :1, :], bounds
|
|
196
212
|
)
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
)
|
|
200
|
-
best_inputs_list += [best_inputs]
|
|
201
|
-
best_objective_list += [best_result["objective"]]
|
|
202
|
-
best_misalignment_list += [best_misalignment]
|
|
213
|
+
best_tuning_inputs_list += [best_tuning_inputs]
|
|
214
|
+
best_objective_list += [best_result.objective]
|
|
203
215
|
best_scan_inputs_list += [best_meas_scan_inputs]
|
|
204
216
|
best_scan_outputs_list += [best_meas_scan_outputs]
|
|
205
217
|
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
218
|
+
input_execution_paths = torch.cat(best_scan_inputs_list)
|
|
219
|
+
output_execution_paths = torch.cat(best_scan_outputs_list)
|
|
220
|
+
best_inputs = torch.cat(best_tuning_inputs_list)
|
|
221
|
+
best_objective = torch.cat(best_objective_list)
|
|
222
|
+
solution_center = best_inputs.mean(dim=0)
|
|
223
|
+
solution_entropy = float(torch.log(best_inputs.std(dim=0) ** 2).sum())
|
|
224
|
+
|
|
225
|
+
algorithm_result = OptimizationAlgorithmResult(
|
|
226
|
+
best_inputs=best_inputs.detach(),
|
|
227
|
+
best_objective=best_objective.detach(),
|
|
228
|
+
input_execution_paths=input_execution_paths.detach(),
|
|
229
|
+
output_execution_paths=output_execution_paths.detach(),
|
|
230
|
+
solution_center=solution_center.detach(),
|
|
231
|
+
solution_entropy=solution_entropy,
|
|
217
232
|
)
|
|
218
233
|
|
|
219
|
-
|
|
234
|
+
return algorithm_result
|
|
235
|
+
|
|
236
|
+
def _get_optimization_indeces(self, bounds: Tensor) -> Tensor:
|
|
220
237
|
"""
|
|
221
238
|
Get indeces specifying parameters for virtual objective optimization.
|
|
222
239
|
"""
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import pickle
|
|
1
3
|
import torch
|
|
2
4
|
from matplotlib import pyplot as plt
|
|
3
5
|
from typing import List
|
|
@@ -16,6 +18,7 @@ def visualize_virtual_measurement_result(
|
|
|
16
18
|
reference_point: dict = None,
|
|
17
19
|
n_grid: int = 11,
|
|
18
20
|
n_samples: int = 100,
|
|
21
|
+
show_observations: bool = False,
|
|
19
22
|
kwargs: dict = None,
|
|
20
23
|
result_keys: List[str] = ["objective"],
|
|
21
24
|
) -> tuple:
|
|
@@ -41,11 +44,15 @@ def visualize_virtual_measurement_result(
|
|
|
41
44
|
Number of grid points per dimension used to display the model predictions.
|
|
42
45
|
n_samples : int, optional
|
|
43
46
|
Number of virtual measurement samples to evaluate for each point in the scan.
|
|
47
|
+
show_observations: bool, optional
|
|
48
|
+
Whether to add a scatter plot of the real observations ("partial measurements", i.e. training data)
|
|
49
|
+
to the virtual objective plots.
|
|
44
50
|
kwargs : dict, optional
|
|
45
51
|
Additional keyword arguments for evaluating the virtual measurement.
|
|
46
52
|
|
|
47
53
|
Returns:
|
|
48
54
|
--------
|
|
55
|
+
fig, ax
|
|
49
56
|
The matplotlib figure and axes objects.
|
|
50
57
|
"""
|
|
51
58
|
vocs, data = generator.vocs, generator.data
|
|
@@ -84,7 +91,9 @@ def visualize_virtual_measurement_result(
|
|
|
84
91
|
f"which are not in generator.vocs.variable_names."
|
|
85
92
|
)
|
|
86
93
|
tkwargs = generator.tkwargs
|
|
87
|
-
x = _generate_input_mesh(
|
|
94
|
+
x = _generate_input_mesh(
|
|
95
|
+
vocs, variable_names, data, reference_point, n_grid, tkwargs
|
|
96
|
+
)
|
|
88
97
|
|
|
89
98
|
# get bax observable models and bounds
|
|
90
99
|
bax_model, bounds = get_bax_model_and_bounds(generator)
|
|
@@ -92,8 +101,8 @@ def visualize_virtual_measurement_result(
|
|
|
92
101
|
# get virtual measurement (sample) results
|
|
93
102
|
kwargs = kwargs if kwargs else {}
|
|
94
103
|
measurement_result = generator.algorithm.perform_virtual_measurement(
|
|
95
|
-
bax_model, x, bounds,
|
|
96
|
-
)
|
|
104
|
+
bax_model, x, bounds, n_samples=n_samples, **kwargs
|
|
105
|
+
).model_dump()
|
|
97
106
|
|
|
98
107
|
# create figure and subplots
|
|
99
108
|
figsize = (4 * dim_x, 3 * len(result_keys))
|
|
@@ -129,6 +138,12 @@ def visualize_virtual_measurement_result(
|
|
|
129
138
|
alpha=0.5,
|
|
130
139
|
label="95% C.I.",
|
|
131
140
|
)
|
|
141
|
+
if show_observations:
|
|
142
|
+
x_obs = generator.data[variable_names[0]].to_numpy()
|
|
143
|
+
label = "Partial Measurements"
|
|
144
|
+
for val in x_obs:
|
|
145
|
+
ax_i.axvline(val, ymax=0.1, c="C1", label=label)
|
|
146
|
+
label = None
|
|
132
147
|
ax_i.legend()
|
|
133
148
|
ax_i.set_ylabel(key)
|
|
134
149
|
ax_i.set_xlabel(variable_names[0])
|
|
@@ -160,6 +175,12 @@ def visualize_virtual_measurement_result(
|
|
|
160
175
|
rasterized=True,
|
|
161
176
|
)
|
|
162
177
|
|
|
178
|
+
if show_observations:
|
|
179
|
+
x_obs = generator.data[variable_names].to_numpy()
|
|
180
|
+
label = "Partial Measurements"
|
|
181
|
+
ax_ij.scatter(x=x_obs[:, 0], y=x_obs[:, 1], c="C1", label=label)
|
|
182
|
+
ax_ij.legend()
|
|
183
|
+
|
|
163
184
|
from mpl_toolkits.axes_grid1 import make_axes_locatable # lazy import
|
|
164
185
|
|
|
165
186
|
divider = make_axes_locatable(ax_ij)
|
|
@@ -172,3 +193,153 @@ def visualize_virtual_measurement_result(
|
|
|
172
193
|
|
|
173
194
|
fig.tight_layout()
|
|
174
195
|
return fig, ax
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
def plot_bax_objective_convergence(
|
|
199
|
+
generator: BaxGenerator,
|
|
200
|
+
) -> tuple:
|
|
201
|
+
"""
|
|
202
|
+
Plots the algorithm sample objective optimization results from each step of BAX.
|
|
203
|
+
|
|
204
|
+
Parameters
|
|
205
|
+
----------
|
|
206
|
+
generator : BaxGenerator
|
|
207
|
+
|
|
208
|
+
Returns:
|
|
209
|
+
--------
|
|
210
|
+
fig, ax
|
|
211
|
+
The matplotlib figure and axes objects.
|
|
212
|
+
"""
|
|
213
|
+
file_prefix = os.path.basename(generator.algorithm_results_file) + "_"
|
|
214
|
+
file_ext = ".pkl"
|
|
215
|
+
prefix_len = len(file_prefix)
|
|
216
|
+
ext_len = len(file_ext)
|
|
217
|
+
directory = os.path.dirname(os.path.abspath(generator.algorithm_results_file))
|
|
218
|
+
file_names = [
|
|
219
|
+
file_name
|
|
220
|
+
for file_name in os.listdir(directory)
|
|
221
|
+
if file_name.startswith(file_prefix)
|
|
222
|
+
]
|
|
223
|
+
file_names = sorted(file_names, key=lambda x: int(x[prefix_len:-ext_len]))
|
|
224
|
+
file_paths = [os.path.join(directory, file_name) for file_name in file_names]
|
|
225
|
+
|
|
226
|
+
results_dicts = []
|
|
227
|
+
aggregated_results_dict = {}
|
|
228
|
+
for file_path in file_paths:
|
|
229
|
+
with open(file_path, "rb") as fpath:
|
|
230
|
+
loaded_dict = pickle.load(fpath)
|
|
231
|
+
results_dicts += [loaded_dict]
|
|
232
|
+
aggregated_results_dict["best_objective"] = torch.stack(
|
|
233
|
+
[res["best_objective"] for res in results_dicts]
|
|
234
|
+
)
|
|
235
|
+
|
|
236
|
+
# plot the sample objective optima
|
|
237
|
+
objs = aggregated_results_dict["best_objective"].squeeze(-1).squeeze(-1)
|
|
238
|
+
iters = range(objs.shape[0])
|
|
239
|
+
med = objs.quantile(0.5, dim=1).detach()
|
|
240
|
+
upper = objs.quantile(0.975, dim=1).detach()
|
|
241
|
+
lower = objs.quantile(0.025, dim=1).detach()
|
|
242
|
+
|
|
243
|
+
fig, ax = plt.subplots(1, 1)
|
|
244
|
+
ax.set_title("BAX Solutions: Objective")
|
|
245
|
+
ax.plot(iters, med, label="median")
|
|
246
|
+
ax.fill_between(iters, lower, upper, alpha=0.5, label="95% C.I.")
|
|
247
|
+
ax.legend(loc="upper left")
|
|
248
|
+
ax.set_xlabel("iteration")
|
|
249
|
+
ax.set_ylabel("Objective")
|
|
250
|
+
|
|
251
|
+
fig.set_size_inches(6, 3)
|
|
252
|
+
fig.tight_layout()
|
|
253
|
+
|
|
254
|
+
return fig, ax
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
def plot_bax_input_convergence(
|
|
258
|
+
generator: BaxGenerator,
|
|
259
|
+
) -> tuple:
|
|
260
|
+
"""
|
|
261
|
+
Plots the algorithm sample input optimization results from each step of BAX.
|
|
262
|
+
|
|
263
|
+
Parameters
|
|
264
|
+
----------
|
|
265
|
+
generator : BaxGenerator
|
|
266
|
+
|
|
267
|
+
Returns:
|
|
268
|
+
--------
|
|
269
|
+
fig, ax
|
|
270
|
+
The matplotlib figure and axes objects.
|
|
271
|
+
"""
|
|
272
|
+
file_prefix = os.path.basename(generator.algorithm_results_file) + "_"
|
|
273
|
+
file_ext = ".pkl"
|
|
274
|
+
prefix_len = len(file_prefix)
|
|
275
|
+
ext_len = len(file_ext)
|
|
276
|
+
directory = os.path.dirname(os.path.abspath(generator.algorithm_results_file))
|
|
277
|
+
file_names = [
|
|
278
|
+
file_name
|
|
279
|
+
for file_name in os.listdir(directory)
|
|
280
|
+
if file_name.startswith(file_prefix)
|
|
281
|
+
]
|
|
282
|
+
file_names = sorted(file_names, key=lambda x: int(x[prefix_len:-ext_len]))
|
|
283
|
+
file_paths = [os.path.join(directory, file_name) for file_name in file_names]
|
|
284
|
+
|
|
285
|
+
results_dicts = []
|
|
286
|
+
aggregated_results_dict = {}
|
|
287
|
+
for file_path in file_paths:
|
|
288
|
+
with open(file_path, "rb") as fpath:
|
|
289
|
+
loaded_dict = pickle.load(fpath)
|
|
290
|
+
results_dicts += [loaded_dict]
|
|
291
|
+
aggregated_results_dict["best_inputs"] = torch.stack(
|
|
292
|
+
[res["best_inputs"] for res in results_dicts]
|
|
293
|
+
)
|
|
294
|
+
|
|
295
|
+
# plot the sample input optima
|
|
296
|
+
solutions = aggregated_results_dict["best_inputs"]
|
|
297
|
+
solutions = solutions.squeeze(-2)
|
|
298
|
+
iters = range(solutions.shape[0])
|
|
299
|
+
if hasattr(generator.algorithm, "_get_optimization_indeces") and callable(
|
|
300
|
+
getattr(generator.algorithm, "_get_optimization_indeces")
|
|
301
|
+
):
|
|
302
|
+
variable_indeces = generator.algorithm._get_optimization_indeces(
|
|
303
|
+
generator._get_optimization_bounds()
|
|
304
|
+
)
|
|
305
|
+
ndim = len(variable_indeces)
|
|
306
|
+
else:
|
|
307
|
+
ndim = solutions.shape[-1]
|
|
308
|
+
variable_indeces = range(ndim)
|
|
309
|
+
fig, ax = plt.subplots(ndim, 1, sharex=True)
|
|
310
|
+
if ndim == 1:
|
|
311
|
+
ax = [ax]
|
|
312
|
+
for i, j in enumerate(variable_indeces):
|
|
313
|
+
med = solutions[..., i].quantile(0.5, dim=1)
|
|
314
|
+
upper = solutions[..., i].quantile(0.975, dim=1)
|
|
315
|
+
lower = solutions[..., i].quantile(0.025, dim=1)
|
|
316
|
+
ax_i = ax[i]
|
|
317
|
+
if i == 0:
|
|
318
|
+
ax_i.set_title("BAX Solutions: Input(s)")
|
|
319
|
+
ax_i.plot(iters, med, label="median")
|
|
320
|
+
ax_i.fill_between(iters, lower, upper, alpha=0.5, label="95% C.I.")
|
|
321
|
+
var_name = list(generator.vocs.variables.keys())[j]
|
|
322
|
+
bbox = dict(
|
|
323
|
+
boxstyle="round,pad=0.5",
|
|
324
|
+
facecolor="lightgray",
|
|
325
|
+
edgecolor="silver",
|
|
326
|
+
alpha=0.7,
|
|
327
|
+
)
|
|
328
|
+
ax_i.text(
|
|
329
|
+
0.0275,
|
|
330
|
+
0.06,
|
|
331
|
+
var_name,
|
|
332
|
+
transform=ax_i.transAxes,
|
|
333
|
+
fontsize=12,
|
|
334
|
+
verticalalignment="bottom",
|
|
335
|
+
horizontalalignment="left",
|
|
336
|
+
bbox=bbox,
|
|
337
|
+
)
|
|
338
|
+
ax_i.set_ylim(generator.vocs.variables[var_name].domain)
|
|
339
|
+
ax_i.legend(loc="upper left")
|
|
340
|
+
ax_i.set_xlabel("iteration")
|
|
341
|
+
|
|
342
|
+
fig.set_size_inches(6, 3 * ndim)
|
|
343
|
+
fig.tight_layout()
|
|
344
|
+
|
|
345
|
+
return fig, ax
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: bax_algorithms
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.4
|
|
4
4
|
Summary: Collection of algorithms that can be used in Bayesian Algorithm Execution (BAX) Xopt generators.
|
|
5
5
|
Author: Dylan Kennedy, Sean Gasiorowski, Samuel Klein
|
|
6
6
|
License: Apache License
|
|
@@ -209,13 +209,12 @@ Project-URL: Homepage, https://github.com/xopt-org/bax_algorithms
|
|
|
209
209
|
Keywords: machine learning,accelerator physics
|
|
210
210
|
Classifier: Natural Language :: English
|
|
211
211
|
Classifier: Programming Language :: Python :: 3 :: Only
|
|
212
|
-
Classifier: Programming Language :: Python :: 3.10
|
|
213
212
|
Classifier: Programming Language :: Python :: 3.11
|
|
214
213
|
Classifier: Programming Language :: Python :: 3.12
|
|
215
214
|
Classifier: Programming Language :: Python :: 3.13
|
|
216
215
|
Classifier: Intended Audience :: Science/Research
|
|
217
216
|
Classifier: Topic :: Scientific/Engineering
|
|
218
|
-
Requires-Python: >=3.
|
|
217
|
+
Requires-Python: >=3.11
|
|
219
218
|
Description-Content-Type: text/markdown
|
|
220
219
|
License-File: LICENSE
|
|
221
220
|
Requires-Dist: numpy
|
|
@@ -223,7 +222,7 @@ Requires-Dist: botorch>=0.8.5
|
|
|
223
222
|
Requires-Dist: gpytorch>=1.10
|
|
224
223
|
Requires-Dist: pydantic>=1.10.9
|
|
225
224
|
Requires-Dist: scipy>=1.10.1
|
|
226
|
-
Requires-Dist: xopt>=
|
|
225
|
+
Requires-Dist: xopt>=3.1.2
|
|
227
226
|
Requires-Dist: jupyterlab>=3
|
|
228
227
|
Provides-Extra: dev
|
|
229
228
|
Requires-Dist: pytest; extra == "dev"
|
|
@@ -17,6 +17,8 @@ bax_algorithms.egg-info/PKG-INFO
|
|
|
17
17
|
bax_algorithms.egg-info/SOURCES.txt
|
|
18
18
|
bax_algorithms.egg-info/dependency_links.txt
|
|
19
19
|
bax_algorithms.egg-info/requires.txt
|
|
20
|
+
bax_algorithms.egg-info/scm_file_list.json
|
|
21
|
+
bax_algorithms.egg-info/scm_version.json
|
|
20
22
|
bax_algorithms.egg-info/top_level.txt
|
|
21
23
|
bax_algorithms/pathwise/base.py
|
|
22
24
|
bax_algorithms/pathwise/optimize.py
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"files": [
|
|
3
|
+
".gitattributes",
|
|
4
|
+
".pre-commit-config.yaml",
|
|
5
|
+
"pyproject.toml",
|
|
6
|
+
"LICENSE",
|
|
7
|
+
"README.md",
|
|
8
|
+
".gitignore",
|
|
9
|
+
".github/workflows/lint.yml",
|
|
10
|
+
".github/workflows/tests.yml",
|
|
11
|
+
".github/workflows/pypi_upload.yml",
|
|
12
|
+
"examples/emittance.ipynb",
|
|
13
|
+
"examples/solenoid_alignment.ipynb",
|
|
14
|
+
"examples/pathwise_tutorial.ipynb",
|
|
15
|
+
"tests/test_emittance.py",
|
|
16
|
+
"tests/test_solenoid_alignment.py",
|
|
17
|
+
"bax_algorithms/__init__.py",
|
|
18
|
+
"bax_algorithms/visualize.py",
|
|
19
|
+
"bax_algorithms/solenoid_alignment.py",
|
|
20
|
+
"bax_algorithms/emittance.py",
|
|
21
|
+
"bax_algorithms/utils.py",
|
|
22
|
+
"bax_algorithms/pathwise/sampling.py",
|
|
23
|
+
"bax_algorithms/pathwise/optimize.py",
|
|
24
|
+
"bax_algorithms/pathwise/base.py"
|
|
25
|
+
]
|
|
26
|
+
}
|
|
@@ -196,7 +196,7 @@
|
|
|
196
196
|
"\n",
|
|
197
197
|
"print(\"variable_names =\", vocs.variable_names)\n",
|
|
198
198
|
"print(\"meas_param =\", \"'\" + meas_param + \"'\")\n",
|
|
199
|
-
"print(\"domain =\\n\", vocs.bounds
|
|
199
|
+
"print(\"domain =\\n\", vocs.bounds)"
|
|
200
200
|
]
|
|
201
201
|
},
|
|
202
202
|
{
|
|
@@ -267,7 +267,7 @@
|
|
|
267
267
|
"outputs": [],
|
|
268
268
|
"source": [
|
|
269
269
|
"x = torch.zeros(100, ndim)\n",
|
|
270
|
-
"x[:, 0] = torch.linspace(*vocs.bounds
|
|
270
|
+
"x[:, 0] = torch.linspace(*vocs.bounds[0], 100)\n",
|
|
271
271
|
"plt.plot(x[:, 0], eval_true_emittance(x, use_bmag=use_bmag), c=\"k\")"
|
|
272
272
|
]
|
|
273
273
|
},
|
|
@@ -445,16 +445,6 @@
|
|
|
445
445
|
")"
|
|
446
446
|
]
|
|
447
447
|
},
|
|
448
|
-
{
|
|
449
|
-
"cell_type": "code",
|
|
450
|
-
"execution_count": null,
|
|
451
|
-
"id": "a2915525-0490-4749-8439-1df1abe55d7c",
|
|
452
|
-
"metadata": {},
|
|
453
|
-
"outputs": [],
|
|
454
|
-
"source": [
|
|
455
|
-
"X.generator.algorithm.results[\"best_inputs\"]"
|
|
456
|
-
]
|
|
457
|
-
},
|
|
458
448
|
{
|
|
459
449
|
"cell_type": "code",
|
|
460
450
|
"execution_count": null,
|
|
@@ -471,12 +461,12 @@
|
|
|
471
461
|
" reference_point=reference_point,\n",
|
|
472
462
|
" n_grid=100,\n",
|
|
473
463
|
" n_samples=100,\n",
|
|
474
|
-
" result_keys=[\"objective\", \"
|
|
464
|
+
" result_keys=[\"objective\", \"emittance_x\", \"emittance_y\", \"bmag_x\", \"bmag_y\"],\n",
|
|
475
465
|
" # show_samples=True,\n",
|
|
476
466
|
")\n",
|
|
477
467
|
"print(time.time() - start)\n",
|
|
478
468
|
"x = torch.zeros(100, ndim)\n",
|
|
479
|
-
"x[:, 0] = torch.linspace(*vocs.bounds
|
|
469
|
+
"x[:, 0] = torch.linspace(*vocs.bounds[0], 100)\n",
|
|
480
470
|
"ax[0].plot(x[:, 0], eval_true_emittance(x, use_bmag=use_bmag), c=\"k\")\n",
|
|
481
471
|
"# plt.ylim(top=0.01)"
|
|
482
472
|
]
|
|
@@ -567,12 +557,12 @@
|
|
|
567
557
|
" reference_point=reference_point,\n",
|
|
568
558
|
" n_grid=100,\n",
|
|
569
559
|
" n_samples=100,\n",
|
|
570
|
-
" result_keys=[\"objective\", \"
|
|
560
|
+
" result_keys=[\"objective\", \"emittance_x\", \"emittance_y\", \"bmag_x\", \"bmag_y\"],\n",
|
|
571
561
|
" # show_samples=True,\n",
|
|
572
562
|
")\n",
|
|
573
563
|
"print(time.time() - start)\n",
|
|
574
564
|
"x = torch.zeros(100, ndim)\n",
|
|
575
|
-
"x[:, 0] = torch.linspace(*vocs.bounds
|
|
565
|
+
"x[:, 0] = torch.linspace(*vocs.bounds[0], 100)\n",
|
|
576
566
|
"ax[0].plot(x[:, 0], eval_true_emittance(x, use_bmag=use_bmag), c=\"k\")"
|
|
577
567
|
]
|
|
578
568
|
},
|
|
@@ -592,7 +582,7 @@
|
|
|
592
582
|
" reference_point=reference_point,\n",
|
|
593
583
|
" n_grid=15,\n",
|
|
594
584
|
" n_samples=100,\n",
|
|
595
|
-
" result_keys=[\"objective\", \"
|
|
585
|
+
" result_keys=[\"objective\", \"emittance_x\", \"emittance_y\", \"bmag_x\", \"bmag_y\"],\n",
|
|
596
586
|
" # show_samples=True,\n",
|
|
597
587
|
")\n",
|
|
598
588
|
"print(time.time() - start)"
|
|
@@ -627,28 +617,6 @@
|
|
|
627
617
|
"outputs": [],
|
|
628
618
|
"source": []
|
|
629
619
|
},
|
|
630
|
-
{
|
|
631
|
-
"cell_type": "code",
|
|
632
|
-
"execution_count": null,
|
|
633
|
-
"id": "c2bbf136-7f1e-4d6f-964c-f0a9ed634cde",
|
|
634
|
-
"metadata": {},
|
|
635
|
-
"outputs": [],
|
|
636
|
-
"source": [
|
|
637
|
-
"from bax_algorithms.utils import uniform_random_sample_in_bounds\n",
|
|
638
|
-
"from bax_algorithms.utils import get_bax_model_and_bounds\n",
|
|
639
|
-
"\n",
|
|
640
|
-
"bax_model, bounds = get_bax_model_and_bounds(X.generator)\n",
|
|
641
|
-
"x0 = uniform_random_sample_in_bounds(3000, bounds)\n",
|
|
642
|
-
"start = time.time()\n",
|
|
643
|
-
"algo.n_steps_measurement_param = 11\n",
|
|
644
|
-
"algo.evaluate_virtual_objective(\n",
|
|
645
|
-
" X.generator.algorithm.results[\"sample_functions_list\"],\n",
|
|
646
|
-
" x0.reshape(n_samples, 1000, -1),\n",
|
|
647
|
-
" bounds,\n",
|
|
648
|
-
")\n",
|
|
649
|
-
"print(time.time() - start)"
|
|
650
|
-
]
|
|
651
|
-
},
|
|
652
620
|
{
|
|
653
621
|
"cell_type": "code",
|
|
654
622
|
"execution_count": null,
|
|
@@ -231,16 +231,6 @@
|
|
|
231
231
|
"X.data"
|
|
232
232
|
]
|
|
233
233
|
},
|
|
234
|
-
{
|
|
235
|
-
"cell_type": "code",
|
|
236
|
-
"execution_count": null,
|
|
237
|
-
"id": "235e773e-d35e-4916-a063-48692db44988",
|
|
238
|
-
"metadata": {},
|
|
239
|
-
"outputs": [],
|
|
240
|
-
"source": [
|
|
241
|
-
"X.generator.algorithm.results[\"best_inputs\"]"
|
|
242
|
-
]
|
|
243
|
-
},
|
|
244
234
|
{
|
|
245
235
|
"cell_type": "code",
|
|
246
236
|
"execution_count": null,
|
|
@@ -448,6 +438,16 @@
|
|
|
448
438
|
"id": "6473cd0e-bd20-4c64-953f-e34c0dcd4c46",
|
|
449
439
|
"metadata": {},
|
|
450
440
|
"outputs": [],
|
|
441
|
+
"source": [
|
|
442
|
+
"X.generator.vocs.bounds"
|
|
443
|
+
]
|
|
444
|
+
},
|
|
445
|
+
{
|
|
446
|
+
"cell_type": "code",
|
|
447
|
+
"execution_count": null,
|
|
448
|
+
"id": "d381a0d4-3f83-432e-a2e2-4ce63150ec07",
|
|
449
|
+
"metadata": {},
|
|
450
|
+
"outputs": [],
|
|
451
451
|
"source": []
|
|
452
452
|
}
|
|
453
453
|
],
|
|
@@ -15,21 +15,20 @@ keywords = ["machine learning", "accelerator physics"]
|
|
|
15
15
|
classifiers = [
|
|
16
16
|
"Natural Language :: English",
|
|
17
17
|
"Programming Language :: Python :: 3 :: Only",
|
|
18
|
-
"Programming Language :: Python :: 3.10",
|
|
19
18
|
"Programming Language :: Python :: 3.11",
|
|
20
19
|
"Programming Language :: Python :: 3.12",
|
|
21
20
|
"Programming Language :: Python :: 3.13",
|
|
22
21
|
"Intended Audience :: Science/Research",
|
|
23
22
|
"Topic :: Scientific/Engineering",
|
|
24
23
|
]
|
|
25
|
-
requires-python = ">=3.
|
|
24
|
+
requires-python = ">=3.11"
|
|
26
25
|
dependencies = [
|
|
27
26
|
"numpy",
|
|
28
27
|
"botorch>=0.8.5",
|
|
29
28
|
"gpytorch>=1.10",
|
|
30
29
|
"pydantic>=1.10.9",
|
|
31
30
|
"scipy>=1.10.1",
|
|
32
|
-
"xopt>=
|
|
31
|
+
"xopt>=3.1.2",
|
|
33
32
|
"jupyterlab>=3",
|
|
34
33
|
]
|
|
35
34
|
dynamic = ["version"]
|
|
@@ -106,7 +106,7 @@ class TestEmittanceBax:
|
|
|
106
106
|
reference_point=self.reference_point,
|
|
107
107
|
n_grid=100,
|
|
108
108
|
n_samples=100,
|
|
109
|
-
result_keys=["objective", "
|
|
109
|
+
result_keys=["objective", "emittance_x", "emittance_y", "bmag_x", "bmag_y"],
|
|
110
110
|
)
|
|
111
111
|
|
|
112
112
|
def test_visualize_virtual_measurement_result_2d(self):
|
|
@@ -116,5 +116,5 @@ class TestEmittanceBax:
|
|
|
116
116
|
reference_point=self.reference_point,
|
|
117
117
|
n_grid=10,
|
|
118
118
|
n_samples=100,
|
|
119
|
-
result_keys=["objective", "
|
|
119
|
+
result_keys=["objective", "emittance_x", "emittance_y", "bmag_x", "bmag_y"],
|
|
120
120
|
)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|