Example usage for org.apache.commons.math3.optim.nonlinear.vector.jacobian LevenbergMarquardtOptimizer getRMS

List of usage examples for org.apache.commons.math3.optim.nonlinear.vector.jacobian LevenbergMarquardtOptimizer getRMS

Introduction

In this page you can find the example usage for org.apache.commons.math3.optim.nonlinear.vector.jacobian LevenbergMarquardtOptimizer getRMS.

Prototype

public double getRMS() 

Source Link

Document

Gets the root-mean-square (RMS) value.

Usage

From source file:gdsc.utils.Cell_Outliner.java

/**
 * Find an ellipse that optimises the fit to the polygon detected edges.
 * /*from   w  w w  . ja va  2s  .  com*/
 * @param roi
 * @param params
 * @param weightMap
 * @param angle
 * @return
 */
private double[] fitPolygonalCell(PolygonRoi roi, double[] params, FloatProcessor weightMap,
        FloatProcessor angle) {
    // Get an estimate of the starting parameters using the current polygon
    double[] startPoint = params;
    startPoint = estimateStartPoint(roi, weightMap.getWidth(), weightMap.getHeight());

    int maxEval = 2000;
    final DifferentiableEllipticalFitFunction func = new DifferentiableEllipticalFitFunction(roi, weightMap);

    double relativeThreshold = 100 * Precision.EPSILON;
    double absoluteThreshold = 100 * Precision.SAFE_MIN;
    ConvergenceChecker<PointVectorValuePair> checker = new SimplePointChecker<PointVectorValuePair>(
            relativeThreshold, absoluteThreshold);
    double initialStepBoundFactor = 10;
    double costRelativeTolerance = 1e-10;
    double parRelativeTolerance = 1e-10;
    double orthoTolerance = 1e-10;
    double threshold = Precision.SAFE_MIN;

    LevenbergMarquardtOptimizer optimiser = new LevenbergMarquardtOptimizer(initialStepBoundFactor, checker,
            costRelativeTolerance, parRelativeTolerance, orthoTolerance, threshold);

    try {
        PointVectorValuePair solution = optimiser.optimize(new MaxIter(maxEval), new MaxEval(Integer.MAX_VALUE),
                new ModelFunctionJacobian(new MultivariateMatrixFunction() {
                    public double[][] value(double[] point) throws IllegalArgumentException {
                        return func.jacobian(point);
                    }
                }), new ModelFunction(func), new Target(func.calculateTarget()),
                new Weight(func.calculateWeights()), new InitialGuess(startPoint));

        if (debug)
            IJ.log(String.format("Eval = %d (Iter = %d), RMS = %f", optimiser.getEvaluations(),
                    optimiser.getIterations(), optimiser.getRMS()));
        return solution.getPointRef();
    } catch (Exception e) {
        IJ.log("Failed to find an elliptical solution, defaulting to polygon");
        e.printStackTrace();
    }

    return null;
}

From source file:org.eclipse.dawnsci.analysis.dataset.roi.fitting.CircleCoordinatesFunction.java

@Override
public void geometricFit(IDataset x, IDataset y, double[] init) {
    residual = Double.NaN;/*ww w .j a  v  a 2 s  .  com*/
    if (x.getSize() < PARAMETERS || y.getSize() < PARAMETERS) {
        throw new IllegalArgumentException("Need " + PARAMETERS + " or more points");
    }

    if (x.getSize() == PARAMETERS) {
        init = quickfit(x, y);
        for (int i = 0; i < PARAMETERS; i++)
            parameters[i] = init[i];
        residual = 0;
        return;
    }

    if (init == null)
        init = quickfit(x, y);
    else if (init.length < PARAMETERS)
        throw new IllegalArgumentException("Need " + PARAMETERS + " parameters");

    CircleCoordinatesFunction f = (CircleCoordinatesFunction) getFitFunction(x, y);
    LevenbergMarquardtOptimizer opt = new LevenbergMarquardtOptimizer();

    try {
        PointVectorValuePair result = opt.optimize(new ModelFunction(f),
                new ModelFunctionJacobian(f.jacobian()), f.getTarget(), f.getWeight(),
                f.calcAllInitValues(init), new MaxEval(MAX_EVALUATIONS));

        double[] point = result.getPointRef();
        for (int i = 0; i < PARAMETERS; i++)
            parameters[i] = point[i];

        residual = opt.getRMS();
        logger.trace("Circle fit: rms = {}, x^2 = {}", opt.getRMS(), opt.getChiSquare());
    } catch (DimensionMismatchException e) {
        // cannot happen
    } catch (IllegalArgumentException e) {
        // should not happen!
    } catch (TooManyEvaluationsException e) {
        throw new IllegalArgumentException("Problem with optimizer converging");
    }
}

From source file:org.eclipse.dawnsci.analysis.dataset.roi.fitting.EllipseCoordinatesFunction.java

@Override
public void geometricFit(IDataset x, IDataset y, double[] init) {
    residual = Double.NaN;/*from  w  ww.ja  v  a  2 s . com*/
    if (x.getSize() < PARAMETERS || y.getSize() < PARAMETERS) {
        throw new IllegalArgumentException("Need " + PARAMETERS + " or more points");
    }

    if (init == null)
        init = quickfit(x, y);
    else if (init.length < PARAMETERS)
        throw new IllegalArgumentException("Need " + PARAMETERS + " parameters");

    EllipseCoordinatesFunction f = (EllipseCoordinatesFunction) getFitFunction(x, y);
    LevenbergMarquardtOptimizer opt = new LevenbergMarquardtOptimizer();

    try {
        PointVectorValuePair result = opt.optimize(new ModelFunction(f),
                new ModelFunctionJacobian(f.jacobian()), f.getTarget(), f.getWeight(),
                f.calcAllInitValues(init), new MaxEval(MAX_EVALUATIONS));

        double[] point = result.getPointRef();
        for (int i = 0; i < PARAMETERS; i++)
            parameters[i] = point[i];

        residual = opt.getRMS();

        logger.trace("Ellipse fit: rms = {}, x^2 = {}", residual, opt.getChiSquare());
    } catch (DimensionMismatchException e) {
        // cannot happen
    } catch (IllegalArgumentException e) {
        // should not happen!
    } catch (TooManyEvaluationsException e) {
        throw new IllegalArgumentException("Problem with optimizer converging");
    }
}