-
Notifications
You must be signed in to change notification settings - Fork 2
/
plot-eval-recall.py
149 lines (123 loc) · 4.56 KB
/
plot-eval-recall.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
import sys
# 100 docs
tesspath = "evaluation/bestpick-tess-eval-100.txt"
cunipath = 'evaluation/bestpick-cuni-eval-100.txt'
# optimalpath = "evaluation/bestpick-optimal-eval-100.txt"
optimalpath = "evaluation/bestpick-optimal-fuzzy/opt.0.txt"
fuzzypath = "evaluation/bestpick-optimal-fuzzy/"
# fuzzypath = "evaluation/bestpick-optimal-fuzzy-trgm/"
ddpath = 'eval-results.txt'
# DISTRANGE = range(1, 6)
DISTRANGE = range(1, 4)
# DISTRANGE = [0.1, 0.3, 0.5, 0.7, 0.9]
# # 30 docs
# tesspath = "evaluation/bestpick-tess-eval-30-sample.txt"
# cunipath = 'evaluation/bestpick-cuni-eval-30-sample.txt'
# optimalpath = "evaluation/bestpick-optimal-fuzzy/opt.0.txt"
# fuzzypath = "evaluation/bestpick-optimal-fuzzy/"
# ddpath = 'eval-results.txt'
# if len(sys.argv) == 3:
# path = sys.argv[1]
# num = int(sys.argv[2])
# else:
# print 'Usage:',sys.argv[0],'<ocrbase>'
# print 'e.g. ',sys.argv[0],'evaluation/100doc-ocrexp-backup/'
if len(sys.argv) == 5:
tesspath, cunipath, optimalpath, ddpath = sys.argv[1:]
else:
print 'Usage:',sys.argv[0],'tesspath cunipath optimalpath ddpath. Used default.'
tess = [l.strip().split('\t') for l in open(tesspath).readlines()]
cuni = [l.strip().split('\t') for l in open(cunipath).readlines()]
opt = [l.strip().split('\t') for l in open(optimalpath).readlines()]
dd = [l.strip().split('\t') for l in open(ddpath).readlines()]
tess = [ (t[0], float(t[-1].strip('()'))) for t in tess]
cuni = [ (t[0], float(t[-1].strip('()'))) for t in cuni]
dd = [ (t[0], float(t[-1])) for t in dd]
opt = [ (t[0], float(t[-1].strip('()'))) for t in opt]
data = {'tess':tess, 'opt':opt, 'dd':dd} # not added cuni
# data = {'opt':opt, 'dd':dd} # not added cuni / tess
for dist in DISTRANGE:
# path = fuzzypath + 'opt.%d.txt' % dist
path = fuzzypath + 'opt.' + str(dist) + '.txt'
tmp = [l.strip().split('\t') for l in open(path).readlines()]
tmp = [ (t[0], float(t[-1].strip('()'))) for t in tmp]
data['opt('+str(dist)+')'] = tmp
docids = [_[0] for _ in sorted(data['opt'], key=lambda x:x[1])]
# plotorder = [i for i in reversed(['tess', 'dd', 'opt'] + ['opt(%d)' % i for i in DISTRANGE] )]
# not added tess
plotorder = [i for i in reversed(['tess', 'dd', 'opt'] + ['opt(' + str(i) + ')' for i in DISTRANGE] )]
plotdata = {}
for key in data:
oldlist = data[key]
olddict = {}
for pair in oldlist:
olddict[pair[0]] = pair[1]
newlist = []
for docid in docids:
if docid not in olddict:
newlist.append(None)
else:
newlist.append(olddict[docid])
plotdata[key] = newlist
import numpy
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import pylab
# Called by function below
def PlotPrep(xlabel, ylabel, loglog=False):
grid_width = 50
pylab.grid(True)
# possible formatting:
# plt.plot(line_x, line_y, 'r-')
# plt.plot(pdf_x, pdf_y, 'y.')
# plt.plot(epdf_x, epdf_y, 'b.')
pylab.xlabel(xlabel)
pylab.ylabel(ylabel)
if loglog:
plt.loglog()
# if legends != None:
# plt.legend(legends)
PlotPrep(xlabel='Document ID', ylabel='Word Recall')
colors = [i for i in reversed(['r', 'g', 'orange',
'blue', 'yellow', 'purple'
# , 'gray', 'black'
# 'bo--', 'yo--', 'mo--', 'go--', 'ro--'
# '0.7', '0.6', '0.5', '0.4', '0.3'
])]
i = 0
# plt.yscale('linear')
for dname in plotorder:
ploty = plotdata[dname]
plotx = range(len(docids))
my_xticks = [ d[len('JOURNAL_'):] for d in docids]
plt.xticks(plotx, my_xticks, rotation=45) # Adding custom sticks
plt.plot(plotx, ploty, colors[i])
i += 1
maxscore = max( [ max(plotdata[name]) for name in plotdata ])
minscore = min( [ min(plotdata[name]) for name in plotdata ])
# pylab.ylim([0.85, maxscore])
pylab.ylim([0.85, 1])
# pylab.ylim([minscore, 1])
plt.legend(tuple([name for name in plotorder]), loc='lower right')
plt.savefig('pick-result.eps')
plt.clf()
print 'Plot saved to: ', 'pick-result.eps'
def MacroErrRed(new, base):
counter = 0
sumred = 0.0
for i in range(len(base)):
if new[i] and base[i]: # have data for both (Tess has Nones)
counter += 1
# print '%.4f %.4f: ErrRed =' % (new[i], base[i]), (new[i] - base[i]) / float(1 - base[i])
sumred += (new[i] - base[i]) / float(1 - base[i])
return sumred / counter
# return sum([(new[i] - base[i]) / float(base[i]) for i in range(len(base)) if new[i] and base[i]]) / len(base)
tessred = MacroErrRed(plotdata['dd'], plotdata['tess'])
optred = MacroErrRed(plotdata['dd'], plotdata['opt'])
print 'Avg Error reduction from Tesseract:', tessred
print 'Avg Error reduction from Optimal:', optred
fout = open('result-errred.txt', 'w')
print >>fout, tessred
print >>fout, optred
fout.close()