SedSat3 1.1.6
Sediment Source Apportionment Tool - Advanced statistical methods for environmental pollution research
Loading...
Searching...
No Matches
cmbvector.cpp
Go to the documentation of this file.
1#include "cmbvector.h"
2#include "QJsonArray"
3#include "Vector.h"
4#include <QFile>
5
7{
8
9}
10
20
21CMBVector::CMBVector(const CVector& mp):CVector(mp), Interface()
22{
23 labels.resize(getsize());
24}
25
26CMBVector::CMBVector(const CVector_arma& mp): Interface()
27{
28 labels.resize(getsize());
29 CVector::operator=(mp);
30}
31
32CMBVector::CMBVector(int n):CVector(n), Interface()
33{
34 labels.resize(n);
35
36}
37
39{
40 labels = mp.labels;
43 lowlimit = mp.lowlimit;
45 CVector::operator=(mp);
46 return *this;
47
48}
49
50CMBVector& CMBVector::operator=(const CVector_arma &mp)
51{
52 CVector::operator=(mp);
53 labels.resize(mp.size());
54 return *this;
55}
56
58{
59 CVector::operator=(mp);
60 labels.resize(getsize());
61 return *this;
62}
63
64
65QJsonObject CMBVector::toJsonObject() const
66{
67 QJsonObject out;
68 QJsonArray vector;
69 QJsonArray elementlabels;
70 for (int i=0; i<getsize(); i++)
71 {
72 vector.append(valueAt(i));
73 elementlabels.append(QString::fromStdString(labels[i]));
74 }
75 out["Vector"] = vector;
76 out["Labels"] = elementlabels;
77 out["XAxisLabel"] = XAxisLabel();
78 out["YAxisLabel"] = YAxisLabel();
79 return out;
80}
81bool CMBVector::ReadFromJsonObject(const QJsonObject &jsonobject)
82{
83 QJsonArray array = jsonobject["Vector"].toArray();
84 QJsonArray elementlabels = jsonobject["Labels"].toArray();
85 vec.resize(array.size());
86 num = array.size();
87 labels.resize(array.size());
88 for (unsigned int i=0; i<array.size(); i++)
89 {
90 vec[i]=array[i].toDouble();
91 labels[i]=elementlabels[i].toString().toStdString();
92 }
93
94 if (jsonobject.contains("XAxisLabel"))
95 {
96 SetXAxisLabel(jsonobject["XAxisLabel"].toString());
97 }
98
99 if (jsonobject.contains("YAxisLabel"))
100 {
101 SetXAxisLabel(jsonobject["YAxisLabel"].toString());
102 }
103 return true;
104}
105
106
108{
109 string out;
110 out += XAxisLabel().toStdString() + "," + YAxisLabel().toStdString() + "\n";
111 if (!boolean_values)
112 { for (int j=0; j<getsize(); j++)
113 {
114 out += labels[j] + "," + QString::number(valueAt(j)).toStdString()+"\n";
115 }
116 }
117 else
118 { for (int j=0; j<getsize(); j++)
119 {
120 if (valueAt(j)==1)
121 out += labels[j] + ", Fail \n";
122 else
123 out += labels[j] + ", Pass \n";
124 }
125 }
126
127 return out;
128}
129
130bool CMBVector::writetofile(QFile* file)
131{
132 file->write(QString::fromStdString(ToString()).toUtf8());
133 return true;
134}
135
136double CMBVector::valueAt(int i) const
137{
138 return CVector::operator[](i);
139}
140
141double CMBVector::valueAt(const string &label ) const
142{
143 for (int i=0; i<getsize(); i++)
144 if (labels[i]==label)
145 return valueAt(i);
146 return -999;
147}
148
149int CMBVector::LookupLabel(const string &label ) const
150{
151 for (int i=0; i<getsize(); i++)
152 if (labels[i]==label)
153 return i;
154 return -1;
155}
156
157QTableWidget *CMBVector::ToTable()
158{
159 QTableWidget *tablewidget = new QTableWidget();
160 tablewidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
161 tablewidget->setColumnCount(1);
162 tablewidget->setRowCount(getsize());
163 QStringList rowheaders;
164 QStringList colheaders;
165
166 colheaders << YAxisLabel();
167
168 for (int i=0; i<getsize(); i++)
169 {
170 rowheaders << QString::fromStdString(labels[i]);
171 if (!boolean_values)
172 { tablewidget->setItem(i,0, new QTableWidgetItem(QString::number(valueAt(i))));
174 {
175 if (valueAt(i)>highlimit || valueAt(i)<lowlimit)
176 {
177 tablewidget->item(i,0)->setForeground(QColor(Qt::red));
178 }
179 }
180 }
181 else
182 {
183 if (valueAt(i)==1)
184 tablewidget->setItem(i,0, new QTableWidgetItem("Fail"));
185 else
186 tablewidget->setItem(i,0, new QTableWidgetItem("Pass"));
187
188 }
189 }
190
191 tablewidget->setHorizontalHeaderLabels(colheaders);
192 tablewidget->setVerticalHeaderLabels(rowheaders);
193 return tablewidget;
194}
195
196CVector CMBVector::toVector() const
197{
198 CVector out(num);
199 out.vec = vec;
200 return out;
201}
202
203CMBVector CMBVector::Sort(const CMBVector &sortvector) const
204{
205 CMBVector eliminated;
206 if (sortvector.getsize()==0)
207 eliminated = *this;
208 else
209 eliminated = sortvector;
210 CMBVector out;
211 for (int i=0; i<getsize(); i++)
212 {
213 string max_element = eliminated.MaxElement();
214 out.append(max_element,valueAt(max_element));
215 eliminated = eliminated.Eliminate(max_element);
216 }
217 return out;
218}
219CMBVector CMBVector::AbsSort(const CMBVector &sortvector) const
220{
221 CMBVector eliminated;
222 if (sortvector.getsize()==0)
223 eliminated = *this;
224 else
225 eliminated = sortvector;
226 CMBVector out;
227 for (int i=0; i<getsize(); i++)
228 {
229 string max_element = eliminated.MaxAbsElement();
230 out.append(max_element,valueAt(max_element));
231 eliminated = eliminated.Eliminate(max_element);
232 }
233 return out;
234}
236{
237 double val = -1e24;
238 string out;
239 for (int i=0; i<getsize(); i++)
240 {
241 if (val<valueAt(i))
242 { val = valueAt(i);
243 out = Label(i);
244 }
245 }
246 return out;
247}
249{
250 double val = -1e24;
251 string out;
252 for (int i=0; i<getsize(); i++)
253 {
254 if (val<fabs(valueAt(i)))
255 { val = fabs(valueAt(i));
256 out = Label(i);
257 }
258 }
259 return out;
260}
261CMBVector CMBVector::Eliminate(const string &element) const
262{
263 CMBVector out;
264 for (int i=0; i<getsize(); i++)
265 {
266 if (Label(i)!=element)
267 out.append(Label(i),valueAt(i));
268 }
269 return out;
270}
271
272void CMBVector::append(const string &label, const double &val)
273{
274 vec.push_back(val);
275 labels.push_back(label);
276 num = vec.size();
277
278}
279
280CMBVector CMBVector::ExtractWithinRange(const double &lowval, const double &highval) const
281{
282 CMBVector out;
283 for (int i=0; i<size(); i++)
284 {
285 if (valueAt(i)<highval && valueAt(i)>lowval)
286 out.append(labels[i],valueAt(i));
287 }
288 return out;
289}
290
292{
293 CMBVector out;
294 out.append(labels[0], valueAt(0));
295 for (int i = 1; i < size(); i++)
296 {
297 if (valueAt(i) < valueAt(i-1))
298 out.append(labels[i], valueAt(i));
299 }
300 return out;
301}
302
303CMBVector CMBVector::Extract(int start, int end) const
304{
305 CMBVector out;
306 for (int i=start; i<=end; i++)
307 {
308 out.append(Label(i),valueAt(i));
309 }
310 return out;
311}
312
313
315{
316 CMBVector out = V1;
317 out.SetLabels(V1.Labels());
318 out += V2;
319 return out;
320}
321CMBVector operator+(double d, const CMBVector& V1)
322{
323 CMBVector out = V1;
324 out.SetLabels(V1.Labels());
325 out += d;
326 return out;
327}
328CMBVector operator+(const CMBVector& V1, double d)
329{
330 CMBVector out = V1;
331 out.SetLabels(V1.Labels());
332 out += d;
333 return out;
334}
336{
337 CMBVector out = V1;
338 out.SetLabels(V1.Labels());
339 out -= V2;
340 return out;
341}
342CMBVector operator-(double d, const CMBVector& V1)
343{
344 CMBVector out(V1.size());
345 out.SetLabels(V1.Labels());
346 out -= V1;
347 out += d;
348 return out;
349}
350CMBVector operator-(const CMBVector& V1, double d)
351{
352 CMBVector out = V1;
353 out.SetLabels(V1.Labels());
354 out -= d;
355 return out;
356}
358{
359 CMBVector out = V1;
360 out.SetLabels(V1.Labels());
361 out *= V2;
362 return out;
363}
364CMBVector operator*(double d, const CMBVector& V1)
365{
366 CMBVector out = V1;
367 out.SetLabels(V1.Labels());
368 out *= d;
369 return out;
370}
371CMBVector operator/(const CMBVector& V1, double d)
372{
373 CMBVector out = V1;
374 out.SetLabels(V1.Labels());
375 out /= d;
376 return out;
377}
Vector class with string labels for Chemical Mass Balance analysis.
Definition cmbvector.h:17
CMBVector & operator=(const CMBVector &mp)
Assignment operator from CMBVector.
Definition cmbvector.cpp:38
CVector toVector() const
Converts to base CVector type (without labels)
QTableWidget * ToTable() override
Creates a QTableWidget for displaying vector data.
CMBVector Eliminate(const string &element) const
Creates new vector with specified element removed.
bool writetofile(QFile *file) override
Writes vector to file in CSV format.
void SetLabels(const vector< string > &label)
Sets all element labels at once.
Definition cmbvector.h:151
vector< string > labels
Labels for each element.
Definition cmbvector.h:242
QJsonObject toJsonObject() const override
Serializes vector to JSON format.
Definition cmbvector.cpp:65
CMBVector Extract(int start, int end) const
Extracts a range of elements by index.
CMBVector ExtractWithinRange(const double &lowval, const double &highval) const
Extracts elements with values within specified range.
string MaxAbsElement() const
Finds label of element with maximum absolute value.
string ToString() const override
Converts vector to CSV-formatted string.
CMBVector Sort(const CMBVector &sortvector=CMBVector()) const
Sorts vector by values in descending order.
string MaxElement() const
Finds label of element with maximum value.
bool ReadFromJsonObject(const QJsonObject &jsonobject) override
Deserializes vector from JSON format.
Definition cmbvector.cpp:81
CMBVector ExtractUpToMinimum() const
Extracts elements up to first minimum value.
int size() const
Gets number of elements in vector.
Definition cmbvector.h:221
void append(const string &label, const double &val)
Appends a labeled element to the vector.
CMBVector()
Default constructor - creates an empty vector.
Definition cmbvector.cpp:6
bool boolean_values
Display values as Pass/Fail instead of numbers.
Definition cmbvector.h:243
CMBVector AbsSort(const CMBVector &sortvector=CMBVector()) const
Sorts vector by absolute values in descending order.
double valueAt(int i) const
Gets value at specified index.
string Label(int i) const
Gets label at specified index.
Definition cmbvector.h:118
vector< string > Labels() const
Gets all element labels.
Definition cmbvector.h:138
int LookupLabel(const string &label) const
Finds index of element with given label.
Abstract base class providing common serialization and visualization interface.
Definition interface.h:65
double lowlimit
Lower threshold for value highlighting (when enabled)
Definition interface.h:211
QString XAxisLabel() const
Get the X-axis label.
Definition interface.h:287
bool SetXAxisLabel(const QString &label)
Set the X-axis label.
Definition interface.h:307
QString YAxisLabel() const
Get the Y-axis label.
Definition interface.h:298
bool highlightoutsideoflimit
Flag indicating whether to highlight values outside defined limits.
Definition interface.h:221
double highlimit
Upper threshold for value highlighting (when enabled)
Definition interface.h:212
CMBVector operator/(const CMBVector &V1, double d)
Vector-scalar division operator.
CMBVector operator*(const CMBVector &V1, const CMBVector &V2)
Element-wise vector multiplication operator.
CMBVector operator+(const CMBVector &V1, const CMBVector &V2)
Vector addition operator.
CMBVector operator-(const CMBVector &V1, const CMBVector &V2)
Vector subtraction operator.