-
Notifications
You must be signed in to change notification settings - Fork 20
/
avg-results-all.py
137 lines (108 loc) · 3.37 KB
/
avg-results-all.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
import sys
if(len(sys.argv) > 2):
tool = sys.argv[1]
level = sys.argv[2]
else:
print "Usage: python " + sys.argv[0] + " <tool> <level>\n"
sys.exit()
def get_avg(l):
if len(l) == 0:
return 0.0
return reduce(lambda x, y: x + y, l) / float(len(l))
def get_accuracy(str):
chunks = str.split()
out = []
out.append(("accuracy",float( chunks[9] )))
out.append(("precision",float( chunks[1] )))
out.append(("recall",float( chunks[3] )))
out.append(("FB1",float( chunks[5] )))
out.append(("M_FB1",float( chunks[7] )))
return out
def get_level_result(str):
chunks = str.split()
out = []
cats = chunks[8]
out.append(("category", chunks[8] ))
out.append(("precision",float( chunks[1] )))
out.append(("recall",float( chunks[3] )))
out.append(("FB1",float( chunks[5] )))
out.append(("M_FB1",float( chunks[7] )))
return out, cats
#####################################
results_files = []
for i in range(4):
f = open('../results/' + tool + '/repeat-' + str(i) + '/avg/' + level + '-avg.txt', 'r')
results_files.append(f.read())
f.close()
results = []
cats = []
for results_file in results_files:
lines = results_file.splitlines()
result = []
result.append(get_accuracy(lines[0]))
for line in lines[1:]:
l = get_level_result(line)
result.append(l[0])
cats.append(l[1])
results.append(result)
cats = list(set(cats))
##########
## AVGs ##
g_acc = []
g_p = []
g_r = []
g_fb1 = []
g_m_fb1 = []
cats = dict([(i , [[],[],[],[]]) for i in cats])
for result in results:
acc_line = result[0]
g_acc.append(acc_line[0][1]) # get accuracy value
g_p.append(acc_line[1][1]) # get p value
g_r.append(acc_line[2][1]) # get r value
g_fb1.append(acc_line[3][1]) # get fb1 value
g_m_fb1.append(acc_line[4][1]) # get m_fb1 value
for cat in range(len(cats)):
try:
name = result[cat+1][0][1]
#cats[name][0] = result[cat+1][0][1] # get category name
cats[name][0].append(result[cat+1][1][1]) # get p
cats[name][1].append(result[cat+1][2][1]) # get r
cats[name][2].append(result[cat+1][3][1]) # get fb1
cats[name][3].append(result[cat+1][4][1]) # get m_fb1
except IndexError:
#print "One fold (" + str(i) + ") does not have all categories!"
continue #already dealt with
avg_g_acc = get_avg(g_acc)
avg_g_p = get_avg(g_p)
avg_g_r = get_avg(g_r)
avg_g_fb1 = get_avg(g_fb1)
avg_g_m_fb1 = get_avg(g_m_fb1)
if (avg_g_p + avg_g_r) == 0.0:
avg_g_t_m_fb1 = 0.0
else:
avg_g_t_m_fb1 = 2 * (avg_g_p * avg_g_r) / (avg_g_p + avg_g_r)
to_file = ""
to_file += "precision: {:04.2f}".format(avg_g_p)
to_file += "\trecall: {:04.2f}".format(avg_g_r)
to_file += "\tFB1: {:04.2f}".format(avg_g_fb1)
to_file += "\tAVG_M_FB1: {:04.2f}".format(avg_g_m_fb1)
to_file += "\tT_M_FB1: {:04.2f}".format(avg_g_t_m_fb1)
to_file += "\taccuracy: {:04.2f}".format(avg_g_acc) + '\n'
for cat in cats:
precision = get_avg(cats[cat][0])
recall = get_avg(cats[cat][1])
fb1 = get_avg(cats[cat][2])
m_fb1 = get_avg(cats[cat][3])
if (precision + recall) == 0.0:
t_m_fb1 = 0.0
else:
t_m_fb1 = 2 * (precision * recall) / (precision + recall)
to_file += "precision: {:05.2f}".format(precision)
to_file += "\trecall: {:05.2f}".format(recall)
to_file += "\tFB1: {:05.2f}".format(fb1)
to_file += "\tAVG_M_FB1: {:05.2f}".format(m_fb1)
to_file += "\tT_M_FB1: {:05.2f}".format(t_m_fb1)
to_file += '\t' + cat + '\n'
f = open('../results/' + tool + '/avg/' + level + '-avg.txt', 'w')
f.write(to_file)
f.close()