8 #ifndef NUKLEI_PLOTTER_H
9 #define NUKLEI_PLOTTER_H
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)
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;
35 void push(mapkey_t key, T val)
37 densities_[key].push_back(val);
40 void push(mapkey_t key, T x, T y)
42 functions_[key].push_back(std::make_pair(x, y));
46 void comment(std::string key, U value)
48 comments_.push_back(key +
" = " +
stringify(value));
51 void comment(std::string c)
53 comments_.push_back(c);
56 void write_r(std::string filename)
58 std::ofstream ofs(filename.c_str(), std::ios::out);
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;
65 for (function_map::const_iterator fm_i = functions_.begin();
66 fm_i != functions_.end(); fm_i++)
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++)
73 if (f_i != --fm_i->second.end()) ofs <<
", ";
75 ofs <<
");\n" << std::endl;
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++)
82 if (f_i != --fm_i->second.end()) ofs <<
", ";
84 ofs <<
");\n" << std::endl;
87 for (density_map::const_iterator dm_i = densities_.begin();
88 dm_i != densities_.end(); dm_i++)
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++)
95 if (d_i != --dm_i->second.end()) ofs <<
", ";
97 ofs <<
");\n" << std::endl;
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;
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;
113 void write_octave(std::string filename)
115 std::ofstream ofs(filename.c_str(), std::ios::out);
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;
122 ofs <<
"figure; hold on;\n" << std::endl;
124 for (function_map::const_iterator fm_i = functions_.begin();
125 fm_i != functions_.end(); fm_i++)
127 ofs << clean(fm_i->first) <<
" = [ ";
129 for (function_t::const_iterator f_i = fm_i->second.begin();
130 f_i != fm_i->second.end(); f_i++)
132 ofs << f_i->first <<
" " << f_i->second;
133 if (f_i != --fm_i->second.end()) ofs <<
" ; ";
135 ofs <<
"];\n" << std::endl;
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;
145 if (!densities_.empty())
146 ofs <<
"error(\"KDE not supported in octave.\")\n" << std::endl;
149 static int main(
int argc,
char ** argv )
153 p.comment(
"This is a test.");
155 const double STEP = .1;
157 for (
double d = -3; d <= 3; d += STEP)
163 p.comment(
"step", STEP);
165 p.write_octave(
"/tmp/octave.m");
166 p.write_r(
"/tmp/r.r");
172 function_map functions_;
173 density_map densities_;
174 comment_collection comments_;
176 std::string clean(
const std::string &dirty)
178 std::string c = dirty;
179 for (std::string::iterator s_i = c.begin(); s_i != c.end(); s_i++)
181 if (! (*s_i >=
'a' && *s_i <=
'z') &&
182 ! (*s_i >=
'A' && *s_i <=
'z') &&
183 ! (*s_i >=
'0' && *s_i <=
'9'))