Search in sources :

Example 1 with IndexSupplier

use of uk.ac.sussex.gdsc.test.utils.functions.IndexSupplier 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 2 with IndexSupplier

use of uk.ac.sussex.gdsc.test.utils.functions.IndexSupplier 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 3 with IndexSupplier

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

the class LsqVarianceGradientProcedureTest method gradientProcedureIsFasterUnrolledThanGradientProcedure.

private void gradientProcedureIsFasterUnrolledThanGradientProcedure(RandomSeed seed, final int nparams, final boolean precomputed) {
    Assumptions.assumeTrue(TestSettings.allow(TestComplexity.MEDIUM));
    final int iter = 100;
    final ArrayList<double[]> paramsList = new ArrayList<>(iter);
    createFakeParams(RngUtils.create(seed.getSeed()), nparams, iter, paramsList);
    // Remove the timing of the function call by creating a dummy function
    final FakeGradientFunction f = new FakeGradientFunction(blockWidth, nparams);
    final Gradient1Function func = (precomputed) ? OffsetGradient1Function.wrapGradient1Function(f, SimpleArrayUtils.newArray(f.size(), 0.1, 1.3)) : f;
    final IndexSupplier msg = new IndexSupplier(1, "M ", null);
    for (int i = 0; i < paramsList.size(); i++) {
        final LsqVarianceGradientProcedure p1 = new LsqVarianceGradientProcedure(func);
        p1.variance(paramsList.get(i));
        p1.variance(paramsList.get(i));
        final LsqVarianceGradientProcedure p2 = LsqVarianceGradientProcedureUtils.create(func);
        p2.variance(paramsList.get(i));
        p2.variance(paramsList.get(i));
        // Check they are the same
        Assertions.assertArrayEquals(p1.variance, p2.variance, msg.set(0, 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 < paramsList.size(); i++) {
                final LsqVarianceGradientProcedure p1 = new LsqVarianceGradientProcedure(func);
                for (int j = loops; j-- > 0; ) {
                    p1.variance(paramsList.get(k++ % iter));
                }
            }
        }
    };
    final long time1 = t1.getTime();
    final Timer t2 = new Timer(t1.loops) {

        @Override
        void run() {
            for (int i = 0, k = 0; i < paramsList.size(); i++) {
                final LsqVarianceGradientProcedure p2 = LsqVarianceGradientProcedureUtils.create(func);
                for (int j = loops; j-- > 0; ) {
                    p2.variance(paramsList.get(k++ % iter));
                }
            }
        }
    };
    final long time2 = t2.getTime();
    logger.log(TestLogUtils.getTimingRecord("precomputed=" + precomputed + " Standard " + nparams, time1, "Unrolled", time2));
}
Also used : Gradient1Function(uk.ac.sussex.gdsc.smlm.function.Gradient1Function) OffsetGradient1Function(uk.ac.sussex.gdsc.smlm.function.OffsetGradient1Function) IndexSupplier(uk.ac.sussex.gdsc.test.utils.functions.IndexSupplier) ArrayList(java.util.ArrayList) FakeGradientFunction(uk.ac.sussex.gdsc.smlm.function.FakeGradientFunction)

Example 4 with IndexSupplier

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

the class FastMleGradient2ProcedureTest method gradientProcedureComputesSameLogLikelihoodAsMleGradientCalculator.

private void gradientProcedureComputesSameLogLikelihoodAsMleGradientCalculator(RandomSeed seed, int nparams, DoubleDoubleBiPredicate equality) {
    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 MleGradientCalculator calc = (MleGradientCalculator) GradientCalculatorUtils.newCalculator(nparams, true);
    final IndexSupplier msg = new IndexSupplier(1, "[" + nparams + "] Result: not same @ ", null);
    for (int i = 0; i < paramsList.size(); i++) {
        final FastMleGradient2Procedure p = FastMleGradient2ProcedureUtils.createUnrolled(yList.get(i), func);
        final double s = p.computeLogLikelihood(paramsList.get(i));
        final double s2 = calc.logLikelihood(yList.get(i), paramsList.get(i), func);
        // Virtually the same ...
        TestAssertions.assertTest(s, s2, equality, msg.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 5 with IndexSupplier

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

the class LsqLvmGradientProcedureTest method gradientProcedureComputesSameAsGradientCalculator.

private void gradientProcedureComputesSameAsGradientCalculator(RandomSeed seed, int nparams, BaseLsqLvmGradientProcedureFactory factory) {
    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 GradientCalculator calc = GradientCalculatorUtils.newCalculator(nparams, false);
    final String name = factory.getClass().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 p = factory.createProcedure(yList.get(i), func);
        p.gradient(paramsList.get(i));
        final double s = p.value;
        final double s2 = calc.findLinearised(n, yList.get(i), paramsList.get(i), alpha, beta, func);
        // Exactly the same ...
        Assertions.assertEquals(s, s2, msgR.set(0, i));
        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 : ArrayList(java.util.ArrayList) DenseMatrix64F(org.ejml.data.DenseMatrix64F) IndexSupplier(uk.ac.sussex.gdsc.test.utils.functions.IndexSupplier) FakeGradientFunction(uk.ac.sussex.gdsc.smlm.function.FakeGradientFunction)

Aggregations

IndexSupplier (uk.ac.sussex.gdsc.test.utils.functions.IndexSupplier)9 FakeGradientFunction (uk.ac.sussex.gdsc.smlm.function.FakeGradientFunction)8 ArrayList (java.util.ArrayList)7 Gradient1Function (uk.ac.sussex.gdsc.smlm.function.Gradient1Function)3 DenseMatrix64F (org.ejml.data.DenseMatrix64F)2 FastLog (uk.ac.sussex.gdsc.smlm.function.FastLog)2 OffsetGradient1Function (uk.ac.sussex.gdsc.smlm.function.OffsetGradient1Function)2 FHT (ij.process.FHT)1 FloatProcessor (ij.process.FloatProcessor)1 LogRecord (java.util.logging.LogRecord)1 UniformRandomProvider (org.apache.commons.rng.UniformRandomProvider)1 FhtFilter (uk.ac.sussex.gdsc.smlm.ij.filters.FhtFilter)1 DoubleDoubleBiPredicate (uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate)1 FloatFloatBiPredicate (uk.ac.sussex.gdsc.test.api.function.FloatFloatBiPredicate)1 TestCounter (uk.ac.sussex.gdsc.test.utils.TestCounter)1