Skip to content

Commit

Permalink
Merge pull request #5416 from eukaryo/v4pr-deprecated-optimize-command
Browse files Browse the repository at this point in the history
Remove deprecated `study optimize` CLI command
  • Loading branch information
c-bata committed May 1, 2024
2 parents 6c082c7 + 58cb6f4 commit 44611a4
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 148 deletions.
92 changes: 1 addition & 91 deletions optuna/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@
from argparse import Namespace
import datetime
from enum import Enum
from importlib.machinery import SourceFileLoader
import inspect
import json
import logging
import os
import sys
import types
from typing import Any
from typing import Dict
from typing import List
Expand Down Expand Up @@ -632,93 +630,6 @@ def take_action(self, parsed_args: Namespace) -> int:
return 0


class _StudyOptimize(_BaseCommand):
"""Start optimization of a study. Deprecated since version 2.0.0."""

def add_arguments(self, parser: ArgumentParser) -> None:
parser.add_argument(
"--n-trials",
type=int,
help="The number of trials. If this argument is not given, as many "
"trials run as possible.",
)
parser.add_argument(
"--timeout",
type=float,
help="Stop study after the given number of second(s). If this argument"
" is not given, as many trials run as possible.",
)
parser.add_argument(
"--n-jobs",
type=int,
default=1,
help="The number of parallel jobs. If this argument is set to -1, the "
"number is set to CPU counts.",
)
parser.add_argument(
"--study", default=None, help="This argument is deprecated. Use --study-name instead."
)
parser.add_argument(
"--study-name", default=None, help="The name of the study to start optimization on."
)
parser.add_argument(
"file",
help="Python script file where the objective function resides.",
)
parser.add_argument(
"method",
help="The method name of the objective function.",
)

def take_action(self, parsed_args: Namespace) -> int:
message = (
"The use of the `study optimize` command is deprecated. Please execute your Python "
"script directly instead."
)
warnings.warn(message, FutureWarning)

storage = _get_storage(parsed_args.storage, parsed_args.storage_class)

if parsed_args.study and parsed_args.study_name:
raise ValueError(
"Both `--study-name` and the deprecated `--study` was specified. "
"Please remove the `--study` flag."
)
elif parsed_args.study:
message = "The use of `--study` is deprecated. Please use `--study-name` instead."
warnings.warn(message, FutureWarning)
study = optuna.load_study(storage=storage, study_name=parsed_args.study)
elif parsed_args.study_name:
study = optuna.load_study(storage=storage, study_name=parsed_args.study_name)
else:
raise ValueError("Missing study name. Please use `--study-name`.")

# We force enabling the debug flag. As we are going to execute user codes, we want to show
# exception stack traces by default.
parsed_args.debug = True

module_name = "optuna_target_module"
target_module = types.ModuleType(module_name)
loader = SourceFileLoader(module_name, parsed_args.file)
loader.exec_module(target_module)

try:
target_method = getattr(target_module, parsed_args.method)
except AttributeError:
self.logger.error(
"Method {} not found in file {}.".format(parsed_args.method, parsed_args.file)
)
return 1

study.optimize(
target_method,
n_trials=parsed_args.n_trials,
timeout=parsed_args.timeout,
n_jobs=parsed_args.n_jobs,
)
return 0


class _StorageUpgrade(_BaseCommand):
"""Upgrade the schema of an RDB storage."""

Expand Down Expand Up @@ -914,7 +825,6 @@ def take_action(self, parsed_args: Namespace) -> int:
"trials": _Trials,
"best-trial": _BestTrial,
"best-trials": _BestTrials,
"study optimize": _StudyOptimize,
"storage upgrade": _StorageUpgrade,
"ask": _Ask,
"tell": _Tell,
Expand Down Expand Up @@ -1012,7 +922,7 @@ def _get_parser(description: str = "") -> Tuple[ArgumentParser, Dict[str, Argume

def _preprocess_argv(argv: List[str]) -> List[str]:
# Some preprocess is necessary for argv because some subcommand includes space
# (e.g. optuna study optimize, optuna storage upgrade, ...).
# (e.g. optuna storage upgrade).
argv = argv[1:] if len(argv) > 1 else ["help"]

for i in range(len(argv)):
Expand Down
57 changes: 0 additions & 57 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
from optuna.storages import JournalRedisStorage
from optuna.storages import JournalStorage
from optuna.storages import RDBStorage
from optuna.storages._base import DEFAULT_STUDY_NAME_PREFIX
from optuna.study import StudyDirection
from optuna.testing.storages import StorageSupplier
from optuna.testing.tempfile_pool import NamedTemporaryFilePool
Expand Down Expand Up @@ -1044,62 +1043,6 @@ def test_create_study_command_with_skip_if_exists() -> None:
assert study_id == new_study_id # The existing study instance is reused.


@pytest.mark.skip_coverage
def test_study_optimize_command() -> None:
with StorageSupplier("sqlite") as storage:
assert isinstance(storage, RDBStorage)
storage_url = str(storage.engine.url)

study_name = storage.get_study_name_from_id(
storage.create_new_study(directions=[StudyDirection.MINIMIZE])
)
command = [
"optuna",
"study",
"optimize",
"--study-name",
study_name,
"--n-trials",
"10",
__file__,
"objective_func",
"--storage",
storage_url,
]
subprocess.check_call(command)

study = optuna.load_study(storage=storage_url, study_name=study_name)
assert len(study.trials) == 10
assert "x" in study.best_params

# Check if a default value of study_name is stored in the storage.
assert storage.get_study_name_from_id(study._study_id).startswith(
DEFAULT_STUDY_NAME_PREFIX
)


@pytest.mark.skip_coverage
def test_study_optimize_command_inconsistent_args() -> None:
with NamedTemporaryFilePool() as tf:
db_url = "sqlite:///{}".format(tf.name)

# --study-name argument is missing.
with pytest.raises(subprocess.CalledProcessError):
subprocess.check_call(
[
"optuna",
"study",
"optimize",
"--storage",
db_url,
"--n-trials",
"10",
__file__,
"objective_func",
]
)


@pytest.mark.skip_coverage
def test_empty_argv() -> None:
command_empty = ["optuna"]
Expand Down

0 comments on commit 44611a4

Please sign in to comment.