From 55e10364335fa640791cd2ad9f55af39d71e99e1 Mon Sep 17 00:00:00 2001 From: nabenabe0928 Date: Wed, 8 May 2024 06:10:22 +0200 Subject: [PATCH 1/3] Bundle experimental feature warning --- optuna/_experimental.py | 8 ++++++ optuna/importance/__init__.py | 7 ++--- optuna/samplers/_cmaes.py | 37 +++++---------------------- optuna/samplers/_tpe/sampler.py | 31 +++++----------------- optuna/samplers/nsgaii/_sampler.py | 25 ++++-------------- optuna/visualization/_pareto_front.py | 7 ++--- 6 files changed, 30 insertions(+), 85 deletions(-) diff --git a/optuna/_experimental.py b/optuna/_experimental.py index 40161dc034..6202d36cf3 100644 --- a/optuna/_experimental.py +++ b/optuna/_experimental.py @@ -26,6 +26,14 @@ """ +def warn_experimental_option(option_name: str) -> None: + warnings.warn( + f"``{option_name}`` option is an experimental feature." + " The interface can change in the future.", + ExperimentalWarning, + ) + + def _validate_version(version: str) -> None: if not isinstance(version, str) or len(version.split(".")) != 3: raise ValueError( diff --git a/optuna/importance/__init__.py b/optuna/importance/__init__.py index a326e9129b..634e8bdbc0 100644 --- a/optuna/importance/__init__.py +++ b/optuna/importance/__init__.py @@ -4,6 +4,7 @@ from typing import Optional import warnings +from optuna._experimental import warn_experimental_option from optuna.exceptions import ExperimentalWarning from optuna.importance._base import BaseImportanceEvaluator from optuna.importance._fanova import FanovaImportanceEvaluator @@ -119,9 +120,5 @@ def get_param_importances( else: return dict((param, value / s) for (param, value) in res.items()) else: - warnings.warn( - "`normalize` option is an experimental feature." - " The interface can change in the future.", - ExperimentalWarning, - ) + warn_experimental_option("normalize") return res diff --git a/optuna/samplers/_cmaes.py b/optuna/samplers/_cmaes.py index 13f9680f24..6c9dce9d71 100644 --- a/optuna/samplers/_cmaes.py +++ b/optuna/samplers/_cmaes.py @@ -20,6 +20,7 @@ import optuna from optuna import logging +from optuna._experimental import warn_experimental_option from optuna._imports import _LazyImport from optuna._transform import _SearchSpaceTransform from optuna.distributions import BaseDistribution @@ -282,46 +283,22 @@ def __init__( self._source_trials = source_trials if self._restart_strategy: - warnings.warn( - "`restart_strategy` option is an experimental feature." - " The interface can change in the future.", - ExperimentalWarning, - ) + warn_experimental_option("restart_strategy") if self._consider_pruned_trials: - warnings.warn( - "`consider_pruned_trials` option is an experimental feature." - " The interface can change in the future.", - ExperimentalWarning, - ) + warn_experimental_option("consider_pruned_trials") if self._use_separable_cma: - warnings.warn( - "`use_separable_cma` option is an experimental feature." - " The interface can change in the future.", - ExperimentalWarning, - ) + warn_experimental_option("use_separable_cma") if self._source_trials is not None: - warnings.warn( - "`source_trials` option is an experimental feature." - " The interface can change in the future.", - ExperimentalWarning, - ) + warn_experimental_option("source_trials") if self._with_margin: - warnings.warn( - "`with_margin` option is an experimental feature." - " The interface can change in the future.", - ExperimentalWarning, - ) + warn_experimental_option("with_margin") if self._lr_adapt: - warnings.warn( - "`lr_adapt` option is an experimental feature." - " The interface can change in the future.", - ExperimentalWarning, - ) + warn_experimental_option("lr_adapt") if source_trials is not None and (x0 is not None or sigma0 is not None): raise ValueError( diff --git a/optuna/samplers/_tpe/sampler.py b/optuna/samplers/_tpe/sampler.py index 30d6d82e27..153a1ae79c 100644 --- a/optuna/samplers/_tpe/sampler.py +++ b/optuna/samplers/_tpe/sampler.py @@ -12,6 +12,7 @@ import numpy as np +from optuna._experimental import warn_experimental_option from optuna._hypervolume import WFG from optuna._hypervolume.hssp import _solve_hssp from optuna.distributions import BaseDistribution @@ -322,44 +323,24 @@ def __init__( self._parzen_estimator_cls = _ParzenEstimator if multivariate: - warnings.warn( - "``multivariate`` option is an experimental feature." - " The interface can change in the future.", - ExperimentalWarning, - ) + warn_experimental_option("multivariate") if group: if not multivariate: raise ValueError( "``group`` option can only be enabled when ``multivariate`` is enabled." ) - warnings.warn( - "``group`` option is an experimental feature." - " The interface can change in the future.", - ExperimentalWarning, - ) + warn_experimental_option("group") self._group_decomposed_search_space = _GroupDecomposedSearchSpace(True) if constant_liar: - warnings.warn( - "``constant_liar`` option is an experimental feature." - " The interface can change in the future.", - ExperimentalWarning, - ) + warn_experimental_option("constant_liar") if constraints_func is not None: - warnings.warn( - "The ``constraints_func`` option is an experimental feature." - " The interface can change in the future.", - ExperimentalWarning, - ) + warn_experimental_option("constraints_func") if categorical_distance_func is not None: - warnings.warn( - "The ``categorical_distance_func`` option is an experimental feature." - " The interface can change in the future.", - ExperimentalWarning, - ) + warn_experimental_option("categorical_distance_func") def reseed_rng(self) -> None: self._rng.rng.seed() diff --git a/optuna/samplers/nsgaii/_sampler.py b/optuna/samplers/nsgaii/_sampler.py index 1d8f92d6f2..0a89c61bb9 100644 --- a/optuna/samplers/nsgaii/_sampler.py +++ b/optuna/samplers/nsgaii/_sampler.py @@ -9,6 +9,7 @@ import warnings import optuna +from optuna._experimental import warn_experimental_option from optuna.distributions import BaseDistribution from optuna.exceptions import ExperimentalWarning from optuna.samplers._base import BaseSampler @@ -161,31 +162,15 @@ def __init__( raise ValueError("`population_size` must be greater than or equal to 2.") if constraints_func is not None: - warnings.warn( - "The constraints_func option is an experimental feature." - " The interface can change in the future.", - ExperimentalWarning, - ) + warn_experimental_option("constraints_func") if after_trial_strategy is not None: - warnings.warn( - "The after_trial_strategy option is an experimental feature." - " The interface can change in the future.", - ExperimentalWarning, - ) + warn_experimental_option("after_trial_strategy") if child_generation_strategy is not None: - warnings.warn( - "The child_generation_strategy option is an experimental feature." - " The interface can change in the future.", - ExperimentalWarning, - ) + warn_experimental_option("child_generation_strategy") if elite_population_selection_strategy is not None: - warnings.warn( - "The elite_population_selection_strategy option is an experimental feature." - " The interface can change in the future.", - ExperimentalWarning, - ) + warn_experimental_option("elite_population_selection_strategy") if crossover is None: crossover = UniformCrossover(swapping_prob) diff --git a/optuna/visualization/_pareto_front.py b/optuna/visualization/_pareto_front.py index 43254d0314..755ca4ba3f 100644 --- a/optuna/visualization/_pareto_front.py +++ b/optuna/visualization/_pareto_front.py @@ -7,6 +7,7 @@ import warnings import optuna +from optuna._experimental import warn_experimental_option from optuna.exceptions import ExperimentalWarning from optuna.study import Study from optuna.study._multi_objective import _get_pareto_front_trials_by_trials @@ -252,11 +253,7 @@ def _get_pareto_front_info( ) if constraints_func is not None: - warnings.warn( - "``constraints_func`` argument is an experimental feature." - " The interface can change in the future.", - ExperimentalWarning, - ) + warn_experimental_option("constraints_func") feasible_trials = [] infeasible_trials = [] for trial in study.get_trials(deepcopy=False, states=(TrialState.COMPLETE,)): From 41f50401cb21702c2f48b10772d32cfed6553c54 Mon Sep 17 00:00:00 2001 From: nabenabe0928 Date: Wed, 8 May 2024 06:15:21 +0200 Subject: [PATCH 2/3] Apply formatter --- optuna/importance/__init__.py | 2 -- optuna/samplers/_cmaes.py | 2 -- optuna/samplers/_tpe/sampler.py | 1 - optuna/samplers/nsgaii/_sampler.py | 2 -- optuna/visualization/_pareto_front.py | 1 - 5 files changed, 8 deletions(-) diff --git a/optuna/importance/__init__.py b/optuna/importance/__init__.py index 634e8bdbc0..3849ee9dc0 100644 --- a/optuna/importance/__init__.py +++ b/optuna/importance/__init__.py @@ -2,10 +2,8 @@ from typing import Dict from typing import List from typing import Optional -import warnings from optuna._experimental import warn_experimental_option -from optuna.exceptions import ExperimentalWarning from optuna.importance._base import BaseImportanceEvaluator from optuna.importance._fanova import FanovaImportanceEvaluator from optuna.importance._mean_decrease_impurity import MeanDecreaseImpurityImportanceEvaluator diff --git a/optuna/samplers/_cmaes.py b/optuna/samplers/_cmaes.py index 6c9dce9d71..bdc064c1ea 100644 --- a/optuna/samplers/_cmaes.py +++ b/optuna/samplers/_cmaes.py @@ -14,7 +14,6 @@ from typing import Tuple from typing import TYPE_CHECKING from typing import Union -import warnings import numpy as np @@ -26,7 +25,6 @@ from optuna.distributions import BaseDistribution from optuna.distributions import FloatDistribution from optuna.distributions import IntDistribution -from optuna.exceptions import ExperimentalWarning from optuna.samplers import BaseSampler from optuna.samplers._lazy_random_state import LazyRandomState from optuna.search_space import IntersectionSearchSpace diff --git a/optuna/samplers/_tpe/sampler.py b/optuna/samplers/_tpe/sampler.py index 153a1ae79c..7666657c81 100644 --- a/optuna/samplers/_tpe/sampler.py +++ b/optuna/samplers/_tpe/sampler.py @@ -17,7 +17,6 @@ from optuna._hypervolume.hssp import _solve_hssp from optuna.distributions import BaseDistribution from optuna.distributions import CategoricalChoiceType -from optuna.exceptions import ExperimentalWarning from optuna.logging import get_logger from optuna.samplers._base import _CONSTRAINTS_KEY from optuna.samplers._base import _process_constraints_after_trial diff --git a/optuna/samplers/nsgaii/_sampler.py b/optuna/samplers/nsgaii/_sampler.py index 0a89c61bb9..7c603b5593 100644 --- a/optuna/samplers/nsgaii/_sampler.py +++ b/optuna/samplers/nsgaii/_sampler.py @@ -6,12 +6,10 @@ import hashlib from typing import Any from typing import TYPE_CHECKING -import warnings import optuna from optuna._experimental import warn_experimental_option from optuna.distributions import BaseDistribution -from optuna.exceptions import ExperimentalWarning from optuna.samplers._base import BaseSampler from optuna.samplers._lazy_random_state import LazyRandomState from optuna.samplers._random import RandomSampler diff --git a/optuna/visualization/_pareto_front.py b/optuna/visualization/_pareto_front.py index 755ca4ba3f..0b31be2488 100644 --- a/optuna/visualization/_pareto_front.py +++ b/optuna/visualization/_pareto_front.py @@ -8,7 +8,6 @@ import optuna from optuna._experimental import warn_experimental_option -from optuna.exceptions import ExperimentalWarning from optuna.study import Study from optuna.study._multi_objective import _get_pareto_front_trials_by_trials from optuna.trial import FrozenTrial From c4de8281ca278531f581a380e52c4aaf336e6275 Mon Sep 17 00:00:00 2001 From: nabenabe0928 Date: Wed, 15 May 2024 07:09:54 +0200 Subject: [PATCH 3/3] Replace option with argument Co-authored-by: Naoto Mizuno --- optuna/_experimental.py | 4 ++-- optuna/importance/__init__.py | 4 ++-- optuna/samplers/_cmaes.py | 14 +++++++------- optuna/samplers/_tpe/sampler.py | 12 ++++++------ optuna/samplers/nsgaii/_sampler.py | 10 +++++----- optuna/visualization/_pareto_front.py | 4 ++-- 6 files changed, 24 insertions(+), 24 deletions(-) diff --git a/optuna/_experimental.py b/optuna/_experimental.py index 6202d36cf3..8f2d69666c 100644 --- a/optuna/_experimental.py +++ b/optuna/_experimental.py @@ -26,9 +26,9 @@ """ -def warn_experimental_option(option_name: str) -> None: +def warn_experimental_argument(option_name: str) -> None: warnings.warn( - f"``{option_name}`` option is an experimental feature." + f"Argument ``{option_name}`` is an experimental feature." " The interface can change in the future.", ExperimentalWarning, ) diff --git a/optuna/importance/__init__.py b/optuna/importance/__init__.py index 3849ee9dc0..e21aad7b7c 100644 --- a/optuna/importance/__init__.py +++ b/optuna/importance/__init__.py @@ -3,7 +3,7 @@ from typing import List from typing import Optional -from optuna._experimental import warn_experimental_option +from optuna._experimental import warn_experimental_argument from optuna.importance._base import BaseImportanceEvaluator from optuna.importance._fanova import FanovaImportanceEvaluator from optuna.importance._mean_decrease_impurity import MeanDecreaseImpurityImportanceEvaluator @@ -118,5 +118,5 @@ def get_param_importances( else: return dict((param, value / s) for (param, value) in res.items()) else: - warn_experimental_option("normalize") + warn_experimental_argument("normalize") return res diff --git a/optuna/samplers/_cmaes.py b/optuna/samplers/_cmaes.py index bdc064c1ea..475f1780af 100644 --- a/optuna/samplers/_cmaes.py +++ b/optuna/samplers/_cmaes.py @@ -19,7 +19,7 @@ import optuna from optuna import logging -from optuna._experimental import warn_experimental_option +from optuna._experimental import warn_experimental_argument from optuna._imports import _LazyImport from optuna._transform import _SearchSpaceTransform from optuna.distributions import BaseDistribution @@ -281,22 +281,22 @@ def __init__( self._source_trials = source_trials if self._restart_strategy: - warn_experimental_option("restart_strategy") + warn_experimental_argument("restart_strategy") if self._consider_pruned_trials: - warn_experimental_option("consider_pruned_trials") + warn_experimental_argument("consider_pruned_trials") if self._use_separable_cma: - warn_experimental_option("use_separable_cma") + warn_experimental_argument("use_separable_cma") if self._source_trials is not None: - warn_experimental_option("source_trials") + warn_experimental_argument("source_trials") if self._with_margin: - warn_experimental_option("with_margin") + warn_experimental_argument("with_margin") if self._lr_adapt: - warn_experimental_option("lr_adapt") + warn_experimental_argument("lr_adapt") if source_trials is not None and (x0 is not None or sigma0 is not None): raise ValueError( diff --git a/optuna/samplers/_tpe/sampler.py b/optuna/samplers/_tpe/sampler.py index 7666657c81..6ef8d933fc 100644 --- a/optuna/samplers/_tpe/sampler.py +++ b/optuna/samplers/_tpe/sampler.py @@ -12,7 +12,7 @@ import numpy as np -from optuna._experimental import warn_experimental_option +from optuna._experimental import warn_experimental_argument from optuna._hypervolume import WFG from optuna._hypervolume.hssp import _solve_hssp from optuna.distributions import BaseDistribution @@ -322,24 +322,24 @@ def __init__( self._parzen_estimator_cls = _ParzenEstimator if multivariate: - warn_experimental_option("multivariate") + warn_experimental_argument("multivariate") if group: if not multivariate: raise ValueError( "``group`` option can only be enabled when ``multivariate`` is enabled." ) - warn_experimental_option("group") + warn_experimental_argument("group") self._group_decomposed_search_space = _GroupDecomposedSearchSpace(True) if constant_liar: - warn_experimental_option("constant_liar") + warn_experimental_argument("constant_liar") if constraints_func is not None: - warn_experimental_option("constraints_func") + warn_experimental_argument("constraints_func") if categorical_distance_func is not None: - warn_experimental_option("categorical_distance_func") + warn_experimental_argument("categorical_distance_func") def reseed_rng(self) -> None: self._rng.rng.seed() diff --git a/optuna/samplers/nsgaii/_sampler.py b/optuna/samplers/nsgaii/_sampler.py index 7c603b5593..3d242fbf88 100644 --- a/optuna/samplers/nsgaii/_sampler.py +++ b/optuna/samplers/nsgaii/_sampler.py @@ -8,7 +8,7 @@ from typing import TYPE_CHECKING import optuna -from optuna._experimental import warn_experimental_option +from optuna._experimental import warn_experimental_argument from optuna.distributions import BaseDistribution from optuna.samplers._base import BaseSampler from optuna.samplers._lazy_random_state import LazyRandomState @@ -160,15 +160,15 @@ def __init__( raise ValueError("`population_size` must be greater than or equal to 2.") if constraints_func is not None: - warn_experimental_option("constraints_func") + warn_experimental_argument("constraints_func") if after_trial_strategy is not None: - warn_experimental_option("after_trial_strategy") + warn_experimental_argument("after_trial_strategy") if child_generation_strategy is not None: - warn_experimental_option("child_generation_strategy") + warn_experimental_argument("child_generation_strategy") if elite_population_selection_strategy is not None: - warn_experimental_option("elite_population_selection_strategy") + warn_experimental_argument("elite_population_selection_strategy") if crossover is None: crossover = UniformCrossover(swapping_prob) diff --git a/optuna/visualization/_pareto_front.py b/optuna/visualization/_pareto_front.py index 0b31be2488..2f19a38ee8 100644 --- a/optuna/visualization/_pareto_front.py +++ b/optuna/visualization/_pareto_front.py @@ -7,7 +7,7 @@ import warnings import optuna -from optuna._experimental import warn_experimental_option +from optuna._experimental import warn_experimental_argument from optuna.study import Study from optuna.study._multi_objective import _get_pareto_front_trials_by_trials from optuna.trial import FrozenTrial @@ -252,7 +252,7 @@ def _get_pareto_front_info( ) if constraints_func is not None: - warn_experimental_option("constraints_func") + warn_experimental_argument("constraints_func") feasible_trials = [] infeasible_trials = [] for trial in study.get_trials(deepcopy=False, states=(TrialState.COMPLETE,)):