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,
int (*p_rng)(
int) =
nullptr)
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";
95 template<
typename TModelType = RansacModelLinear>
101 static std::vector<std::pair<double, double> >
ransac(
102 const std::vector<std::pair<double, double> >& pairs,
139 static std::vector<std::pair<double, double> >
ransac(
140 const std::vector<std::pair<double, double> >& pairs,
145 bool relative_d =
false,
146 int (*rng)(
int) =
nullptr)
151 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."));
152 d = pairs.size() * d / 100;
157 if (pairs.size() <= n)
160 String(
"RANSAC: Number of total data points (") +
String(pairs.size()) +
") must be larger than number of initial points (n=" +
String(n) +
").");
165 std::vector< std::pair<double, double> > alsoinliers, betterdata, bestdata;
166 std::vector<std::pair<double, double> > pairs_shuffled = pairs;
167 double besterror = std::numeric_limits<double>::max();
168 typename TModelType::ModelParameters coeff;
170 std::pair<double, double > bestcoeff;
171 double betterrsq = 0;
175 for (
size_t ransac_int=0; ransac_int<
k; ransac_int++)
178 if (bestdata.size() == pairs.size())
break;
182 std::random_shuffle(pairs_shuffled.begin(), pairs_shuffled.end(), rng);
184 std::random_shuffle(pairs_shuffled.begin(), pairs_shuffled.end());
190 coeff = model.rm_fit(pairs_shuffled.begin(), pairs_shuffled.begin()+n);
197 alsoinliers = model.rm_inliers(pairs_shuffled.begin()+n, pairs_shuffled.end(), coeff, t);
199 if (alsoinliers.size() > d
200 || alsoinliers.size() >= (pairs_shuffled.size()-n))
203 std::copy( pairs_shuffled.begin(), pairs_shuffled.begin()+n, back_inserter(betterdata) );
204 betterdata.insert( betterdata.end(), alsoinliers.begin(), alsoinliers.end() );
205 typename TModelType::ModelParameters bettercoeff = model.rm_fit(betterdata.begin(), betterdata.end());
206 double bettererror = model.rm_rss(betterdata.begin(), betterdata.end(), bettercoeff);
208 betterrsq = model.rm_rsq(betterdata);
215 if (betterdata.size() > bestdata.size() || (betterdata.size() == bestdata.size() && (bettererror < besterror)))
217 besterror = bettererror;
218 bestdata = betterdata;
220 bestcoeff = bettercoeff;
222 std::cout <<
"RANSAC " << ransac_int <<
": Points: " << betterdata.size() <<
" RSQ: " << bestrsq <<
" Error: " << besterror <<
" c0: " << bestcoeff.first <<
" c1: " << bestcoeff.second << std::endl;
229 std::cout <<
"=======STARTPOINTS=======" << std::endl;
230 for (std::vector<std::pair<double, double> >::iterator it = bestdata.begin(); it != bestdata.end(); ++it)
232 std::cout << it->first <<
"\t" << it->second << std::endl;
234 std::cout <<
"=======ENDPOINTS=======" << std::endl;
A more convenient string class.
Definition: String.h:57
bool relative_d
Should 'd' be interpreted as percentages (0-100) of data input size.
Definition: RANSAC.h:86
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:101
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 k
iterations: The maximum number of iterations allowed in the algorithm
Definition: RANSAC.h:83
A simple struct to carry all the parameters required for a RANSAC run.
Definition: RANSAC.h:58
std::string toString() const
Definition: RANSAC.h:75
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
Precondition failed exception.
Definition: Exception.h:166
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)=nullptr)
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:139
int(* rng)(int)
Optional RNG function (useful for testing with fixed seeds)
Definition: RANSAC.h:87
RANSACParam()
Default constructor.
Definition: RANSAC.h:61
double t
Threshold value: for determining when a data point fits a model. Corresponds to the maximal squared d...
Definition: RANSAC.h:84
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)=nullptr)
Full constructor.
Definition: RANSAC.h:66
size_t n
data points: The minimum number of data points required to fit the model
Definition: RANSAC.h:82
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:96