OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/rtc_tools/event_log_visualizer/plot_python.h" | 11 #include "webrtc/rtc_tools/event_log_visualizer/plot_python.h" |
12 | 12 |
13 #include <stdio.h> | 13 #include <stdio.h> |
14 | 14 |
15 #include <memory> | 15 #include <memory> |
16 | 16 |
| 17 #include "webrtc/rtc_base/checks.h" |
| 18 |
17 namespace webrtc { | 19 namespace webrtc { |
18 namespace plotting { | 20 namespace plotting { |
19 | 21 |
20 PythonPlot::PythonPlot() {} | 22 PythonPlot::PythonPlot() {} |
21 | 23 |
22 PythonPlot::~PythonPlot() {} | 24 PythonPlot::~PythonPlot() {} |
23 | 25 |
24 void PythonPlot::Draw() { | 26 void PythonPlot::Draw() { |
25 // Write python commands to stdout. Intended program usage is | 27 // Write python commands to stdout. Intended program usage is |
26 // ./event_log_visualizer event_log160330.dump | python | 28 // ./event_log_visualizer event_log160330.dump | python |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 "marker='.')\n", | 69 "marker='.')\n", |
68 i, i, i, series_list_[i].label.c_str()); | 70 i, i, i, series_list_[i].label.c_str()); |
69 } else if (series_list_[i].style == LINE_STEP_GRAPH) { | 71 } else if (series_list_[i].style == LINE_STEP_GRAPH) { |
70 // Draw lines from (x[0],y[0]) to (x[1],y[0]) to (x[1],y[1]) and so on | 72 // Draw lines from (x[0],y[0]) to (x[1],y[0]) to (x[1],y[1]) and so on |
71 // to illustrate the "steps". This can be expressed by duplicating all | 73 // to illustrate the "steps". This can be expressed by duplicating all |
72 // elements except the first in x and the last in y. | 74 // elements except the first in x and the last in y. |
73 printf("x%zu = [v for dup in x%zu for v in [dup, dup]]\n", i, i); | 75 printf("x%zu = [v for dup in x%zu for v in [dup, dup]]\n", i, i); |
74 printf("y%zu = [v for dup in y%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); |
75 printf( | 77 printf( |
76 "plt.plot(x%zu[1:], y%zu[:-1], color=rgb_colors[%zu], " | 78 "plt.plot(x%zu[1:], y%zu[:-1], color=rgb_colors[%zu], " |
| 79 "path_effects=[pe.Stroke(linewidth=2, foreground='black'), " |
| 80 "pe.Normal()], " |
77 "label=\'%s\')\n", | 81 "label=\'%s\')\n", |
78 i, i, i, series_list_[i].label.c_str()); | 82 i, i, i, series_list_[i].label.c_str()); |
79 } else if (series_list_[i].style == DOT_GRAPH) { | 83 } else if (series_list_[i].style == DOT_GRAPH) { |
80 printf( | 84 printf( |
81 "plt.plot(x%zu, y%zu, color=rgb_colors[%zu], label=\'%s\', " | 85 "plt.plot(x%zu, y%zu, color=rgb_colors[%zu], label=\'%s\', " |
82 "marker='o', ls=' ')\n", | 86 "marker='o', ls=' ')\n", |
83 i, i, i, series_list_[i].label.c_str()); | 87 i, i, i, series_list_[i].label.c_str()); |
84 } else { | 88 } else { |
85 printf("raise Exception(\"Unknown graph type\")\n"); | 89 printf("raise Exception(\"Unknown graph type\")\n"); |
86 } | 90 } |
87 } | 91 } |
| 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 } |
88 } | 133 } |
89 | 134 |
90 printf("plt.xlim(%f, %f)\n", xaxis_min_, xaxis_max_); | 135 printf("plt.xlim(%f, %f)\n", xaxis_min_, xaxis_max_); |
91 printf("plt.ylim(%f, %f)\n", yaxis_min_, yaxis_max_); | 136 printf("plt.ylim(%f, %f)\n", yaxis_min_, yaxis_max_); |
92 printf("plt.xlabel(\'%s\')\n", xaxis_label_.c_str()); | 137 printf("plt.xlabel(\'%s\')\n", xaxis_label_.c_str()); |
93 printf("plt.ylabel(\'%s\')\n", yaxis_label_.c_str()); | 138 printf("plt.ylabel(\'%s\')\n", yaxis_label_.c_str()); |
94 printf("plt.title(\'%s\')\n", title_.c_str()); | 139 printf("plt.title(\'%s\')\n", title_.c_str()); |
95 if (!series_list_.empty()) { | 140 if (!series_list_.empty() || !interval_list_.empty()) { |
96 printf("plt.legend(loc=\'best\', fontsize=\'small\')\n"); | 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"); |
97 } | 146 } |
98 } | 147 } |
99 | 148 |
100 PythonPlotCollection::PythonPlotCollection() {} | 149 PythonPlotCollection::PythonPlotCollection() {} |
101 | 150 |
102 PythonPlotCollection::~PythonPlotCollection() {} | 151 PythonPlotCollection::~PythonPlotCollection() {} |
103 | 152 |
104 void PythonPlotCollection::Draw() { | 153 void PythonPlotCollection::Draw() { |
105 printf("import matplotlib.pyplot as plt\n"); | 154 printf("import matplotlib.pyplot as plt\n"); |
| 155 printf("import matplotlib.patches as mpatches\n"); |
| 156 printf("import matplotlib.patheffects as pe\n"); |
106 printf("import colorsys\n"); | 157 printf("import colorsys\n"); |
107 for (size_t i = 0; i < plots_.size(); i++) { | 158 for (size_t i = 0; i < plots_.size(); i++) { |
108 printf("plt.figure(%zu)\n", i); | 159 printf("plt.figure(%zu)\n", i); |
109 plots_[i]->Draw(); | 160 plots_[i]->Draw(); |
110 } | 161 } |
111 printf("plt.show()\n"); | 162 printf("plt.show()\n"); |
112 } | 163 } |
113 | 164 |
114 Plot* PythonPlotCollection::AppendNewPlot() { | 165 Plot* PythonPlotCollection::AppendNewPlot() { |
115 Plot* plot = new PythonPlot(); | 166 Plot* plot = new PythonPlot(); |
116 plots_.push_back(std::unique_ptr<Plot>(plot)); | 167 plots_.push_back(std::unique_ptr<Plot>(plot)); |
117 return plot; | 168 return plot; |
118 } | 169 } |
119 | 170 |
120 } // namespace plotting | 171 } // namespace plotting |
121 } // namespace webrtc | 172 } // namespace webrtc |
OLD | NEW |