forked from anilkyelam/columbus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plotv2.py
333 lines (270 loc) · 9.94 KB
/
plotv2.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
322
323
324
325
326
327
328
329
330
331
332
333
import sys
import os
import re
import math
from datetime import datetime
from datetime import timedelta
import time
import random
import matplotlib
import matplotlib.pyplot as plt
from collections import Counter, defaultdict
import argparse
import pandas as pd
import numpy as np
from enum import Enum
import scipy.stats as scstats
colors = ['r','b','g','brown', 'c','k', 'orange', 'm','orangered']
linetypes = ['g-','g--','g-+']
markers = ['x','+','o','s','+', '|', '^']
class PlotType(Enum):
line = 'line'
scatter = 'scatter'
bar = 'bar'
cdf = 'cdf'
def __str__(self):
return self.value
def gen_cdf(npArray, num_bin):
x = np.sort(npArray)
y = 1. * np.arange(len(npArray)) / (len(npArray) - 1)
# h, edges = np.histogram(array, density=True, bins=num_bin )
# h = np.cumsum(h)/np.cumsum(h).max()
# x = edges.repeat(2)[:-1]
# y = np.zeros_like(x)
# y[1:] = h.repeat(2)
return x, y
# Argument Parser
def parse_args():
parser = argparse.ArgumentParser("Python Generic Plotter")
parser.add_argument('-d', '--datafile',
action='append',
help='path to the data file, multiple values are allowed')
parser.add_argument('-xc', '--xcol',
action='store',
help='X column from csv file. Defaults to row index if not provided.',
required=False)
parser.add_argument('-yc', '--ycol',
action='append',
help='Y column from csv file, multiple values are allowed')
parser.add_argument('-dxc', '--dfilexcol',
nargs=2,
action='store',
help='X column from a specific csv file. Defaults to row index if not provided.',
required=False)
parser.add_argument('-dyc', '--dfileycol',
nargs=2,
action='append',
metavar=('datafile', 'ycol'),
help='Y column from a specific file that is included with this argument')
parser.add_argument('-o', '--output',
action='store',
help='path to the output plot png',
default="result.png")
parser.add_argument('-p', '--print_',
action='store_true',
help='print data (with nicer format) instead of plot',
default=False)
# plot options
parser.add_argument('-z', '--ptype',
action='store',
help='type of the plot. Defaults to line',
type=PlotType,
choices=list(PlotType),
default=PlotType.line)
parser.add_argument('-t', '--ptitle',
action='store',
help='title of the plot')
parser.add_argument('-xl', '--xlabel',
action='store',
help='Custom x-axis label')
parser.add_argument('-yl', '--ylabel',
action='store',
help='Custom y-axis label')
parser.add_argument('-xm', '--xmul',
action='store',
type=float,
help='Custom x-axis multiplier constant (e.g., to change units)',
default=1)
parser.add_argument('-ym', '--ymul',
action='store',
type=float,
help='Custom y-axis multiplier constant (e.g., to change units)',
default=1)
parser.add_argument('--xlog',
action='store_true',
help='Plot x-axis on log scale',
default=False)
parser.add_argument('--ylog',
action='store_true',
help='Plot y-axis on log scale',
default=False)
parser.add_argument('-l', '--plabel',
action='append',
help='plot label, can provide one label per ycol or datafile')
parser.add_argument('-nm', '--nomarker',
action='store_true',
help='dont add markers to plots',
default=False)
parser.add_argument('-nt', '--notail',
action='store',
help='eliminate last x% tail from CDF. Defaults to 1%',
nargs='?',
type=float,
const=1.0)
parser.add_argument('-nh', '--nohead',
action='store',
help='eliminate first x% head from CDF. Defaults to 1%',
nargs='?',
type=float,
const=1.0)
parser.add_argument('-s', '--show',
action='store_true',
help='Display the plot after saving it. Blocks the program.',
default=False)
args = parser.parse_args()
return args
def main():
args = parse_args()
# Plot can be:
# 1. One datafile with multiple ycolumns plotted against single xcolumn
# 2. Single ycolumn from multiple datafiles plotted against an xcolumn
# 3. If ycols from multiple datafiles must be plotted, use -dyc argument style
dyc=(args.dfileycol is not None)
dandyc=(args.datafile is not None or args.ycol is not None)
if (dyc and dandyc) or not (dyc or dandyc):
print("Use either the (-dyc) or the (-d and -yc) approach - atleast one, and not both!")
return -1
if (args.datafile or args.ycol) and \
(args.datafile and len(args.datafile) > 1) and \
(args.ycol and len(args.ycol) > 1):
print("Only one of datafile or ycolumn arguments can provide multiple values. Use -dyc style if this doesn't work for you.")
return -1
# Get files, xcols and ycols from args
num_plots = 0
dfile_xcol = None
dfile_ycol_map = [] #Maintain the input order
if args.dfileycol:
dfilexcol = args.dfilexcol
for (dfile, ycol) in args.dfileycol:
if args.xcol and not dfile_xcol:
dfile_xcol = (dfile, args.xcol)
dfile_ycol_map.append((dfile, ycol))
num_plots += 1
else:
for dfile in args.datafile:
if args.xcol and not dfile_xcol:
dfile_xcol = (dfile, args.xcol)
for ycol in args.ycol:
dfile_ycol_map.append((dfile, ycol))
num_plots += 1
if args.plabel and len(args.plabel) != num_plots:
print("If plot labels are provided, they must be provided for all the plots and are mapped one-to-one in input order")
return -1
xlabel = args.xlabel if args.xlabel else args.xcol
ylabel = args.ylabel if args.ylabel else args.ycol
cidx = 0
midx = 0
lidx = 0
aidx = 0
font = {'family' : 'sans',
'size' : 15}
matplotlib.rc('font', **font)
matplotlib.rc('figure', autolayout=True)
fig, ax = plt.subplots(1, 1, figsize=(8,5))
fig.suptitle(args.ptitle if args.ptitle else '')
#plt.ylim(0, 1000)
if args.xlog:
ax.set_xscale('log')
if args.ylog:
ax.set_yscale('log')
xcol = None
if dfile_xcol:
df = pd.read_csv(dfile_xcol[0])
xcol = df[dfile_xcol[1]]
plot_num = 0
for (datafile, ycol) in dfile_ycol_map:
if not os.path.exists(datafile):
print("Datafile {0} does not exist".format(datafile))
return -1
df = pd.read_csv(datafile)
if args.print_:
for ycol in ycols:
label = "{0}:{1}".format(datafile, ycol)
print(label, df[ycol].mean(), df[ycol].std())
continue
if args.plabel: label = args.plabel[plot_num]
elif len(args.datafile) == 1: label = ycol
else: label = datafile
if xcol is None:
xcol = df.index
if args.ptype == PlotType.line:
xc = xcol
yc = df[ycol]
xc = [x * args.xmul for x in xc]
yc = [y * args.ymul for y in yc]
if args.nomarker:
ax.plot(xc, yc, label=label, color=colors[cidx])
else:
ax.plot(xc, yc, label=label, color=colors[cidx],
marker=markers[midx],markerfacecolor=colors[cidx])
elif args.ptype == PlotType.scatter:
xc = xcol
yc = df[ycol]
xc = [x * args.xmul for x in xc]
yc = [y * args.ymul for y in yc]
if args.nomarker:
ax.scatter(xc, yc, label=label, color=colors[cidx])
else:
ax.scatter(xc, yc, label=label, color=colors[cidx],
marker=markers[midx])
elif args.ptype == PlotType.bar:
xc = xcol
yc = df[ycol]
xc = [x * args.xmul for x in xc]
yc = [y * args.ymul for y in yc]
if args.nomarker:
ax.bar(xc, yc, label=label, color=colors[cidx])
else:
ax.bar(xc, yc, label=label, color=colors[cidx],
marker=markers[midx],markerfacecolor=colors[cidx])
elif args.ptype == PlotType.cdf:
xc, yc = gen_cdf(df[ycol], 100000)
head = 0
tail = len(xc)
if args.nohead:
for i, val in enumerate(yc):
if val <= args.nohead/100.0:
head = i
if args.notail:
for i, val in enumerate(yc):
if val > (100.0 - args.notail)/100.0:
tail = i
break
for i, val in enumerate(yc):
if val > 0.99:
idx = i
break
xc = xc[head:tail]
yc = yc[head:tail]
xc = [x * args.xmul for x in xc]
yc = [y * args.ymul for y in yc]
if args.nomarker:
ax.plot(xc, yc, label=label, color=colors[cidx])
else:
ax.plot(xc, yc, label=label, color=colors[cidx],
marker=markers[midx],markerfacecolor=colors[cidx])
ylabel = "CDF"
#ax.set_xlim(0, 1000000)
#ax.set_ylim(0, 25)
ax.set_xlabel(xlabel)
ax.set_ylabel(ylabel)
# ax.legend(loc='upper center', bbox_to_anchor=(0.5, 1.05), ncol=3, fancybox=True, shadow=True)
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5), ncol=1, fancybox=True, shadow=True)
cidx = (cidx + 1) % len(colors)
midx = (midx + 1) % len(markers)
plot_num += 1
plt.savefig(args.output, format="eps")
if args.show:
plt.show()
if __name__ == '__main__':
main()