OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license | |
5 * that can be found in the LICENSE file in the root of the source | |
6 * tree. An additional intellectual property rights grant can be found | |
7 * in the file PATENTS. All contributing project authors may | |
8 * be found in the AUTHORS file in the root of the source tree. | |
9 */ | |
10 | |
11 #include "webrtc/tools/event_log_visualizer/plot_python.h" | |
12 | |
13 #include <stdio.h> | |
14 | |
15 #include <memory> | |
16 | |
17 namespace webrtc { | |
18 namespace plotting { | |
19 | |
20 PythonPlot::PythonPlot() {} | |
21 | |
22 PythonPlot::~PythonPlot() {} | |
23 | |
24 void PythonPlot::Draw() { | |
25 // Write python commands to stdout. Intended program usage is | |
26 // ./event_log_visualizer event_log160330.dump | python | |
27 | |
28 if (!series_list_.empty()) { | |
29 printf("color_count = %zu\n", series_list_.size()); | |
30 printf( | |
31 "hls_colors = [(i*1.0/color_count, 0.25+i*0.5/color_count, 0.8) for i " | |
32 "in range(color_count)]\n"); | |
33 printf("rgb_colors = [colorsys.hls_to_rgb(*hls) for hls in hls_colors]\n"); | |
34 | |
35 for (size_t i = 0; i < series_list_.size(); i++) { | |
36 printf("\n# === Series: %s ===\n", series_list_[i].label.c_str()); | |
37 // List x coordinates | |
38 printf("x%zu = [", i); | |
39 if (series_list_[i].points.size() > 0) | |
40 printf("%G", series_list_[i].points[0].x); | |
41 for (size_t j = 1; j < series_list_[i].points.size(); j++) | |
42 printf(", %G", series_list_[i].points[j].x); | |
43 printf("]\n"); | |
44 | |
45 // List y coordinates | |
46 printf("y%zu = [", i); | |
47 if (series_list_[i].points.size() > 0) | |
48 printf("%G", series_list_[i].points[0].y); | |
49 for (size_t j = 1; j < series_list_[i].points.size(); j++) | |
50 printf(", %G", series_list_[i].points[j].y); | |
51 printf("]\n"); | |
52 | |
53 if (series_list_[i].style == BAR_GRAPH) { | |
54 // There is a plt.bar function that draws bar plots, | |
55 // but it is *way* too slow to be useful. | |
56 printf( | |
57 "plt.vlines(x%zu, map(lambda t: min(t,0), y%zu), map(lambda t: " | |
58 "max(t,0), y%zu), color=rgb_colors[%zu], " | |
59 "label=\'%s\')\n", | |
60 i, i, i, i, series_list_[i].label.c_str()); | |
61 } else if (series_list_[i].style == LINE_GRAPH) { | |
62 printf("plt.plot(x%zu, y%zu, color=rgb_colors[%zu], label=\'%s\')\n", i, | |
63 i, i, series_list_[i].label.c_str()); | |
64 } else if (series_list_[i].style == LINE_DOT_GRAPH) { | |
65 printf( | |
66 "plt.plot(x%zu, y%zu, color=rgb_colors[%zu], label=\'%s\', " | |
67 "marker='.')\n", | |
68 i, i, i, series_list_[i].label.c_str()); | |
69 } else if (series_list_[i].style == LINE_STEP_GRAPH) { | |
70 // Draw lines from (x[0],y[0]) to (x[1],y[0]) to (x[1],y[1]) and so on | |
71 // to illustrate the "steps". This can be expressed by duplicating all | |
72 // elements except the first in x and the last in y. | |
73 printf("x%zu = [v for dup in x%zu for v in [dup, dup]]\n", i, i); | |
74 printf("y%zu = [v for dup in y%zu for v in [dup, dup]]\n", i, i); | |
75 printf( | |
76 "plt.plot(x%zu[1:], y%zu[:-1], color=rgb_colors[%zu], " | |
77 "label=\'%s\')\n", | |
78 i, i, i, series_list_[i].label.c_str()); | |
79 } else if (series_list_[i].style == DOT_GRAPH) { | |
80 printf( | |
81 "plt.plot(x%zu, y%zu, color=rgb_colors[%zu], label=\'%s\', " | |
82 "marker='o', ls=' ')\n", | |
83 i, i, i, series_list_[i].label.c_str()); | |
84 } else { | |
85 printf("raise Exception(\"Unknown graph type\")\n"); | |
86 } | |
87 } | |
88 } | |
89 | |
90 printf("plt.xlim(%f, %f)\n", xaxis_min_, xaxis_max_); | |
91 printf("plt.ylim(%f, %f)\n", yaxis_min_, yaxis_max_); | |
92 printf("plt.xlabel(\'%s\')\n", xaxis_label_.c_str()); | |
93 printf("plt.ylabel(\'%s\')\n", yaxis_label_.c_str()); | |
94 printf("plt.title(\'%s\')\n", title_.c_str()); | |
95 if (!series_list_.empty()) { | |
96 printf("plt.legend(loc=\'best\', fontsize=\'small\')\n"); | |
97 } | |
98 } | |
99 | |
100 PythonPlotCollection::PythonPlotCollection() {} | |
101 | |
102 PythonPlotCollection::~PythonPlotCollection() {} | |
103 | |
104 void PythonPlotCollection::Draw() { | |
105 printf("import matplotlib.pyplot as plt\n"); | |
106 printf("import colorsys\n"); | |
107 for (size_t i = 0; i < plots_.size(); i++) { | |
108 printf("plt.figure(%zu)\n", i); | |
109 plots_[i]->Draw(); | |
110 } | |
111 printf("plt.show()\n"); | |
112 } | |
113 | |
114 Plot* PythonPlotCollection::AppendNewPlot() { | |
115 Plot* plot = new PythonPlot(); | |
116 plots_.push_back(std::unique_ptr<Plot>(plot)); | |
117 return plot; | |
118 } | |
119 | |
120 } // namespace plotting | |
121 } // namespace webrtc | |
OLD | NEW |