Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages
RANSAC.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-2016.
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: George Rosenberger $
32 // $Authors: George Rosenberger, Hannes Roest, Chris Bielow $
33 // --------------------------------------------------------------------------
34 
35 #ifndef OPENMS_MATH_MISC_RANSAC_H
36 #define OPENMS_MATH_MISC_RANSAC_H
37 
38 #include <OpenMS/config.h>
39 
41 
45 
46 #include <algorithm> // std::random_shuffle
47 #include <limits> // std::numeric_limits
48 #include <vector> // std::vector
49 
50 namespace OpenMS
51 {
52 
53  namespace Math
54  {
55 
61  template<typename TModelType = RansacModelLinear>
62  class RANSAC
63  {
64 public:
65 
83  static std::vector<std::pair<double, double> > ransac(
84  const std::vector<std::pair<double, double> >& pairs,
85  size_t n,
86  size_t k,
87  double t,
88  size_t d,
89  int (*rng)(int) = NULL)
90  {
91  TModelType model;
92 
93  // implementation of the RANSAC algorithm according to http://wiki.scipy.org/Cookbook/RANSAC.
94 
95  if (pairs.size() <= n)
96  {
97  throw Exception::Precondition(__FILE__, __LINE__, __PRETTY_FUNCTION__,
98  String("RANSAC: Number of pairs (") + String(pairs.size()) + ") must be larger than number of initial points (n=" + String(n) + ").");
99  }
100 
101  std::vector< std::pair<double, double> > alsoinliers, betterdata, bestdata;
102 
103  double besterror = std::numeric_limits<double>::max();
104  #ifdef DEBUG_MRMRTNORMALIZER
105  std::pair<double, double > bestcoeff;
106  double betterrsq = 0;
107  double bestrsq = 0;
108  #endif
109 
110  // mutable data. will be shuffled in every iteration
111  std::vector<std::pair<double, double> > pairs_shuffled = pairs;
112 
113  for (size_t ransac_int=0; ransac_int<k; ransac_int++)
114  {
115  // check if the model already includes all points
116  if (bestdata.size() == pairs.size()) break;
117 
118  if (rng != NULL)
119  { // use portable RNG in test mode
120  std::random_shuffle(pairs_shuffled.begin(), pairs_shuffled.end(), rng);
121  } else {
122  std::random_shuffle(pairs_shuffled.begin(), pairs_shuffled.end());
123  }
124 
125  // test 'maybeinliers'
126  typename TModelType::ModelParameters coeff = model.rm_fit(pairs_shuffled.begin(), pairs_shuffled.begin()+n);
127  // apply model to remaining data; pick inliers
128  alsoinliers = model.rm_inliers(pairs_shuffled.begin()+n, pairs_shuffled.end(), coeff, t);
129  // ... and add data
130  if (alsoinliers.size() > d)
131  {
132  betterdata.clear();
133  std::copy( pairs_shuffled.begin(), pairs_shuffled.begin()+n, back_inserter(betterdata) );
134  betterdata.insert( betterdata.end(), alsoinliers.begin(), alsoinliers.end() );
135  typename TModelType::ModelParameters bettercoeff = model.rm_fit(betterdata.begin(), betterdata.end());
136  double bettererror = model.rm_rss(betterdata.begin(), betterdata.end(), bettercoeff);
137  #ifdef DEBUG_MRMRTNORMALIZER
138  betterrsq = model.rm_rsq(betterdata);
139  #endif
140 
141  if (bettererror < besterror)
142  {
143  besterror = bettererror;
144  bestdata = betterdata;
145  #ifdef DEBUG_MRMRTNORMALIZER
146  bestcoeff = bettercoeff;
147  bestrsq = betterrsq;
148  std::cout << "RANSAC " << ransac_int << ": Points: " << betterdata.size() << " RSQ: " << bestrsq << " Error: " << besterror << " c0: " << bestcoeff.first << " c1: " << bestcoeff.second << std::endl;
149  #endif
150  }
151  }
152  }
153 
154  #ifdef DEBUG_MRMRTNORMALIZER
155  std::cout << "=======STARTPOINTS=======" << std::endl;
156  for (std::vector<std::pair<double, double> >::iterator it = bestdata.begin(); it != bestdata.end(); ++it)
157  {
158  std::cout << it->first << "\t" << it->second << std::endl;
159  }
160  std::cout << "=======ENDPOINTS=======" << std::endl;
161  #endif
162 
163  return(bestdata);
164  } // ransac()
165 
166  }; // class
167 
168  } // namespace Math
169 
170 
171 } // namespace OpenMS
172 #endif // OPENMS_MATH_MISC_RANSAC_H
const double k
A more convenient string class.
Definition: String.h:57
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
Precondition failed exception.
Definition: Exception.h:167
static std::vector< std::pair< double, double > > ransac(const std::vector< std::pair< double, double > > &pairs, size_t n, size_t k, double t, size_t d, int(*rng)(int)=NULL)
This function provides a generic implementation of the RANSAC outlier detection algorithm. Is implemented and tested after the SciPy reference: http://wiki.scipy.org/Cookbook/RANSAC.
Definition: RANSAC.h:83
This class provides a generic implementation of the RANSAC outlier detection algorithm. Is implemented and tested after the SciPy reference: http://wiki.scipy.org/Cookbook/RANSAC.
Definition: RANSAC.h:62

OpenMS / TOPP release 2.0.0 Documentation generated on Tue Jan 31 2017 06:39:52 using doxygen 1.8.8