37 #include <OpenMS/config.h>
66 RANSACParam(
size_t p_n,
size_t p_k,
double p_t,
size_t p_d,
bool p_relative_d =
false)
71 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."));
78 r <<
"RANSAC param:\n n: " <<
n <<
"\n k: " <<
k <<
" iterations\n t: " <<
t <<
" threshold\n d: " <<
d <<
" inliers\n\n";
94 template<
typename TModelType = RansacModelLinear>
99 explicit RANSAC(uint64_t seed = time(
nullptr)):
113 std::vector<std::pair<double, double> >
ransac(
114 const std::vector<std::pair<double, double> >& pairs,
150 std::vector<std::pair<double, double> >
ransac(
151 const std::vector<std::pair<double, double> >& pairs,
156 bool relative_d =
false)
161 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."));
162 d = pairs.size() * d / 100;
167 if (pairs.size() <= n)
170 String(
"RANSAC: Number of total data points (") +
String(pairs.size()) +
") must be larger than number of initial points (n=" +
String(n) +
").");
175 std::vector< std::pair<double, double> > alsoinliers, betterdata, bestdata;
176 std::vector<std::pair<double, double> > pairs_shuffled = pairs;
177 double besterror = std::numeric_limits<double>::max();
178 typename TModelType::ModelParameters coeff;
180 std::pair<double, double > bestcoeff;
181 double betterrsq = 0;
185 for (
size_t ransac_int=0; ransac_int<
k; ransac_int++)
188 if (bestdata.size() == pairs.size())
break;
196 coeff = model.rm_fit(pairs_shuffled.begin(), pairs_shuffled.begin()+n);
203 alsoinliers = model.rm_inliers(pairs_shuffled.begin()+n, pairs_shuffled.end(), coeff, t);
205 if (alsoinliers.size() > d
206 || alsoinliers.size() >= (pairs_shuffled.size()-n))
209 std::copy( pairs_shuffled.begin(), pairs_shuffled.begin()+n, back_inserter(betterdata) );
210 betterdata.insert( betterdata.end(), alsoinliers.begin(), alsoinliers.end() );
211 typename TModelType::ModelParameters bettercoeff = model.rm_fit(betterdata.begin(), betterdata.end());
212 double bettererror = model.rm_rss(betterdata.begin(), betterdata.end(), bettercoeff);
214 betterrsq = model.rm_rsq(betterdata);
221 if (betterdata.size() > bestdata.size() || (betterdata.size() == bestdata.size() && (bettererror < besterror)))
223 besterror = bettererror;
224 bestdata = betterdata;
226 bestcoeff = bettercoeff;
228 std::cout <<
"RANSAC " << ransac_int <<
": Points: " << betterdata.size() <<
" RSQ: " << bestrsq <<
" Error: " << besterror <<
" c0: " << bestcoeff.first <<
" c1: " << bestcoeff.second << std::endl;
235 std::cout <<
"=======STARTPOINTS=======" << std::endl;
236 for (std::vector<std::pair<double, double> >::iterator it = bestdata.begin(); it != bestdata.end(); ++it)
238 std::cout << it->first <<
"\t" << it->second << std::endl;
240 std::cout <<
"=======ENDPOINTS=======" << std::endl;
Precondition failed exception.
Definition: Exception.h:159
This class provides a generic implementation of the RANSAC outlier detection algorithm....
Definition: RANSAC.h:96
void setSeed(uint64_t seed)
set seed for random shuffle
Definition: RANSAC.h:107
Math::RandomShuffler shuffler_
Definition: RANSAC.h:247
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)
This function provides a generic implementation of the RANSAC outlier detection algorithm....
Definition: RANSAC.h:150
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:113
RANSAC(uint64_t seed=time(nullptr))
Definition: RANSAC.h:99
Definition: MathFunctions.h:408
void seed(uint64_t val)
Definition: MathFunctions.h:432
void portable_random_shuffle(RandomAccessIterator first, RandomAccessIterator last)
Definition: MathFunctions.h:423
A more convenient string class.
Definition: String.h:60
const double k
Definition: Constants.h:158
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:48
A simple struct to carry all the parameters required for a RANSAC run.
Definition: RANSAC.h:59
std::string toString() const
Definition: RANSAC.h:75
size_t d
The number of close data values (according to 't') required to assert that a model fits well to data.
Definition: RANSAC.h:85
size_t n
data points: The minimum number of data points required to fit the model
Definition: RANSAC.h:82
double t
Threshold value: for determining when a data point fits a model. Corresponds to the maximal squared d...
Definition: RANSAC.h:84
size_t k
iterations: The maximum number of iterations allowed in the algorithm
Definition: RANSAC.h:83
RANSACParam(size_t p_n, size_t p_k, double p_t, size_t p_d, bool p_relative_d=false)
Full constructor.
Definition: RANSAC.h:66
bool relative_d
Should 'd' be interpreted as percentages (0-100) of data input size.
Definition: RANSAC.h:86
RANSACParam()
Default constructor.
Definition: RANSAC.h:61