use of gdsc.smlm.fitting.nonlinear.NonLinearFit 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, null);
}
// Remove noise model
gaussianFunction.setNoiseModel(null);
NonLinearFit nlinfit;
switch(fitSolver) {
case MLE:
// Only the Poisson likelihood function supports gradients
if (searchMethod.usesGradients() && modelCamera) {
throw new IllegalArgumentException(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));
}
MaximumLikelihoodFitter fitter = new MaximumLikelihoodFitter(gaussianFunction);
fitter.setRelativeThreshold(relativeThreshold);
fitter.setAbsoluteThreshold(absoluteThreshold);
fitter.setMaxEvaluations(maxFunctionEvaluations);
fitter.setMaxIterations(maxIterations);
fitter.setSearchMethod(searchMethod);
fitter.setGradientLineMinimisation(gradientLineMinimisation);
// Specify the likelihood function to use
if (modelCamera) {
// Set the camera read noise
fitter.setSigma(readNoise);
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 (amplification <= 0) {
throw new IllegalArgumentException("The amplification is required for the " + fitSolver.getName());
}
fitter.setAlpha(1.0 / amplification);
return fitter;
case BOUNDED_LVM_WEIGHTED:
gaussianFunction.setNoiseModel(getNoiseModel());
case BOUNDED_LVM:
case LVM_MLE:
if (fitSolver == FitSolver.LVM_MLE && gain <= 0) {
throw new IllegalArgumentException("The gain is required for the " + fitSolver.getName());
}
if (useClamping) {
bounds = new ParameterBounds(gaussianFunction);
setClampValues(bounds);
}
BoundedNonLinearFit bnlinfit = new BoundedNonLinearFit(gaussianFunction, getStoppingCriteria(), bounds);
nlinfit = bnlinfit;
break;
case LVM_QUASI_NEWTON:
// This only works with a Gaussian2DFunction
if (gaussianFunction instanceof Gaussian2DFunction) {
ApacheLVMFitter apacheNLinFit = new ApacheLVMFitter((Gaussian2DFunction) gaussianFunction);
apacheNLinFit.setMaxEvaluations(maxIterations);
// TODO - Configure stopping criteria ...
return apacheNLinFit;
}
case LVM_WEIGHTED:
case LVM:
default:
// Only set the weighting function if necessary
if (fitSolver == FitSolver.LVM_WEIGHTED)
gaussianFunction.setNoiseModel(getNoiseModel());
nlinfit = new NonLinearFit(gaussianFunction, getStoppingCriteria());
}
nlinfit.setInitialLambda(getLambda());
if (fitSolver == FitSolver.LVM_MLE) {
nlinfit.setMLE(true);
}
return nlinfit;
}
Aggregations