From 648337a48ad35ad417f45f839699b4a40e3a649a Mon Sep 17 00:00:00 2001 From: Thomas Morris Date: Tue, 24 Oct 2023 13:44:08 -0400 Subject: [PATCH] clean up after rebasing --- bloptools/bayesian/acquisition/__init__.py | 6 - bloptools/bayesian/agent.py | 52 +++---- bloptools/bayesian/objective.py | 15 +- bloptools/bayesian/plotting.py | 27 +--- bloptools/tests/conftest.py | 11 -- bloptools/tests/test_acq_funcs.py | 4 - bloptools/tests/test_passive_dofs.py | 10 +- bloptools/utils/__init__.py | 5 - docs/source/tutorials/himmelblau.ipynb | 156 ++++---------------- docs/source/tutorials/hyperparameters.ipynb | 28 ++-- docs/source/tutorials/passive-dofs.ipynb | 145 ++---------------- 11 files changed, 75 insertions(+), 384 deletions(-) diff --git a/bloptools/bayesian/acquisition/__init__.py b/bloptools/bayesian/acquisition/__init__.py index cbf4736..c0d8f39 100644 --- a/bloptools/bayesian/acquisition/__init__.py +++ b/bloptools/bayesian/acquisition/__init__.py @@ -1,8 +1,3 @@ -<<<<<<< HEAD -from .agent import * # noqa F401 -from .devices import * # noqa F401 -from .objective import * # noqa F401 -======= import os import yaml @@ -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) diff --git a/bloptools/bayesian/agent.py b/bloptools/bayesian/agent.py index db010a6..9b29deb 100644 --- a/bloptools/bayesian/agent.py +++ b/bloptools/bayesian/agent.py @@ -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 @@ -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, @@ -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): """ @@ -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) @@ -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() @@ -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) @@ -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) diff --git a/bloptools/bayesian/objective.py b/bloptools/bayesian/objective.py index 7e76a70..9a7be9f 100644 --- a/bloptools/bayesian/objective.py +++ b/bloptools/bayesian/objective.py @@ -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): @@ -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): diff --git a/bloptools/bayesian/plotting.py b/bloptools/bayesian/plotting.py index c5ddd32..a368c89 100644 --- a/bloptools/bayesian/plotting.py +++ b/bloptools/bayesian/plotting.py @@ -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 @@ -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) @@ -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) @@ -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) \ No newline at end of file + plt.ylim(y_min - border * width_y, y_min + border * width_y) diff --git a/bloptools/tests/conftest.py b/bloptools/tests/conftest.py index 83bed94..a345227 100644 --- a/bloptools/tests/conftest.py +++ b/bloptools/tests/conftest.py @@ -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") @@ -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, diff --git a/bloptools/tests/test_acq_funcs.py b/bloptools/tests/test_acq_funcs.py index fb1b08d..c5c183f 100644 --- a/bloptools/tests/test_acq_funcs.py +++ b/bloptools/tests/test_acq_funcs.py @@ -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) diff --git a/bloptools/tests/test_passive_dofs.py b/bloptools/tests/test_passive_dofs.py index bad80cd..3db1c06 100644 --- a/bloptools/tests/test_passive_dofs.py +++ b/bloptools/tests/test_passive_dofs.py @@ -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 = [ @@ -39,4 +31,4 @@ def test_passive_dofs(RE, db): agent.plot_objectives() agent.plot_acquisition() - agent.plot_validity() \ No newline at end of file + agent.plot_validity() diff --git a/bloptools/utils/__init__.py b/bloptools/utils/__init__.py index 61e6e09..6f3bd85 100644 --- a/bloptools/utils/__init__.py +++ b/bloptools/utils/__init__.py @@ -1,4 +1,3 @@ -<<<<<<< HEAD import botorch import numpy as np import scipy as sp @@ -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) diff --git a/docs/source/tutorials/himmelblau.ipynb b/docs/source/tutorials/himmelblau.ipynb index 2075e37..1a93aa7 100644 --- a/docs/source/tutorials/himmelblau.ipynb +++ b/docs/source/tutorials/himmelblau.ipynb @@ -19,18 +19,6 @@ "Let's use ``bloptools`` to minimize Himmelblau's function, which has four global minima:" ] }, - { - "cell_type": "code", - "execution_count": null, - "id": "cf27fc9e-d11c-40f4-a200-98e7814f506b", - "metadata": {}, - "outputs": [], - "source": [ - "from bloptools.utils import prepare_re_env\n", - "\n", - "%run -i $prepare_re_env.__file__ --db-type=temp" - ] - }, { "cell_type": "code", "execution_count": null, @@ -41,22 +29,16 @@ "import numpy as np\n", "import matplotlib as mpl\n", "from matplotlib import pyplot as plt\n", - "from bloptools.utils import functions\n", + "from bloptools import test_functions\n", "\n", "x1 = x2 = np.linspace(-6, 6, 1024)\n", "X1, X2 = np.meshgrid(x1, x2)\n", + "from bloptools.tasks import Task\n", "\n", -<<<<<<< HEAD -<<<<<<< HEAD - "F = functions.himmelblau(X1, X2)\n", -======= + "task = Task(key=\"himmelblau\", kind=\"min\")\n", "F = test_functions.himmelblau(X1, X2)\n", ->>>>>>> 39a579f (make sure DOF bounds are cast to floats) -======= - "F = functions.himmelblau(X1, X2)\n", ->>>>>>> 05b8e73 (fixed shapes in grid plots) "\n", - "plt.pcolormesh(x1, x2, F, norm=mpl.colors.LogNorm(vmin=1e-1, vmax=1e3), cmap=\"magma_r\")\n", + "plt.pcolormesh(x1, x2, F, norm=mpl.colors.LogNorm())\n", "plt.colorbar()\n", "plt.xlabel(\"x1\")\n", "plt.ylabel(\"x2\")" @@ -77,19 +59,11 @@ "metadata": {}, "outputs": [], "source": [ - "from bloptools.bayesian import DOF, BrownianMotion\n", + "from bloptools import devices\n", "\n", "dofs = [\n", - " DOF(name=\"x1\", limits=(-6, 6)),\n", - " DOF(name=\"x2\", limits=(-6, 6)),\n", -<<<<<<< HEAD -<<<<<<< HEAD -======= - " DOF(BrownianMotion(name=\"brownian1\"), read_only=True),\n", - " DOF(BrownianMotion(name=\"brownian2\"), read_only=True),\n", ->>>>>>> 39a579f (make sure DOF bounds are cast to floats) -======= ->>>>>>> 8db416a (update syntax in notebooks) + " {\"device\": devices.DOF(name=\"x1\"), \"limits\": (-6, 6), \"kind\": \"active\"},\n", + " {\"device\": devices.DOF(name=\"x2\"), \"limits\": (-6, 6), \"kind\": \"active\"},\n", "]" ] }, @@ -108,9 +82,9 @@ "metadata": {}, "outputs": [], "source": [ - "from bloptools.bayesian import Objective\n", - "\n", - "objectives = [Objective(key=\"himmelblau\", minimize=True)]" + "tasks = [\n", + " {\"key\": \"himmelblau\", \"kind\": \"minimize\"},\n", + "]" ] }, { @@ -133,7 +107,7 @@ " products = db[uid].table()\n", "\n", " for index, entry in products.iterrows():\n", - " products.loc[index, \"himmelblau\"] = functions.himmelblau(entry.x1, entry.x2)\n", + " products.loc[index, \"himmelblau\"] = test_functions.himmelblau(entry.x1, entry.x2)\n", "\n", " return products" ] @@ -156,62 +130,54 @@ }, "outputs": [], "source": [ - "from bloptools.bayesian import Agent\n", + "from bloptools.utils import prepare_re_env\n", "\n", + "%run -i $prepare_re_env.__file__ --db-type=temp\n", + "from bloptools.bayesian import Agent\n", "\n", "agent = Agent(\n", " dofs=dofs,\n", - " objectives=objectives,\n", + " tasks=tasks,\n", " digestion=digestion,\n", " db=db,\n", ")" ] }, - { - "cell_type": "code", - "execution_count": null, - "id": "e964d5a5-2a4a-4403-8c06-4ad17b00cecf", - "metadata": {}, - "outputs": [], - "source": [ - "agent.test_inputs_grid().shape" - ] - }, { "cell_type": "markdown", - "id": "27685849", + "id": "b7a608d6", "metadata": {}, "source": [ - "Without any data, we can't make any inferences about what the function looks like, and so we can't use any non-trivial acquisition functions. Let's start by quasi-randomly sampling the parameter space, and plotting our model of the function:" + "To decide which points to sample, the agent needs an acquisition function. The available acquisition function are here:" ] }, { "cell_type": "code", "execution_count": null, - "id": "996da937", + "id": "fb06739b", "metadata": {}, "outputs": [], "source": [ - "RE(agent.learn(\"quasi-random\", n=32))\n", - "agent.plot_objectives()" + "agent.acq_func_info" ] }, { "cell_type": "markdown", - "id": "dc264346-10fb-4c88-9925-4bfcf0dd3b07", + "id": "27685849", "metadata": {}, "source": [ - "To decide which points to sample, the agent needs an acquisition function. The available acquisition function are here:" + "Without any data, we can't make any inferences about what the function looks like, and so we can't use any non-trivial acquisition functions. Let's start by quasi-randomly sampling the parameter space, and plotting our model of the function:" ] }, { "cell_type": "code", "execution_count": null, - "id": "fb06739b", + "id": "996da937", "metadata": {}, "outputs": [], "source": [ - "agent.acq_func_info" + "RE(agent.learn(\"quasi-random\", n=32))\n", + "agent.plot_tasks()" ] }, { @@ -230,43 +196,9 @@ "metadata": {}, "outputs": [], "source": [ - "agent.plot_acquisition(acq_funcs=[\"qei\", \"pi\", \"qucb\"])" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "9c0b42dc-2df3-4ba6-b02f-569dab48db80", - "metadata": {}, - "outputs": [], - "source": [ - "agent.dofs.limits" + "agent.plot_acquisition(acq_funcs=[\"qei\", \"qpi\", \"ucb\"])" ] }, - { - "cell_type": "code", - "execution_count": null, - "id": "62b24d9b-740f-45a6-9617-47796c260273", - "metadata": {}, - "outputs": [], - "source": [ - "self = agent\n", - "import torch\n", - "\n", - "acq_func_lower_bounds = [dof.lower_limit if not dof.read_only else dof.readback for dof in self.dofs]\n", - "acq_func_upper_bounds = [dof.upper_limit if not dof.read_only else dof.readback for dof in self.dofs]\n", - "\n", - "torch.tensor(np.vstack([acq_func_lower_bounds, acq_func_upper_bounds]))" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "217158c0-aa65-409c-a7ab-63bb924723ad", - "metadata": {}, - "outputs": [], - "source": [] - }, { "attachments": {}, "cell_type": "markdown", @@ -276,24 +208,6 @@ "To decide where to go, the agent will find the inputs that maximize a given acquisition function:" ] }, - { - "cell_type": "code", - "execution_count": null, - "id": "16ec3c97-211b-49df-9e45-fcdd61ae98eb", - "metadata": {}, - "outputs": [], - "source": [ - "agent.acquisition_function_bounds" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "a066da53-0cdc-429b-a588-ce22b4a599b5", - "metadata": {}, - "outputs": [], - "source": [] - }, { "cell_type": "code", "execution_count": null, @@ -324,13 +238,9 @@ }, "outputs": [], "source": [ - "X, _ = agent.ask(\"qei\", n=8, route=True)\n", + "X, _ = agent.ask(\"qei\", n=8)\n", "agent.plot_acquisition(acq_funcs=[\"qei\"])\n", - "plt.scatter(*X.T, marker=\"d\", facecolor=\"w\", edgecolor=\"k\")\n", - "plt.plot(\n", - " *X.T,\n", - " color=\"r\",\n", - ")" + "plt.plot(*X.T, lw=5e-1, c=\"r\", marker=\"x\")" ] }, { @@ -366,17 +276,9 @@ "metadata": {}, "outputs": [], "source": [ - "agent.plot_objectives()\n", - "# print(agent.best_inputs)" + "agent.plot_tasks()\n", + "print(agent.best_inputs)" ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "b6453b87-f864-40af-ba70-9a42960f54b9", - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { diff --git a/docs/source/tutorials/hyperparameters.ipynb b/docs/source/tutorials/hyperparameters.ipynb index 568c73c..e4026e1 100644 --- a/docs/source/tutorials/hyperparameters.ipynb +++ b/docs/source/tutorials/hyperparameters.ipynb @@ -21,12 +21,12 @@ "import numpy as np\n", "import matplotlib as mpl\n", "from matplotlib import pyplot as plt\n", - "from bloptools.utils import functions\n", + "from bloptools import test_functions\n", "\n", "x1 = x2 = np.linspace(-10, 10, 256)\n", "X1, X2 = np.meshgrid(x1, x2)\n", "\n", - "F = functions.booth(X1, X2)\n", + "F = test_functions.booth(X1, X2)\n", "\n", "plt.pcolormesh(x1, x2, F, norm=mpl.colors.LogNorm(), shading=\"auto\")\n", "plt.colorbar()\n", @@ -54,7 +54,7 @@ " products = db[uid].table()\n", "\n", " for index, entry in products.iterrows():\n", - " products.loc[index, \"booth\"] = functions.booth(entry.x1, entry.x2)\n", + " products.loc[index, \"booth\"] = test_functions.booth(entry.x1, entry.x2)\n", "\n", " return products" ] @@ -72,28 +72,28 @@ "\n", "%run -i $prepare_re_env.__file__ --db-type=temp\n", "\n", - "from bloptools.bayesian import DOF, Objective, Agent\n", + "from bloptools import devices\n", + "from bloptools.bayesian import Agent\n", "\n", "dofs = [\n", - " DOF(name=\"x1\", limits=(-6, 6)),\n", - " DOF(name=\"x2\", limits=(-6, 6)),\n", + " {\"device\": devices.DOF(name=\"x1\"), \"limits\": (-5, 5), \"kind\": \"active\"},\n", + " {\"device\": devices.DOF(name=\"x2\"), \"limits\": (-5, 5), \"kind\": \"active\"},\n", "]\n", "\n", - "objectives = [\n", - " Objective(key=\"booth\", minimize=True),\n", + "tasks = [\n", + " {\"key\": \"booth\", \"kind\": \"minimize\"},\n", "]\n", "\n", - "\n", "agent = Agent(\n", " dofs=dofs,\n", - " objectives=objectives,\n", + " tasks=tasks,\n", " digestion=digestion,\n", " db=db,\n", ")\n", "\n", "RE(agent.learn(acq_func=\"qr\", n=16))\n", "\n", - "agent.plot_objectives()" + "agent.plot_tasks()" ] }, { @@ -125,13 +125,13 @@ "outputs": [], "source": [ "RE(agent.learn(\"qei\", n=4, iterations=4))\n", - "agent.plot_objectives()" + "agent.plot_tasks()" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3.11.4 64-bit", "language": "python", "name": "python3" }, @@ -145,7 +145,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.4" + "version": "3.11.5" }, "vscode": { "interpreter": { diff --git a/docs/source/tutorials/passive-dofs.ipynb b/docs/source/tutorials/passive-dofs.ipynb index 8034aab..363a2a0 100644 --- a/docs/source/tutorials/passive-dofs.ipynb +++ b/docs/source/tutorials/passive-dofs.ipynb @@ -26,164 +26,43 @@ "metadata": {}, "outputs": [], "source": [ - "from bloptools.utils import prepare_re_env\n", - "\n", - "%run -i $prepare_re_env.__file__ --db-type=temp" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "4cf5dbd1-e404-4504-b822-3956ca61ef74", - "metadata": {}, - "outputs": [], - "source": [ -<<<<<<< HEAD -<<<<<<< HEAD - "import pytest\n", -======= "from bloptools.utils import prepare_re_env\n", "\n", "%run -i $prepare_re_env.__file__ --db-type=temp\n", -======= - "import pytest\n", ->>>>>>> 05b8e73 (fixed shapes in grid plots) "\n", - "from bloptools.utils import functions\n", - "from bloptools.bayesian import DOF, Agent, BrownianMotion, Objective\n", + "from bloptools import devices, test_functions\n", + "from bloptools.bayesian import Agent\n", "\n", -<<<<<<< HEAD "\n", - "@pytest.mark.test_func\n", - "def test_passive_dofs(RE, db):\n", - " dofs = [\n", - " DOF(name=\"x1\", limits=(-5.0, 5.0)),\n", - " DOF(name=\"x2\", limits=(-5.0, 5.0)),\n", - " DOF(name=\"x3\", limits=(-5.0, 5.0), active=False),\n", - " DOF(BrownianMotion(name=\"brownian1\"), read_only=True),\n", - " DOF(BrownianMotion(name=\"brownian2\"), read_only=True, active=False),\n", - " ]\n", + "def digestion(db, uid):\n", + " products = db[uid].table()\n", "\n", - " objectives = [\n", - " Objective(key=\"himmelblau\", minimize=True),\n", - " ]\n", + " for index, entry in products.iterrows():\n", + " products.loc[index, \"styblinksi-tang\"] = test_functions.styblinski_tang(entry.x - 1e-1 * entry.brownian)\n", "\n", -<<<<<<< HEAD " return products\n", ->>>>>>> 39a579f (make sure DOF bounds are cast to floats) - "\n", - "from bloptools.utils import functions\n", - "from bloptools.bayesian import DOF, Agent, BrownianMotion, Objective\n", -======= - " agent = Agent(\n", - " dofs=dofs,\n", - " objectives=objectives,\n", - " digestion=functions.constrained_himmelblau_digestion,\n", - " db=db,\n", - " verbose=True,\n", - " tolerate_acquisition_errors=False,\n", - " )\n", "\n", - " RE(agent.learn(\"qr\", n=32))\n", ->>>>>>> 05b8e73 (fixed shapes in grid plots) "\n", - " agent.plot_objectives()\n", - " agent.plot_acquisition()\n", - " agent.plot_validity()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "c88d54d9-c600-41ad-9b3e-53af510d8760", - "metadata": {}, - "outputs": [], - "source": [ "dofs = [\n", - " DOF(name=\"x1\", limits=(-5.0, 5.0)),\n", - " DOF(name=\"x2\", limits=(-5.0, 5.0)),\n", -<<<<<<< HEAD - " DOF(name=\"x3\", limits=(-5.0, 5.0), active=False),\n", - " DOF(BrownianMotion(name=\"brownian1\"), read_only=True),\n", - " DOF(BrownianMotion(name=\"brownian2\"), read_only=True, active=False),\n", + " {\"device\": devices.DOF(name=\"x\"), \"limits\": (-5, 5), \"kind\": \"active\"},\n", + " {\"device\": devices.BrownianMotion(name=\"brownian\"), \"limits\": (-2, 2), \"kind\": \"passive\"},\n", "]\n", "\n", - "objectives = [\n", - " Objective(key=\"himmelblau\", minimize=True),\n", -======= -======= - "dofs = [\n", - " DOF(name=\"x1\", limits=(-5.0, 5.0)),\n", - " DOF(name=\"x2\", limits=(-5.0, 5.0)),\n", - " DOF(name=\"x3\", limits=(-5.0, 5.0), active=False),\n", ->>>>>>> 8db416a (update syntax in notebooks) - " DOF(BrownianMotion(name=\"brownian1\"), read_only=True),\n", - " DOF(BrownianMotion(name=\"brownian2\"), read_only=True, active=False),\n", - "]\n", - "\n", - "objectives = [\n", -<<<<<<< HEAD - " Objective(key=\"styblinksi-tang\", minimize=True),\n", ->>>>>>> 39a579f (make sure DOF bounds are cast to floats) -======= - " Objective(key=\"himmelblau\", minimize=True),\n", ->>>>>>> 8db416a (update syntax in notebooks) + "tasks = [\n", + " {\"key\": \"styblinksi-tang\", \"kind\": \"minimize\"},\n", "]\n", "\n", "agent = Agent(\n", " dofs=dofs,\n", - " objectives=objectives,\n", -<<<<<<< HEAD -<<<<<<< HEAD - " digestion=functions.constrained_himmelblau_digestion,\n", - " db=db,\n", - " verbose=True,\n", - " tolerate_acquisition_errors=False,\n", - ")\n", - "\n", - "RE(agent.learn(\"qr\", n=32))\n", - "\n", - "agent.plot_objectives()\n", - "agent.plot_acquisition()\n", - "agent.plot_validity()" -======= + " tasks=tasks,\n", " digestion=digestion,\n", -======= - " digestion=functions.constrained_himmelblau_digestion,\n", ->>>>>>> 8db416a (update syntax in notebooks) " db=db,\n", - " verbose=True,\n", - " tolerate_acquisition_errors=False,\n", ")\n", "\n", "RE(agent.learn(\"qr\", n=32))\n", "\n", - "agent.plot_objectives()\n", - "agent.plot_acquisition()\n", - "agent.plot_validity()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "990a877e-f533-419c-bf5d-569ad7e72c6b", - "metadata": {}, - "outputs": [], -<<<<<<< HEAD - "source": [ - "agent.plot_objectives()" ->>>>>>> 39a579f (make sure DOF bounds are cast to floats) + "agent.plot_tasks()" ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "990a877e-f533-419c-bf5d-569ad7e72c6b", - "metadata": {}, - "outputs": [], -======= ->>>>>>> 8db416a (update syntax in notebooks) - "source": [] } ], "metadata": {