Search in sources :

Example 6 with ValueProcedure

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

the class ErfGaussian2DFunctionTest method functionComputesGradientForEach.

@Test
void functionComputesGradientForEach() {
    final ErfGaussian2DFunction f1 = (ErfGaussian2DFunction) this.f1;
    final int n = f1.size();
    final double[] du_da = new double[f1.getNumberOfGradients()];
    final double[] du_db = new double[f1.getNumberOfGradients()];
    final double[] d2u_da2 = new double[f1.getNumberOfGradients()];
    final double[] values = new double[n];
    final double[][] jacobian = new double[n][];
    final double[][] jacobian2 = new double[n][];
    for (final double background : testbackground) {
        // Peak 1
        for (final double signal1 : testsignal1) {
            for (final double cx1 : testcx1) {
                for (final double cy1 : testcy1) {
                    for (final double cz1 : testcz1) {
                        for (final double[] w1 : testw1) {
                            for (final double angle1 : testangle1) {
                                final double[] a = createParameters(background, signal1, cx1, cy1, cz1, w1[0], w1[1], angle1);
                                f1.initialiseExtended2(a);
                                // Compute single
                                for (int i = 0; i < n; i++) {
                                    final double o1 = f1.eval(i, du_da);
                                    final double o2 = f1.eval2(i, du_db, d2u_da2);
                                    Assertions.assertEquals(o1, o2, 1e-10, "Value");
                                    Assertions.assertArrayEquals(du_da, du_db, 1e-10, "Jacobian!=Jacobian");
                                    values[i] = o1;
                                    jacobian[i] = du_da.clone();
                                    jacobian2[i] = d2u_da2.clone();
                                }
                                // Use procedures
                                f1.forEach(new ValueProcedure() {

                                    int index = 0;

                                    @Override
                                    public void execute(double value) {
                                        Assertions.assertEquals(values[index], value, 1e-10, "Value ValueProcedure");
                                        index++;
                                    }
                                });
                                f1.forEach(new Gradient1Procedure() {

                                    int index = 0;

                                    @Override
                                    public void execute(double value, double[] dyDa) {
                                        Assertions.assertEquals(values[index], value, 1e-10, "Value Gradient1Procedure");
                                        Assertions.assertArrayEquals(jacobian[index], dyDa, 1e-10, "du_da Gradient1Procedure");
                                        index++;
                                    }
                                });
                                f1.forEach(new Gradient2Procedure() {

                                    int index = 0;

                                    @Override
                                    public void execute(double value, double[] dyDa, double[] d2yDa2) {
                                        Assertions.assertEquals(values[index], value, 1e-10, "Value Gradient2Procedure");
                                        Assertions.assertArrayEquals(jacobian[index], dyDa, 1e-10, "du_da Gradient2Procedure");
                                        Assertions.assertArrayEquals(jacobian2[index], d2yDa2, 1e-10, "d2u_da2 Gradient2Procedure");
                                        index++;
                                    }
                                });
                                f1.forEach(new ExtendedGradient2Procedure() {

                                    int index = 0;

                                    @Override
                                    public void executeExtended(double value, double[] dyDa, double[] d2yDaDb) {
                                        Assertions.assertEquals(values[index], value, 1e-10, "Value ExtendedGradient2Procedure");
                                        Assertions.assertArrayEquals(jacobian[index], dyDa, 1e-10, "du_da ExtendedGradient2Procedure");
                                        for (int j = 0, k = 0; j < d2u_da2.length; j++, k += d2u_da2.length + 1) {
                                            d2u_da2[j] = d2yDaDb[k];
                                        }
                                        Assertions.assertArrayEquals(jacobian2[index], d2u_da2, 1e-10, "d2u_da2 Gradient2Procedure");
                                        index++;
                                    }
                                });
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : ValueProcedure(uk.ac.sussex.gdsc.smlm.function.ValueProcedure) IntegralValueProcedure(uk.ac.sussex.gdsc.smlm.function.IntegralValueProcedure) Gradient2Procedure(uk.ac.sussex.gdsc.smlm.function.Gradient2Procedure) ExtendedGradient2Procedure(uk.ac.sussex.gdsc.smlm.function.ExtendedGradient2Procedure) ExtendedGradient2Procedure(uk.ac.sussex.gdsc.smlm.function.ExtendedGradient2Procedure) Gradient1Procedure(uk.ac.sussex.gdsc.smlm.function.Gradient1Procedure) Gaussian2DFunctionTest(uk.ac.sussex.gdsc.smlm.function.gaussian.Gaussian2DFunctionTest) Test(org.junit.jupiter.api.Test)

Example 7 with ValueProcedure

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

the class EjmlLinearSolverTest method runSolverSpeedTest.

private void runSolverSpeedTest(RandomSeed seed, int flags) {
    Assumptions.assumeTrue(TestSettings.allow(TestComplexity.MEDIUM));
    final Gaussian2DFunction f0 = GaussianFunctionFactory.create2D(1, 10, 10, flags, null);
    final int n = f0.size();
    final double[] y = new double[n];
    final LocalList<DenseMatrix64F> aList = new LocalList<>();
    final LocalList<DenseMatrix64F> bList = new LocalList<>();
    final double[] testbackground = new double[] { 0.2, 0.7 };
    final double[] testsignal1 = new double[] { 30, 100, 300 };
    final double[] testcx1 = new double[] { 4.9, 5.3 };
    final double[] testcy1 = new double[] { 4.8, 5.2 };
    final double[] testw1 = new double[] { 1.1, 1.2, 1.5 };
    final int np = f0.getNumberOfGradients();
    final GradientCalculator calc = GradientCalculatorUtils.newCalculator(np);
    final UniformRandomProvider rng = RngUtils.create(seed.getSeed());
    // double lambda = 10;
    for (final double background : testbackground) {
        // Peak 1
        for (final double signal1 : testsignal1) {
            for (final double cx1 : testcx1) {
                for (final double cy1 : testcy1) {
                    for (final double w1 : testw1) {
                        final double[] p = new double[] { background, signal1, 0, cx1, cy1, w1, w1 };
                        f0.initialise(p);
                        f0.forEach(new ValueProcedure() {

                            int index = 0;

                            @Override
                            public void execute(double value) {
                                // Poisson data
                                y[index++] = GdscSmlmTestUtils.createPoissonSampler(rng, value).sample();
                            }
                        });
                        final double[][] alpha = new double[np][np];
                        final double[] beta = new double[np];
                        // double ss =
                        calc.findLinearised(n, y, p, alpha, beta, f0);
                        // TestLog.fine(logger,"SS = %f", ss);
                        // As per the LVM algorithm
                        // for (int i = 0; i < np; i++)
                        // alpha[i][i] *= lambda;
                        aList.add(EjmlLinearSolver.toA(alpha));
                        bList.add(EjmlLinearSolver.toB(beta));
                    }
                }
            }
        }
    }
    final DenseMatrix64F[] a = aList.toArray(new DenseMatrix64F[0]);
    final DenseMatrix64F[] b = bList.toArray(new DenseMatrix64F[0]);
    final int runs = 100000 / a.length;
    final TimingService ts = new TimingService(runs);
    final LocalList<SolverTimingTask> tasks = new LocalList<>();
    // Added in descending speed order
    tasks.add(new PseudoInverseSolverTimingTask(a, b));
    tasks.add(new LinearSolverTimingTask(a, b));
    tasks.add(new CholeskySolverTimingTask(a, b));
    tasks.add(new CholeskyLdltSolverTimingTask(a, b));
    tasks.add(new DirectInversionSolverTimingTask(a, b));
    for (final SolverTimingTask task : tasks) {
        if (!task.badSolver) {
            ts.execute(task);
        }
    }
    final int size = ts.getSize();
    ts.repeat();
    if (logger.isLoggable(Level.INFO)) {
        logger.info(ts.getReport(size));
    }
    // Just check the PseudoInverse is slowest
    for (int i = 1; i < size; i++) {
        logger.log(TestLogUtils.getTimingRecord(ts.get(-(size)), ts.get(-i)));
    }
    if (np > 2) {
        // The Direct solver may not be faster at size=5
        int i = (np == 5) ? 2 : 1;
        final int size_1 = size - 1;
        for (; i < size_1; i++) {
            logger.log(TestLogUtils.getTimingRecord(ts.get(-(size_1)), ts.get(-i)));
        }
    }
}
Also used : ValueProcedure(uk.ac.sussex.gdsc.smlm.function.ValueProcedure) DenseMatrix64F(org.ejml.data.DenseMatrix64F) LocalList(uk.ac.sussex.gdsc.core.utils.LocalList) Gaussian2DFunction(uk.ac.sussex.gdsc.smlm.function.gaussian.Gaussian2DFunction) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) GradientCalculator(uk.ac.sussex.gdsc.smlm.fitting.nonlinear.gradient.GradientCalculator) TimingService(uk.ac.sussex.gdsc.test.utils.TimingService)

Example 8 with ValueProcedure

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

the class EjmlLinearSolverTest method runInversionSpeedTest.

private void runInversionSpeedTest(RandomSeed seed, int flags) {
    Assumptions.assumeTrue(TestSettings.allow(TestComplexity.MEDIUM));
    final Gaussian2DFunction f0 = GaussianFunctionFactory.create2D(1, 10, 10, flags, null);
    final int n = f0.size();
    final double[] y = new double[n];
    final LocalList<DenseMatrix64F> aList = new LocalList<>();
    final double[] testbackground = new double[] { 0.2, 0.7 };
    final double[] testsignal1 = new double[] { 30, 100, 300 };
    final double[] testcx1 = new double[] { 4.9, 5.3 };
    final double[] testcy1 = new double[] { 4.8, 5.2 };
    final double[] testw1 = new double[] { 1.1, 1.2, 1.5 };
    final int np = f0.getNumberOfGradients();
    final GradientCalculator calc = GradientCalculatorUtils.newCalculator(np);
    final UniformRandomProvider rng = RngUtils.create(seed.getSeed());
    // double lambda = 10;
    for (final double background : testbackground) {
        // Peak 1
        for (final double signal1 : testsignal1) {
            for (final double cx1 : testcx1) {
                for (final double cy1 : testcy1) {
                    for (final double w1 : testw1) {
                        final double[] p = new double[] { background, signal1, 0, cx1, cy1, w1, w1 };
                        f0.initialise(p);
                        f0.forEach(new ValueProcedure() {

                            int index = 0;

                            @Override
                            public void execute(double value) {
                                // Poisson data
                                y[index++] = GdscSmlmTestUtils.createPoissonSampler(rng, value).sample();
                            }
                        });
                        final double[][] alpha = new double[np][np];
                        final double[] beta = new double[np];
                        // double ss =
                        calc.findLinearised(n, y, p, alpha, beta, f0);
                        // TestLog.fine(logger,"SS = %f", ss);
                        // As per the LVM algorithm
                        // for (int i = 0; i < np; i++)
                        // alpha[i][i] *= lambda;
                        aList.add(EjmlLinearSolver.toA(alpha));
                    }
                }
            }
        }
    }
    final DenseMatrix64F[] a = aList.toArray(new DenseMatrix64F[0]);
    final boolean[] ignore = new boolean[a.length];
    final double[][] answer = new double[a.length][];
    final int runs = 100000 / a.length;
    final TimingService ts = new TimingService(runs);
    final LocalList<InversionTimingTask> tasks = new LocalList<>();
    // Added in descending speed order
    tasks.add(new PseudoInverseInversionTimingTask(a, ignore, answer));
    tasks.add(new LinearInversionTimingTask(a, ignore, answer));
    tasks.add(new CholeskyLdltInversionTimingTask(a, ignore, answer));
    tasks.add(new CholeskyInversionTimingTask(a, ignore, answer));
    tasks.add(new DirectInversionInversionTimingTask(a, ignore, answer));
    tasks.add(new DiagonalDirectInversionInversionTimingTask(a, ignore, answer));
    for (final InversionTimingTask task : tasks) {
        if (!task.badSolver) {
            ts.execute(task);
        }
    }
    final int size = ts.getSize();
    ts.repeat();
    if (logger.isLoggable(Level.INFO)) {
        logger.info(ts.getReport(size));
    }
    // When it is present the DiagonalDirect is fastest (n<=5)
    if (np <= 5) {
        for (int i = 2; i <= size; i++) {
            logger.log(TestLogUtils.getTimingRecord(ts.get(-i), ts.get(-1)));
        }
        if (np < 5) {
            // n < 5 Direct is fastest
            for (int i = 3; i <= size; i++) {
                logger.log(TestLogUtils.getTimingRecord(ts.get(-i), ts.get(-2)));
            }
        } else {
            // and may not be faster than Direct at n=5 so that comparison is ignored.
            for (int i = 4; i <= size; i++) {
                logger.log(TestLogUtils.getTimingRecord(ts.get(-i), ts.get(-3)));
            }
        }
    } else {
        // Cholesky should be fastest.
        for (int i = 2; i <= size; i++) {
            logger.log(TestLogUtils.getTimingRecord(ts.get(-i), ts.get(-1)));
        }
    }
}
Also used : ValueProcedure(uk.ac.sussex.gdsc.smlm.function.ValueProcedure) DenseMatrix64F(org.ejml.data.DenseMatrix64F) LocalList(uk.ac.sussex.gdsc.core.utils.LocalList) Gaussian2DFunction(uk.ac.sussex.gdsc.smlm.function.gaussian.Gaussian2DFunction) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) GradientCalculator(uk.ac.sussex.gdsc.smlm.fitting.nonlinear.gradient.GradientCalculator) TimingService(uk.ac.sussex.gdsc.test.utils.TimingService)

Aggregations

ValueProcedure (uk.ac.sussex.gdsc.smlm.function.ValueProcedure)8 UniformRandomProvider (org.apache.commons.rng.UniformRandomProvider)4 IntegralValueProcedure (uk.ac.sussex.gdsc.smlm.function.IntegralValueProcedure)3 Gaussian2DFunction (uk.ac.sussex.gdsc.smlm.function.gaussian.Gaussian2DFunction)3 DenseMatrix64F (org.ejml.data.DenseMatrix64F)2 Test (org.junit.jupiter.api.Test)2 LocalList (uk.ac.sussex.gdsc.core.utils.LocalList)2 GradientCalculator (uk.ac.sussex.gdsc.smlm.fitting.nonlinear.gradient.GradientCalculator)2 ExtendedGradient2Procedure (uk.ac.sussex.gdsc.smlm.function.ExtendedGradient2Procedure)2 Gradient1Procedure (uk.ac.sussex.gdsc.smlm.function.Gradient1Procedure)2 Gradient2Procedure (uk.ac.sussex.gdsc.smlm.function.Gradient2Procedure)2 Gaussian2DFunctionTest (uk.ac.sussex.gdsc.smlm.function.gaussian.Gaussian2DFunctionTest)2 ErfGaussian2DFunction (uk.ac.sussex.gdsc.smlm.function.gaussian.erf.ErfGaussian2DFunction)2 SingleFreeCircularErfGaussian2DFunction (uk.ac.sussex.gdsc.smlm.function.gaussian.erf.SingleFreeCircularErfGaussian2DFunction)2 TimingService (uk.ac.sussex.gdsc.test.utils.TimingService)2 ArrayList (java.util.ArrayList)1 ConvergenceException (org.apache.commons.math3.exception.ConvergenceException)1 TooManyEvaluationsException (org.apache.commons.math3.exception.TooManyEvaluationsException)1 TooManyIterationsException (org.apache.commons.math3.exception.TooManyIterationsException)1 LeastSquaresBuilder (org.apache.commons.math3.fitting.leastsquares.LeastSquaresBuilder)1