OpenMS
SignalToNoiseEstimatorMedianRapid.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // OpenMS -- Open-Source Mass Spectrometry
3 // --------------------------------------------------------------------------
4 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5 // ETH Zurich, and Freie Universitaet Berlin 2002-2023.
6 //
7 // This software is released under a three-clause BSD license:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of any author or any participating institution
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // For a full list of authors, refer to the file AUTHORS.
17 // --------------------------------------------------------------------------
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // --------------------------------------------------------------------------
31 // $Maintainer: Hannes Roest $
32 // $Authors: Hannes Roest $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
39 #include <cassert>
40 #include <vector>
41 
42 namespace OpenMS
43 {
44 
71  {
74 
75 public:
76 
89  struct OPENMS_DLLAPI NoiseEstimator
90  {
94  double mz_start;
96  double window_length;
98  std::vector<double> result_windows_even;
100  std::vector<double> result_windows_odd;
101 
104 
106  NoiseEstimator(double nr_windows_, double mz_start_, double win_len_) :
107  nr_windows(nr_windows_),
108  mz_start(mz_start_),
109  window_length(win_len_),
110  result_windows_even(nr_windows_),
111  result_windows_odd(nr_windows_+1)
112  {}
113 
121  double get_noise_value (double mz)
122  {
123  // Take the average of the two stored values
124  // Avoid division by 0 (since most clients will divide by the noise value)
125  return std::max(1.0, (get_noise_even(mz)+get_noise_odd(mz))/2.0 );
126  }
127 
128  double get_noise_even (double mz)
129  {
130  // PRECONDITION
131  int window_nr = (int)((mz - mz_start)/window_length);
132  assert(window_nr >= 0);
133  assert(window_nr < (int)result_windows_even.size());
134 
135  double noise = result_windows_even[window_nr];
136  return noise;
137  }
138 
139  double get_noise_odd (double mz)
140  {
141  // PRECONDITION
142  int window_nr = (int)((mz - mz_start + window_length/2.0)/window_length);
143  assert(window_nr >= 0);
144  assert(window_nr < (int)result_windows_odd.size());
145 
146  double noise = result_windows_odd[window_nr];
147  return noise;
148  }
149  };
150 
152  SignalToNoiseEstimatorMedianRapid(double window_length) :
153  window_length_(window_length)
154  {
155  }
156 
162  {
163  return estimateNoise(spectrum->getMZArray()->data, spectrum->getIntensityArray()->data);
164  }
165 
171  {
172  return estimateNoise(chrom->getTimeArray()->data, chrom->getIntensityArray()->data);
173  }
174 
179  NoiseEstimator estimateNoise(const std::vector<double>& mz_array, const std::vector<double>& int_array)
180  {
181  // PRECONDITION
182  assert(mz_array.size() == int_array.size());
183  assert(mz_array.size() > 2);
184 
185  int nr_windows = (int)((mz_array[mz_array.size()-1] - mz_array[0])/window_length_) + 1;
186  NoiseEstimator eval(nr_windows, mz_array[0], window_length_);
187 
188  // Compute even windows
189  computeNoiseInWindows_(mz_array, int_array, eval.result_windows_even, mz_array[0]);
190  // Compute odd windows
191  computeNoiseInWindows_(mz_array, int_array, eval.result_windows_odd, mz_array[0] - window_length_/2.0);
192 
193  return eval;
194  }
195 
196 private:
197 
203  void computeNoiseInWindows_(const std::vector<double>& mz_array, std::vector<double> int_array, std::vector<double> & result, double mz_start);
204 
211  double computeMedian_(std::vector<double>::iterator & first, std::vector<double>::iterator & last);
212 
213  };
214 
215 } // namespace OpenMS
216 
217 
Estimates the signal/noise (S/N) ratio of each data point in a scan by using the median (window based...
Definition: SignalToNoiseEstimatorMedianRapid.h:71
double window_length_
Window length parameter.
Definition: SignalToNoiseEstimatorMedianRapid.h:73
NoiseEstimator estimateNoise(OpenMS::Interfaces::SpectrumPtr spectrum)
Compute noise estimator for an m/z and intensity array using windows.
Definition: SignalToNoiseEstimatorMedianRapid.h:161
NoiseEstimator estimateNoise(const std::vector< double > &mz_array, const std::vector< double > &int_array)
Compute noise estimator for an m/z and intensity array using windows.
Definition: SignalToNoiseEstimatorMedianRapid.h:179
SignalToNoiseEstimatorMedianRapid(double window_length)
default constructor
Definition: SignalToNoiseEstimatorMedianRapid.h:152
void computeNoiseInWindows_(const std::vector< double > &mz_array, std::vector< double > int_array, std::vector< double > &result, double mz_start)
Computes the noise in windows for two input arrays and stores the median intensity in the result (int...
double computeMedian_(std::vector< double >::iterator &first, std::vector< double >::iterator &last)
Median computation on a part of an array [first,last)
NoiseEstimator estimateNoise(OpenMS::Interfaces::ChromatogramPtr chrom)
Compute noise estimator for an m/z and intensity array using windows.
Definition: SignalToNoiseEstimatorMedianRapid.h:170
boost::shared_ptr< Chromatogram > ChromatogramPtr
Definition: openms/include/OpenMS/INTERFACES/DataStructures.h:156
boost::shared_ptr< Spectrum > SpectrumPtr
Definition: openms/include/OpenMS/INTERFACES/DataStructures.h:236
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:48
Class to compute the noise value at a given position.
Definition: SignalToNoiseEstimatorMedianRapid.h:90
NoiseEstimator(double nr_windows_, double mz_start_, double win_len_)
Constructor.
Definition: SignalToNoiseEstimatorMedianRapid.h:106
double window_length
Length of the window in m/z direction.
Definition: SignalToNoiseEstimatorMedianRapid.h:96
double get_noise_even(double mz)
Definition: SignalToNoiseEstimatorMedianRapid.h:128
double get_noise_value(double mz)
Return the noise value at a given m/z position.
Definition: SignalToNoiseEstimatorMedianRapid.h:121
NoiseEstimator()
Constructor.
Definition: SignalToNoiseEstimatorMedianRapid.h:103
double mz_start
Start of m/z domain.
Definition: SignalToNoiseEstimatorMedianRapid.h:94
std::vector< double > result_windows_odd
Noise values for window starting at mz_start - 0.5 * window_length (length = nr_windows + 1)
Definition: SignalToNoiseEstimatorMedianRapid.h:100
std::vector< double > result_windows_even
Noise values for window starting at mz_start (length = nr_windows)
Definition: SignalToNoiseEstimatorMedianRapid.h:98
double get_noise_odd(double mz)
Definition: SignalToNoiseEstimatorMedianRapid.h:139
int nr_windows
Number of windows in m/z direction for which noise values are stored.
Definition: SignalToNoiseEstimatorMedianRapid.h:92