Search in sources :

Example 1 with SingleFreeCircularGaussian2DFunction

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

the class SolverSpeedTest method createSolverData.

private static boolean createSolverData(UniformRandomProvider rand, float[][] alpha, float[] beta, boolean positiveDifinite) {
    // Generate a 2D Gaussian
    final SingleFreeCircularGaussian2DFunction func = new SingleFreeCircularGaussian2DFunction(10, 10);
    final double[] params = new double[1 + Gaussian2DFunction.PARAMETERS_PER_PEAK];
    params[Gaussian2DFunction.BACKGROUND] = 2 + rand.nextDouble() * 2;
    params[Gaussian2DFunction.SIGNAL] = 100 + rand.nextDouble() * 5;
    params[Gaussian2DFunction.X_POSITION] = 4.5 + rand.nextDouble();
    params[Gaussian2DFunction.Y_POSITION] = 4.5 + rand.nextDouble();
    params[Gaussian2DFunction.X_SD] = 1 + rand.nextDouble();
    params[Gaussian2DFunction.Y_SD] = 1 + rand.nextDouble();
    params[Gaussian2DFunction.ANGLE] = rand.nextDouble();
    final int[] x = new int[100];
    final double[] y = new double[100];
    func.initialise(params);
    for (int i = 0; i < x.length; i++) {
        // Add random noise
        y[i] = func.eval(i) + ((rand.nextDouble() < 0.5) ? -rand.nextDouble() * 5 : rand.nextDouble() * 5);
    }
    // Randomise parameters
    for (int i = 0; i < params.length; i++) {
        params[i] += (rand.nextDouble() < 0.5) ? -rand.nextDouble() : rand.nextDouble();
    }
    // Compute the Hessian and parameter gradient vector
    final GradientCalculator calc = new GradientCalculator(6);
    final double[][] alpha2 = new double[6][6];
    final double[] beta2 = new double[6];
    calc.findLinearised(y.length, y, params, alpha2, beta2, func);
    // Update the Hessian using a lambda shift
    final double lambda = 1.001;
    for (int i = 0; i < alpha2.length; i++) {
        alpha2[i][i] *= lambda;
    }
    // Copy back
    for (int i = 0; i < beta.length; i++) {
        beta[i] = (float) beta2[i];
        for (int j = 0; j < beta.length; j++) {
            alpha[i][j] = (float) alpha2[i][j];
        }
    }
    // Check for a positive definite matrix
    if (positiveDifinite) {
        final EjmlLinearSolver solver = new EjmlLinearSolver();
        return solver.solveCholeskyLdlT(copydouble(alpha), copydouble(beta));
    }
    return true;
}
Also used : SingleFreeCircularGaussian2DFunction(uk.ac.sussex.gdsc.smlm.function.gaussian.SingleFreeCircularGaussian2DFunction) GradientCalculator(uk.ac.sussex.gdsc.smlm.fitting.nonlinear.gradient.GradientCalculator)

Example 2 with SingleFreeCircularGaussian2DFunction

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

the class GradientCalculatorSpeedTest method gradientCalculatorAssumedXIsFasterThanGradientCalculator.

@SeededTest
void gradientCalculatorAssumedXIsFasterThanGradientCalculator(RandomSeed seed) {
    Assumptions.assumeTrue(TestSettings.allow(TestComplexity.MEDIUM));
    final int iter = 10000;
    final ArrayList<double[]> paramsList = new ArrayList<>(iter);
    final ArrayList<double[]> yList = new ArrayList<>(iter);
    final int[] x = createData(RngUtils.create(seed.getSeed()), 1, iter, paramsList, yList);
    final GradientCalculator calc = new GradientCalculator6();
    final GradientCalculator calc2 = new GradientCalculator6();
    final SingleFreeCircularGaussian2DFunction func = new SingleFreeCircularGaussian2DFunction(blockWidth, blockWidth);
    final int n = x.length;
    final int ng = func.getNumberOfGradients();
    final double[][] alpha = new double[ng][ng];
    final double[] beta = new double[ng];
    for (int i = 0; i < paramsList.size(); i++) {
        calc.findLinearised(x, yList.get(i), paramsList.get(i), alpha, beta, func);
    }
    for (int i = 0; i < paramsList.size(); i++) {
        calc2.findLinearised(n, yList.get(i), paramsList.get(i), alpha, beta, func);
    }
    long start1 = System.nanoTime();
    for (int i = 0; i < paramsList.size(); i++) {
        calc.findLinearised(x, yList.get(i), paramsList.get(i), alpha, beta, func);
    }
    start1 = System.nanoTime() - start1;
    long start2 = System.nanoTime();
    for (int i = 0; i < paramsList.size(); i++) {
        calc2.findLinearised(n, yList.get(i), paramsList.get(i), alpha, beta, func);
    }
    start2 = System.nanoTime() - start2;
    logger.log(TestLogUtils.getTimingRecord("GradientCalculator", start1, "GradientCalculatorAssumed", start2));
}
Also used : ArrayList(java.util.ArrayList) SingleFreeCircularGaussian2DFunction(uk.ac.sussex.gdsc.smlm.function.gaussian.SingleFreeCircularGaussian2DFunction) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Aggregations

SingleFreeCircularGaussian2DFunction (uk.ac.sussex.gdsc.smlm.function.gaussian.SingleFreeCircularGaussian2DFunction)2 ArrayList (java.util.ArrayList)1 GradientCalculator (uk.ac.sussex.gdsc.smlm.fitting.nonlinear.gradient.GradientCalculator)1 SeededTest (uk.ac.sussex.gdsc.test.junit5.SeededTest)1