Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 1 | #include "absl/flags/flag.h" |
Austin Schuh | 59d93f4 | 2022-07-18 16:52:32 -0700 | [diff] [blame] | 2 | #include "absl/strings/str_cat.h" |
| 3 | #include "absl/strings/str_split.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 4 | |
Stephan Pleines | 85b295c | 2024-02-04 17:50:26 -0800 | [diff] [blame] | 5 | #include "aos/analysis/in_process_plotter.h" |
Austin Schuh | 59d93f4 | 2022-07-18 16:52:32 -0700 | [diff] [blame] | 6 | #include "aos/init.h" |
| 7 | #include "aos/util/file.h" |
Austin Schuh | 59d93f4 | 2022-07-18 16:52:32 -0700 | [diff] [blame] | 8 | |
Stephan Pleines | 9e40c8e | 2024-02-07 20:58:28 -0800 | [diff] [blame] | 9 | using aos::analysis::Plotter; |
Austin Schuh | 59d93f4 | 2022-07-18 16:52:32 -0700 | [diff] [blame] | 10 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 11 | ABSL_FLAG(bool, all, false, "If true, plot *all* the nodes at once"); |
| 12 | ABSL_FLAG(bool, bounds, false, "If true, plot the noncausal bounds too."); |
| 13 | ABSL_FLAG(bool, samples, true, "If true, plot the samples too."); |
Austin Schuh | 69d0b73 | 2022-07-20 21:19:32 -0700 | [diff] [blame] | 14 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 15 | ABSL_FLAG(std::string, offsets, "", |
| 16 | "Offsets to add to the monotonic clock for each node. Use the " |
| 17 | "format of node=offset,node=offest"); |
Austin Schuh | 69d0b73 | 2022-07-20 21:19:32 -0700 | [diff] [blame] | 18 | |
Austin Schuh | 59d93f4 | 2022-07-18 16:52:32 -0700 | [diff] [blame] | 19 | // Simple C++ application to read the CSV files and use the in process plotter |
| 20 | // to plot them. This smokes the pants off gnuplot in terms of interactivity. |
| 21 | |
| 22 | namespace aos { |
| 23 | |
Austin Schuh | 69d0b73 | 2022-07-20 21:19:32 -0700 | [diff] [blame] | 24 | // Returns all the nodes. |
| 25 | std::vector<std::string> Nodes() { |
| 26 | const std::string start_time_file = aos::util::ReadFileToStringOrDie( |
| 27 | "/tmp/timestamp_noncausal_starttime.csv"); |
| 28 | std::vector<std::string_view> nodes = absl::StrSplit(start_time_file, '\n'); |
| 29 | |
| 30 | std::vector<std::string> formatted_nodes; |
| 31 | for (const std::string_view n : nodes) { |
| 32 | if (n == "") { |
| 33 | continue; |
| 34 | } |
| 35 | |
| 36 | std::vector<std::string_view> l = absl::StrSplit(n, ", "); |
| 37 | CHECK_EQ(l.size(), 2u) << "'" << n << "'"; |
| 38 | formatted_nodes.emplace_back(l[0]); |
| 39 | } |
| 40 | |
| 41 | return formatted_nodes; |
| 42 | } |
| 43 | |
| 44 | std::string SampleFile(std::string_view node1, std::string_view node2) { |
| 45 | return absl::StrCat("/tmp/timestamp_noncausal_", node1, "_", node2, |
| 46 | "_samples.csv"); |
| 47 | } |
| 48 | |
Austin Schuh | 59d93f4 | 2022-07-18 16:52:32 -0700 | [diff] [blame] | 49 | std::pair<std::vector<double>, std::vector<double>> ReadSamples( |
| 50 | std::string_view node1, std::string_view node2, bool flip) { |
| 51 | std::vector<double> samplefile12_t; |
| 52 | std::vector<double> samplefile12_o; |
Austin Schuh | 2c89924 | 2023-02-17 12:19:57 -0800 | [diff] [blame] | 53 | const std::string path = SampleFile(node1, node2); |
Austin Schuh | 59d93f4 | 2022-07-18 16:52:32 -0700 | [diff] [blame] | 54 | |
Austin Schuh | 2c89924 | 2023-02-17 12:19:57 -0800 | [diff] [blame] | 55 | if (!aos::util::PathExists(path)) { |
| 56 | return {}; |
| 57 | } |
| 58 | |
| 59 | const std::string file = aos::util::ReadFileToStringOrDie(path); |
Austin Schuh | 59d93f4 | 2022-07-18 16:52:32 -0700 | [diff] [blame] | 60 | bool first = true; |
| 61 | std::vector<std::string_view> lines = absl::StrSplit(file, '\n'); |
| 62 | samplefile12_t.reserve(lines.size()); |
| 63 | for (const std::string_view n : lines) { |
| 64 | if (first) { |
| 65 | first = false; |
| 66 | continue; |
| 67 | } |
| 68 | if (n == "") { |
| 69 | continue; |
| 70 | } |
| 71 | |
| 72 | std::vector<std::string_view> l = absl::StrSplit(n, ", "); |
Austin Schuh | 8c2e8c7 | 2022-08-06 18:31:19 -0700 | [diff] [blame] | 73 | if (l.size() != 4u) { |
| 74 | continue; |
| 75 | } |
Austin Schuh | 59d93f4 | 2022-07-18 16:52:32 -0700 | [diff] [blame] | 76 | double t; |
| 77 | double o; |
| 78 | CHECK(absl::SimpleAtod(l[0], &t)); |
| 79 | CHECK(absl::SimpleAtod(l[1], &o)); |
| 80 | samplefile12_t.emplace_back(t); |
| 81 | samplefile12_o.emplace_back(flip ? -o : o); |
| 82 | } |
| 83 | return std::make_pair(samplefile12_t, samplefile12_o); |
| 84 | } |
| 85 | |
Austin Schuh | 69d0b73 | 2022-07-20 21:19:32 -0700 | [diff] [blame] | 86 | void Offset(std::vector<double> *v, double offset) { |
| 87 | for (double &x : *v) { |
| 88 | x += offset; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // Returns all the nodes which talk to each other. |
| 93 | std::vector<std::pair<std::string, std::string>> NodeConnections() { |
| 94 | const std::vector<std::string> nodes = Nodes(); |
| 95 | std::vector<std::pair<std::string, std::string>> result; |
| 96 | for (size_t i = 1; i < nodes.size(); ++i) { |
| 97 | for (size_t j = 0; j < i; ++j) { |
| 98 | const std::string_view node1 = nodes[j]; |
| 99 | const std::string_view node2 = nodes[i]; |
Austin Schuh | 2c89924 | 2023-02-17 12:19:57 -0800 | [diff] [blame] | 100 | if (aos::util::PathExists(SampleFile(node1, node2)) || |
| 101 | aos::util::PathExists(SampleFile(node2, node1))) { |
Austin Schuh | 69d0b73 | 2022-07-20 21:19:32 -0700 | [diff] [blame] | 102 | result.emplace_back(node1, node2); |
| 103 | LOG(INFO) << "Found pairing " << node1 << ", " << node2; |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | return result; |
| 108 | } |
| 109 | |
| 110 | // Class to encapsulate the plotter state to make it easy to plot multiple |
| 111 | // connections. |
| 112 | class NodePlotter { |
| 113 | public: |
| 114 | NodePlotter() : nodes_(Nodes()) { |
| 115 | plotter_.AddFigure("Time"); |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 116 | if (!absl::GetFlag(FLAGS_offsets).empty()) { |
| 117 | for (std::string_view nodeoffset : |
| 118 | absl::StrSplit(absl::GetFlag(FLAGS_offsets), ',')) { |
Austin Schuh | 69d0b73 | 2022-07-20 21:19:32 -0700 | [diff] [blame] | 119 | std::vector<std::string_view> node_offset = |
| 120 | absl::StrSplit(nodeoffset, '='); |
| 121 | CHECK_EQ(node_offset.size(), 2u); |
| 122 | double o; |
| 123 | CHECK(absl::SimpleAtod(node_offset[1], &o)); |
| 124 | offset_.emplace(std::string(node_offset[0]), o); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | void AddNodes(std::string_view node1, std::string_view node2); |
| 130 | |
| 131 | void Serve() { |
| 132 | plotter_.Publish(); |
| 133 | plotter_.Spin(); |
| 134 | } |
| 135 | |
| 136 | private: |
| 137 | std::pair<std::vector<double>, std::vector<double>> ReadLines( |
| 138 | std::string_view node1, std::string_view node2, bool flip); |
| 139 | |
| 140 | std::pair<std::vector<double>, std::vector<double>> ReadOffset( |
| 141 | std::string_view node1, std::string_view node2); |
| 142 | |
| 143 | double TimeOffset(std::string_view node) { |
| 144 | auto it = offset_.find(std::string(node)); |
| 145 | if (it == offset_.end()) { |
| 146 | return 0.0; |
| 147 | } else { |
| 148 | return it->second; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | std::map<std::string, double> offset_; |
| 153 | |
| 154 | Plotter plotter_; |
| 155 | |
| 156 | std::vector<std::string> nodes_; |
| 157 | }; |
| 158 | |
| 159 | std::pair<std::vector<double>, std::vector<double>> NodePlotter::ReadLines( |
Austin Schuh | 59d93f4 | 2022-07-18 16:52:32 -0700 | [diff] [blame] | 160 | std::string_view node1, std::string_view node2, bool flip) { |
| 161 | std::vector<double> samplefile12_t; |
| 162 | std::vector<double> samplefile12_o; |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 163 | const std::string path = |
| 164 | absl::StrCat("/tmp/timestamp_noncausal_", node1, "_", node2, ".csv"); |
Austin Schuh | 59d93f4 | 2022-07-18 16:52:32 -0700 | [diff] [blame] | 165 | |
Austin Schuh | 2c89924 | 2023-02-17 12:19:57 -0800 | [diff] [blame] | 166 | if (!aos::util::PathExists(path)) { |
| 167 | return {}; |
| 168 | } |
| 169 | |
| 170 | const std::string file = aos::util::ReadFileToStringOrDie(path); |
Austin Schuh | 59d93f4 | 2022-07-18 16:52:32 -0700 | [diff] [blame] | 171 | bool first = true; |
| 172 | std::vector<std::string_view> lines = absl::StrSplit(file, '\n'); |
| 173 | samplefile12_t.reserve(lines.size()); |
| 174 | for (const std::string_view n : lines) { |
| 175 | if (first) { |
| 176 | first = false; |
| 177 | continue; |
| 178 | } |
| 179 | if (n == "") { |
| 180 | continue; |
| 181 | } |
| 182 | |
| 183 | std::vector<std::string_view> l = absl::StrSplit(n, ", "); |
Austin Schuh | 8c2e8c7 | 2022-08-06 18:31:19 -0700 | [diff] [blame] | 184 | if (l.size() != 3u) { |
| 185 | continue; |
| 186 | } |
Austin Schuh | 59d93f4 | 2022-07-18 16:52:32 -0700 | [diff] [blame] | 187 | double t; |
| 188 | double o; |
| 189 | CHECK(absl::SimpleAtod(l[0], &t)); |
| 190 | CHECK(absl::SimpleAtod(l[2], &o)); |
| 191 | samplefile12_t.emplace_back(t); |
| 192 | samplefile12_o.emplace_back(flip ? -o : o); |
| 193 | } |
| 194 | return std::make_pair(samplefile12_t, samplefile12_o); |
| 195 | } |
| 196 | |
Austin Schuh | 69d0b73 | 2022-07-20 21:19:32 -0700 | [diff] [blame] | 197 | std::pair<std::vector<double>, std::vector<double>> NodePlotter::ReadOffset( |
Austin Schuh | 59d93f4 | 2022-07-18 16:52:32 -0700 | [diff] [blame] | 198 | std::string_view node1, std::string_view node2) { |
| 199 | int node1_index = -1; |
| 200 | int node2_index = -1; |
| 201 | |
| 202 | { |
Austin Schuh | 59d93f4 | 2022-07-18 16:52:32 -0700 | [diff] [blame] | 203 | int index = 0; |
Austin Schuh | 69d0b73 | 2022-07-20 21:19:32 -0700 | [diff] [blame] | 204 | for (const std::string &n : nodes_) { |
| 205 | if (n == node1) { |
Austin Schuh | 59d93f4 | 2022-07-18 16:52:32 -0700 | [diff] [blame] | 206 | node1_index = index; |
| 207 | } |
Austin Schuh | 69d0b73 | 2022-07-20 21:19:32 -0700 | [diff] [blame] | 208 | if (n == node2) { |
Austin Schuh | 59d93f4 | 2022-07-18 16:52:32 -0700 | [diff] [blame] | 209 | node2_index = index; |
| 210 | } |
| 211 | ++index; |
| 212 | } |
| 213 | } |
| 214 | CHECK_NE(node1_index, -1) << ": Unknown node " << node1; |
| 215 | CHECK_NE(node2_index, -1) << ": Unknown node " << node2; |
| 216 | std::vector<double> offsetfile_t; |
| 217 | std::vector<double> offsetfile_o; |
| 218 | |
| 219 | const std::string file = |
| 220 | aos::util::ReadFileToStringOrDie("/tmp/timestamp_noncausal_offsets.csv"); |
| 221 | bool first = true; |
| 222 | std::vector<std::string_view> lines = absl::StrSplit(file, '\n'); |
| 223 | offsetfile_t.reserve(lines.size()); |
| 224 | for (const std::string_view n : lines) { |
| 225 | if (first) { |
| 226 | first = false; |
| 227 | continue; |
| 228 | } |
| 229 | if (n == "") { |
| 230 | continue; |
| 231 | } |
| 232 | |
| 233 | std::vector<std::string_view> l = absl::StrSplit(n, ", "); |
| 234 | CHECK_LT(static_cast<size_t>(node1_index + 1), l.size()); |
| 235 | CHECK_LT(static_cast<size_t>(node2_index + 1), l.size()); |
| 236 | double t; |
| 237 | double o1; |
| 238 | double o2; |
| 239 | CHECK(absl::SimpleAtod(l[0], &t)); |
| 240 | CHECK(absl::SimpleAtod(l[1 + node1_index], &o1)); |
| 241 | CHECK(absl::SimpleAtod(l[1 + node2_index], &o2)); |
| 242 | offsetfile_t.emplace_back(t); |
| 243 | offsetfile_o.emplace_back(o2 - o1); |
| 244 | } |
| 245 | return std::make_pair(offsetfile_t, offsetfile_o); |
| 246 | } |
| 247 | |
Austin Schuh | 69d0b73 | 2022-07-20 21:19:32 -0700 | [diff] [blame] | 248 | void NodePlotter::AddNodes(std::string_view node1, std::string_view node2) { |
| 249 | const double offset1 = TimeOffset(node1); |
| 250 | const double offset2 = TimeOffset(node2); |
| 251 | |
| 252 | std::pair<std::vector<double>, std::vector<double>> samplefile12 = |
Austin Schuh | 59d93f4 | 2022-07-18 16:52:32 -0700 | [diff] [blame] | 253 | ReadSamples(node1, node2, false); |
Austin Schuh | 69d0b73 | 2022-07-20 21:19:32 -0700 | [diff] [blame] | 254 | std::pair<std::vector<double>, std::vector<double>> samplefile21 = |
Austin Schuh | 59d93f4 | 2022-07-18 16:52:32 -0700 | [diff] [blame] | 255 | ReadSamples(node2, node1, true); |
| 256 | |
Austin Schuh | 69d0b73 | 2022-07-20 21:19:32 -0700 | [diff] [blame] | 257 | std::pair<std::vector<double>, std::vector<double>> noncausalfile12 = |
Austin Schuh | 59d93f4 | 2022-07-18 16:52:32 -0700 | [diff] [blame] | 258 | ReadLines(node1, node2, false); |
Austin Schuh | 69d0b73 | 2022-07-20 21:19:32 -0700 | [diff] [blame] | 259 | std::pair<std::vector<double>, std::vector<double>> noncausalfile21 = |
Austin Schuh | 59d93f4 | 2022-07-18 16:52:32 -0700 | [diff] [blame] | 260 | ReadLines(node2, node1, true); |
| 261 | |
Austin Schuh | 69d0b73 | 2022-07-20 21:19:32 -0700 | [diff] [blame] | 262 | std::pair<std::vector<double>, std::vector<double>> offsetfile = |
Austin Schuh | 59d93f4 | 2022-07-18 16:52:32 -0700 | [diff] [blame] | 263 | ReadOffset(node1, node2); |
| 264 | |
Austin Schuh | 69d0b73 | 2022-07-20 21:19:32 -0700 | [diff] [blame] | 265 | Offset(&samplefile12.second, offset2 - offset1); |
| 266 | Offset(&samplefile21.second, offset2 - offset1); |
| 267 | Offset(&noncausalfile12.second, offset2 - offset1); |
| 268 | Offset(&noncausalfile21.second, offset2 - offset1); |
| 269 | Offset(&offsetfile.second, offset2 - offset1); |
| 270 | |
Austin Schuh | 59d93f4 | 2022-07-18 16:52:32 -0700 | [diff] [blame] | 271 | CHECK_EQ(samplefile12.first.size(), samplefile12.second.size()); |
| 272 | CHECK_EQ(samplefile21.first.size(), samplefile21.second.size()); |
| 273 | CHECK_EQ(noncausalfile12.first.size(), noncausalfile12.second.size()); |
| 274 | CHECK_EQ(noncausalfile21.first.size(), noncausalfile21.second.size()); |
| 275 | |
| 276 | LOG(INFO) << samplefile12.first.size() + samplefile21.first.size() + |
| 277 | noncausalfile12.first.size() + noncausalfile21.first.size() |
| 278 | << " points"; |
Austin Schuh | 59d93f4 | 2022-07-18 16:52:32 -0700 | [diff] [blame] | 279 | |
Austin Schuh | 69d0b73 | 2022-07-20 21:19:32 -0700 | [diff] [blame] | 280 | plotter_.AddLine(offsetfile.first, offsetfile.second, |
Austin Schuh | 59d93f4 | 2022-07-18 16:52:32 -0700 | [diff] [blame] | 281 | Plotter::LineOptions{ |
| 282 | .label = absl::StrCat("filter ", node2, " ", node1), |
| 283 | // TODO(austin): roboRIO compiler wants all the fields |
| 284 | // filled out, but other compilers don't... Sigh. |
| 285 | .line_style = "*-", |
Austin Schuh | 69d0b73 | 2022-07-20 21:19:32 -0700 | [diff] [blame] | 286 | .color = "yellow", |
| 287 | .point_size = 2.0}); |
| 288 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 289 | if (absl::GetFlag(FLAGS_samples)) { |
Austin Schuh | 69d0b73 | 2022-07-20 21:19:32 -0700 | [diff] [blame] | 290 | plotter_.AddLine(samplefile12.first, samplefile12.second, |
| 291 | Plotter::LineOptions{ |
| 292 | .label = absl::StrCat("sample ", node1, " ", node2), |
| 293 | .line_style = "*", |
| 294 | .color = "purple", |
| 295 | }); |
| 296 | plotter_.AddLine(samplefile21.first, samplefile21.second, |
| 297 | Plotter::LineOptions{ |
| 298 | .label = absl::StrCat("sample ", node2, " ", node1), |
| 299 | .line_style = "*", |
| 300 | .color = "green", |
| 301 | }); |
| 302 | } |
| 303 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 304 | if (absl::GetFlag(FLAGS_bounds)) { |
Austin Schuh | 69d0b73 | 2022-07-20 21:19:32 -0700 | [diff] [blame] | 305 | plotter_.AddLine( |
| 306 | noncausalfile12.first, noncausalfile12.second, |
| 307 | Plotter::LineOptions{.label = absl::StrCat("nc ", node1, " ", node2), |
| 308 | .line_style = "-", |
| 309 | .color = "blue"}); |
| 310 | plotter_.AddLine( |
| 311 | noncausalfile21.first, noncausalfile21.second, |
| 312 | Plotter::LineOptions{.label = absl::StrCat("nc ", node2, " ", node1), |
| 313 | .line_style = "-", |
| 314 | .color = "orange"}); |
| 315 | } |
Austin Schuh | 59d93f4 | 2022-07-18 16:52:32 -0700 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | int Main(int argc, const char *const *argv) { |
Austin Schuh | 69d0b73 | 2022-07-20 21:19:32 -0700 | [diff] [blame] | 319 | NodePlotter plotter; |
Austin Schuh | 59d93f4 | 2022-07-18 16:52:32 -0700 | [diff] [blame] | 320 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 321 | if (absl::GetFlag(FLAGS_all)) { |
Austin Schuh | 2c89924 | 2023-02-17 12:19:57 -0800 | [diff] [blame] | 322 | const std::vector<std::pair<std::string, std::string>> connections = |
| 323 | NodeConnections(); |
| 324 | for (std::pair<std::string, std::string> ab : connections) { |
Austin Schuh | 69d0b73 | 2022-07-20 21:19:32 -0700 | [diff] [blame] | 325 | plotter.AddNodes(ab.first, ab.second); |
| 326 | } |
Austin Schuh | 2c89924 | 2023-02-17 12:19:57 -0800 | [diff] [blame] | 327 | if (connections.size() == 0) { |
| 328 | LOG(WARNING) << "No connections found, is something wrong?"; |
| 329 | } |
Austin Schuh | 69d0b73 | 2022-07-20 21:19:32 -0700 | [diff] [blame] | 330 | } else { |
| 331 | CHECK_EQ(argc, 3); |
Austin Schuh | 59d93f4 | 2022-07-18 16:52:32 -0700 | [diff] [blame] | 332 | |
Austin Schuh | 69d0b73 | 2022-07-20 21:19:32 -0700 | [diff] [blame] | 333 | LOG(INFO) << argv[1]; |
| 334 | LOG(INFO) << argv[2]; |
Austin Schuh | 59d93f4 | 2022-07-18 16:52:32 -0700 | [diff] [blame] | 335 | |
Austin Schuh | 69d0b73 | 2022-07-20 21:19:32 -0700 | [diff] [blame] | 336 | const std::string_view node1 = argv[1]; |
| 337 | const std::string_view node2 = argv[2]; |
Austin Schuh | 59d93f4 | 2022-07-18 16:52:32 -0700 | [diff] [blame] | 338 | |
Austin Schuh | 69d0b73 | 2022-07-20 21:19:32 -0700 | [diff] [blame] | 339 | plotter.AddNodes(node1, node2); |
| 340 | } |
Austin Schuh | 59d93f4 | 2022-07-18 16:52:32 -0700 | [diff] [blame] | 341 | |
Austin Schuh | 69d0b73 | 2022-07-20 21:19:32 -0700 | [diff] [blame] | 342 | plotter.Serve(); |
Austin Schuh | 59d93f4 | 2022-07-18 16:52:32 -0700 | [diff] [blame] | 343 | |
| 344 | return 0; |
| 345 | } |
| 346 | |
| 347 | } // namespace aos |
| 348 | |
| 349 | int main(int argc, char **argv) { |
| 350 | aos::InitGoogle(&argc, &argv); |
| 351 | |
| 352 | aos::Main(argc, argv); |
| 353 | } |