Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/evaluate/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -993,11 +993,17 @@ def _merge_results(self, results):
for module_name, result in zip(self.evaluation_module_names, results):
for k, v in result.items():
if k not in duplicate_keys and not self.force_prefix:
merged_results[f"{k}"] = v
merged_key = f"{k}"
elif module_name in duplicate_counter:
merged_results[f"{module_name}_{duplicate_counter[module_name]}_{k}"] = v
merged_key = f"{module_name}_{duplicate_counter[module_name]}_{k}"
else:
merged_results[f"{module_name}_{k}"] = v
merged_key = f"{module_name}_{k}"
if merged_key in merged_results:
raise ValueError(
f"Multiple evaluation results resolve to the key {merged_key!r}. "
"Choose distinct module names or score keys."
)
merged_results[merged_key] = v

if module_name in duplicate_counter:
duplicate_counter[module_name] += 1
Expand All @@ -1021,6 +1027,9 @@ def combine(evaluations, force_prefix=False):
If `True` all scores from the modules are prefixed with their name. If
a dictionary is passed the keys are used as name otherwise the module's name.

Raises:
ValueError: If result keys collide after applying the selected prefixes.

Examples:

```py
Expand Down
52 changes: 52 additions & 0 deletions tests/test_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,58 @@ def test_metric_with_non_standard_feature_names_compute(tmp_path):
assert results == AccuracyWithNonStandardFeatureNames.expected_results()


class NamedScoreMetric(DummyMetric):
def __init__(self, key, score, **kwargs):
self.key, self.score = key, score
super().__init__(**kwargs)

def _compute(self, predictions, references):
return {self.key: self.score}


@pytest.mark.parametrize("reverse", [False, True])
@pytest.mark.parametrize(
"force_prefix, definitions, conflicting_key",
[
(False, [("model", "score", 0.1), ("baseline", "score", 0.2), ("custom", "model_score", 0.9)], "model_score"),
(True, [("a_b", "c", 0.1), ("a", "b_c", 0.9)], "a_b_c"),
],
)
def test_combined_evaluation_rejects_final_key_collision(
tmp_path, reverse, force_prefix, definitions, conflicting_key
):
modules = {
name: NamedScoreMetric(key, score, cache_dir=tmp_path, keep_in_memory=True)
for name, key, score in (reversed(definitions) if reverse else definitions)
}
with pytest.raises(ValueError, match=conflicting_key):
combine(modules, force_prefix=force_prefix).compute(predictions=[1], references=[1])


def test_combined_evaluation_rejects_numbered_key_collision(tmp_path):
first = NamedScoreMetric("score", 0.1, cache_dir=tmp_path, keep_in_memory=True)
conflicting_key = f"{first.name}_0_score"
modules = [
first,
NamedScoreMetric("score", 0.2, cache_dir=tmp_path, keep_in_memory=True),
NamedScoreMetric(conflicting_key, 0.9, cache_dir=tmp_path, keep_in_memory=True),
]
with pytest.raises(ValueError, match=conflicting_key):
combine(modules).compute(predictions=[1], references=[1])


def test_combined_evaluation_preserves_distinct_final_keys(tmp_path):
modules = {
name: NamedScoreMetric(key, score, cache_dir=tmp_path, keep_in_memory=True)
for name, key, score in [("model", "score", 0.1), ("baseline", "score", 0.2), ("custom", "other_score", 0.9)]
}
assert combine(modules).compute(predictions=[1], references=[1]) == {
"model_score": 0.1,
"baseline_score": 0.2,
"other_score": 0.9,
}


class TestEvaluationcombined_evaluation(TestCase):
def test_single_module(self):
preds, refs = DummyMetric.predictions_and_references()
Expand Down