terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 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> |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 14 | |
Stefan Holmer | 1318103 | 2016-07-29 14:48:54 +0200 | [diff] [blame] | 15 | #include <memory> |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 16 | |
| 17 | namespace webrtc { |
| 18 | namespace plotting { |
| 19 | |
| 20 | PythonPlot::PythonPlot() {} |
| 21 | |
| 22 | PythonPlot::~PythonPlot() {} |
| 23 | |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 24 | void PythonPlot::Draw() { |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 25 | // Write python commands to stdout. Intended program usage is |
| 26 | // ./event_log_visualizer event_log160330.dump | python |
| 27 | |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 28 | if (!series_list_.empty()) { |
| 29 | printf("color_count = %zu\n", series_list_.size()); |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 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 | |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 35 | for (size_t i = 0; i < series_list_.size(); i++) { |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 36 | // List x coordinates |
| 37 | printf("x%zu = [", i); |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 38 | 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); |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 42 | printf("]\n"); |
| 43 | |
| 44 | // List y coordinates |
| 45 | printf("y%zu = [", i); |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 46 | 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); |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 50 | printf("]\n"); |
| 51 | |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 52 | if (series_list_[i].style == BAR_GRAPH) { |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 53 | // 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", |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 59 | i, i, i, i, series_list_[i].label.c_str()); |
| 60 | } else if (series_list_[i].style == LINE_GRAPH) { |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 61 | printf("plt.plot(x%zu, y%zu, color=rgb_colors[%zu], label=\'%s\')\n", i, |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 62 | i, i, series_list_[i].label.c_str()); |
| 63 | } else if (series_list_[i].style == LINE_DOT_GRAPH) { |
Stefan Holmer | 1318103 | 2016-07-29 14:48:54 +0200 | [diff] [blame] | 64 | printf( |
| 65 | "plt.plot(x%zu, y%zu, color=rgb_colors[%zu], label=\'%s\', " |
| 66 | "marker='.')\n", |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 67 | i, i, i, series_list_[i].label.c_str()); |
terelius | 77f0580 | 2017-02-01 06:34:53 -0800 | [diff] [blame] | 68 | } else if (series_list_[i].style == LINE_STEP_GRAPH) { |
| 69 | // Draw lines from (x[0],y[0]) to (x[1],y[0]) to (x[1],y[1]) and so on |
| 70 | // to illustrate the "steps". This can be expressed by duplicating all |
| 71 | // elements except the first in x and the last in y. |
| 72 | printf("x%zu = [v for dup in x%zu for v in [dup, dup]]\n", i, i); |
| 73 | printf("y%zu = [v for dup in y%zu for v in [dup, dup]]\n", i, i); |
| 74 | printf( |
| 75 | "plt.plot(x%zu[1:], y%zu[:-1], color=rgb_colors[%zu], " |
| 76 | "label=\'%s\')\n", |
| 77 | i, i, i, series_list_[i].label.c_str()); |
philipel | e127e7a | 2017-03-29 16:28:53 +0200 | [diff] [blame] | 78 | } else if (series_list_[i].style == DOT_GRAPH) { |
| 79 | printf( |
| 80 | "plt.plot(x%zu, y%zu, color=rgb_colors[%zu], label=\'%s\', " |
philipel | 10fc0e6 | 2017-04-11 01:50:23 -0700 | [diff] [blame] | 81 | "marker='o', ls=' ')\n", |
philipel | e127e7a | 2017-03-29 16:28:53 +0200 | [diff] [blame] | 82 | i, i, i, series_list_[i].label.c_str()); |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 83 | } else { |
| 84 | printf("raise Exception(\"Unknown graph type\")\n"); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 89 | printf("plt.xlim(%f, %f)\n", xaxis_min_, xaxis_max_); |
| 90 | printf("plt.ylim(%f, %f)\n", yaxis_min_, yaxis_max_); |
| 91 | printf("plt.xlabel(\'%s\')\n", xaxis_label_.c_str()); |
| 92 | printf("plt.ylabel(\'%s\')\n", yaxis_label_.c_str()); |
| 93 | printf("plt.title(\'%s\')\n", title_.c_str()); |
| 94 | if (!series_list_.empty()) { |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 95 | printf("plt.legend(loc=\'best\', fontsize=\'small\')\n"); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | PythonPlotCollection::PythonPlotCollection() {} |
| 100 | |
| 101 | PythonPlotCollection::~PythonPlotCollection() {} |
| 102 | |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 103 | void PythonPlotCollection::Draw() { |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 104 | printf("import matplotlib.pyplot as plt\n"); |
| 105 | printf("import colorsys\n"); |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 106 | for (size_t i = 0; i < plots_.size(); i++) { |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 107 | printf("plt.figure(%zu)\n", i); |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 108 | plots_[i]->Draw(); |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 109 | } |
| 110 | printf("plt.show()\n"); |
| 111 | } |
| 112 | |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 113 | Plot* PythonPlotCollection::AppendNewPlot() { |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 114 | Plot* plot = new PythonPlot(); |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 115 | plots_.push_back(std::unique_ptr<Plot>(plot)); |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 116 | return plot; |
| 117 | } |
| 118 | |
| 119 | } // namespace plotting |
| 120 | } // namespace webrtc |