Search in sources :

Example 96 with RandomSeed

use of uk.ac.sussex.gdsc.test.junit5.RandomSeed in project GDSC-SMLM by aherbert.

the class PoissonGaussianConvolutionFunctionTest method pdfFasterThanPmf.

@SpeedTag
@SeededTest
void pdfFasterThanPmf(RandomSeed seed) {
    Assumptions.assumeTrue(TestSettings.allow(TestComplexity.MEDIUM));
    // Realistic CCD parameters for speed test
    final double s = 7.16;
    final double g = 3.1;
    final PoissonGaussianConvolutionFunction f1 = PoissonGaussianConvolutionFunction.createWithStandardDeviation(1 / g, s);
    f1.setComputePmf(true);
    final PoissonGaussianConvolutionFunction f2 = PoissonGaussianConvolutionFunction.createWithStandardDeviation(1 / g, s);
    f2.setComputePmf(false);
    final UniformRandomProvider rg = RngUtils.create(seed.getSeed());
    // Generate realistic data from the probability mass function
    final double[][] samples = new double[photons.length][];
    for (int j = 0; j < photons.length; j++) {
        final int start = (int) (4 * -s);
        int mu = start;
        final StoredDataStatistics stats = new StoredDataStatistics();
        while (stats.getSum() < 0.995) {
            final double p = f1.likelihood(mu, photons[j]);
            stats.add(p);
            if (mu > 10 && p / stats.getSum() < 1e-6) {
                break;
            }
            mu++;
        }
        // Generate cumulative probability
        final double[] data = stats.getValues();
        for (int i = 1; i < data.length; i++) {
            data[i] += data[i - 1];
        }
        // Normalise
        for (int i = 0, end = data.length - 1; i < data.length; i++) {
            data[i] /= data[end];
        }
        // Sample
        final double[] sample = new double[1000];
        for (int i = 0; i < sample.length; i++) {
            final double p = rg.nextDouble();
            int x = 0;
            while (x < data.length && data[x] < p) {
                x++;
            }
            sample[i] = start + x;
        }
        samples[j] = sample;
    }
    // Warm-up
    run(f1, samples, photons);
    run(f2, samples, photons);
    long t1 = 0;
    for (int i = 0; i < 5; i++) {
        t1 += run(f1, samples, photons);
    }
    long t2 = 0;
    for (int i = 0; i < 5; i++) {
        t2 += run(f2, samples, photons);
    }
    logger.log(TestLogUtils.getTimingRecord("cdf", t1, "pdf", t2));
}
Also used : StoredDataStatistics(uk.ac.sussex.gdsc.core.utils.StoredDataStatistics) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) SpeedTag(uk.ac.sussex.gdsc.test.junit5.SpeedTag) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Example 97 with RandomSeed

use of uk.ac.sussex.gdsc.test.junit5.RandomSeed in project GDSC-SMLM by aherbert.

the class PrecomputedFunctionTest method precomputedGradient1FunctionWrapsPrecomputedValues.

@SeededTest
void precomputedGradient1FunctionWrapsPrecomputedValues(RandomSeed seed) {
    final int n = 3;
    final UniformRandomProvider r = RngUtils.create(seed.getSeed());
    final int size = 100;
    final double[] v = GdscSmlmTestUtils.generateDoubles(size, r);
    final double[][] g1 = new double[size][];
    for (int i = 0; i < g1.length; i++) {
        g1[i] = GdscSmlmTestUtils.generateDoubles(n, r);
    }
    final Gradient1Function func = new PrecomputedGradient1Function(v, g1);
    final double[][] g1o = new double[size][];
    final double[] vo = evaluateGradient1Function(func, g1o);
    Assertions.assertArrayEquals(v, vo, "values");
    Assertions.assertArrayEquals(g1, g1o, "g1");
}
Also used : UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Example 98 with RandomSeed

use of uk.ac.sussex.gdsc.test.junit5.RandomSeed in project GDSC-SMLM by aherbert.

the class OffsetFunctionTest method offsetGradient1FunctionWrapsPrecomputedValues.

