This repository was archived by the owner on Jun 25, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathplot.py
More file actions
199 lines (168 loc) · 7.02 KB
/
Copy pathplot.py
File metadata and controls
199 lines (168 loc) · 7.02 KB
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
import sys
import os
import json
import numpy as np
import pandas as pd
from collections import OrderedDict
from math import factorial
import matplotlib
# Make it possible to run matplotlib in SSH
NO_DISPLAY = 'DISPLAY' not in os.environ or os.environ['DISPLAY'] == ''
if NO_DISPLAY:
matplotlib.use('Agg')
else:
matplotlib.use('Qt4Agg')
import matplotlib.pyplot as plt
class Plot(object):
def __init__(self, group, data_file, *a):
self.group = group
self.data_file = data_file
self.plot_ext = 'pdf'
def read_data(self, cols):
return pd.read_csv(self.data_file, delimiter="\t", names=cols)
def end_plot(self, plot_file):
# Finish plotting by saving or showing file.
plt.tight_layout()
if NO_DISPLAY:
print('Saving plot to {}'.format(plot_file + '.' + self.plot_ext))
plt.savefig(plot_file + '.' + self.plot_ext)
else:
print("Close the plot window to continue.")
sys.stdout.flush()
try:
plt.show()
except:
# Sometimes things go wrong in the plot display (such as when
# clicking close button too fast), so ignore those errors.
pass
class FreqPlot(Plot):
def __init__(self, *a):
super(FreqPlot, self).__init__("score", *a)
self.title = "Histogram"
self.bins = 100
def make_plot(self):
cols = ['x', 'y']
D = self.read_data(cols)
M = D[cols].values
print(M)
# Sort rows on first column's value
M = M[M[:,0].argsort()]
x = M[:,0]
y = M[:,1]
# widths of bars
window = 101
width = (len(M) / float(self.bins)) / float(window)
plt.title(self.title)
plt.xlabel('score')
plt.ylabel('frequency')
plt.grid(True)
plt.xlim(-1.0,1.0)
plt.axhline(0, color='black')
plt.hist(x, self.bins, weights=y, width=width, linewidth=0.5,alpha=0.5)
# Draw a smooth curve
# Split the region in two sections (negative, positive) to ensure the
# very noisy point of 0.0 is not taken into account in smoothening.
zero = np.where(x == 0)[0][0]
yNeg = self.savitzky_golay(y[0:zero], window, 3)
yPos = self.savitzky_golay(y[zero:-1], window, 3)
plt.plot(x[0:zero], yNeg, 'r')
plt.plot(x[zero:-1], yPos, 'g')
self.end_plot(os.path.splitext(self.data_file)[0])
# Source: http://wiki.scipy.org/Cookbook/SavitzkyGolay
def savitzky_golay(self, y, window_size, order, deriv=0, rate=1):
try:
window_size = np.abs(np.int(window_size))
order = np.abs(np.int(order))
except ValueError:
raise ValueError("window_size and order have to be of type int")
if window_size % 2 != 1 or window_size < 1:
raise TypeError("window_size size must be a positive odd number")
if window_size < order + 2:
raise TypeError("window_size is too small for the polynomials order")
order_range = range(order+1)
half_window = (window_size -1) // 2
# Precompute coefficients
b = np.mat([[k**i for i in order_range] for k in range(-half_window, half_window+1)])
m = np.linalg.pinv(b).A[deriv] * rate**deriv * factorial(deriv)
# Pad the signal at the extremes with values taken from the signal itself
firstvals = y[0] - np.abs( y[1:half_window+1][::-1] - y[0] )
lastvals = y[-1] + np.abs(y[-half_window-1:-1][::-1] - y[-1])
y = np.concatenate((firstvals, y, lastvals))
return np.convolve( m[::-1], y, mode='valid')
class GroupPlot(Plot):
def __init__(self, group, data_file, sort=True, *a):
super(GroupPlot, self).__init__(group, data_file)
self.title = "Histogram"
self.sort = bool(int(sort)) # False = negatives, True = positives
def make_plot(self):
D = self.read_data([self.group, 'x', 'y'])
P = pd.DataFrame(columns=['x','yNeg','yPos','gs'])
i = 1
for name, group in D.groupby(self.group):
if name != '':
P.loc[i,'x'] = name
# Use a relative weighting scheme so that scores around zero
# decrease the importance of the relative score sum.
w = group['x'] * group['y']
gs = abs(w).sum()
P.loc[i,'gs'] = gs
P.loc[i,'yPos'] = (((group['x'] > 0.0) * w).sum() / gs)
P.loc[i,'yNeg'] = (((group['x'] < 0.0) * w).sum() / gs)
i = i + 1
# Filter groups that have very few comments.
ms = max(P['gs'])
P = P.loc[P['gs'] > 0.0005 * ms]
# If sort = False, then sort on negative and take lowest values
# If sort = True, then sort on positive and take only largest groups
P = P.sort('yPos' if self.sort else 'yNeg', ascending=not self.sort)
P = P[0:20]
width = 0.8
plt.title(self.title)
plt.xlabel(self.group)
plt.ylabel('relative score')
plt.grid(True)
plt.axhline(0, color='black')
plt.ylim(-1.0,1.0)
xi = np.arange(len(P))
plt.xticks(xi + width / 2., P['x'].tolist(), rotation=40, ha='right')
plt.bar(xi, P['yPos'].tolist(), width, color='g')
plt.bar(xi, P['yNeg'].tolist(), width, color='r')
self.end_plot(os.path.splitext(self.data_file)[0])
class AlgoPlot(Plot):
def __init__(self, *a):
super(AlgoPlot, self).__init__("algo", 'experiment_results.json', *a)
self.bar_width = 0.5
def make_plot(self):
# Read the data from the experiment results JSON file
data = {}
with open(self.data_file, 'r') as results:
data = json.load(results, object_pairs_hook=OrderedDict)
for algorithm, combinations in data.iteritems():
average = ()
std_dev = ()
for item in combinations.itervalues():
average = average + (item['average'],)
std_dev = std_dev + (item['standard_deviation'],)
# Compose the plot
x_groups = np.arange(len(combinations))
fig, ax = plt.subplots()
rects = ax.bar(x_groups, average, self.bar_width, color='b', ecolor='k', alpha=0.5, yerr=std_dev)
ax.set_xlabel('Parameters')
ax.set_ylabel('Accuracy')
ax.set_title(algorithm)
plt.xticks(x_groups + (self.bar_width / 2.0), rotation=40, ha='right')
ax.set_xticklabels(combinations.keys())
plt.grid(True)
self.end_plot(os.path.splitext(self.data_file)[0] + '-' + algorithm.replace(' ','_'))
def main(argv):
group = argv[0] if len(argv) > 0 else "id"
data_file = argv[1] if len(argv) > 1 else "score.dat"
if group == "score":
plot = FreqPlot(data_file, *argv[2:])
elif group == "algo":
plot = AlgoPlot(*argv[2:])
else:
plot = GroupPlot(group, data_file, *argv[2:])
plot.make_plot()
if __name__ == "__main__":
main(sys.argv[1:])