Search in sources :

Example 11 with TimingService

use of uk.ac.sussex.gdsc.test.utils.TimingService in project GDSC-SMLM by aherbert.

the class RampedSelectionStrategyTest method speedTest.

private static void speedTest(final int size, boolean faster, int runs) {
    final long[] sum = RampedSelectionStrategy.createRampedSum(size);
    final TimingService ts = new TimingService(runs);
    ts.execute(new BaseTimingTask("search" + size) {

        @Override
        public Object getData(int index) {
            return sum;
        }

        @Override
        public Object run(Object data) {
            for (int key = (int) sum[sum.length - 1]; key-- > 0; ) {
                RampedSelectionStrategy.search(sum, key);
            }
            return null;
        }

        @Override
        public int getSize() {
            return 1;
        }
    });
    ts.execute(new BaseTimingTask("binarySearch" + size) {

        @Override
        public Object getData(int index) {
            return sum[index];
        }

        @Override
        public Object run(Object data) {
            for (int key = (int) sum[sum.length - 1]; key-- > 0; ) {
                RampedSelectionStrategy.binarySearch(sum, key);
            }
            return null;
        }

        @Override
        public int getSize() {
            return 1;
        }
    });
    final int n = ts.repeat();
    ts.repeat(n);
    if (logger.isLoggable(Level.INFO)) {
        logger.info(ts.getReport());
    }
    final TimingResult slow = ts.get((faster) ? ts.getSize() - 2 : ts.getSize() - 1);
    final TimingResult fast = ts.get((faster) ? ts.getSize() - 1 : ts.getSize() - 2);
    logger.log(TestLogUtils.getTimingRecord(slow, fast));
}
Also used : TimingResult(uk.ac.sussex.gdsc.test.utils.TimingResult) TimingService(uk.ac.sussex.gdsc.test.utils.TimingService) BaseTimingTask(uk.ac.sussex.gdsc.test.utils.BaseTimingTask)

Example 12 with TimingService

use of uk.ac.sussex.gdsc.test.utils.TimingService in project GDSC-SMLM by aherbert.

the class ErfGaussian2DFunctionTest method functionIsFasterUsingForEach.

// Speed test forEach verses equivalent eval() function calls
@SpeedTag
@Test
void functionIsFasterUsingForEach() {
    Assumptions.assumeTrue(TestSettings.allow(TestComplexity.MEDIUM));
    final ErfGaussian2DFunction f1 = (ErfGaussian2DFunction) this.f1;
    final LocalList<double[]> params = new LocalList<>();
    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);
                                params.add(a);
                            }
                        }
                    }
                }
            }
        }
    }
    final double[][] x = params.toArray(new double[0][]);
    final int runs = 10000 / x.length;
    final TimingService ts = new TimingService(runs);
    ts.execute(new FunctionTimingTask(f1, x, 2));
    ts.execute(new FunctionTimingTask(f1, x, 1));
    ts.execute(new FunctionTimingTask(f1, x, 0));
    ts.execute(new ForEachTimingTask(f1, x, 2));
    ts.execute(new ForEachTimingTask(f1, x, 1));
    ts.execute(new ForEachTimingTask(f1, x, 0));
    final int size = ts.getSize();
    ts.repeat(size);
    if (logger.isLoggable(Level.INFO)) {
        logger.info(ts.getReport());
    }
    for (int i = 1; i <= 3; i++) {
        final TimingResult slow = ts.get(-i - 3);
        final TimingResult fast = ts.get(-i);
        logger.log(TestLogUtils.getTimingRecord(slow, fast));
    }
}
Also used : LocalList(uk.ac.sussex.gdsc.core.utils.LocalList) TimingResult(uk.ac.sussex.gdsc.test.utils.TimingResult) TimingService(uk.ac.sussex.gdsc.test.utils.TimingService) SpeedTag(uk.ac.sussex.gdsc.test.junit5.SpeedTag) Gaussian2DFunctionTest(uk.ac.sussex.gdsc.smlm.function.gaussian.Gaussian2DFunctionTest) Test(org.junit.jupiter.api.Test)

Example 13 with TimingService

use of uk.ac.sussex.gdsc.test.utils.TimingService in project GDSC-SMLM by aherbert.

the class ErfGaussian2DFunctionTest method functionIsFasterThanEquivalentGaussian2DFunction.

