Search in sources :

Example 11 with FakeGradientFunction

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

the class LsqVarianceGradientProcedureTest method gradientProcedureIsNotSlowerThanGradientCalculator.

private void gradientProcedureIsNotSlowerThanGradientCalculator(RandomSeed seed, final int nparams) {
    Assumptions.assumeTrue(TestSettings.allow(TestComplexity.MEDIUM));
    final int iter = 1000;
    final ArrayList<double[]> paramsList = new ArrayList<>(iter);
    createFakeParams(RngUtils.create(seed.getSeed()), nparams, iter, paramsList);
    final int n = blockWidth * blockWidth;
    final FakeGradientFunction func = new FakeGradientFunction(blockWidth, nparams);
    final GradientCalculator calc = GradientCalculatorUtils.newCalculator(nparams, false);
    for (int i = 0; i < paramsList.size(); i++) {
        calc.variance(n, paramsList.get(i), func);
    }
    for (int i = 0; i < paramsList.size(); i++) {
        final LsqVarianceGradientProcedure p = LsqVarianceGradientProcedureUtils.create(func);
        p.variance(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.variance(n, paramsList.get(k++ % iter), 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 LsqVarianceGradientProcedure p = LsqVarianceGradientProcedureUtils.create(func);
                for (int j = loops; j-- > 0; ) {
                    p.variance(paramsList.get(k++ % iter));
                }
            }
        }
    };
    final long time2 = t2.getTime();
    logger.log(TestLogUtils.getTimingRecord("GradientCalculator " + nparams, time1, "LSQVarianceGradientProcedure", time2));
}
Also used : ArrayList(java.util.ArrayList) FakeGradientFunction(uk.ac.sussex.gdsc.smlm.function.FakeGradientFunction)

Example 12 with FakeGradientFunction

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

the class PoissonGradientProcedureTest method gradientProcedureComputesSameAsGradientCalculator.

private void gradientProcedureComputesSameAsGradientCalculator(RandomSeed seed, int nparams) {
    final int iter = 10;
    final ArrayList<double[]> paramsList = new ArrayList<>(iter);
    createFakeParams(RngUtils.create(seed.getSeed()), nparams, iter, paramsList);
    final int n = blockWidth * blockWidth;
    final FakeGradientFunction func = new FakeGradientFunction(blockWidth, nparams);
    final GradientCalculator calc = GradientCalculatorUtils.newCalculator(nparams, false);
    // Create messages
    final IntArrayFormatSupplier msgOal = getMessage(nparams, "[%d] Observations: Not same alpha linear @ %d");
    final IntArrayFormatSupplier msgOam = getMessage(nparams, "[%d] Observations: Not same alpha matrix @ %d");
    final DoubleDoubleBiPredicate predicate = TestHelper.doublesAreClose(1e-10, 0);
    for (int i = 0; i < paramsList.size(); i++) {
        final PoissonGradientProcedure p = PoissonGradientProcedureUtils.create(func);
        p.computeFisherInformation(paramsList.get(i));
        final double[][] m = calc.fisherInformationMatrix(n, paramsList.get(i), func);
        // Not exactly the same ...
        final double[] al = p.getLinear();
        TestAssertions.assertArrayTest(al, new DenseMatrix64F(m).data, predicate, msgOal.set(1, i));
        final double[][] am = p.getMatrix();
        TestAssertions.assertArrayTest(am, m, predicate, msgOam.set(1, i));
    }
}
Also used : DoubleDoubleBiPredicate(uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate) ArrayList(java.util.ArrayList) IntArrayFormatSupplier(uk.ac.sussex.gdsc.test.utils.functions.IntArrayFormatSupplier) FakeGradientFunction(uk.ac.sussex.gdsc.smlm.function.FakeGradientFunction) DenseMatrix64F(org.ejml.data.DenseMatrix64F)

Example 13 with FakeGradientFunction

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

the class PoissonGradientProcedureTest method gradientProcedureUnrolledComputesSameAsGradientProcedure.

private void gradientProcedureUnrolledComputesSameAsGradientProcedure(RandomSeed seed, int nparams, boolean precomputed) {
    final int iter = 10;
    final ArrayList<double[]> paramsList = new ArrayList<>(iter);
    createFakeParams(RngUtils.create(seed.getSeed()), nparams, iter, paramsList);
    Gradient1Function func = new FakeGradientFunction(blockWidth, nparams);
    if (precomputed) {
        func = OffsetGradient1Function.wrapGradient1Function(func, SimpleArrayUtils.newArray(func.size(), 0.1, 1.3));
    }
    // Create messages
    final IntArrayFormatSupplier msgOal = getMessage(nparams, "[%d] Observations: Not same alpha linear @ %d");
    final IntArrayFormatSupplier msgOam = getMessage(nparams, "[%d] Observations: Not same alpha matrix @ %d");
    for (int i = 0; i < paramsList.size(); i++) {
        final PoissonGradientProcedure p1 = new PoissonGradientProcedure(func);
        p1.computeFisherInformation(paramsList.get(i));
        final PoissonGradientProcedure p2 = PoissonGradientProcedureUtils.create(func);
        p2.computeFisherInformation(paramsList.get(i));
        // Exactly the same ...
        Assertions.assertArrayEquals(p1.getLinear(), p2.getLinear(), msgOal.set(1, i));
        final double[][] am1 = p1.getMatrix();
        final double[][] am2 = p2.getMatrix();
        Assertions.assertArrayEquals(am1, am2, msgOam.set(1, i));
    }
}
Also used : Gradient1Function(uk.ac.sussex.gdsc.smlm.function.Gradient1Function) OffsetGradient1Function(uk.ac.sussex.gdsc.smlm.function.OffsetGradient1Function) ArrayList(java.util.ArrayList) IntArrayFormatSupplier(uk.ac.sussex.gdsc.test.utils.functions.IntArrayFormatSupplier) FakeGradientFunction(uk.ac.sussex.gdsc.smlm.function.FakeGradientFunction)

