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 module exception #506

Merged
merged 4 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all 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: 5 additions & 1 deletion bayes_opt/acquisition.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@
from scipy.stats import norm
from sklearn.gaussian_process import GaussianProcessRegressor

from bayes_opt.exception import (
ConstraintNotSupportedError,
NoValidPointRegisteredError,
TargetSpaceEmptyError,
)
from bayes_opt.target_space import TargetSpace
from bayes_opt.util import ConstraintNotSupportedError, NoValidPointRegisteredError, TargetSpaceEmptyError

if TYPE_CHECKING:
from bayes_opt.constraint import ConstraintModel
Expand Down
31 changes: 31 additions & 0 deletions bayes_opt/exception.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""This module contains custom exceptions for Bayesian Optimization."""

from __future__ import annotations

__all__ = [
Copy link
Member

Choose a reason for hiding this comment

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

What is the advantage of declaring the exports explicitly?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When importing objects like from A import *, only the objects defined in __all__ should be imported.
I usually leave this in, but I'll remove it if it's not needed.

"BayesianOptimizationError",
"NotUniqueError",
"ConstraintNotSupportedError",
"NoValidPointRegisteredError",
"TargetSpaceEmptyError",
]


class BayesianOptimizationError(Exception):
"""Base class for exceptions in the Bayesian Optimization."""


class NotUniqueError(BayesianOptimizationError):
"""A point is non-unique."""


class ConstraintNotSupportedError(BayesianOptimizationError):
"""Raised when constrained optimization is not supported."""


class NoValidPointRegisteredError(BayesianOptimizationError):
"""Raised when an acquisition function depends on previous points but none are registered."""


class TargetSpaceEmptyError(BayesianOptimizationError):
"""Raised when the target space is empty."""
3 changes: 2 additions & 1 deletion bayes_opt/target_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import numpy as np
from colorama import Fore

from bayes_opt.util import NotUniqueError, ensure_rng
from bayes_opt.exception import NotUniqueError
from bayes_opt.util import ensure_rng


def _hashable(x):
Expand Down
16 changes: 1 addition & 15 deletions bayes_opt/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,7 @@

import numpy as np


class NotUniqueError(Exception):
"""A point is non-unique."""


class ConstraintNotSupportedError(Exception):
"""Raised when constrained optimization is not supported."""


class NoValidPointRegisteredError(Exception):
"""Raised when an acquisition function depends on previous points but none are registered."""


class TargetSpaceEmptyError(Exception):
"""Raised when the target space is empty."""
from bayes_opt.exception import NotUniqueError


def load_logs(optimizer, logs):
Expand Down
1 change: 1 addition & 0 deletions docsrc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
reference/constraint
reference/domain_reduction
reference/target_space
reference/exception
reference/other

.. raw:: html
Expand Down
5 changes: 5 additions & 0 deletions docsrc/reference/exception.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
:py:mod:`bayes_opt.exception`
-------------------------------

.. automodule:: bayes_opt.exception
:members:
2 changes: 1 addition & 1 deletion tests/test_bayesian_optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
from bayes_opt import BayesianOptimization, acquisition
from bayes_opt.acquisition import AcquisitionFunction
from bayes_opt.event import DEFAULT_EVENTS, Events
from bayes_opt.exception import NotUniqueError
from bayes_opt.logger import ScreenLogger
from bayes_opt.target_space import TargetSpace
from bayes_opt.util import NotUniqueError


def target_func(**kwargs):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_target_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import pytest

from bayes_opt.constraint import ConstraintModel
from bayes_opt.exception import NotUniqueError
from bayes_opt.target_space import TargetSpace
from bayes_opt.util import NotUniqueError


def target_func(**kwargs):
Expand Down
Loading