OpenMS
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-2023.
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 #include <tuple>
45 
46 namespace OpenMS
47 {
75  class OPENMS_DLLAPI SimpleSVM :
76  public DefaultParamHandler
77  {
78 
79  public:
81  typedef std::map<String, std::vector<double> > PredictorMap;
82 
84  typedef std::map<String, std::pair<double, double> > ScaleMap;
85 
87  struct Prediction
88  {
90  double outcome;
91 
93  std::map<double, double> probabilities;
94  };
95 
98 
100  ~SimpleSVM() override;
101 
113  void setup(PredictorMap& predictors, const std::map<Size, double>& outcomes, bool classification = true);
114 
124  void predict(std::vector<Prediction>& predictions,
125  std::vector<Size> indexes = std::vector<Size>()) const;
126 
136  void predict(PredictorMap& predictors, std::vector<Prediction>& predictions) const;
137 
146  void getFeatureWeights(std::map<String, double>& feature_weights) const;
147 
149  void writeXvalResults(const String& path) const;
150 
152  const ScaleMap& getScaling() const;
153 
154  protected:
155  void clear_();
156 
158  typedef std::vector<std::vector<std::vector<double>>> SVMPerformance;
159 
161  std::vector<std::vector<struct svm_node> > nodes_;
162 
164  struct svm_problem data_;
165 
167  struct svm_parameter svm_params_;
168 
170  struct svm_model* model_;
171 
173  std::vector<String> predictor_names_;
174 
177 
179  std::vector<double> log2_C_, log2_gamma_, log2_p_;
180 
183 
186 
188  static void printNull_(const char*) {}
189 
191  void scaleData_(PredictorMap& predictors);
192 
194  void convertData_(const PredictorMap& predictors);
195 
197  std::tuple<double, double, double> chooseBestParameters_(bool higher_better) const;
198 
200  void optimizeParameters_(bool classification);
201  };
202 }
A base class for all classes handling default parameters.
Definition: DefaultParamHandler.h:92
Simple interface to support vector machines for classification and regression (via LIBSVM).
Definition: SimpleSVM.h:77
void convertData_(const PredictorMap &predictors)
Convert predictors to LIBSVM format.
void optimizeParameters_(bool classification)
Run cross-validation to optimize SVM parameters.
~SimpleSVM() override
Destructor.
static void printNull_(const char *)
Dummy function to suppress LIBSVM output.
Definition: SimpleSVM.h:188
void predict(PredictorMap &predictors, std::vector< Prediction > &predictions) const
Predict class labels or regression values (and probabilities).
Size n_parts_
Number of partitions for cross-validation.
Definition: SimpleSVM.h:176
struct svm_model * model_
Pointer to SVM model (LIBSVM format)
Definition: SimpleSVM.h:170
std::vector< std::vector< std::vector< double > > > SVMPerformance
Classification (or regression) performance for different param. combinations (C/gamma/p):
Definition: SimpleSVM.h:158
std::map< double, double > probabilities
Class label (or regression value) and their predicted probabilities.
Definition: SimpleSVM.h:93
void predict(std::vector< Prediction > &predictions, std::vector< Size > indexes=std::vector< Size >()) const
Predict class labels or regression values (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::tuple< double, double, double > chooseBestParameters_(bool higher_better) const
Choose best SVM parameters based on cross-validation results.
std::map< String, std::vector< double > > PredictorMap
Mapping from predictor name to vector of predictor values.
Definition: SimpleSVM.h:81
double outcome
Predicted class label (or regression value)
Definition: SimpleSVM.h:90
const ScaleMap & getScaling() const
Get data range of predictors before scaling to [0, 1].
SVMPerformance performance_
Cross-validation results.
Definition: SimpleSVM.h:185
std::vector< double > log2_C_
Parameter values to try during optimization.
Definition: SimpleSVM.h:179
SimpleSVM()
Default constructor.
std::map< String, std::pair< double, double > > ScaleMap
Mapping from predictor name to predictor min and max.
Definition: SimpleSVM.h:84
std::vector< String > predictor_names_
Names of predictors in the model (excluding uninformative ones)
Definition: SimpleSVM.h:173
void setup(PredictorMap &predictors, const std::map< Size, double > &outcomes, bool classification=true)
Load data and train a model.
ScaleMap scaling_
Mapping from predictor name to predictor min and max.
Definition: SimpleSVM.h:182
void getFeatureWeights(std::map< String, double > &feature_weights) const
Get the weights used for features (predictors) in the SVM model.
SVM/SVR prediction result.
Definition: SimpleSVM.h:88
A more convenient string class.
Definition: String.h:60
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:48