Search in sources :

Example 6 with FitResult

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

the class GaussianFit method fit.

/**
 * Fits a single 2D Gaussian to the data. The fit is initialised at the highest value and then
 * optimised.
 *
 * <p>Data must be arranged in yx block order, i.e. height rows of width.
 *
 * <p>The angle parameter is only set if using elliptical Gaussian fitting.
 *
 * <p>Note: The returned fit coordinates should be offset by 0.5 if the input data represents
 * pixels
 *
 * @param data the data
 * @param width the width
 * @param height the height
 * @return Array containing the fitted curve data: Background, Amplitude, PosX, PosY, StdDevX,
 *         StdDevY, Angle. Null if no fit is possible.
 */
@Nullable
public double[] fit(float[] data, int width, int height) {
    if (data == null || data.length != width * height) {
        return null;
    }
    // Get the limits
    float max = Float.MIN_VALUE;
    int maxIndex = -1;
    for (int i = data.length; i-- > 0; ) {
        final float f = data[i];
        if (max < f) {
            max = f;
            maxIndex = i;
        }
    }
    if (maxIndex < 0) {
        return null;
    }
    final Gaussian2DFitter gf = createGaussianFitter(false);
    final FitResult fitResult = gf.fit(SimpleArrayUtils.toDouble(data), width, height, new int[] { maxIndex });
    if (fitResult.getStatus() == FitStatus.OK) {
        chiSquared = fitResult.getError();
        final double[] params = fitResult.getParameters();
        // Check bounds
        final double x = params[Gaussian2DFunction.X_POSITION];
        final double y = params[Gaussian2DFunction.Y_POSITION];
        if (x < 0 || x >= width || y < 0 || y >= height) {
            return null;
        }
        // Re-arrange order for backwards compatibility with old code.
        final double background = params[Gaussian2DFunction.BACKGROUND];
        final double intensity = params[Gaussian2DFunction.SIGNAL];
        final double sx = params[Gaussian2DFunction.X_SD];
        final double sy = params[Gaussian2DFunction.Y_SD];
        final double angle = params[Gaussian2DFunction.ANGLE];
        final double amplitude = Gaussian2DPeakResultHelper.getAmplitude(intensity, sx, sy);
        return new double[] { background, amplitude, x, y, sx, sy, angle };
    }
    return null;
}
Also used : Gaussian2DFitter(uk.ac.sussex.gdsc.smlm.fitting.Gaussian2DFitter) FitResult(uk.ac.sussex.gdsc.smlm.fitting.FitResult) Nullable(uk.ac.sussex.gdsc.core.annotation.Nullable)

Aggregations

FitResult (uk.ac.sussex.gdsc.smlm.fitting.FitResult)6 ConcurrentRuntimeException (org.apache.commons.lang3.concurrent.ConcurrentRuntimeException)2 Nullable (uk.ac.sussex.gdsc.core.annotation.Nullable)2 MultiPathFitResult (uk.ac.sussex.gdsc.smlm.results.filter.MultiPathFitResult)2 TIntObjectHashMap (gnu.trove.map.hash.TIntObjectHashMap)1 TIntProcedure (gnu.trove.procedure.TIntProcedure)1 TIntHashSet (gnu.trove.set.hash.TIntHashSet)1 IJ (ij.IJ)1 ImagePlus (ij.ImagePlus)1 ImageStack (ij.ImageStack)1 Prefs (ij.Prefs)1 Plot (ij.gui.Plot)1 PlotWindow (ij.gui.PlotWindow)1 PlugIn (ij.plugin.PlugIn)1 TextWindow (ij.text.TextWindow)1 Checkbox (java.awt.Checkbox)1 Color (java.awt.Color)1 Rectangle (java.awt.Rectangle)1 TextArea (java.awt.TextArea)1 TextField (java.awt.TextField)1