Skip to content

Commit 05b67c3

Browse files
committed
Added unit test to test fallback to default config value when not configs are left after filtering for conditions.
Excluded log message branch from test coverage in utils.py.
1 parent 7e2628e commit 05b67c3

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

src/hypershap/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ def _ensure_monotonicity(self, coalition: np.ndarray, value: float) -> float:
217217
"Could not ensure monotonicity as none of the coalitions with one player less has been cached so far.",
218218
)
219219

220-
if value < monotone_value:
220+
if value < monotone_value: # pragma: no cover
221221
logger.debug(
222222
"Ensured monotonicity with a sub-coalition's value. Increased the value of the current coalition from %s to %s.",
223223
value,

tests/test_utils.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
from hypershap import ExplanationTask
1313
from tests.fixtures.simple_setup import SimpleBlackboxFunction
1414

15+
from ConfigSpace import Configuration
16+
1517
from hypershap.task import BaselineExplanationTask
1618
from hypershap.utils import Aggregation, RandomConfigSpaceSearcher, evaluate_aggregation
1719

@@ -144,3 +146,31 @@ def test_evaluate_aggregation() -> None:
144146
assert evaluate_aggregation(Aggregation.MAX, vals) == AGG_LIST[2]
145147
assert evaluate_aggregation(Aggregation.AVG, vals) == np.array(AGG_LIST).mean()
146148
assert abs(evaluate_aggregation(Aggregation.VAR, vals) - np.array(AGG_LIST).var()) < EPSILON
149+
150+
151+
def test_fallback_unfulfilled_conditions(simple_act_base_et: ExplanationTask) -> None:
152+
"""Test the fallback strategy when no configurations are left in random sample after filtering for conditions."""
153+
bet = BaselineExplanationTask(
154+
simple_act_base_et.config_space,
155+
simple_act_base_et.surrogate_model,
156+
simple_act_base_et.config_space.get_default_configuration(),
157+
)
158+
rcss = RandomConfigSpaceSearcher(bet)
159+
rcss.random_sample = np.array([
160+
Configuration(
161+
configuration_space=simple_act_base_et.config_space,
162+
values={
163+
"a": 0.4,
164+
"b": 0.1,
165+
},
166+
).get_array(),
167+
Configuration(
168+
configuration_space=simple_act_base_et.config_space,
169+
values={
170+
"a": 0.5,
171+
"b": 0.1,
172+
},
173+
).get_array(),
174+
])
175+
value = rcss.search(np.array([False, True]))
176+
assert value is not None

0 commit comments

Comments
 (0)