Robowflex  v0.1
Making MoveIt Easy
gnuplot.cpp
Go to the documentation of this file.
1 /* Author: Zachary Kingston */
2 
5 #include <robowflex_library/io.h>
7 
8 using namespace robowflex::IO;
9 
10 #if IS_BOOST_164
11 namespace bp = boost::process;
12 #endif
13 
15 {
16 #if IS_BOOST_164
17  auto path = bp::search_path("gnuplot");
18  if (path.empty())
19  throw Exception(1, "GNUPlot not found, please install!");
20 
21  gnuplot_ = bp::child(bp::search_path("gnuplot"), //"-persist", //
22  bp::std_err > error_, //
23  // bp::std_out > output_, //
24  bp::std_in < input_);
25 #else
26  throw Exception(1, "GNUPlot helper not supported, Boost 1.64 and above is required!");
27 #endif
28 }
29 
31 {
32 #if IS_BOOST_164
33  input_ << line;
34  if (debug_)
35  std::cout << line;
36 #endif
37 }
38 
40 {
41  write(line);
42  flush();
43 }
44 
46 {
47 #if IS_BOOST_164
48  input_ << std::endl;
49  if (debug_)
51 #endif
52 }
53 
55 {
56  auto in = getInstance(options.instance);
57 
58  in->writeline(log::format("set term %1% noraise", options.mode));
59  in->writeline(log::format("set title \"%1%\"", options.title));
60 
61  if (not options.x.label.empty())
62  in->writeline(log::format("set xlabel \"%1%\"", options.x.label));
63 
64  if (std::isfinite(options.x.max))
65  in->writeline(log::format("set xrange [:%1%]", options.x.max));
66 
67  if (std::isfinite(options.x.min))
68  in->writeline(log::format("set xrange [%1%:]", options.x.min));
69 
70  if (not options.y.label.empty())
71  in->writeline(log::format("set ylabel \"%1%\"", options.y.label));
72 
73  if (std::isfinite(options.y.max))
74  in->writeline(log::format("set yrange [:%1%]", options.y.max));
75 
76  if (std::isfinite(options.y.min))
77  in->writeline(log::format("set yrange [%1%:]", options.y.min));
78 }
79 
81 {
82  configurePlot(options);
83  auto in = getInstance(options.instance);
84 
85  in->writeline("set datafile separator \",\"");
86  in->write("plot ");
87 
88  auto n = options.points.size();
89 
90  auto it1 = options.points.begin();
91  for (std::size_t i = 0; i < n; ++i, ++it1)
92  {
93  in->write(log::format("'%1%' using 1:2 with lines lw 2 title \"%2%\"", //
94  (i == 0) ? "-" : "", //
95  it1->first));
96  if (i != n - 1)
97  in->write(", ");
98  }
99 
100  in->flush();
101 
102  auto it2 = options.points.begin();
103  for (std::size_t i = 0; i < n; ++i, ++it2)
104  {
105  for (const auto &point : it2->second)
106  in->writeline(log::format("%1%,%2%", point.first, point.second));
107 
108  in->writeline("e");
109  }
110 }
111 
113 {
114  configurePlot(options);
115  auto in = getInstance(options.instance);
116 
117  in->writeline("set datafile separator \",\"");
118 
119  in->writeline("set style data boxplot");
120  in->writeline("set style fill solid 0.5 border -1");
121  in->writeline("unset key");
122 
123  if (options.sorted)
124  in->writeline("set style boxplot sorted");
125 
126  if (options.outliers)
127  in->writeline("set style boxplot outliers pointtype 7");
128  else
129  in->writeline("set style boxplot nooutliers");
130 
131  auto n = options.values.size();
132 
133  in->write("set xtics (");
134  auto it1 = options.values.begin();
135  for (std::size_t i = 0; i < n; ++i, ++it1)
136  {
137  in->write(log::format("\"%1%\" %2%", it1->first, i + 1));
138  if (i != n - 1)
139  in->write(", ");
140  }
141  in->writeline(") scale 0.0");
142 
143  in->write("plot ");
144  for (std::size_t i = 0; i < n; ++i)
145  {
146  in->write(log::format("'%1%' using (%2%):1", //
147  (i == 0) ? "-" : "", //
148  i + 1));
149  if (i != n - 1)
150  in->write(", ");
151  }
152 
153  in->flush();
154 
155  auto it2 = options.values.begin();
156  for (std::size_t i = 0; i < n; ++i, ++it2)
157  {
158  for (const auto &point : it2->second)
159  in->writeline(log::format("%1%", point));
160 
161  in->writeline("e");
162  }
163 }
164 
166 {
167  if (instances_.find(name) == instances_.end())
168  instances_.emplace(name, std::make_shared<Instance>());
169 
170  return instances_.find(name)->second;
171 }
172 
174 {
175 }
176 
178 {
179 }
180 
182 {
184  bpo.instance = results.name;
185  bpo.title = log::format("\\\"%1%\\\" for Experiment \\\"%2%\\\"", metric_, results.name);
186  bpo.y.label = metric_;
187  bpo.y.min = 0.;
188 
189  for (const auto &query : results.data)
190  {
191  const auto &name = query.first;
192  const auto &points = query.second;
193 
194  std::vector<double> values;
195  for (const auto &run : points)
196  {
197  if (metric_ == "time")
198  values.emplace_back(run->time);
199  else
200  values.emplace_back(boost::get<double>(run->metrics[metric_]));
201  }
202 
203  bpo.values.emplace(name, values);
204  }
205 
206  helper_.boxplot(bpo);
207 }
Exception that contains a message and an error code.
Definition: util.h:15
void writeline(const std::string &line)
Definition: gnuplot.cpp:39
void write(const std::string &line)
Definition: gnuplot.cpp:30
Instance()
Constructor. Setups up pipe to GNUPlot.
Definition: gnuplot.cpp:14
std::map< std::string, std::shared_ptr< Instance > > instances_
Map of open GNUPlot instances.
Definition: gnuplot.h:123
std::shared_ptr< Instance > getInstance(const std::string &name)
Get the named GNUPlot instance.
Definition: gnuplot.cpp:165
void boxplot(const BoxPlotOptions &options)
Plot box data.
Definition: gnuplot.cpp:112
void timeseries(const TimeSeriesOptions &options)
Plot timeseries data.
Definition: gnuplot.cpp:80
void configurePlot(const PlottingOptions &options)
Configure a plot using common options.
Definition: gnuplot.cpp:54
~GNUPlotPlanDataSetOutputter() override
Destructor.
Definition: gnuplot.cpp:177
void dump(const PlanDataSet &results) override
Visualize results.
Definition: gnuplot.cpp:181
GNUPlotPlanDataSetOutputter(const std::string &metric)
Constructor.
Definition: gnuplot.cpp:173
Detailed statistics about a benchmark of multiple queries.
Definition: benchmarking.h:141
std::map< std::string, std::vector< PlanDataPtr > > data
Map of query name to collected data.
Definition: benchmarking.h:177
std::string name
Name of this dataset.
Definition: benchmarking.h:167
T emplace_back(T... args)
T empty(T... args)
T endl(T... args)
T isfinite(T... args)
File and ROS Input / Output operations.
Definition: yaml.cpp:1846
std::string format(const std::string &fmt, Args &&... args)
Recursion base case, return string form of formatted arguments.
Definition: log.h:60
std::map< std::string, Values > values
Map of names to data.
Definition: gnuplot.h:76
double max
Upper axis limit. If NaN, will auto-adjust.
Definition: gnuplot.h:43
double min
Lower axis limit. If NaN, will auto-adjust.
Definition: gnuplot.h:44
std::string title
Title of the plot.
Definition: gnuplot.h:47
std::string mode
Terminal mode for GNUPlot.
Definition: gnuplot.h:48
Time series plotting options.
Definition: gnuplot.h:61
std::map< std::string, Series > points
Map of names to time series data.
Definition: gnuplot.h:62