blob: d4583444efbb786ab69742ea1543e30ae6d8454c [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
11#include "webrtc/tools/event_log_visualizer/plot_python.h"
12
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
17namespace webrtc {
18namespace plotting {
19
20PythonPlot::PythonPlot() {}
21
22PythonPlot::~PythonPlot() {}
23
tereliusdc35dcd2016-08-01 12:03:27 -070024void PythonPlot::Draw() {
terelius54ce6802016-07-13 06:44:41 -070025 // Write python commands to stdout. Intended program usage is
26 // ./event_log_visualizer event_log160330.dump | python
27
tereliusdc35dcd2016-08-01 12:03:27 -070028 if (!series_list_.empty()) {
29 printf("color_count = %zu\n", series_list_.size());
terelius54ce6802016-07-13 06:44:41 -070030 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
tereliusdc35dcd2016-08-01 12:03:27 -070035 for (size_t i = 0; i < series_list_.size(); i++) {
terelius54ce6802016-07-13 06:44:41 -070036 // List x coordinates
37 printf("x%zu = [", i);
tereliusdc35dcd2016-08-01 12:03:27 -070038 if (series_list_[i].points.size() > 0)
39 printf("%G", series_list_[i].points[0].x);
40 for (size_t j = 1; j < series_list_[i].points.size(); j++)
41 printf(", %G", series_list_[i].points[j].x);
terelius54ce6802016-07-13 06:44:41 -070042 printf("]\n");
43
44 // List y coordinates
45 printf("y%zu = [", i);
tereliusdc35dcd2016-08-01 12:03:27 -070046 if (series_list_[i].points.size() > 0)
47 printf("%G", series_list_[i].points[0].y);
48 for (size_t j = 1; j < series_list_[i].points.size(); j++)
49 printf(", %G", series_list_[i].points[j].y);
terelius54ce6802016-07-13 06:44:41 -070050 printf("]\n");
51
tereliusdc35dcd2016-08-01 12:03:27 -070052 if (series_list_[i].style == BAR_GRAPH) {
terelius54ce6802016-07-13 06:44:41 -070053 // There is a plt.bar function that draws bar plots,
54 // but it is *way* too slow to be useful.
55 printf(
56 "plt.vlines(x%zu, map(lambda t: min(t,0), y%zu), map(lambda t: "
57 "max(t,0), y%zu), color=rgb_colors[%zu], "
58 "label=\'%s\')\n",
tereliusdc35dcd2016-08-01 12:03:27 -070059 i, i, i, i, series_list_[i].label.c_str());
60 } else if (series_list_[i].style == LINE_GRAPH) {
terelius54ce6802016-07-13 06:44:41 -070061 printf("plt.plot(x%zu, y%zu, color=rgb_colors[%zu], label=\'%s\')\n", i,
tereliusdc35dcd2016-08-01 12:03:27 -070062 i, i, series_list_[i].label.c_str());
63 } else if (series_list_[i].style == LINE_DOT_GRAPH) {
Stefan Holmer13181032016-07-29 14:48:54 +020064 printf(
65 "plt.plot(x%zu, y%zu, color=rgb_colors[%zu], label=\'%s\', "
66 "marker='.')\n",
tereliusdc35dcd2016-08-01 12:03:27 -070067 i, i, i, series_list_[i].label.c_str());
terelius54ce6802016-07-13 06:44:41 -070068 } else {
69 printf("raise Exception(\"Unknown graph type\")\n");
70 }
71 }
72 }
73
tereliusdc35dcd2016-08-01 12:03:27 -070074 printf("plt.xlim(%f, %f)\n", xaxis_min_, xaxis_max_);
75 printf("plt.ylim(%f, %f)\n", yaxis_min_, yaxis_max_);
76 printf("plt.xlabel(\'%s\')\n", xaxis_label_.c_str());
77 printf("plt.ylabel(\'%s\')\n", yaxis_label_.c_str());
78 printf("plt.title(\'%s\')\n", title_.c_str());
79 if (!series_list_.empty()) {
terelius54ce6802016-07-13 06:44:41 -070080 printf("plt.legend(loc=\'best\', fontsize=\'small\')\n");
81 }
82}
83
84PythonPlotCollection::PythonPlotCollection() {}
85
86PythonPlotCollection::~PythonPlotCollection() {}
87
tereliusdc35dcd2016-08-01 12:03:27 -070088void PythonPlotCollection::Draw() {
terelius54ce6802016-07-13 06:44:41 -070089 printf("import matplotlib.pyplot as plt\n");
90 printf("import colorsys\n");
tereliusdc35dcd2016-08-01 12:03:27 -070091 for (size_t i = 0; i < plots_.size(); i++) {
terelius54ce6802016-07-13 06:44:41 -070092 printf("plt.figure(%zu)\n", i);
tereliusdc35dcd2016-08-01 12:03:27 -070093 plots_[i]->Draw();
terelius54ce6802016-07-13 06:44:41 -070094 }
95 printf("plt.show()\n");
96}
97
tereliusdc35dcd2016-08-01 12:03:27 -070098Plot* PythonPlotCollection::AppendNewPlot() {
terelius54ce6802016-07-13 06:44:41 -070099 Plot* plot = new PythonPlot();
tereliusdc35dcd2016-08-01 12:03:27 -0700100 plots_.push_back(std::unique_ptr<Plot>(plot));
terelius54ce6802016-07-13 06:44:41 -0700101 return plot;
102}
103
104} // namespace plotting
105} // namespace webrtc