Search in sources :

Example 1 with IntArrayFormatSupplier

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

the class ImageJImagePeakResultsTest method checkCanAddUsingDifferentMethods.

private void checkCanAddUsingDifferentMethods(RandomSeed seed, int displayFlags) {
    final UniformRandomProvider rand = RngUtils.create(seed.getSeed());
    displayFlags |= ImageJImagePeakResults.DISPLAY_SIGNAL;
    final ImageJImagePeakResults[] r = new ImageJImagePeakResults[8];
    final PSF psf = PsfHelper.create(PSFType.ONE_AXIS_GAUSSIAN_2D);
    for (int i = 0; i < r.length; i++) {
        r[i] = new ImageJImagePeakResults(title + i, bounds, 1);
        r[i].setDisplayFlags(displayFlags);
        r[i].setPsf(psf);
        begin(r[i]);
    }
    final int size = 20;
    final int[] t = new int[size];
    final float[] x = new float[size];
    final float[] y = new float[size];
    final float[] v = new float[size];
    for (int i = 0; i < size; i++) {
        t[i] = i;
        x[i] = (rand.nextFloat() * bounds.width);
        y[i] = (rand.nextFloat() * bounds.height);
        v[i] = (rand.nextFloat());
        addPeakResult(r[0], x[i], y[i], v[i]);
        addPeakResult(r[1], t[i], x[i], y[i], v[i]);
        addValue(r[2], x[i], y[i], v[i]);
        addValue(r[3], t[i], x[i], y[i], v[i]);
    }
    addPeakResults(r[4], x, y, v);
    addPeakResults(r[5], t, x, y, v);
    addValues(r[6], x, y, v);
    addValues(r[7], t, x, y, v);
    final float[][] image = new float[r.length][];
    for (int i = 0; i < r.length; i++) {
        r[i].end();
        image[i] = getImage(r[i]);
        logger.log(TestLogUtils.getRecord(Level.FINE, "[%d] = %s", i, Arrays.toString(image[i])));
    }
    // Test single value adds
    float[] expecteds = image[0];
    IntArrayFormatSupplier msg = new IntArrayFormatSupplier("Single add image %d", 1);
    for (int i = 1; i < 4; i++) {
        final float[] actuals = image[i];
        Assertions.assertArrayEquals(expecteds, actuals, msg.set(0, i));
    }
    // Test multi value adds
    expecteds = image[4];
    msg = new IntArrayFormatSupplier("Multi add image %d", 1);
    for (int i = 5; i < image.length; i++) {
        final float[] actuals = image[i];
        Assertions.assertArrayEquals(expecteds, actuals, msg.set(0, i));
    }
    // Test they are roughly the same (differences occur due to floating point summation
    Assertions.assertArrayEquals(expecteds, image[0], 1e-5f, "Single != Multi");
}
Also used : PSF(uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.PSF) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) IntArrayFormatSupplier(uk.ac.sussex.gdsc.test.utils.functions.IntArrayFormatSupplier)

Example 2 with IntArrayFormatSupplier

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

the class WPoissonGradientProcedureTest method gradientProcedureUnrolledComputesSameAsGradientProcedure.

private void gradientProcedureUnrolledComputesSameAsGradientProcedure(RandomSeed seed, int nparams, boolean precomputed) {
    final int iter = 10;
    final ArrayList<double[]> paramsList = new ArrayList<>(iter);
    final UniformRandomProvider rng = RngUtils.create(seed.getSeed());
    createFakeParams(rng, nparams, iter, paramsList);
    final Gradient1Function func = new FakeGradientFunction(blockWidth, nparams);
    final double[] v = (precomputed) ? dataCache.computeIfAbsent(seed, WPoissonGradientProcedureTest::createData) : null;
    final IntArrayFormatSupplier msg = getMessage(nparams, "[%d] Observations: Not same linear @ %d");
    for (int i = 0; i < paramsList.size(); i++) {
        final double[] y = createFakeData(rng);
        final WPoissonGradientProcedure p1 = new WPoissonGradientProcedure(y, v, func);
        p1.computeFisherInformation(paramsList.get(i));
        final WPoissonGradientProcedure p2 = WPoissonGradientProcedureUtils.create(y, v, func);
        p2.computeFisherInformation(paramsList.get(i));
        // Exactly the same ...
        Assertions.assertArrayEquals(p1.getLinear(), p2.getLinear(), msg.set(1, i));
    }
}
Also used : Gradient1Function(uk.ac.sussex.gdsc.smlm.function.Gradient1Function) ArrayList(java.util.ArrayList) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) IntArrayFormatSupplier(uk.ac.sussex.gdsc.test.utils.functions.IntArrayFormatSupplier) FakeGradientFunction(uk.ac.sussex.gdsc.smlm.function.FakeGradientFunction)

