Search in sources :

Example 1 with FakeGradientFunction

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

the class LsqLvmGradientProcedureTest method gradientProcedureIsNotSlowerThanGradientCalculator.

private void gradientProcedureIsNotSlowerThanGradientCalculator(RandomSeed seed, final int nparams, final BaseLsqLvmGradientProcedureFactory factory) {
    Assumptions.assumeTrue(TestSettings.allow(TestComplexity.MEDIUM));
    final int iter = 1000;
    final double[][] alpha = new double[nparams][nparams];
    final double[] beta = new double[nparams];
    final ArrayList<double[]> paramsList = new ArrayList<>(iter);
    final ArrayList<double[]> yList = new ArrayList<>(iter);
    final int[] x = createFakeData(RngUtils.create(seed.getSeed()), nparams, iter, paramsList, yList);
    final int n = x.length;
    final FakeGradientFunction func = new FakeGradientFunction(blockWidth, nparams);
    final GradientCalculator calc = GradientCalculatorUtils.newCalculator(nparams, false);
    for (int i = 0; i < paramsList.size(); i++) {
        calc.findLinearised(n, yList.get(i), paramsList.get(i), alpha, beta, func);
    }
    for (int i = 0; i < paramsList.size(); i++) {
        final BaseLsqLvmGradientProcedure p = factory.createProcedure(yList.get(i), func);
        p.gradient(paramsList.get(i));
    }
    // Realistic loops for an optimisation
    final int loops = 15;
    // Run till stable timing
    final Timer t1 = new Timer() {

        @Override
        void run() {
            for (int i = 0, k = 0; i < iter; i++) {
                final GradientCalculator calc = GradientCalculatorUtils.newCalculator(nparams, false);
                for (int j = loops; j-- > 0; ) {
                    calc.findLinearised(n, yList.get(i), paramsList.get(k++ % iter), alpha, beta, func);
                }
            }
        }
    };
    final long time1 = t1.getTime();
    final Timer t2 = new Timer(t1.loops) {

        @Override
        void run() {
            for (int i = 0, k = 0; i < iter; i++) {
                final BaseLsqLvmGradientProcedure p = factory.createProcedure(yList.get(i), func);
                for (int j = loops; j-- > 0; ) {
                    p.gradient(paramsList.get(k++ % iter));
                }
            }
        }
    };
    final long time2 = t2.getTime();
    logger.log(TestLogUtils.getTimingRecord("GradientCalculator " + nparams, time1, factory.getClass().getSimpleName(), time2));
}
Also used : ArrayList(java.util.ArrayList) FakeGradientFunction(uk.ac.sussex.gdsc.smlm.function.FakeGradientFunction)

Example 2 with FakeGradientFunction

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

the class LsqLvmGradientProcedureTest method gradientProcedureUnrolledComputesSameAsGradientProcedure.

private void gradientProcedureUnrolledComputesSameAsGradientProcedure(RandomSeed seed, int nparams) {
    final int iter = 10;
    final ArrayList<double[]> paramsList = new ArrayList<>(iter);
    final ArrayList<double[]> yList = new ArrayList<>(iter);
    createFakeData(RngUtils.create(seed.getSeed()), nparams, iter, paramsList, yList);
    final FakeGradientFunction func = new FakeGradientFunction(blockWidth, nparams);
    final String name = GradientCalculator.class.getSimpleName();
    // Create messages
    final IndexSupplier msgR = new IndexSupplier(1, name + "Result: Not same ", null);
    final IndexSupplier msgOb = new IndexSupplier(1, name + "Observations: Not same beta ", null);
    final IndexSupplier msgOal = new IndexSupplier(1, name + "Observations: Not same alpha linear ", null);
    final IndexSupplier msgOam = new IndexSupplier(1, name + "Observations: Not same alpha matrix ", null);
    for (int i = 0; i < paramsList.size(); i++) {
        final BaseLsqLvmGradientProcedure p1 = LsqLvmGradientProcedureUtils.create(yList.get(i), func);
        p1.gradient(paramsList.get(i));
        final BaseLsqLvmGradientProcedure p2 = new LsqLvmGradientProcedure(yList.get(i), func);
        p2.gradient(paramsList.get(i));
        // Exactly the same ...
        Assertions.assertEquals(p1.value, p2.value, msgR.set(0, i));
        Assertions.assertArrayEquals(p1.beta, p2.beta, msgOb.set(0, i));
        Assertions.assertArrayEquals(p1.getAlphaLinear(), p2.getAlphaLinear(), msgOal.set(0, i));
        final double[][] am1 = p1.getAlphaMatrix();
        final double[][] am2 = p2.getAlphaMatrix();
        Assertions.assertArrayEquals(am1, am2, msgOam.set(0, i));
    }
}
Also used : IndexSupplier(uk.ac.sussex.gdsc.test.utils.functions.IndexSupplier) ArrayList(java.util.ArrayList) FakeGradientFunction(uk.ac.sussex.gdsc.smlm.function.FakeGradientFunction)

