SedSat3 1.1.6
Sediment Source Apportionment Tool - Advanced statistical methods for environmental pollution research
Loading...
Searching...
No Matches
cmbmatrix.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 "cmbmatrix.h"
5#include "QJsonArray"
6#include "Vector.h"
7#include <QFile>
8
10{
11
12}
13
22
23CMBMatrix::CMBMatrix(int n):CMatrix(n), Interface()
24{
25 columnlabels.resize(n);
26 rowlabels.resize(n);
27}
28CMBMatrix::CMBMatrix(int n, int m):CMatrix(m,n), Interface()
29{
30 rowlabels.resize(m);
31 columnlabels.resize(n);
32}
33
35{
38 lowlimit = mp.lowlimit;
41 CMatrix::operator=(mp);
42 return *this;
43}
44QJsonObject CMBMatrix::toJsonObject() const
45{
46 QJsonObject out;
47 QJsonArray matrix;
48 for (int i=0; i<getnumrows(); i++)
49 {
50 QJsonArray row;
51 for (int j=0; j<getnumcols(); j++)
52 row.append(valueAt(i,j));
53 matrix.append(row);
54 }
55 QJsonArray Row_Labels;
56 for (unsigned int i=0; i<rowlabels.size(); i++)
57 {
58 Row_Labels.append(QString::fromStdString(rowlabels[i]));
59 }
60 QJsonArray Column_Labels;
61 for (unsigned int i=0; i<columnlabels.size(); i++)
62 {
63 Column_Labels.append(QString::fromStdString(columnlabels[i]));
64 }
65 out["Matrix"] = matrix;
66 out["RowLabels"] = Row_Labels;
67 out["ColumnLabels"] = Column_Labels;
68 return out;
69}
70bool CMBMatrix::ReadFromJsonObject(const QJsonObject &jsonobject)
71{
72 QJsonArray array = jsonobject["Matrix"].toArray();
73 QJsonArray Row_Labels = jsonobject["RowLabels"].toArray();
74 QJsonArray Column_Labels = jsonobject["ColumnLabels"].toArray();
75
76 if (array.size()==0)
77 return false;
78 Resize(array.size(), array[0].toArray().size());
79 rowlabels.resize(Row_Labels.size());
80 columnlabels.resize(Column_Labels.size());
81
82 for (unsigned int i=0; i<array.size(); i++)
83 {
84 for (unsigned int j=0; j<array[i].toArray().size(); j++)
85 {
86 matr[i][j] = array[i].toArray()[j].toDouble();
87 }
88 }
89
90 for (unsigned int i=0; i<Row_Labels.size(); i++)
91 rowlabels[i] = Row_Labels[i].toString().toStdString();
92
93 for (unsigned int i=0; i<Column_Labels.size(); i++)
94 columnlabels[i] = Column_Labels[i].toString().toStdString();
95
96 return true;
97}
98
99bool CMBMatrix::writetofile(QFile* file)
100{
101 file->write(QString::fromStdString(ToString()).toUtf8());
102 return true;
103}
105{
106 string out;
107 for (int j=0; j<getnumcols(); j++)
108 {
109 out += "," + columnlabels[j];
110 }
111 out += "\n";
112 for (int i=0; i<getnumrows(); i++)
113 {
114 QJsonArray row;
115 out+= rowlabels[i];
116 for (int j=0; j<getnumcols(); j++)
117 {
118 out+=",";
119 out+=QString::number(valueAt(i,j)).toStdString();
120 }
121 out += "\n";
122 }
123 return out;
124}
125QTableWidget *CMBMatrix::ToTable()
126{
127 QTableWidget *tablewidget = new QTableWidget();
128 tablewidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
129 tablewidget->setColumnCount(getnumcols());
130 tablewidget->setRowCount(getnumrows());
131 QStringList rowheaders;
132 QStringList colheaders;
133
134 for (int j=0; j<getnumcols(); j++)
135 colheaders << QString::fromStdString(columnlabels[j]);
136
137 for (int i = 0; i < getnumrows(); i++)
138 rowheaders << QString::fromStdString(rowlabels[i]);
139
140 tablewidget->setHorizontalHeaderLabels(colheaders);
141 tablewidget->setVerticalHeaderLabels(rowheaders);
142
143 for (int i = 0; i < getnumrows(); i++)
144 {
145 for (int j = 0; j < getnumcols(); j++)
146 {
147 QTableWidgetItem* item = nullptr;
148
149 if (boolean_values)
150 {
151 item = new QTableWidgetItem(matr[i][j] == 1 ? "Fail" : "Pass");
152 if (matr[i][j] == 1) // If it's "Fail"
153 {
154 item->setBackground(QBrush(Qt::red));
155 }
156 }
157 else
158 {
159 item = new QTableWidgetItem(QString::number(matr[i][j]));
160
162 {
163 if (matr[i][j] > highlimit || matr[i][j] < lowlimit)
164 {
165 tablewidget->verticalHeaderItem(i)->setForeground(QBrush(Qt::red));
166 //item->setData(Qt::ForegroundRole, QColor(Qt::red));
167 item->setBackground(QBrush(Qt::red));
168 }
169 else
170 {
171 item->setForeground(QBrush(Qt::black));
172 }
173 }
174 }
175
176 tablewidget->setItem(i, j, item);
177 tablewidget->item(i, j)->setTextAlignment(Qt::AlignCenter);
178
179
180 }
181 }
182
183
184 return tablewidget;
185}
186
187double CMBMatrix::valueAt(int i, int j) const
188{
189 return CMatrix::operator[](i).at(j);
190}
191
193{
194 CMBVector out = M.toMatrix()*V.toVector();
195 out.SetLabels(M.RowLabels());
196 return out;
197
198}
199
200CMatrix CMBMatrix::toMatrix() const
201{
202 CMatrix out(getnumrows(), getnumcols());
203 out.matr = matr;
204 return out;
205}
206
207CMBVector CMBMatrix::GetRow(const string &rowlabel)
208{
209 CMBVector out(getnumcols());
210 for (unsigned int i=0; i<rowlabels.size(); i++)
211 {
212 if (rowlabels[i]==rowlabel)
213 {
214 for (unsigned int j=0; j<getnumcols(); j++)
215 out[j] = matr[i][j];
216 }
217 }
218 out.SetLabels(ColumnLabels());
219 return out;
220}
221CMBVector CMBMatrix::GetColumn(const string &columnlabel)
222{
223 CMBVector out(getnumrows());
224 for (unsigned int i=0; i<columnlabels.size(); i++)
225 {
226 if (columnlabels[i]==columnlabel)
227 {
228 for (unsigned int j=0; j<getnumrows(); j++)
229 out[j] = matr[j][i];
230 }
231 }
232 out.SetLabels(RowLabels());
233 return out;
234}
235
237{
238 QStringList out;
239 for (unsigned int i=0; i<rowlabels.size(); i++)
240 {
241 if (!out.contains(QString::fromStdString(rowlabels[i])))
242 out<<QString::fromStdString(rowlabels[i]);
243 }
244 return out;
245}
Matrix class with labeled rows and columns for Chemical Mass Balance analysis.
Definition cmbmatrix.h:22
CMBMatrix()
Default constructor - creates an empty matrix.
Definition cmbmatrix.cpp:9
vector< string > ColumnLabels() const
Gets all column labels.
Definition cmbmatrix.h:156
CMatrix toMatrix() const
Converts to base CMatrix type (without labels).
bool boolean_values
Display values as Pass/Fail instead of numbers.
Definition cmbmatrix.h:191
string ToString() const override
Converts matrix to CSV-formatted string.
bool ReadFromJsonObject(const QJsonObject &jsonobject) override
Deserializes matrix from JSON format.
Definition cmbmatrix.cpp:70
QJsonObject toJsonObject() const override
Serializes matrix to JSON format.
Definition cmbmatrix.cpp:44
QTableWidget * ToTable() override
Creates a QTableWidget for displaying matrix data.
QStringList RowLabelCategories()
Gets unique row label categories.
CMBMatrix & operator=(const CMBMatrix &mp)
Assignment operator.
Definition cmbmatrix.cpp:34
vector< string > columnlabels
Labels for each column.
Definition cmbmatrix.h:189
double valueAt(int i, int j) const
Gets value at specified matrix position.
CMBVector GetColumn(const string &columnlabel)
Extracts a column by its label.
CMBVector GetRow(const string &rowlabel)
Extracts a row by its label.
vector< string > RowLabels() const
Gets all row labels.
Definition cmbmatrix.h:150
bool writetofile(QFile *file) override
Writes matrix to file in CSV format.
Definition cmbmatrix.cpp:99
vector< string > rowlabels
Labels for each row.
Definition cmbmatrix.h:190
Vector class with string labels for Chemical Mass Balance analysis.
Definition cmbvector.h:20
CVector toVector() const
Converts to base CVector type (without labels).
void SetLabels(const vector< string > &label)
Sets all element labels at once.
Definition cmbvector.h:154
double lowlimit
Lower threshold for value highlighting (when enabled).
Definition interface.h:214
Interface()
Default constructor.
Definition interface.cpp:6
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
CMBVector operator*(const CMBMatrix &M, const CMBVector &V)
Matrix-vector multiplication operator.