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> |
Stefan Holmer | 2beea2a | 2016-07-28 17:42:15 +0200 | [diff] [blame^] | 14 | #include <memory> |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 15 | |
| 16 | namespace webrtc { |
| 17 | namespace plotting { |
| 18 | |
| 19 | PythonPlot::PythonPlot() {} |
| 20 | |
| 21 | PythonPlot::~PythonPlot() {} |
| 22 | |
| 23 | void PythonPlot::draw() { |
| 24 | // Write python commands to stdout. Intended program usage is |
| 25 | // ./event_log_visualizer event_log160330.dump | python |
| 26 | |
| 27 | if (!series.empty()) { |
| 28 | printf("color_count = %zu\n", series.size()); |
| 29 | printf( |
| 30 | "hls_colors = [(i*1.0/color_count, 0.25+i*0.5/color_count, 0.8) for i " |
| 31 | "in range(color_count)]\n"); |
| 32 | printf("rgb_colors = [colorsys.hls_to_rgb(*hls) for hls in hls_colors]\n"); |
| 33 | |
| 34 | for (size_t i = 0; i < series.size(); i++) { |
| 35 | // List x coordinates |
| 36 | printf("x%zu = [", i); |
| 37 | if (series[i].points.size() > 0) |
| 38 | printf("%G", series[i].points[0].x); |
| 39 | for (size_t j = 1; j < series[i].points.size(); j++) |
| 40 | printf(", %G", series[i].points[j].x); |
| 41 | printf("]\n"); |
| 42 | |
| 43 | // List y coordinates |
| 44 | printf("y%zu = [", i); |
| 45 | if (series[i].points.size() > 0) |
| 46 | printf("%G", series[i].points[0].y); |
| 47 | for (size_t j = 1; j < series[i].points.size(); j++) |
| 48 | printf(", %G", series[i].points[j].y); |
| 49 | printf("]\n"); |
| 50 | |
| 51 | if (series[i].style == BAR_GRAPH) { |
| 52 | // There is a plt.bar function that draws bar plots, |
| 53 | // but it is *way* too slow to be useful. |
| 54 | printf( |
| 55 | "plt.vlines(x%zu, map(lambda t: min(t,0), y%zu), map(lambda t: " |
| 56 | "max(t,0), y%zu), color=rgb_colors[%zu], " |
| 57 | "label=\'%s\')\n", |
| 58 | i, i, i, i, series[i].label.c_str()); |
| 59 | } else if (series[i].style == LINE_GRAPH) { |
| 60 | printf("plt.plot(x%zu, y%zu, color=rgb_colors[%zu], label=\'%s\')\n", i, |
| 61 | i, i, series[i].label.c_str()); |
Stefan Holmer | 2beea2a | 2016-07-28 17:42:15 +0200 | [diff] [blame^] | 62 | } else if (series[i].style == LINE_DOT_GRAPH) { |
| 63 | printf( |
| 64 | "plt.plot(x%zu, y%zu, color=rgb_colors[%zu], label=\'%s\', " |
| 65 | "marker='.')\n", |
| 66 | i, i, i, series[i].label.c_str()); |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 67 | } else { |
| 68 | printf("raise Exception(\"Unknown graph type\")\n"); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | printf("plt.xlim(%f, %f)\n", xaxis_min, xaxis_max); |
| 74 | printf("plt.ylim(%f, %f)\n", yaxis_min, yaxis_max); |
| 75 | printf("plt.xlabel(\'%s\')\n", xaxis_label.c_str()); |
| 76 | printf("plt.ylabel(\'%s\')\n", yaxis_label.c_str()); |
| 77 | printf("plt.title(\'%s\')\n", title.c_str()); |
| 78 | if (!series.empty()) { |
| 79 | printf("plt.legend(loc=\'best\', fontsize=\'small\')\n"); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | PythonPlotCollection::PythonPlotCollection() {} |
| 84 | |
| 85 | PythonPlotCollection::~PythonPlotCollection() {} |
| 86 | |
| 87 | void PythonPlotCollection::draw() { |
| 88 | printf("import matplotlib.pyplot as plt\n"); |
| 89 | printf("import colorsys\n"); |
| 90 | for (size_t i = 0; i < plots.size(); i++) { |
| 91 | printf("plt.figure(%zu)\n", i); |
| 92 | plots[i]->draw(); |
| 93 | } |
| 94 | printf("plt.show()\n"); |
| 95 | } |
| 96 | |
| 97 | Plot* PythonPlotCollection::append_new_plot() { |
| 98 | Plot* plot = new PythonPlot(); |
| 99 | plots.push_back(std::unique_ptr<Plot>(plot)); |
| 100 | return plot; |
| 101 | } |
| 102 | |
| 103 | } // namespace plotting |
| 104 | } // namespace webrtc |