timing_histogram.cpp
Go to the documentation of this file.
1 /*
2  * OpenVINS: An Open Platform for Visual-Inertial Research
3  * Copyright (C) 2018-2023 Patrick Geneva
4  * Copyright (C) 2018-2023 Guoquan Huang
5  * Copyright (C) 2018-2023 OpenVINS Contributors
6  * Copyright (C) 2018-2019 Kevin Eckenhoff
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <https://www.gnu.org/licenses/>.
20  */
21 
22 #include <Eigen/Eigen>
23 #include <boost/algorithm/string/predicate.hpp>
24 #include <boost/filesystem.hpp>
25 #include <boost/foreach.hpp>
26 #include <fstream>
27 #include <iostream>
28 #include <string>
29 
30 #include "utils/Loader.h"
31 #include "utils/Statistics.h"
32 #include "utils/colors.h"
33 #include "utils/print.h"
34 
35 #ifdef HAVE_PYTHONLIBS
36 
37 // import the c++ wrapper for matplot lib
38 // https://github.com/lava/matplotlib-cpp
39 // sudo apt-get install python-matplotlib python-numpy python2.7-dev
40 #include "plot/matplotlibcpp.h"
41 
42 #endif
43 
44 int main(int argc, char **argv) {
45 
46  // Verbosity setting
48 
49  // Ensure we have a path
50  if (argc < 3) {
51  PRINT_ERROR(RED "ERROR: Please specify a timing file\n" RESET);
52  PRINT_ERROR(RED "ERROR: ./timing_histagram <file_times.txt> <num_bins>\n" RESET);
53  PRINT_ERROR(RED "ERROR: rosrun ov_eval timing_flamegraph <file_times.txt> <num_bins>\n" RESET);
54  std::exit(EXIT_FAILURE);
55  }
56  int nbins = atoi(argv[2]);
57 
58  // Load it!!
59  std::vector<std::string> names;
60  std::vector<double> times;
61  std::vector<Eigen::VectorXd> timing_values;
62  ov_eval::Loader::load_timing_flamegraph(argv[1], names, times, timing_values);
63  PRINT_INFO("[TIME]: loaded %d timestamps from file (%d categories)!!\n", (int)times.size(), (int)names.size());
64 
65  // Our categories
66  std::vector<ov_eval::Statistics> stats;
67  for (size_t i = 0; i < names.size(); i++)
68  stats.push_back(ov_eval::Statistics());
69 
70  // Loop through each and report the average timing information
71  for (size_t i = 0; i < times.size(); i++) {
72  for (size_t c = 0; c < names.size(); c++) {
73  stats.at(c).timestamps.push_back(times.at(i));
74  stats.at(c).values.push_back(1000.0 * timing_values.at(i)(c));
75  }
76  }
77 
78 #ifdef HAVE_PYTHONLIBS
79 
80  // Valid colors
81  // https://matplotlib.org/stable/tutorials/colors/colors.html
82  // std::vector<std::string> colors_valid = {"blue","aqua","lightblue","lightgreen","yellowgreen","green"};
83  std::vector<std::string> colors_valid = {"navy", "blue", "lightgreen", "green", "orange", "goldenrod", "red", "pink", "black"};
84 
85  // Plot each histogram
86  for (size_t i = 0; i < names.size(); i++) {
88  matplotlibcpp::hist(stats.at(i).values, nbins, colors_valid.at(i % colors_valid.size()));
89  matplotlibcpp::ylabel(names.at(i));
90  matplotlibcpp::xlabel("execution time (ms)");
92  }
93 
94  // Display to the user
95  matplotlibcpp::show(true);
96 
97 #endif
98 
99  // Done!
100  return EXIT_SUCCESS;
101 }
Statistics object for a given set scalar time series values.
Definition: Statistics.h:39
#define RESET
RED
bool hist(const std::vector< Numeric > &y, long bins=10, std::string color="b", double alpha=1.0, bool cumulative=false)
void figure_size(size_t w, size_t h)
int main(int argc, char **argv)
void show(const bool block=true)
static void setPrintLevel(const std::string &level)
void xlabel(const std::string &str, const std::map< std::string, std::string > &keywords={})
void ylabel(const std::string &str, const std::map< std::string, std::string > &keywords={})
void tight_layout()
static void load_timing_flamegraph(std::string path, std::vector< std::string > &names, std::vector< double > &times, std::vector< Eigen::VectorXd > &timing_values)
Load comma separated timing file from pid_ros.py file.
Definition: Loader.cpp:240


ov_eval
Author(s): Patrick Geneva , Kevin Eckenhoff , Guoquan Huang
autogenerated on Wed Jun 21 2023 03:05:40