-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Retrieval Metrics: Updating HitRate and MRR for Evaluation@K document…
…s retrieved. Also adding RR as a separate metric (#12997) * Updating metrics: MRR renamed to RR, HitRate updated for multi-doc evaluation and new separate MRR implementation * Updated MRR and HitRate with requested changes * Iteration w/ class attribute implementation for the calculation option choice
- Loading branch information
Showing
2 changed files
with
176 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import pytest | ||
from llama_index.core.evaluation.retrieval.metrics import HitRate, MRR | ||
|
||
|
||
# Test cases for the updated HitRate class using instance attribute | ||
@pytest.mark.parametrize( | ||
("expected_ids", "retrieved_ids", "use_granular", "expected_result"), | ||
[ | ||
(["id1", "id2", "id3"], ["id3", "id1", "id2", "id4"], False, 1.0), | ||
(["id1", "id2", "id3", "id4"], ["id1", "id5", "id2"], True, 2 / 4), | ||
(["id1", "id2"], ["id3", "id4"], False, 0.0), | ||
(["id1", "id2"], ["id2", "id1", "id7"], True, 2 / 2), | ||
], | ||
) | ||
def test_hit_rate(expected_ids, retrieved_ids, use_granular, expected_result): | ||
hr = HitRate() | ||
hr.use_granular_hit_rate = use_granular | ||
result = hr.compute(expected_ids=expected_ids, retrieved_ids=retrieved_ids) | ||
assert result.score == pytest.approx(expected_result) | ||
|
||
|
||
# Test cases for the updated MRR class using instance attribute | ||
@pytest.mark.parametrize( | ||
("expected_ids", "retrieved_ids", "use_granular", "expected_result"), | ||
[ | ||
(["id1", "id2", "id3"], ["id3", "id1", "id2", "id4"], False, 1 / 1), | ||
(["id1", "id2", "id3", "id4"], ["id5", "id1"], False, 1 / 2), | ||
(["id1", "id2"], ["id3", "id4"], False, 0.0), | ||
(["id1", "id2"], ["id2", "id1", "id7"], False, 1 / 1), | ||
( | ||
["id1", "id2", "id3"], | ||
["id3", "id1", "id2", "id4"], | ||
True, | ||
(1 / 1 + 1 / 2 + 1 / 3) / 3, | ||
), | ||
( | ||
["id1", "id2", "id3", "id4"], | ||
["id1", "id2", "id5"], | ||
True, | ||
(1 / 1 + 1 / 2) / 2, | ||
), | ||
(["id1", "id2"], ["id1", "id7", "id15", "id2"], True, (1 / 1 + 1 / 4) / 2), | ||
], | ||
) | ||
def test_mrr(expected_ids, retrieved_ids, use_granular, expected_result): | ||
mrr = MRR() | ||
mrr.use_granular_mrr = use_granular | ||
result = mrr.compute(expected_ids=expected_ids, retrieved_ids=retrieved_ids) | ||
assert result.score == pytest.approx(expected_result) | ||
|
||
|
||
# Test cases for exceptions handling for both HitRate and MRR | ||
@pytest.mark.parametrize( | ||
("expected_ids", "retrieved_ids", "use_granular"), | ||
[ | ||
( | ||
None, | ||
["id3", "id1", "id2", "id4"], | ||
False, | ||
), # None expected_ids should trigger ValueError | ||
( | ||
["id1", "id2", "id3"], | ||
None, | ||
True, | ||
), # None retrieved_ids should trigger ValueError | ||
([], [], False), # Empty IDs should trigger ValueError | ||
], | ||
) | ||
def test_exceptions(expected_ids, retrieved_ids, use_granular): | ||
with pytest.raises(ValueError): | ||
hr = HitRate() | ||
hr.use_granular_hit_rate = use_granular | ||
hr.compute(expected_ids=expected_ids, retrieved_ids=retrieved_ids) | ||
|
||
mrr = MRR() | ||
mrr.use_granular_mrr = use_granular | ||
mrr.compute(expected_ids=expected_ids, retrieved_ids=retrieved_ids) |