35 #ifndef OPENMS_MATH_MISC_RANSAC_H 36 #define OPENMS_MATH_MISC_RANSAC_H 38 #include <OpenMS/config.h> 67 RANSACParam(
size_t p_n,
size_t p_k,
double p_t,
size_t p_d,
bool p_relative_d =
false,
int (*p_rng)(
int) = NULL)
72 if (
d >= 100)
throw Exception::Precondition(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
String(
"RANSAC: Relative 'd' >= 100% given. Use a lower value; the more outliers you expect, the lower it should be."));
79 r <<
"RANSAC param:\n n: " <<
n <<
"\n k: " <<
k <<
" iterations\n t: " <<
t <<
" threshold\n d: " <<
d <<
" inliers\n\n";
96 template<
typename TModelType = RansacModelLinear>
102 static std::vector<std::pair<double, double> >
ransac(
103 const std::vector<std::pair<double, double> >& pairs,
140 static std::vector<std::pair<double, double> >
ransac(
141 const std::vector<std::pair<double, double> >& pairs,
147 int (*
rng)(
int) = NULL)
152 if (d >= 100)
throw Exception::Precondition(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
String(
"RANSAC: Relative 'd' >= 100% given. Use a lower value; the more outliers you expect, the lower it should be."));
153 d = pairs.size() * d / 100;
158 if (pairs.size() <=
n)
161 String(
"RANSAC: Number of total data points (") +
String(pairs.size()) +
") must be larger than number of initial points (n=" +
String(n) +
").");
166 std::vector< std::pair<double, double> > alsoinliers, betterdata, bestdata;
167 std::vector<std::pair<double, double> > pairs_shuffled = pairs;
168 double besterror = std::numeric_limits<double>::max();
169 typename TModelType::ModelParameters coeff;
171 std::pair<double, double > bestcoeff;
172 double betterrsq = 0;
176 for (
size_t ransac_int=0; ransac_int<
k; ransac_int++)
179 if (bestdata.size() == pairs.size())
break;
183 std::random_shuffle(pairs_shuffled.begin(), pairs_shuffled.end(),
rng);
185 std::random_shuffle(pairs_shuffled.begin(), pairs_shuffled.end());
191 coeff = model.rm_fit(pairs_shuffled.begin(), pairs_shuffled.begin()+
n);
198 alsoinliers = model.rm_inliers(pairs_shuffled.begin()+
n, pairs_shuffled.end(), coeff,
t);
200 if (alsoinliers.size() > d
201 || alsoinliers.size() >= (pairs_shuffled.size()-
n))
204 std::copy( pairs_shuffled.begin(), pairs_shuffled.begin()+
n, back_inserter(betterdata) );
205 betterdata.insert( betterdata.end(), alsoinliers.begin(), alsoinliers.end() );
206 typename TModelType::ModelParameters bettercoeff = model.rm_fit(betterdata.begin(), betterdata.end());
207 double bettererror = model.rm_rss(betterdata.begin(), betterdata.end(), bettercoeff);
209 betterrsq = model.rm_rsq(betterdata);
216 if (betterdata.size() > bestdata.size() || (betterdata.size() == bestdata.size() && (bettererror < besterror)))
218 besterror = bettererror;
219 bestdata = betterdata;
221 bestcoeff = bettercoeff;
223 std::cout <<
"RANSAC " << ransac_int <<
": Points: " << betterdata.size() <<
" RSQ: " << bestrsq <<
" Error: " << besterror <<
" c0: " << bestcoeff.first <<
" c1: " << bestcoeff.second << std::endl;
230 std::cout <<
"=======STARTPOINTS=======" << std::endl;
231 for (std::vector<std::pair<double, double> >::iterator it = bestdata.begin(); it != bestdata.end(); ++it)
233 std::cout << it->first <<
"\t" << it->second << std::endl;
235 std::cout <<
"=======ENDPOINTS=======" << std::endl;
247 #endif // OPENMS_MATH_MISC_RANSAC_H A more convenient string class.
Definition: String.h:57
bool relative_d
Definition: RANSAC.h:87
static std::vector< std::pair< double, double > > ransac(const std::vector< std::pair< double, double > > &pairs, const RANSACParam &p)
alias for ransac() with full params
Definition: RANSAC.h:102
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, bool relative_d=false, 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:140
size_t d
Definition: RANSAC.h:86
size_t k
Definition: RANSAC.h:84
A simple struct to carry all the parameters required for a RANSAC run.
Definition: RANSAC.h:59
std::string toString() const
Definition: RANSAC.h:76
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
Precondition failed exception.
Definition: Exception.h:167
int(* rng)(int)
Definition: RANSAC.h:88
RANSACParam(size_t p_n, size_t p_k, double p_t, size_t p_d, bool p_relative_d=false, int(*p_rng)(int)=NULL)
Full constructor.
Definition: RANSAC.h:67
RANSACParam()
Default constructor.
Definition: RANSAC.h:62
double t
Definition: RANSAC.h:85
size_t n
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:97