Search in sources :

Example 1 with StandardGradient1Procedure

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

the class CubicSplineFunctionTest method functionComputesTargetGradient1With2Peaks.

private void functionComputesTargetGradient1With2Peaks(int targetParameter) {
    final int gradientIndex = findGradientIndex(f2, targetParameter);
    final Statistics s = new Statistics();
    final StandardValueProcedure p1a = new StandardValueProcedure();
    final StandardValueProcedure p1b = new StandardValueProcedure();
    final StandardGradient1Procedure p2 = new StandardGradient1Procedure();
    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) {
                        // Peak 2
                        for (final double signal2 : testsignal2) {
                            for (final double cx2 : testcx2) {
                                for (final double cy2 : testcy2) {
                                    for (final double cz2 : testcz2) {
                                        final double[] a = createParameters(background, signal1, cx1, cy1, cz1, signal2, cx2, cy2, cz2);
                                        // System.out.println(java.util.Arrays.toString(a));
                                        // Evaluate all gradients
                                        p2.getValues(f2, 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(f2, a);
                                        a[targetParameter] = xx - h;
                                        p1b.getValues(f2, 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.values[i];
                                                final double low = p1b.values[i];
                                                final double gradient = (high - low) / (2 * h);
                                                final double dyda = p2.gradients[i][gradientIndex];
                                                final double error = DoubleEquality.relativeError(gradient, dyda);
                                                s.add(error);
                                                Assertions.assertTrue((gradient * dyda) >= 0, () -> String.format("%s sign != %s", gradient, dyda));
                                                // logger.fine(FunctionUtils.getSupplier("[%d,%d] %f == [%d] %f? (%g)", x,
                                                // y, gradient, gradientIndex, dyda, error);
                                                Assertions.assertTrue(eq.almostEqualRelativeOrAbsolute(gradient, dyda), () -> String.format("%s != %s", gradient, dyda));
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    logger.info(() -> {
        return String.format("functionComputesTargetGradient1With2Peaks %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) StandardValueProcedure(uk.ac.sussex.gdsc.smlm.function.StandardValueProcedure) Statistics(uk.ac.sussex.gdsc.core.utils.Statistics)

Example 2 with StandardGradient1Procedure

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

the class CubicSplineFunctionTest method functionComputesTargetGradient1.

private void functionComputesTargetGradient1(int targetParameter) {
    final int gradientIndex = findGradientIndex(f1, targetParameter);
    final Statistics s = new Statistics();
    final StandardValueProcedure p1a = new StandardValueProcedure();
    final StandardValueProcedure p1b = new StandardValueProcedure();
    final StandardGradient1Procedure p2 = new StandardGradient1Procedure();
    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));
                        // 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.values[i];
                                final double low = p1b.values[i];
                                final double gradient = (high - low) / (2 * h);
                                final double dyda = p2.gradients[i][gradientIndex];
                                final double error = DoubleEquality.relativeError(gradient, dyda);
                                s.add(error);
                                if ((gradient * dyda) < 0) {
                                    Assertions.fail(String.format("%s sign != %s", gradient, dyda));
                                }
                                // gradient, gradientIndex, dyda, error);
                                if (!eq.almostEqualRelativeOrAbsolute(gradient, dyda)) {
                                    Assertions.fail(String.format("%s != %s", gradient, dyda));
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    logger.info(() -> {
        return String.format("functionComputesTargetGradient1 %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) StandardValueProcedure(uk.ac.sussex.gdsc.smlm.function.StandardValueProcedure) Statistics(uk.ac.sussex.gdsc.core.utils.Statistics)

Example 3 with StandardGradient1Procedure

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

the class CubicSplineFunctionTest method functionComputesTargetGradient2With2Peaks.

private void functionComputesTargetGradient2With2Peaks(int targetParameter) {
    final int gradientIndex = findGradientIndex(f2, 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) {
                        // Peak 2
                        for (final double signal2 : testsignal2) {
                            for (final double cx2 : testcx2) {
                                for (final double cy2 : testcy2) {
                                    for (final double cz2 : testcz2) {
                                        final double[] a = createParameters(background, signal1, cx1, cy1, cz1, signal2, cx2, cy2, cz2);
                                        // System.out.println(java.util.Arrays.toString(a));
                                        f2.initialise2(a);
                                        final boolean test = !f2.isNodeBoundary(gradientIndex);
                                        // Comment out when printing errors
                                        if (!test) {
                                            continue;
                                        }
                                        // Evaluate all gradients
                                        p2.getValues(f2, 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(f2, a);
                                        a[targetParameter] = xx - h;
                                        p1b.getValues(f2, 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);
                                                // y, gradient, gradientIndex, d2yda2, error);
                                                if (test) {
                                                    s.add(error);
                                                    if ((gradient * d2yda2) < 0) {
                                                        Assertions.fail(String.format("%s sign != %s", gradient, d2yda2));
                                                    }
                                                    // x, y, gradient, gradientIndex, d2yda2, error);
                                                    if (!eq.almostEqualRelativeOrAbsolute(gradient, d2yda2)) {
                                                        Assertions.fail(String.format("%s != %s", gradient, d2yda2));
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    logger.info(() -> {
        return String.format("functionComputesTargetGradient2With2Peaks %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)

Example 4 with StandardGradient1Procedure

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

the class CubicSplineFunctionTest method functionComputesTargetWithAndWithoutGradientWith2Peaks.

@Test
void functionComputesTargetWithAndWithoutGradientWith2Peaks() {
    if (f2 == null) {
        return;
    }
    final StandardValueProcedure p0 = new StandardValueProcedure();
    final StandardGradient1Procedure p1 = 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) {
                        // Peak 2
                        for (final double signal2 : testsignal2) {
                            for (final double cx2 : testcx2) {
                                for (final double cy2 : testcy2) {
                                    for (final double cz2 : testcz2) {
                                        final double[] a = createParameters(background, signal1, cx1, cy1, cz1, signal2, cx2, cy2, cz2);
                                        final double[] e = p0.getValues(f1, a);
                                        final double[] o1 = p1.getValues(f1, a);
                                        final double[] o2 = p2.getValues(f1, a);
                                        Assertions.assertArrayEquals(e, o1);
                                        Assertions.assertArrayEquals(e, o2);
                                        for (int i = e.length; i-- > 0; ) {
                                            Assertions.assertArrayEquals(p1.gradients[i], p2.gradients1[i]);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : StandardGradient1Procedure(uk.ac.sussex.gdsc.smlm.function.StandardGradient1Procedure) StandardValueProcedure(uk.ac.sussex.gdsc.smlm.function.StandardValueProcedure) StandardGradient2Procedure(uk.ac.sussex.gdsc.smlm.function.StandardGradient2Procedure) Test(org.junit.jupiter.api.Test)

Example 5 with StandardGradient1Procedure

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

the class CubicSplineFunctionTest method functionComputesTargetWithAndWithoutGradient.

@Test
void functionComputesTargetWithAndWithoutGradient() {
    final StandardValueProcedure p0 = new StandardValueProcedure();
    final StandardGradient1Procedure p1 = 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);
                        final double[] e = p0.getValues(f1, a);
                        final double[] o1 = p1.getValues(f1, a);
                        final double[] o2 = p2.getValues(f1, a);
                        Assertions.assertArrayEquals(e, o1);
                        Assertions.assertArrayEquals(e, o2);
                        for (int i = e.length; i-- > 0; ) {
                            Assertions.assertArrayEquals(p1.gradients[i], p2.gradients1[i]);
                        }
                    }
                }
            }
        }
    }
}
Also used : StandardGradient1Procedure(uk.ac.sussex.gdsc.smlm.function.StandardGradient1Procedure) StandardValueProcedure(uk.ac.sussex.gdsc.smlm.function.StandardValueProcedure) StandardGradient2Procedure(uk.ac.sussex.gdsc.smlm.function.StandardGradient2Procedure) Test(org.junit.jupiter.api.Test)

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