-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLinePlot.cpp
More file actions
157 lines (141 loc) · 3.88 KB
/
LinePlot.cpp
File metadata and controls
157 lines (141 loc) · 3.88 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "LinePlot.h"
#include "Exceptions.h"
#include "Helper.h"
#include "Log.h"
#include "BasicStatistics.h"
#include "Settings.h"
#include <QStringList>
#include <limits>
#include <QProcess>
#include <QStandardPaths>
LinePlot::LinePlot()
: yrange_set_(false)
{
}
void LinePlot::addLine(const QVector<double>& values, QString label)
{
if (xvalues_.count()!=0 && values.count()!=xvalues_.count())
{
THROW(ArgumentException, "Plot '" + label + "' has " + QString::number(values.count()) + " values, but " + QString::number(xvalues_.count()) + " are expected because x axis values are set!");
}
lines_.append(PlotLine(values, label));
}
void LinePlot::setXValues(const QVector<double>& xvalues)
{
if (lines_.count()!=0)
{
THROW(ProgrammingException, "You have to set x axis values of LinePlot before adding any lines!");
}
xvalues_ = xvalues;
}
void LinePlot::setXLabel(QString xlabel)
{
xlabel_ = xlabel;
}
void LinePlot::setYLabel(QString ylabel)
{
ylabel_ = ylabel;
}
void LinePlot::setYRange(double ymin, double ymax)
{
ymin_ = ymin;
ymax_ = ymax;
yrange_set_ = true;
}
void LinePlot::store(QString filename)
{
//check if python is installed
QString python_exe = Settings::path("python_exe", true);
if (python_exe=="") python_exe = QStandardPaths::findExecutable("python3");
if (python_exe=="")
{
Log::warn("Python executable not found in PATH - skipping plot generation!");
return;
}
//create python script
QString scriptfile = Helper::tempFileName(".py");
QStringList script;
script.append("from numpy import nan");
script.append("import matplotlib as mpl");
script.append("mpl.use('Agg')");
script.append("import matplotlib.pyplot as plt");
script.append("plt.figure(figsize=(6, 4), dpi=100)");
if(ylabel_!="") script.append("plt.ylabel('" + ylabel_ + "')");
if(xlabel_!="") script.append("plt.xlabel('" + xlabel_ + "')");
if(!yrange_set_)
{
double min = std::numeric_limits<double>::max();
double max = -std::numeric_limits<double>::max();
foreach(const PlotLine& line, lines_)
{
foreach(double value, line.values)
{
min = std::min(value, min);
max = std::max(value, max);
}
}
ymin_ = min-0.01*(max-min);
ymax_ = max+0.01*(max-min);;
}
if(BasicStatistics::isValidFloat(ymin_) && BasicStatistics::isValidFloat(ymax_))
{
script.append("plt.ylim(" + QString::number(ymin_) + "," + QString::number(ymax_) + ")");
}
QString xvaluestring = "";
if (xvalues_.count()>0)
{
xvaluestring += "[" + QString::number(xvalues_[0]);
for (int i=1; i<xvalues_.count(); ++i)
{
xvaluestring += ","+QString::number(xvalues_[i]);
}
xvaluestring += "],";
}
foreach(const PlotLine& line, lines_)
{
QString valuestring = "[";
if (line.values.count()>0)
{
valuestring += QString::number(line.values[0]);
for (int i=1; i<line.values.count(); ++i)
{
valuestring += ","+QString::number(line.values[i]);
}
}
valuestring += "]";
script.append("plt.plot(" + xvaluestring + valuestring + ", label='" + line.label + "')");
}
if(lines_.count()==1)
{
if (lines_[0].label!="")
{
script.append("plt.title('" + lines_[0].label + "')");
}
}
else
{
script.append("plt.legend(prop={'size':8}, labelspacing=0.2)");
}
script.append("plt.savefig(\"" + filename.replace("\\", "/") + "\", bbox_inches=\'tight\', dpi=100)");
Helper::storeTextFile(scriptfile, script);
//execute scipt
QProcess process;
process.setProcessChannelMode(QProcess::MergedChannels);
process.start(python_exe, QStringList() << scriptfile);
bool success = process.waitForFinished(-1);
QByteArray output = process.readAll();
if (!success || process.exitCode()>0)
{
THROW(ProgrammingException, "Could not execute python script:\n" + scriptfile + "\n Error message is: " + output);
}
//remove temporary file
QFile::remove(scriptfile);
}
LinePlot::PlotLine::PlotLine()
{
}
LinePlot::PlotLine::PlotLine(const QVector<double>& v, QString l)
: values(v)
, label(l)
{
}