Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages

SVMWrapper.h (Maintainer: Nico Pfeifer)

Go to the documentation of this file.
00001 // -*- Mode: C++; tab-width: 2; -*-
00002 // vi: set ts=2:
00003 //
00004 // --------------------------------------------------------------------------
00005 //                   OpenMS Mass Spectrometry Framework
00006 // --------------------------------------------------------------------------
00007 //  Copyright (C) 2003-2008 -- Oliver Kohlbacher, Knut Reinert
00008 //
00009 //  This library is free software; you can redistribute it and/or
00010 //  modify it under the terms of the GNU Lesser General Public
00011 //  License as published by the Free Software Foundation; either
00012 //  version 2.1 of the License, or (at your option) any later version.
00013 // 
00014 //  This library is distributed in the hope that it will be useful,
00015 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 //  Lesser General Public License for more details.
00018 //
00019 //  You should have received a copy of the GNU Lesser General Public
00020 //  License along with this library; if not, write to the Free Software
00021 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 //
00023 // --------------------------------------------------------------------------
00024 // $Maintainer: Nico Pfeifer $
00025 // --------------------------------------------------------------------------
00026 
00027 #ifndef OPENMS_ANALYSIS_SVM_SVMWRAPPER_H
00028 #define OPENMS_ANALYSIS_SVM_SVMWRAPPER_H
00029 
00030 #include <svm.h>
00031 
00032 #include <OpenMS/CONCEPT/Types.h>
00033 #include <OpenMS/DATASTRUCTURES/String.h>
00034 
00035 #include <string>
00036 #include <vector>
00037 #include <map>
00038 #include <math.h>
00039 
00040 namespace OpenMS 
00041 {
00042   
00060   enum SVM_parameter_type{SVM_TYPE, KERNEL_TYPE, DEGREE, C, NU, P, GAMMA, PROBABILITY, SIGMA, BORDER_LENGTH};
00061   
00062   enum SVM_kernel_type{OLIGO = 19, OLIGO_COMBINED}; /* kernel_type */
00063   
00073   class SVMWrapper
00074   {
00075    public:
00076   
00078       SVMWrapper();
00079 
00081       ~SVMWrapper();
00082   
00095       void setParameter(SVM_parameter_type type, Int value);
00096   
00101       void setParameter(SVM_parameter_type type, DoubleReal value);
00102     
00108       Int  train(struct svm_problem* problem);
00109   
00116       void saveModel(std::string modelFilename) const throw (Exception::UnableToCreateFile);
00117   
00124       void loadModel(std::string modelFilename);
00125   
00132       void predict(struct svm_problem* predictProblem, std::vector<DoubleReal>& predicted_rts);
00133 
00147       Int getIntParameter(SVM_parameter_type type);
00148   
00158       DoubleReal getDoubleParameter(SVM_parameter_type type); 
00159 
00166       static void createRandomPartitions(svm_problem* problem, UInt number, std::vector<svm_problem*>& partitions);
00167   
00172       static svm_problem* mergePartitions(const std::vector<svm_problem*>& problems,UInt except);
00173                                           
00180       void predict(const std::vector<svm_node*>& vectors, std::vector<DoubleReal>& predicted_rts);
00181   
00186       static void getLabels(svm_problem* problem, std::vector<DoubleReal>& labels);
00187                                           
00192       DoubleReal performCrossValidation(svm_problem* problem, const std::map<SVM_parameter_type, DoubleReal>& start_values, const std::map<SVM_parameter_type, DoubleReal>& step_sizes, const std::map<SVM_parameter_type, DoubleReal>& end_values, UInt number_of_partitions, UInt number_of_runs, std::map<SVM_parameter_type, DoubleReal>& best_parameters, bool additive_step_size = true, bool output = false, String performances_file_name = "performances.txt", bool mcc_as_performance_measure = false);
00193                                           
00203       DoubleReal getSVRProbability();                                               
00204 
00212       static DoubleReal kernelOligo(const svm_node* x, const svm_node* y, const std::vector<DoubleReal>&  gauss_table, DoubleReal sigma_square = 0, UInt  max_distance = 50);
00213 
00218       void getSignificanceBorders(svm_problem* data, std::pair<DoubleReal, DoubleReal>& borders, DoubleReal confidence = 0.95, UInt number_of_runs = 10, UInt number_of_partitions = 5, DoubleReal step_size = 0.01, UInt max_iterations = 1000000);
00219 
00227       DoubleReal getPValue(DoubleReal sigma1, DoubleReal sigma2, std::pair<DoubleReal, DoubleReal> point);
00228 
00239       void getDecisionValues(svm_problem* data, std::vector<DoubleReal>& decision_values);
00240 
00247       void scaleData(svm_problem* data, Int max_scale_value = -1);
00248 
00249       static void calculateGaussTable(UInt border_length, DoubleReal sigma, std::vector<DoubleReal>&  gauss_table);
00250 
00259       svm_problem* computeKernelMatrix(svm_problem* problem1, svm_problem* problem2);
00260 
00265       void setTrainingSample(svm_problem* training_sample);
00266       
00276       void getSVCProbabilities(struct svm_problem* problem, std::vector<DoubleReal>& probabilities, std::vector<DoubleReal>& prediction_labels);
00277 
00282       void setWeights(const std::vector<Int>& weight_labels, const std::vector<DoubleReal>& weights);
00283 
00284 
00285     protected:
00286             
00287       void destroyProblem_(svm_problem* problem);
00288                                           
00289    private:
00290       UInt getNumberOfEnclosedPoints_(DoubleReal m1, DoubleReal m2, const std::vector<std::pair<DoubleReal, DoubleReal> >&  points);
00291   
00296       void initParameters_();
00297 
00298       svm_parameter*                        param_;               // the parameters for the svm
00299       svm_model*                            model_;               // the learnt svm discriminant
00300       DoubleReal                            sigma_;               // for the oligo kernel (amount of positional smearing) 
00301       std::vector<DoubleReal>               sigmas_;              // for the combined oligo kernel (amount of positional smearing) 
00302       std::vector<DoubleReal>               gauss_table_;         // lookup table for fast computation of the oligo kernel
00303       std::vector<std::vector<DoubleReal> > gauss_tables_;        // lookup table for fast computation of the combined oligo kernel
00304       UInt                            kernel_type_;         // the actual kernel type 
00305       UInt                            border_length_;       // the actual kernel type       
00306       svm_problem*                          training_set_;        // the training set
00307       svm_problem*                          training_problem_;    // the training set
00308 
00309   };
00310  
00311 } // namespace OpenMS
00312 
00313 #endif // OPENMS_ANALYSIS_SVM_SVMWRAPPER_H

Generated Tue Apr 1 15:36:38 2008 -- using doxygen 1.5.4 OpenMS / TOPP 1.1