00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #ifndef OPENMS_ANALYSIS_SVM_SVMWRAPPER_H
00036 #define OPENMS_ANALYSIS_SVM_SVMWRAPPER_H
00037
00038 #include <svm.h>
00039
00040 #include <OpenMS/CONCEPT/Types.h>
00041 #include <OpenMS/CONCEPT/ProgressLogger.h>
00042 #include <OpenMS/DATASTRUCTURES/String.h>
00043 #include <OpenMS/FORMAT/TextFile.h>
00044 #include <OpenMS/SYSTEM/File.h>
00045
00046 #include <string>
00047 #include <vector>
00048 #include <map>
00049 #include <cmath>
00050 #include <iostream>
00051 #include <fstream>
00052
00053 namespace OpenMS
00054 {
00055
00057 struct SVMData
00058 {
00059 std::vector<std::vector<std::pair<Int, DoubleReal> > > sequences;
00060 std::vector<DoubleReal> labels;
00061
00062 SVMData() :
00063 sequences(std::vector<std::vector<std::pair<Int, DoubleReal> > >()),
00064 labels(std::vector<DoubleReal>())
00065 {
00066 }
00067
00068 SVMData(std::vector<std::vector<std::pair<Int, DoubleReal> > > & seqs, std::vector<DoubleReal> & lbls) :
00069 sequences(seqs),
00070 labels(lbls)
00071 {
00072 }
00073
00074 bool operator==(const SVMData & rhs) const
00075 {
00076 return sequences == rhs.sequences
00077 && labels == rhs.labels;
00078 }
00079
00080 bool store(const String & filename) const
00081 {
00082 std::ofstream output_file(filename.c_str());
00083
00084
00085 if (!File::writable(filename) || sequences.size() != labels.size())
00086 {
00087 return false;
00088 }
00089
00090
00091 for (Size i = 0; i < sequences.size(); i++)
00092 {
00093 output_file << labels[i] << " ";
00094 for (Size j = 0; j < sequences[i].size(); ++j)
00095 {
00096 output_file << sequences[i][j].second << ":" << sequences[i][j].first << " ";
00097 }
00098 output_file << std::endl;
00099 }
00100 output_file.flush();
00101 output_file.close();
00102 std::cout.flush();
00103 return true;
00104 }
00105
00106 bool load(const String & filename)
00107 {
00108 Size counter = 0;
00109 std::vector<String> parts;
00110 std::vector<String> temp_parts;
00111
00112 if (!File::exists(filename))
00113 {
00114 return false;
00115 }
00116 if (!File::readable(filename))
00117 {
00118 return false;
00119 }
00120 if (File::empty(filename))
00121 {
00122 return false;
00123 }
00124
00125 TextFile text_file(filename.c_str(), true);
00126 TextFile::iterator it;
00127
00128 it = text_file.begin();
00129
00130 sequences.resize(text_file.size(), std::vector<std::pair<Int, DoubleReal> >());
00131 labels.resize(text_file.size(), 0.);
00132 while (counter < text_file.size() && it != text_file.end())
00133 {
00134 it->split(' ', parts);
00135 labels[counter] = parts[0].trim().toFloat();
00136 sequences[counter].resize(parts.size(), std::pair<Int, DoubleReal>());
00137 for (Size j = 1; j < parts.size(); ++j)
00138 {
00139 parts[j].split(':', temp_parts);
00140 if (temp_parts.size() < 2)
00141 {
00142 return false;
00143 }
00144 sequences[counter][j - 1].second = temp_parts[0].trim().toFloat();
00145 sequences[counter][j - 1].first = temp_parts[1].trim().toInt();
00146 }
00147 ++counter;
00148 ++it;
00149 }
00150 return true;
00151 }
00152
00153 };
00154
00163 class OPENMS_DLLAPI SVMWrapper :
00164 public ProgressLogger
00165 {
00166 public:
00167
00174 enum SVM_parameter_type
00175 {
00176 SVM_TYPE,
00177 KERNEL_TYPE,
00178 DEGREE,
00179 C,
00180 NU,
00181 P,
00182 GAMMA,
00183 PROBABILITY,
00184 SIGMA,
00185 BORDER_LENGTH
00186 };
00187
00189 enum SVM_kernel_type
00190 {
00191 OLIGO = 19,
00192 OLIGO_COMBINED
00193 };
00194
00196 SVMWrapper();
00197
00199 virtual ~SVMWrapper();
00200
00213 void setParameter(SVM_parameter_type type, Int value);
00214
00219 void setParameter(SVM_parameter_type type, DoubleReal value);
00220
00226 Int train(struct svm_problem * problem);
00227
00233 Int train(SVMData & problem);
00234
00243 void saveModel(std::string modelFilename) const;
00244
00251 void loadModel(std::string modelFilename);
00252
00259 void predict(struct svm_problem * problem, std::vector<DoubleReal> & predicted_labels);
00260
00267 void predict(const SVMData & problem, std::vector<DoubleReal> & results);
00268
00282 Int getIntParameter(SVM_parameter_type type);
00283
00293 DoubleReal getDoubleParameter(SVM_parameter_type type);
00294
00301 static void createRandomPartitions(svm_problem * problem, Size number, std::vector<svm_problem *> & partitions);
00302
00309 static void createRandomPartitions(const SVMData & problem,
00310 Size number,
00311 std::vector<SVMData> & problems);
00316 static svm_problem * mergePartitions(const std::vector<svm_problem *> & problems, Size except);
00317
00322 static void mergePartitions(const std::vector<SVMData> & problems,
00323 Size except,
00324 SVMData & merged_problem);
00325
00332 void predict(const std::vector<svm_node *> & vectors, std::vector<DoubleReal> & predicted_rts);
00333
00338 static void getLabels(svm_problem * problem, std::vector<DoubleReal> & labels);
00339
00344 DoubleReal performCrossValidation(svm_problem * problem_ul,
00345 const SVMData & problem_l,
00346 const bool is_labeled,
00347 const std::map<SVM_parameter_type, DoubleReal> & start_values_map,
00348 const std::map<SVM_parameter_type, DoubleReal> & step_sizes_map,
00349 const std::map<SVM_parameter_type, DoubleReal> & end_values_map,
00350 Size number_of_partitions,
00351 Size number_of_runs,
00352 std::map<SVM_parameter_type, DoubleReal> & best_parameters,
00353 bool additive_step_sizes = true,
00354 bool output = false,
00355 String performances_file_name = "performances.txt",
00356 bool mcc_as_performance_measure = false);
00357
00358
00368 DoubleReal getSVRProbability();
00369
00385 static DoubleReal kernelOligo(const std::vector<std::pair<int, double> > & x,
00386 const std::vector<std::pair<int, double> > & y,
00387 const std::vector<double> & gauss_table,
00388 int max_distance = -1);
00389
00397 static DoubleReal kernelOligo(const svm_node * x, const svm_node * y, const std::vector<DoubleReal> & gauss_table, DoubleReal sigma_square = 0, Size max_distance = 50);
00398
00403 void getSignificanceBorders(svm_problem * data, std::pair<DoubleReal, DoubleReal> & borders, DoubleReal confidence = 0.95, Size number_of_runs = 5, Size number_of_partitions = 5, DoubleReal step_size = 0.01, Size max_iterations = 1000000);
00404
00409 void getSignificanceBorders(const SVMData & data,
00410 std::pair<DoubleReal, DoubleReal> & sigmas,
00411 DoubleReal confidence = 0.95,
00412 Size number_of_runs = 5,
00413 Size number_of_partitions = 5,
00414 DoubleReal step_size = 0.01,
00415 Size max_iterations = 1000000);
00416
00424 DoubleReal getPValue(DoubleReal sigma1, DoubleReal sigma2, std::pair<DoubleReal, DoubleReal> point);
00425
00436 void getDecisionValues(svm_problem * data, std::vector<DoubleReal> & decision_values);
00437
00444 void scaleData(svm_problem * data, Int max_scale_value = -1);
00445
00446 static void calculateGaussTable(Size border_length, DoubleReal sigma, std::vector<DoubleReal> & gauss_table);
00447
00456 svm_problem * computeKernelMatrix(svm_problem * problem1, svm_problem * problem2);
00457
00466 svm_problem * computeKernelMatrix(const SVMData & problem1, const SVMData & problem2);
00467
00472 void setTrainingSample(svm_problem * training_sample);
00473
00478 void setTrainingSample(SVMData & training_sample);
00479
00489 void getSVCProbabilities(struct svm_problem * problem, std::vector<DoubleReal> & probabilities, std::vector<DoubleReal> & prediction_labels);
00490
00495 void setWeights(const std::vector<Int> & weight_labels, const std::vector<DoubleReal> & weights);
00496
00497 private:
00505 bool nextGrid_(const std::vector<DoubleReal> & start_values,
00506 const std::vector<DoubleReal> & step_sizes,
00507 const std::vector<DoubleReal> & end_values,
00508 const bool additive_step_sizes,
00509 std::vector<DoubleReal> & actual_values);
00510
00511 Size getNumberOfEnclosedPoints_(DoubleReal m1, DoubleReal m2, const std::vector<std::pair<DoubleReal, DoubleReal> > & points);
00512
00517 void initParameters_();
00518
00525 static void printToVoid_(const char * );
00526
00527 svm_parameter * param_;
00528 svm_model * model_;
00529 DoubleReal sigma_;
00530 std::vector<DoubleReal> sigmas_;
00531 std::vector<DoubleReal> gauss_table_;
00532 std::vector<std::vector<DoubleReal> > gauss_tables_;
00533 Size kernel_type_;
00534 Size border_length_;
00535 svm_problem * training_set_;
00536 svm_problem * training_problem_;
00537 SVMData training_data_;
00538
00539 };
00540
00541 }
00542
00543 #endif // OPENMS_ANALYSIS_SVM_SVMWRAPPER_H