Search in sources :

Example 26 with DoubleDoubleBiPredicate

use of uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate in project GDSC-SMLM by aherbert.

the class Gaussian2DPeakResultHelperTest method canComputeMeanSignalUsingP.

@SeededTest
void canComputeMeanSignalUsingP(RandomSeed seed) {
    final UniformRandomProvider rg = RngUtils.create(seed.getSeed());
    final DoubleDoubleBiPredicate predicate = TestHelper.doublesAreClose(1e-10, 0);
    for (int i = 0; i < 10; i++) {
        final double intensity = rg.nextDouble() * 100;
        final double sx = rg.nextDouble() * 2;
        final double sy = rg.nextDouble() * 2;
        final double p = rg.nextDouble();
        double expected = intensity * p / (Math.PI * MathUtils.pow2(Gaussian2DPeakResultHelper.inverseCumulative2D(p)) * sx * sy);
        double observed = Gaussian2DPeakResultHelper.getMeanSignalUsingP(intensity, sx, sy, p);
        assertEquals(expected, observed, predicate);
        // Test fixed versions verse dynamic
        expected = Gaussian2DPeakResultHelper.getMeanSignalUsingP(intensity, sx, sy, 0.5);
        observed = Gaussian2DPeakResultHelper.getMeanSignalUsingP05(intensity, sx, sy);
        assertEquals(expected, observed, predicate);
    }
}
Also used : DoubleDoubleBiPredicate(uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Example 27 with DoubleDoubleBiPredicate

use of uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate in project GDSC-SMLM by aherbert.

the class Gaussian2DPeakResultHelperTest method canComputeMeanSignalUsingR.

@SeededTest
void canComputeMeanSignalUsingR(RandomSeed seed) {
    final UniformRandomProvider rg = RngUtils.create(seed.getSeed());
    final DoubleDoubleBiPredicate predicate = TestHelper.doublesAreClose(1e-10, 0);
    for (int i = 0; i < 10; i++) {
        final double intensity = rg.nextDouble() * 100;
        final double sx = rg.nextDouble() * 2;
        final double sy = rg.nextDouble() * 2;
        final double r = rg.nextDouble() * 5;
        assertEquals(intensity * Gaussian2DPeakResultHelper.cumulative2D(r) / (Math.PI * r * r * sx * sy), Gaussian2DPeakResultHelper.getMeanSignalUsingR(intensity, sx, sy, r), predicate);
        // Test fixed versions verse dynamic
        assertEquals(Gaussian2DPeakResultHelper.getMeanSignalUsingR(intensity, sx, sy, 1), Gaussian2DPeakResultHelper.getMeanSignalUsingR1(intensity, sx, sy), predicate);
        assertEquals(Gaussian2DPeakResultHelper.getMeanSignalUsingR(intensity, sx, sy, 2), Gaussian2DPeakResultHelper.getMeanSignalUsingR2(intensity, sx, sy), predicate);
    }
}
Also used : DoubleDoubleBiPredicate(uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Example 28 with DoubleDoubleBiPredicate

use of uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate in project GDSC-SMLM by aherbert.

the class PoissonFunctionTest method probabilityMatchesLogProbabilty.

private static void probabilityMatchesLogProbabilty(final double gain, final double mu) {
    final double o = mu;
    final PoissonFunction f = new PoissonFunction(1.0 / gain);
    final double p = 0;
    final int[] range = getRange(gain, mu);
    final int min = range[0];
    final int max = range[1];
    // Allow a relative difference or an exact match (for the case of -Infinity)
    final DoublePredicate isNegativeInfinity = TestHelper.doubleEquals(Double.NEGATIVE_INFINITY);
    final DoubleDoubleBiPredicate predicate = TestHelper.doublesAreClose(1e-8, 0).or(TestHelper.and(isNegativeInfinity, isNegativeInfinity));
    for (int x = min; x <= max; x++) {
        final double v1 = Math.log(f.likelihood(x, o));
        final double v2 = f.logLikelihood(x, o);
        // System.out.printf("x=%d, v1=%s, v2=%s%n", x, v1, v2);
        TestAssertions.assertTest(v1, v2, predicate, FunctionUtils.getSupplier("g=%f, mu=%f, x=%d", gain, mu, x));
    }
}
Also used : DoubleDoubleBiPredicate(uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate) DoublePredicate(uk.ac.sussex.gdsc.test.api.function.DoublePredicate)

Example 29 with DoubleDoubleBiPredicate

use of uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate in project GDSC-SMLM by aherbert.

the class PoissonGammaGaussianFunctionTest method cumulativeGaussianProbabilityIsCorrect.

private static void cumulativeGaussianProbabilityIsCorrect(double sd, double gain) {
    // Read noise should be in proportion to the camera gain
    final PoissonGammaGaussianFunction f = new PoissonGammaGaussianFunction(1 / gain, sd);
    final double range = 5 * sd;
    final int upper = (int) Math.ceil(range);
    final int lower = (int) Math.floor(-range);
    final SimpsonIntegrator in = new SimpsonIntegrator(1e-4, 1e-8, 3, 32);
    final UnivariateFunction uf = new UnivariateFunction() {

        @Override
        public double value(double x) {
            return f.gaussianPdf(x);
        }
    };
    final DoubleDoubleBiPredicate integratePredicate = TestHelper.doublesAreClose(0.1, 0);
    final DoubleDoubleBiPredicate rangePredicate = TestHelper.doublesAreClose(1e-6, 0);
    for (int u = lower; u <= upper; u++) {
        final double ux = u + 0.5;
        final double lx = u - 0.5;
        final double e = in.integrate(20000, uf, lx, ux);
        final double o = f.gaussianCdf(ux) - f.gaussianCdf(lx);
        final double o2 = f.gaussianCdf(lx, ux);
        TestAssertions.assertTest(e, o, integratePredicate);
        TestAssertions.assertTest(o, o2, rangePredicate);
    }
}
Also used : SimpsonIntegrator(org.apache.commons.math3.analysis.integration.SimpsonIntegrator) DoubleDoubleBiPredicate(uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate) UnivariateFunction(org.apache.commons.math3.analysis.UnivariateFunction)

Example 30 with DoubleDoubleBiPredicate

use of uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate in project GDSC-SMLM by aherbert.

the class PoissonPoissonFunctionTest method probabilityMatchesLogProbability.

private static void probabilityMatchesLogProbability(final double gain, double mu, final double sd) {
    // Note: The input s parameter is pre-gain.
    final PoissonPoissonFunction f = PoissonPoissonFunction.createWithStandardDeviation(1.0 / gain, sd * gain);
    // Evaluate an initial range.
    // Gaussian should have >99% within +/- s
    // Poisson will have mean mu with a variance mu.
    // At large mu it is approximately normal so use 3 sqrt(mu) for the range added to the mean
    final int[] range = PoissonGaussianFunctionTest.getRange(gain, mu, sd);
    final int min = range[0];
    final int max = range[1];
    // Note: The input mu parameter is pre-gain.
    final double e = mu;
    final Supplier<String> msg = () -> String.format("g=%f, mu=%f, s=%f", gain, mu, sd);
    final DoubleDoubleBiPredicate predicate = TestHelper.doublesAreClose(1e-3, 0);
    for (int x = min; x <= max; x++) {
        final double p = f.likelihood(x, e);
        if (p == 0) {
            continue;
        }
        final double logP = f.logLikelihood(x, e);
        TestAssertions.assertTest(Math.log(p), logP, predicate, msg);
    }
}
Also used : DoubleDoubleBiPredicate(uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate)

Aggregations

DoubleDoubleBiPredicate (uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate)63 SeededTest (uk.ac.sussex.gdsc.test.junit5.SeededTest)27 Test (org.junit.jupiter.api.Test)22 UniformRandomProvider (org.apache.commons.rng.UniformRandomProvider)12 PoissonDistribution (org.apache.commons.math3.distribution.PoissonDistribution)6 TDoubleArrayList (gnu.trove.list.array.TDoubleArrayList)4 ArrayList (java.util.ArrayList)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 UnivariateFunction (org.apache.commons.math3.analysis.UnivariateFunction)3 SimpsonIntegrator (org.apache.commons.math3.analysis.integration.SimpsonIntegrator)3 ArrayRealVector (org.apache.commons.math3.linear.ArrayRealVector)3 RealMatrix (org.apache.commons.math3.linear.RealMatrix)3 RealVector (org.apache.commons.math3.linear.RealVector)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 BigDecimal (java.math.BigDecimal)2 MathContext (java.math.MathContext)2 MixtureMultivariateNormalDistribution (org.apache.commons.math3.distribution.MixtureMultivariateNormalDistribution)2 MultivariateNormalDistribution (org.apache.commons.math3.distribution.MultivariateNormalDistribution)2 MultivariateJacobianFunction (org.apache.commons.math3.fitting.leastsquares.MultivariateJacobianFunction)2