Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages
SVMWrapper.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-2017.
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: Timo Sachsenberg $
32 // $Authors: Nico Pfeifer, Chris Bielow $
33 // --------------------------------------------------------------------------
34 
35 #ifndef OPENMS_ANALYSIS_SVM_SVMWRAPPER_H
36 #define OPENMS_ANALYSIS_SVM_SVMWRAPPER_H
37 
38 #include <svm.h>
39 
40 #include <OpenMS/CONCEPT/Types.h>
43 #include <OpenMS/FORMAT/TextFile.h>
44 #include <OpenMS/SYSTEM/File.h>
45 
46 #include <string>
47 #include <vector>
48 #include <map>
49 #include <cmath>
50 
51 namespace OpenMS
52 {
53 
55  struct OPENMS_DLLAPI SVMData
56  {
57  std::vector<std::vector<std::pair<Int, double> > > sequences;
58  std::vector<double> labels;
59 
60  SVMData();
61 
62  SVMData(std::vector<std::vector<std::pair<Int, double> > >& seqs, std::vector<double>& lbls);
63 
64  bool operator==(const SVMData& rhs) const;
65 
66  bool store(const String& filename) const;
67 
68  bool load(const String& filename);
69 
70  };
71 
80  class OPENMS_DLLAPI SVMWrapper :
81  public ProgressLogger
82  {
83 public:
84 
92  {
96  C,
97  NU,
98  P,
102  BORDER_LENGTH
103  };
104 
107  {
108  OLIGO = 19,
109  OLIGO_COMBINED
110  };
111 
113  SVMWrapper();
114 
116  virtual ~SVMWrapper();
117 
159  void setParameter(SVM_parameter_type type, Int value);
160 
167  void setParameter(SVM_parameter_type type, double value);
168 
174  Int train(struct svm_problem* problem);
175 
181  Int train(SVMData& problem);
182 
193  void saveModel(std::string modelFilename) const;
194 
203  void loadModel(std::string modelFilename);
204 
210  void predict(struct svm_problem* problem, std::vector<double>& predicted_labels);
211 
217  void predict(const SVMData& problem, std::vector<double>& results);
218 
258  Int getIntParameter(SVM_parameter_type type);
259 
287  double getDoubleParameter(SVM_parameter_type type);
288 
294  static void createRandomPartitions(svm_problem* problem, Size number, std::vector<svm_problem*>& partitions);
295 
301  static void createRandomPartitions(const SVMData& problem,
302  Size number,
303  std::vector<SVMData>& problems);
307  static svm_problem* mergePartitions(const std::vector<svm_problem*>& problems, Size except);
308 
312  static void mergePartitions(const std::vector<SVMData>& problems,
313  Size except,
314  SVMData& merged_problem);
315 
322  void predict(const std::vector<svm_node*>& vectors, std::vector<double>& predicted_rts);
323 
328  static void getLabels(svm_problem* problem, std::vector<double>& labels);
329 
334  double performCrossValidation(svm_problem* problem_ul,
335  const SVMData& problem_l,
336  const bool is_labeled,
337  const std::map<SVM_parameter_type, double>& start_values_map,
338  const std::map<SVM_parameter_type, double>& step_sizes_map,
339  const std::map<SVM_parameter_type, double>& end_values_map,
340  Size number_of_partitions,
341  Size number_of_runs,
342  std::map<SVM_parameter_type, double>& best_parameters,
343  bool additive_step_sizes = true,
344  bool output = false,
345  String performances_file_name = "performances.txt",
346  bool mcc_as_performance_measure = false);
347 
348 
358  double getSVRProbability();
359 
375  static double kernelOligo(const std::vector<std::pair<int, double> >& x,
376  const std::vector<std::pair<int, double> >& y,
377  const std::vector<double>& gauss_table,
378  int max_distance = -1);
379 
387  static double kernelOligo(const svm_node* x, const svm_node* y, const std::vector<double>& gauss_table, double sigma_square = 0, Size max_distance = 50);
388 
392  void getSignificanceBorders(svm_problem* data, std::pair<double, double>& borders, double confidence = 0.95, Size number_of_runs = 5, Size number_of_partitions = 5, double step_size = 0.01, Size max_iterations = 1000000);
393 
397  void getSignificanceBorders(const SVMData& data,
398  std::pair<double, double>& sigmas,
399  double confidence = 0.95,
400  Size number_of_runs = 5,
401  Size number_of_partitions = 5,
402  double step_size = 0.01,
403  Size max_iterations = 1000000);
404 
411  double getPValue(double sigma1, double sigma2, std::pair<double, double> point);
412 
422  void getDecisionValues(svm_problem* data, std::vector<double>& decision_values);
423 
430  void scaleData(svm_problem* data, Int max_scale_value = -1);
431 
432  static void calculateGaussTable(Size border_length, double sigma, std::vector<double>& gauss_table);
433 
441  svm_problem* computeKernelMatrix(svm_problem* problem1, svm_problem* problem2);
442 
450  svm_problem* computeKernelMatrix(const SVMData& problem1, const SVMData& problem2);
451 
456  void setTrainingSample(svm_problem* training_sample);
457 
461  void setTrainingSample(SVMData& training_sample);
462 
472  void getSVCProbabilities(struct svm_problem* problem, std::vector<double>& probabilities, std::vector<double>& prediction_labels);
473 
477  void setWeights(const std::vector<Int>& weight_labels, const std::vector<double>& weights);
478 
479 private:
486  bool nextGrid_(const std::vector<double>& start_values,
487  const std::vector<double>& step_sizes,
488  const std::vector<double>& end_values,
489  const bool additive_step_sizes,
490  std::vector<double>& actual_values);
491 
492  Size getNumberOfEnclosedPoints_(double m1, double m2, const std::vector<std::pair<double, double> >& points);
493 
497  void initParameters_();
498 
504  static void printToVoid_(const char* /*s*/);
505 
506  svm_parameter* param_; // the parameters for the svm
507  svm_model* model_; // the learned svm discriminant
508  double sigma_; // for the oligo kernel (amount of positional smearing)
509  std::vector<double> sigmas_; // for the combined oligo kernel (amount of positional smearing)
510  std::vector<double> gauss_table_; // lookup table for fast computation of the oligo kernel
511  std::vector<std::vector<double> > gauss_tables_; // lookup table for fast computation of the combined oligo kernel
512  Size kernel_type_; // the actual kernel type
513  Size border_length_; // the actual kernel type
514  svm_problem* training_set_; // the training set
515  svm_problem* training_problem_; // the training set
516  SVMData training_data_; // the training set (different encoding)
517  };
518 
519 } // namespace OpenMS
520 
521 #endif // OPENMS_ANALYSIS_SVM_SVMWRAPPER_H
the C parameter of the svm
Definition: SVMWrapper.h:96
A more convenient string class.
Definition: String.h:57
SVMData training_data_
Definition: SVMWrapper.h:516
svm_model * model_
Definition: SVMWrapper.h:507
svm_problem * training_problem_
Definition: SVMWrapper.h:515
Serves as a wrapper for the libsvm.
Definition: SVMWrapper.h:80
svm_problem * training_set_
Definition: SVMWrapper.h:514
the epsilon parameter for epsilon-SVR
Definition: SVMWrapper.h:98
SVM_kernel_type
Kernel type.
Definition: SVMWrapper.h:106
bool operator==(_Iterator< _Val, _Ref, _Ptr > const &, _Iterator< _Val, _Ref, _Ptr > const &)
Definition: KDTree.h:806
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
std::vector< std::vector< std::pair< Int, double > > > sequences
Definition: SVMWrapper.h:57
Size border_length_
Definition: SVMWrapper.h:513
Size kernel_type_
Definition: SVMWrapper.h:512
svm_parameter * param_
Definition: SVMWrapper.h:506
Data structure used in SVMWrapper.
Definition: SVMWrapper.h:55
the svm type cab be NU_SVR or EPSILON_SVR
Definition: SVMWrapper.h:93
std::vector< double > gauss_table_
Definition: SVMWrapper.h:510
Definition: SVMWrapper.h:101
the gamma parameter of the POLY, RBF and SIGMOID kernel
Definition: SVMWrapper.h:99
Definition: SVMWrapper.h:100
double sigma_
Definition: SVMWrapper.h:508
the degree for the polynomial- kernel
Definition: SVMWrapper.h:95
std::vector< double > labels
Definition: SVMWrapper.h:58
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:128
std::vector< std::vector< double > > gauss_tables_
Definition: SVMWrapper.h:511
Base class for all classes that want to report their progress.
Definition: ProgressLogger.h:55
int Int
Signed integer type.
Definition: Types.h:103
std::vector< double > sigmas_
Definition: SVMWrapper.h:509
the nu parameter for nu-SVR
Definition: SVMWrapper.h:97
the kernel type
Definition: SVMWrapper.h:94
SVM_parameter_type
Parameters for the svm to be set from outside.
Definition: SVMWrapper.h:91

OpenMS / TOPP release 2.3.0 Documentation generated on Tue Jan 9 2018 18:22:04 using doxygen 1.8.13