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> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame^] | 14 | |
Stefan Holmer | 1318103 | 2016-07-29 14:48:54 +0200 | [diff] [blame] | 15 | #include <memory> |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 16 | #include <string> |
| 17 | #include <vector> |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 18 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "rtc_base/checks.h" |
philipel | 23c7f25 | 2017-07-14 06:30:03 -0700 | [diff] [blame] | 20 | |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 21 | namespace webrtc { |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 22 | |
| 23 | PythonPlot::PythonPlot() {} |
| 24 | |
| 25 | PythonPlot::~PythonPlot() {} |
| 26 | |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 27 | void PythonPlot::Draw() { |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 28 | // Write python commands to stdout. Intended program usage is |
| 29 | // ./event_log_visualizer event_log160330.dump | python |
| 30 | |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 31 | if (!series_list_.empty()) { |
| 32 | printf("color_count = %zu\n", series_list_.size()); |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 33 | printf( |
| 34 | "hls_colors = [(i*1.0/color_count, 0.25+i*0.5/color_count, 0.8) for i " |
| 35 | "in range(color_count)]\n"); |
Bjorn Terelius | b577d5e | 2017-11-10 16:21:34 +0100 | [diff] [blame] | 36 | printf("colors = [colorsys.hls_to_rgb(*hls) for hls in hls_colors]\n"); |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 37 | |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 38 | for (size_t i = 0; i < series_list_.size(); i++) { |
philipel | 35ba9bd | 2017-04-19 05:58:51 -0700 | [diff] [blame] | 39 | printf("\n# === Series: %s ===\n", series_list_[i].label.c_str()); |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 40 | // List x coordinates |
| 41 | printf("x%zu = [", i); |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 42 | if (series_list_[i].points.size() > 0) |
Stefan Holmer | 1d4a227 | 2018-05-24 13:48:09 +0200 | [diff] [blame] | 43 | printf("%.3f", series_list_[i].points[0].x); |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 44 | for (size_t j = 1; j < series_list_[i].points.size(); j++) |
Stefan Holmer | 1d4a227 | 2018-05-24 13:48:09 +0200 | [diff] [blame] | 45 | printf(", %.3f", series_list_[i].points[j].x); |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 46 | printf("]\n"); |
| 47 | |
| 48 | // List y coordinates |
| 49 | printf("y%zu = [", i); |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 50 | if (series_list_[i].points.size() > 0) |
| 51 | printf("%G", series_list_[i].points[0].y); |
| 52 | for (size_t j = 1; j < series_list_[i].points.size(); j++) |
| 53 | printf(", %G", series_list_[i].points[j].y); |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 54 | printf("]\n"); |
| 55 | |
Bjorn Terelius | b577d5e | 2017-11-10 16:21:34 +0100 | [diff] [blame] | 56 | if (series_list_[i].line_style == LineStyle::kBar) { |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 57 | // There is a plt.bar function that draws bar plots, |
| 58 | // but it is *way* too slow to be useful. |
| 59 | printf( |
| 60 | "plt.vlines(x%zu, map(lambda t: min(t,0), y%zu), map(lambda t: " |
Bjorn Terelius | b577d5e | 2017-11-10 16:21:34 +0100 | [diff] [blame] | 61 | "max(t,0), y%zu), color=colors[%zu], " |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 62 | "label=\'%s\')\n", |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 63 | i, i, i, i, series_list_[i].label.c_str()); |
Bjorn Terelius | b577d5e | 2017-11-10 16:21:34 +0100 | [diff] [blame] | 64 | if (series_list_[i].point_style == PointStyle::kHighlight) { |
| 65 | printf( |
| 66 | "plt.plot(x%zu, y%zu, color=colors[%zu], " |
| 67 | "marker='.', ls=' ')\n", |
| 68 | i, i, i); |
| 69 | } |
| 70 | } else if (series_list_[i].line_style == LineStyle::kLine) { |
| 71 | if (series_list_[i].point_style == PointStyle::kHighlight) { |
| 72 | printf( |
| 73 | "plt.plot(x%zu, y%zu, color=colors[%zu], label=\'%s\', " |
| 74 | "marker='.')\n", |
| 75 | i, i, i, series_list_[i].label.c_str()); |
| 76 | } else { |
| 77 | printf("plt.plot(x%zu, y%zu, color=colors[%zu], label=\'%s\')\n", i, |
| 78 | i, i, series_list_[i].label.c_str()); |
| 79 | } |
| 80 | } else if (series_list_[i].line_style == LineStyle::kStep) { |
terelius | 77f0580 | 2017-02-01 06:34:53 -0800 | [diff] [blame] | 81 | // Draw lines from (x[0],y[0]) to (x[1],y[0]) to (x[1],y[1]) and so on |
| 82 | // to illustrate the "steps". This can be expressed by duplicating all |
| 83 | // elements except the first in x and the last in y. |
Bjorn Terelius | b577d5e | 2017-11-10 16:21:34 +0100 | [diff] [blame] | 84 | printf("xd%zu = [dup for v in x%zu for dup in [v, v]]\n", i, i); |
| 85 | printf("yd%zu = [dup for v in y%zu for dup in [v, v]]\n", i, i); |
terelius | 77f0580 | 2017-02-01 06:34:53 -0800 | [diff] [blame] | 86 | printf( |
Bjorn Terelius | b577d5e | 2017-11-10 16:21:34 +0100 | [diff] [blame] | 87 | "plt.plot(xd%zu[1:], yd%zu[:-1], color=colors[%zu], " |
terelius | 77f0580 | 2017-02-01 06:34:53 -0800 | [diff] [blame] | 88 | "label=\'%s\')\n", |
| 89 | i, i, i, series_list_[i].label.c_str()); |
Bjorn Terelius | b577d5e | 2017-11-10 16:21:34 +0100 | [diff] [blame] | 90 | if (series_list_[i].point_style == PointStyle::kHighlight) { |
| 91 | printf( |
| 92 | "plt.plot(x%zu, y%zu, color=colors[%zu], " |
| 93 | "marker='.', ls=' ')\n", |
| 94 | i, i, i); |
| 95 | } |
| 96 | } else if (series_list_[i].line_style == LineStyle::kNone) { |
philipel | e127e7a | 2017-03-29 16:28:53 +0200 | [diff] [blame] | 97 | printf( |
Bjorn Terelius | b577d5e | 2017-11-10 16:21:34 +0100 | [diff] [blame] | 98 | "plt.plot(x%zu, y%zu, color=colors[%zu], label=\'%s\', " |
philipel | 10fc0e6 | 2017-04-11 01:50:23 -0700 | [diff] [blame] | 99 | "marker='o', ls=' ')\n", |
philipel | e127e7a | 2017-03-29 16:28:53 +0200 | [diff] [blame] | 100 | i, i, i, series_list_[i].label.c_str()); |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 101 | } else { |
| 102 | printf("raise Exception(\"Unknown graph type\")\n"); |
| 103 | } |
| 104 | } |
philipel | 23c7f25 | 2017-07-14 06:30:03 -0700 | [diff] [blame] | 105 | |
| 106 | // IntervalSeries |
Ilya Nikolaevskiy | a4259f6 | 2017-12-05 13:19:45 +0100 | [diff] [blame] | 107 | printf("interval_colors = ['#ff8e82','#5092fc','#c4ffc4','#aaaaaa']\n"); |
| 108 | RTC_CHECK_LE(interval_list_.size(), 4); |
Bjorn Terelius | b577d5e | 2017-11-10 16:21:34 +0100 | [diff] [blame] | 109 | // To get the intervals to show up in the legend we have to create patches |
philipel | 23c7f25 | 2017-07-14 06:30:03 -0700 | [diff] [blame] | 110 | // for them. |
| 111 | printf("legend_patches = []\n"); |
| 112 | for (size_t i = 0; i < interval_list_.size(); i++) { |
| 113 | // List intervals |
| 114 | printf("\n# === IntervalSeries: %s ===\n", |
| 115 | interval_list_[i].label.c_str()); |
| 116 | printf("ival%zu = [", i); |
| 117 | if (interval_list_[i].intervals.size() > 0) { |
| 118 | printf("(%G, %G)", interval_list_[i].intervals[0].begin, |
| 119 | interval_list_[i].intervals[0].end); |
| 120 | } |
| 121 | for (size_t j = 1; j < interval_list_[i].intervals.size(); j++) { |
| 122 | printf(", (%G, %G)", interval_list_[i].intervals[j].begin, |
| 123 | interval_list_[i].intervals[j].end); |
| 124 | } |
| 125 | printf("]\n"); |
| 126 | |
| 127 | printf("for i in range(0, %zu):\n", interval_list_[i].intervals.size()); |
| 128 | if (interval_list_[i].orientation == IntervalSeries::kVertical) { |
| 129 | printf( |
| 130 | " plt.axhspan(ival%zu[i][0], ival%zu[i][1], " |
| 131 | "facecolor=interval_colors[%zu], " |
| 132 | "alpha=0.3)\n", |
| 133 | i, i, i); |
| 134 | } else { |
| 135 | printf( |
| 136 | " plt.axvspan(ival%zu[i][0], ival%zu[i][1], " |
| 137 | "facecolor=interval_colors[%zu], " |
| 138 | "alpha=0.3)\n", |
| 139 | i, i, i); |
| 140 | } |
| 141 | printf( |
| 142 | "legend_patches.append(mpatches.Patch(ec=\'black\', " |
| 143 | "fc=interval_colors[%zu], label='%s'))\n", |
| 144 | i, interval_list_[i].label.c_str()); |
| 145 | } |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 146 | } |
| 147 | |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 148 | printf("plt.xlim(%f, %f)\n", xaxis_min_, xaxis_max_); |
| 149 | printf("plt.ylim(%f, %f)\n", yaxis_min_, yaxis_max_); |
| 150 | printf("plt.xlabel(\'%s\')\n", xaxis_label_.c_str()); |
| 151 | printf("plt.ylabel(\'%s\')\n", yaxis_label_.c_str()); |
| 152 | printf("plt.title(\'%s\')\n", title_.c_str()); |
philipel | 23c7f25 | 2017-07-14 06:30:03 -0700 | [diff] [blame] | 153 | if (!series_list_.empty() || !interval_list_.empty()) { |
| 154 | printf("handles, labels = plt.gca().get_legend_handles_labels()\n"); |
| 155 | printf("for lp in legend_patches:\n"); |
| 156 | printf(" handles.append(lp)\n"); |
| 157 | printf(" labels.append(lp.get_label())\n"); |
| 158 | printf("plt.legend(handles, labels, loc=\'best\', fontsize=\'small\')\n"); |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
Bjorn Terelius | ff8cce3 | 2019-04-11 18:34:01 +0200 | [diff] [blame] | 162 | PythonPlotCollection::PythonPlotCollection(bool shared_xaxis) |
| 163 | : shared_xaxis_(shared_xaxis) {} |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 164 | |
| 165 | PythonPlotCollection::~PythonPlotCollection() {} |
| 166 | |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 167 | void PythonPlotCollection::Draw() { |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 168 | printf("import matplotlib.pyplot as plt\n"); |
Konrad Hofbauer | 9316c1a | 2019-03-29 15:39:53 +0100 | [diff] [blame] | 169 | printf("plt.rcParams.update({'figure.max_open_warning': 0})\n"); |
philipel | 23c7f25 | 2017-07-14 06:30:03 -0700 | [diff] [blame] | 170 | printf("import matplotlib.patches as mpatches\n"); |
| 171 | printf("import matplotlib.patheffects as pe\n"); |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 172 | printf("import colorsys\n"); |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 173 | for (size_t i = 0; i < plots_.size(); i++) { |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 174 | printf("plt.figure(%zu)\n", i); |
Bjorn Terelius | ff8cce3 | 2019-04-11 18:34:01 +0200 | [diff] [blame] | 175 | if (shared_xaxis_) { |
| 176 | // Link x-axes across all figures for synchronized zooming. |
| 177 | if (i == 0) { |
| 178 | printf("axis0 = plt.subplot(111)\n"); |
| 179 | } else { |
| 180 | printf("plt.subplot(111, sharex=axis0)\n"); |
| 181 | } |
Konrad Hofbauer | 225f4f6 | 2019-03-26 13:23:54 +0100 | [diff] [blame] | 182 | } |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 183 | plots_[i]->Draw(); |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 184 | } |
| 185 | printf("plt.show()\n"); |
| 186 | } |
| 187 | |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 188 | Plot* PythonPlotCollection::AppendNewPlot() { |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 189 | Plot* plot = new PythonPlot(); |
terelius | dc35dcd | 2016-08-01 12:03:27 -0700 | [diff] [blame] | 190 | plots_.push_back(std::unique_ptr<Plot>(plot)); |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 191 | return plot; |
| 192 | } |
| 193 | |
terelius | 54ce680 | 2016-07-13 06:44:41 -0700 | [diff] [blame] | 194 | } // namespace webrtc |