-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplotting.py
322 lines (288 loc) · 11 KB
/
plotting.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
from numpy import loadtxt
from scipy.signal import correlate
from scipy.stats import pearsonr
from matplotlib.pyplot import *
from matplotlib import pyplot as pp
import matplotlib.pyplot as plt
from ast import literal_eval
from argparse import ArgumentParser
import subprocess
from os import remove
#titlle = "Prediction for sentence semantic similarity regression"
titlle = "Sentence summary candidature"
#yylabel = "Similarity score"
yylabel = "Average candidature score"
def read_results(file_name):
out = open(file_name, 'r').readlines()
outs = []
for res in out:
if res.startswith('{'):
outs.append(literal_eval(res.strip()))
else:
continue
if not outs:
print "not found or unparsed results"
exit()
return outs
def plotter(goldStandard_file, predictions_file, log_scale=False, number_result=None, same_figure=True):
label_file = goldStandard_file #"/home/iarroyof/data/sts_test_13/STS.output.FNWN.txt"
output_file = predictions_file # "/home/iarroyof/data/output_1_sub_ccbsp_topic.txt"
labs = goldStandard_file
sample = range(0, len(labs))
est_outs = []
performs = []
for est in output_file:
est_outs.append(est['estimated_output'])
try:
performs.append(est['best_score'])
except KeyError:
try:
performs.append(est['performance'])
except KeyError:
pass
if len(labs) != len(est_outs[0]):
print "Compared predicitons and goldStandard are not of the same length"
print "len gs: ", len(labs), " vs len outs: ", len(est_outs[0])
exit()
labels = sorted(zip(labs, sample), key = lambda tup: tup[0])
ordd_est_outs = []
true = []
est_out = []
ccorrs = []
true = zip(*labels)[0]
for out in est_outs:
for i in labels:
est_out.append(out[i[1]])
ccorrs.append(correlate(true, est_out, mode = 'same')/len(labs))
ordd_est_outs.append(est_out)
est_out = []
#set_trace()
i = 0
if number_result:
grid(True)
title(titlle+" ["+str(i+1)+"]")
grid(True)
p1 = Rectangle((0, 0), 1, 1, fc="r")
p2 = Rectangle((0, 0), 1, 1, fc="b")
legend((p1, p2, p3), ["Gd_std sorted relationship", "Predicted sorted output", "Cross correlation"], loc=4)
xlabel('GoldStandad-sorted samples')
ylabel(yylabel)
if log_scale:
yscale('log')
plot(sample, true, color = 'r', linewidth=2)
plot(sample, ordd_est_outs[number_result], color = 'b--', linewidth=2)
show()
else:
for est_o in ordd_est_outs:
figure()
grid(True)
title(titlle+"["+str(i+1)+"]")
grid(True)
p1 = Rectangle((0, 0), 1, 1, fc="r")
p2 = Rectangle((0, 0), 1, 1, fc="b")
#p3 = Rectangle((0, 0), 1, 1, fc="g")
legend((p1, p2), ["GS candidatures", "Predicted candidatures"], loc=4)
xlabel('Sorted samples')
ylabel(yylabel)
if log_scale:
yscale('log')
plot(sample, true, color = 'r', linewidth=2)
plot(sample, est_o, color = 'b', linewidth=2)
plot(sample, ccorrs[i], color = 'g', linewidth=2)
i += 1
if same_figure:
show()
if not same_figure:
show()
def pearsons(gl, el):
with open("GL.txt", "w") as f:
for p in gl:
f.write("%s\n" % p)
with open("EL.txt", "w") as f:
for p in el:
f.write("%s\n" % p)
gs="GL.txt"
est="EL.txt"
pipe = subprocess.Popen(["perl", "./correlation-noconfidence.pl", gs, est], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
pipe.stdin.write("%s %s" % (gs, est))
try:
pearson= float(str(pipe.stdout.read()).split()[1])
except:
print str(pipe.stdout.read())
exit()
remove(gs)
remove(est)
pipe.stdin.close()
pipe.stdout.close()
return pearson
parsed = ArgumentParser(description='Plots desired labels, predicted outputs and calculates their corss-correlation.')
parsed.add_argument('-g', type=str, dest = 'goldStandard_file', help='Specifies the goldStandard file.')
parsed.add_argument('-p', type=str, dest = 'predictions_file', help='Specifies the machine predictions file.')
parsed.add_argument('-l', action='store_true', dest = 'log_scale', help='Toggles log scale for plotting.')
parsed.add_argument('-r', type=int, dest = 'number_result', help='If you know wath of all input results only to show, give it.')
parsed.add_argument('-s', action='store_true', dest = 'same_figure', help='Toggles plotting all loaded results in the same figure or each result in a different figure.')
parsed.add_argument('-S', action='store_true', dest = 'Sorted_outs', help='Toggles plotting sorted output and goldstandard.')
args = parsed.parse_args()
label_file = args.goldStandard_file #"/home/iarroyof/data/sts_test_13/STS.output.FNWN.txt"
output_file = args.predictions_file # "/home/iarroyof/data/output_1_sub_ccbsp_topic.txt"
from pdb import set_trace as st
from sklearn.metrics import mean_squared_error as mse
from sklearn.metrics import r2_score
labs = loadtxt(label_file)
sample = range(0, len(labs))
est_outs = []
models = []
weights = []
performs = []
params = []
results = read_results(output_file)
if not args.number_result:
try:
results = sorted(results, key = lambda k: k['performance'], reverse = True)
except:
pass
for est in results:
est_outs.append(est['estimated_output'])
try:
models.append(est['learned_model'])
#if 'file' in est['learned_model']:
# model = False # Es solo en tanto no se decodifique aqui el modelo desde un pickle (svr)
except KeyError:
model = False
pass
try:
performs.append(est['best_score'])
except KeyError:
try:
performs.append(est['performance'])
except KeyError:
performs.append(0.0)
pass
try:
params.append(est['best_params'])
except:
params.append("none")
pass
TT = 1
for out in est_outs:
if len(labs) != len(out):
print "Compared predicitons and goldStandard are not of the same length"
print "len gs: ", len(labs), " vs len outs: ", len(out)
print "index prediction:", TT
exit()
TT += 1
if args.Sorted_outs:
labels = sorted(zip(labs, sample), key = lambda tup: tup[0])
else:
labels = zip(labs, sample)
ordd_est_outs = []
true = []
est_out = []
ccorrs = []
true = zip(*labels)[0]
for out in est_outs:
for i in labels:
est_out.append(out[i[1]])
ccorrs.append(correlate(true, est_out, mode = 'same')/len(labs))
ordd_est_outs.append(est_out)
est_out = []
i = 0
if args.number_result:
try:
x = range(len(models[args.number_result]['weights']));
y = models[args.number_result]['weights']
f, axarr = plt.subplots(1, 3)
model = True
except KeyError:
f, axarr = plt.subplots(1, 1)
model = False
except IndexError:
f, axarr = plt.subplots(1, 1)
model = False
MSE = mse(labs, est_outs[int(args.number_result)])
try:
print "%d:" % args.number_result, params[int(args.number_result)], "perform: %f, %f" % (performs[int(args.number_result)], MSE)
print models[int(args.number_result)],"\n"
except:
pass
#pearson = pearsonr(true, ordd_est_outs[args.number_result])
r2 = r2_score(labs, est_outs[int(args.number_result)])
pearson = pearsons(labs, est_outs[int(args.number_result)])
grid(True)
#title( "%s [%d],\nPearson: %.5f, R^2: %.4f" % (titlle, args.number_result, pearson, r2)) #performs[args.number_result]))
title( "%s,\n weighted Pearson: %.5f, R^2: %.4f" % (titlle, pearson, r2))
title( "%s,\nPearson: %.5f, R^2: %.4f" % (titlle, pearson, r2)) #args.number_result, pearson, r2)) #performs[args.number_result]))
grid(True)
#p1 = Rectangle((0, 0), 1, 1, fc="r")
#p2 = Rectangle((0, 0), 1, 1, fc="b")
#axarr.legend((p1, p2), ["Gd_std human candidatures", "Predicted candidatures"], loc='best')
axarr.set_xlabel('Sorted samples')
axarr.set_ylabel(yylabel)
if args.log_scale and model:
axarr.set_yscale('log')
# -------------------------------------------
#if args.Sorted_outs:
# plot(sample, true, color = 'r', linewidth=2)
# plot(sample, est_o, color = 'b', linewidth=2)
# else:
# plot(sample, labs, color = 'r', linewidth=2)
# plot(sample, est_outs[k-1], color = 'b', linewidth=2)
#-------------------
if args.Sorted_outs:
axarr.plot(sample, labs, color = 'r', linewidth=3)
axarr.plot(sample, ordd_est_outs[args.number_result], color = 'b', linestyle = "--" , linewidth=3)
else:
axarr.plot(sample, true, color = 'r', linewidth=3)
axarr.plot(sample, est_outs[args.number_result], color = 'b', linestyle = "--" , linewidth=3)
axarr.plot(sample, true, color = 'r', linewidth=3)
axarr.plot(sample, ordd_est_outs[args.number_result], color = 'b', linestyle="--", linewidth=3)
axarr.plot(sample, ordd_est_outs[args.number_result], color = 'b', linestyle = "--" , linewidth=3)
if model:
axarr[1].scatter(x, y)
axarr[1].set_title('Learned weights')
y = models[args.number_result]['widths']
axarr[2].scatter(x, y)
axarr[2].set_title('Generated widths')
axarr[2].set_xlabel('Basis '+models[args.number_result]['family']+' index')
show()
else:
k = 0
for est_o in ordd_est_outs:
figure()
grid(True)
pearson = pearsons(labs, est_outs[k])
#MSE = mse(labs, est_outs[k])
try:
MSE = performs[k-1]
except:
MSE = r2_score(labs, est_outs[k])
#pearson = pearsonr(labs, est_outs[k])[1]
k += 1
try:
print "%d:" % k, params[k-1], "perform: %f, %f" % (MSE, pearson)
print models[k-1],"\n"
except:
pass
title( "%s [%d],\npearson: %.5f, perform: %.4f" % (titlle, k, pearson, performs[k-1]) )
grid(True)
p1 = Rectangle((0, 0), 1, 1, fc="r")
p2 = Rectangle((0, 0), 1, 1, fc="b")
#p3 = Rectangle((0, 0), 1, 1, fc="g")
#p4 = Rectangle((0, 0), 1, 1, fc="b")
#legend((p1, p2, p3, p4), ["Gd_std sorted relationship", "Predicted sorted output", "Goldstandard", "Prediction"], loc="best")
legend((p1, p2), ["Goldstandard relationship", "Regression estimation"], loc='best')
xlabel('Samples')
ylabel(yylabel)
if args.log_scale:
yscale('log')
if args.Sorted_outs:
plot(sample, true, color = 'r', linewidth=2)
plot(sample, est_o, color = 'b', linewidth=2)
else:
plot(sample, labs, color = 'r', linewidth=2)
plot(sample, est_outs[k-1], color = 'b', linewidth=2)
i += 1
if args.same_figure:
show()
if not args.same_figure:
show()