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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame^] | 11 | #include "rtc_tools/event_log_visualizer/plot_python.h" |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame^] | 17 | #include "rtc_base/checks.h" |
philipel | 23c7f25 | 2017-07-14 06:30:03 -0700 | [diff] [blame] | 18 | |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 19 | namespace webrtc { |
| 20 | namespace plotting { |
| 21 | |
| 22 | PythonPlot::PythonPlot() {} |
| 23 | |
| 24 | PythonPlot::~PythonPlot() {} |
| 25 | |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 26 | void PythonPlot::Draw() { |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 27 | // Write python commands to stdout. Intended program usage is |
| 28 | // ./event_log_visualizer event_log160330.dump | python |
| 29 | |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 30 | if (!series_list_.empty()) { |
| 31 | printf("color_count = %zu\n", series_list_.size()); |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 32 | 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 | |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 37 | for (size_t i = 0; i < series_list_.size(); i++) { |
philipel | 35ba9bd | 2017-04-19 05:58:51 -0700 | [diff] [blame] | 38 | printf("\n# === Series: %s ===\n", series_list_[i].label.c_str()); |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 39 | // List x coordinates |
| 40 | printf("x%zu = [", i); |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 41 | 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); |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 45 | printf("]\n"); |
| 46 | |
| 47 | // List y coordinates |
| 48 | printf("y%zu = [", i); |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 49 | 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); |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 53 | printf("]\n"); |
| 54 | |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 55 | if (series_list_[i].style == BAR_GRAPH) { |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 56 | // 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", |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 62 | i, i, i, i, series_list_[i].label.c_str()); |
| 63 | } else if (series_list_[i].style == LINE_GRAPH) { |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 64 | 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] | 65 | i, i, series_list_[i].label.c_str()); |
| 66 | } else if (series_list_[i].style == LINE_DOT_GRAPH) { |
Stefan Holmer | 1318103 | 2016-07-29 14:48:54 +0200 | [diff] [blame] | 67 | printf( |
| 68 | "plt.plot(x%zu, y%zu, color=rgb_colors[%zu], label=\'%s\', " |
| 69 | "marker='.')\n", |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 70 | i, i, i, series_list_[i].label.c_str()); |
terelius | 77f0580 | 2017-02-01 06:34:53 -0800 | [diff] [blame] | 71 | } 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], " |
philipel | 23c7f25 | 2017-07-14 06:30:03 -0700 | [diff] [blame] | 79 | "path_effects=[pe.Stroke(linewidth=2, foreground='black'), " |
| 80 | "pe.Normal()], " |
terelius | 77f0580 | 2017-02-01 06:34:53 -0800 | [diff] [blame] | 81 | "label=\'%s\')\n", |
| 82 | i, i, i, series_list_[i].label.c_str()); |
philipel | e127e7a | 2017-03-29 16:28:53 +0200 | [diff] [blame] | 83 | } else if (series_list_[i].style == DOT_GRAPH) { |
| 84 | printf( |
| 85 | "plt.plot(x%zu, y%zu, color=rgb_colors[%zu], label=\'%s\', " |
philipel | 10fc0e6 | 2017-04-11 01:50:23 -0700 | [diff] [blame] | 86 | "marker='o', ls=' ')\n", |
philipel | e127e7a | 2017-03-29 16:28:53 +0200 | [diff] [blame] | 87 | i, i, i, series_list_[i].label.c_str()); |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 88 | } else { |
| 89 | printf("raise Exception(\"Unknown graph type\")\n"); |
| 90 | } |
| 91 | } |
philipel | 23c7f25 | 2017-07-14 06:30:03 -0700 | [diff] [blame] | 92 | |
| 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 | } |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 133 | } |
| 134 | |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 135 | 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()); |
philipel | 23c7f25 | 2017-07-14 06:30:03 -0700 | [diff] [blame] | 140 | 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"); |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
| 149 | PythonPlotCollection::PythonPlotCollection() {} |
| 150 | |
| 151 | PythonPlotCollection::~PythonPlotCollection() {} |
| 152 | |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 153 | void PythonPlotCollection::Draw() { |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 154 | printf("import matplotlib.pyplot as plt\n"); |
philipel | 23c7f25 | 2017-07-14 06:30:03 -0700 | [diff] [blame] | 155 | printf("import matplotlib.patches as mpatches\n"); |
| 156 | printf("import matplotlib.patheffects as pe\n"); |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 157 | printf("import colorsys\n"); |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 158 | for (size_t i = 0; i < plots_.size(); i++) { |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 159 | printf("plt.figure(%zu)\n", i); |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 160 | plots_[i]->Draw(); |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 161 | } |
| 162 | printf("plt.show()\n"); |
| 163 | } |
| 164 | |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 165 | Plot* PythonPlotCollection::AppendNewPlot() { |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 166 | Plot* plot = new PythonPlot(); |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 167 | plots_.push_back(std::unique_ptr<Plot>(plot)); |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 168 | return plot; |
| 169 | } |
| 170 | |
| 171 | } // namespace plotting |
| 172 | } // namespace webrtc |