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

Improve prior build error message #889

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
6 changes: 3 additions & 3 deletions src/orion/algo/space.py
Original file line number Diff line number Diff line change
Expand Up @@ -880,13 +880,13 @@ class Fidelity(Dimension):
# pylint:disable=super-init-not-called
def __init__(self, name, low, high, base=2):
if low <= 0:
raise AttributeError("Minimum resources must be a positive number.")
raise ValueError("Minimum resources must be a positive number.")
elif low > high:
raise AttributeError(
raise ValueError(
"Minimum resources must be smaller than maximum resources."
)
if base < 1:
raise AttributeError("Base should be greater than or equal to 1")
raise ValueError("Base should be greater than or equal to 1")
self.name = name
self.low = int(low)
self.high = int(high)
Expand Down
18 changes: 13 additions & 5 deletions src/orion/core/io/space_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,16 @@ def loguniform(self, *args, **kwargs):
klass = _real_or_int(kwargs)
return klass(name, "reciprocal", *args, **kwargs)

def _build_custom(self, expression, globals_, locals_):
name = expression.split("(")[0]
if not hasattr(self, name):
return None
try:
dimension = eval("self." + expression, globals_, locals_)
return dimension
except AttributeError as error:
raise AttributeError(f"Parameter '{name}': {error}") from error

def _build(self, name, expression):
"""Build a `Dimension` object using a string as its `name` and another
string, `expression`, from configuration as a "function" to create it.
Expand All @@ -205,12 +215,10 @@ def _build(self, name, expression):
import numpy as np

globals_["np"] = np
try:
dimension = eval("self." + expression, globals_, {"self": self})

locals_ = {"self": self}
dimension = self._build_custom(expression, globals_, locals_)
if dimension is not None:
return dimension
except AttributeError:
pass

# If not found in the methods of `DimensionBuilder`.
# try to see if it is legit scipy stuff and call a `Dimension`
Expand Down
11 changes: 11 additions & 0 deletions tests/unittests/core/io/test_space_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,17 @@ def test_build_fails_because_of_ValueError_on_init(self, dimbuilder):
assert "Parameter" in str(exc.value)
assert "size" in str(exc.value.__cause__)

def test_build_fails_because_of_invalid_arguments_for_custom_prior(
self, dimbuilder
):
"""Build fails because ValueError happens on init of builtin prior."""
with pytest.raises(ValueError) as exc:
Copy link
Collaborator

@lebrice lebrice May 17, 2022

Choose a reason for hiding this comment

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

You could use the match argument of raises instead of checking if the value is in the exception message, I think.
(That might be only true for pytest.warns though, not 100% sure).

Copy link
Member Author

Choose a reason for hiding this comment

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

Indeed. This test is looking at two different parts of exc.value however.

dimbuilder.build("yolo2", "fidelity(0, 10, base=1)")
assert "Parameter" in str(exc.value)
assert "Minimum resources must be a positive number." in str(
exc.value.__cause__
)

def test_build_gaussian(self, dimbuilder):
"""Check that gaussian/normal/norm is built into reciprocal correctly."""
dim = dimbuilder.build("yolo", "gaussian(3, 5)")
Expand Down