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);
}
}
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);
}
}
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));
}
}
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);
}
}
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);
}
}
Aggregations