|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectij.measure.CurveFitter
public class CurveFitter
Curve fitting class based on the Simplex method in the Minimizer class Notes on fitting polynomial functions: (i) The range of x values should not be too far from 0, especially for higher-order polynomials. For polynomials of 4th order, the average x value should fulfill |xMean| < 2*(xMax-xMin). For polynomials of 5th order or higher, the x range should encompass x=0; and for 7th and 8th order it is desirable to have x=0 near the center of the x range. (ii) In contrast to some fitting algorithms using the normal equations and matrix inversion, the simplex algorithm used here can cope with parameters having very different orders of magnitude, as long as the coefficients of the polynomial are within a reasonable range (say, 1e-80 to 1e80). Thus, it is usually not needed to scale the x values, even for high-order polynomials. Version history: 2008-01-21: Modified to do Gaussian fitting by Stefan Woerz (s.woerz at dkfz.de). 2012-01-30: Modified for external Minimizer class and UserFunction fit by Michael Schmid. - Also fixed incorrect equation string for 'Gamma Variate' & 'Rodbard (NIH Image)', - Added 'Inverse Rodbard', 'Exponential (linear regression)', 'Power (linear regression)' functions and polynomials of order 5-8. Added 'nicely' sorted list of types. - Added absolute error for minimizer to avoid endless minimization if exact fit is possible. - Added 'initialParamVariations' (order of magnitude if parameter variations) - this is important for safer and better convergence. - Linear regression for up to 2 linear parameters, reduces the number of parameters handled by the simplex Minimizer and improves convergence. These parameters can be an offset and either a linear slope or a factor that the full function is multiplied with. 2012-10-07: added GAUSSIAN_NOOFFSET fit type 2012-11-20: Bugfix: exception on Gaussian&Rodbard with initial params, bad initial params for Gaussian 2013-09-24: Added "Exponential Recovery (no offset)" and "Chapman-Richards" (3-parameter) fit types. 2013-10-11: bugfixes, added setStatusAndEsc to show iterations and enable abort by ESC 2015-03-26: bugfix, did not use linear regression for RODBARD
Field Summary | |
---|---|
static int |
CHAPMAN
Constants for the built-in fit functions |
static int |
EXP_RECOVERY
Constants for the built-in fit functions |
static int |
EXP_RECOVERY_NOOFFSET
Constants for the built-in fit functions |
static int |
EXP_REGRESSION
Constants for the built-in fit functions |
static int |
EXP_WITH_OFFSET
Constants for the built-in fit functions |
static int |
EXPONENTIAL
Constants for the built-in fit functions |
static String[] |
fitList
Names of the built-in fit functions |
static String[] |
fList
Equations of the built-in fit functions |
static int |
GAMMA_VARIATE
Constants for the built-in fit functions |
static int |
GAUSSIAN
Constants for the built-in fit functions |
static int |
GAUSSIAN_NOOFFSET
Constants for the built-in fit functions |
static int |
INV_RODBARD
Constants for the built-in fit functions |
static int |
IterFactor
Deprecated. now in the Minimizer class (since ImageJ 1.46f). (probably of not much value for anyone anyhow?) |
static int |
LOG
Constants for the built-in fit functions |
static int |
LOG2
Constants for the built-in fit functions |
static int |
POLY2
Constants for the built-in fit functions |
static int |
POLY3
Constants for the built-in fit functions |
static int |
POLY4
Constants for the built-in fit functions |
static int |
POLY5
Constants for the built-in fit functions |
static int |
POLY6
Constants for the built-in fit functions |
static int |
POLY7
Constants for the built-in fit functions |
static int |
POLY8
Constants for the built-in fit functions |
static int |
POWER
Constants for the built-in fit functions |
static int |
POWER_REGRESSION
Constants for the built-in fit functions |
static int |
RODBARD
Constants for the built-in fit functions |
static int |
RODBARD2
Constants for the built-in fit functions |
static int[] |
sortedTypes
Nicer sequence of the built-in function types |
static int |
STRAIGHT_LINE
Constants for the built-in fit functions |
Constructor Summary | |
---|---|
CurveFitter(double[] xData,
double[] yData)
Construct a new CurveFitter. |
Method Summary | |
---|---|
int |
doCustomFit(String equation,
double[] initialParams,
boolean showSettings)
Fit a function defined as a macro String like "y = a + b*x + c*x*x". |
void |
doCustomFit(UserFunction userFunction,
int numParams,
String formula,
double[] initialParams,
double[] initialParamVariations,
boolean showSettings)
Fit a function defined in a user plugin implementing the UserFunction interface Use getStatus() and/or getStatusString() to see whether fitting was (probably) successful and getParams() to access the result. |
void |
doFit(int fitType)
Perform curve fitting with one of the built-in functions doFit(fitType) does the fit quietly Use getStatus() and/or getStatusString() to see whether fitting was (probably) successful and getParams() to access the result. |
void |
doFit(int fitType,
boolean showSettings)
Perform curve fitting with one of the built-in functions doFit(fitType, true) pops up a dialog allowing the user to set the initial fit parameters and various numbers controlling the Minimizer Use getStatus() and/or getStatusString() to see whether fitting was (probably) successful and getParams() to access the result. |
double |
f(double x)
Returns the formula value for parameters 'p' at 'x'. |
double |
f(double[] p,
double x)
Returns the formula value for parameters 'p' at 'x'. |
static double |
f(int fitType,
double[] p,
double x)
Returns value of built-in 'fitType' formula value for parameters "p" at "x" |
int |
getFit()
returns the code of the fit type of the fit performed |
static int |
getFitCode(String fitName)
Returns the code for a fit with given name as defined in fitList, or -1 if not found |
double |
getFitGoodness()
Get a measure of "goodness of fit" where 1.0 is best. |
String |
getFormula()
returns a String with the formula of the fit function used |
int |
getIterations()
Get number of iterations performed. |
static int |
getMax(double[] array)
Gets index of highest value in an array. |
int |
getMaxIterations()
Get maximum number of iterations allowed (sum of iteration count for all restarts) |
Minimizer |
getMinimizer()
Returns a reference to the Minimizer used, for accessing Minimizer methods directly. |
String |
getName()
returns the name of the fit function of the fit performed |
int |
getNumParams()
Get number of parameters for current fit formula Do not use before 'doFit', because the fit function would be undefined. |
double[] |
getParams()
Get the result of fitting, i.e. |
double[] |
getResiduals()
Returns residuals array, i.e., differences between data and curve. |
int |
getRestarts()
Get maximum number of simplex restarts to do. |
String |
getResultString()
Get a string with detailed description of the curve fitting results (several lines, including the fit parameters). |
double |
getRSquared()
Returns R^2, where 1.0 is best. |
double |
getSD()
Returns the standard deviation of the residuals. |
static String[] |
getSortedFitList()
Returns an array of fit names with nicer sorting |
int |
getStatus()
|
String |
getStatusString()
Get a short text with a short description of the status. |
double |
getSumResidualsSqr()
|
double[] |
getXPoints()
returns the array with the x data |
double[] |
getYPoints()
returns the array with the y data |
void |
setInitialParameters(double[] initialParams)
Sets the initial parameters, which override the default initial parameters. |
void |
setMaxError(double maxRelError)
Set the maximum error. |
void |
setMaxIterations(int maxIter)
Set maximum number of iterations allowed (sum of iteration count for all restarts) |
void |
setOffsetMultiplySlopeParams(int offsetParam,
int multiplyParam,
int slopeParam)
For improved fitting performance when using a custom fit formula, one may specify parameters that can be calculated directly by linear regression. |
void |
setRestarts(int maxRestarts)
Set maximum number of simplex restarts to do. |
void |
setStatusAndEsc(String ijStatusString,
boolean checkEscape)
Create output on the number of iterations in the ImageJ Status line, e.g. |
double |
userFunction(double[] params,
double dummy)
This function is called by the Minimizer and calculates the sum of squared residuals for given parameters. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final int STRAIGHT_LINE
public static final int POLY2
public static final int POLY3
public static final int POLY4
public static final int EXPONENTIAL
public static final int POWER
public static final int LOG
public static final int RODBARD
public static final int GAMMA_VARIATE
public static final int LOG2
public static final int RODBARD2
public static final int EXP_WITH_OFFSET
public static final int GAUSSIAN
public static final int EXP_RECOVERY
public static final int INV_RODBARD
public static final int EXP_REGRESSION
public static final int POWER_REGRESSION
public static final int POLY5
public static final int POLY6
public static final int POLY7
public static final int POLY8
public static final int GAUSSIAN_NOOFFSET
public static final int EXP_RECOVERY_NOOFFSET
public static final int CHAPMAN
public static final int[] sortedTypes
public static final String[] fitList
public static final String[] fList
public static final int IterFactor
Constructor Detail |
---|
public CurveFitter(double[] xData, double[] yData)
Method Detail |
---|
public void doFit(int fitType)
public void doFit(int fitType, boolean showSettings)
public int doCustomFit(String equation, double[] initialParams, boolean showSettings)
public void doCustomFit(UserFunction userFunction, int numParams, String formula, double[] initialParams, double[] initialParamVariations, boolean showSettings)
userFunction
- A plugin where the fit function is defined by the
userFunction(params, x) method.
This function must allow simultaneous calls in multiple threads.numParams
- Number of parameters of the fit function.formula
- A String describing the fit formula, may be null.initialParams
- Starting point for the parameters; may be null (than values
of 0 are used). The fit function with these parameters must
not return NaN for any of the data points given in the
constructor (xData).initialParamVariations
- Each parameter is initially varied by up to +/- this value.
If not given (null), initial variations are taken as
10% of initial parameter value or 0.01 for parameters that are zero.
When this array is given, all elements must be positive (nonzero).
See Minimizer.minimize for details.showSettings
- Displays a popup dialog for modifying the initial parameters and
a few numbers controlling the minimizer.public void setInitialParameters(double[] initialParams)
public Minimizer getMinimizer()
public void setOffsetMultiplySlopeParams(int offsetParam, int multiplyParam, int slopeParam)
offsetParam
- Index of a parameter that is a pure offset:
E.g. '0' if f(p0, p1, p2...) = p0 + function(p1, p2, ...).multiplyParam
- Index of a parameter that is purely multiplicative.
E.g. multiplyParams=1 if f(p0, p1, p2, p3...) can be expressed as
p1*func(p0, p2, p3, ...) or p0 +p1*func(p0, p2, p3, ...) with '0' being
the offsetparam.slopeParam
- Index of a parameter that is multiplied with x and then summed to the function.
E.g. '1' for f(p0, p1, p2, p3...) = p1*x + func(p0, p2, p3, ...)
Only one, multiplyParam and slopeParam can be used (ie.e, the other
should be set to -1)public int getNumParams()
public final double f(double x)
public final double f(double[] p, double x)
public static double f(int fitType, double[] p, double x)
public double[] getParams()
public double[] getResiduals()
public double getSumResidualsSqr()
public double getSD()
public double getRSquared()
r^2 = 1 - SSE/SSD where: SSE = sum of the squared errors SSD = sum of the squared deviations about the mean.For power, exp by linear regression and 'Rodbard NIH Image', this is calculated for the fit actually done, not for the residuals of the original data.
public double getFitGoodness()
public int getStatus()
public String getStatusString()
public String getResultString()
public void setRestarts(int maxRestarts)
public void setMaxError(double maxRelError)
public void setStatusAndEsc(String ijStatusString, boolean checkEscape)
ijStatusString
- Displayed in the beginning of the status message. No display if null.
E.g. "Curve Fit: Iteration "checkEscape
- When true, the Minimizer stops if escape is pressed and the status
becomes ABORTED. Note that checking for ESC does not work in the Event Queue thread.public int getIterations()
public int getMaxIterations()
public void setMaxIterations(int maxIter)
public int getRestarts()
public double[] getXPoints()
public double[] getYPoints()
public int getFit()
public String getName()
public String getFormula()
public static String[] getSortedFitList()
public static int getFitCode(String fitName)
public final double userFunction(double[] params, double dummy)
userFunction
in interface UserFunction
params
- When minimizing, array of variables.
For curve fit array of fit parameters.
The array contents should not be modified.
Note that the function can get an array with more
elements then needed to specify the parameters.
Ignore the rest (and don't modify them).dummy
- For a fit function, the independent variable of the function.
Ignore it when using the minimizer.
public static int getMax(double[] array)
array
- the array.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |