OpenMS  2.7.0
SimpleSVM.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // OpenMS -- Open-Source Mass Spectrometry
3 // --------------------------------------------------------------------------
4 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5 // ETH Zurich, and Freie Universitaet Berlin 2002-2021.
6 //
7 // This software is released under a three-clause BSD license:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of any author or any participating institution
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // For a full list of authors, refer to the file AUTHORS.
17 // --------------------------------------------------------------------------
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // --------------------------------------------------------------------------
31 // $Maintainer: Hendrik Weisser $
32 // $Authors: Hendrik Weisser $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
38 
39 #include <svm.h>
40 
41 #include <map>
42 #include <vector>
43 #include <utility> // for "pair"
44 
45 namespace OpenMS
46 {
65  class OPENMS_DLLAPI SimpleSVM :
66  public DefaultParamHandler
67  {
68 
69  public:
71  typedef std::map<String, std::vector<double> > PredictorMap;
72 
74  typedef std::map<String, std::pair<double, double> > ScaleMap;
75 
77  struct Prediction
78  {
81 
83  std::map<Int, double> probabilities;
84  };
85 
88 
90  ~SimpleSVM() override;
91 
102  void setup(PredictorMap& predictors, const std::map<Size, Int>& labels);
103 
113  void predict(std::vector<Prediction>& predictions,
114  std::vector<Size> indexes = std::vector<Size>()) const;
115 
124  void getFeatureWeights(std::map<String, double>& feature_weights) const;
125 
127  void writeXvalResults(const String& path) const;
128 
130  const ScaleMap& getScaling() const;
131  protected:
133  typedef std::vector<std::vector<double> > SVMPerformance;
134 
136  std::vector<std::vector<struct svm_node> > nodes_;
137 
139  struct svm_problem data_;
140 
142  struct svm_parameter svm_params_;
143 
145  struct svm_model* model_;
146 
148  std::vector<String> predictor_names_;
149 
152 
154  std::vector<double> log2_C_, log2_gamma_;
155 
158 
161 
163  static void printNull_(const char*) {}
164 
166  void scaleData_(PredictorMap& predictors);
167 
169  void convertData_(const PredictorMap& predictors);
170 
172  std::pair<double, double> chooseBestParameters_() const;
173 
176  };
177 }
178 
A base class for all classes handling default parameters.
Definition: DefaultParamHandler.h:93
Simple interface to support vector machines for classification (via LIBSVM).
Definition: SimpleSVM.h:67
void convertData_(const PredictorMap &predictors)
Convert predictors to LIBSVM format.
~SimpleSVM() override
Destructor.
static void printNull_(const char *)
Dummy function to suppress LIBSVM output.
Definition: SimpleSVM.h:163
Int label
Predicted class label.
Definition: SimpleSVM.h:80
std::vector< std::vector< double > > SVMPerformance
Classification performance for different param. combinations (C/gamma):
Definition: SimpleSVM.h:133
Size n_parts_
Number of partitions for cross-validation.
Definition: SimpleSVM.h:151
struct svm_model * model_
Pointer to SVM model (LIBSVM format)
Definition: SimpleSVM.h:145
void setup(PredictorMap &predictors, const std::map< Size, Int > &labels)
Load data and train a model.
void predict(std::vector< Prediction > &predictions, std::vector< Size > indexes=std::vector< Size >()) const
Predict class labels (and probabilities).
void scaleData_(PredictorMap &predictors)
Scale predictor values to range 0-1.
void writeXvalResults(const String &path) const
Write cross-validation (parameter optimization) results to a CSV file.
std::map< String, std::vector< double > > PredictorMap
Mapping from predictor name to vector of predictor values.
Definition: SimpleSVM.h:71
std::map< Int, double > probabilities
Predicted probabilities for different classes.
Definition: SimpleSVM.h:83
const ScaleMap & getScaling() const
Get data range of predictors before scaling to [0, 1].
SVMPerformance performance_
Cross-validation results.
Definition: SimpleSVM.h:160
void optimizeParameters_()
Run cross-validation to optimize SVM parameters.
std::vector< double > log2_C_
Parameter values to try during optimization.
Definition: SimpleSVM.h:154
std::pair< double, double > chooseBestParameters_() const
Choose best SVM parameters based on cross-validation results.
SimpleSVM()
Default constructor.
std::map< String, std::pair< double, double > > ScaleMap
Mapping from predictor name to predictor min and max.
Definition: SimpleSVM.h:74
std::vector< String > predictor_names_
Names of predictors in the model (excluding uninformative ones)
Definition: SimpleSVM.h:148
ScaleMap scaling_
Mapping from predictor name to predictor min and max.
Definition: SimpleSVM.h:157
void getFeatureWeights(std::map< String, double > &feature_weights) const
Get the weights used for features (predictors) in the SVM model.
SVM prediction result.
Definition: SimpleSVM.h:78
A more convenient string class.
Definition: String.h:61
int Int
Signed integer type.
Definition: Types.h:102
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47