Example 3 with FakeGradientFunction

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

the class LvmGradientProcedureTest method gradientProcedureIsNotSlowerThanGradientCalculator.

private void gradientProcedureIsNotSlowerThanGradientCalculator(RandomSeed seed, final int nparams, final Type type) {
    Assumptions.assumeTrue(TestSettings.allow(TestComplexity.MEDIUM));
    final int iter = 1000;
    final double[][] alpha = new double[nparams][nparams];
    final double[] beta = new double[nparams];
    final ArrayList<double[]> paramsList = new ArrayList<>(iter);
    final ArrayList<double[]> yList = new ArrayList<>(iter);
    final int[] x = createFakeData(RngUtils.create(seed.getSeed()), nparams, iter, paramsList, yList);
    final int n = x.length;
    final FakeGradientFunction func = new FakeGradientFunction(blockWidth, nparams);
    final boolean mle = type != Type.LSQ;
    final FastLog fastLog = (type == Type.FAST_LOG_MLE) ? getFastLog() : null;
    final GradientCalculator calc = GradientCalculatorUtils.newCalculator(nparams, mle);
    for (int i = 0; i < paramsList.size(); i++) {
        calc.findLinearised(n, yList.get(i), paramsList.get(i), alpha, beta, func);
    }
    for (int i = 0; i < paramsList.size(); i++) {
        final LvmGradientProcedure p = LvmGradientProcedureUtils.create(yList.get(i), func, type, fastLog);
        p.gradient(paramsList.get(i));
    }
    // Realistic loops for an optimisation
    final int loops = 15;
    // Run till stable timing
    final Timer t1 = new Timer() {

        @Override
        void run() {
            for (int i = 0, k = 0; i < iter; i++) {
                final GradientCalculator calc = GradientCalculatorUtils.newCalculator(nparams, mle);
                for (int j = loops; j-- > 0; ) {
                    calc.findLinearised(n, yList.get(i), paramsList.get(k++ % iter), alpha, beta, func);
                }
            }
        }
    };
    final long time1 = t1.getTime();
    final Timer t2 = new Timer(t1.loops) {

        @Override
        void run() {
            for (int i = 0, k = 0; i < iter; i++) {
                final LvmGradientProcedure p = LvmGradientProcedureUtils.create(yList.get(i), func, type, fastLog);
                for (int j = loops; j-- > 0; ) {
                    p.gradient(paramsList.get(k++ % iter));
                }
            }
        }
    };
    final long time2 = t2.getTime();
    logger.log(TestLogUtils.getTimingRecord(new TimingResult("GradientCalculator", time1), new TimingResult(() -> String.format("LVMGradientProcedure %d %s", nparams, type), time2)));
}
Also used : TimingResult(uk.ac.sussex.gdsc.test.utils.TimingResult) ArrayList(java.util.ArrayList) FastLog(uk.ac.sussex.gdsc.smlm.function.FastLog) FakeGradientFunction(uk.ac.sussex.gdsc.smlm.function.FakeGradientFunction)

Example 4 with FakeGradientFunction

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

the class LvmGradientProcedureTest method gradientProcedureComputesSameAsGradientCalculator.

