~valavanisalex/ubuntu/maverick/scidavis/scidavis-fix-565206

« back to all changes in this revision

Viewing changes to scidavis/src/future/core/AbstractFit.h

  • Committer: Bazaar Package Importer
  • Author(s): Ruben Molina
  • Date: 2009-09-06 11:34:04 UTC
  • Revision ID: james.westby@ubuntu.com-20090906113404-4awaey82l3686w4q
Tags: upstream-0.2.3
ImportĀ upstreamĀ versionĀ 0.2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
    File                 : AbstractFit.h
 
3
    Project              : SciDAVis
 
4
    --------------------------------------------------------------------
 
5
    Copyright            : (C) 2007-2009 by Knut Franke
 
6
    Email (use @ for *)  : knut.franke*gmx.de
 
7
    Description          : Base class for doing fits using the algorithms
 
8
                           provided by GSL.
 
9
 
 
10
 ***************************************************************************/
 
11
 
 
12
/***************************************************************************
 
13
 *                                                                         *
 
14
 *  This program is free software; you can redistribute it and/or modify   *
 
15
 *  it under the terms of the GNU General Public License as published by   *
 
16
 *  the Free Software Foundation; either version 2 of the License, or      *
 
17
 *  (at your option) any later version.                                    *
 
18
 *                                                                         *
 
19
 *  This program is distributed in the hope that it will be useful,        *
 
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
 
22
 *  GNU General Public License for more details.                           *
 
23
 *                                                                         *
 
24
 *   You should have received a copy of the GNU General Public License     *
 
25
 *   along with this program; if not, write to the Free Software           *
 
26
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
 
27
 *   Boston, MA  02110-1301  USA                                           *
 
28
 *                                                                         *
 
29
 ***************************************************************************/
 
30
#ifndef ABSTRACT_FIT_H
 
31
#define ABSTRACT_FIT_H
 
32
 
 
33
#include "AbstractFilter.h"
 
34
 
 
35
class gsl_vector;
 
36
class gsl_matrix;
 
37
 
 
38
class FitSetAlgorithmCmd;
 
39
 
 
40
/**
 
41
 * \brief Base class for doing fits using the algorithms provided by GSL.
 
42
 *
 
43
 * Implementations of AbstractFit essentially provide methods to compute a specific function to be
 
44
 * fitted, its derivatives with respect to the fit parameters and sensible estimates for the
 
45
 * parameters (given the data to be fitted).
 
46
 *
 
47
 * Input is accepted on two ports: X and Y. Y is assumed to be a function of X, which is
 
48
 * approximated by the function specified by the implementation of AbstractFit being used.
 
49
 *
 
50
 * On its output ports, AbstractFit provides (in order) the current values of the parameters,
 
51
 * their names, and textual descriptions. Current values can be initial values specified by
 
52
 * the user, automated estimates made by the implementation, or the results of the fit.
 
53
 * You only ever get to see the first two when you haven't connected the input ports of
 
54
 * AbstractFit yet, because after the first connect (and possibly subsequent calls to
 
55
 * setInitialValue()), the fit is automatically recomputed. After calling setAutoRefit(),
 
56
 * it is also recomputed whenever the input data changes.
 
57
 */
 
58
class AbstractFit : public AbstractFilter
 
59
{
 
60
        public:
 
61
                enum Algorithm { ScaledLevenbergMarquardt, UnscaledLevenbergMarquardt, NelderMeadSimplex };
 
62
                //! Possible sources for weighting data.
 
63
                enum Weighting {
 
64
                        NoWeighting, //!< Use 1.0 as weight for all points (=> cannot produce sensible error estimates)
 
65
                        Poisson,     //!< Assume data follows Poisson distribution (weight with sqrt(N)).
 
66
                        Dataset      //!< Use weighting data from a user-specified data source.
 
67
                };
 
68
                static QString nameOf(Algorithm algo) {
 
69
                        switch(algo) {
 
70
                                case ScaledLevenbergMarquardt: return tr("scaled Levenberg-Marquardt");
 
71
                                case UnscaledLevenbergMarquardt: return tr("unscaled Levenberg-Marquardt");
 
72
                                case NelderMeadSimplex: return tr("Nelder-Mead / simplex");
 
73
                        }
 
74
                };
 
75
                static QString nameOf(Weighting weighting) {
 
76
                        switch(weighting) {
 
77
                                case NoWeighting: return tr("no weighting");
 
78
                                case Poisson: return tr("Poisson / sqrt(N)");
 
79
                                case Dataset: return tr("user-supplied");
 
80
                        }
 
81
                }
 
82
                AbstractFit() : d_algorithm(ScaledLevenbergMarquardt), d_tolerance(1e-8), d_maxiter(1000), d_auto_refit(false), d_outdated(true) {}
 
83
                virtual ~AbstractFit() {}
 
84
                bool isOutdated() const { return d_outdated; }
 
85
                void setAlgorithm(Algorithm a) { d_algorithm = a; }
 
86
                void setAutoRefit(bool auto_refit = true) { d_auto_refit = auto_refit; }
 
87
 
 
88
                //!\name Handling of fit parameters
 
89
                //@{
 
90
                void setInitialValue(int parameter, double value);
 
91
                virtual void guessInitialValues() = 0;
 
92
                //@}
 
93
 
 
94
                //!\name Reimplemented from AbstractFilter
 
95
                //@{
 
96
        public:
 
97
                virtual int inputCount() const { return 2; }
 
98
                virtual QString inputName(int port) const { return port==0 ? "X" : "Y"; }
 
99
                virtual int outputCount() const { return 3; }
 
100
        protected:
 
101
                virtual void dataChanged(AbstractDataSource*) {
 
102
                }
 
103
                //@}
 
104
 
 
105
        protected:
 
106
                static virtual double fitFunctionSimplex(const gsl_vector*, void*) = 0;
 
107
                static virtual int fitFunction(const gsl_vector*, void*, gsl_vector*) = 0;
 
108
                static virtual int fitFunctionDf(const gsl_vector*, void*, gsl_matrix*) = 0;
 
109
                static virtual int fitFunctionFdf(const gsl_vector*, void*, gsl_vector*, gsl_matrix*) {
 
110
                }
 
111
 
 
112
        private:
 
113
                //! Fit algorithm to use.
 
114
                Algorithm d_algorithm;
 
115
                //! Where to take weights from.
 
116
                WeightingMethod d_weighting;
 
117
                //! The tolerance ("epsilon") to be used for deciding when the fit was successful.
 
118
                double d_tolerance;
 
119
                //! The maximum number of iterations to do before declaring the fit to have failed.
 
120
                int d_maxiter;
 
121
                //! Whether to redo the fit each time input data changes.
 
122
                bool d_auto_refit;
 
123
                //! If #d_auto_refit is false, this is true when the input data has changed since the last fit.
 
124
                bool d_outdated;
 
125
 
 
126
        friend class FitSetAlgorithmCmd;
 
127
};
 
128
 
 
129
#endif // ifndef ABSTRACT_FIT_H