Search in sources :

Example 1 with SingleFreeCircularErfGaussian2DFunction

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

the class FastMleJacobianGradient2ProcedureTest method gradientCalculatorComputesGradient.

@Override
@SeededTest
void gradientCalculatorComputesGradient(RandomSeed seed) {
    gradientCalculatorComputesGradient(seed, 1, new SingleFreeCircularErfGaussian2DFunction(blockWidth, blockWidth));
    gradientCalculatorComputesGradient(seed, 2, new MultiFreeCircularErfGaussian2DFunction(2, blockWidth, blockWidth));
    // Use a reasonable z-depth function from the Smith, et al (2010) paper (page 377)
    final double sx = 1.08;
    final double sy = 1.01;
    final double gamma = 0.389;
    final double d = 0.531;
    final double Ax = -0.0708;
    final double Bx = -0.073;
    final double Ay = 0.164;
    final double By = 0.0417;
    final HoltzerAstigmatismZModel zModel = HoltzerAstigmatismZModel.create(sx, sy, gamma, d, Ax, Bx, Ay, By);
    gradientCalculatorComputesGradient(seed, 1, new SingleAstigmatismErfGaussian2DFunction(blockWidth, blockWidth, zModel));
}
Also used : SingleAstigmatismErfGaussian2DFunction(uk.ac.sussex.gdsc.smlm.function.gaussian.erf.SingleAstigmatismErfGaussian2DFunction) HoltzerAstigmatismZModel(uk.ac.sussex.gdsc.smlm.function.gaussian.HoltzerAstigmatismZModel) SingleFreeCircularErfGaussian2DFunction(uk.ac.sussex.gdsc.smlm.function.gaussian.erf.SingleFreeCircularErfGaussian2DFunction) MultiFreeCircularErfGaussian2DFunction(uk.ac.sussex.gdsc.smlm.function.gaussian.erf.MultiFreeCircularErfGaussian2DFunction) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Example 2 with SingleFreeCircularErfGaussian2DFunction

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

the class LsqLvmGradientProcedureTest method gradientProcedureComputesSameOutputWithBias.

@SeededTest
void gradientProcedureComputesSameOutputWithBias(RandomSeed seed) {
    final ErfGaussian2DFunction func = new SingleFreeCircularErfGaussian2DFunction(blockWidth, blockWidth);
    final int nparams = func.getNumberOfGradients();
    final int iter = 100;
    final Level logLevel = Level.FINER;
    final boolean debug = logger.isLoggable(logLevel);
    final ArrayList<double[]> paramsList = new ArrayList<>(iter);
    final ArrayList<double[]> yList = new ArrayList<>(iter);
    final ArrayList<double[]> alphaList = new ArrayList<>(iter);
    final ArrayList<double[]> betaList = new ArrayList<>(iter);
    final ArrayList<double[]> xList = new ArrayList<>(iter);
    // Manipulate the background
    final double defaultBackground = background;
    try {
        background = 1e-2;
        createData(RngUtils.create(seed.getSeed()), 1, iter, paramsList, yList, true);
        final EjmlLinearSolver solver = new EjmlLinearSolver(1e-5, 1e-6);
        for (int i = 0; i < paramsList.size(); i++) {
            final double[] y = yList.get(i);
            final double[] a = paramsList.get(i);
            final BaseLsqLvmGradientProcedure p = LsqLvmGradientProcedureUtils.create(y, func);
            p.gradient(a);
            final double[] beta = p.beta;
            alphaList.add(p.getAlphaLinear());
            betaList.add(beta.clone());
            for (int j = 0; j < nparams; j++) {
                if (Math.abs(beta[j]) < 1e-6) {
                    logger.log(TestLogUtils.getRecord(Level.INFO, "[%d] Tiny beta %s %g", i, func.getGradientParameterName(j), beta[j]));
                }
            }
            // Solve
            if (!solver.solve(p.getAlphaMatrix(), beta)) {
                throw new AssertionError();
            }
            xList.add(beta);
        // System.out.println(Arrays.toString(beta));
        }
        // for (int b = 1; b < 1000; b *= 2)
        for (final double b : new double[] { -500, -100, -10, -1, -0.1, 0, 0.1, 1, 10, 100, 500 }) {
            final Statistics[] rel = new Statistics[nparams];
            final Statistics[] abs = new Statistics[nparams];
            if (debug) {
                for (int i = 0; i < nparams; i++) {
                    rel[i] = new Statistics();
                    abs[i] = new Statistics();
                }
            }
            for (int i = 0; i < paramsList.size(); i++) {
                final double[] y = add(yList.get(i), b);
                final double[] a = paramsList.get(i).clone();
                a[0] += b;
                final BaseLsqLvmGradientProcedure p = LsqLvmGradientProcedureUtils.create(y, func);
                p.gradient(a);
                final double[] beta = p.beta;
                final double[] alpha2 = alphaList.get(i);
                final double[] beta2 = betaList.get(i);
                final double[] x2 = xList.get(i);
                Assertions.assertArrayEquals(beta2, beta, 1e-10, "Beta");
                Assertions.assertArrayEquals(alpha2, p.getAlphaLinear(), 1e-10, "Alpha");
                // Solve
                solver.solve(p.getAlphaMatrix(), beta);
                Assertions.assertArrayEquals(x2, beta, 1e-10, "X");
                if (debug) {
                    for (int j = 0; j < nparams; j++) {
                        rel[j].add(DoubleEquality.relativeError(x2[j], beta[j]));
                        abs[j].add(Math.abs(x2[j] - beta[j]));
                    }
                }
            }
            if (debug) {
                for (int i = 0; i < nparams; i++) {
                    logger.log(TestLogUtils.getRecord(logLevel, "Bias = %.2f : %s : Rel %g +/- %g: Abs %g +/- %g", b, func.getGradientParameterName(i), rel[i].getMean(), rel[i].getStandardDeviation(), abs[i].getMean(), abs[i].getStandardDeviation()));
                }
            }
        }
    } finally {
        background = defaultBackground;
    }
}
Also used : SingleFreeCircularErfGaussian2DFunction(uk.ac.sussex.gdsc.smlm.function.gaussian.erf.SingleFreeCircularErfGaussian2DFunction) ErfGaussian2DFunction(uk.ac.sussex.gdsc.smlm.function.gaussian.erf.ErfGaussian2DFunction) ArrayList(java.util.ArrayList) Statistics(uk.ac.sussex.gdsc.core.utils.Statistics) SingleFreeCircularErfGaussian2DFunction(uk.ac.sussex.gdsc.smlm.function.gaussian.erf.SingleFreeCircularErfGaussian2DFunction) Level(java.util.logging.Level) TestLevel(uk.ac.sussex.gdsc.test.utils.TestLogUtils.TestLevel) EjmlLinearSolver(uk.ac.sussex.gdsc.smlm.fitting.linear.EjmlLinearSolver) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Example 3 with SingleFreeCircularErfGaussian2DFunction

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

