Search in sources :

Example 1 with EllipticalGaussian2DFunction

use of uk.ac.sussex.gdsc.smlm.function.gaussian.EllipticalGaussian2DFunction in project GDSC-SMLM by aherbert.

the class GaussianFit method addToImage.

/**
 * Adds the function to the image at the specified origin.
 *
 * @param ox the x-origin
 * @param oy the y-origin
 * @param renderedImage the rendered image
 * @param params the function parameters
 * @param npeaks the number of peaks
 * @param width the function width
 * @param height the function height
 */
private void addToImage(int ox, int oy, final FloatProcessor renderedImage, double[] params, int npeaks, int width, int height) {
    final EllipticalGaussian2DFunction f = new EllipticalGaussian2DFunction(npeaks, width, height);
    invertParameters(params);
    final FloatProcessor fp = new FloatProcessor(width, height, f.computeValues(params));
    // Insert into a full size image
    renderedImage.copyBits(fp, ox, oy, Blitter.ADD);
}
Also used : FloatProcessor(ij.process.FloatProcessor) EllipticalGaussian2DFunction(uk.ac.sussex.gdsc.smlm.function.gaussian.EllipticalGaussian2DFunction)

Example 2 with EllipticalGaussian2DFunction

use of uk.ac.sussex.gdsc.smlm.function.gaussian.EllipticalGaussian2DFunction in project GDSC-SMLM by aherbert.

the class GradientCalculatorSpeedTest method doubleCreateGaussianData.

/**
 * Create random elliptical Gaussian data an returns the data plus an estimate of the parameters.
 * Only the chosen parameters are randomised and returned for a maximum of (background, amplitude,
 * angle, xpos, ypos, xwidth, ywidth }
 *
 * @param rng the random
 * @param npeaks the npeaks
 * @param params set on output
 * @param randomiseParams Set to true to randomise the params
 * @return the double[]
 */
private double[] doubleCreateGaussianData(UniformRandomProvider rng, int npeaks, double[] params, boolean randomiseParams) {
    final int n = blockWidth * blockWidth;
    // Generate a 2D Gaussian
    final EllipticalGaussian2DFunction func = new EllipticalGaussian2DFunction(npeaks, blockWidth, blockWidth);
    params[0] = random(rng, background);
    for (int i = 0, j = 0; i < npeaks; i++, j += Gaussian2DFunction.PARAMETERS_PER_PEAK) {
        params[j + Gaussian2DFunction.SIGNAL] = random(rng, amplitude);
        params[j + Gaussian2DFunction.ANGLE] = random(rng, angle);
        params[j + Gaussian2DFunction.X_POSITION] = random(rng, xpos);
        params[j + Gaussian2DFunction.Y_POSITION] = random(rng, ypos);
        params[j + Gaussian2DFunction.X_SD] = random(rng, xwidth);
        params[j + Gaussian2DFunction.Y_SD] = random(rng, ywidth);
    }
    final double[] y = new double[n];
    func.initialise(params);
    for (int i = 0; i < y.length; i++) {
        // Add random Poisson noise
        final double u = func.eval(i);
        y[i] = GdscSmlmTestUtils.createPoissonSampler(rng, u).sample();
    }
    if (randomiseParams) {
        params[0] = random(rng, params[0]);
        for (int i = 0, j = 0; i < npeaks; i++, j += Gaussian2DFunction.PARAMETERS_PER_PEAK) {
            params[j + Gaussian2DFunction.SIGNAL] = random(rng, params[j + Gaussian2DFunction.SIGNAL]);
            params[j + Gaussian2DFunction.ANGLE] = random(rng, params[j + Gaussian2DFunction.ANGLE]);
            params[j + Gaussian2DFunction.X_POSITION] = random(rng, params[j + Gaussian2DFunction.X_POSITION]);
            params[j + Gaussian2DFunction.Y_POSITION] = random(rng, params[j + Gaussian2DFunction.Y_POSITION]);
            params[j + Gaussian2DFunction.X_SD] = random(rng, params[j + Gaussian2DFunction.X_SD]);
            params[j + Gaussian2DFunction.Y_SD] = random(rng, params[j + Gaussian2DFunction.Y_SD]);
        }
    }
    return y;
}
Also used : SingleEllipticalGaussian2DFunction(uk.ac.sussex.gdsc.smlm.function.gaussian.SingleEllipticalGaussian2DFunction) EllipticalGaussian2DFunction(uk.ac.sussex.gdsc.smlm.function.gaussian.EllipticalGaussian2DFunction)

