-
Notifications
You must be signed in to change notification settings - Fork 0
/
system_eval.py
125 lines (101 loc) · 4.04 KB
/
system_eval.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
124
125
"""
System evaluation.
example usage:
```
python system_eval.py --qrel "/Users/nikhilarora/data/latimes/a3/upload-to-learn
/qrels/LA-only.trec8-401.450.minus416-423-437-444-447.txt"
--results "/Users/nikhilarora/data/latimes/a3/upload-to-learn/
results-files/student1.results"
--max-results 1000
```
evaluating my systems: baseline
```
python system_eval.py --qrel "/Users/nikhilarora/data/latimes/a3/upload-to-learn\
/qrels/LA-only.trec8-401.450.minus416-423-437-444-447.txt" \
--results "/Users/nikhilarora/data/latimes/n5arora-hw4-bm25-baseline.txt" \
--max-results 1000
```
evaluating my systems: stemmed index
```
python system_eval.py --qrel "/Users/nikhilarora/data/latimes/a3/upload-to-learn\
/qrels/LA-only.trec8-401.450.minus416-423-437-444-447.txt" \
--results "/Users/nikhilarora/data/latimes/n5arora-hw4-bm25-stem.txt" \
--max-results 1000
```
"""
import os
import argparse
import numpy as np
import pandas as pd
from eval_helpers import *
parser = argparse.ArgumentParser(description='todo: insert description')
parser.add_argument('--qrel', required=True, help='Path to qrel')
parser.add_argument('--results', required=True, help='Path to file containing results')
parser.add_argument('--max-results', type=int, required=True, help='')
def main():
cli = parser.parse_args()
qrels_f = cli.qrel
res_f = cli.results
max_results = cli.max_results
output_dir = '/Users/nikhilarora/data/latimes/a4/outputs/'
qrels_headings = ['topic_id', '_', 'docno', 'relevance']
res_headings = ['topic_id', '_', 'docno', 'rank', 'score', 'run_id']
qrels_df = pd.read_csv(qrels_f, delimiter=' ', names = qrels_headings)
r_res_df = pd.read_csv(res_f, delimiter=' ', names = res_headings)
run_id = r_res_df.loc[0,'run_id']
r_res_df = r_res_df.sort_values(['topic_id', 'score'], ascending=[True, False])
topic_ids = list(set(qrels_df['topic_id']))
# label each result as rel=`1` or non-rel=`0` in `relevance` col
res_df = pd.merge(
r_res_df,
qrels_df,
how='left',
on=['topic_id', 'docno'],
sort=False,
copy=False
)\
.fillna(0)\
.sort_values(['topic_id', 'score'], ascending=[True, False])\
.drop(['__x', '__y'], axis=1)
print(res_df.head())
print(len(res_df['topic_id']))
# compute the rel_count dict --> store mapping from topic_id to relevance count
rel_counts = dict()
for topic_id, topic_df in qrels_df.groupby('topic_id'):
rel_counts[topic_id] = len(topic_df[topic_df['relevance'] == 1])
# what will be done for each topic:
measures = {}
measures['topic_id'] = []
measures['ap'] = []
measures['p@10'] = []
measures['ndcg@10'] = []
measures['ndcg@1000'] = []
measures['tbg'] = []
for topic_id, topic_df in res_df.groupby('topic_id'):
print('Computing measures for topic_id: {}'.format(topic_id))
topic_df = topic_df.sort_values('score', ascending=False)
measures['topic_id'].append(topic_id)
# calc precision at k=10
measures['p@10'].append(get_precision_at_k(list(topic_df['relevance']),
10))
# calc avg precision with k=1000
measures['ap'].append(
get_avg_precision_k(list(topic_df['relevance']),
rel_counts[topic_id], 1000))
# calc ndcg with k=10
measures['ndcg@10'].append(get_ndcg_k(list(topic_df['relevance']), 10))
# calc ndcg with k=1000
measures['ndcg@1000'].append(get_ndcg_k(list(topic_df['relevance']),
1000))
# calc tbg
doc_len_vect = get_doc_len_vect(list(topic_df['docno']))
measures['tbg'].append(get_tbg_k(list(topic_df['relevance']),
doc_len_vect, 1000))
measures_df = pd.DataFrame(measures).sort_values('topic_id').fillna(0)
col_order = ['topic_id', 'ap', 'p@10', 'ndcg@10', 'ndcg@1000', 'tbg']
measures_df = measures_df[col_order]
print(measures_df)
measures_df.to_csv(os.path.join(output_dir,
'{}_metrics.csv'.format(run_id)))
if __name__ == '__main__':
main()