Vespucci  1.0.0
metaanalyzer.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 *******************************************************************************/
21 #include "Data/Analysis/vcadata.h"
24 #include "Data/Analysis/plsdata.h"
25 #include <mlpack/methods/kmeans/kmeans.hpp>
29 MetaAnalyzer::MetaAnalyzer(QSharedPointer<VespucciWorkspace> ws)
30 {
31  workspace_ = ws;
32 }
33 
35 {
36 
37 }
38 
39 void MetaAnalyzer::Univariate(const QString &name, double &left_bound, double &right_bound, arma::uword bound_window)
40 {
41  QString new_name = FindUniqueName(name);
42  QSharedPointer<UnivariateData> univariate_data(new UnivariateData(new_name));
43  univariate_data->Apply(left_bound, right_bound, bound_window, data_, abscissa_);
44  QStringList matrix_keys({"Peak Centers",
45  "Peak Intensities",
46  "Adjusted Peak Intensities",
47  "Total Area",
48  "Adjusted Area",
49  "Area Between Inflection Points",
50  "Adjusted Area Between Inflection Points",
51  "Empirical Full-Width at Half-Maximum"});
52  AddAnalysisResults(univariate_data, matrix_keys);
53 }
54 
55 void MetaAnalyzer::FitPeak(const QString &name, const QString &peak_shape, double &left_bound, double &right_bound)
56 {
57  QSharedPointer<UnivariateData> univariate_data(new UnivariateData(name));
58  univariate_data->Apply(peak_shape, left_bound, right_bound, data_, abscissa_);
59  QStringList matrix_keys;
60  if (peak_shape == "Voigt"){
61  matrix_keys = QStringList({"Intensity",
62  "Area",
63  "Gaussian Full-Width at Half Maximum",
64  "Lorentzian Full-Width at Half Maximum",
65  "Full-Width at Half Maximum",
66  "Peak Centers",
67  "R²"});
68  }
69  else{
70  matrix_keys = QStringList({"Intensity",
71  "Area",
72  "Full-Width at Half Maximum",
73  "Peak Centers",
74  "R²"});
75  }
76  AddAnalysisResults(univariate_data, matrix_keys);
77 }
78 
79 void MetaAnalyzer::BandRatio(const QString &name,
80  double &first_left_bound,
81  double &first_right_bound,
82  double &second_left_bound,
83  double &second_right_bound,
84  arma::uword bound_window)
85 {
86  QString new_name = FindUniqueName(name);
87  QSharedPointer<UnivariateData> univariate_data(new UnivariateData(new_name));
88  univariate_data->Apply(first_left_bound, first_right_bound,
89  second_left_bound, second_right_bound,
90  bound_window, data_, abscissa_);
91  QStringList matrix_keys({"Band Ratios"});
92  AddAnalysisResults(univariate_data, matrix_keys);
93 }
94 
95 void MetaAnalyzer::ClassicalLeastSquares(const QString &name, const QStringList &reference_keys)
96 {
97  QString new_name = FindUniqueName(name);
98  QSharedPointer<AnalysisResults> cls_results(new AnalysisResults(new_name, "CLS Analysis"));
99  mat reference = workspace_->GetMatrix(reference_keys);
100  mat coefs = Vespucci::Math::LinLeastSq::OrdinaryLeastSquares(reference, data_).t();
101  cls_results->AddMatrix("Coefficients", coefs.t());
102  AddAnalysisResults(cls_results, QStringList({"Coefficients"}));
103 }
104 
105 void MetaAnalyzer::VertexComponents(const QString &name, uword endmembers)
106 {
107  QString new_name = FindUniqueName(name);
108  QSharedPointer<VCAData> vca_data(new VCAData(new_name));
109  vca_data->Apply(data_, endmembers);
110  QStringList matrix_keys({"Fractional Abundances"});
111  AddAnalysisResults(vca_data, matrix_keys);
112 }
113 
114 void MetaAnalyzer::KMeans(const QString &name,
115  const QString &metric_text, const QString &partition_policy, bool allow_empty,
116  size_t clusters)
117 {
118  QString new_name = FindUniqueName(name);
119  mat centroids;
120  Vespucci::Math::KMeansWrapper k(partition_policy.toStdString(), metric_text.toStdString(), allow_empty);
121  vec assignments = k.Cluster(data_, clusters, centroids);
122 
123  QSharedPointer<AnalysisResults> km_results(new AnalysisResults(new_name, "k-Means Analysis"));
124  km_results->AddMatrix("Assignments", assignments);
125  km_results->AddMatrix("Centroids", centroids);
126  QStringList matrix_keys({"Assignments"});
127 
128  AddAnalysisResults(km_results, matrix_keys);
129 }
130 
131 void MetaAnalyzer::PrincipalComponents(const QString &name)
132 {
133  QString new_name = FindUniqueName(name);
134  QSharedPointer<PrincipalComponentsData> pca_data(new PrincipalComponentsData(new_name));
135  pca_data->Apply(data_);
136  QStringList matrix_keys({"Scores", "Hotelling t²"});
137  AddAnalysisResults(pca_data, matrix_keys);
138 }
139 
140 void MetaAnalyzer::PrincipalComponents(const QString &name, bool scale_data)
141 {
142  QString new_name = FindUniqueName(name);
143  QSharedPointer<MlpackPCAData> pca_data(new MlpackPCAData(new_name));
144  pca_data->Apply(data_, scale_data);
145  AddAnalysisResults(pca_data, QStringList());
146 }
147 
148 void MetaAnalyzer::PartialLeastSquares(const QString &name, uword components)
149 {
150  QString new_name = FindUniqueName(name);
151  QSharedPointer<PLSData> pls_data(new PLSData(new_name));
152  pls_data->Classify(data_, abscissa_, components);
153  QStringList matrices({"Predictor Scores", "Response Scores"});
154  AddAnalysisResults(pls_data, matrices);
155 }
156 
157 void MetaAnalyzer::PLSCalibration(const QString &name, const QStringList &control_keys)
158 {
159  QString new_name = FindUniqueName(name);
160  QSharedPointer<PLSData> pls_data(new PLSData(new_name));
161  mat controls = workspace_->GetMatrix(control_keys);
162  pls_data->Calibrate(data_, controls);
163  QStringList matrices({"Predictor Scores", "Response Scores"});
164  AddAnalysisResults(pls_data, matrices);
165 }
166 
167 void MetaAnalyzer::TrainPLSDA(const QString &name, const QStringList &label_keys)
168 {
169  QString new_name = FindUniqueName(name);
170  QSharedPointer<PLSData> pls_data(new PLSData(new_name));
171  mat labels = workspace_->GetMatrix(label_keys);
172  pls_data->Discriminate(data_, labels);
173  QStringList matrices({"Predictor Scores", "Response Scores"});
174  AddAnalysisResults(pls_data, matrices);
175 }
176 
177 void MetaAnalyzer::AgglomerativeClustering(const QString &name, const QString &metric, const QString &linkage)
178 {
179  QSharedPointer<AnalysisResults> ahca_results(new AnalysisResults(name, "AHCA"));
180  mat assignments;
182  ahca.SetLinkage(linkage.toStdString());
183  ahca.SetMetric(metric.toStdString());
184  ahca.Link(data_);
185  assignments = ahca.Cluster(data_.n_cols);
186 
187  ahca_results->AddMatrix("Assignments", assignments);
188  ahca_results->AddMatrix("Spectrum Distances", ahca.dist());
189  ahca_results->AddMatrix("Cluster Distances", ahca.merge_data());
190  QStringList matrix_keys = QStringList({"Assignments"});
191  AddAnalysisResults(ahca_results, matrix_keys);
192 }
193 
194 size_t MetaAnalyzer::columns() const
195 {
196  return data_.n_cols;
197 }
198 
200 {
201  return abscissa_.min();
202 }
203 
205 {
206  return abscissa_.max();
207 }
208 
209 arma::vec MetaAnalyzer::abscissa() const
210 {
211  return abscissa_;
212 }
213 
214 arma::vec MetaAnalyzer::PointSpectrum(arma::uword index) const
215 {
216  return data_.col((index < data_.n_cols ? index : data_.n_cols - 1));
217 }
The PLSData class A class for performing and storing data related to partial least squares determinan...
Definition: plsdata.h:29
void Univariate(const QString &name, double &left_bound, double &right_bound, arma::uword bound_window)
arma::vec Cluster(const arma::mat &data, const size_t clusters, arma::mat &centroids)
The PrincipalComponentsData class A class for performing and storing data from principal components a...
void PartialLeastSquares(const QString &name, uword components)
QSharedPointer< VespucciWorkspace > workspace_
workspace_ The global workspace, used to obtain access to matrices
Definition: metaanalyzer.h:100
arma::vec PointSpectrum(arma::uword index) const
void Link(const arma::mat &data)
Vespucci::Math::Clustering::AHCA::Link.
void SetLinkage(std::string linkage_method)
void VertexComponents(const QString &name, uword endmembers)
MetaAnalyzer(QSharedPointer< VespucciWorkspace > ws)
The KMeansWrapper class mlpack relies heavily on template metaprogramming. This means that you only n...
Definition: kmeanswrapper.h:33
void AgglomerativeClustering(const QString &name, const QString &metric, const QString &linkage)
void SetMetric(std::string metric_type)
void KMeans(const QString &name, const QString &metric_text, const QString &partition_policy, bool allow_empty, size_t clusters)
double AbscissaMax() const
void FitPeak(const QString &name, const QString &peak_shape, double &left_bound, double &right_bound)
double AbscissaMin() const
void PLSCalibration(const QString &name, const QStringList &control_keys)
arma::vec abscissa() const
arma::mat Cluster(arma::uword k)
Vespucci::Math::Clustering::AHCA::Cluster Generate cluster assignments from tree. ...
void BandRatio(const QString &name, double &first_left_bound, double &first_right_bound, double &second_left_bound, double &second_right_bound, arma::uword bound_window)
The VCAData class A class for performing and storing data from Vertex Components Analysis.
Definition: vcadata.h:28
void TrainPLSDA(const QString &name, const QStringList &label_keys)
void PrincipalComponents(const QString &name)
The AnalysisResults class A container for a mat object that allows a mat to be copied to a heap-alloc...
void ClassicalLeastSquares(const QString &name, const QStringList &reference_keys)
VESPUCCI_EXPORT arma::mat OrdinaryLeastSquares(const arma::mat &X, const arma::mat &y)
Vespucci::Math::LinLeastSq::OrdinaryLeastSquares Perform Squares.
Definition: linleastsq.cpp:149
The AHCA class Handles agglomerative hierarchical clustering of data This class holds a tree which re...
size_t columns() const