-
Notifications
You must be signed in to change notification settings - Fork 31
/
testtreceval.py
123 lines (94 loc) · 5.48 KB
/
testtreceval.py
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import unittest
from trectools import TrecRun, TrecQrel, TrecEval
class TestTrecEval(unittest.TestCase):
def setUp(self):
run1 = TrecRun("./unittests/files/r4.run")
qrels1 = TrecQrel("./unittests/files/qrel1.txt")
run2 = TrecRun("./unittests/files/input.uic0301")
qrels2 = TrecQrel("./unittests/files/robust03_cs_qrels.txt")
# Contains the first 30 documents for the first 10 topics in input.uic0301
run3 = TrecRun("./unittests/files/input.uic0301_top30")
self.common_topics = ["303", "307", "310", "314", "320", "322", "325", "330", "336", "341"]
self.teval1 = TrecEval(run1, qrels1)
self.teval2 = TrecEval(run2, qrels2)
self.teval3 = TrecEval(run3, qrels2)
def tearDown(self):
pass
def test_get_reciprocal_rank(self):
value = self.teval1.get_reciprocal_rank(depth=1000, trec_eval=True)
self.assertAlmostEqual(value, 0.5000, places=4)
value = self.teval2.get_reciprocal_rank(depth=1000, trec_eval=True)
self.assertAlmostEqual(value, 0.6466, places=4)
values1 = self.teval2.get_reciprocal_rank(depth=30, per_query=True, trec_eval=True).loc[self.common_topics].values
values2 = self.teval3.get_reciprocal_rank(depth=1000, per_query=True, trec_eval=True).loc[self.common_topics].values
for v1, v2 in zip(values1, values2):
self.assertAlmostEqual(v1, v2, places=4)
results = self.teval2.get_reciprocal_rank(depth=1000, trec_eval=True, per_query=True)
correct_results = [0.0017, 0.1429, 0.3333]
values = map(float, results.loc[["378","650","624"]].values)
for v, c in zip(values, correct_results):
self.assertAlmostEqual(v, c, places=4)
def test_get_map(self):
value = self.teval1.get_map(depth=1000, trec_eval=True)
self.assertAlmostEqual(value, 0.2685, places=4)
value = self.teval2.get_map(depth=1000, trec_eval=True)
self.assertAlmostEqual(value, 0.2396, places=4)
values1 = self.teval2.get_map(depth=30, per_query=True, trec_eval=True).loc[self.common_topics].values
values2 = self.teval3.get_map(depth=1000, per_query=True, trec_eval=True).loc[self.common_topics].values
for v1, v2 in zip(values1, values2):
self.assertAlmostEqual(v1, v2, places=4)
results = self.teval2.get_map(depth=1000, trec_eval=True, per_query=True)
correct_results = [0.4926, 0.2808, 0.2335]
values = map(float, results.loc[["622", "609", "320"]].values)
for v, c in zip(values, correct_results):
self.assertAlmostEqual(v, c, places=4)
def test_get_precision(self):
value = self.teval1.get_precision(depth=1000, trec_eval=True)
self.assertAlmostEqual(value, 0.0010, places=4)
value = self.teval2.get_precision(depth=1000, trec_eval=True)
self.assertAlmostEqual(value, 0.0371, places=4)
values1 = self.teval2.get_precision(depth=30, per_query=True, trec_eval=True).loc[self.common_topics].values
values2 = self.teval3.get_precision(depth=30, per_query=True, trec_eval=True).loc[self.common_topics].values
for v1, v2 in zip(values1, values2):
self.assertAlmostEqual(v1, v2, places=4)
values1 = self.teval2.get_precision(depth=500, per_query=True, trec_eval=True).loc[self.common_topics].values
values2 = self.teval3.get_precision(depth=500, per_query=True, trec_eval=True).loc[self.common_topics].values
for v1, v2 in zip(values1, values2):
self.assertNotAlmostEqual(float(v1), float(v2), places=4)
results = self.teval2.get_precision(depth=30, trec_eval=True, per_query=True)
correct_results = [0.1333, 0.0333, 0.5333]
values = map(float, results.loc[["607", "433", "375"]].values)
for v, c in zip(values, correct_results):
self.assertAlmostEqual(v, c, places=4)
def test_get_recall(self):
value = self.teval1.get_recall(depth=1000, trec_eval=True)
self.assertAlmostEqual(value, 0.3889, places=4)
expected_results = {
5: 0.0858,
10: 0.1314,
15: 0.1686,
20: 0.1968,
30: 0.2530,
100: 0.4243,
200: 0.5323,
500: 0.6569,
1000: 0.7518
}
for recall_at in expected_results.keys():
value = self.teval2.get_recall(depth=recall_at, trec_eval=True)
self.assertAlmostEqual(value, expected_results[recall_at], places=4)
values1 = self.teval2.get_recall(depth=30, per_query=True, trec_eval=True).loc[self.common_topics].values
values2 = self.teval3.get_recall(depth=30, per_query=True, trec_eval=True).loc[self.common_topics].values
for v1, v2 in zip(values1, values2):
self.assertAlmostEqual(v1, v2, places=4)
values1 = self.teval2.get_recall(depth=500, per_query=True, trec_eval=True).loc[self.common_topics].values
values2 = self.teval3.get_recall(depth=500, per_query=True, trec_eval=True).loc[self.common_topics].values
for v1, v2 in zip(values1, values2):
self.assertNotAlmostEqual(float(v1), float(v2), places=4)
results = self.teval2.get_recall(depth=30, trec_eval=True, per_query=True)
correct_results = [0.3636, 0.0769, 0.2000, 0.1000]
values = map(float, results.loc[["607", "433", "375", "303"]].values)
for v, c in zip(values, correct_results):
self.assertAlmostEqual(v, c, places=4)
if __name__ == '__main__':
unittest.main()