Search in sources :

Example 36 with DoubleDoubleBiPredicate

use of uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate in project GDSC-SMLM by aherbert.

the class FastMleJacobianGradient2ProcedureTest method gradientProcedureComputesSameAsBaseGradientProcedure.

@SeededTest
void gradientProcedureComputesSameAsBaseGradientProcedure(RandomSeed seed) {
    // Test the base functionality of computing the partial derivatives is the same
    final DoubleDoubleBiPredicate equality = TestHelper.doublesAreClose(1e-5, 0);
    gradientProcedureComputesSameAsBaseGradientProcedure(seed, 4, equality);
    gradientProcedureComputesSameAsBaseGradientProcedure(seed, 5, equality);
    gradientProcedureComputesSameAsBaseGradientProcedure(seed, 6, equality);
    gradientProcedureComputesSameAsBaseGradientProcedure(seed, 11, equality);
    gradientProcedureComputesSameAsBaseGradientProcedure(seed, 21, equality);
}
Also used : DoubleDoubleBiPredicate(uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Example 37 with DoubleDoubleBiPredicate

use of uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate in project GDSC-SMLM by aherbert.

the class JumpDistanceAnalysisTest method canIntegrateProbabilityToCumulativeWithMixedPopulation.

@Disabled("Commented out as this test always passes")
@Test
void canIntegrateProbabilityToCumulativeWithMixedPopulation() {
    final JumpDistanceAnalysis jd = new JumpDistanceAnalysis();
    jd.setMinD(0);
    jd.setMinFraction(0);
    final SimpsonIntegrator si = new SimpsonIntegrator(1e-3, 1e-8, 2, SimpsonIntegrator.SIMPSON_MAX_ITERATIONS_COUNT);
    final DoubleDoubleBiPredicate equality = TestHelper.doublesAreClose(1e-2, 0);
    for (final double d : D) {
        for (final double f : new double[] { 0, 0.1, 0.2, 0.4, 0.7, 0.9, 1 }) {
            final double[] params = new double[] { f, d, 1 - f, d * 0.1 };
            final MixedJumpDistanceFunction fp = new MixedJumpDistanceFunction(null, d, 2);
            final MixedJumpDistanceCumulFunction fc = new MixedJumpDistanceCumulFunction(null, null, d, 2);
            final UnivariateFunction func = new UnivariateFunction() {

                @Override
                public double value(double x) {
                    return fp.evaluate(x, params);
                }
            };
            double x = d / 8;
            for (int i = 1; i < 10; i++, x *= 2) {
                final double e = fc.evaluate(x, params);
                // Integrate
                final double o = si.integrate(10000, func, 0, x);
                // logger.info(FunctionUtils.getSupplier("Integrate d=%.1f, f=%.1f : x=%.1f, e=%f, o=%f,
                // iter=%d, eval=%d", d, f, x, e, o,
                // si.getIterations(), si.getEvaluations());
                TestAssertions.assertTest(e, o, equality, FunctionUtils.getSupplier("Failed to integrate: x=%g", x));
            }
        }
    }
}
Also used : SimpsonIntegrator(org.apache.commons.math3.analysis.integration.SimpsonIntegrator) MixedJumpDistanceCumulFunction(uk.ac.sussex.gdsc.smlm.fitting.JumpDistanceAnalysis.MixedJumpDistanceCumulFunction) DoubleDoubleBiPredicate(uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate) MixedJumpDistanceFunction(uk.ac.sussex.gdsc.smlm.fitting.JumpDistanceAnalysis.MixedJumpDistanceFunction) UnivariateFunction(org.apache.commons.math3.analysis.UnivariateFunction) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 38 with DoubleDoubleBiPredicate

use of uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate in project GDSC-SMLM by aherbert.

the class JumpDistanceAnalysisTest method canIntegrateProbabilityToCumulativeWithSinglePopulation.

@Disabled("Commented out as this test always passes")
@Test
void canIntegrateProbabilityToCumulativeWithSinglePopulation() {
    final JumpDistanceAnalysis jd = new JumpDistanceAnalysis();
    jd.setMinD(0);
    jd.setMinFraction(0);
    final SimpsonIntegrator si = new SimpsonIntegrator(1e-3, 1e-8, 2, SimpsonIntegrator.SIMPSON_MAX_ITERATIONS_COUNT);
    final DoubleDoubleBiPredicate equality = TestHelper.doublesAreClose(1e-2, 0);
    for (final double d : D) {
        final double[] params = new double[] { d };
        final JumpDistanceFunction fp = new JumpDistanceFunction(null, d);
        final JumpDistanceCumulFunction fc = new JumpDistanceCumulFunction(null, null, d);
        double x = d / 8;
        final UnivariateFunction func = new UnivariateFunction() {

            @Override
            public double value(double x) {
                return fp.evaluate(x, params);
            }
        };
        for (int i = 1; i < 10; i++, x *= 2) {
            final double e = fc.evaluate(x, params);
            // Integrate
            final double o = si.integrate(10000, func, 0, x);
            // logger.info(FunctionUtils.getSupplier("Integrate d=%.1f : x=%.1f, e=%f, o=%f, iter=%d,
            // eval=%d", d, x, e, o, si.getIterations(),
            // si.getEvaluations());
            TestAssertions.assertTest(e, o, equality, FunctionUtils.getSupplier("Failed to integrate: x=%g", x));
        }
    }
}
Also used : SimpsonIntegrator(org.apache.commons.math3.analysis.integration.SimpsonIntegrator) DoubleDoubleBiPredicate(uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate) UnivariateFunction(org.apache.commons.math3.analysis.UnivariateFunction) JumpDistanceCumulFunction(uk.ac.sussex.gdsc.smlm.fitting.JumpDistanceAnalysis.JumpDistanceCumulFunction) MixedJumpDistanceCumulFunction(uk.ac.sussex.gdsc.smlm.fitting.JumpDistanceAnalysis.MixedJumpDistanceCumulFunction) MixedJumpDistanceFunction(uk.ac.sussex.gdsc.smlm.fitting.JumpDistanceAnalysis.MixedJumpDistanceFunction) JumpDistanceFunction(uk.ac.sussex.gdsc.smlm.fitting.JumpDistanceAnalysis.JumpDistanceFunction) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 39 with DoubleDoubleBiPredicate

use of uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate in project GDSC-SMLM by aherbert.

the class SolverSpeedTest method solveLinearAndGaussJordanReturnSameSolutionResult.

@SeededTest
void solveLinearAndGaussJordanReturnSameSolutionResult(RandomSeed seed) {
    final int iter = 100;
    final SolverSpeedTestData data = ensureData(seed, iter);
    final ArrayList<double[][]> adata = copyAdouble(data.adata, iter);
    final ArrayList<double[]> bdata = copyBdouble(data.bdata, iter);
    final ArrayList<double[][]> adata2 = copyAdouble(data.adata, iter);
    final ArrayList<double[]> bdata2 = copyBdouble(data.bdata, iter);
    final GaussJordan solver = new GaussJordan();
    final EjmlLinearSolver solver2 = new EjmlLinearSolver();
    final int failureLimit = TestCounter.computeFailureLimit(iter, 0.1);
    final TestCounter failCounter = new TestCounter(failureLimit);
    final DoubleDoubleBiPredicate predicate = TestHelper.doublesAreClose(1e-2, 0);
    int fail = 0;
    for (int i = 0; i < iter; i++) {
        final double[][] a1 = adata.get(i);
        final double[] b1 = bdata.get(i);
        final double[][] a2 = adata2.get(i);
        final double[] b2 = bdata2.get(i);
        final boolean r1 = solver.solve(a1, b1);
        final boolean r2 = solver2.solve(a2, b2);
        // Assertions.assertTrue("Different solve result @ " + i, r1 == r2);
        if (r1 && r2) {
            failCounter.run(() -> {
                TestAssertions.assertArrayTest(b1, b2, predicate, "Different b result");
            });
        } else {
            fail++;
        }
    }
    if (fail > iter / 2) {
        Assertions.fail(String.format("Failed to solve %d / %d", fail, iter));
    }
}
Also used : TestCounter(uk.ac.sussex.gdsc.test.utils.TestCounter) DoubleDoubleBiPredicate(uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Example 40 with DoubleDoubleBiPredicate

use of uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate in project GDSC-SMLM by aherbert.

the class SolverSpeedTest method gaussJordanFloatAndDoubleReturnSameSolutionAndInversionResult.

@SeededTest
void gaussJordanFloatAndDoubleReturnSameSolutionAndInversionResult(RandomSeed seed) {
    final int iter = 100;
    final SolverSpeedTestData data = ensureData(seed, iter);
    final ArrayList<float[][]> adata = copyAfloat(data.adata, iter);
    final ArrayList<float[]> bdata = copyBfloat(data.bdata, iter);
    final ArrayList<double[][]> adata2 = copyAdouble(data.adata, iter);
    final ArrayList<double[]> bdata2 = copyBdouble(data.bdata, iter);
    final GaussJordan solver = new GaussJordan();
    final int failureLimit = TestCounter.computeFailureLimit(iter, 0.1);
    final TestCounter failCounter = new TestCounter(failureLimit, 2);
    final DoubleDoubleBiPredicate predicate = TestHelper.doublesAreClose(1e-2, 0);
    int fail = 0;
    for (int i = 0; i < adata.size(); i++) {
        final float[][] a1 = adata.get(i);
        final float[] b1 = bdata.get(i);
        final double[][] a2 = adata2.get(i);
        final double[] b2 = bdata2.get(i);
        final boolean r1 = solver.solve(a1, b1);
        final boolean r2 = solver.solve(a2, b2);
        // Assertions.assertTrue("Different solve result @ " + i, r1 == r2);
        if (r1 && r2) {
            final double[] db1 = SimpleArrayUtils.toDouble(b1);
            final double[][] da1 = new double[a1.length][];
            for (int j = a1.length; j-- > 0; ) {
                da1[j] = SimpleArrayUtils.toDouble(a1[j]);
            }
            failCounter.run(0, () -> {
                TestAssertions.assertArrayTest(db1, b2, predicate, "Different b result");
            });
            failCounter.run(1, () -> {
                TestAssertions.assertArrayTest(da1, a2, predicate, "Different a result");
            });
        } else {
            fail++;
        }
    }
    if (fail > iter / 2) {
        Assertions.fail(String.format("Failed to solve %d / %d", fail, iter));
    }
}
Also used : DoubleDoubleBiPredicate(uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate) TestCounter(uk.ac.sussex.gdsc.test.utils.TestCounter) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Aggregations

DoubleDoubleBiPredicate (uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate)63 SeededTest (uk.ac.sussex.gdsc.test.junit5.SeededTest)27 Test (org.junit.jupiter.api.Test)22 UniformRandomProvider (org.apache.commons.rng.UniformRandomProvider)12 PoissonDistribution (org.apache.commons.math3.distribution.PoissonDistribution)6 TDoubleArrayList (gnu.trove.list.array.TDoubleArrayList)4 ArrayList (java.util.ArrayList)4 MixtureMultivariateGaussianDistribution (uk.ac.sussex.gdsc.smlm.math3.distribution.fitting.MultivariateGaussianMixtureExpectationMaximization.MixtureMultivariateGaussianDistribution)4 MultivariateGaussianDistribution (uk.ac.sussex.gdsc.smlm.math3.distribution.fitting.MultivariateGaussianMixtureExpectationMaximization.MixtureMultivariateGaussianDistribution.MultivariateGaussianDistribution)4 UnivariateFunction (org.apache.commons.math3.analysis.UnivariateFunction)3 SimpsonIntegrator (org.apache.commons.math3.analysis.integration.SimpsonIntegrator)3 ArrayRealVector (org.apache.commons.math3.linear.ArrayRealVector)3 RealMatrix (org.apache.commons.math3.linear.RealMatrix)3 RealVector (org.apache.commons.math3.linear.RealVector)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 BigDecimal (java.math.BigDecimal)2 MathContext (java.math.MathContext)2 MixtureMultivariateNormalDistribution (org.apache.commons.math3.distribution.MixtureMultivariateNormalDistribution)2 MultivariateNormalDistribution (org.apache.commons.math3.distribution.MultivariateNormalDistribution)2 MultivariateJacobianFunction (org.apache.commons.math3.fitting.leastsquares.MultivariateJacobianFunction)2