Plotter.h
Go to the documentation of this file.
1 // (C) Copyright Renaud Detry 2007-2015.
2 // Distributed under the GNU General Public License and under the
3 // BSD 3-Clause License (See accompanying file LICENSE.txt).
4 
5 /** @file */
6 
7 
8 #ifndef NUKLEI_PLOTTER_H
9 #define NUKLEI_PLOTTER_H
10 
11 #include <map>
12 #include <set>
13 #include <vector>
14 #include <cassert>
15 #include <fstream>
16 #include <nuklei/Common.h>
17 
18 namespace nuklei {
19 
20 #define NUKLEI_PLOTTER_F(p, x, y) p.push(#y, x, y)
21 #define NUKLEI_PLOTTER_D(p, x) p.push(#x, x)
22 #define NUKLEI_PLOTTER_COMMENT(p, var) p.comment(#var, var)
23 
24  class Plotter
25  {
26  public:
27  typedef double T;
28  typedef std::string mapkey_t;
29  typedef std::vector<std::string> comment_collection;
30  typedef std::vector< std::pair<T,T> > function_t;
31  typedef std::vector<T> density_t;
32  typedef std::map< mapkey_t, function_t > function_map;
33  typedef std::map< mapkey_t, density_t > density_map;
34 
35  void push(mapkey_t key, T val)
36  {
37  densities_[key].push_back(val);
38  }
39 
40  void push(mapkey_t key, T x, T y)
41  {
42  functions_[key].push_back(std::make_pair(x, y));
43  }
44 
45  template<typename U>
46  void comment(std::string key, U value)
47  {
48  comments_.push_back(key + " = " + stringify(value));
49  }
50 
51  void comment(std::string c)
52  {
53  comments_.push_back(c);
54  }
55 
56  void write_r(std::string filename)
57  {
58  std::ofstream ofs(filename.c_str(), std::ios::out);
59 
60  for (std::vector<std::string>::const_iterator v_i = comments_.begin();
61  v_i != comments_.end(); v_i++)
62  ofs << "# " << *v_i << std::endl;
63  ofs << std::endl;
64 
65  for (function_map::const_iterator fm_i = functions_.begin();
66  fm_i != functions_.end(); fm_i++)
67  {
68  ofs << clean(fm_i->first) << "_x = c(";
69  for (function_t::const_iterator f_i = fm_i->second.begin();
70  f_i != fm_i->second.end(); f_i++)
71  {
72  ofs << f_i->first;
73  if (f_i != --fm_i->second.end()) ofs << ", ";
74  }
75  ofs << ");\n" << std::endl;
76 
77  ofs << clean(fm_i->first) << "_y = c(";
78  for (function_t::const_iterator f_i = fm_i->second.begin();
79  f_i != fm_i->second.end(); f_i++)
80  {
81  ofs << f_i->second;
82  if (f_i != --fm_i->second.end()) ofs << ", ";
83  }
84  ofs << ");\n" << std::endl;
85  }
86 
87  for (density_map::const_iterator dm_i = densities_.begin();
88  dm_i != densities_.end(); dm_i++)
89  {
90  ofs << clean(dm_i->first) << " = c( ";
91  for (density_t::const_iterator d_i = dm_i->second.begin();
92  d_i != dm_i->second.end(); d_i++)
93  {
94  ofs << *d_i;
95  if (d_i != --dm_i->second.end()) ofs << ", ";
96  }
97  ofs << ");\n" << std::endl;
98  }
99 
100  for (function_map::const_iterator fm_i = functions_.begin();
101  fm_i != functions_.end(); fm_i++)
102  ofs << "plot(" << clean(fm_i->first) << "_x, "
103  << clean(fm_i->first) << "_y"
104  << ");\n" << std::endl;
105 
106  for (density_map::const_iterator dm_i = densities_.begin();
107  dm_i != densities_.end(); dm_i++)
108  ofs << "plot(density(" << clean(dm_i->first)
109  << "));\n" << std::endl;
110 
111  }
112 
113  void write_octave(std::string filename)
114  {
115  std::ofstream ofs(filename.c_str(), std::ios::out);
116 
117  for (std::vector<std::string>::const_iterator v_i = comments_.begin();
118  v_i != comments_.end(); v_i++)
119  ofs << "% " << *v_i << std::endl;
120  ofs << std::endl;
121 
122  ofs << "figure; hold on;\n" << std::endl;
123 
124  for (function_map::const_iterator fm_i = functions_.begin();
125  fm_i != functions_.end(); fm_i++)
126  {
127  ofs << clean(fm_i->first) << " = [ ";
128 
129  for (function_t::const_iterator f_i = fm_i->second.begin();
130  f_i != fm_i->second.end(); f_i++)
131  {
132  ofs << f_i->first << " " << f_i->second;
133  if (f_i != --fm_i->second.end()) ofs << " ; ";
134  }
135  ofs << "];\n" << std::endl;
136  }
137 
138 
139  for (function_map::const_iterator fm_i = functions_.begin();
140  fm_i != functions_.end(); fm_i++)
141  ofs << "plot(" << clean(fm_i->first) << "(:,1), "
142  << clean(fm_i->first) << "(:,2)"
143  << ", '-;" << fm_i->first << ";');\n" << std::endl;
144 
145  if (!densities_.empty())
146  ofs << "error(\"KDE not supported in octave.\")\n" << std::endl;
147  }
148 
149  static int main( int argc, char ** argv )
150  {
151  Plotter p;
152 
153  p.comment("This is a test.");
154 
155  const double STEP = .1;
156 
157  for (double d = -3; d <= 3; d += STEP)
158  {
159  p.push("y", d, d*d);
160  p.push("p", d*d);
161  }
162 
163  p.comment("step", STEP);
164 
165  p.write_octave("/tmp/octave.m");
166  p.write_r("/tmp/r.r");
167 
168  return 0;
169  }
170 
171  private:
172  function_map functions_;
173  density_map densities_;
174  comment_collection comments_;
175 
176  std::string clean(const std::string &dirty)
177  {
178  std::string c = dirty;
179  for (std::string::iterator s_i = c.begin(); s_i != c.end(); s_i++)
180  {
181  if (! (*s_i >= 'a' && *s_i <= 'z') &&
182  ! (*s_i >= 'A' && *s_i <= 'z') &&
183  ! (*s_i >= '0' && *s_i <= '9'))
184  *s_i = '_';
185  }
186  return "a" + c;
187  }
188  };
189 
190 
191 }
192 
193 #endif
194 
Public namespace.
Definition: Color.cpp:9
std::string stringify(const T &x, int precision=-1, int width=0)
Converts an object to a std::string using operator<<.
Definition: Common.h:333
Definition: Plotter.h:24
© Copyright 2007-2013 Renaud Detry.
Distributed under the terms of the GNU General Public License (GPL).
(See accompanying file LICENSE.txt or copy at http://www.gnu.org/copyleft/gpl.html.)
Revised Sun Sep 13 2020 19:10:06.