From d15955bd6f189f6b4a8747f4d581434cfc330e98 Mon Sep 17 00:00:00 2001 From: Hiroki Takizawa Date: Wed, 24 Apr 2024 03:54:01 +0900 Subject: [PATCH 1/4] drop implicit study creation in 'ask' command --- optuna/cli.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/optuna/cli.py b/optuna/cli.py index 96793437b7..65016c9094 100644 --- a/optuna/cli.py +++ b/optuna/cli.py @@ -831,7 +831,10 @@ def take_action(self, parsed_args: Namespace) -> int: ) except KeyError: - study = optuna.create_study(**create_study_kwargs) + raise KeyError( + "Implicit study creation within the 'ask' command was dropped in Optuna v4.0." + "Please use the 'create-study' command beforehand." + ) trial = study.ask(fixed_distributions=search_space) self.logger.info(f"Asked trial {trial.number} with parameters {trial.params}.") From 7ae6e1071312e7ff4c00b7904d0442ded96f17ba Mon Sep 17 00:00:00 2001 From: Hiroki Takizawa Date: Wed, 24 Apr 2024 03:58:38 +0900 Subject: [PATCH 2/4] create study beforehand in test_cli.py --- tests/test_cli.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/test_cli.py b/tests/test_cli.py index b7503a4b8b..a104941ec1 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1223,6 +1223,9 @@ def test_ask( with NamedTemporaryFilePool() as tf: db_url = "sqlite:///{}".format(tf.name) + args = ["optuna", "create-study", "--storage", db_url, "--study-name", study_name] + subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + args = [ "optuna", "ask", @@ -1275,6 +1278,9 @@ def test_ask_flatten( with NamedTemporaryFilePool() as tf: db_url = "sqlite:///{}".format(tf.name) + args = ["optuna", "create-study", "--storage", db_url, "--study-name", study_name] + subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + args = [ "optuna", "ask", @@ -1318,6 +1324,9 @@ def test_ask_empty_search_space(output_format: str) -> None: with NamedTemporaryFilePool() as tf: db_url = "sqlite:///{}".format(tf.name) + args = ["optuna", "create-study", "--storage", db_url, "--study-name", study_name] + subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + args = [ "optuna", "ask", @@ -1351,6 +1360,9 @@ def test_ask_empty_search_space_flatten(output_format: str) -> None: with NamedTemporaryFilePool() as tf: db_url = "sqlite:///{}".format(tf.name) + args = ["optuna", "create-study", "--storage", db_url, "--study-name", study_name] + subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + args = [ "optuna", "ask", @@ -1388,6 +1400,9 @@ def test_ask_sampler_kwargs_without_sampler() -> None: with NamedTemporaryFilePool() as tf: db_url = "sqlite:///{}".format(tf.name) + args = ["optuna", "create-study", "--storage", db_url, "--study-name", study_name] + subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + args = [ "optuna", "ask", @@ -1478,6 +1493,9 @@ def test_tell() -> None: with NamedTemporaryFilePool() as tf: db_url = "sqlite:///{}".format(tf.name) + args = ["optuna", "create-study", "--storage", db_url, "--study-name", study_name] + subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + output: Any = subprocess.check_output( [ "optuna", @@ -1555,6 +1573,9 @@ def test_tell_with_nan() -> None: with NamedTemporaryFilePool() as tf: db_url = "sqlite:///{}".format(tf.name) + args = ["optuna", "create-study", "--storage", db_url, "--study-name", study_name] + subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + output: Any = subprocess.check_output( [ "optuna", From 7d1307bd0e05eaf94770958183fbd489715cdbd8 Mon Sep 17 00:00:00 2001 From: Hiroki Takizawa Date: Wed, 24 Apr 2024 18:32:00 +0900 Subject: [PATCH 3/4] Update optuna/cli.py Co-authored-by: Naoto Mizuno --- optuna/cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/optuna/cli.py b/optuna/cli.py index 65016c9094..8951634752 100644 --- a/optuna/cli.py +++ b/optuna/cli.py @@ -832,7 +832,7 @@ def take_action(self, parsed_args: Namespace) -> int: except KeyError: raise KeyError( - "Implicit study creation within the 'ask' command was dropped in Optuna v4.0." + "Implicit study creation within the 'ask' command was dropped in Optuna v4.0.0. " "Please use the 'create-study' command beforehand." ) trial = study.ask(fixed_distributions=search_space) From ccf4f2118b033a43d04bab4dc901a0a5bb854ae6 Mon Sep 17 00:00:00 2001 From: Hiroki Takizawa Date: Thu, 25 Apr 2024 15:27:07 +0900 Subject: [PATCH 4/4] add `test_ask_without_create_study_beforehand` --- tests/test_cli.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/test_cli.py b/tests/test_cli.py index a104941ec1..e9fd22170f 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1421,6 +1421,36 @@ def test_ask_sampler_kwargs_without_sampler() -> None: assert "`--sampler_kwargs` is set without `--sampler`." in error_message +@pytest.mark.skip_coverage +def test_ask_without_create_study_beforehand() -> None: + study_name = "test_study" + search_space = ( + '{"x": {"name": "FloatDistribution", "attributes": {"low": 0.0, "high": 1.0}}, ' + '"y": {"name": "CategoricalDistribution", "attributes": {"choices": ["foo"]}}}' + ) + + with NamedTemporaryFilePool() as tf: + db_url = "sqlite:///{}".format(tf.name) + + args = [ + "optuna", + "ask", + "--storage", + db_url, + "--study-name", + study_name, + "--search-space", + search_space, + ] + + result = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + error_message = result.stderr.decode() + assert ( + "Implicit study creation within the 'ask' command was dropped in Optuna v4.0.0." + in error_message + ) + + @pytest.mark.skip_coverage @pytest.mark.parametrize( "direction,directions,sampler,sampler_kwargs",