From 4802c4059f564f04f9dfbd2948b84ef1b17b8ad9 Mon Sep 17 00:00:00 2001 From: Olivier Delalleau <507137+odelalleau@users.noreply.github.com> Date: Mon, 20 Mar 2023 14:30:38 -0400 Subject: [PATCH] Fix handling of quoted strings with the nevergrad sweeper (#2613) This also changes the type of non-sweep variables, that all become strings in the eye of nevergrad, while in the past they could also be for instance int or float. This should not be an issue since nevergrad is not supposed to touch them. --- .../hydra_plugins/hydra_nevergrad_sweeper/_impl.py | 2 +- .../tests/test_nevergrad_sweeper_plugin.py | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/plugins/hydra_nevergrad_sweeper/hydra_plugins/hydra_nevergrad_sweeper/_impl.py b/plugins/hydra_nevergrad_sweeper/hydra_plugins/hydra_nevergrad_sweeper/_impl.py index 70f9ba38a85..6424e4c7f95 100644 --- a/plugins/hydra_nevergrad_sweeper/hydra_plugins/hydra_nevergrad_sweeper/_impl.py +++ b/plugins/hydra_nevergrad_sweeper/hydra_plugins/hydra_nevergrad_sweeper/_impl.py @@ -60,7 +60,7 @@ def create_nevergrad_param_from_config( def create_nevergrad_parameter_from_override(override: Override) -> Any: val = override.value() if not override.is_sweep_override(): - return val + return override.get_value_element_as_str() if override.is_choice_sweep(): assert isinstance(val, ChoiceSweep) vals = [x for x in override.sweep_iterator(transformer=Transformer.encode)] diff --git a/plugins/hydra_nevergrad_sweeper/tests/test_nevergrad_sweeper_plugin.py b/plugins/hydra_nevergrad_sweeper/tests/test_nevergrad_sweeper_plugin.py index bdc09c3a092..033f920af09 100644 --- a/plugins/hydra_nevergrad_sweeper/tests/test_nevergrad_sweeper_plugin.py +++ b/plugins/hydra_nevergrad_sweeper/tests/test_nevergrad_sweeper_plugin.py @@ -36,6 +36,8 @@ def assert_ng_param_equals(expected: Any, actual: Any) -> None: elif isinstance(actual, ng.p.Log) or isinstance(actual, ng.p.Scalar): assert expected.bounds == actual.bounds assert expected.integer == actual.integer + elif isinstance(actual, str): + assert expected == actual else: assert False, f"Unexpected type: {type(actual)}" @@ -95,6 +97,13 @@ def test_create_nevergrad_parameter_from_config( "key=tag(log, int(interval(1,12)))", get_scalar_with_integer_bounds(lower=1, upper=12, type=ng.p.Log), ), + ("key=abc", "abc"), + ("key='a&c'", "'a&c'"), + ("key='${f:${x}}'", "'${f:${x}}'"), + (r"key=$\{f\:$\{x\}\}", r"$\{f\:$\{x\}\}"), + ("key=0", "0"), + ("key=true", "True"), + ("key=null", "null"), ], ) def test_create_nevergrad_parameter_from_override(