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

Patch for Xopt 2.5.4 #144

Merged
merged 6 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
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
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ dependencies = [
"qdarkstyle>=3.0",
"pillow",
"requests",
"xopt>=2.2.2",
"botorch==0.12.0"
"xopt>=2.5.4",
]
dynamic = ["version"]
[tool.setuptools_scm]
Expand Down
5 changes: 5 additions & 0 deletions src/badger/core_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ def run_routine_subprocess(
# set required arguments
try:
routine = load_run(args["routine_filename"])
# Call the reset generator API before starting the optimization
try:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of using try/except, can we use an isinstance (routine.generator, BayesianGenerator) which would be more stable?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think for our current application/code the two approaches would perform exactly the same. If we add in more generators with state in Xopt in the future I feel we might need more general reset() API, in that case it could be easier to just write try except blocks...

I'm of course open to further discussion, it would be great if you can show me an example where the isinstance approach is better than the try catch one, then we can change the code accordingly :)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are no generators outside Bayesian ones that are likely to use the same turbo controller type. There are many possible ways inside calling the turbo controller that could raise an Attribute error which in turn would fail silently due to the try except block.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't want things to fail silently

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see! Thanks for the explanation/example! That makes sense. So we don't want the potential internal Attribute errors to be ignored when we call turbo_controller.reset(). Will do the isinstance logic once the reset API works.

routine.generator.turbo_controller.reset()
except AttributeError: # if not BO generators
pass
# TODO: might need to consider the case where routine.data is None?
if routine.data is not None:
routine.data = routine.data.iloc[0:0] # reset the data
Expand Down
17 changes: 12 additions & 5 deletions src/badger/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,20 @@ def _get_bounds(
variable_names_new = [
name for name in variable_names if not len(self.variables.get(name, []))
]
if len(variable_names_new):
self.variables.update(self.get_bounds(variable_names_new))

# Set a default value for the bounds if not defined
default_value = [-1, 1]
# Get bound one by one due to potential failure
for name in variable_names_new:
try:
bound = self.get_bound(name)
except Exception:
raise BadgerEnvVarError(f"Failed to get bound for {name}")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this puts up a warning box in the GUI correct?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's right -- any issues raised during an action through GUI would result in a warning box (implemented by Shamin) :)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great thx


return {k: self.variables.get(k, default_value) for k in variable_names}
if bound[1] <= bound[0]:
raise BadgerEnvVarError(f"Invalid bound for {name}: {bound}")

self.variables.update({name: bound})

return {k: self.variables[k] for k in variable_names}


def instantiate_env(env_class, configs, manager=None):
Expand Down
25 changes: 2 additions & 23 deletions src/badger/gui/acr/components/routine_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,13 +312,7 @@ def refresh_ui(self, routine: Routine = None, silent: bool = False):
all_variables.update(i)
if routine.additional_variables: # there are additional variables
env = self.create_env()
# Have to check each variable since some could fail
for v in routine.additional_variables:
try:
b = env.get_bound(v)
except Exception:
b = [-1000, 1000] # default wide range
all_variables.update({v: b})
all_variables.update(env._get_bounds(routine.additional_variables))
# Format for update_variables method
all_variables = dict(sorted(all_variables.items()))
all_variables = [{key: value} for key, value in all_variables.items()]
Expand Down Expand Up @@ -939,7 +933,6 @@ def _compose_vocs(self) -> (VOCS, list[str]):

def _compose_routine(self) -> Routine:
# Compose the routine

# Metadata
name = self.edit_save.text() or self.edit_save.placeholderText()
description = self.edit_descr.toPlainText()
Expand Down Expand Up @@ -967,6 +960,7 @@ def _compose_routine(self) -> Routine:
if "turbo_controller" not in generator_params:
generator_params["turbo_controller"] = "optimize"

# TODO: remove this patch when Xopt reset API works
# Nullify a few properties in turbo that can cause issues
turbo_config = generator_params["turbo_controller"]
if type(turbo_config) is dict:
Expand All @@ -985,21 +979,6 @@ def _compose_routine(self) -> Routine:
raise BadgerRoutineError("no variables selected")
if not vocs.objectives:
raise BadgerRoutineError("no objectives selected")
# Sanity check on BO + VOCS cross-compatibility
flag_safety_bo = False
if generator_name in all_generator_names["bo"]:
turbo_config = generator_params["turbo_controller"]
if type(turbo_config) is dict:
if turbo_config["name"] == "safety":
flag_safety_bo = True
elif turbo_config == "safety":
flag_safety_bo = True

if flag_safety_bo and not vocs.constraints:
raise BadgerRoutineError(
"TuRBO in safety mode requires constraints, "
"please add at least one constraint in the VOCS config panel"
)

# Initial points
init_points_df = pd.DataFrame.from_dict(
Expand Down
9 changes: 2 additions & 7 deletions src/badger/gui/default/components/var_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,11 +311,6 @@ def add_additional_variable(self, item):
# TODO: handle this case? Right now I don't think it should happen
raise "Environment cannot be found for new variable bounds!"

# Sanitize vrange
# TODO: raise a heads-up regarding the invalid bounds
if vrange[1] <= vrange[0]:
vrange = [-1000, 1000] # fallback to some default values

# Add checkbox only when a PV is entered
self.setCellWidget(idx, 0, QCheckBox())

Expand Down Expand Up @@ -350,8 +345,8 @@ def get_bounds(self, name):
self.env = instantiate_env(self.env_class, self.configs)

value = self.env.get_variable(name)
bounds = self.env.get_bound(name)
return value, bounds
bound = self.env._get_bounds([name])[name]
return value, bound

def add_variable(self, name, lb, ub):
var = {name: [lb, ub]}
Expand Down
Loading