diff --git a/optuna/_experimental.py b/optuna/_experimental.py index 40161dc034..8f2d69666c 100644 --- a/optuna/_experimental.py +++ b/optuna/_experimental.py @@ -26,6 +26,14 @@ """ +def warn_experimental_argument(option_name: str) -> None: + warnings.warn( + f"Argument ``{option_name}`` 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..e21aad7b7c 100644 --- a/optuna/importance/__init__.py +++ b/optuna/importance/__init__.py @@ -2,9 +2,8 @@ from typing import Dict from typing import List from typing import Optional -import warnings -from optuna.exceptions import ExperimentalWarning +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 @@ -119,9 +118,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_argument("normalize") return res diff --git a/optuna/samplers/_cmaes.py b/optuna/samplers/_cmaes.py index 13f9680f24..475f1780af 100644 --- a/optuna/samplers/_cmaes.py +++ b/optuna/samplers/_cmaes.py @@ -14,18 +14,17 @@ from typing import Tuple from typing import TYPE_CHECKING from typing import Union -import warnings import numpy as np import optuna from optuna import logging +from optuna._experimental import warn_experimental_argument from optuna._imports import _LazyImport from optuna._transform import _SearchSpaceTransform 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 @@ -282,46 +281,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_argument("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_argument("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_argument("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_argument("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_argument("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_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 d24636a771..4a8abfd062 100644 --- a/optuna/samplers/_tpe/sampler.py +++ b/optuna/samplers/_tpe/sampler.py @@ -10,11 +10,11 @@ import numpy as np +from optuna._experimental import warn_experimental_argument from optuna._hypervolume import WFG 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 @@ -320,44 +320,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_argument("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_argument("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_argument("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_argument("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_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 1d8f92d6f2..3d242fbf88 100644 --- a/optuna/samplers/nsgaii/_sampler.py +++ b/optuna/samplers/nsgaii/_sampler.py @@ -6,11 +6,10 @@ import hashlib from typing import Any from typing import TYPE_CHECKING -import warnings import optuna +from optuna._experimental import warn_experimental_argument 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 @@ -161,31 +160,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_argument("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_argument("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_argument("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_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 43254d0314..2f19a38ee8 100644 --- a/optuna/visualization/_pareto_front.py +++ b/optuna/visualization/_pareto_front.py @@ -7,7 +7,7 @@ import warnings import optuna -from optuna.exceptions import ExperimentalWarning +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,11 +252,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_argument("constraints_func") feasible_trials = [] infeasible_trials = [] for trial in study.get_trials(deepcopy=False, states=(TrialState.COMPLETE,)):