Search in sources :

Example 1 with LseLvmSteppingFunctionSolver

use of uk.ac.sussex.gdsc.smlm.fitting.nonlinear.LseLvmSteppingFunctionSolver in project GDSC-SMLM by aherbert.

the class FitConfiguration method createFunctionSolver.

private BaseFunctionSolver createFunctionSolver() {
    if (gaussianFunction == null) {
        // Other code may want to call getFunctionSolver() to see if exceptions are thrown
        // so create a dummy function so we can return a function solver.
        gaussianFunction = createGaussianFunction(1, 1, 1);
    }
    if (getFitSolverValue() == FitSolver.MLE_VALUE) {
        // Only support CCD/EM-CCD at the moment
        if (!calibration.isCcdCamera()) {
            throw new IllegalStateException("CCD/EM-CCD camera is required for fit solver: " + getFitSolver());
        }
        // This requires the gain
        if (gain <= 0) {
            throw new IllegalStateException("The gain is required for fit solver: " + getFitSolver());
        }
        final MaximumLikelihoodFitter.SearchMethod searchMethod = convertSearchMethod();
        // Only the Poisson likelihood function supports gradients
        if (searchMethod.usesGradients() && isModelCamera()) {
            throw new IllegalStateException(String.format("The derivative based search method '%s' can only be used with the " + "'%s' likelihood function, i.e. no model camera noise", searchMethod, MaximumLikelihoodFitter.LikelihoodFunction.POISSON));
        }
        final MaximumLikelihoodFitter fitter = new MaximumLikelihoodFitter(gaussianFunction);
        fitter.setRelativeThreshold(getRelativeThreshold());
        fitter.setAbsoluteThreshold(getAbsoluteThreshold());
        fitter.setMaxEvaluations(getMaxFunctionEvaluations());
        fitter.setMaxIterations(getMaxIterations());
        fitter.setSearchMethod(searchMethod);
        fitter.setGradientLineMinimisation(isGradientLineMinimisation());
        // Specify the likelihood function to use
        if (isModelCamera()) {
            // Set the camera read noise.
            // Do not check if this is set as 0 is a valid option.
            fitter.setSigma(calibration.getReadNoise());
            if (emCcd) {
                // EMCCD = Poisson+Gamma+Gaussian
                fitter.setLikelihoodFunction(MaximumLikelihoodFitter.LikelihoodFunction.POISSON_GAMMA_GAUSSIAN);
            } else {
                // CCD = Poisson+Gaussian
                fitter.setLikelihoodFunction(MaximumLikelihoodFitter.LikelihoodFunction.POISSON_GAUSSIAN);
            }
        } else {
            fitter.setLikelihoodFunction(MaximumLikelihoodFitter.LikelihoodFunction.POISSON);
        }
        // All models use the amplification gain (i.e. how many ADUs/electron)
        if (!calibration.hasCountPerElectron()) {
            throw new IllegalStateException("The amplification is required for the fit solver: " + getFitSolver());
        }
        fitter.setAlpha(1.0 / calibration.getCountPerElectron());
        return fitter;
    }
    // All the remaining solvers are based on the stepping function solver
    final ToleranceChecker tc = getToleranceChecker();
    final ParameterBounds bounds = new ParameterBounds(gaussianFunction);
    if (isUseClamping()) {
        setClampValues(bounds);
    }
    SteppingFunctionSolver solver;
    switch(getFitSolverValue()) {
        case FitSolver.LVM_LSE_VALUE:
            solver = new LseLvmSteppingFunctionSolver(gaussianFunction, tc, bounds);
            break;
        case FitSolver.LVM_MLE_VALUE:
            checkCameraCalibration();
            solver = new MleLvmSteppingFunctionSolver(gaussianFunction, tc, bounds);
            break;
        case FitSolver.LVM_WLSE_VALUE:
            checkCameraCalibration();
            solver = new WLseLvmSteppingFunctionSolver(gaussianFunction, tc, bounds);
            break;
        case FitSolver.FAST_MLE_VALUE:
            checkCameraCalibration();
            // This may throw a class cast exception if the function does not support
            // the Gradient2Function interface
            solver = new FastMleSteppingFunctionSolver((Gradient2Function) gaussianFunction, tc, bounds);
            break;
        default:
            throw new IllegalStateException("Unknown fit solver: " + getFitSolver());
    }
    if (solver instanceof LvmSteppingFunctionSolver) {
        ((LvmSteppingFunctionSolver) solver).setInitialLambda(getLambda());
    } else if (solver instanceof FastMleSteppingFunctionSolver) {
        ((FastMleSteppingFunctionSolver) solver).setLineSearchMethod(convertLineSearchMethod());
    }
    return solver;
}
Also used : ToleranceChecker(uk.ac.sussex.gdsc.smlm.fitting.nonlinear.ToleranceChecker) ParameterBounds(uk.ac.sussex.gdsc.smlm.fitting.nonlinear.ParameterBounds) MleLvmSteppingFunctionSolver(uk.ac.sussex.gdsc.smlm.fitting.nonlinear.MleLvmSteppingFunctionSolver) WLseLvmSteppingFunctionSolver(uk.ac.sussex.gdsc.smlm.fitting.nonlinear.WLseLvmSteppingFunctionSolver) LseLvmSteppingFunctionSolver(uk.ac.sussex.gdsc.smlm.fitting.nonlinear.LseLvmSteppingFunctionSolver) MleLvmSteppingFunctionSolver(uk.ac.sussex.gdsc.smlm.fitting.nonlinear.MleLvmSteppingFunctionSolver) SteppingFunctionSolver(uk.ac.sussex.gdsc.smlm.fitting.nonlinear.SteppingFunctionSolver) WLseLvmSteppingFunctionSolver(uk.ac.sussex.gdsc.smlm.fitting.nonlinear.WLseLvmSteppingFunctionSolver) FastMleSteppingFunctionSolver(uk.ac.sussex.gdsc.smlm.fitting.nonlinear.FastMleSteppingFunctionSolver) LvmSteppingFunctionSolver(uk.ac.sussex.gdsc.smlm.fitting.nonlinear.LvmSteppingFunctionSolver) LseLvmSteppingFunctionSolver(uk.ac.sussex.gdsc.smlm.fitting.nonlinear.LseLvmSteppingFunctionSolver) WLseLvmSteppingFunctionSolver(uk.ac.sussex.gdsc.smlm.fitting.nonlinear.WLseLvmSteppingFunctionSolver) Gradient2Function(uk.ac.sussex.gdsc.smlm.function.Gradient2Function) FastMleSteppingFunctionSolver(uk.ac.sussex.gdsc.smlm.fitting.nonlinear.FastMleSteppingFunctionSolver) MaximumLikelihoodFitter(uk.ac.sussex.gdsc.smlm.fitting.nonlinear.MaximumLikelihoodFitter) LseLvmSteppingFunctionSolver(uk.ac.sussex.gdsc.smlm.fitting.nonlinear.LseLvmSteppingFunctionSolver) MleLvmSteppingFunctionSolver(uk.ac.sussex.gdsc.smlm.fitting.nonlinear.MleLvmSteppingFunctionSolver) WLseLvmSteppingFunctionSolver(uk.ac.sussex.gdsc.smlm.fitting.nonlinear.WLseLvmSteppingFunctionSolver) LvmSteppingFunctionSolver(uk.ac.sussex.gdsc.smlm.fitting.nonlinear.LvmSteppingFunctionSolver)