Example 3 with EllipticalGaussian2DFunction

use of uk.ac.sussex.gdsc.smlm.function.gaussian.EllipticalGaussian2DFunction in project GDSC-SMLM by aherbert.

the class GradientCalculatorSpeedTest method mleCalculatorComputesLogLikelihoodRatio.

@SeededTest
void mleCalculatorComputesLogLikelihoodRatio(RandomSeed seed) {
    final EllipticalGaussian2DFunction func = new EllipticalGaussian2DFunction(1, blockWidth, blockWidth);
    final int n = blockWidth * blockWidth;
    final double[] a = new double[1 + Gaussian2DFunction.PARAMETERS_PER_PEAK];
    final UniformRandomProvider rng = RngUtils.create(seed.getSeed());
    final DoubleDoubleBiPredicate predicate = TestHelper.doublesAreClose(1e-10, 0);
    for (int run = 5; run-- > 0; ) {
        a[Gaussian2DFunction.BACKGROUND] = random(rng, background);
        a[Gaussian2DFunction.SIGNAL] = random(rng, amplitude);
        a[Gaussian2DFunction.ANGLE] = random(rng, angle);
        a[Gaussian2DFunction.X_POSITION] = random(rng, xpos);
        a[Gaussian2DFunction.Y_POSITION] = random(rng, ypos);
        a[Gaussian2DFunction.X_SD] = random(rng, xwidth);
        a[Gaussian2DFunction.Y_SD] = random(rng, ywidth);
        // Simulate Poisson process
        func.initialise(a);
        final double[] u = new double[n];
        final double[] x = new double[n];
        for (int i = 0; i < n; i++) {
            final double value = func.eval(i);
            u[i] = value;
            // Add random Poisson noise
            if (value > 0) {
                x[i] = new PoissonSampler(rng, value).sample();
            }
        }
        final int ng = func.getNumberOfGradients();
        final double[][] alpha = new double[ng][ng];
        final double[] beta = new double[ng];
        final GradientCalculator calc = GradientCalculatorUtils.newCalculator(ng, true);
        final double llr = PoissonCalculator.logLikelihoodRatio(u, x);
        final double llr2 = calc.findLinearised(n, x, a, alpha, beta, func);
        // logger.fine(FunctionUtils.getSupplier("llr=%f, llr2=%f", llr, llr2));
        TestAssertions.assertTest(llr, llr2, predicate, "Log-likelihood ratio");
    }
}
Also used : DoubleDoubleBiPredicate(uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate) SingleEllipticalGaussian2DFunction(uk.ac.sussex.gdsc.smlm.function.gaussian.SingleEllipticalGaussian2DFunction) EllipticalGaussian2DFunction(uk.ac.sussex.gdsc.smlm.function.gaussian.EllipticalGaussian2DFunction) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) PoissonSampler(org.apache.commons.rng.sampling.distribution.PoissonSampler) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Aggregations

EllipticalGaussian2DFunction (uk.ac.sussex.gdsc.smlm.function.gaussian.EllipticalGaussian2DFunction)3 SingleEllipticalGaussian2DFunction (uk.ac.sussex.gdsc.smlm.function.gaussian.SingleEllipticalGaussian2DFunction)2 FloatProcessor (ij.process.FloatProcessor)1 UniformRandomProvider (org.apache.commons.rng.UniformRandomProvider)1 PoissonSampler (org.apache.commons.rng.sampling.distribution.PoissonSampler)1 DoubleDoubleBiPredicate (uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate)1 SeededTest (uk.ac.sussex.gdsc.test.junit5.SeededTest)1