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);
}
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;
}
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");
}
}
Aggregations