-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse_aggr_res.py
163 lines (144 loc) · 5.65 KB
/
parse_aggr_res.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import argparse
import csv
from collections import defaultdict
from functools import reduce
import pandas as pd
parser = argparse.ArgumentParser(description='UQV aggregation script',
usage='Create new UQV scores',
epilog='ROBUST version')
parser.add_argument('results', metavar='Aggregate_Results_File', default=None, help='path to agg res file')
parser.add_argument('--ap', default=False, action='store_true')
def read_file(file):
"""Reads the results file into a dictionary"""
predictor, ap, pred, mean, var, std = None, None, None, None, None, None
temp_dict = defaultdict(list)
with open(file, 'r') as f:
reader = csv.reader(f)
for row in reader:
if len(row) == 0:
continue
else:
row = row[0].split()
if row[0].lower() == 'predictor':
predictor = row[1]
continue
elif row[0].lower().startswith('ap-'):
ap = row[0].split('-')[1]
pred = row[1].split('-')[1]
continue
elif row[0].lower().startswith('mean'):
mean = row[2]
continue
elif row[0].lower().startswith('var'):
var = row[2]
continue
elif row[0].lower().startswith('stand'):
std = row[3]
temp_dict[predictor].append({'ap': ap, 'predictor': pred, 'mean': mean, 'var': var, 'std': std})
f.close()
return temp_dict
def read_ap_file(file):
"""Reads the AP results file into a dictionary"""
ap, pred, mean = None, None, None
temp = list()
with open(file, 'r') as f:
reader = csv.reader(f)
for row in reader:
if len(row) == 0:
continue
else:
row = row[0].split()
# print(row)
if row[0].lower().startswith('ap-'):
ap = row[0].split('-')[1]
pred = row[1].split('-')[1]
continue
else:
mean = row[0]
temp.append({'ap': ap, 'predictor': pred, 'mean': mean})
f.close()
return temp
def print_latex(res_dict):
"""Returns a dict of small tables latex strings"""
df = pd.DataFrame.from_records(res_dict, columns=['ap', 'predictor', 'mean'])
df = df.set_index(['ap'])
df = df.sort_index()
df = df.sort_values(['predictor'])
avg_df = df.loc['avg']
max_df = df.loc['max']
med_df = df.loc['med']
min_df = df.loc['min']
std_df = df.loc['std']
x = reduce(lambda left, right: pd.merge(left, right, on='predictor'), [avg_df, max_df, med_df, min_df, std_df])
x = x.set_index('predictor')
x.columns = ['avg', 'max', 'med', 'min', 'std']
return x.to_dict(orient='dict')
def bold_max_col(res_dict):
"""Boldface the maximum value for each AP aggregate"""
max_cols = defaultdict()
for agg in aggregates_list:
max_pred = defaultdict()
max_val = 0
for pred in predictors_list:
max_agg = max(res_dict[pred][agg], key=res_dict[pred][agg].get)
_max_val = float(res_dict[pred][agg][max_agg])
if _max_val > max_val:
max_val = _max_val
max_pred = {'aggregate': max_agg, 'predictor': pred, 'corr': max_val}
res_dict[max_pred['predictor']][agg][max_pred['aggregate']] = '\\textbf{{{}}}'.format(max_pred['corr'])
max_cols[agg] = max_pred
return max_cols
def print_big_latex(res_dict):
"""Merge and prints the large table latex string"""
test = defaultdict()
agg_list = aggregates_list
for pred in agg_list:
for ap in agg_list:
test[ap, pred] = {'clarity': '${}$'.format(res_dict['clarity'][ap][pred]),
'wig': '${}$'.format(res_dict['wig'][ap][pred]),
'nqc': '${}$'.format(res_dict['nqc'][ap][pred]),
'qf': '${}$'.format(res_dict['qf'][ap][pred])}
col = 0
print('\\begin{tabular}{lccccc}')
print('\\toprule')
print('{AP} & avg & max & med & min & std \\\\')
print('predictor & & & & & \\\\')
print('\\midrule')
for pred in agg_list:
for ap in agg_list:
x = pd.DataFrame.from_dict(test[ap, pred], orient='index')
if col % 5 == 0:
print('{} &'.format(pred))
latex = x.to_latex(header=False, multirow=True, index=True, escape=False)
else:
latex = x.to_latex(header=False, multirow=True, index=False, escape=False)
latex = latex.replace('\\toprule', '')
latex = latex.replace('\\bottomrule', '')
print(latex)
col += 1
if col % 5 == 0:
print('\\\\')
if pred != 'std':
print('\\hline')
else:
print('&')
print('\\bottomrule')
print('\\end{tabular}')
def main(args: parser):
results_file = args.results
ap_file = args.ap
if ap_file:
res_dict = read_ap_file(results_file)
print_latex(res_dict)
else:
_dict = defaultdict()
res_dic = read_file(results_file)
for p in res_dic:
_dict[p] = print_latex(res_dic[p])
bold_max_col(_dict)
print_big_latex(_dict)
if __name__ == '__main__':
predictors_list = ['clarity', 'wig', 'nqc', 'qf']
aggregates_list = ['avg', 'max', 'med', 'min', 'std']
args = parser.parse_args()
main(args)