Skip to content

Commit

Permalink
bug: fix MRR and MAP calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
Amna Mubashar authored and Amna Mubashar committed Jun 11, 2024
1 parent 58dd972 commit 4676247
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 20 deletions.
26 changes: 14 additions & 12 deletions haystack/components/evaluators/document_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,24 +69,26 @@ def run(

for ground_truth, retrieved in zip(ground_truth_documents, retrieved_documents):
score = 0.0
average_precision = 0.0
relevant_documents = 0
ground_truth_content = []

for ground_document in ground_truth:
if ground_document.content is None:
continue
ground_truth_content.append(ground_document.content)

average_precision = 0.0
relevant_documents = 0

for rank, retrieved_document in enumerate(retrieved):
if retrieved_document.content is None:
continue
for rank, retrieved_document in enumerate(retrieved):
if retrieved_document.content is None:
continue

if ground_document.content in retrieved_document.content:
relevant_documents += 1
average_precision += relevant_documents / (rank + 1)
if relevant_documents > 0:
score = average_precision / relevant_documents
if retrieved_document.content in ground_truth_content:
relevant_documents += 1
average_precision += relevant_documents / (rank + 1)
if relevant_documents > 0:
score = average_precision / relevant_documents
individual_scores.append(score)

score = sum(individual_scores) / len(retrieved_documents)
score = sum(individual_scores) / len(ground_truth_documents)

return {"score": score, "individual_scores": individual_scores}
20 changes: 13 additions & 7 deletions haystack/components/evaluators/document_mrr.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,23 @@ def run(

for ground_truth, retrieved in zip(ground_truth_documents, retrieved_documents):
score = 0.0
flag = False
ground_truth_content = []

for ground_document in ground_truth:
if ground_document.content is None:
continue
ground_truth_content.append(ground_document.content)

for rank, retrieved_document in enumerate(retrieved):
if retrieved_document.content is None:
continue

if ground_document.content in retrieved_document.content:
score = 1 / (rank + 1)
break
for rank, retrieved_document in enumerate(retrieved):
if flag:
break
if retrieved_document.content is None:
continue
if retrieved_document.content in ground_truth_content:
score = 1 / (rank + 1)
flag = True
break
individual_scores.append(score)

score = sum(individual_scores) / len(retrieved_documents)
Expand Down
12 changes: 11 additions & 1 deletion test/components/evaluators/test_document_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,17 @@ def test_run_with_complex_data():
],
],
)
assert result == {"individual_scores": [1.0, 0.8333333333333333, 1.0, 0.5, 0.0, 1.0], "score": 0.7222222222222222}
assert result == {
"individual_scores": [
1.0,
pytest.approx(0.8333333333333333),
1.0,
pytest.approx(0.5833333333333333),
0.0,
pytest.approx(0.8055555555555555),
],
"score": pytest.approx(0.7037037037037037),
}


def test_run_with_different_lengths():
Expand Down

0 comments on commit 4676247

Please sign in to comment.