Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages
LevMarqFitter1D.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-2017.
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: Timo Sachsenberg $
32 // $Authors: $
33 // --------------------------------------------------------------------------
34 
35 #ifndef OPENMS_TRANSFORMATIONS_FEATUREFINDER_LEVMARQFITTER1D_H
36 #define OPENMS_TRANSFORMATIONS_FEATUREFINDER_LEVMARQFITTER1D_H
37 
39 
40 #include <unsupported/Eigen/NonLinearOptimization>
41 
42 #include <algorithm>
43 
45 
46 namespace OpenMS
47 {
48 
52  class OPENMS_DLLAPI LevMarqFitter1D :
53  public Fitter1D
54  {
55 
56 public:
57 
58  typedef std::vector<double> ContainerType;
59 
61  //TODO: This is copy and paste from TraceFitter.h. Make a generic wrapper for LM optimization
63  {
64  public:
65  int inputs() const { return m_inputs; }
66  int values() const { return m_values; }
67 
68  GenericFunctor(int dimensions, int num_data_points)
69  : m_inputs(dimensions), m_values(num_data_points) {}
70 
71  virtual ~GenericFunctor() {}
72 
73  virtual int operator()(const Eigen::VectorXd &x, Eigen::VectorXd &fvec) = 0;
74 
75  // compute Jacobian matrix for the different parameters
76  virtual int df(const Eigen::VectorXd &x, Eigen::MatrixXd &J) = 0;
77 
78  protected:
79  const int m_inputs, m_values;
80  };
81 
84  Fitter1D()
85  {
86  this->defaults_.setValue("max_iteration", 500, "Maximum number of iterations using by Levenberg-Marquardt algorithm.", ListUtils::create<String>("advanced"));
87  }
88 
90  LevMarqFitter1D(const LevMarqFitter1D & source) :
91  Fitter1D(source),
92  max_iteration_(source.max_iteration_)
93  {
94  }
95 
97  virtual ~LevMarqFitter1D()
98  {
99  }
100 
102  virtual LevMarqFitter1D & operator=(const LevMarqFitter1D & source)
103  {
104  if (&source == this) return *this;
105 
106  Fitter1D::operator=(source);
107  max_iteration_ = source.max_iteration_;
108 
109  return *this;
110  }
111 
112 protected:
113 
118 
124  void optimize_(Eigen::VectorXd& x_init, GenericFunctor& functor)
125  {
126  //TODO: this function is copy&paste from TraceFitter.h. Make a generic wrapper for
127  //LM optimization
128  int data_count = functor.values();
129  int num_params = functor.inputs();
130 
131  // LM always expects N>=p, cause Jacobian be rectangular M x N with M>=N
132  if (data_count < num_params) throw Exception::UnableToFit(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "UnableToFit-FinalSet", "Skipping feature, we always expects N>=p");
133 
134  Eigen::LevenbergMarquardt<GenericFunctor> lmSolver (functor);
135  lmSolver.parameters.maxfev = max_iteration_;
136  Eigen::LevenbergMarquardtSpace::Status status = lmSolver.minimize(x_init);
137 
138  //the states are poorly documented. after checking the source, we believe that
139  //all states except NotStarted, Running and ImproperInputParameters are good
140  //termination states.
141  if (status <= Eigen::LevenbergMarquardtSpace::ImproperInputParameters)
142  {
143  throw Exception::UnableToFit(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "UnableToFit-FinalSet", "Could not fit the gaussian to the data: Error " + String(status));
144  }
145  }
146 
148  {
150  max_iteration_ = this->param_.getValue("max_iteration");
151  }
152 
153  };
154 }
155 
156 #endif // OPENMS_TRANSFORMATIONS_FEATUREFINDER_LEVMARQFITTER1D_H
Abstract class for 1D-model fitter using Levenberg-Marquardt algorithm for parameter optimization...
Definition: LevMarqFitter1D.h:52
int inputs() const
Definition: LevMarqFitter1D.h:65
A more convenient string class.
Definition: String.h:57
virtual ~LevMarqFitter1D()
destructor
Definition: LevMarqFitter1D.h:97
Int max_iteration_
Maximum number of iterations.
Definition: LevMarqFitter1D.h:117
GenericFunctor(int dimensions, int num_data_points)
Definition: LevMarqFitter1D.h:68
virtual LevMarqFitter1D & operator=(const LevMarqFitter1D &source)
assignment operator
Definition: LevMarqFitter1D.h:102
LevMarqFitter1D(const LevMarqFitter1D &source)
copy constructor
Definition: LevMarqFitter1D.h:90
Definition: LevMarqFitter1D.h:62
void optimize_(Eigen::VectorXd &x_init, GenericFunctor &functor)
Optimize start parameter.
Definition: LevMarqFitter1D.h:124
const int m_values
Definition: LevMarqFitter1D.h:79
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
virtual Fitter1D & operator=(const Fitter1D &source)
assignment operator
int values() const
Definition: LevMarqFitter1D.h:66
LevMarqFitter1D()
Default constructor.
Definition: LevMarqFitter1D.h:83
bool symmetric_
Parameter indicates symmetric peaks.
Definition: LevMarqFitter1D.h:115
Exception used if an error occurred while fitting a model to a given dataset.
Definition: Exception.h:677
Abstract base class for all 1D-dimensional model fitter.
Definition: Fitter1D.h:59
std::vector< double > ContainerType
Definition: LevMarqFitter1D.h:58
virtual void updateMembers_()
This method is used to update extra member variables at the end of the setParameters() method...
int Int
Signed integer type.
Definition: Types.h:103
virtual ~GenericFunctor()
Definition: LevMarqFitter1D.h:71
void updateMembers_()
This method is used to update extra member variables at the end of the setParameters() method...
Definition: LevMarqFitter1D.h:147

OpenMS / TOPP release 2.3.0 Documentation generated on Tue Jan 9 2018 18:22:01 using doxygen 1.8.13