From 4ead28ca0578f08110f9b176082b8d29066f6bd3 Mon Sep 17 00:00:00 2001 From: phi Date: Thu, 25 Jul 2024 20:48:42 +0900 Subject: [PATCH 1/4] feat: exceptions --- bayes_opt/acquisition.py | 6 +++++- bayes_opt/exceptions.py | 31 +++++++++++++++++++++++++++++++ bayes_opt/target_space.py | 3 ++- bayes_opt/util.py | 16 +--------------- 4 files changed, 39 insertions(+), 17 deletions(-) create mode 100644 bayes_opt/exceptions.py diff --git a/bayes_opt/acquisition.py b/bayes_opt/acquisition.py index 7bc47e0b4..bda31d214 100644 --- a/bayes_opt/acquisition.py +++ b/bayes_opt/acquisition.py @@ -33,8 +33,12 @@ from scipy.stats import norm from sklearn.gaussian_process import GaussianProcessRegressor +from bayes_opt.exceptions 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 diff --git a/bayes_opt/exceptions.py b/bayes_opt/exceptions.py new file mode 100644 index 000000000..628e20d77 --- /dev/null +++ b/bayes_opt/exceptions.py @@ -0,0 +1,31 @@ +"""This module contains custom exceptions for Bayesian Optimization.""" + +from __future__ import annotations + +__all__ = [ + "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.""" diff --git a/bayes_opt/target_space.py b/bayes_opt/target_space.py index c6f516947..d3ed77739 100644 --- a/bayes_opt/target_space.py +++ b/bayes_opt/target_space.py @@ -7,7 +7,8 @@ import numpy as np from colorama import Fore -from bayes_opt.util import NotUniqueError, ensure_rng +from bayes_opt.exceptions import NotUniqueError +from bayes_opt.util import ensure_rng def _hashable(x): diff --git a/bayes_opt/util.py b/bayes_opt/util.py index 80e9c7084..aef92c499 100644 --- a/bayes_opt/util.py +++ b/bayes_opt/util.py @@ -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.exceptions import NotUniqueError def load_logs(optimizer, logs): From aa334ececc16f1229ab5434669acff4d718da79b Mon Sep 17 00:00:00 2001 From: phi Date: Thu, 25 Jul 2024 20:55:13 +0900 Subject: [PATCH 2/4] fix: rename exceptions -> exception --- bayes_opt/acquisition.py | 2 +- bayes_opt/{exceptions.py => exception.py} | 0 bayes_opt/target_space.py | 2 +- bayes_opt/util.py | 2 +- 4 files changed, 3 insertions(+), 3 deletions(-) rename bayes_opt/{exceptions.py => exception.py} (100%) diff --git a/bayes_opt/acquisition.py b/bayes_opt/acquisition.py index bda31d214..4fa7eaa1b 100644 --- a/bayes_opt/acquisition.py +++ b/bayes_opt/acquisition.py @@ -33,7 +33,7 @@ from scipy.stats import norm from sklearn.gaussian_process import GaussianProcessRegressor -from bayes_opt.exceptions import ( +from bayes_opt.exception import ( ConstraintNotSupportedError, NoValidPointRegisteredError, TargetSpaceEmptyError, diff --git a/bayes_opt/exceptions.py b/bayes_opt/exception.py similarity index 100% rename from bayes_opt/exceptions.py rename to bayes_opt/exception.py diff --git a/bayes_opt/target_space.py b/bayes_opt/target_space.py index d3ed77739..04bc3f13b 100644 --- a/bayes_opt/target_space.py +++ b/bayes_opt/target_space.py @@ -7,7 +7,7 @@ import numpy as np from colorama import Fore -from bayes_opt.exceptions import NotUniqueError +from bayes_opt.exception import NotUniqueError from bayes_opt.util import ensure_rng diff --git a/bayes_opt/util.py b/bayes_opt/util.py index aef92c499..b1e745b13 100644 --- a/bayes_opt/util.py +++ b/bayes_opt/util.py @@ -7,7 +7,7 @@ import numpy as np -from bayes_opt.exceptions import NotUniqueError +from bayes_opt.exception import NotUniqueError def load_logs(optimizer, logs): From 2a2bdce7fba2707a929d60b903585ec092cae235 Mon Sep 17 00:00:00 2001 From: phi Date: Thu, 25 Jul 2024 21:05:32 +0900 Subject: [PATCH 3/4] fix tests --- tests/test_bayesian_optimization.py | 2 +- tests/test_target_space.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_bayesian_optimization.py b/tests/test_bayesian_optimization.py index 48283ade6..64af3581f 100644 --- a/tests/test_bayesian_optimization.py +++ b/tests/test_bayesian_optimization.py @@ -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): diff --git a/tests/test_target_space.py b/tests/test_target_space.py index ee196a2f1..e1e9163c1 100644 --- a/tests/test_target_space.py +++ b/tests/test_target_space.py @@ -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): From f0249a50e381f421e972837117ea3ac0bccddfa1 Mon Sep 17 00:00:00 2001 From: phi Date: Sun, 8 Sep 2024 17:44:41 +0900 Subject: [PATCH 4/4] docs: add reference/exception.rst --- docsrc/index.rst | 1 + docsrc/reference/exception.rst | 5 +++++ 2 files changed, 6 insertions(+) create mode 100644 docsrc/reference/exception.rst diff --git a/docsrc/index.rst b/docsrc/index.rst index d6f9384d0..ac664a582 100644 --- a/docsrc/index.rst +++ b/docsrc/index.rst @@ -26,6 +26,7 @@ reference/constraint reference/domain_reduction reference/target_space + reference/exception reference/other .. raw:: html diff --git a/docsrc/reference/exception.rst b/docsrc/reference/exception.rst new file mode 100644 index 000000000..9315628c9 --- /dev/null +++ b/docsrc/reference/exception.rst @@ -0,0 +1,5 @@ +:py:mod:`bayes_opt.exception` +------------------------------- + +.. automodule:: bayes_opt.exception + :members: