Skip to content

Commit

Permalink
Added more line breaks in the code. Re #7220.
Browse files Browse the repository at this point in the history
  • Loading branch information
mantid-roman committed May 31, 2013
1 parent d32fe7d commit 5e2dfe8
Showing 1 changed file with 82 additions and 20 deletions.
102 changes: 82 additions & 20 deletions Code/Mantid/Framework/CurveFitting/src/Fit.cpp
Original file line number Diff line number Diff line change
@@ -1,55 +1,104 @@
/*WIKI*
This is a generic algorithm for fitting data in a Workspace with a function. The workspace must have the type supported by the algorithm. Currently supported types are: [[MatrixWorkspace]] for fitting with a [[IFunction1D]] and [[IMDWorkspace]] for fitting with [[IFunctionMD]]. After Function and InputWorkspace properties are set the algorithm may decide that it needs more information from the caller to locate the fitting data. For example, if a spectrum in a MatrixWorkspace is to be fit with a 1D function it will need to know at least the index of that spectrum. To request this information Fit dynamically creates relevant properties which the caller can set. Note that the dynamic properties depend both on the workspace and the function. For example, the data in a MatrixWorkspace can be fit with a 2D function. In this case all spectra will be used in the fit and no additional properties will be declared. The Function property must be set before any other.
The function and the initial values for its parameters are set with the Function property. A function can be simple or composite. A [[:Category:Fit_functions|simple function]] has a name registered with Mantid framework. The Fit algorithm creates an instance of a function by this name. A composite function is an arithmetic sum of two or more functions (simple or composite). Each function has a number of named parameters, the names are case sensitive. All function parameters will be used in the fit unless some of them are tied. Parameters can be tied by setting the Ties property. A tie is a mathematical expression which is used to calculate the value of a (dependent) parameter. Only the parameter names of the same function can be used as variables in this expression.
Using the Minimizer property, Fit can be set to use different algorithms to perform the minimization. By default if the function's derivatives can be evaluated then Fit uses the GSL Levenberg-Marquardt minimizer.
In Mantidplot this algorithm can be run from the [[MantidPlot:_Data Analysis and Curve Fitting#Simple Peak Fitting with the Fit Wizard|Fit Property Browser]] which allows all the settings to be specified via its graphical user interface.
This is a generic algorithm for fitting data in a Workspace with a function.
The workspace must have the type supported by the algorithm. Currently supported
types are: [[MatrixWorkspace]] for fitting with a [[IFunction1D]] and
[[IMDWorkspace]] for fitting with [[IFunctionMD]]. After Function and InputWorkspace
properties are set the algorithm may decide that it needs more information from
the caller to locate the fitting data. For example, if a spectrum in a MatrixWorkspace
is to be fit with a 1D function it will need to know at least the index of that spectrum.
To request this information Fit dynamically creates relevant properties which the caller
can set. Note that the dynamic properties depend both on the workspace and the function.
For example, the data in a MatrixWorkspace can be fit with a 2D function. In this case all
spectra will be used in the fit and no additional properties will be declared. The Function
property must be set before any other.
The function and the initial values for its parameters are set with the Function property.
A function can be simple or composite. A [[:Category:Fit_functions|simple function]] has a
name registered with Mantid framework. The Fit algorithm creates an instance of a function
by this name. A composite function is an arithmetic sum of two or more functions (simple or
composite). Each function has a number of named parameters, the names are case sensitive.
All function parameters will be used in the fit unless some of them are tied. Parameters
can be tied by setting the Ties property. A tie is a mathematical expression which is used
to calculate the value of a (dependent) parameter. Only the parameter names of the same
function can be used as variables in this expression.
Using the Minimizer property, Fit can be set to use different algorithms to perform the
minimization. By default if the function's derivatives can be evaluated then Fit uses the
GSL Levenberg-Marquardt minimizer.
In Mantidplot this algorithm can be run from the [[MantidPlot:_Data Analysis and Curve
Fitting#Simple Peak Fitting with the Fit Wizard|Fit Property Browser]] which allows all
the settings to be specified via its graphical user interface.
===Setting a simple function===
To use a simple function for a fit set its name and initial parameter values using the Function property. This property is a comma separated list of name=value pairs. The name of the first name=value pairs must be "name" and it must be set equal to the name of one of a [[:Category:Fit_functions|simple function]]. This name=value pair is followed by name=value pairs specifying values for the parameters of this function. If a parameter is not set in Function it will be given its default value defined by the function. All names are case sensitive. For example for fitting a Gaussian the Function property might look like this:
To use a simple function for a fit set its name and initial parameter values using the
Function property. This property is a comma separated list of name=value pairs. The name
of the first name=value pairs must be "name" and it must be set equal to the name of one
of a [[:Category:Fit_functions|simple function]]. This name=value pair is followed by
name=value pairs specifying values for the parameters of this function. If a parameter
is not set in Function it will be given its default value defined by the function. All
names are case sensitive. For example for fitting a Gaussian the Function property might
look like this:
Function: "name=Gaussian, PeakCentre=4.6, Height=10, Sigma=0.5"
Some functions have attributes. An attribute is a non-fitting parameter and can be of one of the following types: text string, integer, or double. Attributes are set just like the parameters using name=value pairs. For example:
Some functions have attributes. An attribute is a non-fitting parameter and can be of
one of the following types: text string, integer, or double. Attributes are set just
like the parameters using name=value pairs. For example:
Function: "name=UserFunction, Formula=a+b*x, a=1, b=2"
In this example Formula is the name of a string attribute which defines an expression for the user UserFunction. The fitting parameters a and b are created when the Formula attribute is set. It is important that Formula is defined before initializing the parameters.
In this example Formula is the name of a string attribute which defines an expression
for the user UserFunction. The fitting parameters a and b are created when the Formula
attribute is set. It is important that Formula is defined before initializing the parameters.
A list of the available simple functions can be found [[:Category:Fit_functions|here]].
===Setting a composite function===
A composite function is a sum of simple functions. It does not have a name. To define a composite function set a number of simple functions in the Function property. Each simple function definition must be separated by a semicolon ';'. For example fitting two Gaussians on a linear background might look like this:
A composite function is a sum of simple functions. It does not have a name. To define a
composite function set a number of simple functions in the Function property. Each simple
function definition must be separated by a semicolon ';'. For example fitting two Gaussians
on a linear background might look like this:
Function: "name=LinearBackground, A0=0.3;
name=Gaussian, PeakCentre=4.6, Height=10, Sigma=0.5;
name=Gaussian, PeakCentre=7.6, Height=8, Sigma=0.5"
===Setting ties===
Parameters can be tied to other parameters or to a constant. In this case they do not take part in the fitting but are evaluated using the tying expressions. Use Ties property to set any ties. In case of a simple function the parameter names are used as variables in the tying expressions. For example
Parameters can be tied to other parameters or to a constant. In this case they do not take
part in the fitting but are evaluated using the tying expressions. Use Ties property to set
any ties. In case of a simple function the parameter names are used as variables in the
tying expressions. For example
Ties: "a=2*b+1, c=2"
This ties parameter "a" to parameter "b" and fixes "c" to the constant 2.
In case of a composite function the variable name must refer to both the parameter name and the simple function it belongs to. It is done by writing the variable name in the following format:
In case of a composite function the variable name must refer to both the parameter name and
the simple function it belongs to. It is done by writing the variable name in the following format:
f<index>.<name>
The format consists of two parts separated by a period '.'. The first part defines the function by its index in the composite function (starting at 0). The index corresponds to the order in which the functions are defined in the Function property. For example:
The format consists of two parts separated by a period '.'. The first part defines the
function by its index in the composite function (starting at 0). The index corresponds
to the order in which the functions are defined in the Function property. For example:
Ties: "f1.Sigma=f0.Sigma,f2.Sigma=f0.Sigma"
This ties parameter "Sigma" of functions 1 and 2 to the "Sigma" of function 0. Of course all three functions must have a parameter called "Sigma" for this to work. The last example can also be written
This ties parameter "Sigma" of functions 1 and 2 to the "Sigma" of function 0. Of course
all three functions must have a parameter called "Sigma" for this to work. The last example
can also be written
Ties: "f1.Sigma=f2.Sigma=f0.Sigma"
===Setting constraints===
Parameters can be constrained to be above a lower boundary and/or below an upper boundary. If a constraint is violated a penalty to the fit is applied which should result the parameters satisfying the constraint. The penalty applied is described in more detail [[FitConstraint|here]]. Use Constraints property to set any constraints. In case of a simple function the parameter names are used as variables in the constraint expressions. For example
Parameters can be constrained to be above a lower boundary and/or below an upper boundary.
If a constraint is violated a penalty to the fit is applied which should result the parameters
satisfying the constraint. The penalty applied is described in more detail [[FitConstraint|here]].
Use Constraints property to set any constraints. In case of a simple function the parameter names
are used as variables in the constraint expressions. For example
Constraints: "4.0 < c < 4.2"
Expand All @@ -71,11 +120,22 @@ constrain the parameter "c" of function 1.
===Fitting to data in a MatrixWorkspace===
The error values in the input workspace are used to weight the data in the fit. Zero error values are not allowed and are replaced with ones.
The error values in the input workspace are used to weight the data in the fit. Zero error values
are not allowed and are replaced with ones.
===Output===
Setting the Output property defines the names of the two output workspaces. One of them is a [[TableWorkspace]] with the fitted parameter values. The other is a [[Workspace2D]] which compares the fit with the original data. It has three spectra. The first (index 0) contains the original data, the second one the data simulated with the fitting function and the third spectrum is the difference between the first two. For example, if the Output was set to "MyResults" the parameter TableWorkspace will have name "MyResults_Parameters" and the Workspace2D will be named "MyResults_Workspace". If the function's derivatives can be evaluated an additional TableWorkspace is returned. When the Output is set to "MyResults" this TableWorkspace will have the name "MyResults_NormalisedCovarianceMatrix" and it returns a calculated correlation matrix. Denote this matrix C and its elements Cij then the diagonal elements are listed as 1.0 and the off diagnonal elements as percentages of correlation between parameter i and j equal to 100*Cij/sqrt(Cii*Cjj).
Setting the Output property defines the names of the two output workspaces. One of them is a
[[TableWorkspace]] with the fitted parameter values. The other is a [[Workspace2D]] which
compares the fit with the original data. It has three spectra. The first (index 0) contains
the original data, the second one the data simulated with the fitting function and the third
spectrum is the difference between the first two. For example, if the Output was set to "MyResults"
the parameter TableWorkspace will have name "MyResults_Parameters" and the Workspace2D will be named
"MyResults_Workspace". If the function's derivatives can be evaluated an additional TableWorkspace is
returned. When the Output is set to "MyResults" this TableWorkspace will have the name
"MyResults_NormalisedCovarianceMatrix" and it returns a calculated correlation matrix.
Denote this matrix C and its elements Cij then the diagonal elements are listed as 1.0
and the off diagnonal elements as percentages of correlation between parameter i and j equal to 100*Cij/sqrt(Cii*Cjj).
==Examples==
Expand All @@ -102,7 +162,8 @@ The next example shows a fit of the same data but with a tie.
----
This example shows a fit of two overlapping Gaussians on a linear background. Here we create a composite function with a LinearBackground and two Gaussians:
This example shows a fit of two overlapping Gaussians on a linear background. Here we create
a composite function with a LinearBackground and two Gaussians:
InputWorkspace: Test
WorkspaceIndex: 0
Expand All @@ -128,7 +189,8 @@ This example repeats the previous one but with the Sigmas of the two Gaussians t
[[Image:Gaussian2Fit_Ties.jpg]]
=== Additional properties for a 1D function and a MatrixWorkspace ===
If Function defines a one-dimensional function and InputWorkspace is a [[MatrixWorkspace]] the algorithm will have these additional properties:
If Function defines a one-dimensional function and InputWorkspace is a [[MatrixWorkspace]]
the algorithm will have these additional properties:
{| border="1" cellpadding="5" cellspacing="0"
!Name
Expand Down

0 comments on commit 5e2dfe8

Please sign in to comment.