62 template <
typename Container = MSSpectrum>
85 this->
setName(
"SignalToNoiseEstimatorMedian");
87 defaults_.
setValue(
"max_intensity", -1,
"maximal intensity considered for histogram construction. By default, it will be calculated automatically (see auto_mode)." \
88 " Only provide this parameter if you know what you are doing (and change 'auto_mode' to '-1')!" \
89 " All intensities EQUAL/ABOVE 'max_intensity' will be added to the LAST histogram bin." \
90 " If you choose 'max_intensity' too small, the noise estimate might be too small as well. " \
91 " If chosen too big, the bins become quite large (which you could counter by increasing 'bin_count', which increases runtime)." \
92 " In general, the Median-S/N estimator is more robust to a manual max_intensity than the MeanIterative-S/N.", {
"advanced"});
95 defaults_.
setValue(
"auto_max_stdev_factor", 3.0,
"parameter for 'max_intensity' estimation (if 'auto_mode' == 0): mean + 'auto_max_stdev_factor' * stdev", {
"advanced"});
99 defaults_.
setValue(
"auto_max_percentile", 95,
"parameter for 'max_intensity' estimation (if 'auto_mode' == 1): auto_max_percentile th percentile", {
"advanced"});
103 defaults_.
setValue(
"auto_mode", 0,
"method to use to determine maximal intensity: -1 --> use 'max_intensity'; 0 --> 'auto_max_stdev_factor' method (default); 1 --> 'auto_max_percentile' method", {
"advanced"});
113 defaults_.
setValue(
"min_required_elements", 10,
"minimum number of elements required in a window (otherwise it is considered sparse)");
116 defaults_.
setValue(
"noise_for_empty_window", std::pow(10.0, 20),
"noise value used for sparse windows", {
"advanced"});
118 defaults_.
setValue(
"write_log_messages",
"true",
"Write out log messages in case of sparse windows or median in rightmost histogram bin");
137 if (&source ==
this)
return *
this;
196 "SignalToNoiseEstimatorMedian: a setting is outside the range of its parameter (win_len >= 1, "
197 "bin_count >= 3, min_required_elements >= 1, auto_mode in [-1, 1], max_intensity >= -1, "
198 "auto_max_stdev_factor in [0, 999])");
229 OPENMS_PRETTY_FUNCTION,
230 "auto_mode is on AUTOMAXBYPERCENT! auto_max_percentile is not in [0,100]. Use setAutoMaxPercentile(<value>) to change it!",
238 std::vector<int> histogram_auto(100, 0);
241 auto maxIt = std::max_element(c.begin(), c.end() ,[](
const PeakType& a,
const PeakType& b){ return a.getIntensity() < b.getIntensity();});
244 double bin_size = maxInt > 0 ?
static_cast<double>(maxInt) / 100.0 : 1.0;
247 for(
const auto& peak : c)
249 ++histogram_auto[(int) std::max(0.0, std::min(peak.getIntensity() / bin_size, 99.0))];
253 int elements_below_percentile = std::max(1, (
int) (settings.
auto_max_percentile * c.size() / 100));
254 int elements_seen = 0;
257 while (i < 99 && elements_seen < elements_below_percentile)
260 elements_seen += histogram_auto[i];
272 OPENMS_PRETTY_FUNCTION,
273 "auto_mode is on MANUAL! max_intensity is <=0. Needs to be positive! Use setMaxIntensity(<value>) or enable auto_mode!",
288 double window_half_size = settings.
win_len / 2;
290 int bin_count_minus_1 = settings.
bin_count - 1;
292 std::vector<int> histogram(settings.
bin_count, 0);
293 std::vector<double> bin_value(settings.
bin_count, 0);
295 for (
int bin = 0; bin < settings.
bin_count; bin++)
298 bin_value[bin] = (bin + 0.5) * bin_size;
306 int element_inc_count = 0;
309 int elements_in_window = 0;
311 int window_count = 0;
314 int element_in_window_half = 0;
319 if (progress !=
nullptr) progress->startProgress(0, c.size(),
"noise estimation of data");
322 while (window_pos_center != scan_last_)
326 while ((*window_pos_borderleft).getPos() < (*window_pos_center).getPos() - window_half_size)
328 to_bin = (int) std::max(std::min((*window_pos_borderleft).getIntensity() / bin_size, (double) bin_count_minus_1), 0.0);
330 --elements_in_window;
331 ++window_pos_borderleft;
335 while ((window_pos_borderright != scan_last_)
336 && ((*window_pos_borderright).getPos() <= (*window_pos_center).getPos() + window_half_size))
339 to_bin = (int) std::max(std::min((*window_pos_borderright).getIntensity() / bin_size, (double) bin_count_minus_1), 0.0);
341 ++elements_in_window;
342 ++window_pos_borderright;
354 element_inc_count = 0;
355 element_in_window_half = (elements_in_window + 1) / 2;
356 while (median_bin < bin_count_minus_1 && element_inc_count < element_in_window_half)
359 element_inc_count += histogram[median_bin];
372 const int elements_in_median_bin = histogram[median_bin];
373 double noise_estimate;
374 if (elements_in_median_bin > 0)
376 const int elements_before_median_bin = element_inc_count - elements_in_median_bin;
377 const double median_bin_lower_edge = median_bin * bin_size;
378 noise_estimate = median_bin_lower_edge
379 + ((double)(element_in_window_half - elements_before_median_bin) / elements_in_median_bin) * bin_size;
383 noise_estimate = bin_value[median_bin];
387 noise = std::max(1.0, noise_estimate);
391 sn[window_count] = (*window_pos_center).getIntensity() / noise;
398 if (progress !=
nullptr) progress->setProgress(window_count);
402 if (progress !=
nullptr) progress->endProgress();
412 <<
"% of all windows were sparse. You should consider increasing 'win_len' or decreasing 'min_required_elements'"
421 <<
"% of all Signal-to-Noise estimates are too high, because the median was found in the rightmost histogram-bin. "
422 <<
"You should consider increasing 'max_intensity' (and maybe 'bin_count' with it, to keep bin width reasonable)"
#define OPENMS_LOG_WARN
Macro for warnings.
Definition LogStream.h:691
void defaultsToParam_()
Updates the parameters after the defaults have been set in the constructor.
Param param_
Container for current parameters.
Definition DefaultParamHandler.h:139
Param defaults_
Container for default parameters. This member should be filled in the constructor of derived classes!
Definition DefaultParamHandler.h:146
void setName(const std::string &name)
Mutable access to the name.
Exception indicating that an invalid parameter was handed over to an algorithm.
Definition Exception.h:317
Invalid value exception.
Definition Exception.h:306
bool toBool() const
Conversion to bool.
const ParamValue & getValue(const std::string &key) const
Returns a value of a parameter.
void setValidStrings(const std::string &key, const std::vector< std::string > &strings)
Sets the valid strings for the parameter key.
void setMaxFloat(const std::string &key, double max)
Sets the maximum value for the floating point or floating point list parameter key.
void setMaxInt(const std::string &key, int max)
Sets the maximum value for the integer or integer list parameter key.
void setMinInt(const std::string &key, int min)
Sets the minimum value for the integer or integer list parameter key.
void setValue(const std::string &key, const ParamValue &value, const std::string &description="", const std::vector< std::string > &tags=std::vector< std::string >())
Sets a value.
void setMinFloat(const std::string &key, double min)
Sets the minimum value for the floating point or floating point list parameter key.
float IntensityType
Intensity type.
Definition Peak2D.h:37
Base class for all classes that want to report their progress.
Definition ProgressLogger.h:28
This class represents the abstract base class of a signal to noise estimator.
Definition SignalToNoiseEstimator.h:33
double variance
variance of estimated Gaussian
Definition SignalToNoiseEstimator.h:108
PeakIterator::value_type PeakType
Definition SignalToNoiseEstimator.h:40
SignalToNoiseEstimator & operator=(const SignalToNoiseEstimator &source)
Assignment operator.
Definition SignalToNoiseEstimator.h:60
static GaussianEstimate estimate_(const PeakIterator &scan_first_, const PeakIterator &scan_last_)
calculate mean & stdev of intensities of a spectrum
Definition SignalToNoiseEstimator.h:113
double mean
mean of estimated Gaussian
Definition SignalToNoiseEstimator.h:107
std::vector< double > stn_estimates_
stores the noise estimate for each peak
Definition SignalToNoiseEstimator.h:146
Container::const_iterator PeakIterator
Definition SignalToNoiseEstimator.h:39
protected struct to store parameters my, sigma for a Gaussian distribution
Definition SignalToNoiseEstimator.h:106
std::string toStr(int i)
Definition StringUtils.h:100
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19