Vespucci  1.0.0
datamodel.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  Copyright (C) 2014-2016 Wright State University - All Rights Reserved
3  Daniel P. Foose - Maintainer/Lead Developer
4 
5  This file is part of Vespucci.
6 
7  Vespucci is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  Vespucci is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with Vespucci. If not, see <http://www.gnu.org/licenses/>.
19 *******************************************************************************/
20 #include "datamodel.h"
21 
23 {
24 
25 }
26 
32 QSharedPointer<VespucciDataset> DataModel::GetDataset(const QString &key) const
33 {
34  for (int i = 0; i < datasets_.size(); ++i)
35  if (key == datasets_.at(i)->name()) return datasets_.at(i);
36  throw invalid_argument("Dataset "
37  + key.toStdString()
38  + " does not exist!");
39 
40 }
41 
49 QSharedPointer<AnalysisResults> DataModel::GetResults(const QString &dataset_key,
50  const QString &results_key) const
51 {
52  if (HasDataset(dataset_key)){
53  if (GetDataset(dataset_key)->AnalysisResultsKeys().contains(results_key))
54  return GetDataset(dataset_key)->GetAnalysisResult(results_key);
55  else
56  throw invalid_argument("Dataset "
57  + dataset_key.toStdString()
58  + " does not have analysis result "
59  + results_key.toStdString());
60  }
61  else{
62  throw invalid_argument("Dataset "
63  + dataset_key.toStdString()
64  + " does not exist!");
65  }
66 }
67 
68 QSharedPointer<AnalysisResults> DataModel::GetResults(const QStringList &keys) const
69 {
70  if (keys.size() != 2)
71  throw invalid_argument("Wrong number of keys provided");
72  return GetResults(keys[0], keys[1]);
73 }
74 
81 QSharedPointer<MapData> DataModel::GetMap(const QString &dataset_key,
82  const QString &map_key) const
83 {
84  if (HasDataset(dataset_key)){
85  if (GetDataset(dataset_key)->MapKeys().contains(map_key))
86  return GetDataset(dataset_key)->GetMapData(map_key);
87  else
88  throw invalid_argument("Dataset "
89  + dataset_key.toStdString()
90  + " does not have map "
91  + map_key.toStdString());
92  }
93  else{
94  throw invalid_argument("Dataset "
95  + dataset_key.toStdString()
96  + " does not exist!");
97  }
98 }
99 
100 QSharedPointer<MapData> DataModel::GetMap(const QStringList &keys) const
101 {
102  if (keys.size() != 2) throw invalid_argument("Wrong number of keys provided");
103  return GetMap(keys[0], keys[1]);
104 }
105 
106 QList<QSharedPointer<MapData> > DataModel::GetMapsUsingColorRange(const QString &range_key)
107 {
108  QList<QSharedPointer<MapData> > maps_using_range;
109  for (auto dataset: datasets_){
110  for (auto key: dataset->MapKeys()){
111  QSharedPointer<MapData> map = dataset->GetMapData(key);
112  if (map->global_gradient_key() == range_key)
113  maps_using_range << map;
114  }
115  }
116  return maps_using_range;
117 }
118 
119 const mat & DataModel::GetResultsMatrix(const QString &dataset_key,
120  const QString &results_key,
121  const QString &matrix_key) const
122 {
123  if (HasDataset(dataset_key)){
124  if (GetDataset(dataset_key)->AnalysisResultsKeys().contains(results_key)){
125  if (GetDataset(dataset_key)->GetAnalysisResult(results_key)->KeyList().contains(matrix_key))
126  return GetDataset(dataset_key)->GetAnalysisResultMatrix(results_key, matrix_key);
127  else
128  throw invalid_argument("Analysis Result"
129  + results_key.toStdString()
130  + " does not have matrix"
131  + matrix_key.toStdString());
132 
133  }
134  else{
135  throw invalid_argument("Dataset "
136  + dataset_key.toStdString()
137  + " does not have result "
138  + results_key.toStdString());
139  }
140  }
141  else{
142  throw invalid_argument("Dataset "
143  + dataset_key.toStdString()
144  + " does not exist!");
145  }
146 }
147 
148 const mat & DataModel::GetResultsMatrix(const QStringList &keys) const
149 {
150  if (keys.size() != 3) throw invalid_argument("Wrong number of keys provided");
151  return GetResultsMatrix(keys[0], keys[1], keys[2]);
152 }
153 
160 const mat & DataModel::GetCoreMatrix(const QString &dataset_key, const QString &matrix_key) const
161 {
162  if (HasDataset(dataset_key)){
163  if (matrix_key == "Spectra" || matrix_key == "Spectral Abscissa"
164  || matrix_key == "x" || matrix_key == "y")
165  return GetDataset(dataset_key)->GetCoreMatrix(matrix_key);
166  else
167  throw invalid_argument("Dataset "
168  + dataset_key.toStdString()
169  + " does not have core matrix "
170  + matrix_key.toStdString());
171  }
172  else{
173  throw invalid_argument("Dataset "
174  + dataset_key.toStdString()
175  + " does not exist!");
176  }
177 }
178 
179 const mat & DataModel::GetCoreMatrix(const QStringList &keys) const
180 {
181  if (keys.size() != 2) throw invalid_argument("Wrong number of keys provided");
182  return GetCoreMatrix(keys[0], keys[1]);
183 }
184 
191 const mat & DataModel::GetAuxiliaryMatrix(const QString &dataset_key,
192  const QString &matrix_key) const
193 {
194  if (HasDataset(dataset_key)){
195  if (GetDataset(dataset_key)->AuxiliaryMatrixKeys().contains(matrix_key))
196  return GetDataset(dataset_key)->GetAuxiliaryMatrix(matrix_key);
197  else
198  throw invalid_argument("Dataset "
199  + dataset_key.toStdString()
200  + " does not have auxiliary matrix "
201  + matrix_key.toStdString());
202  }
203  else{
204  throw invalid_argument("Dataset " + dataset_key.toStdString()
205  + " does not exist!");
206  }
207 }
208 
209 const mat & DataModel::GetAuxiliaryMatrix(const QStringList &keys) const
210 {
211  if (keys.size() != 2) throw invalid_argument("Wrong number of keys provided");
212  return GetAuxiliaryMatrix(keys[0], keys[1]);
213 }
214 
215 const mat & DataModel::GetMatrix(const QString &dataset_key, const QString &matrix_key) const
216 {
217  if (HasDataset(dataset_key)){
218  if (GetDataset(dataset_key)->CoreMatrixKeys().contains(matrix_key))
219  return GetDataset(dataset_key)->GetCoreMatrix(matrix_key);
220  else if (GetDataset(dataset_key)->AuxiliaryMatrixKeys().contains(matrix_key))
221  return GetDataset(dataset_key)->GetAuxiliaryMatrix(matrix_key);
222  else
223  throw invalid_argument("Dataset "
224  + dataset_key.toStdString()
225  + " does not have matrix "
226  + matrix_key.toStdString());
227  }
228  else{
229  throw invalid_argument("Dataset "
230  + dataset_key.toStdString()
231  + "does not exist!");
232  }
233 }
234 
235 const mat & DataModel::GetMatrix(const QStringList &keys) const
236 {
237  if (keys.size() == 2) return GetMatrix(keys[0], keys[1]);
238  if (keys.size() == 3) return GetResultsMatrix(keys);
239  throw invalid_argument("too few keys provided");
240 }
241 
242 bool DataModel::Mappable(const QStringList &keys) const
243 {
244  bool mappable;
245  try{
246  uword spatial_rows = GetDataset(keys[0])->x_ptr()->n_rows;
247  uword data_rows = GetMatrix(keys).n_rows;
248  mappable = (data_rows == spatial_rows);
249  }catch(exception e){
250  return false;
251  }
252  return mappable;
253 }
254 
255 bool DataModel::Plottable(const QStringList &keys) const
256 {
257  uword abscissa_rows = GetDataset(keys[0])->abscissa_ptr()->n_rows;
258  uword data_rows = GetMatrix(keys).n_rows;
259  return (data_rows == abscissa_rows);
260 }
261 
266 QStringList DataModel::DatasetNames() const
267 {
268  QStringList dataset_names;
269  for (auto dataset: datasets_)
270  dataset_names << dataset->name();
271  return dataset_names;
272 }
273 
279 QStringList DataModel::AnalysisResultsNames(const QString &dataset_key) const
280 {
281  if (HasDataset(dataset_key))
282  return GetDataset(dataset_key)->AnalysisResultsKeys();
283  else
284  throw invalid_argument("Dataset "
285  + dataset_key.toStdString()
286  + "does not exist!");
287 }
288 
294 QStringList DataModel::AuxiliaryMatrixNames(const QString &dataset_key) const
295 {
296  if (HasDataset(dataset_key))
297  return GetDataset(dataset_key)->AuxiliaryMatrixKeys();
298  else
299  throw invalid_argument("Dataset "
300  + dataset_key.toStdString()
301  + "does not exist!");
302 }
303 
304 QStringList DataModel::CoreMatrixNames(const QString &dataset_key)
305 {
306  if (HasDataset(dataset_key))
307  return GetDataset(dataset_key)->CoreMatrixKeys();
308  else
309  throw invalid_argument("Dataset "
310  + dataset_key.toStdString()
311  + "does not exist!");
312 }
313 
320 void DataModel::AddDataset(QSharedPointer<VespucciDataset> dataset)
321 {
322  int i = 1;
323  QString name = dataset->name();
324  while (HasDataset(dataset->name()))
325  dataset->SetName(name + "(" + QString::number(i++) + ")");
326  datasets_.append(dataset);
327 }
328 
333 void DataModel::RemoveDataset(const QString &name)
334 {
335  for (int i = 0; i < datasets_.size(); ++i)
336  if (datasets_.at(i)->name() == name) datasets_.removeAt(i);
337 }
338 
339 const mat & DataModel::EmptyMatrix() const
340 {
341  return empty_matrix_;
342 }
343 
344 bool DataModel::HasDataset(const QString &key) const
345 {
346  for (int i = 0; i < datasets_.size(); ++i)
347  if (datasets_.at(i)->name() == key) return true;
348  return false;
349 }
350 
351 bool DataModel::HasMatrix(const QStringList &keys) const
352 {
353  if (!HasDataset(keys.first())) return false;
354 
355  QSharedPointer<VespucciDataset> dataset = GetDataset(keys.first());
356 
357  if (keys.size() == 2)
358  return dataset->AuxiliaryMatrixKeys().contains(keys.last());
359 
360  if (keys.size() == 3 && dataset->AnalysisResultsKeys().contains(keys[1]))
361  return dataset->GetAnalysisResult(keys[1])->HasMatrix(keys[2]);
362 
363  return false;
364 }
365 
const mat & EmptyMatrix() const
Definition: datamodel.cpp:339
QStringList CoreMatrixNames(const QString &dataset_key)
Definition: datamodel.cpp:304
QSharedPointer< MapData > GetMap(const QString &dataset_key, const QString &map_key) const
DataModel::GetMap Get the MapData object named map_key from dataset named dataset_key.
Definition: datamodel.cpp:81
QStringList DatasetNames() const
DataModel::DatasetNames.
Definition: datamodel.cpp:266
const mat & GetAuxiliaryMatrix(const QString &dataset_key, const QString &matrix_key) const
DataModel::GetAuxiliaryMatrix.
Definition: datamodel.cpp:191
bool Plottable(const QStringList &keys) const
Definition: datamodel.cpp:255
void AddDataset(QSharedPointer< VespucciDataset > dataset)
DataModel::AddDataset.
Definition: datamodel.cpp:320
QSharedPointer< AnalysisResults > GetResults(const QString &dataset_key, const QString &results_key) const
DataModel::GetResults Get the AnalysisResults object named results_key from the dataset named dataset...
Definition: datamodel.cpp:49
QSharedPointer< VespucciDataset > GetDataset(const QString &key) const
DataModel::GetDataset Get the VespucciDataset named key.
Definition: datamodel.cpp:32
bool HasMatrix(const QStringList &keys) const
Definition: datamodel.cpp:351
const mat & GetCoreMatrix(const QString &dataset_key, const QString &matrix_key) const
DataModel::GetCoreMatrix.
Definition: datamodel.cpp:160
QStringList AuxiliaryMatrixNames(const QString &dataset_key) const
DataModel::AuxiliaryMatrixNames.
Definition: datamodel.cpp:294
void RemoveDataset(const QString &name)
DataModel::RemoveDataset.
Definition: datamodel.cpp:333
bool Mappable(const QStringList &keys) const
Definition: datamodel.cpp:242
bool HasDataset(const QString &key) const
Definition: datamodel.cpp:344
QStringList AnalysisResultsNames(const QString &dataset_key) const
DataModel::AnalysisResultsNames.
Definition: datamodel.cpp:279
const mat & GetMatrix(const QString &dataset_key, const QString &matrix_key) const
Definition: datamodel.cpp:215
QList< QSharedPointer< MapData > > GetMapsUsingColorRange(const QString &range_key)
Definition: datamodel.cpp:106
const mat & GetResultsMatrix(const QString &dataset_key, const QString &results_key, const QString &matrix_key) const
Definition: datamodel.cpp:119