// Speed test verses equivalent Gaussian2DFunction
@SpeedTag
@Test
void functionIsFasterThanEquivalentGaussian2DFunction() {
    Assumptions.assumeTrue(TestSettings.allow(TestComplexity.MEDIUM));
    final int flags = this.flags & ~GaussianFunctionFactory.FIT_ERF;
    final Gaussian2DFunction gf = GaussianFunctionFactory.create2D(1, maxx, maxy, flags, zModel);
    final boolean zDepth = (flags & GaussianFunctionFactory.FIT_Z) != 0;
    final LocalList<double[]> params1 = new LocalList<>();
    final LocalList<double[]> params2 = new LocalList<>();
    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) {
                                double[] params = createParameters(background, signal1, cx1, cy1, cz1, w1[0], w1[1], angle1);
                                params1.add(params);
                                if (zDepth) {
                                    // Change to a standard free circular function
                                    params = params.clone();
                                    params[Gaussian2DFunction.X_SD] *= zModel.getSx(params[Gaussian2DFunction.Z_POSITION]);
                                    params[Gaussian2DFunction.Y_SD] *= zModel.getSy(params[Gaussian2DFunction.Z_POSITION]);
                                    params[Gaussian2DFunction.Z_POSITION] = 0;
                                    params2.add(params);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    final double[][] x = params1.toArray(new double[0][]);
    final double[][] x2 = (zDepth) ? params2.toArray(new double[0][]) : x;
    final int runs = 10000 / x.length;
    final TimingService ts = new TimingService(runs);
    ts.execute(new FunctionTimingTask(gf, x2, 1));
    ts.execute(new FunctionTimingTask(gf, x2, 0));
    ts.execute(new FunctionTimingTask(f1, x, 2));
    ts.execute(new FunctionTimingTask(f1, x, 1));
    ts.execute(new FunctionTimingTask(f1, x, 0));
    final int size = ts.getSize();
    ts.repeat(size);
    if (logger.isLoggable(Level.INFO)) {
        logger.info(ts.getReport());
    }
    for (int i = 1; i <= 2; i++) {
        final TimingResult slow = ts.get(-i - 3);
        final TimingResult fast = ts.get(-i);
        logger.log(TestLogUtils.getTimingRecord(slow, fast));
    }
}
Also used : LocalList(uk.ac.sussex.gdsc.core.utils.LocalList) TimingResult(uk.ac.sussex.gdsc.test.utils.TimingResult) Gaussian2DFunction(uk.ac.sussex.gdsc.smlm.function.gaussian.Gaussian2DFunction) TimingService(uk.ac.sussex.gdsc.test.utils.TimingService) SpeedTag(uk.ac.sussex.gdsc.test.junit5.SpeedTag) Gaussian2DFunctionTest(uk.ac.sussex.gdsc.smlm.function.gaussian.Gaussian2DFunctionTest) Test(org.junit.jupiter.api.Test)

Example 14 with TimingService

use of uk.ac.sussex.gdsc.test.utils.TimingService 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 15 with TimingService

use of uk.ac.sussex.gdsc.test.utils.TimingService 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

TimingService (uk.ac.sussex.gdsc.test.utils.TimingService)23 SeededTest (uk.ac.sussex.gdsc.test.junit5.SeededTest)15 UniformRandomProvider (org.apache.commons.rng.UniformRandomProvider)14 SpeedTag (uk.ac.sussex.gdsc.test.junit5.SpeedTag)13 TimingResult (uk.ac.sussex.gdsc.test.utils.TimingResult)9 Test (org.junit.jupiter.api.Test)7 LocalList (uk.ac.sussex.gdsc.core.utils.LocalList)6 Gaussian2DFunction (uk.ac.sussex.gdsc.smlm.function.gaussian.Gaussian2DFunction)4 BaseTimingTask (uk.ac.sussex.gdsc.test.utils.BaseTimingTask)3 FloatProcessor (ij.process.FloatProcessor)2 MultivariateNormalMixtureExpectationMaximization (org.apache.commons.math3.distribution.fitting.MultivariateNormalMixtureExpectationMaximization)2 SharedStateContinuousSampler (org.apache.commons.rng.sampling.distribution.SharedStateContinuousSampler)2 DenseMatrix64F (org.ejml.data.DenseMatrix64F)2 GradientCalculator (uk.ac.sussex.gdsc.smlm.fitting.nonlinear.gradient.GradientCalculator)2 ValueProcedure (uk.ac.sussex.gdsc.smlm.function.ValueProcedure)2 Gaussian2DFunctionTest (uk.ac.sussex.gdsc.smlm.function.gaussian.Gaussian2DFunctionTest)2 ImageProcessor (ij.process.ImageProcessor)1 Rectangle (java.awt.Rectangle)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1