use of uk.ac.sussex.gdsc.test.junit5.SeededTest in project GDSC-SMLM by aherbert.
the class FastMleGradient2ProcedureTest method gradientProcedureComputesSameWithPrecomputed.
@SeededTest
void gradientProcedureComputesSameWithPrecomputed(RandomSeed seed) {
final int iter = 10;
final UniformRandomProvider rng = RngUtils.create(seed.getSeed());
final ErfGaussian2DFunction f1 = (ErfGaussian2DFunction) GaussianFunctionFactory.create2D(1, 10, 10, GaussianFunctionFactory.FIT_ERF_FREE_CIRCLE, null);
final ErfGaussian2DFunction f2 = (ErfGaussian2DFunction) GaussianFunctionFactory.create2D(2, 10, 10, GaussianFunctionFactory.FIT_ERF_FREE_CIRCLE, null);
final double[] a1 = new double[1 + Gaussian2DFunction.PARAMETERS_PER_PEAK];
final double[] a2 = new double[1 + 2 * Gaussian2DFunction.PARAMETERS_PER_PEAK];
final double[] x = new double[f1.size()];
final double[] b = new double[f1.size()];
for (int i = 0; i < iter; i++) {
final int ii = i;
a2[Gaussian2DFunction.BACKGROUND] = nextUniform(rng, 0.1, 0.3);
a2[Gaussian2DFunction.SIGNAL] = nextUniform(rng, 100, 300);
a2[Gaussian2DFunction.X_POSITION] = nextUniform(rng, 3, 5);
a2[Gaussian2DFunction.Y_POSITION] = nextUniform(rng, 3, 5);
a2[Gaussian2DFunction.Z_POSITION] = nextUniform(rng, -2, 2);
a2[Gaussian2DFunction.X_SD] = nextUniform(rng, 1, 1.3);
a2[Gaussian2DFunction.Y_SD] = nextUniform(rng, 1, 1.3);
a2[Gaussian2DFunction.PARAMETERS_PER_PEAK + Gaussian2DFunction.SIGNAL] = nextUniform(rng, 100, 300);
a2[Gaussian2DFunction.PARAMETERS_PER_PEAK + Gaussian2DFunction.X_POSITION] = nextUniform(rng, 5, 7);
a2[Gaussian2DFunction.PARAMETERS_PER_PEAK + Gaussian2DFunction.Y_POSITION] = nextUniform(rng, 5, 7);
a2[Gaussian2DFunction.PARAMETERS_PER_PEAK + Gaussian2DFunction.Z_POSITION] = nextUniform(rng, -3, 1);
a2[Gaussian2DFunction.PARAMETERS_PER_PEAK + Gaussian2DFunction.X_SD] = nextUniform(rng, 1, 1.3);
a2[Gaussian2DFunction.PARAMETERS_PER_PEAK + Gaussian2DFunction.Y_SD] = nextUniform(rng, 1, 1.3);
// Simulate Poisson data
f2.initialise0(a2);
f1.forEach(new ValueProcedure() {
int index = 0;
@Override
public void execute(double value) {
if (value > 0) {
x[index++] = GdscSmlmTestUtils.createPoissonSampler(rng, value).sample();
} else {
x[index++] = 0;
}
}
});
// Precompute peak 2 (no background)
a1[Gaussian2DFunction.BACKGROUND] = 0;
for (int j = 1; j < 7; j++) {
a1[j] = a2[Gaussian2DFunction.PARAMETERS_PER_PEAK + j];
}
f1.initialise0(a1);
f1.forEach(new ValueProcedure() {
int index = 0;
@Override
public void execute(double value) {
b[index++] = value;
}
});
// Reset to peak 1
for (int j = 0; j < 7; j++) {
a1[j] = a2[j];
}
// Compute peak 1+2
final FastMleGradient2Procedure p12 = FastMleGradient2ProcedureUtils.create(x, f2);
p12.computeSecondDerivative(a2);
final double[] d11 = Arrays.copyOf(p12.d1, f1.getNumberOfGradients());
final double[] d21 = Arrays.copyOf(p12.d2, f1.getNumberOfGradients());
// Compute peak 1+(precomputed 2)
final FastMleGradient2Procedure p1b2 = FastMleGradient2ProcedureUtils.create(x, OffsetGradient2Function.wrapGradient2Function(f1, b));
p1b2.computeSecondDerivative(a1);
final double[] d12 = p1b2.d1;
final double[] d22 = p1b2.d2;
Assertions.assertArrayEquals(p12.u, p1b2.u, 1e-10, () -> " Result: Not same @ " + ii);
Assertions.assertArrayEquals(d11, d12, 1e-10, () -> " D1: Not same @ " + ii);
Assertions.assertArrayEquals(d21, d22, 1e-10, () -> " D2: Not same @ " + ii);
final double[] v1 = p12.computeValue(a2);
final double[] v2 = p1b2.computeValue(a1);
Assertions.assertArrayEquals(v1, v2, 1e-10, () -> " Value: Not same @ " + ii);
final double[] d1 = Arrays.copyOf(p12.computeFirstDerivative(a2), f1.getNumberOfGradients());
final double[] d2 = p1b2.computeFirstDerivative(a1);
Assertions.assertArrayEquals(d1, d2, 1e-10, () -> " 1st derivative: Not same @ " + ii);
}
}
use of uk.ac.sussex.gdsc.test.junit5.SeededTest in project GDSC-SMLM by aherbert.
the class OffsetFunctionTest method offsetValueFunctionWrapsPrecomputedValues.
@SeededTest
void offsetValueFunctionWrapsPrecomputedValues(RandomSeed seed) {
final int n = 3;
final UniformRandomProvider r = RngUtils.create(seed.getSeed());
final ValueFunction 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 ValueFunction f1 = OffsetValueFunction.wrapValueFunction(f0, b1);
final ValueFunction f2 = OffsetValueFunction.wrapValueFunction(f1, b2);
final double[] p = new double[n];
for (int i = 0; i < n; i++) {
p[i] = r.nextDouble();
}
final double[] v0 = evaluateValueFunction(f0, p);
final double[] v1 = evaluateValueFunction(f1, p);
final double[] v2 = evaluateValueFunction(f2, p);
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");
}
}
use of uk.ac.sussex.gdsc.test.junit5.SeededTest in project GDSC-SMLM by aherbert.
the class OffsetFunctionTest method offsetGradient2FunctionWrapsPrecomputedValues.
@SeededTest
void offsetGradient2FunctionWrapsPrecomputedValues(RandomSeed seed) {
final int n = 3;
final UniformRandomProvider r = RngUtils.create(seed.getSeed());
final Gradient2Function 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 Gradient2Function f1 = OffsetGradient2Function.wrapGradient2Function(f0, b1);
final Gradient2Function f2 = OffsetGradient2Function.wrapGradient2Function(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[] d20 = new double[n];
final double[] d21 = new double[n];
final double[] d22 = new double[n];
final double[] v0 = evaluateGradient2Function(f0, p, d0, d20);
final double[] v1 = evaluateGradient2Function(f1, p, d1, d21);
final double[] v2 = evaluateGradient2Function(f2, p, d2, d22);
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");
Assertions.assertArrayEquals(d20, d21, "d21");
Assertions.assertArrayEquals(d20, d22, "d22");
}
use of uk.ac.sussex.gdsc.test.junit5.SeededTest in project GDSC-SMLM by aherbert.
the class ScmosLikelihoodWrapperTest method fitEllipticalComputesGradient.
@SeededTest
void fitEllipticalComputesGradient(RandomSeed seed) {
// The elliptical function gradient evaluation is worse
final DoubleEquality tmp = eq;
eq = eqPerDatum;
functionComputesGradient(seed, GaussianFunctionFactory.FIT_ELLIPTICAL);
eq = tmp;
}
use of uk.ac.sussex.gdsc.test.junit5.SeededTest in project GDSC-SMLM by aherbert.
the class PrecomputedFunctionTest method precomputedValueFunctionWrapsPrecomputedValues.
@SeededTest
void precomputedValueFunctionWrapsPrecomputedValues(RandomSeed seed) {
final UniformRandomProvider r = RngUtils.create(seed.getSeed());
final int size = 100;
final double[] v = GdscSmlmTestUtils.generateDoubles(size, r);
final ValueFunction func = new PrecomputedValueFunction(v);
final double[] vo = evaluateValueFunction(func);
Assertions.assertArrayEquals(v, vo, "values");
}
Aggregations