Vespucci  1.0.0
analysisresults.cpp
Go to the documentation of this file.
2 #include <QSet>
8 AnalysisResults::AnalysisResults(QString name, QString type) :
9  name_(name),
10  type_(type)
11 {
12 
13 }
14 
15 
17 {
18 
19 }
20 
21 const mat & AnalysisResults::GetMatrix(const QString &key)
22 {
23  if (matrices_.contains(key)) return *matrices_[key];
24  else return EmptyMatrix();
25 }
26 
27 void AnalysisResults::AddMetadata(QString key, QString value)
28 {
29  metadata_[key] = value;
30 }
31 
32 void AnalysisResults::AddField(const QString &key, const field<mat> &value)
33 {
34  fields_[key] = QSharedPointer<field<mat> >(new field<mat>(value));
35 }
36 
37 const field<mat> &AnalysisResults::GetField(const QString &key)
38 {
39  return *fields_[key];
40 }
41 
42 const mat &AnalysisResults::GetFieldElement(const QString &key, const uword &index)
43 {
44  if (!HasField(key) || (HasField(key) && index >= fields_[key]->n_elem))
45  return empty_matrix_;
46  else return fields_[key]->at(index);
47 }
48 
49 bool AnalysisResults::HasField(const QString &key) const
50 {
51  return fields_.contains(key);
52 }
53 
54 bool AnalysisResults::HasMatrix(const QString &key) const
55 {
56  return matrices_.contains(key);
57 }
58 
59 void AnalysisResults::AddMatrix(const QString &key, const mat &value, QStringList column_headings)
60 {
61  matrices_[key] = QSharedPointer<mat>(new mat(value));
62  column_headings_[key] = column_headings;
63 }
64 
65 void AnalysisResults::AddColumns(const QStringList &keys, const mat &value)
66 {
67  //if more columns than provided column names, stop when we run out of
68  //names. If more names than columns, stop when we run out of columns
69  uword count = (uword(keys.size()) < value.n_cols ? keys.size() : value.n_cols);
70  for (uword i = 0; i < count; ++i)
71  matrices_[keys[i]] = QSharedPointer<mat>(new mat(value.col(i)));
72 }
73 
74 const QStringList AnalysisResults::KeyList() const
75 {
76  return matrices_.keys();
77 }
78 
79 const QString AnalysisResults::name() const
80 {
81  return name_;
82 }
83 
84 const QString AnalysisResults::type() const
85 {
86  return type_;
87 }
88 
90 {
91  return empty_matrix_;
92 }
93 
94 const QMap<QString, QString> AnalysisResults::GetMetadata() const
95 {
96  return metadata_;
97 }
98 
99 const QString AnalysisResults::GetColumnHeading(const QString &key, int column)
100 {
101  if (column_headings_.contains(key) && column_headings_[key].size() < column)
102  return column_headings_[key][column];
103  else return QString();
104 }
105 
106 const QString AnalysisResults::GetRowHeading(const QString &key, int row)
107 {
108  if (row_headings_.contains(key) && row_headings_[key].size() < row)
109  return row_headings_[key][row];
110  else return QString();
111 }
112 
113 void AnalysisResults::SetName(const QString &new_name)
114 {
115  name_ = new_name;
116 }
117 
118 void AnalysisResults::SetType(const QString &new_type)
119 {
120  type_ = new_type;
121 }
122 
123 QSharedPointer<AnalysisResults> AnalysisResults::Subset(QStringList matrices, uword start_row, uword end_row)
124 {
125  QSharedPointer<AnalysisResults> results(new AnalysisResults(name_ + " (subset)", type_ + " (subset)"));
126  for (auto matrix_key: matrices){
127  mat submatrix = matrices_.value(matrix_key)->rows(start_row, end_row);
128  results->AddMatrix(matrix_key, submatrix);
129  }
130  return results;
131 }
132 
133 void AnalysisResults::AddParent(QString key, uword start_row, uword end_row)
134 {
135  parent_rows_[key] = uvec({start_row, end_row});
136 }
137 
138 QMap<QString, uvec> AnalysisResults::parent_rows()
139 {
140  return parent_rows_;
141 }
142 
149 bool AnalysisResults::Concatenate(QSharedPointer<AnalysisResults> other)
150 {
151  QSet<QString> intersection = KeyList().toSet().intersect(other->KeyList().toSet());
152  //Check mergability (this should not be a problem if both come from same dataset)
153  //User may be allowed to merge from two different datasets, if for some reason they wanted to do that
154  QSet<QString> mergeable_matrices = intersection;
155  for (auto key: intersection)
156  if (GetMatrix(key).n_rows != other->GetMatrix(key).n_rows){
157  mergeable_matrices.remove(key);
158  RemoveMatrix(key);
159  }
160  if (mergeable_matrices.isEmpty()) return false;
161  for (auto key: mergeable_matrices){
162  mat matrix = GetMatrix(key);
163  mat other_matrix = other->GetMatrix(key);
164  matrix = join_horiz(matrix, other_matrix);
165  AddMatrix(key, matrix);
166  }
167  return true;
168 }
169 
175 QSharedPointer<AnalysisResults> AnalysisResults::Replicate()
176 {
177  QSharedPointer<AnalysisResults> new_result(new AnalysisResults(name_, type_));
178  for (auto key: KeyList()){
179  new_result->AddMatrix(key, GetMatrix(key));
180  }
181  return new_result;
182 }
183 
184 void AnalysisResults::RemoveMatrix(const QString &key)
185 {
186  matrices_.remove(key);
187 }
void SetType(const QString &new_type)
void AddMetadata(QString key, QString value)
QSharedPointer< AnalysisResults > Subset(QStringList matrices, uword start_row, uword end_row)
const mat & EmptyMatrix()
bool Concatenate(QSharedPointer< AnalysisResults > other)
AnalysisResults::Concatenate.
QMap< QString, uvec > parent_rows()
const mat & GetMatrix(const QString &key)
void RemoveMatrix(const QString &key)
const QString GetColumnHeading(const QString &key, int column)
void AddParent(QString key, uword start_row, uword end_row)
const field< mat > & GetField(const QString &key)
const QString GetRowHeading(const QString &key, int row)
const QString name() const
void AddField(const QString &key, const field< mat > &value)
bool HasField(const QString &key) const
AnalysisResults(QString name, QString type)
AnalysisResults::AnalysisResults.
QSharedPointer< AnalysisResults > Replicate()
AnalysisResults::Replicate.
void SetName(const QString &new_name)
const QStringList KeyList() const
void AddMatrix(const QString &key, const mat &value, QStringList column_headings=QStringList())
void AddColumns(const QStringList &keys, const mat &value)
const QMap< QString, QString > GetMetadata() const
const mat & GetFieldElement(const QString &key, const uword &index)
bool HasMatrix(const QString &key) const
const QString type() const
virtual ~AnalysisResults()