-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcurve.hpp
57 lines (50 loc) · 1.62 KB
/
curve.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/* curve.hpp for LEICHT
* Copyright (C) 2017 Mo Zhou <[email protected]>
* MIT License
*/
#if !defined(_LEICHT_CURVE_HPP)
#include "leicht.hpp"
#include <cstdlib>
#include <cassert>
#include <iostream>
#include <fstream>
#include <string>
class Curve {
public:
// list of iter-value tuples
std::vector<std::pair<size_t, float>> data;
// Construct a curve object
Curve(void) { }
// append a new value to the curve
void append(size_t iteration, float value) {
data.push_back(std::pair<size_t, float>(iteration, value));
}
// dump to screen or a file
void dump(std::string fname = "") {
if (fname == "")
for (size_t i = 0; i < data.size(); i++)
std::cout << data[i].first << " " << data[i].second << std::endl;
else {
std::ofstream f (fname); assert(f);
for (size_t i = 0; i < data.size(); i++)
f << data[i].first << " " << data[i].second << "\n";
f.close();
}
}
// draw the curve info a file, the format is controled by the file name
// extension. The extension is parsed by pylab. If you need to customize
// the generated picture, just modify the generated python file.
// XXX: Dirty hack, but it works very well.
void draw(std::string fname) {
this->dump(fname + ".data");
std::ofstream py (fname + ".py"); assert(py);
py << "import pylab" << std::endl;
py << "curve = pylab.loadtxt('" << fname << ".data')" << std::endl;
py << "pylab.plot(curve[:,0], curve[:,1])" << std::endl;
py << "pylab.savefig('" << fname << "')" << endl;
py.close();
system(("python3 " + fname + ".py").c_str());
std::cout << "Curve: curve saved to " << fname << std::endl;
}
};
#endif // _LEICHT_CURVE_HPP