-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrdplot.py
More file actions
executable file
·468 lines (432 loc) · 13 KB
/
rdplot.py
File metadata and controls
executable file
·468 lines (432 loc) · 13 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
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#!/usr/bin/env python3
# (c) Facebook, Inc. and its affiliates. Confidential and proprietary.
import argparse
import math
import sys
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import seaborn as sb
RESOLUTIONS = [
"216x120",
"432x240",
"640x360",
"864x480",
"1280x720",
]
PLOT_NAMES = {
"vmaf_mean": "VMAF Score",
"psnr_y_mean": "PSNR Score",
"ssim_y_mean": "SSIM Score",
"overshoot": "Bitrate Overshoot (Percentage)",
"quality": "Bitrate",
"actual_bitrate": "Actual Bitrate",
"encoder_duration": "Encoder Duration (sec)",
}
COLORS = {
"mjpeg": "green",
"x264": "blue",
"openh264": "yellow",
"x265": "red",
"vp8": "green",
"vp9": "magenta",
"libaom-av1": "black",
}
FORMATS = {
120: ".:",
240: ".-.",
360: ".-",
480: ".--",
}
COLORS2 = {
"mjpeg": {
# green-ish
120: "#204020",
240: "#208020",
360: "#20c020",
480: "#20ff20",
},
"x264": {
# blue
120: "#00c0ff",
240: "#0080ff",
360: "#0040ff",
480: "#0000ff",
},
"openh264": {
# yellow
120: "#ffc0ff",
240: "#ff80ff",
360: "#ff40ff",
480: "#ff00ff",
},
"x265": {
# red
120: "#ffc000",
240: "#ff8000",
360: "#ff4000",
480: "#ff0000",
},
"vp8": {
# green
120: "#004000",
240: "#008000",
360: "#00c000",
480: "#00ff00",
},
"vp9": {
# magenta
120: "#ff0080",
240: "#ff00a0",
360: "#ff00d0",
480: "#ff00ff",
},
"libaom-av1": {
# black
120: "#ffc0ff",
240: "#ff80ff",
360: "#ff40ff",
480: "#ff00ff",
},
}
PLOT_TYPES = {
"bitrate-vmaf", # traditional rd-test
"bitrate-ssim", # traditional rd-test
"bitrate-psnr", # traditional rd-test
"bitrate-overshoot", # traditional rd-test
"bitrate-actual_bitrate", # traditional rd-test
"bitrate-duration", # traditional rd-test
"resolution-vmaf",
"vmaf-bitrate",
"all",
}
default_values = {
"debug": 0,
"plot_type": "resolution-vmaf",
"simple": False,
"filter": False,
"infiles": [],
"outfile": None,
}
def get_resolution(row):
return int(row["resolution"].split("x")[1])
def get_overshoot(row):
# bitrate column is quality columns. Of course this only makes sense if it actually is bitrate
# but we calculate it just the same. The magnitude for bitrate is in kbps
return (100.0 * (row["actual_bitrate"] - row["quality"] * 1000)) / (
row["quality"] * 1000
)
def plot_max_min(df, ycol, ax):
bitrate = int(ax.title.get_text().split(" = ")[1])
myset = df[df.bitrate == bitrate]
max_values = {}
for codec in myset.codec.unique():
m = myset[myset.codec == codec]
max_values[codec] = {
"resolution": m.loc[m[ycol].idxmax()]["resolution"],
ycol: m.loc[m[ycol].idxmax()][ycol],
}
for codec in max_values.keys():
# add dots and lines for the best scores
# ax.scatter(x=max_values[codec]['resolution'],
# y=max_values[codec][ycol],
# color='r')
# add horizontal lines for the best score
y = max_values[codec][ycol]
ax.axhline(y=y, color=COLORS[codec], linestyle=":")
# add vertical arrow for the best score
# y2 = max_values['x264'][ycol]
# ycol_delta = y1 - y2
# color = 'k' if ycol_delta > 0 else 'r'
# import code; code.interact(local=locals()) # python gdb/debugging
# ax.annotate('%s' % ycol_delta, xy=(x,y), xytext=(x,y),
# arrowprops=dict(arrowstyle="<->", color=color))
# ax.vlines(x, y1, y2, color=color)
def process_input(options):
# create pandas dataframe
df = pd.DataFrame([], dtype=None)
# read CSV input
for infile in options.infiles:
df_ = pd.read_csv(infile)
df_["in_filename"] = infile
if df is None:
df = df_
else:
df = pd.concat([df, df_])
# add resolution and overshoot fields
df["resolution"] = df.apply(lambda row: get_resolution(row), axis=1)
df["overshoot"] = df.apply(lambda row: get_overshoot(row), axis=1)
df.sort_values(by=["in_filename", "codec", "resolution", "rcmode"], inplace=True)
if options.filter:
# filter overshooting values
for index, row in df.iterrows():
if row["overshoot"] > 10.0:
df.drop(index, inplace=True)
if options.plot_type == "resolution-vmaf":
plot_resolution_vmaf(options, df)
elif options.plot_type == "vmaf-bitrate":
plot_traditional(
"vmaf_mean",
"actual_bitrate",
options,
df,
options.simple,
legend_loc="upper left",
)
elif options.plot_type == "bitrate-vmaf":
plot_traditional(
"actual_bitrate",
"vmaf_mean",
options,
df,
options.simple,
legend_loc="lower right",
)
elif options.plot_type == "bitrate-psnr":
plot_traditional(
"actual_bitrate",
"psnr_y_mean",
options,
df,
options.simple,
legend_loc="lower right",
)
elif options.plot_type == "bitrate-ssim":
plot_traditional(
"actual_bitrate",
"ssim_y_mean",
options,
df,
options.simple,
legend_loc="lower right",
)
elif options.plot_type == "bitrate-overshoot":
plot_traditional(
"bitrate",
"overshoot",
options,
df,
options.simple,
legend_loc="lower right",
)
elif options.plot_type == "bitrate-actual_bitrate":
plot_traditional(
"quality",
"actual_bitrate",
options,
df,
options.simple,
legend_loc="lower right",
)
elif options.plot_type == "bitrate-duration":
plot_traditional(
"actual_bitrate",
"encoder_duration",
options,
df,
options.simple,
legend_loc="upper left",
)
elif options.plot_type == "all":
# plot_resolution_vmaf(options, df)
plot_traditional(
"vmaf_mean",
"actual_bitrate",
options,
df,
options.simple,
legend_loc="upper left",
)
plot_traditional("actual_bitrate", "vmaf_mean", options, df, options.simple)
plot_traditional("actual_bitrate", "psnr_y_mean", options, df, options.simple)
plot_traditional("actual_bitrate", "ssim_y_mean", options, df, options.simple)
plot_traditional("quality", "overshoot", options, df, options.simple)
def plot_resolution_vmaf(options, df):
# common plot settings
sb.set_style("darkgrid", {"axes.facecolor": ".9"})
xcol = "resolution"
for ycol in PLOT_NAMES:
if ycol == "quality":
continue
plot_name = PLOT_NAMES[ycol]
kwargs = {
"x": xcol,
"y": ycol,
"col": "quality",
"hue": "codec",
"ci": "sd",
"capsize": 0.2,
"palette": "Paired",
"height": 6,
"aspect": 0.75,
"kind": "point",
"data": df,
"col_wrap": 3,
"row_order": RESOLUTIONS,
}
fg = sb.catplot(**kwargs)
fg.set_ylabels(plot_name, fontsize=15)
# process all the Axes in the figure
for ax in fg.axes:
# make sure all the x-axes show xticks
plt.setp(ax.get_xticklabels(), visible=True)
# plot_max_min(df, ycol, ax)
# write to disk
outfile = "%s.%s-%s.png" % (options.outfile, xcol, ycol)
fg.savefig(outfile)
def plot_traditional(xcol, ycol, options, df, simple=False, **kwargs):
vcol = "codec"
pcol = "resolution"
if simple:
plot_generic_simple(options, df, xcol, ycol, vcol, pcol, **kwargs)
else:
plot_generic(options, df, xcol, ycol, vcol, pcol, **kwargs)
def plot_generic(options, df, xcol, ycol, vcol, pcol, **kwargs):
# plot the results
fig = plt.figure()
num_pcol = df[pcol].nunique()
max_ncols = 2
ncols = min(num_pcol, max_ncols)
nrows = math.ceil(num_pcol / max_ncols)
# different plots
for plot_id in range(num_pcol):
pval = df[pcol].unique()[plot_id]
pdf = df[df[pcol] == pval]
ax = fig.add_subplot(nrows, ncols, 1 + plot_id)
# different lines in each plot
for var_id in range(pdf[vcol].nunique()):
vval = pdf[vcol].unique()[var_id]
color = COLORS[vval]
xvals = pdf[pdf[vcol] == vval][xcol].tolist()
yvals = pdf[pdf[vcol] == vval][ycol].tolist()
label = str(vval)
fmt = ".-"
ax.plot(xvals, yvals, fmt, label=label, color=color)
ax.set_xlabel(PLOT_NAMES[xcol])
if plot_id % max_ncols == 0:
ax.set_ylabel(PLOT_NAMES[ycol])
ax.legend(loc=kwargs.get("legend_loc", "upper left"))
ax.set_title("%s: %s" % (pcol, pval))
# write to disk
outfile = "%s.%s-%s.png" % (options.outfile, xcol, ycol)
plt.savefig(outfile)
# same than plot_generic, but mixing pcol and vcol in the same Figure
def plot_generic_simple(options, df, xcol, ycol, vcol, pcol, **kwargs):
# plot the results
fig = plt.figure()
num_pcol = df[pcol].nunique()
# different plots
ax = fig.add_subplot(1, 1, 1)
# turn plots into lines
for plot_id in range(num_pcol):
pval = df[pcol].unique()[plot_id]
pdf = df[df[pcol] == pval]
fmt = FORMATS[pval]
# different lines in each plot
for var_id in range(pdf[vcol].nunique()):
vval = pdf[vcol].unique()[var_id]
color = COLORS2[vval][pval]
xvals = pdf[pdf[vcol] == vval][xcol].tolist()
yvals = pdf[pdf[vcol] == vval][ycol].tolist()
label = "%s.%s" % (str(pval), str(vval))
ax.plot(xvals, yvals, fmt, label=label, color=color)
ax.set_xlabel(PLOT_NAMES[xcol])
ax.set_ylabel(PLOT_NAMES[ycol])
ax.legend(loc=kwargs.get("legend_loc", "lower right"))
ax.set_title("%s" % (list(df.iterrows())[0][1]["in_filename"]))
# write to disk
outfile = "%s.%s-%s.png" % (options.outfile, xcol, ycol)
plt.savefig(outfile)
def get_options(argv):
"""Generic option parser.
Args:
argv: list containing arguments
Returns:
Namespace - An argparse.ArgumentParser-generated option object
"""
# init parser
# usage = 'usage: %prog [options] arg1 arg2'
# parser = argparse.OptionParser(usage=usage)
# parser.print_help() to get argparse.usage (large help)
# parser.print_usage() to get argparse.usage (just usage line)
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"-d",
"--debug",
action="count",
dest="debug",
default=default_values["debug"],
help="Increase verbosity (multiple times for more)",
)
parser.add_argument(
"--quiet",
action="store_const",
dest="debug",
const=-1,
help="Zero verbosity",
)
parser.add_argument(
"--simple",
action="store_true",
dest="simple",
default=default_values["simple"],
help="Simple Plots",
)
parser.add_argument(
"--filter",
action="store_true",
dest="filter",
default=default_values["filter"],
help="Filter Out Overshooting Samples",
)
parser.add_argument(
"--plot",
action="store",
type=str,
dest="plot_type",
default=default_values["plot_type"],
choices=PLOT_TYPES,
metavar="PLOT_TYPE",
help="plot type %r" % PLOT_TYPES,
)
parser.add_argument(
"--traditional",
action="store_const",
dest="plot_type",
const="bitrate-vmaf",
metavar="PLOT_TYPE",
help="plot type: bitrate-vmaf",
)
parser.add_argument(
"-i",
"--infile",
action="append",
type=str,
dest="infiles",
default=default_values["infiles"],
metavar="input-files",
help="input files",
)
parser.add_argument(
"outfile",
type=str,
default=default_values["outfile"],
metavar="output-file",
help="output file",
)
# do the parsing
options = parser.parse_args(argv[1:])
infile_list = []
for infile in options.infiles:
if infile == "-":
infile = sys.stdin
infile_list.append(infile)
options.infiles = infile_list
return options
def main(argv):
# parse options
options = get_options(argv)
process_input(options)
if __name__ == "__main__":
# at least the CLI program name: (CLI) execution
main(sys.argv)