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