36 #ifndef OPENMS_FILTERING_NOISEESTIMATION_SIGNALTONOISEESTIMATORMEDIAN_H
37 #define OPENMS_FILTERING_NOISEESTIMATION_SIGNALTONOISEESTIMATORMEDIAN_H
70 template <
typename Container = MSSpectrum<> >
96 this->
setName(
"SignalToNoiseEstimatorMedian");
98 defaults_.
setValue(
"max_intensity", -1,
"maximal intensity considered for histogram construction. By default, it will be calculated automatically (see auto_mode)." \
99 " Only provide this parameter if you know what you are doing (and change 'auto_mode' to '-1')!" \
100 " All intensities EQUAL/ABOVE 'max_intensity' will be added to the LAST histogram bin." \
101 " If you choose 'max_intensity' too small, the noise estimate might be too small as well. " \
102 " If chosen too big, the bins become quite large (which you could counter by increasing 'bin_count', which increases runtime)." \
103 " In general, the Median-S/N estimator is more robust to a manual max_intensity than the MeanIterative-S/N.",
StringList::create(
"advanced"));
106 defaults_.
setValue(
"auto_max_stdev_factor", 3.0,
"parameter for 'max_intensity' estimation (if 'auto_mode' == 0): mean + 'auto_max_stdev_factor' * stdev",
StringList::create(
"advanced"));
110 defaults_.
setValue(
"auto_max_percentile", 95,
"parameter for 'max_intensity' estimation (if 'auto_mode' == 1): auto_max_percentile th percentile",
StringList::create(
"advanced"));
114 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",
StringList::create(
"advanced"));
124 defaults_.
setValue(
"min_required_elements", 10,
"minimum number of elements required in a window (otherwise it is considered sparse)");
146 if (&source ==
this)
return *
this;
172 double sparse_window_percent = 0;
174 double histogram_oob_percent = 0;
196 "auto_mode is on AUTOMAXBYPERCENT! auto_max_percentile is not in [0,100]. Use setAutoMaxPercentile(<value>) to change it!",
200 std::vector<int> histogram_auto(100, 0);
206 while (run != scan_last_)
208 maxInt = std::max(maxInt, (*run).getIntensity());
213 double bin_size = maxInt / 100;
217 while (run != scan_last_)
219 ++histogram_auto[(int) (((*run).getIntensity() - 1) / bin_size)];
225 int elements_seen = 0;
229 while (run != scan_last_ && elements_seen < elements_below_percentile)
232 elements_seen += histogram_auto[i];
246 "auto_mode is on MANUAL! max_intensity is <=0. Needs to be positive! Use setMaxIntensity(<value>) or enable auto_mode!",
253 std::cerr <<
"TODO SignalToNoiseEstimatorMedian: the max_intensity_ value should be positive! " <<
max_intensity_ << std::endl;
261 double window_half_size =
win_len_ / 2;
271 bin_value[bin] = (bin + 0.5) * bin_size;
279 int element_inc_count = 0;
282 int elements_in_window = 0;
284 int window_count = 0;
287 int element_in_window_half = 0;
292 int windows_overall = 0;
294 while (run != scan_last_)
302 while (window_pos_center != scan_last_)
306 while ((*window_pos_borderleft).getMZ() < (*window_pos_center).getMZ() - window_half_size)
308 to_bin = std::max(std::min<int>((
int)((*window_pos_borderleft).getIntensity() / bin_size), bin_count_minus_1), 0);
310 --elements_in_window;
311 ++window_pos_borderleft;
315 while ((window_pos_borderright != scan_last_)
316 && ((*window_pos_borderright).getMZ() <= (*window_pos_center).getMZ() + window_half_size))
319 to_bin = std::max(std::min<int>((
int)((*window_pos_borderright).getIntensity() / bin_size), bin_count_minus_1), 0);
321 ++elements_in_window;
322 ++window_pos_borderright;
328 ++sparse_window_percent;
334 element_inc_count = 0;
335 element_in_window_half = (elements_in_window + 1) / 2;
336 while (median_bin < bin_count_minus_1 && element_inc_count < element_in_window_half)
339 element_inc_count += histogram[median_bin];
343 if (median_bin == bin_count_minus_1) {++histogram_oob_percent; }
346 noise = std::max(1.0, bin_value[median_bin]);
350 stn_estimates_[*window_pos_center] = (*window_pos_center).getIntensity() / noise;
363 sparse_window_percent = sparse_window_percent * 100 / window_count;
364 histogram_oob_percent = histogram_oob_percent * 100 / window_count;
367 if (sparse_window_percent > 20)
369 LOG_WARN <<
"WARNING in SignalToNoiseEstimatorMedian: "
370 << sparse_window_percent
371 <<
"% of all windows were sparse. You should consider increasing 'win_len' or decreasing 'min_required_elements'"
376 if (histogram_oob_percent > 1)
378 LOG_WARN <<
"WARNING in SignalToNoiseEstimatorMedian: "
379 << histogram_oob_percent
380 <<
"% of all Signal-to-Noise estimates are too high, because the median was found in the rightmost histogram-bin. "
381 <<
"You should consider increasing 'max_intensity' (and maybe 'bin_count' with it, to keep bin width reasonable)"
425 #endif //OPENMS_FILTERING_NOISEESTIMATION_DSIGNALTONOISEESTIMATORMEDIAN_H
Real IntensityType
Intensity type.
Definition: Peak2D.h:63
Param defaults_
Container for default parameters. This member should be filled in the constructor of derived classes!...
Definition: DefaultParamHandler.h:155
void setValue(const String &key, const DataValue &value, const String &description="", const StringList &tags=StringList())
Sets a value.
A more convenient string class.
Definition: String.h:56
Param param_
Container for current parameters.
Definition: DefaultParamHandler.h:148
SignalToNoiseEstimator & operator=(const SignalToNoiseEstimator &source)
Assignment operator.
Definition: SignalToNoiseEstimator.h:93
Container::const_iterator PeakIterator
Definition: SignalToNoiseEstimator.h:66
GaussianEstimate estimate_(const PeakIterator &scan_first_, const PeakIterator &scan_last_) const
calculate mean & stdev of intensities of a spectrum
Definition: SignalToNoiseEstimator.h:176
#define LOG_WARN
Macro if a warning, a piece of information which should be read by the user, should be logged...
Definition: LogStream.h:451
bool is_result_valid_
flag: set to true if SignalToNoise estimates are calculated and none of the params were changed...
Definition: SignalToNoiseEstimator.h:216
void setMaxInt(const String &key, Int max)
Sets the maximum value for the integer or integer list parameter key.
protected struct to store parameters my, sigma for a Gaussian distribution
Definition: SignalToNoiseEstimator.h:168
void endProgress() const
Ends the progress display.
const DataValue & getValue(const String &key) const
Returns a value of a parameter.
PeakIterator::value_type PeakType
Definition: SignalToNoiseEstimator.h:67
static StringList create(const String &list, const char splitter= ',')
Returns a list that is created by splitting the given (comma-separated) string (String are not trimme...
Invalid value exception.
Definition: Exception.h:336
void setMinInt(const String &key, Int min)
Sets the minimum value for the integer or integer list parameter key.
This class represents the abstract base class of a signal to noise estimator.
Definition: SignalToNoiseEstimator.h:58
std::map< PeakType, double, typename PeakType::PositionLess > stn_estimates_
stores the noise estimate for each peak
Definition: SignalToNoiseEstimator.h:209
void startProgress(SignedSize begin, SignedSize end, const String &label) const
Initializes the progress display.
void setProgress(SignedSize value) const
Sets the current progress.
void setName(const String &name)
Mutable access to the name.
void setMinFloat(const String &key, DoubleReal min)
Sets the minimum value for the floating point or floating point list parameter key.
void defaultsToParam_()
Updates the parameters after the defaults have been set in the constructor.
void setMaxFloat(const String &key, DoubleReal max)
Sets the maximum value for the floating point or floating point list parameter key.