OpenMS  2.7.0
EmgGradientDescent.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-2021.
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: Douglas McCloskey, Pasquale Domenico Colaianni $
32 // $Authors: Douglas McCloskey, Pasquale Domenico Colaianni $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
37 #include <OpenMS/config.h> // OPENMS_DLLAPI
41 
42 namespace OpenMS
43 {
64  class OPENMS_DLLAPI EmgGradientDescent :
65  public DefaultParamHandler
66  {
67 public:
71  ~EmgGradientDescent() = default;
72 
73  void getDefaultParameters(Param& params);
74 
77 
111  template <typename PeakContainerT>
113  const PeakContainerT& input_peak,
114  PeakContainerT& output_peak,
115  const double left_pos = 0.0,
116  const double right_pos = 0.0
117  ) const;
118 
132  const std::vector<double>& xs,
133  const std::vector<double>& ys,
134  double& best_h,
135  double& best_mu,
136  double& best_sigma,
137  double& best_tau
138  ) const;
139 
155  const std::vector<double>& xs,
156  const double h,
157  const double mu,
158  const double sigma,
159  const double tau,
160  std::vector<double>& out_xs,
161  std::vector<double>& out_ys
162  ) const;
163 
164 protected:
165  void updateMembers_() override;
166 
190  const std::vector<double>& xs,
191  const std::vector<double>& ys,
192  std::vector<double>& TrX,
193  std::vector<double>& TrY
194  ) const;
195 
208  double computeMuMaxDistance(const std::vector<double>& xs) const;
209 
224  const std::vector<double>& xs,
225  const std::vector<double>& ys
226  ) const;
227 
228 private:
247  const double prev_diff_E_param,
248  double& diff_E_param,
249  double& param_lr,
250  double& param_update,
251  double& param,
252  const double current_E,
253  const double previous_E
254  ) const;
255 
272  const std::vector<double>& xs,
273  const std::vector<double>& ys,
274  const double h,
275  const double mu,
276  const double sigma,
277  const double tau
278  ) const;
279 
295  double E_wrt_h(
296  const std::vector<double>& xs,
297  const std::vector<double>& ys,
298  const double h,
299  const double mu,
300  const double sigma,
301  const double tau
302  ) const;
303 
319  double E_wrt_mu(
320  const std::vector<double>& xs,
321  const std::vector<double>& ys,
322  const double h,
323  const double mu,
324  const double sigma,
325  const double tau
326  ) const;
327 
343  double E_wrt_sigma(
344  const std::vector<double>& xs,
345  const std::vector<double>& ys,
346  const double h,
347  const double mu,
348  const double sigma,
349  const double tau
350  ) const;
351 
367  double E_wrt_tau(
368  const std::vector<double>& xs,
369  const std::vector<double>& ys,
370  const double h,
371  const double mu,
372  const double sigma,
373  const double tau
374  ) const;
375 
396  double compute_z(
397  const double x,
398  const double mu,
399  const double sigma,
400  const double tau
401  ) const;
402 
414  double emg_point(
415  const double x,
416  const double h,
417  const double mu,
418  const double sigma,
419  const double tau
420  ) const;
421 
423  const double PI = OpenMS::Constants::PI;
424 
431 
434 
440  };
441 
443  {
444 public:
447 
449  const std::vector<double>& xs,
450  const std::vector<double>& ys,
451  const double h,
452  const double mu,
453  const double sigma,
454  const double tau
455  ) const
456  {
457  return emg_gd_.Loss_function(xs, ys, h, mu, sigma, tau);
458  }
459 
460  double computeMuMaxDistance(const std::vector<double>& xs) const
461  {
462  return emg_gd_.computeMuMaxDistance(xs);
463  }
464 
466  const std::vector<double>& xs,
467  const std::vector<double>& ys,
468  std::vector<double>& TrX,
469  std::vector<double>& TrY
470  ) const
471  {
472  emg_gd_.extractTrainingSet(xs, ys, TrX, TrY);
473  }
474 
476  const std::vector<double>& xs,
477  const std::vector<double>& ys
478  ) const
479  {
480  return emg_gd_.computeInitialMean(xs, ys);
481  }
482 
484  const double prev_diff_E_param,
485  double& diff_E_param,
486  double& param_lr,
487  double& param_update,
488  double& param,
489  const double current_E,
490  const double previous_E
491  ) const
492  {
494  prev_diff_E_param, diff_E_param, param_lr,
495  param_update, param, current_E, previous_E
496  );
497  }
498 
499  double compute_z(
500  const double x,
501  const double mu,
502  const double sigma,
503  const double tau
504  ) const
505  {
506  return emg_gd_.compute_z(x, mu, sigma, tau);
507  }
508 
510  const std::vector<double>& xs,
511  const double h,
512  const double mu,
513  const double sigma,
514  const double tau,
515  std::vector<double>& out_xs,
516  std::vector<double>& out_ys
517  ) const
518  {
519  emg_gd_.applyEstimatedParameters(xs, h, mu, sigma, tau, out_xs, out_ys);
520  }
521 
522  double emg_point(
523  const double x,
524  const double h,
525  const double mu,
526  const double sigma,
527  const double tau
528  ) const
529  {
530  return emg_gd_.emg_point(x, h, mu, sigma, tau);
531  }
532 
534  };
535 }
A base class for all classes handling default parameters.
Definition: DefaultParamHandler.h:93
Definition: EmgGradientDescent.h:443
double computeInitialMean(const std::vector< double > &xs, const std::vector< double > &ys) const
Definition: EmgGradientDescent.h:475
void iRpropPlus(const double prev_diff_E_param, double &diff_E_param, double &param_lr, double &param_update, double &param, const double current_E, const double previous_E) const
Definition: EmgGradientDescent.h:483
double Loss_function(const std::vector< double > &xs, const std::vector< double > &ys, const double h, const double mu, const double sigma, const double tau) const
Definition: EmgGradientDescent.h:448
double emg_point(const double x, const double h, const double mu, const double sigma, const double tau) const
Definition: EmgGradientDescent.h:522
void extractTrainingSet(const std::vector< double > &xs, const std::vector< double > &ys, std::vector< double > &TrX, std::vector< double > &TrY) const
Definition: EmgGradientDescent.h:465
double computeMuMaxDistance(const std::vector< double > &xs) const
Definition: EmgGradientDescent.h:460
void applyEstimatedParameters(const std::vector< double > &xs, const double h, const double mu, const double sigma, const double tau, std::vector< double > &out_xs, std::vector< double > &out_ys) const
Definition: EmgGradientDescent.h:509
double compute_z(const double x, const double mu, const double sigma, const double tau) const
Definition: EmgGradientDescent.h:499
EmgGradientDescent emg_gd_
Definition: EmgGradientDescent.h:533
Compute the area, background and shape metrics of a peak.
Definition: EmgGradientDescent.h:66
double E_wrt_mu(const std::vector< double > &xs, const std::vector< double > &ys, const double h, const double mu, const double sigma, const double tau) const
Compute the cost given by the partial derivative of the loss function E, with respect to `mu` (the me...
UInt print_debug_
Definition: EmgGradientDescent.h:430
double computeInitialMean(const std::vector< double > &xs, const std::vector< double > &ys) const
Compute an estimation of the mean of a peak.
void iRpropPlus(const double prev_diff_E_param, double &diff_E_param, double &param_lr, double &param_update, double &param, const double current_E, const double previous_E) const
Apply the iRprop+ algorithm for gradient descent.
double Loss_function(const std::vector< double > &xs, const std::vector< double > &ys, const double h, const double mu, const double sigma, const double tau) const
Compute the cost given by loss function E.
void fitEMGPeakModel(const PeakContainerT &input_peak, PeakContainerT &output_peak, const double left_pos=0.0, const double right_pos=0.0) const
Fit the given peak (either MSChromatogram or MSSpectrum) to the EMG peak model.
double E_wrt_tau(const std::vector< double > &xs, const std::vector< double > &ys, const double h, const double mu, const double sigma, const double tau) const
Compute the cost given by the partial derivative of the loss function E, with respect to `tau` (the e...
double emg_point(const double x, const double h, const double mu, const double sigma, const double tau) const
Compute the EMG function on a single point.
void extractTrainingSet(const std::vector< double > &xs, const std::vector< double > &ys, std::vector< double > &TrX, std::vector< double > &TrY) const
Given a peak, extract a training set to be used with the gradient descent algorithm.
UInt max_gd_iter_
Maximum number of gradient descent iterations in `fitEMGPeakModel()`.
Definition: EmgGradientDescent.h:433
double E_wrt_h(const std::vector< double > &xs, const std::vector< double > &ys, const double h, const double mu, const double sigma, const double tau) const
Compute the cost given by the partial derivative of the loss function E, with respect to `h` (the amp...
EmgGradientDescent()
Constructor.
~EmgGradientDescent()=default
Destructor.
bool compute_additional_points_
Definition: EmgGradientDescent.h:439
void updateMembers_() override
This method is used to update extra member variables at the end of the setParameters() method.
void getDefaultParameters(Param &params)
double computeMuMaxDistance(const std::vector< double > &xs) const
Compute the boundary for the mean (`mu`) parameter in gradient descent.
void applyEstimatedParameters(const std::vector< double > &xs, const double h, const double mu, const double sigma, const double tau, std::vector< double > &out_xs, std::vector< double > &out_ys) const
Compute the EMG function on a set of points.
double compute_z(const double x, const double mu, const double sigma, const double tau) const
Compute EMG's z parameter.
double E_wrt_sigma(const std::vector< double > &xs, const std::vector< double > &ys, const double h, const double mu, const double sigma, const double tau) const
Compute the cost given by the partial derivative of the loss function E, with respect to `sigma` (the...
UInt estimateEmgParameters(const std::vector< double > &xs, const std::vector< double > &ys, double &best_h, double &best_mu, double &best_sigma, double &best_tau) const
The implementation of the gradient descent algorithm for the EMG peak model.
Management and storage of parameters / INI files.
Definition: Param.h:70
unsigned int UInt
Unsigned integer type.
Definition: Types.h:94
const double PI
PI.
Definition: Constants.h:75
const double h
Definition: Constants.h:162
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47