private void gradientProcedureComputesSameAsGradientCalculator(RandomSeed seed, int nparams, Type type, double error) {
    final int iter = 10;
    final double[][] alpha = new double[nparams][nparams];
    final double[] beta = new double[nparams];
    final ArrayList<double[]> paramsList = new ArrayList<>(iter);
    final ArrayList<double[]> yList = new ArrayList<>(iter);
    final int[] x = createFakeData(RngUtils.create(seed.getSeed()), nparams, iter, paramsList, yList);
    final int n = x.length;
    final FakeGradientFunction func = new FakeGradientFunction(blockWidth, nparams);
    final boolean mle = type != Type.LSQ;
    final FastLog fastLog = (type == Type.FAST_LOG_MLE) ? getFastLog() : null;
    final GradientCalculator calc = GradientCalculatorUtils.newCalculator(nparams, mle);
    final String name = String.format("[%d] %b", nparams, mle);
    // Create messages
    final IndexSupplier msgR = new IndexSupplier(1, name + "Result: Not same ", null);
    final IndexSupplier msgOb = new IndexSupplier(1, name + "Observations: Not same beta ", null);
    final IndexSupplier msgOal = new IndexSupplier(1, name + "Observations: Not same alpha linear ", null);
    final IndexSupplier msgOam = new IndexSupplier(1, name + "Observations: Not same alpha matrix ", null);
    final DoubleDoubleBiPredicate predicate = (error == 0) ? TestHelper.doublesEqual() : TestHelper.doublesAreClose(error, 0);
    for (int i = 0; i < paramsList.size(); i++) {
        // Reference implementation
        final double s = calc.findLinearised(n, yList.get(i), paramsList.get(i), alpha, beta, func);
        // Procedure
        final LvmGradientProcedure p = LvmGradientProcedureUtils.create(yList.get(i), func, type, fastLog);
        p.gradient(paramsList.get(i));
        final double s2 = p.value;
        // Value may be different depending on log implementation
        msgR.set(0, i);
        TestAssertions.assertTest(s, s2, predicate, msgR);
        // Exactly the same ...
        Assertions.assertArrayEquals(p.beta, beta, msgOb.set(0, i));
        final double[] al = p.getAlphaLinear();
        Assertions.assertArrayEquals(al, new DenseMatrix64F(alpha).data, msgOal.set(0, i));
        final double[][] am = p.getAlphaMatrix();
        Assertions.assertArrayEquals(am, alpha, msgOam.set(0, i));
    }
}
Also used : DoubleDoubleBiPredicate(uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate) ArrayList(java.util.ArrayList) FastLog(uk.ac.sussex.gdsc.smlm.function.FastLog) DenseMatrix64F(org.ejml.data.DenseMatrix64F) IndexSupplier(uk.ac.sussex.gdsc.test.utils.functions.IndexSupplier) FakeGradientFunction(uk.ac.sussex.gdsc.smlm.function.FakeGradientFunction)

Example 5 with FakeGradientFunction

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

the class ParameterBoundsTest method canBoundParameter.

private static void canBoundParameter(double value) {
    final ParameterBounds bounds = new ParameterBounds(new FakeGradientFunction(1, 1, 1));
    if (value < 0) {
        bounds.setBounds(new double[] { 2 * value }, null);
    } else {
        bounds.setBounds(null, new double[] { 2 * value });
    }
    final double[] a1 = new double[1];
    final double[] a2 = new double[1];
    final double[] step = new double[] { value };
    bounds.applyBounds(a1, step, a2);
    Assertions.assertArrayEquals(a2, new double[] { 1 * value }, "Step 1");
    bounds.applyBounds(a2, step, a1);
    Assertions.assertArrayEquals(a1, new double[] { 2 * value }, "Step 2");
    bounds.applyBounds(a1, step, a2);
    // Should be bounded
    Assertions.assertArrayEquals(a2, new double[] { 2 * value }, "Step 3");
}
Also used : FakeGradientFunction(uk.ac.sussex.gdsc.smlm.function.FakeGradientFunction)

Aggregations

FakeGradientFunction (uk.ac.sussex.gdsc.smlm.function.FakeGradientFunction)28 ArrayList (java.util.ArrayList)25 IntArrayFormatSupplier (uk.ac.sussex.gdsc.test.utils.functions.IntArrayFormatSupplier)11 Gradient1Function (uk.ac.sussex.gdsc.smlm.function.Gradient1Function)8 IndexSupplier (uk.ac.sussex.gdsc.test.utils.functions.IndexSupplier)8 OffsetGradient1Function (uk.ac.sussex.gdsc.smlm.function.OffsetGradient1Function)6 FastLog (uk.ac.sussex.gdsc.smlm.function.FastLog)4 DenseMatrix64F (org.ejml.data.DenseMatrix64F)3 UniformRandomProvider (org.apache.commons.rng.UniformRandomProvider)2 DoubleDoubleBiPredicate (uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate)2 TimingResult (uk.ac.sussex.gdsc.test.utils.TimingResult)2 LogRecord (java.util.logging.LogRecord)1 Test (org.junit.jupiter.api.Test)1 Gradient2Function (uk.ac.sussex.gdsc.smlm.function.Gradient2Function)1 OffsetGradient2Function (uk.ac.sussex.gdsc.smlm.function.OffsetGradient2Function)1