the class LvmGradientProcedureTest method gradientProcedureFastLogMleCannotComputeGradientWithHighPrecision.

@Disabled("This test now passes as the tolerance for computing the gradient has been lowered " + " so that the tests pass under a stress test using many different random seeds.")
@SeededTest
void gradientProcedureFastLogMleCannotComputeGradientWithHighPrecision(RandomSeed seed) {
    // Try different precision
    for (int n = FastLog.N; n < 23; n++) {
        try {
            // logger.fine(FunctionUtils.getSupplier("Precision n=%d", n);
            fastLog = new TurboLog2(n);
            gradientProcedureComputesGradient(seed, new SingleFreeCircularErfGaussian2DFunction(blockWidth, blockWidth), Type.FAST_LOG_MLE, false);
        } catch (final AssertionError ex) {
            continue;
        } finally {
            // Reset
            fastLog = null;
        }
        return;
    }
    Assertions.fail();
}
Also used : TurboLog2(uk.ac.sussex.gdsc.smlm.function.TurboLog2) SingleFreeCircularErfGaussian2DFunction(uk.ac.sussex.gdsc.smlm.function.gaussian.erf.SingleFreeCircularErfGaussian2DFunction) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest) Disabled(org.junit.jupiter.api.Disabled)

Example 4 with SingleFreeCircularErfGaussian2DFunction

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

the class FastMleGradient2ProcedureTest method gradientCalculatorComputesGradient.

@SeededTest
void gradientCalculatorComputesGradient(RandomSeed seed) {
    gradientCalculatorComputesGradient(seed, new SingleFreeCircularErfGaussian2DFunction(blockWidth, blockWidth));
    // Use a reasonable z-depth function from the Smith, et al (2010) paper (page 377)
    final double sx = 1.08;
    final double sy = 1.01;
    final double gamma = 0.389;
    final double d = 0.531;
    final double Ax = -0.0708;
    final double Bx = -0.073;
    final double Ay = 0.164;
    final double By = 0.0417;
    final HoltzerAstigmatismZModel zModel = HoltzerAstigmatismZModel.create(sx, sy, gamma, d, Ax, Bx, Ay, By);
    gradientCalculatorComputesGradient(seed, new SingleAstigmatismErfGaussian2DFunction(blockWidth, blockWidth, zModel));
}
Also used : SingleAstigmatismErfGaussian2DFunction(uk.ac.sussex.gdsc.smlm.function.gaussian.erf.SingleAstigmatismErfGaussian2DFunction) HoltzerAstigmatismZModel(uk.ac.sussex.gdsc.smlm.function.gaussian.HoltzerAstigmatismZModel) SingleFreeCircularErfGaussian2DFunction(uk.ac.sussex.gdsc.smlm.function.gaussian.erf.SingleFreeCircularErfGaussian2DFunction) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Aggregations

SingleFreeCircularErfGaussian2DFunction (uk.ac.sussex.gdsc.smlm.function.gaussian.erf.SingleFreeCircularErfGaussian2DFunction)4 SeededTest (uk.ac.sussex.gdsc.test.junit5.SeededTest)4 HoltzerAstigmatismZModel (uk.ac.sussex.gdsc.smlm.function.gaussian.HoltzerAstigmatismZModel)2 SingleAstigmatismErfGaussian2DFunction (uk.ac.sussex.gdsc.smlm.function.gaussian.erf.SingleAstigmatismErfGaussian2DFunction)2 ArrayList (java.util.ArrayList)1 Level (java.util.logging.Level)1 Disabled (org.junit.jupiter.api.Disabled)1 Statistics (uk.ac.sussex.gdsc.core.utils.Statistics)1 EjmlLinearSolver (uk.ac.sussex.gdsc.smlm.fitting.linear.EjmlLinearSolver)1 TurboLog2 (uk.ac.sussex.gdsc.smlm.function.TurboLog2)1 ErfGaussian2DFunction (uk.ac.sussex.gdsc.smlm.function.gaussian.erf.ErfGaussian2DFunction)1 MultiFreeCircularErfGaussian2DFunction (uk.ac.sussex.gdsc.smlm.function.gaussian.erf.MultiFreeCircularErfGaussian2DFunction)1 TestLevel (uk.ac.sussex.gdsc.test.utils.TestLogUtils.TestLevel)1