OpenMS
Loading...
Searching...
No Matches
SignalToNoiseEstimatorMedianRapid.h
Go to the documentation of this file.
1// Copyright (c) 2002-present, OpenMS Inc. -- EKU Tuebingen, ETH Zurich, and FU Berlin
2// SPDX-License-Identifier: BSD-3-Clause
3//
4// --------------------------------------------------------------------------
5// $Maintainer: Hannes Roest $
6// $Authors: Hannes Roest $
7// --------------------------------------------------------------------------
8
9#pragma once
10
14#include <cassert>
15#include <vector>
16
17namespace OpenMS
18{
19
44 {
47
48public:
49
62 struct OPENMS_DLLAPI NoiseEstimator
63 {
67 double mz_start;
71 std::vector<double> result_windows_even;
73 std::vector<double> result_windows_odd;
74
77
79 NoiseEstimator(double nr_windows_, double mz_start_, double win_len_) :
80 nr_windows(nr_windows_),
81 mz_start(mz_start_),
82 window_length(win_len_),
83 result_windows_even(nr_windows_),
84 result_windows_odd(nr_windows_+1)
85 {}
86
94 double get_noise_value (double mz)
95 {
96 // Take the average of the two stored values
97 // Avoid division by 0 (since most clients will divide by the noise value)
98 return std::max(1.0, (get_noise_even(mz)+get_noise_odd(mz))/2.0 );
99 }
100
107 double get_noise_even (double mz)
108 {
109 return result_windows_even[windowIndex_((mz - mz_start) / window_length, result_windows_even.size())];
110 }
111
118 double get_noise_odd (double mz)
119 {
120 return result_windows_odd[windowIndex_((mz - mz_start + window_length / 2.0) / window_length, result_windows_odd.size())];
121 }
122
123 private:
131 static Size windowIndex_(double window, Size n_windows)
132 {
133 if (!(window > -1.0 && window < static_cast<double>(n_windows)))
134 {
135 throwWindowOutOfRange_(window, n_windows);
136 }
137 // convert via a signed integer: a single instruction on x86, unlike double -> unsigned
138 return static_cast<Size>(static_cast<SignedSize>(window));
139 }
140
141 [[noreturn]] static void throwWindowOutOfRange_(double window, Size n_windows);
142 };
143
145 SignalToNoiseEstimatorMedianRapid(double window_length) :
146 window_length_(window_length)
147 {
148 }
149
155 {
156 return estimateNoise(spectrum->getMZArray()->data, spectrum->getIntensityArray()->data);
157 }
158
164 {
165 return estimateNoise(chrom->getTimeArray()->data, chrom->getIntensityArray()->data);
166 }
167
174 NoiseEstimator estimateNoise(const std::vector<double>& mz_array, const std::vector<double>& int_array)
175 {
176 // PRECONDITION
177 checkInputArrays_(mz_array, int_array);
178 assert(mz_array.size() > 2);
179
180 int nr_windows = (int)((mz_array[mz_array.size()-1] - mz_array[0])/window_length_) + 1;
181 NoiseEstimator eval(nr_windows, mz_array[0], window_length_);
182
183 // Compute even windows
184 computeNoiseInWindows_(mz_array, int_array, eval.result_windows_even, mz_array[0]);
185 // Compute odd windows
186 computeNoiseInWindows_(mz_array, int_array, eval.result_windows_odd, mz_array[0] - window_length_/2.0);
187
188 return eval;
189 }
190
191private:
192
194 static void checkInputArrays_(const std::vector<double>& mz_array, const std::vector<double>& int_array);
195
226 void computeNoiseInWindows_(const std::vector<double>& mz_array, std::vector<double> int_array, std::vector<double> & result, double mz_start);
227
234 double computeMedian_(std::vector<double>::iterator & first, std::vector<double>::iterator & last);
235
236 };
237
238} // namespace OpenMS
239
240
Estimates the signal/noise (S/N) ratio of each data point in a scan by using the median (window based...
Definition SignalToNoiseEstimatorMedianRapid.h:44
double window_length_
Window length parameter.
Definition SignalToNoiseEstimatorMedianRapid.h:46
NoiseEstimator estimateNoise(OpenMS::Interfaces::SpectrumPtr spectrum)
Compute noise estimator for an m/z and intensity array using windows.
Definition SignalToNoiseEstimatorMedianRapid.h:154
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:174
SignalToNoiseEstimatorMedianRapid(double window_length)
default constructor
Definition SignalToNoiseEstimatorMedianRapid.h:145
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...
static void checkInputArrays_(const std::vector< double > &mz_array, const std::vector< double > &int_array)
Throws Exception::InvalidParameter if the arrays are empty or differ in length.
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:163
OpenMS looks in the< code > OPENMS_THERMO_MANAGED_DIR</code > environment variable first
Definition common-cmake-parameters.doxygen:179
ptrdiff_t SignedSize
Signed Size type e.g. used as pointer difference.
Definition Types.h:104
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition Types.h:97
std::shared_ptr< Chromatogram > ChromatogramPtr
Definition openms/include/OpenMS/INTERFACES/DataStructures.h:130
std::shared_ptr< Spectrum > SpectrumPtr
Definition openms/include/OpenMS/INTERFACES/DataStructures.h:210
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
Class to compute the noise value at a given position.
Definition SignalToNoiseEstimatorMedianRapid.h:63
NoiseEstimator(double nr_windows_, double mz_start_, double win_len_)
Constructor.
Definition SignalToNoiseEstimatorMedianRapid.h:79
double window_length
Length of the window in m/z direction.
Definition SignalToNoiseEstimatorMedianRapid.h:69
double get_noise_even(double mz)
Return the noise value of the even window that contains mz.
Definition SignalToNoiseEstimatorMedianRapid.h:107
static Size windowIndex_(double window, Size n_windows)
Convert a fractional window position into a checked window index.
Definition SignalToNoiseEstimatorMedianRapid.h:131
double get_noise_value(double mz)
Return the noise value at a given m/z position.
Definition SignalToNoiseEstimatorMedianRapid.h:94
NoiseEstimator()
Constructor.
Definition SignalToNoiseEstimatorMedianRapid.h:76
static void throwWindowOutOfRange_(double window, Size n_windows)
double mz_start
Start of m/z domain.
Definition SignalToNoiseEstimatorMedianRapid.h:67
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:73
std::vector< double > result_windows_even
Noise values for window starting at mz_start (length = nr_windows)
Definition SignalToNoiseEstimatorMedianRapid.h:71
double get_noise_odd(double mz)
Return the noise value of the odd window (shifted by half a window length) that contains mz.
Definition SignalToNoiseEstimatorMedianRapid.h:118
int nr_windows
Number of windows in m/z direction for which noise values are stored.
Definition SignalToNoiseEstimatorMedianRapid.h:65