Example 3 with IntArrayFormatSupplier

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

the class WPoissonGradientProcedureTest method gradientProcedureIsFasterThanWLseGradientProcedure.

private void gradientProcedureIsFasterThanWLseGradientProcedure(RandomSeed seed, final int nparams) {
    Assumptions.assumeTrue(TestSettings.allow(TestComplexity.MEDIUM));
    final int iter = 100;
    final ArrayList<double[]> paramsList = new ArrayList<>(iter);
    final ArrayList<double[]> yList = new ArrayList<>(iter);
    final double[] var = dataCache.computeIfAbsent(seed, WPoissonGradientProcedureTest::createData);
    createFakeData(RngUtils.create(seed.getSeed()), nparams, iter, paramsList, yList);
    // Remove the timing of the function call by creating a dummy function
    final FakeGradientFunction func = new FakeGradientFunction(blockWidth, nparams);
    final IntArrayFormatSupplier msg = new IntArrayFormatSupplier("M [%d]", 1);
    for (int i = 0; i < paramsList.size(); i++) {
        final double[] y = yList.get(i);
        final WLsqLvmGradientProcedure p1 = WLsqLvmGradientProcedureUtils.create(y, var, func);
        p1.gradient(paramsList.get(i));
        final WPoissonGradientProcedure p2 = WPoissonGradientProcedureUtils.create(y, var, func);
        p2.computeFisherInformation(paramsList.get(i));
        // Check they are the same
        Assertions.assertArrayEquals(p1.getAlphaLinear(), p2.getLinear(), 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 WLsqLvmGradientProcedure p1 = WLsqLvmGradientProcedureUtils.create(yList.get(i), var, func);
                for (int j = loops; j-- > 0; ) {
                    p1.gradient(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 WPoissonGradientProcedure p2 = WPoissonGradientProcedureUtils.create(yList.get(i), var, func);
                for (int j = loops; j-- > 0; ) {
                    p2.computeFisherInformation(paramsList.get(k++ % iter));
                }
            }
        }
    };
    final long time2 = t2.getTime();
    logger.log(TestLogUtils.getTimingRecord("WLsqLvmGradientProcedure " + nparams, time1, "WPoissonGradientProcedure", time2));
}
Also used : ArrayList(java.util.ArrayList) IntArrayFormatSupplier(uk.ac.sussex.gdsc.test.utils.functions.IntArrayFormatSupplier) FakeGradientFunction(uk.ac.sussex.gdsc.smlm.function.FakeGradientFunction)

Example 4 with IntArrayFormatSupplier

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

the class GradientCalculatorSpeedTest method gradientCalculatorNComputesSameAsGradientCalculator.

private void gradientCalculatorNComputesSameAsGradientCalculator(RandomSeed seed, Gaussian2DFunction func, int nparams, boolean mle) {
    // Check the function is the correct size
    Assertions.assertEquals(nparams, func.gradientIndices().length);
    final int iter = 50;
    final double[][] alpha = new double[nparams][nparams];
    final double[] beta = new double[nparams];
    final double[][] alpha2 = new double[nparams][nparams];
    final double[] beta2 = new double[nparams];
    final ArrayList<double[]> paramsList = new ArrayList<>(iter);
    final ArrayList<double[]> yList = new ArrayList<>(iter);
    final int[] x = createData(RngUtils.create(seed.getSeed()), 1, iter, paramsList, yList);
    final GradientCalculator calc = (mle) ? new MleGradientCalculator(beta.length) : new GradientCalculator(beta.length);
    final GradientCalculator calc2 = GradientCalculatorUtils.newCalculator(nparams, mle);
    // Create messages
    IntArrayFormatSupplier msgR = new IntArrayFormatSupplier("Result: Not same @ %d", 1);
    IntArrayFormatSupplier msgB = new IntArrayFormatSupplier("Observations: Not same beta @ %d [%d]", 2);
    IntArrayFormatSupplier msgA = new IntArrayFormatSupplier("Observations: Not same alpha @ %d [%d][%d]", 3);
    for (int i = 0; i < paramsList.size(); i++) {
        final double s = calc.findLinearised(x, yList.get(i), paramsList.get(i), alpha, beta, func);
        final double s2 = calc2.findLinearised(x, yList.get(i), paramsList.get(i), alpha2, beta2, func);
        Assertions.assertTrue(eq.almostEqualRelativeOrAbsolute(s, s2), msgR.set(0, i));
        msgB.set(0, i);
        for (int j = 0; j < beta.length; j++) {
            Assertions.assertTrue(eq.almostEqualRelativeOrAbsolute(beta[j], beta2[j]), msgB.set(1, j));
        }
        msgA.set(0, i);
        for (int j = 0; j < beta.length; j++) {
            msgA.set(1, j);
            for (int k = 0; k < alpha[j].length; k++) {
                Assertions.assertTrue(eq.almostEqualRelativeOrAbsolute(alpha[j][k], alpha2[j][k]), msgA.set(2, k));
            }
        }
    }
    msgR = new IntArrayFormatSupplier("N-Result: Not same @ %d", 1);
    msgB = new IntArrayFormatSupplier("N-Observations: Not same beta @ %d [%d]", 2);
    msgA = new IntArrayFormatSupplier("N-Observations: Not same alpha @ %d [%d][%d]", 3);
    for (int i = 0; i < paramsList.size(); i++) {
        final double s = calc.findLinearised(x.length, yList.get(i), paramsList.get(i), alpha, beta, func);
        final double s2 = calc2.findLinearised(x.length, yList.get(i), paramsList.get(i), alpha2, beta2, func);
        Assertions.assertTrue(eq.almostEqualRelativeOrAbsolute(s, s2), msgR.set(0, i));
        msgB.set(0, i);
        for (int j = 0; j < beta.length; j++) {
            Assertions.assertTrue(eq.almostEqualRelativeOrAbsolute(beta[j], beta2[j]), msgB.set(1, j));
        }
        msgA.set(0, i);
        for (int j = 0; j < beta.length; j++) {
            msgA.set(1, j);
            for (int k = 0; k < alpha[j].length; k++) {
                Assertions.assertTrue(eq.almostEqualRelativeOrAbsolute(alpha[j][k], alpha2[j][k]), msgA.set(2, k));
            }
        }
    }
    if (!mle) {
        func.setNoiseModel(CameraNoiseModel.createNoiseModel(10, 0, true));
        msgR = new IntArrayFormatSupplier("Result+Noise: Not same @ %d", 1);
        msgB = new IntArrayFormatSupplier("Observations+Noise: Not same beta @ %d [%d]", 2);
        msgA = new IntArrayFormatSupplier("Observations+Noise: Not same alpha @ %d [%d][%d]", 3);
        for (int i = 0; i < paramsList.size(); i++) {
            final double s = calc.findLinearised(x, yList.get(i), paramsList.get(i), alpha, beta, func);
            final double s2 = calc2.findLinearised(x, yList.get(i), paramsList.get(i), alpha2, beta2, func);
            Assertions.assertTrue(eq.almostEqualRelativeOrAbsolute(s, s2), msgR.set(0, i));
            msgB.set(0, i);
            for (int j = 0; j < beta.length; j++) {
                Assertions.assertTrue(eq.almostEqualRelativeOrAbsolute(beta[j], beta2[j]), msgB.set(1, j));
            }
            msgA.set(0, i);
            for (int j = 0; j < beta.length; j++) {
                msgA.set(1, j);
                for (int k = 0; k < alpha[j].length; k++) {
                    Assertions.assertTrue(eq.almostEqualRelativeOrAbsolute(alpha[j][k], alpha2[j][k]), msgA.set(2, k));
                }
            }
        }
        msgR = new IntArrayFormatSupplier("N-Result+Noise: Not same @ %d", 1);
        msgB = new IntArrayFormatSupplier("N-Observations+Noise: Not same beta @ %d [%d]", 2);
        msgA = new IntArrayFormatSupplier("N-Observations+Noise: Not same alpha @ %d [%d][%d]", 3);
        for (int i = 0; i < paramsList.size(); i++) {
            final double s = calc.findLinearised(x.length, yList.get(i), paramsList.get(i), alpha, beta, func);
            final double s2 = calc2.findLinearised(x.length, yList.get(i), paramsList.get(i), alpha2, beta2, func);
            Assertions.assertTrue(eq.almostEqualRelativeOrAbsolute(s, s2), msgR.set(0, i));
            msgB.set(0, i);
            for (int j = 0; j < beta.length; j++) {
                Assertions.assertTrue(eq.almostEqualRelativeOrAbsolute(beta[j], beta2[j]), msgB.set(1, j));
            }
            msgA.set(0, i);
            for (int j = 0; j < beta.length; j++) {
                msgA.set(1, j);
                for (int k = 0; k < alpha[j].length; k++) {
                    Assertions.assertTrue(eq.almostEqualRelativeOrAbsolute(alpha[j][k], alpha2[j][k]), msgA.set(2, k));
                }
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) IntArrayFormatSupplier(uk.ac.sussex.gdsc.test.utils.functions.IntArrayFormatSupplier)

Example 5 with IntArrayFormatSupplier

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

the class GradientCalculatorSpeedTest method gradientCalculatorComputesGradient.

private void gradientCalculatorComputesGradient(RandomSeed seed, GradientCalculator calc) {
    final int nparams = calc.nparams;
    final Gaussian2DFunction func = new SingleEllipticalGaussian2DFunction(blockWidth, blockWidth);
    // Check the function is the correct size
    final int[] indices = func.gradientIndices();
    Assertions.assertEquals(nparams, indices.length);
    final int iter = 50;
    final double[] beta = new double[nparams];
    final double[] beta2 = new double[nparams];
    final ArrayList<double[]> paramsList = new ArrayList<>(iter);
    final ArrayList<double[]> yList = new ArrayList<>(iter);
    final int[] x = createData(RngUtils.create(seed.getSeed()), 1, iter, paramsList, yList, true);
    final double delta = 1e-3;
    final DoubleEquality eq = new DoubleEquality(1e-3, 1e-3);
    final IntArrayFormatSupplier msg = new IntArrayFormatSupplier("[%d] Not same gradient @ %d", 2);
    for (int i = 0; i < paramsList.size(); i++) {
        msg.set(0, i);
        final double[] y = yList.get(i);
        final double[] a = paramsList.get(i);
        final double[] a2 = a.clone();
        // double s =
        calc.evaluate(x, y, a, beta, func);
        for (int k = 0; k < nparams; k++) {
            final int j = indices[k];
            final double d = Precision.representableDelta(a[j], (a[j] == 0) ? 1e-3 : a[j] * delta);
            a2[j] = a[j] + d;
            final double s1 = calc.evaluate(x, y, a2, beta2, func);
            a2[j] = a[j] - d;
            final double s2 = calc.evaluate(x, y, a2, beta2, func);
            a2[j] = a[j];
            final double gradient = (s1 - s2) / (2 * d);
            // logger.fine(FunctionUtils.getSupplier("[%d,%d] %f (%s %f+/-%f) %f ?= %f", i, j, s,
            // func.getName(j), a[j], d, beta[k],
            // gradient));
            Assertions.assertTrue(eq.almostEqualRelativeOrAbsolute(beta[k], gradient), msg.set(1, j));
        }
    }
}
Also used : SingleFreeCircularGaussian2DFunction(uk.ac.sussex.gdsc.smlm.function.gaussian.SingleFreeCircularGaussian2DFunction) SingleEllipticalGaussian2DFunction(uk.ac.sussex.gdsc.smlm.function.gaussian.SingleEllipticalGaussian2DFunction) SingleNbFixedGaussian2DFunction(uk.ac.sussex.gdsc.smlm.function.gaussian.SingleNbFixedGaussian2DFunction) EllipticalGaussian2DFunction(uk.ac.sussex.gdsc.smlm.function.gaussian.EllipticalGaussian2DFunction) SingleFixedGaussian2DFunction(uk.ac.sussex.gdsc.smlm.function.gaussian.SingleFixedGaussian2DFunction) Gaussian2DFunction(uk.ac.sussex.gdsc.smlm.function.gaussian.Gaussian2DFunction) SingleCircularGaussian2DFunction(uk.ac.sussex.gdsc.smlm.function.gaussian.SingleCircularGaussian2DFunction) SingleEllipticalGaussian2DFunction(uk.ac.sussex.gdsc.smlm.function.gaussian.SingleEllipticalGaussian2DFunction) ArrayList(java.util.ArrayList) DoubleEquality(uk.ac.sussex.gdsc.core.utils.DoubleEquality) IntArrayFormatSupplier(uk.ac.sussex.gdsc.test.utils.functions.IntArrayFormatSupplier)

Aggregations

IntArrayFormatSupplier (uk.ac.sussex.gdsc.test.utils.functions.IntArrayFormatSupplier)18 ArrayList (java.util.ArrayList)13 FakeGradientFunction (uk.ac.sussex.gdsc.smlm.function.FakeGradientFunction)11 Gradient1Function (uk.ac.sussex.gdsc.smlm.function.Gradient1Function)5 OffsetGradient1Function (uk.ac.sussex.gdsc.smlm.function.OffsetGradient1Function)4 UniformRandomProvider (org.apache.commons.rng.UniformRandomProvider)3 DoubleDoubleBiPredicate (uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate)2 PoissonDistribution (org.apache.commons.math3.distribution.PoissonDistribution)1 DenseMatrix64F (org.ejml.data.DenseMatrix64F)1 DoubleEquality (uk.ac.sussex.gdsc.core.utils.DoubleEquality)1 PSF (uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.PSF)1 FastLog (uk.ac.sussex.gdsc.smlm.function.FastLog)1 EllipticalGaussian2DFunction (uk.ac.sussex.gdsc.smlm.function.gaussian.EllipticalGaussian2DFunction)1 Gaussian2DFunction (uk.ac.sussex.gdsc.smlm.function.gaussian.Gaussian2DFunction)1 SingleCircularGaussian2DFunction (uk.ac.sussex.gdsc.smlm.function.gaussian.SingleCircularGaussian2DFunction)1 SingleEllipticalGaussian2DFunction (uk.ac.sussex.gdsc.smlm.function.gaussian.SingleEllipticalGaussian2DFunction)1 SingleFixedGaussian2DFunction (uk.ac.sussex.gdsc.smlm.function.gaussian.SingleFixedGaussian2DFunction)1 SingleFreeCircularGaussian2DFunction (uk.ac.sussex.gdsc.smlm.function.gaussian.SingleFreeCircularGaussian2DFunction)1 SingleNbFixedGaussian2DFunction (uk.ac.sussex.gdsc.smlm.function.gaussian.SingleNbFixedGaussian2DFunction)1 TimingResult (uk.ac.sussex.gdsc.test.utils.TimingResult)1