Skip to content

Commit

Permalink
clean up after rebasing
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Morris committed Oct 24, 2023
1 parent 85d92fe commit 648337a
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 384 deletions.
6 changes: 0 additions & 6 deletions bloptools/bayesian/acquisition/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
<<<<<<< HEAD
from .agent import * # noqa F401
from .devices import * # noqa F401
from .objective import * # noqa F401
=======
import os

import yaml
Expand Down Expand Up @@ -132,4 +127,3 @@ def get_acquisition_function(agent, acq_func_identifier="qei", return_metadata=T
acq_func_meta = {"name": acq_func_name, "args": {}}

return (acq_func, acq_func_meta) if return_metadata else acq_func
>>>>>>> 39a579f (make sure DOF bounds are cast to floats)
52 changes: 17 additions & 35 deletions bloptools/bayesian/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def tell(self, new_table=None, append=True, train=True, **kwargs):
"""
Inform the agent about new inputs and targets for the model.
If run with no arguments, it will just reconstruct all the models.
If run with no arguments, it will just reconstruct all the models.
"""

new_table = pd.DataFrame() if new_table is None else new_table
Expand Down Expand Up @@ -213,7 +213,7 @@ def ask(self, acq_func_identifier="qei", n=1, route=True, sequential=True, **acq
NUM_RESTARTS = 8
RAW_SAMPLES = 1024

candidates, acqf_objective = botorch.optim.optimize_acqf(
candidates, acqf_obj = botorch.optim.optimize_acqf(
acq_function=acq_func,
bounds=self.acquisition_function_bounds,
q=n,
Expand All @@ -226,43 +226,44 @@ def ask(self, acq_func_identifier="qei", n=1, route=True, sequential=True, **acq

active_dofs_are_read_only = np.array([dof.read_only for dof in self.dofs.subset(active=True)])

acquisition_X = x[..., ~active_dofs_are_read_only]
acq_points = x[..., ~active_dofs_are_read_only]
read_only_X = x[..., active_dofs_are_read_only]
acq_func_meta["read_only_values"] = read_only_X

else:

acqf_objective = None
acqf_obj = None

if acq_func_name == "random":
acquisition_X = torch.rand()
acq_points = torch.rand()
acq_func_meta = {"name": "random", "args": {}}

if acq_func_name == "quasi-random":
acquisition_X = self._subset_inputs_sampler(n=n, active=True, read_only=False).squeeze(1).numpy()
acq_points = self._subset_inputs_sampler(n=n, active=True, read_only=False).squeeze(1).numpy()
acq_func_meta = {"name": "quasi-random", "args": {}}

elif acq_func_name == "grid":
n_active_dims = len(self.dofs.subset(active=True, read_only=False))
acquisition_X = self.test_inputs_grid(max_inputs=n).reshape(-1, n_active_dims).numpy()
acq_points = self.test_inputs_grid(max_inputs=n).reshape(-1, n_active_dims).numpy()
acq_func_meta = {"name": "grid", "args": {}}

else:
raise ValueError()

# define dummy acqf objective
acqf_objective = None
acqf_obj = None

acq_func_meta["duration"] = duration = ttime.monotonic() - start_time

if self.verbose:
print(f"found points {acquisition_X} with acqf {acq_func_meta['name']} in {duration:.01f} seconds (obj = {acqf_objective})")
print(
f"found points {acq_points} with acqf {acq_func_meta['name']} in {duration:.01f} seconds (obj = {acqf_obj})"
)

if route and n > 1:
routing_index = utils.route(self.dofs.subset(active=True, read_only=False).readback, acquisition_X)
acquisition_X = acquisition_X[routing_index]
routing_index = utils.route(self.dofs.subset(active=True, read_only=False).readback, acq_points)
acq_points = acq_points[routing_index]

return acquisition_X, acq_func_meta
return acq_points, acq_func_meta

def acquire(self, acquisition_inputs):
"""
Expand All @@ -272,7 +273,7 @@ def acquire(self, acquisition_inputs):
"""
try:
acquisition_devices = self.dofs.subset(active=True, read_only=False).devices
#read_only_devices = self.dofs.subset(active=True, read_only=True).devices
# read_only_devices = self.dofs.subset(active=True, read_only=True).devices

# the acquisition plan always takes as arguments:
# (things to move, where to move them, things to trigger once you get there)
Expand Down Expand Up @@ -356,12 +357,12 @@ def reset(self):
def benchmark(
self, output_dir="./", runs=16, n_init=64, learning_kwargs_list=[{"acq_func": "qei", "n": 4, "iterations": 16}]
):
cache_limits = {dof.name:dof.limits for dof in self.dofs}
cache_limits = {dof.name: dof.limits for dof in self.dofs}

for run in range(runs):
for dof in self.dofs:
offset = 0.25 * np.ptp(dof.limits) * np.random.uniform(low=-1, high=1)
dof.limits = (dof.limits[0] + offset, dof.limits[1] + offset)
dof.limits = (cache_limits[dof.name] + offset, cache_limits[dof.name] + offset)

self.reset()

Expand Down Expand Up @@ -470,28 +471,13 @@ def acquisition_function_bounds(self):
"""
Returns a (2, n_active_dof) array of bounds for the acquisition function
"""
<<<<<<< HEAD
<<<<<<< HEAD
=======
>>>>>>> 6fffe33 (work at ATF on Oct 12)
active_dofs = self.dofs.subset(active=True)

acq_func_lower_bounds = [dof.lower_limit if not dof.read_only else dof.readback for dof in active_dofs]
acq_func_upper_bounds = [dof.upper_limit if not dof.read_only else dof.readback for dof in active_dofs]
<<<<<<< HEAD
=======
acq_func_lower_bounds = [dof.lower_limit if not dof.read_only else dof.readback for dof in self.dofs]
acq_func_upper_bounds = [dof.upper_limit if not dof.read_only else dof.readback for dof in self.dofs]
>>>>>>> 39a579f (make sure DOF bounds are cast to floats)
=======
>>>>>>> 6fffe33 (work at ATF on Oct 12)

return torch.tensor(np.vstack([acq_func_lower_bounds, acq_func_upper_bounds]), dtype=torch.double)

return torch.tensor(
[dof.limits if not dof.read_only else tuple(2 * [dof.readback]) for dof in self.dofs.subset(active=True)]
).T

# @property
# def num_objectives(self):
# return len(self.objectives)
Expand Down Expand Up @@ -669,8 +655,4 @@ def plot_validity(self, **kwargs):
plotting._plot_valid_many_dofs(self, **kwargs)

def plot_history(self, **kwargs):
<<<<<<< HEAD
plotting._plot_history(self, **kwargs)
=======
plotting._plot_history(self, **kwargs)
>>>>>>> 39a579f (make sure DOF bounds are cast to floats)
15 changes: 1 addition & 14 deletions bloptools/bayesian/objective.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,8 @@

numeric = Union[float, int]

DEFAULT_MINIMUM_SNR = 2e1
<<<<<<< HEAD
<<<<<<< HEAD
OBJ_FIELDS = ["name", "key", "limits", "weight", "minimize", "log", "noise"]

=======
DEFAULT_MINIMUM_SNR = 1e1
OBJ_FIELDS = ["name", "key", "limits", "weight", "minimize", "log"]
>>>>>>> 39a579f (make sure DOF bounds are cast to floats)
=======
OBJ_FIELDS = ["name", "key", "limits", "weight", "minimize", "log", "noise"]
>>>>>>> 6fffe33 (work at ATF on Oct 12)


class DuplicateKeyError(ValueError):
Expand Down Expand Up @@ -73,10 +64,6 @@ def __repr__(self):
@property
def noise(self):
return self.model.likelihood.noise.item() if hasattr(self, "model") else None
<<<<<<< HEAD

=======
>>>>>>> 6fffe33 (work at ATF on Oct 12)


class ObjectiveList(Sequence):
Expand Down
27 changes: 1 addition & 26 deletions bloptools/bayesian/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,9 @@ def _plot_objs_many_dofs(agent, axes=[0, 1], shading="nearest", cmap=DEFAULT_COL
)

agent.obj_axes = np.atleast_2d(agent.obj_axes)
<<<<<<< HEAD
<<<<<<< HEAD

x_dof, y_dof = plottable_dofs[axes[0]], plottable_dofs[axes[1]]
=======

x_dof, y_dof = agent.dofs.subset(active=True)[axes[0]], agent.dofs.subset(active=True)[axes[1]]
>>>>>>> 6fffe33 (work at ATF on Oct 12)
=======

x_dof, y_dof = plottable_dofs[axes[0]], plottable_dofs[axes[1]]
>>>>>>> 05b8e73 (fixed shapes in grid plots)
x_values = agent.table.loc[:, x_dof.device.name].values
y_values = agent.table.loc[:, y_dof.device.name].values

Expand Down Expand Up @@ -238,15 +229,7 @@ def _plot_acq_many_dofs(

agent.acq_axes = np.atleast_1d(agent.acq_axes)

<<<<<<< HEAD
<<<<<<< HEAD
x_dof, y_dof = plottable_dofs[axes[0]], plottable_dofs[axes[1]]
=======
x_dof, y_dof = agent.dofs.subset(active=True)[axes[0]], agent.dofs.subset(active=True)[axes[1]]
>>>>>>> 6fffe33 (work at ATF on Oct 12)
=======
x_dof, y_dof = plottable_dofs[axes[0]], plottable_dofs[axes[1]]
>>>>>>> 05b8e73 (fixed shapes in grid plots)

# test_inputs has shape (..., 1, n_active_dofs)
test_inputs = agent.test_inputs_grid() if gridded else agent.test_inputs(n=1024)
Expand Down Expand Up @@ -309,15 +292,7 @@ def _plot_valid_many_dofs(agent, axes=[0, 1], shading="nearest", cmap=DEFAULT_CO
if gridded is None:
gridded = len(plottable_dofs) == 2

<<<<<<< HEAD
<<<<<<< HEAD
x_dof, y_dof = plottable_dofs[axes[0]], plottable_dofs[axes[1]]
=======
x_dof, y_dof = agent.dofs.subset(active=True)[axes[0]], agent.dofs.subset(active=True)[axes[1]]
>>>>>>> 6fffe33 (work at ATF on Oct 12)
=======
x_dof, y_dof = plottable_dofs[axes[0]], plottable_dofs[axes[1]]
>>>>>>> 05b8e73 (fixed shapes in grid plots)

# test_inputs has shape (..., 1, n_active_dofs)
test_inputs = agent.test_inputs_grid() if gridded else agent.test_inputs(n=1024)
Expand Down Expand Up @@ -417,4 +392,4 @@ def inspect_beam(agent, index, border=None):

if border is not None:
plt.xlim(x_min - border * width_x, x_min + border * width_x)
plt.ylim(y_min - border * width_y, y_min + border * width_y)
plt.ylim(y_min - border * width_y, y_min + border * width_y)
11 changes: 0 additions & 11 deletions bloptools/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,8 @@
from databroker import Broker
from ophyd.utils import make_dir_tree

<<<<<<< HEAD
from bloptools.bayesian import Agent

from .. import devices, test_functions
=======
from bloptools.bayesian import DOF, Agent, Objective
from bloptools.utils import functions
>>>>>>> 05b8e73 (fixed shapes in grid plots)


@pytest.fixture(scope="function")
Expand Down Expand Up @@ -64,13 +58,8 @@ def agent(db):

agent = Agent(
dofs=dofs,
<<<<<<< HEAD
tasks=tasks,
digestion=test_functions.constrained_himmelblau_digestion,
=======
objectives=objectives,
digestion=functions.constrained_himmelblau_digestion,
>>>>>>> 05b8e73 (fixed shapes in grid plots)
db=db,
verbose=True,
tolerate_acquisition_errors=False,
Expand Down
4 changes: 0 additions & 4 deletions bloptools/tests/test_acq_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,4 @@ def test_analytic_acq_funcs_single_task(agent, RE, acq_func):
@pytest.mark.parametrize("acq_func", ["qei", "qpi", "qem", "qucb"])
def test_monte_carlo_acq_funcs_single_task(agent, RE, acq_func):
RE(agent.learn("qr", n=32))
<<<<<<< HEAD
RE(agent.learn(acq_func, n=4))
=======
RE(agent.learn(acq_func, n=4))
>>>>>>> a53c02b (parametrize acquisition function tests)
10 changes: 1 addition & 9 deletions bloptools/tests/test_passive_dofs.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,7 @@ def test_passive_dofs(RE, db):
DOF(name="x2", limits=(-5.0, 5.0)),
DOF(name="x3", limits=(-5.0, 5.0), active=False),
DOF(BrownianMotion(name="brownian1"), read_only=True),
<<<<<<< HEAD
<<<<<<< HEAD
DOF(BrownianMotion(name="brownian2"), read_only=True, active=False),
=======
DOF(BrownianMotion(name="brownian2"), read_only=True),
>>>>>>> 39a579f (make sure DOF bounds are cast to floats)
=======
DOF(BrownianMotion(name="brownian2"), read_only=True, active=False),
>>>>>>> 05b8e73 (fixed shapes in grid plots)
]

objectives = [
Expand All @@ -39,4 +31,4 @@ def test_passive_dofs(RE, db):

agent.plot_objectives()
agent.plot_acquisition()
agent.plot_validity()
agent.plot_validity()
5 changes: 0 additions & 5 deletions bloptools/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<<<<<<< HEAD
import botorch
import numpy as np
import scipy as sp
Expand Down Expand Up @@ -226,7 +225,3 @@ def best_image_feedback(image):
yw = 2 * np.sqrt((np.sum(y_weight * y**2) / np.sum(y_weight) - y0**2))

return x0, xw, y0, yw
=======
from .functions import * # noqa F401
from .misc import * # noqa F401
>>>>>>> 05b8e73 (fixed shapes in grid plots)
Loading

0 comments on commit 648337a

Please sign in to comment.