OpenMS
EmgGradientDescent.h
Go to the documentation of this file.
1 // Copyright (c) 2002-2023, The OpenMS Team -- EKU Tuebingen, ETH Zurich, and FU Berlin
2 // SPDX-License-Identifier: BSD-3-Clause
3 //
4 // --------------------------------------------------------------------------
5 // $Maintainer: Douglas McCloskey, Pasquale Domenico Colaianni $
6 // $Authors: Douglas McCloskey, Pasquale Domenico Colaianni $
7 // --------------------------------------------------------------------------
8 
9 #pragma once
10 
11 #include <OpenMS/config.h> // OPENMS_DLLAPI
15 
16 namespace OpenMS
17 {
38  class OPENMS_DLLAPI EmgGradientDescent :
39  public DefaultParamHandler
40  {
41 public:
45  ~EmgGradientDescent() override = default;
46 
47  void getDefaultParameters(Param& params);
48 
51 
85  template <typename PeakContainerT>
87  const PeakContainerT& input_peak,
88  PeakContainerT& output_peak,
89  const double left_pos = 0.0,
90  const double right_pos = 0.0
91  ) const;
92 
106  const std::vector<double>& xs,
107  const std::vector<double>& ys,
108  double& best_h,
109  double& best_mu,
110  double& best_sigma,
111  double& best_tau
112  ) const;
113 
129  const std::vector<double>& xs,
130  const double h,
131  const double mu,
132  const double sigma,
133  const double tau,
134  std::vector<double>& out_xs,
135  std::vector<double>& out_ys
136  ) const;
137 
138 protected:
139  void updateMembers_() override;
140 
164  const std::vector<double>& xs,
165  const std::vector<double>& ys,
166  std::vector<double>& TrX,
167  std::vector<double>& TrY
168  ) const;
169 
182  double computeMuMaxDistance(const std::vector<double>& xs) const;
183 
198  const std::vector<double>& xs,
199  const std::vector<double>& ys
200  ) const;
201 
202 private:
221  const double prev_diff_E_param,
222  double& diff_E_param,
223  double& param_lr,
224  double& param_update,
225  double& param,
226  const double current_E,
227  const double previous_E
228  ) const;
229 
246  const std::vector<double>& xs,
247  const std::vector<double>& ys,
248  const double h,
249  const double mu,
250  const double sigma,
251  const double tau
252  ) const;
253 
269  double E_wrt_h(
270  const std::vector<double>& xs,
271  const std::vector<double>& ys,
272  const double h,
273  const double mu,
274  const double sigma,
275  const double tau
276  ) const;
277 
293  double E_wrt_mu(
294  const std::vector<double>& xs,
295  const std::vector<double>& ys,
296  const double h,
297  const double mu,
298  const double sigma,
299  const double tau
300  ) const;
301 
317  double E_wrt_sigma(
318  const std::vector<double>& xs,
319  const std::vector<double>& ys,
320  const double h,
321  const double mu,
322  const double sigma,
323  const double tau
324  ) const;
325 
341  double E_wrt_tau(
342  const std::vector<double>& xs,
343  const std::vector<double>& ys,
344  const double h,
345  const double mu,
346  const double sigma,
347  const double tau
348  ) const;
349 
370  double compute_z(
371  const double x,
372  const double mu,
373  const double sigma,
374  const double tau
375  ) const;
376 
388  double emg_point(
389  const double x,
390  const double h,
391  const double mu,
392  const double sigma,
393  const double tau
394  ) const;
395 
397  const double PI = OpenMS::Constants::PI;
398 
405 
408 
414  };
415 
417  {
418 public:
421 
423  const std::vector<double>& xs,
424  const std::vector<double>& ys,
425  const double h,
426  const double mu,
427  const double sigma,
428  const double tau
429  ) const
430  {
431  return emg_gd_.Loss_function(xs, ys, h, mu, sigma, tau);
432  }
433 
434  double computeMuMaxDistance(const std::vector<double>& xs) const
435  {
436  return emg_gd_.computeMuMaxDistance(xs);
437  }
438 
440  const std::vector<double>& xs,
441  const std::vector<double>& ys,
442  std::vector<double>& TrX,
443  std::vector<double>& TrY
444  ) const
445  {
446  emg_gd_.extractTrainingSet(xs, ys, TrX, TrY);
447  }
448 
450  const std::vector<double>& xs,
451  const std::vector<double>& ys
452  ) const
453  {
454  return emg_gd_.computeInitialMean(xs, ys);
455  }
456 
458  const double prev_diff_E_param,
459  double& diff_E_param,
460  double& param_lr,
461  double& param_update,
462  double& param,
463  const double current_E,
464  const double previous_E
465  ) const
466  {
468  prev_diff_E_param, diff_E_param, param_lr,
469  param_update, param, current_E, previous_E
470  );
471  }
472 
473  double compute_z(
474  const double x,
475  const double mu,
476  const double sigma,
477  const double tau
478  ) const
479  {
480  return emg_gd_.compute_z(x, mu, sigma, tau);
481  }
482 
484  const std::vector<double>& xs,
485  const double h,
486  const double mu,
487  const double sigma,
488  const double tau,
489  std::vector<double>& out_xs,
490  std::vector<double>& out_ys
491  ) const
492  {
493  emg_gd_.applyEstimatedParameters(xs, h, mu, sigma, tau, out_xs, out_ys);
494  }
495 
496  double emg_point(
497  const double x,
498  const double h,
499  const double mu,
500  const double sigma,
501  const double tau
502  ) const
503  {
504  return emg_gd_.emg_point(x, h, mu, sigma, tau);
505  }
506 
508  };
509 }
A base class for all classes handling default parameters.
Definition: DefaultParamHandler.h:66
Definition: EmgGradientDescent.h:417
double computeInitialMean(const std::vector< double > &xs, const std::vector< double > &ys) const
Definition: EmgGradientDescent.h:449
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:457
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:422
double emg_point(const double x, const double h, const double mu, const double sigma, const double tau) const
Definition: EmgGradientDescent.h:496
void extractTrainingSet(const std::vector< double > &xs, const std::vector< double > &ys, std::vector< double > &TrX, std::vector< double > &TrY) const
Definition: EmgGradientDescent.h:439
double computeMuMaxDistance(const std::vector< double > &xs) const
Definition: EmgGradientDescent.h:434
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:483
double compute_z(const double x, const double mu, const double sigma, const double tau) const
Definition: EmgGradientDescent.h:473
EmgGradientDescent emg_gd_
Definition: EmgGradientDescent.h:507
Compute the area, background and shape metrics of a peak.
Definition: EmgGradientDescent.h:40
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:404
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:407
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.
bool compute_additional_points_
Definition: EmgGradientDescent.h:413
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.
~EmgGradientDescent() override=default
Destructor.
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:44
unsigned int UInt
Unsigned integer type.
Definition: Types.h:68
const double PI
PI.
Definition: Constants.h:49
const double h
Definition: Constants.h:141
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:22