blob: f8643caa99988bba6a365bb1f857b48297f8c8fb [file] [log] [blame]
terelius54ce6802016-07-13 06:44:41 -07001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_tools/event_log_visualizer/plot_python.h"
terelius54ce6802016-07-13 06:44:41 -070012
13#include <stdio.h>
tereliusdc35dcd2016-08-01 12:03:27 -070014
Stefan Holmer13181032016-07-29 14:48:54 +020015#include <memory>
terelius54ce6802016-07-13 06:44:41 -070016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/checks.h"
philipel23c7f252017-07-14 06:30:03 -070018
terelius54ce6802016-07-13 06:44:41 -070019namespace webrtc {
20namespace plotting {
21
22PythonPlot::PythonPlot() {}
23
24PythonPlot::~PythonPlot() {}
25
tereliusdc35dcd2016-08-01 12:03:27 -070026void PythonPlot::Draw() {
terelius54ce6802016-07-13 06:44:41 -070027 // Write python commands to stdout. Intended program usage is
28 // ./event_log_visualizer event_log160330.dump | python
29
tereliusdc35dcd2016-08-01 12:03:27 -070030 if (!series_list_.empty()) {
31 printf("color_count = %zu\n", series_list_.size());
terelius54ce6802016-07-13 06:44:41 -070032 printf(
33 "hls_colors = [(i*1.0/color_count, 0.25+i*0.5/color_count, 0.8) for i "
34 "in range(color_count)]\n");
35 printf("rgb_colors = [colorsys.hls_to_rgb(*hls) for hls in hls_colors]\n");
36
tereliusdc35dcd2016-08-01 12:03:27 -070037 for (size_t i = 0; i < series_list_.size(); i++) {
philipel35ba9bd2017-04-19 05:58:51 -070038 printf("\n# === Series: %s ===\n", series_list_[i].label.c_str());
terelius54ce6802016-07-13 06:44:41 -070039 // List x coordinates
40 printf("x%zu = [", i);
tereliusdc35dcd2016-08-01 12:03:27 -070041 if (series_list_[i].points.size() > 0)
42 printf("%G", series_list_[i].points[0].x);
43 for (size_t j = 1; j < series_list_[i].points.size(); j++)
44 printf(", %G", series_list_[i].points[j].x);
terelius54ce6802016-07-13 06:44:41 -070045 printf("]\n");
46
47 // List y coordinates
48 printf("y%zu = [", i);
tereliusdc35dcd2016-08-01 12:03:27 -070049 if (series_list_[i].points.size() > 0)
50 printf("%G", series_list_[i].points[0].y);
51 for (size_t j = 1; j < series_list_[i].points.size(); j++)
52 printf(", %G", series_list_[i].points[j].y);
terelius54ce6802016-07-13 06:44:41 -070053 printf("]\n");
54
tereliusdc35dcd2016-08-01 12:03:27 -070055 if (series_list_[i].style == BAR_GRAPH) {
terelius54ce6802016-07-13 06:44:41 -070056 // There is a plt.bar function that draws bar plots,
57 // but it is *way* too slow to be useful.
58 printf(
59 "plt.vlines(x%zu, map(lambda t: min(t,0), y%zu), map(lambda t: "
60 "max(t,0), y%zu), color=rgb_colors[%zu], "
61 "label=\'%s\')\n",
tereliusdc35dcd2016-08-01 12:03:27 -070062 i, i, i, i, series_list_[i].label.c_str());
63 } else if (series_list_[i].style == LINE_GRAPH) {
terelius54ce6802016-07-13 06:44:41 -070064 printf("plt.plot(x%zu, y%zu, color=rgb_colors[%zu], label=\'%s\')\n", i,
tereliusdc35dcd2016-08-01 12:03:27 -070065 i, i, series_list_[i].label.c_str());
66 } else if (series_list_[i].style == LINE_DOT_GRAPH) {
Stefan Holmer13181032016-07-29 14:48:54 +020067 printf(
68 "plt.plot(x%zu, y%zu, color=rgb_colors[%zu], label=\'%s\', "
69 "marker='.')\n",
tereliusdc35dcd2016-08-01 12:03:27 -070070 i, i, i, series_list_[i].label.c_str());
terelius77f05802017-02-01 06:34:53 -080071 } else if (series_list_[i].style == LINE_STEP_GRAPH) {
72 // Draw lines from (x[0],y[0]) to (x[1],y[0]) to (x[1],y[1]) and so on
73 // to illustrate the "steps". This can be expressed by duplicating all
74 // elements except the first in x and the last in y.
75 printf("x%zu = [v for dup in x%zu for v in [dup, dup]]\n", i, i);
76 printf("y%zu = [v for dup in y%zu for v in [dup, dup]]\n", i, i);
77 printf(
78 "plt.plot(x%zu[1:], y%zu[:-1], color=rgb_colors[%zu], "
philipel23c7f252017-07-14 06:30:03 -070079 "path_effects=[pe.Stroke(linewidth=2, foreground='black'), "
80 "pe.Normal()], "
terelius77f05802017-02-01 06:34:53 -080081 "label=\'%s\')\n",
82 i, i, i, series_list_[i].label.c_str());
philipele127e7a2017-03-29 16:28:53 +020083 } else if (series_list_[i].style == DOT_GRAPH) {
84 printf(
85 "plt.plot(x%zu, y%zu, color=rgb_colors[%zu], label=\'%s\', "
philipel10fc0e62017-04-11 01:50:23 -070086 "marker='o', ls=' ')\n",
philipele127e7a2017-03-29 16:28:53 +020087 i, i, i, series_list_[i].label.c_str());
terelius54ce6802016-07-13 06:44:41 -070088 } else {
89 printf("raise Exception(\"Unknown graph type\")\n");
90 }
91 }
philipel23c7f252017-07-14 06:30:03 -070092
93 // IntervalSeries
94 printf("interval_colors = ['#ff8e82','#5092fc','#c4ffc4']\n");
95 RTC_CHECK_LE(interval_list_.size(), 3);
96 // To get the intervals to show up in the legend we have to created patches
97 // for them.
98 printf("legend_patches = []\n");
99 for (size_t i = 0; i < interval_list_.size(); i++) {
100 // List intervals
101 printf("\n# === IntervalSeries: %s ===\n",
102 interval_list_[i].label.c_str());
103 printf("ival%zu = [", i);
104 if (interval_list_[i].intervals.size() > 0) {
105 printf("(%G, %G)", interval_list_[i].intervals[0].begin,
106 interval_list_[i].intervals[0].end);
107 }
108 for (size_t j = 1; j < interval_list_[i].intervals.size(); j++) {
109 printf(", (%G, %G)", interval_list_[i].intervals[j].begin,
110 interval_list_[i].intervals[j].end);
111 }
112 printf("]\n");
113
114 printf("for i in range(0, %zu):\n", interval_list_[i].intervals.size());
115 if (interval_list_[i].orientation == IntervalSeries::kVertical) {
116 printf(
117 " plt.axhspan(ival%zu[i][0], ival%zu[i][1], "
118 "facecolor=interval_colors[%zu], "
119 "alpha=0.3)\n",
120 i, i, i);
121 } else {
122 printf(
123 " plt.axvspan(ival%zu[i][0], ival%zu[i][1], "
124 "facecolor=interval_colors[%zu], "
125 "alpha=0.3)\n",
126 i, i, i);
127 }
128 printf(
129 "legend_patches.append(mpatches.Patch(ec=\'black\', "
130 "fc=interval_colors[%zu], label='%s'))\n",
131 i, interval_list_[i].label.c_str());
132 }
terelius54ce6802016-07-13 06:44:41 -0700133 }
134
tereliusdc35dcd2016-08-01 12:03:27 -0700135 printf("plt.xlim(%f, %f)\n", xaxis_min_, xaxis_max_);
136 printf("plt.ylim(%f, %f)\n", yaxis_min_, yaxis_max_);
137 printf("plt.xlabel(\'%s\')\n", xaxis_label_.c_str());
138 printf("plt.ylabel(\'%s\')\n", yaxis_label_.c_str());
139 printf("plt.title(\'%s\')\n", title_.c_str());
philipel23c7f252017-07-14 06:30:03 -0700140 if (!series_list_.empty() || !interval_list_.empty()) {
141 printf("handles, labels = plt.gca().get_legend_handles_labels()\n");
142 printf("for lp in legend_patches:\n");
143 printf(" handles.append(lp)\n");
144 printf(" labels.append(lp.get_label())\n");
145 printf("plt.legend(handles, labels, loc=\'best\', fontsize=\'small\')\n");
terelius54ce6802016-07-13 06:44:41 -0700146 }
147}
148
149PythonPlotCollection::PythonPlotCollection() {}
150
151PythonPlotCollection::~PythonPlotCollection() {}
152
tereliusdc35dcd2016-08-01 12:03:27 -0700153void PythonPlotCollection::Draw() {
terelius54ce6802016-07-13 06:44:41 -0700154 printf("import matplotlib.pyplot as plt\n");
philipel23c7f252017-07-14 06:30:03 -0700155 printf("import matplotlib.patches as mpatches\n");
156 printf("import matplotlib.patheffects as pe\n");
terelius54ce6802016-07-13 06:44:41 -0700157 printf("import colorsys\n");
tereliusdc35dcd2016-08-01 12:03:27 -0700158 for (size_t i = 0; i < plots_.size(); i++) {
terelius54ce6802016-07-13 06:44:41 -0700159 printf("plt.figure(%zu)\n", i);
tereliusdc35dcd2016-08-01 12:03:27 -0700160 plots_[i]->Draw();
terelius54ce6802016-07-13 06:44:41 -0700161 }
162 printf("plt.show()\n");
163}
164
tereliusdc35dcd2016-08-01 12:03:27 -0700165Plot* PythonPlotCollection::AppendNewPlot() {
terelius54ce6802016-07-13 06:44:41 -0700166 Plot* plot = new PythonPlot();
tereliusdc35dcd2016-08-01 12:03:27 -0700167 plots_.push_back(std::unique_ptr<Plot>(plot));
terelius54ce6802016-07-13 06:44:41 -0700168 return plot;
169}
170
171} // namespace plotting
172} // namespace webrtc