From 7cc0841bf2c3e53ed3632c0d0d3c76da3fd710b6 Mon Sep 17 00:00:00 2001 From: FOWEPJF255 Date: Mon, 9 Nov 2026 02:33:54 +0800 Subject: [PATCH] Reject ambiguous combined evaluation result keys --- src/evaluate/module.py | 15 +++++++++--- tests/test_metric.py | 52 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 3 deletions(-) diff --git a/src/evaluate/module.py b/src/evaluate/module.py index ca38b9b15..dd9a839f1 100644 --- a/src/evaluate/module.py +++ b/src/evaluate/module.py @@ -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 @@ -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 diff --git a/tests/test_metric.py b/tests/test_metric.py index 598b0f929..edde9a22f 100644 --- a/tests/test_metric.py +++ b/tests/test_metric.py @@ -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()