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