Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add classes for DOFs and objectives #49

Merged
merged 27 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
2df6e67
fixed bug in computed constraint shape in acquisition functions
Oct 4, 2023
fa09b69
added grid scan as initialization plan
thomaswmorris Oct 11, 2023
2b2320d
added class representations for DOFs and objectives
thomaswmorris Oct 12, 2023
24dae2a
make sure DOF bounds are cast to floats
Oct 12, 2023
29c4ba2
move test functions to utils
Oct 12, 2023
7c77eee
work at ATF on Oct 12
AmbarCRoAl Oct 12, 2023
2909c29
fixed shapes in grid plots
Oct 12, 2023
4b3bf66
update syntax in notebooks
Oct 12, 2023
91be2d1
better verbose output for agent.ask()
Oct 12, 2023
de2689f
fixed bug in computed constraint shape in acquisition functions
Oct 4, 2023
0f328fa
added grid scan as initialization plan
thomaswmorris Oct 11, 2023
d1346cf
added class representations for DOFs and objectives
thomaswmorris Oct 12, 2023
aac4999
make sure DOF bounds are cast to floats
Oct 12, 2023
96fcb54
move test functions to utils
Oct 12, 2023
1072c67
work at ATF on Oct 12
AmbarCRoAl Oct 12, 2023
8ea4e73
fixed shapes in grid plots
Oct 12, 2023
a4647c8
update syntax in notebooks
Oct 12, 2023
44cf99d
better verbose output for agent.ask()
Oct 12, 2023
85d92fe
parametrize acquisition function tests
Oct 24, 2023
648337a
clean up after rebasing
Oct 24, 2023
bbdf4ab
fixed tutorial notebooks
Oct 24, 2023
f0bc991
fixed multi-objective acquisition functions
Oct 28, 2023
fca00f0
access table by device name and not dof name
Oct 28, 2023
b140195
fixed docs and added more prereqs
thomaswmorris Oct 30, 2023
063ee59
added a whole bunch of docstrings (and other tweaks)
Nov 4, 2023
8d3ded3
change typing syntax for python <3.10
Nov 4, 2023
30d6710
fix dev requirements
Nov 4, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions bloptools/bayesian/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def tell(self, new_table=None, append=True, train=True, **kwargs):

likelihood = gpytorch.likelihoods.GaussianLikelihood(
noise_constraint=gpytorch.constraints.Interval(
torch.tensor(1e-4).square(),
torch.tensor(1e-2).square(),
torch.tensor(1 / obj.min_snr).square(),
),
# noise_prior=gpytorch.priors.torch_priors.LogNormalPrior(loc=loc, scale=scale),
Expand Down Expand Up @@ -293,6 +293,9 @@ def acquire(self, acquisition_inputs):
# for obj in self.objectives:
# products.loc[index, objective["key"]] = getattr(entry, objective["key"])

except KeyboardInterrupt:
raise KeyboardInterrupt()
thomaswmorris marked this conversation as resolved.
Show resolved Hide resolved

except Exception as error:
if not self.allow_acquisition_errors:
raise error
Expand Down Expand Up @@ -334,11 +337,12 @@ def learn(

if acq_func is not None:
for i in range(iterations):
x, acq_func_meta = self.ask(n=n, acq_func_identifier=acq_func, **kwargs)

new_table = yield from self.acquire(x)
new_table.loc[:, "acq_func"] = acq_func_meta["name"]
self.tell(new_table=new_table, train=train)
print(f"running iteration {i + 1} / {iterations}")
for single_acq_func in np.atleast_1d(acq_func):
x, acq_func_meta = self.ask(n=n, acq_func_identifier=single_acq_func, **kwargs)
new_table = yield from self.acquire(x)
new_table.loc[:, "acq_func"] = acq_func_meta["name"]
self.tell(new_table=new_table, train=train)

self.initialized = True

Expand All @@ -357,12 +361,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 = (cache_limits[dof.name] + offset, cache_limits[dof.name] + offset)
# for dof in self.dofs:
# offset = 0.25 * np.ptp(dof.limits) * np.random.uniform(low=-1, high=1)
# dof.limits = (cache_limits[dof.name][0] + offset, cache_limits[dof.name][1] + offset)

self.reset()

Expand Down
2 changes: 2 additions & 0 deletions bloptools/bayesian/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ def __init__(
self.active = active
self.latent_group = latent_group if latent_group is not None else str(uuid.uuid4())

self.device.kind = "hinted"

@property
def lower_limit(self):
return float(self.limits[0])
Expand Down
69 changes: 38 additions & 31 deletions docs/source/tutorials/passive-dofs.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -38,43 +38,42 @@
"metadata": {},
"outputs": [],
"source": [
"from bloptools.utils import prepare_re_env\n",
"\n",
"%run -i $prepare_re_env.__file__ --db-type=temp\n",
"import pytest\n",
"\n",
"from bloptools.utils import functions\n",
"from bloptools.bayesian import DOF, Agent, BrownianMotion, Objective\n",
"\n",
"\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",
"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",
"\n",
" objectives = [\n",
" Objective(key=\"himmelblau\", minimize=True),\n",
" ]\n",
"objectives = [\n",
" Objective(key=\"himmelblau\", minimize=True),\n",
"]\n",
"\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",
"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",
"\n",
" agent.plot_objectives()\n",
" agent.plot_acquisition()\n",
" agent.plot_validity()"
"RE(agent.learn(\"qr\", n=16))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "49127601",
"metadata": {},
"outputs": [],
"source": [
"agent.dofs"
]
},
{
Expand All @@ -86,6 +85,14 @@
"source": [
"agent.plot_objectives()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4d856a7d",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -104,7 +111,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.4"
"version": "3.10.12"
},
"vscode": {
"interpreter": {
Expand Down
3 changes: 3 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
# the documentation) but not necessarily required for _using_ it.
black
pytest-codecov
chardet
coverage
flake8
furo
isort
markupsafe
nbstripout
numpydoc
thomaswmorris marked this conversation as resolved.
Show resolved Hide resolved
pre-commit
pre-commit-hooks
pytest
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ botorch
databroker
gpytorch
h5py
IPython
thomaswmorris marked this conversation as resolved.
Show resolved Hide resolved
matplotlib
numpy
ophyd
Expand Down
Loading