Example 14 with FakeGradientFunction

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

the class FastMleGradient2ProcedureTest 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);
    // Create messages
    final IntArrayFormatSupplier msgLl = getMessage(nparams, "[%d] LL: Not same @ %d");
    final IntArrayFormatSupplier msg1Dv = getMessage(nparams, "[%d] first derivative: Not same value @ %d");
    final IntArrayFormatSupplier msg1Dd1 = getMessage(nparams, "[%d] first derivative: Not same d1 @ %d");
    final IntArrayFormatSupplier msg2Dv = getMessage(nparams, "[%d] second derivative: Not same value @ %d");
    final IntArrayFormatSupplier msg2Dd1 = getMessage(nparams, "[%d] second derivative: Not same d1 @ %d");
    final IntArrayFormatSupplier msg2Dd2 = getMessage(nparams, "[%d] second derivative: Not same d2 @ %d");
    for (int i = 0; i < paramsList.size(); i++) {
        FastMleGradient2Procedure p1 = new FastMleGradient2Procedure(yList.get(i), func);
        FastMleGradient2Procedure p2 = FastMleGradient2ProcedureUtils.createUnrolled(yList.get(i), func);
        final double[] a = paramsList.get(i);
        final double ll1 = p1.computeLogLikelihood(a);
        final double ll2 = p2.computeLogLikelihood(a);
        Assertions.assertEquals(ll1, ll2, msgLl.set(1, i));
        p1 = new FastMleGradient2Procedure(yList.get(i), func);
        p2 = FastMleGradient2ProcedureUtils.createUnrolled(yList.get(i), func);
        p1.computeFirstDerivative(a);
        p2.computeFirstDerivative(a);
        Assertions.assertArrayEquals(p1.u, p2.u, msg1Dv.set(1, i));
        Assertions.assertArrayEquals(p1.d1, p2.d1, msg1Dd1.set(1, i));
        p1 = new FastMleGradient2Procedure(yList.get(i), func);
        p2 = FastMleGradient2ProcedureUtils.createUnrolled(yList.get(i), func);
        p1.computeSecondDerivative(a);
        p2.computeSecondDerivative(a);
        Assertions.assertArrayEquals(p1.u, p2.u, msg2Dv.set(1, i));
        Assertions.assertArrayEquals(p1.d1, p2.d1, msg2Dd1.set(1, i));
        Assertions.assertArrayEquals(p1.d2, p2.d2, msg2Dd2.set(1, i));
    }
}
Also used : ArrayList(java.util.ArrayList) IntArrayFormatSupplier(uk.ac.sussex.gdsc.test.utils.functions.IntArrayFormatSupplier) FakeGradientFunction(uk.ac.sussex.gdsc.smlm.function.FakeGradientFunction)

Example 15 with FakeGradientFunction

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

the class FastMleGradient2ProcedureTest method gradientProcedureIsNotSlowerThanGradientCalculator.

private void gradientProcedureIsNotSlowerThanGradientCalculator(RandomSeed seed, final int nparams) {
    Assumptions.assumeTrue(TestSettings.allow(TestComplexity.MEDIUM));
    final int iter = 1000;
    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 MleGradientCalculator calc = (MleGradientCalculator) GradientCalculatorUtils.newCalculator(nparams, true);
    for (int i = 0; i < paramsList.size(); i++) {
        calc.logLikelihood(yList.get(i), paramsList.get(i), func);
    }
    for (int i = 0; i < paramsList.size(); i++) {
        final FastMleGradient2Procedure p = FastMleGradient2ProcedureUtils.createUnrolled(yList.get(i), func);
        p.computeLogLikelihood(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 MleGradientCalculator calc = (MleGradientCalculator) GradientCalculatorUtils.newCalculator(nparams, true);
                for (int j = loops; j-- > 0; ) {
                    calc.logLikelihood(yList.get(i), paramsList.get(k++ % iter), 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 FastMleGradient2Procedure p = FastMleGradient2ProcedureUtils.createUnrolled(yList.get(i), func);
                for (int j = loops; j-- > 0; ) {
                    p.computeLogLikelihood(paramsList.get(k++ % iter));
                }
            }
        }
    };
    final long time2 = t2.getTime();
    logger.log(TestLogUtils.getTimingRecord("GradientCalculator " + nparams, time1, "FastMLEGradient2Procedure", time2));
}
Also used : ArrayList(java.util.ArrayList) 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