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