Vespucci  1.0.0
vespuccidataset.h
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 #ifndef VespucciDataset_H
21 #define VespucciDataset_H
22 #include "Math/VespucciMath.h"
24 #include <QTextStream>
25 #include <iostream>
26 #include <QVector>
27 #include <vector>
28 #include <qcustomplot.h>
30 #include "Data/Analysis/plsdata.h"
31 #include "Data/Analysis/vcadata.h"
33 #include "Data/Imaging/mapdata.h"
34 #include <cmath>
35 #include <fstream>
36 #include "GUI/mainwindow.h"
37 #include "Data/Analysis/plsdata.h"
38 #include "Data/Import/textimport.h"
40 #include "Data/Import/textimport.h"
46 
47 
48 
49 class MapData;
51 class MlpackPCAData;
52 class PLSData;
53 class VCAData;
54 class SpectrumViewer;
55 class MainWindow;
56 class VespucciWorkspace;
57 class MapListModel;
58 class UnivariateData;
59 class AnalysisResults;
60 class CWTData;
61 class TreeItem;
62 
63 
64 
65 using namespace std;
66 using namespace arma;
67 
68 
76 {
77 public:
78  VespucciDataset(const QString &h5_filename,
79  MainWindow *main_window,
80  QSharedPointer<VespucciWorkspace> ws);
81 
82  VespucciDataset(QString text_filename,
83  MainWindow *main_window,
84  QString *directory,
85  QString name,
86  QString x_axis_description,
87  QString y_axis_description,
88  bool swap_spatial,
89  std::string format);
90 
91  VespucciDataset(map<pair<int,int>, string> text_filenames,
92  MainWindow *main_window,
93  QString *directory,
94  QString name,
95  QString x_axis_description,
96  QString y_axis_description,
97  int rows, int cols);
98 
99  VespucciDataset(QString name,
100  MainWindow *main_window,
101  QString *directory,
102  QSharedPointer<VespucciDataset> original,
103  uvec indices);
104 
105  VespucciDataset(QString name,
106  MainWindow *main_window,
107  QString *directory);
108 
109  ~VespucciDataset();
110  // PRE-PROCESSING FUNCTIONS //
111  //general
112  void Undo();
113 
114  // map editing
115  void CropSpectra(double x_min, double x_max, double y_min, double y_max, double wl_min, double wl_max);
116 
117  // Normalization functions
118 
119  mat ZScoreNormCopy();
120  void MinMaxNormalize();
121  void VectorNormalize(uword norm);
122  void MeanCenter();
123  void ZScoreNormalize();
124  void SNVNormalize(double offset, bool center);
125  void AbsoluteValue();
126  void PeakIntensityNormalize(double peak_position);
127  void PeakIntensityNormalize(double left_bound, double right_bound);
128  void Booleanize(double min, double max, bool keep_inside, bool oneify);
129  void Clamp(double min, double max);
130  void ShedZeroSpectra();
131  void ShedZeroWavelengths();
132 
133  //Spectral Pre-processing
134 
135  void SubtractBackground(const QStringList &data_keys);
136  void MedianFilter(unsigned int window_size);
137  void LinearMovingAverage(unsigned int window_size);
138  void SavitzkyGolay(unsigned int derivative_order,
139  unsigned int polynomial_order,
140  unsigned int window_size);
141  void SingularValue(unsigned int singular_values);
142  int QUIC_SVD(double epsilon);
143  void MFBaseline(int window_size, int iterations);
144  void RollingBallBaseline(size_t wm, size_t ws);
145  void CWTBaseline(int lambda, int penalty_order, double SNR_threshold, double peak_shape_threshold);
146  void IModPolyBaseline(const uword poly_order, const uword max_it, double threshold);
147  void RemoveClippedSpectra(double threshold);
148  void RemoveFlatSpectra(double threshold);
149  void ZeroClippedSpectra(double threshold);
150  void ZeroFlatSpectra(double threshold);
151  void Scale(double scaling_factor);
152  void ShedSpectrum(const uword index);
153  void ZeroSpectrum(const uword index);
154  int HySime();
155  void TransformAbscissa(QString input_units, double input_factor,
156  QString output_units, double output_factor, QString description);
157  void InterpolateToNewAbscissa(const vec &new_abscissa,
158  unsigned int poly_order,
159  unsigned int window_size);
160  void InterpolateToNewAbscissa(const vec &new_abscissa);
161  void FourierTransform(int n);
162  void InverseFourierTransform(int n);
163  void ApplyFTWeight(QString type, double param);
164  void ApplyFTWeight(double start_offset, double end_offset,
165  double power);
166 
167 
168 
169  // HELPER FUNCTIONS //
170 
171  // Grabs one line of spectra_, at indices specified, recasts as QVector for QCustomPlot //
172  vec PointSpectrum(uword index) const;
173 
174  // Converts wavelength_ to QVector //
175  QVector<double> WavelengthQVector() const;
176 
177  // Finds the index of spectra_ with x and y values closest to the input //
178  int FindIndex(int x_value, int y_value) const;
179  uword FindIndex(double abscissa_value) const;
180 
181  // Returns vector containing start and end index within a range
182  uvec FindRange(double start, double end) const;
183 
184  uword FindOrigin() const;
185 
186  // These QCustomPlot sizes apply to all maps created from dataset //
187  int KeySize() const;
188  int ValueSize() const;
189  QCPRange KeyRange() const;
190  QCPRange ValueRange() const;
191 
192  bool Save(QString filename);
193  bool Load(QString filename);
194  bool SaveSpectrum(QString filename, uword column, file_type type);
195 
196  void Univariate(const QString &name, double &left_bound, double &right_bound,
197  uword bound_window);
198  void FitPeak(const QString &name,
199  const QString &peak_shape,
200  double &left_bound,
201  double &right_bound);
202  void BandRatio(const QString &name,
203  double &first_left_bound, double &first_right_bound,
204  double &second_left_bound, double &second_right_bound,
205  uword bound_window);
206  void ClassicalLeastSquares(const QString &name, const QStringList &reference_keys);
207  void PartialLeastSquares(const QString &name, uword components);
208  void PLSCalibration(const QString &name, const QStringList &control_keys);
209  void TrainPLSDA(const QString &name, const QStringList &label_keys);
210  void CorrelationAnalysis(const QString &control_key, QString name);
211  void VertexComponents(const QString &name, uword endmembers);
212  void KMeans(const QString &name, const QString &metric_text,
213  const QString &partition_policy, bool allow_empty, size_t clusters);
214  void PrincipalComponents(const QString &name);
215  void PrincipalComponents(const QString &name, bool scale_data);
216  void FindPeaks(const QString &name, double sel, double threshold,
217  uword poly_order, uword window_size);
218  void AgglomerativeClustering(const QString &name, const QString &linkage, const QString &metric);
219  void CalculateRepresentativeSpectrum(const QString &name, QString statistic, QString metric);
220 
221 
222  //MEMBER ACCESS FUNCTIONS:
223  vec wavelength() const;
224  vec abscissa() const;
225  vec x() const;
226  vec y() const;
227  vec x(uvec indices) const;
228  vec y(uvec indices) const;
229  double x(uword index) const;
230  double y(uword index) const;
231  vec wavelength(uvec indices) const;
232  mat spectra(uvec indices) const;
233  mat spectra() const;
234  cx_mat cx_spectra() const;
235  cx_mat cx_spectra(uvec indices) const;
236  const QString name() const;
237  vec indices() const;
238  mat *indices_ptr();
239  QStringList operations();
240 
241  bool principal_components_calculated() const;
242  bool mlpack_pca_calculated() const;
243  bool vertex_components_calculated() const;
244  bool partial_least_squares_calculated() const;
245 
246  QList<QSharedPointer<UnivariateData> > univariate_datas();
247  const QString x_axis_description() const;
248  const QString y_axis_description() const;
249 
250  void SetName(QString new_name);
251  void SetData(const mat &spectra, const vec &wavelength, const vec &x, const vec &y);
252 
253  void SetIndices(vec indices);
254 
255  void AddMap(QSharedPointer<MapData> map);
256  void RemoveMap(const QString &name);
257  int map_loading_count() const;
258 
259  void SetXDescription(QString description);
260  void SetYDescription(QString description);
261 
262  QCPRange WavelengthRange() const;
263  QCPRange PointSpectrumRange(int i) const;
264 
265  QCPColorGradient GetGradient(int gradient_number) const;
266  QCPColorGradient GetClusterGradient(int clusters) const;
267 
268  bool ConstructorCancelled() const;
269  mat AverageSpectrum(bool stats) const;
270 
271  mat *x_ptr();
272  mat *y_ptr();
273  mat *wavelength_ptr();
274  mat *abscissa_ptr();
275  mat *spectra_ptr();
276  mat *spectra_old_ptr(bool *ok);
277 
278  const mat &spectra_ref();
279  const vec &abscissa_ref();
280  const vec &x_ref();
281  const vec &y_ref();
282 
283  size_t columns() const;
284  double AbscissaMin() const;
285  double AbscissaMax() const;
286 
287  bool non_spatial() const;
288  bool meta() const;
289  void SetParentDatasetIndices(mat parent_dataset_indices);
290  mat *parent_dataset_indices();
291  bool Undoable() const;
292  uword UniqueX() const;
293  uword UniqueY() const;
294  int UnivariateCount() const;
295 
296  const QString last_operation() const;
297 
301  QTextStream log_text_stream_;
302 
303  MapListModel *map_list_model();
304 
305  void AddAnalysisResult(QSharedPointer<AnalysisResults> analysis_result);
306  void AddAnalysisResult(QSharedPointer<AnalysisResults> analysis_result, uword start_row, uword end_row);
307 
308  QStringList AnalysisResultsKeys() const;
309  QMap<QString, QStringList> AnalysisResultsTreeStructure() const;
310 
311  void ImportAuxiliaryMatrix(const QString &name, const QString &filename);
312  void AddAuxiliaryMatrix(const QString &name, mat &matrix);
313  void AddMatrix(const QString &name, mat &matrix);
314  QStringList AuxiliaryMatrixKeys() const;
315  QStringList CoreMatrixKeys() const;
316 
317 
318  const mat & GetAnalysisResultMatrix(const QString &results_key, const QString &matrix_key) const;
319  QSharedPointer<AnalysisResults> GetAnalysisResult(const QString &key);
320  const mat & GetAuxiliaryMatrix(const QString &key) const;
321  const mat &GetCoreMatrix(const QString &key) const;
322  bool IsCoreMatrix(const QString &key) const;
323  QSharedPointer<MapData> GetMapData(const QString &key);
324 
325  void CreateMap(const QString &map_name,
326  const QString &results_key,
327  const QString &matrix_key,
328  uword column,
329  QCPColorGradient gradient);
330  void CreateMap(const QString &map_name,
331  const QString &matrix_key,
332  uword column,
333  QCPColorGradient gradient);
334 
335  bool ShowMapViewer(const QString &map_key, bool show);
336 
337  QStringList MapKeys() const;
338 
339  void SetOldCopies();
340 
341  bool Contains(const QString &key) const;
342 
343  bool IsValid() const;
344 
345  bool state_changed() const;
346 
347  QString filename() const;
348  QString last_save_filename() const;
349 
350  bool saved() const;
351 
352  private:
353 
358  const mat empty_matrix_;
359 
363  QSharedPointer<AnalysisResults> auxiliary_matrices_;
364 
368  vec abscissa_;
369  vec abscissa_old_;
370 
371  QSharedPointer<VespucciWorkspace> workspace_;
372 
376  vec x_;
377  vec x_old_;
378 
382  vec y_;
383  vec y_old_;
384 
389  mat spectra_;
390 
391 
392 
398  mat spectra_old_;
399 
403  mat spectra_imag_;
404 
405 
410  mat baselines_;
411 
415  QString name_;
416 
420  QString last_operation_;
421 
425  QList<QSharedPointer<MapData> > maps_;
426 
431  mat parent_dataset_indices_;
432 
436  int map_loading_count_;
437 
441  QString x_axis_description_; //abscissa legend
442 
446  QString y_axis_description_; //ordinate legend
447 
451  QString *directory_;
452 
456  vec indices_;
457 
463  bool z_scores_calculated_;
464 
468  bool constructor_canceled_;
469 
473  bool flipped_;
474 
478  MainWindow *main_window_;
479 
480 
484  bool non_spatial_;
485 
489  bool meta_;
490 
496  QVector<QSharedPointer<AnalysisResults> > analysis_results_;
497 
502  QStringList operations_;
503 
507  QString last_save_filename_;
508 
512  bool state_changed_;
513 
517  QString filename_;
518 
522  map<pair<int, int>, string> text_filenames_;
523 
524 
525  bool saved_;
526 
527 };
528 Q_DECLARE_METATYPE(QSharedPointer<VespucciDataset> *)
529 
530 #endif // VespucciDataset_H
The PLSData class A class for performing and storing data related to partial least squares determinan...
Definition: plsdata.h:29
QTextStream log_text_stream_
log_text_stream_ A log of all UI-induced functions called on this dataset
The PrincipalComponentsData class A class for performing and storing data from principal components a...
The MapData class Class for processed map data. Images are created from this data.
Definition: mapdata.h:44
VESPUCCI_EXPORT arma::uword max(arma::uword a, arma::uword b)
Vespucci::Math::max.
Definition: accessory.cpp:237
VESPUCCI_EXPORT arma::vec MedianFilter(const arma::vec &X, arma::uword window_size)
Vespucci::Math::Smoothing::MedianFilter.
Definition: nonlinear.cpp:30
The VespucciDataset class This is the main class for dealing with hyperspectral data. This handles the import and export of spectra, and the creation of maps. Images are handled by the MapData class. This class is intended to be allocated on the heap inside of a smart pointer, there is no copy constructor.
The AbstractDataAnalyzer class Pure abstract class describing an object that can be passed as a point...
VESPUCCI_EXPORT size_t HySime(const arma::mat &y, const arma::mat &n, arma::mat &Rn, arma::mat &Ek)
Vespucci::Math::DimensionReduction::HySime.
Definition: VCA.cpp:159
VESPUCCI_EXPORT arma::vec RollingBallBaseline(const arma::vec &spectrum, arma::vec &baseline, arma::uword wm, arma::uword ws)
Vespucci::Math::Baseline::RollingBallBaseline.
Definition: rollingball.cpp:33
The VespucciWorkspace class A class which contains all "global variables" (that aren&#39;t held in MainWi...
VESPUCCI_EXPORT arma::mat InterpolateToNewAbscissa(const arma::mat &spectra, const arma::vec &old_abscissa, const arma::vec &new_abscissa, const int window_size, const int order)
InterpolateToNewAbscissa.
Definition: FIR.cpp:177
VESPUCCI_EXPORT arma::vec FindPeaks(arma::vec X, arma::vec dX, double sel, double threshold, arma::vec &peak_magnitudes)
Vespucci::Math::PeakFinding::FindPeaks an implementation of the peakfinder arma::mat routine...
Definition: peakfinding.cpp:44
VESPUCCI_EXPORT arma::uword min(arma::uword a, arma::uword b)
Vespucci::Math::min.
Definition: accessory.cpp:249
The VCAData class A class for performing and storing data from Vertex Components Analysis.
Definition: vcadata.h:28
Defines a color gradient for use with e.g. QCPColorMap.
Definition: qcustomplot.h:1905
Represents the range an axis is encompassing.
Definition: qcustomplot.h:481
The MainWindow class The main window of the program, this is where the user performs most operations...
Definition: mainwindow.h:58
The AnalysisResults class A container for a mat object that allows a mat to be copied to a heap-alloc...
The MapListModel class Exposes the UI to the contents of the master map list.
Definition: maplistmodel.h:33