Search in sources :

Example 6 with StandardGradient1Procedure

use of uk.ac.sussex.gdsc.smlm.function.StandardGradient1Procedure in project GDSC-SMLM by aherbert.

the class CubicSplineFunctionTest method functionComputesTargetGradient2.

private void functionComputesTargetGradient2(int targetParameter) {
    final int gradientIndex = findGradientIndex(f1, targetParameter);
    final Statistics s = new Statistics();
    final StandardGradient1Procedure p1a = new StandardGradient1Procedure();
    final StandardGradient1Procedure p1b = new StandardGradient1Procedure();
    final StandardGradient2Procedure p2 = new StandardGradient2Procedure();
    for (final double background : testbackground) {
        // Peak 1
        for (final double signal1 : testsignal1) {
            for (final double cx1 : testcx1) {
                for (final double cy1 : testcy1) {
                    for (final double cz1 : testcz1) {
                        final double[] a = createParameters(background, signal1, cx1, cy1, cz1);
                        // System.out.println(java.util.Arrays.toString(a));
                        f1.initialise2(a);
                        final boolean test = !f1.isNodeBoundary(gradientIndex);
                        // Comment out when printing errors
                        if (!test) {
                            continue;
                        }
                        // Evaluate all gradients
                        p2.getValues(f1, a);
                        // Numerically solve gradient.
                        // Calculate the step size h to be an exact numerical representation
                        final double xx = a[targetParameter];
                        // Get h to minimise roundoff error
                        final double h = Precision.representableDelta(xx, stepH);
                        // Evaluate at (x+h) and (x-h)
                        a[targetParameter] = xx + h;
                        p1a.getValues(f1, a);
                        a[targetParameter] = xx - h;
                        p1b.getValues(f1, a);
                        // Only test close to the XY centre
                        for (final int x : testx) {
                            for (final int y : testy) {
                                final int i = y * maxx + x;
                                final double high = p1a.gradients[i][gradientIndex];
                                final double low = p1b.gradients[i][gradientIndex];
                                final double gradient = (high - low) / (2 * h);
                                final double d2yda2 = p2.gradients2[i][gradientIndex];
                                final double error = DoubleEquality.relativeError(gradient, d2yda2);
                                // gradient, gradientIndex, d2yda2, error);
                                if (test) {
                                    s.add(error);
                                    Assertions.assertTrue((gradient * d2yda2) >= 0, () -> String.format("%s sign != %s", gradient, d2yda2));
                                    // logger.fine(FunctionUtils.getSupplier("[%d,%d] %f == [%d] %f? (%g)", x, y,
                                    // gradient, gradientIndex, d2yda2, error);
                                    Assertions.assertTrue(eq.almostEqualRelativeOrAbsolute(gradient, d2yda2), () -> String.format("%s != %s", gradient, d2yda2));
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    logger.info(() -> {
        return String.format("functionComputesTargetGradient2 %s %s (error %s +/- %s)", f1.getClass().getSimpleName(), CubicSplineFunction.getName(targetParameter), MathUtils.rounded(s.getMean()), MathUtils.rounded(s.getStandardDeviation()));
    });
}
Also used : StandardGradient1Procedure(uk.ac.sussex.gdsc.smlm.function.StandardGradient1Procedure) StandardGradient2Procedure(uk.ac.sussex.gdsc.smlm.function.StandardGradient2Procedure) Statistics(uk.ac.sussex.gdsc.core.utils.Statistics)

Aggregations

StandardGradient1Procedure (uk.ac.sussex.gdsc.smlm.function.StandardGradient1Procedure)6 Statistics (uk.ac.sussex.gdsc.core.utils.Statistics)4 StandardGradient2Procedure (uk.ac.sussex.gdsc.smlm.function.StandardGradient2Procedure)4 StandardValueProcedure (uk.ac.sussex.gdsc.smlm.function.StandardValueProcedure)4 Test (org.junit.jupiter.api.Test)2