use of uk.ac.sussex.gdsc.smlm.function.gaussian.SingleEllipticalGaussian2DFunction in project GDSC-SMLM by aherbert.
the class GradientCalculatorSpeedTest method gradientCalculatorComputesSameOutputWithBias.
@SeededTest
void gradientCalculatorComputesSameOutputWithBias(RandomSeed seed) {
final Gaussian2DFunction func = new SingleEllipticalGaussian2DFunction(blockWidth, blockWidth);
final int nparams = func.getNumberOfGradients();
final GradientCalculator calc = new GradientCalculator(nparams);
final int n = func.size();
final int iter = 50;
final ArrayList<double[]> paramsList = new ArrayList<>(iter);
final ArrayList<double[]> yList = new ArrayList<>(iter);
final ArrayList<double[][]> alphaList = new ArrayList<>(iter);
final ArrayList<double[]> betaList = new ArrayList<>(iter);
final ArrayList<double[]> xList = new ArrayList<>(iter);
// Manipulate the background
final double defaultBackground = background;
final boolean report = logger.isLoggable(Level.INFO);
try {
background = 1e-2;
createData(RngUtils.create(seed.getSeed()), 1, iter, paramsList, yList, true);
final EjmlLinearSolver solver = new EjmlLinearSolver(1e-5, 1e-6);
for (int i = 0; i < paramsList.size(); i++) {
final double[] y = yList.get(i);
final double[] a = paramsList.get(i);
final double[][] alpha = new double[nparams][nparams];
final double[] beta = new double[nparams];
calc.findLinearised(n, y, a, alpha, beta, func);
alphaList.add(alpha);
betaList.add(beta.clone());
for (int j = 0; j < nparams; j++) {
if (Math.abs(beta[j]) < 1e-6) {
logger.info(FunctionUtils.getSupplier("[%d] Tiny beta %s %g", i, func.getGradientParameterName(j), beta[j]));
}
}
// Solve
if (!solver.solve(alpha, beta)) {
throw new AssertionError();
}
xList.add(beta);
// System.out.println(Arrays.toString(beta));
}
final double[][] alpha = new double[nparams][nparams];
final double[] beta = new double[nparams];
final Statistics[] rel = new Statistics[nparams];
final Statistics[] abs = new Statistics[nparams];
for (int i = 0; i < nparams; i++) {
rel[i] = new Statistics();
abs[i] = new Statistics();
}
final DoubleDoubleBiPredicate predicate = TestHelper.doublesAreClose(1e-10, 0);
// for (double b : new double[] { -500, -100, -10, -1, -0.1, 0.1, 1, 10, 100, 500 })
for (final double b : new double[] { -10, -1, -0.1, 0.1, 1, 10 }) {
if (report) {
for (int i = 0; i < nparams; i++) {
rel[i].reset();
abs[i].reset();
}
}
for (int i = 0; i < paramsList.size(); i++) {
final double[] y = add(yList.get(i), b);
final double[] a = paramsList.get(i).clone();
a[0] += b;
calc.findLinearised(n, y, a, alpha, beta, func);
final double[][] alpha2 = alphaList.get(i);
final double[] beta2 = betaList.get(i);
final double[] x2 = xList.get(i);
TestAssertions.assertArrayTest(beta2, beta, predicate, "Beta");
TestAssertions.assertArrayTest(alpha2, alpha, predicate, "Alpha");
// Solve
solver.solve(alpha, beta);
Assertions.assertArrayEquals(x2, beta, 1e-10, "X");
if (report) {
for (int j = 0; j < nparams; j++) {
rel[j].add(DoubleEquality.relativeError(x2[j], beta[j]));
abs[j].add(Math.abs(x2[j] - beta[j]));
}
}
}
if (report) {
for (int i = 0; i < nparams; i++) {
logger.info(FunctionUtils.getSupplier("Bias = %.2f : %s : Rel %g +/- %g: Abs %g +/- %g", b, func.getGradientParameterName(i), rel[i].getMean(), rel[i].getStandardDeviation(), abs[i].getMean(), abs[i].getStandardDeviation()));
}
}
}
} finally {
background = defaultBackground;
}
}
use of uk.ac.sussex.gdsc.smlm.function.gaussian.SingleEllipticalGaussian2DFunction 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));
}
}
}
Aggregations