OpenMS  2.4.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-2018.
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>
112  void fitEMGPeakModel(
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 
119 protected:
120  void updateMembers_();
121 
144  void extractTrainingSet(
145  const std::vector<double>& xs,
146  const std::vector<double>& ys,
147  std::vector<double>& TrX,
148  std::vector<double>& TrY
149  ) const;
150 
163  double computeMuMaxDistance(const std::vector<double>& xs) const;
164 
178  double computeInitialMean(
179  const std::vector<double>& xs,
180  const std::vector<double>& ys
181  ) const;
182 
183 private:
196  UInt emg_gradient_descent(
197  const std::vector<double>& xs,
198  const std::vector<double>& ys,
199  double& best_h,
200  double& best_mu,
201  double& best_sigma,
202  double& best_tau
203  ) const;
204 
222  void iRpropPlus(
223  const double prev_diff_E_param,
224  double& diff_E_param,
225  double& param_lr,
226  double& param_update,
227  double& param,
228  const double current_E,
229  const double previous_E
230  ) const;
231 
247  double Loss_function(
248  const std::vector<double>& xs,
249  const std::vector<double>& ys,
250  const double h,
251  const double mu,
252  const double sigma,
253  const double tau
254  ) const;
255 
271  double E_wrt_h(
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_mu(
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_sigma(
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_tau(
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 
372  double compute_z(
373  const double x,
374  const double mu,
375  const double sigma,
376  const double tau
377  ) const;
378 
393  void emg_vector(
394  const std::vector<double>& xs,
395  const double h,
396  const double mu,
397  const double sigma,
398  const double tau,
399  std::vector<double>& out_xs,
400  std::vector<double>& out_ys
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:
445  EmgGradientDescent_friend() = default;
446  ~EmgGradientDescent_friend() = default;
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_.emg_vector(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 }
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.
void emg_vector(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 emg_point(const double x, const double h, const double mu, const double sigma, const double tau) const
Definition: EmgGradientDescent.h:522
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.
Compute the area, background and shape metrics of a peak.
Definition: EmgGradientDescent.h:64
double compute_z(const double x, const double mu, const double sigma, const double tau) const
Compute EMG&#39;s z parameter.
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 computeInitialMean(const std::vector< double > &xs, const std::vector< double > &ys) const
Definition: EmgGradientDescent.h:475
unsigned int UInt
Unsigned integer type.
Definition: Types.h:94
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 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 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
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
double computeInitialMean(const std::vector< double > &xs, const std::vector< double > &ys) const
Compute an estimation of the mean of a peak.
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
Definition: EmgGradientDescent.h:442
const double h
Management and storage of parameters / INI files.
Definition: Param.h:74
const double PI
PI.
void emg_vector(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 computeMuMaxDistance(const std::vector< double > &xs) const
Compute the boundary for the mean (`mu`) parameter in gradient descent.
EmgGradientDescent emg_gd_
Definition: EmgGradientDescent.h:533
A base class for all classes handling default parameters.
Definition: DefaultParamHandler.h:91
double compute_z(const double x, const double mu, const double sigma, const double tau) const
Definition: EmgGradientDescent.h:499
double computeMuMaxDistance(const std::vector< double > &xs) const
Definition: EmgGradientDescent.h:460
bool compute_additional_points_
Definition: EmgGradientDescent.h:439
UInt print_debug_
Definition: EmgGradientDescent.h:430
UInt max_gd_iter_
Maximum number of gradient descent iterations in `fitEMGPeakModel()`.
Definition: EmgGradientDescent.h:433