From 277c10f8b77e73bdac9800a0f0c4e3299aad16c2 Mon Sep 17 00:00:00 2001 From: Simon-Martin Schroeder Date: Tue, 19 Dec 2023 20:41:22 +0100 Subject: [PATCH] ConfigurationSampler.contains_subset_of: Handle unset --- experitur/core/configurators.py | 17 +++++++++------- tests/core/test_configurators.py | 34 +++++++++++++++++++++++--------- 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/experitur/core/configurators.py b/experitur/core/configurators.py index 5bf25a9..ae9af04 100644 --- a/experitur/core/configurators.py +++ b/experitur/core/configurators.py @@ -173,26 +173,29 @@ def contains_subset_of( if exclude is None: exclude = set() - values = { + # The parameter values that should contain the supplied configuration, without `exclude`ed + # If the values contain , a trial can assume any value. + parameter_values = { k: v for k, v in self.configurator.parameter_values.items() - if k not in exclude + if (k not in exclude) and (unset not in v) } - exclude = exclude.union(self.configurator.parameter_values.keys()) - conf_parameters = configuration.get("parameters", {}) # Check if all configured parameters are contained if any( k not in conf_parameters or conf_parameters[k] not in v - for k, v in values.items() + for k, v in parameter_values.items() ): return False - parent_params = {k: v for k, v in conf_parameters.items() if k not in values} + parent_params = { + k: v for k, v in conf_parameters.items() if k not in parameter_values + } # Let parents check the rest of the configuration + exclude = exclude.union(self.configurator.parameter_values.keys()) return self.parent.contains_subset_of( dict(configuration, parameters=parent_params), exclude=exclude ) @@ -202,7 +205,7 @@ def contains_superset_of(self, configuration: Mapping) -> bool: Return True if there exists a sample that is a superset of `configuration`. - `configuration` does not match if it contains additional keys not produced by the sampler (or its parents). - - `configuration` matches if it lacks keys produced by the sampler. + - `configuration` matches if it lacks keys produced by the sampler. - `configuration` does not match if values for existing keys are different. """ diff --git a/tests/core/test_configurators.py b/tests/core/test_configurators.py index b877130..dd05fb3 100644 --- a/tests/core/test_configurators.py +++ b/tests/core/test_configurators.py @@ -20,13 +20,18 @@ def test_empty_parameter_product(): def test_Const(): - configurator = Const({"a": 1, "b": 2}, c=3) + configurator = Const({"a": 1, "b": 2}, c=3, d=unset) # Test __str__ str(configurator) # Assert correct behavior of "parameter_values" - assert configurator.parameter_values == {"a": (1,), "b": (2,), "c": (3,)} + assert configurator.parameter_values == { + "a": (1,), + "b": (2,), + "c": (3,), + "d": (unset,), + } sampler = configurator.build_sampler() @@ -39,19 +44,26 @@ def test_Const(): # Assert exististence of all grid cells assert samples == { - (("a", 1), ("b", 2), ("c", 3)), + (("a", 1), ("b", 2), ("c", 3), ("d", unset)), } @pytest.mark.parametrize("cls", [Grid, RandomGrid]) def test_Grid(cls): - configurator: Union[Grid, RandomGrid] = cls({"a": [1, 2], "b": [3, 4], "c": [0]}) + configurator: Union[Grid, RandomGrid] = cls( + {"a": [1, 2], "b": [3, 4], "c": [0], "d": [0, unset]} + ) # Test __str__ str(configurator) # Assert correct behavior of "parameter_values" - assert configurator.parameter_values == {"a": (1, 2), "b": (3, 4), "c": (0,)} + assert configurator.parameter_values == { + "a": (1, 2), + "b": (3, 4), + "c": (0,), + "d": (0, unset), + } sampler = configurator.build_sampler() @@ -65,10 +77,14 @@ def test_Grid(cls): # Assert exististence of all grid cells assert samples == { - (("a", 2), ("b", 4), ("c", 0)), - (("a", 1), ("b", 4), ("c", 0)), - (("a", 2), ("b", 3), ("c", 0)), - (("a", 1), ("b", 3), ("c", 0)), + (("a", 2), ("b", 4), ("c", 0), ("d", 0)), + (("a", 1), ("b", 4), ("c", 0), ("d", 0)), + (("a", 2), ("b", 3), ("c", 0), ("d", 0)), + (("a", 1), ("b", 3), ("c", 0), ("d", 0)), + (("a", 2), ("b", 4), ("c", 0), ("d", unset)), + (("a", 1), ("b", 4), ("c", 0), ("d", unset)), + (("a", 2), ("b", 3), ("c", 0), ("d", unset)), + (("a", 1), ("b", 3), ("c", 0), ("d", unset)), }