@SeededTest
void offsetGradient1FunctionWrapsPrecomputedValues(RandomSeed seed) {
    final int n = 3;
    final UniformRandomProvider r = RngUtils.create(seed.getSeed());
    final Gradient1Function f0 = new FakeGradientFunction(3, n);
    final int size = f0.size();
    final double[] b1 = GdscSmlmTestUtils.generateDoubles(size, r);
    final double[] b2 = GdscSmlmTestUtils.generateDoubles(size, r);
    final Gradient1Function f1 = OffsetGradient1Function.wrapGradient1Function(f0, b1);
    final Gradient1Function f2 = OffsetGradient1Function.wrapGradient1Function(f1, b2);
    final double[] p = new double[n];
    for (int i = 0; i < n; i++) {
        p[i] = r.nextDouble();
    }
    final double[] d0 = new double[n];
    final double[] d1 = new double[n];
    final double[] d2 = new double[n];
    final double[] v0 = evaluateGradient1Function(f0, p, d0);
    final double[] v1 = evaluateGradient1Function(f1, p, d1);
    final double[] v2 = evaluateGradient1Function(f2, p, d2);
    for (int i = 0; i < v0.length; i++) {
        final double e = v0[i] + b1[i] + b2[i];
        final double o1 = v1[i] + b2[i];
        final double o2 = v2[i];
        Assertions.assertEquals(e, o1, "o1");
        Assertions.assertEquals(e, o2, 1e-6, "o2");
    }
    Assertions.assertArrayEquals(d0, d1, "d1");
    Assertions.assertArrayEquals(d0, d2, "d2");
}
Also used : UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Example 99 with RandomSeed

use of uk.ac.sussex.gdsc.test.junit5.RandomSeed in project GDSC-SMLM by aherbert.

the class ScmosLikelihoodWrapperTest method fitNbEllipticalComputesGradient.

@SeededTest
void fitNbEllipticalComputesGradient(RandomSeed seed) {
    // The elliptical function gradient evaluation is worse
    final DoubleEquality tmp = eq;
    eq = eqPerDatum;
    functionComputesGradient(seed, GaussianFunctionFactory.FIT_SIMPLE_NB_ELLIPTICAL);
    eq = tmp;
}
Also used : DoubleEquality(uk.ac.sussex.gdsc.core.utils.DoubleEquality) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Example 100 with RandomSeed

use of uk.ac.sussex.gdsc.test.junit5.RandomSeed in project GDSC-SMLM by aherbert.

the class FastMleJacobianGradient2ProcedureTest method gradientProcedureComputesSameAsBaseGradientProcedure.

@SeededTest
void gradientProcedureComputesSameAsBaseGradientProcedure(RandomSeed seed) {
    // Test the base functionality of computing the partial derivatives is the same
    final DoubleDoubleBiPredicate equality = TestHelper.doublesAreClose(1e-5, 0);
    gradientProcedureComputesSameAsBaseGradientProcedure(seed, 4, equality);
    gradientProcedureComputesSameAsBaseGradientProcedure(seed, 5, equality);
    gradientProcedureComputesSameAsBaseGradientProcedure(seed, 6, equality);
    gradientProcedureComputesSameAsBaseGradientProcedure(seed, 11, equality);
    gradientProcedureComputesSameAsBaseGradientProcedure(seed, 21, equality);
}
Also used : DoubleDoubleBiPredicate(uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Aggregations

SeededTest (uk.ac.sussex.gdsc.test.junit5.SeededTest)161 UniformRandomProvider (org.apache.commons.rng.UniformRandomProvider)142 DoubleDoubleBiPredicate (uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate)17 Rectangle (java.awt.Rectangle)12 SpeedTag (uk.ac.sussex.gdsc.test.junit5.SpeedTag)12 TimingService (uk.ac.sussex.gdsc.test.utils.TimingService)11 SharedStateContinuousSampler (org.apache.commons.rng.sampling.distribution.SharedStateContinuousSampler)7 TDoubleArrayList (gnu.trove.list.array.TDoubleArrayList)6 FloatProcessor (ij.process.FloatProcessor)5 SingleFreeCircularErfGaussian2DFunction (uk.ac.sussex.gdsc.smlm.function.gaussian.erf.SingleFreeCircularErfGaussian2DFunction)5 TimingResult (uk.ac.sussex.gdsc.test.utils.TimingResult)5 ArrayList (java.util.ArrayList)4 ErfGaussian2DFunction (uk.ac.sussex.gdsc.smlm.function.gaussian.erf.ErfGaussian2DFunction)4 MixtureMultivariateGaussianDistribution (uk.ac.sussex.gdsc.smlm.math3.distribution.fitting.MultivariateGaussianMixtureExpectationMaximization.MixtureMultivariateGaussianDistribution)4 MultivariateGaussianDistribution (uk.ac.sussex.gdsc.smlm.math3.distribution.fitting.MultivariateGaussianMixtureExpectationMaximization.MixtureMultivariateGaussianDistribution.MultivariateGaussianDistribution)4 DoubleConvolutionValueProcedure (uk.ac.sussex.gdsc.smlm.utils.Convolution.DoubleConvolutionValueProcedure)4 FloatFloatBiPredicate (uk.ac.sussex.gdsc.test.api.function.FloatFloatBiPredicate)4 MixtureMultivariateNormalDistribution (org.apache.commons.math3.distribution.MixtureMultivariateNormalDistribution)3 MultivariateNormalMixtureExpectationMaximization (org.apache.commons.math3.distribution.fitting.MultivariateNormalMixtureExpectationMaximization)3 DoubleEquality (uk.ac.sussex.gdsc.core.utils.DoubleEquality)3