forked from Ikalus1988/MisakaNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_retrieval_noisebench.py
More file actions
80 lines (43 loc) · 1.73 KB
/
Copy pathtest_retrieval_noisebench.py
File metadata and controls
80 lines (43 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"""Tests for #481: Retrieval NoiseBench metrics."""
import pytest
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
from scripts.retrieval_noisebench import (
_forbidden_hit_rate,
_mrr,
_precision_at_k,
)
# ── Precision@K ──
def test_precision_at_k_all_relevant():
assert _precision_at_k(["a", "b", "c"], {"a", "b", "c"}, 3) == 1.0
def test_precision_at_k_none_relevant():
assert _precision_at_k(["x", "y", "z"], {"a", "b"}, 3) == 0.0
def test_precision_at_k_partial():
assert _precision_at_k(["a", "x", "b"], {"a", "b"}, 3) == pytest.approx(2 / 3)
def test_precision_at_k_k1():
assert _precision_at_k(["a", "x", "y"], {"a"}, 1) == 1.0
assert _precision_at_k(["x", "a", "y"], {"a"}, 1) == 0.0
def test_precision_at_k_empty():
assert _precision_at_k([], {"a"}, 3) == 0.0
# ── MRR ──
def test_mrr_first():
assert _mrr(["a", "b", "c"], {"a"}) == 1.0
def test_mrr_second():
assert _mrr(["x", "a", "c"], {"a"}) == 0.5
def test_mrr_third():
assert _mrr(["x", "y", "a"], {"a"}) == pytest.approx(1 / 3)
def test_mrr_none():
assert _mrr(["x", "y", "z"], {"a"}) == 0.0
def test_mrr_multiple_relevant():
# Should return 1/rank of FIRST relevant
assert _mrr(["x", "a", "b"], {"a", "b"}) == 0.5
# ── Forbidden Hit Rate ──
def test_forbidden_none():
assert _forbidden_hit_rate(["a", "b", "c"], {"x"}, 3) == 0.0
def test_forbidden_one():
assert _forbidden_hit_rate(["a", "x", "b"], {"x"}, 3) == pytest.approx(1 / 3)
def test_forbidden_multiple():
assert _forbidden_hit_rate(["x", "y", "z"], {"x", "y", "z"}, 3) == 1.0
def test_forbidden_empty():
assert _forbidden_hit_rate(["a", "b"], set(), 3) == 0.0