Skip to content

Commit

Permalink
ConfigurationSampler.contains_subset_of: Handle unset
Browse files Browse the repository at this point in the history
  • Loading branch information
moi90 committed Dec 19, 2023
1 parent 1e85064 commit 277c10f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
17 changes: 10 additions & 7 deletions experitur/core/configurators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <unset>, 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
)
Expand All @@ -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.
"""

Expand Down
34 changes: 25 additions & 9 deletions tests/core/test_configurators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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()

Expand All @@ -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)),
}


Expand Down

0 comments on commit 277c10f

Please # to comment.