Aggregations

FastMleSteppingFunctionSolver (uk.ac.sussex.gdsc.smlm.fitting.nonlinear.FastMleSteppingFunctionSolver)1 LseLvmSteppingFunctionSolver (uk.ac.sussex.gdsc.smlm.fitting.nonlinear.LseLvmSteppingFunctionSolver)1 LvmSteppingFunctionSolver (uk.ac.sussex.gdsc.smlm.fitting.nonlinear.LvmSteppingFunctionSolver)1 MaximumLikelihoodFitter (uk.ac.sussex.gdsc.smlm.fitting.nonlinear.MaximumLikelihoodFitter)1 MleLvmSteppingFunctionSolver (uk.ac.sussex.gdsc.smlm.fitting.nonlinear.MleLvmSteppingFunctionSolver)1 ParameterBounds (uk.ac.sussex.gdsc.smlm.fitting.nonlinear.ParameterBounds)1 SteppingFunctionSolver (uk.ac.sussex.gdsc.smlm.fitting.nonlinear.SteppingFunctionSolver)1 ToleranceChecker (uk.ac.sussex.gdsc.smlm.fitting.nonlinear.ToleranceChecker)1 WLseLvmSteppingFunctionSolver (uk.ac.sussex.gdsc.smlm.fitting.nonlinear.WLseLvmSteppingFunctionSolver)1 Gradient2Function (uk.ac.sussex.gdsc.smlm.function.Gradient2Function)1