00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #ifndef OPENMS_FILTERING_NOISEESTIMATION_SIGNALTONOISEESTIMATORMEDIAN_H
00037 #define OPENMS_FILTERING_NOISEESTIMATION_SIGNALTONOISEESTIMATORMEDIAN_H
00038
00039
00040 #include <OpenMS/FILTERING/NOISEESTIMATION/SignalToNoiseEstimator.h>
00041 #include <OpenMS/CONCEPT/LogStream.h>
00042 #include <OpenMS/CONCEPT/Exception.h>
00043 #include <vector>
00044
00045 namespace OpenMS
00046 {
00070 template <typename Container = MSSpectrum<> >
00071 class SignalToNoiseEstimatorMedian :
00072 public SignalToNoiseEstimator<Container>
00073 {
00074
00075 public:
00076
00078 enum IntensityThresholdCalculation {MANUAL = -1, AUTOMAXBYSTDEV = 0, AUTOMAXBYPERCENT = 1};
00079
00080 using SignalToNoiseEstimator<Container>::stn_estimates_;
00081 using SignalToNoiseEstimator<Container>::first_;
00082 using SignalToNoiseEstimator<Container>::last_;
00083 using SignalToNoiseEstimator<Container>::is_result_valid_;
00084 using SignalToNoiseEstimator<Container>::defaults_;
00085 using SignalToNoiseEstimator<Container>::param_;
00086
00087 typedef typename SignalToNoiseEstimator<Container>::PeakIterator PeakIterator;
00088 typedef typename SignalToNoiseEstimator<Container>::PeakType PeakType;
00089
00090 typedef typename SignalToNoiseEstimator<Container>::GaussianEstimate GaussianEstimate;
00091
00093 inline SignalToNoiseEstimatorMedian()
00094 {
00095
00096 this->setName("SignalToNoiseEstimatorMedian");
00097
00098 defaults_.setValue("max_intensity", -1, "maximal intensity considered for histogram construction. By default, it will be calculated automatically (see auto_mode)." \
00099 " Only provide this parameter if you know what you are doing (and change 'auto_mode' to '-1')!" \
00100 " All intensities EQUAL/ABOVE 'max_intensity' will be added to the LAST histogram bin." \
00101 " If you choose 'max_intensity' too small, the noise estimate might be too small as well. " \
00102 " If chosen too big, the bins become quite large (which you could counter by increasing 'bin_count', which increases runtime)." \
00103 " In general, the Median-S/N estimator is more robust to a manual max_intensity than the MeanIterative-S/N.", StringList::create("advanced"));
00104 defaults_.setMinInt("max_intensity", -1);
00105
00106 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"));
00107 defaults_.setMinFloat("auto_max_stdev_factor", 0.0);
00108 defaults_.setMaxFloat("auto_max_stdev_factor", 999.0);
00109
00110 defaults_.setValue("auto_max_percentile", 95, "parameter for 'max_intensity' estimation (if 'auto_mode' == 1): auto_max_percentile th percentile", StringList::create("advanced"));
00111 defaults_.setMinInt("auto_max_percentile", 0);
00112 defaults_.setMaxInt("auto_max_percentile", 100);
00113
00114 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"));
00115 defaults_.setMinInt("auto_mode", -1);
00116 defaults_.setMaxInt("auto_mode", 1);
00117
00118 defaults_.setValue("win_len", 200.0, "window length in Thomson");
00119 defaults_.setMinFloat("win_len", 1.0);
00120
00121 defaults_.setValue("bin_count", 30, "number of bins for intensity values");
00122 defaults_.setMinInt("bin_count", 3);
00123
00124 defaults_.setValue("min_required_elements", 10, "minimum number of elements required in a window (otherwise it is considered sparse)");
00125 defaults_.setMinInt("min_required_elements", 1);
00126
00127 defaults_.setValue("noise_for_empty_window", std::pow(10.0, 20), "noise value used for sparse windows", StringList::create("advanced"));
00128
00129
00130 SignalToNoiseEstimator<Container>::defaultsToParam_();
00131 }
00132
00134 inline SignalToNoiseEstimatorMedian(const SignalToNoiseEstimatorMedian & source) :
00135 SignalToNoiseEstimator<Container>(source)
00136 {
00137 updateMembers_();
00138 }
00139
00143
00144 inline SignalToNoiseEstimatorMedian & operator=(const SignalToNoiseEstimatorMedian & source)
00145 {
00146 if (&source == this) return *this;
00147
00148 SignalToNoiseEstimator<Container>::operator=(source);
00149 updateMembers_();
00150 return *this;
00151 }
00152
00154
00155
00157 virtual ~SignalToNoiseEstimatorMedian()
00158 {}
00159
00160
00161 protected:
00162
00163
00169 void computeSTN_(const PeakIterator & scan_first_, const PeakIterator & scan_last_)
00170 {
00171
00172 double sparse_window_percent = 0;
00173
00174 double histogram_oob_percent = 0;
00175
00176
00177 stn_estimates_.clear();
00178
00179
00180 if (auto_mode_ == AUTOMAXBYSTDEV)
00181 {
00182
00183 GaussianEstimate gauss_global = SignalToNoiseEstimator<Container>::estimate_(scan_first_, scan_last_);
00184 max_intensity_ = gauss_global.mean + std::sqrt(gauss_global.variance) * auto_max_stdev_Factor_;
00185 }
00186 else if (auto_mode_ == AUTOMAXBYPERCENT)
00187 {
00188
00189
00190 if ((auto_max_percentile_ < 0) || (auto_max_percentile_ > 100))
00191 {
00192 String s = auto_max_percentile_;
00193 throw Exception::InvalidValue(__FILE__,
00194 __LINE__,
00195 __PRETTY_FUNCTION__,
00196 "auto_mode is on AUTOMAXBYPERCENT! auto_max_percentile is not in [0,100]. Use setAutoMaxPercentile(<value>) to change it!",
00197 s);
00198 }
00199
00200 std::vector<int> histogram_auto(100, 0);
00201
00202
00203 int size = 0;
00204 typename PeakType::IntensityType maxInt = 0;
00205 PeakIterator run = scan_first_;
00206 while (run != scan_last_)
00207 {
00208 maxInt = std::max(maxInt, (*run).getIntensity());
00209 ++size;
00210 ++run;
00211 }
00212
00213 double bin_size = maxInt / 100;
00214
00215
00216 run = scan_first_;
00217 while (run != scan_last_)
00218 {
00219 ++histogram_auto[(int) (((*run).getIntensity() - 1) / bin_size)];
00220 ++run;
00221 }
00222
00223
00224 int elements_below_percentile = (int) (auto_max_percentile_ * size / 100);
00225 int elements_seen = 0;
00226 int i = -1;
00227 run = scan_first_;
00228
00229 while (run != scan_last_ && elements_seen < elements_below_percentile)
00230 {
00231 ++i;
00232 elements_seen += histogram_auto[i];
00233 ++run;
00234 }
00235
00236 max_intensity_ = (((double)i) + 0.5) * bin_size;
00237 }
00238 else
00239 {
00240 if (max_intensity_ <= 0)
00241 {
00242 String s = max_intensity_;
00243 throw Exception::InvalidValue(__FILE__,
00244 __LINE__,
00245 __PRETTY_FUNCTION__,
00246 "auto_mode is on MANUAL! max_intensity is <=0. Needs to be positive! Use setMaxIntensity(<value>) or enable auto_mode!",
00247 s);
00248 }
00249 }
00250
00251 if (max_intensity_ < 0)
00252 {
00253 std::cerr << "TODO SignalToNoiseEstimatorMedian: the max_intensity_ value should be positive! " << max_intensity_ << std::endl;
00254 return;
00255 }
00256
00257 PeakIterator window_pos_center = scan_first_;
00258 PeakIterator window_pos_borderleft = scan_first_;
00259 PeakIterator window_pos_borderright = scan_first_;
00260
00261 double window_half_size = win_len_ / 2;
00262 double bin_size = std::max(1.0, max_intensity_ / bin_count_);
00263 int bin_count_minus_1 = bin_count_ - 1;
00264
00265 std::vector<int> histogram(bin_count_, 0);
00266 std::vector<double> bin_value(bin_count_, 0);
00267
00268 for (int bin = 0; bin < bin_count_; bin++)
00269 {
00270 histogram[bin] = 0;
00271 bin_value[bin] = (bin + 0.5) * bin_size;
00272 }
00273
00274 int to_bin = 0;
00275
00276
00277 int median_bin = 0;
00278
00279 int element_inc_count = 0;
00280
00281
00282 int elements_in_window = 0;
00283
00284 int window_count = 0;
00285
00286
00287 int element_in_window_half = 0;
00288
00289 double noise;
00290
00291
00292 int windows_overall = 0;
00293 PeakIterator run = scan_first_;
00294 while (run != scan_last_)
00295 {
00296 ++windows_overall;
00297 ++run;
00298 }
00299 SignalToNoiseEstimator<Container>::startProgress(0, windows_overall, "noise estimation of data");
00300
00301
00302 while (window_pos_center != scan_last_)
00303 {
00304
00305
00306 while ((*window_pos_borderleft).getMZ() < (*window_pos_center).getMZ() - window_half_size)
00307 {
00308 to_bin = std::max(std::min<int>((int)((*window_pos_borderleft).getIntensity() / bin_size), bin_count_minus_1), 0);
00309 --histogram[to_bin];
00310 --elements_in_window;
00311 ++window_pos_borderleft;
00312 }
00313
00314
00315 while ((window_pos_borderright != scan_last_)
00316 && ((*window_pos_borderright).getMZ() <= (*window_pos_center).getMZ() + window_half_size))
00317 {
00318
00319 to_bin = std::max(std::min<int>((int)((*window_pos_borderright).getIntensity() / bin_size), bin_count_minus_1), 0);
00320 ++histogram[to_bin];
00321 ++elements_in_window;
00322 ++window_pos_borderright;
00323 }
00324
00325 if (elements_in_window < min_required_elements_)
00326 {
00327 noise = noise_for_empty_window_;
00328 ++sparse_window_percent;
00329 }
00330 else
00331 {
00332
00333 median_bin = -1;
00334 element_inc_count = 0;
00335 element_in_window_half = (elements_in_window + 1) / 2;
00336 while (median_bin < bin_count_minus_1 && element_inc_count < element_in_window_half)
00337 {
00338 ++median_bin;
00339 element_inc_count += histogram[median_bin];
00340 }
00341
00342
00343 if (median_bin == bin_count_minus_1) {++histogram_oob_percent; }
00344
00345
00346 noise = std::max(1.0, bin_value[median_bin]);
00347 }
00348
00349
00350 stn_estimates_[*window_pos_center] = (*window_pos_center).getIntensity() / noise;
00351
00352
00353
00354 ++window_pos_center;
00355 ++window_count;
00356
00357 SignalToNoiseEstimator<Container>::setProgress(window_count);
00358
00359 }
00360
00361 SignalToNoiseEstimator<Container>::endProgress();
00362
00363 sparse_window_percent = sparse_window_percent * 100 / window_count;
00364 histogram_oob_percent = histogram_oob_percent * 100 / window_count;
00365
00366
00367 if (sparse_window_percent > 20)
00368 {
00369 LOG_WARN << "WARNING in SignalToNoiseEstimatorMedian: "
00370 << sparse_window_percent
00371 << "% of all windows were sparse. You should consider increasing 'win_len' or decreasing 'min_required_elements'"
00372 << std::endl;
00373 }
00374
00375
00376 if (histogram_oob_percent > 1)
00377 {
00378 LOG_WARN << "WARNING in SignalToNoiseEstimatorMedian: "
00379 << histogram_oob_percent
00380 << "% of all Signal-to-Noise estimates are too high, because the median was found in the rightmost histogram-bin. "
00381 << "You should consider increasing 'max_intensity' (and maybe 'bin_count' with it, to keep bin width reasonable)"
00382 << std::endl;
00383 }
00384
00385 }
00386
00388 void updateMembers_()
00389 {
00390 max_intensity_ = (double)param_.getValue("max_intensity");
00391 auto_max_stdev_Factor_ = (double)param_.getValue("auto_max_stdev_factor");
00392 auto_max_percentile_ = param_.getValue("auto_max_percentile");
00393 auto_mode_ = param_.getValue("auto_mode");
00394 win_len_ = (double)param_.getValue("win_len");
00395 bin_count_ = param_.getValue("bin_count");
00396 min_required_elements_ = param_.getValue("min_required_elements");
00397 noise_for_empty_window_ = (double)param_.getValue("noise_for_empty_window");
00398 is_result_valid_ = false;
00399 }
00400
00402 double max_intensity_;
00404 double auto_max_stdev_Factor_;
00406 double auto_max_percentile_;
00408 int auto_mode_;
00410 double win_len_;
00412 int bin_count_;
00414 int min_required_elements_;
00417 double noise_for_empty_window_;
00418
00419
00420
00421 };
00422
00423 }
00424
00425 #endif //OPENMS_FILTERING_NOISEESTIMATION_DSIGNALTONOISEESTIMATORMEDIAN_H