Search in sources :

Example 11 with RandomSeed

use of uk.ac.sussex.gdsc.test.junit5.RandomSeed in project GDSC-SMLM by aherbert.

the class BinomialFitterTest method canFitBinomialWithUnknownNUsingLeastSquaresEstimator.

@SeededTest
void canFitBinomialWithUnknownNUsingLeastSquaresEstimator(RandomSeed seed) {
    Assumptions.assumeTrue(TestSettings.allow(nonEssentialTestComplexity));
    final UniformRandomProvider rg = RngUtils.create(seed.getSeed());
    final boolean zeroTruncated = false;
    final boolean maximumLikelihood = false;
    for (final int n : N) {
        for (final double p : P) {
            fitBinomial(rg, n, p, zeroTruncated, maximumLikelihood, 1, n);
        }
    }
}
Also used : UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Example 12 with RandomSeed

use of uk.ac.sussex.gdsc.test.junit5.RandomSeed in project GDSC-SMLM by aherbert.

the class JumpDistanceAnalysisTest method canDoBenchmark.

/**
 * This is not actually a test but runs the fitting algorithm many times to collect benchmark data
 * to file.
 */
@SeededTest
void canDoBenchmark(RandomSeed seed) {
    // Skip this as it is slow
    Assumptions.assumeTrue(false);
    final UniformRandomProvider rg = RngUtils.create(seed.getSeed());
    out = null;
    try {
        final FileOutputStream fos = new FileOutputStream("JumpDistanceAnalysisTest.dat");
        out = new OutputStreamWriter(fos, "UTF-8");
        // Run the fitting to produce benchmark data for a mixed population of 2
        final int n = 2;
        writeHeader(n);
        for (int repeat = 10; repeat-- > 0; ) {
            resetData();
            for (final boolean mle : new boolean[] { true, false }) {
                for (int f = 1; f <= 9; f++) {
                    final double fraction = f / 10.0;
                    final String title = String.format("%s Dual=%.1f", (mle) ? "MLE" : "LSQ", fraction);
                    for (int samples = 500, k = 0; k < 6; samples *= 2, k++) {
                        for (int i = 0; i < D.length; i++) {
                            for (int j = i + 1; j < D.length; j++) {
                                try {
                                    fit(rg, title, samples, 0, new double[] { D[i], D[j] }, new double[] { fraction, 1 - fraction }, mle);
                                } catch (final AssertionError ex) {
                                // Carry on with the benchmarking
                                }
                                // If the fit had the correct N then no need to repeat
                                if (fitN == n) {
                                    continue;
                                }
                                try {
                                    fit(rg, title + " Fixed", samples, n, new double[] { D[i], D[j] }, new double[] { fraction, 1 - fraction }, mle);
                                } catch (final AssertionError ex) {
                                // Carry on with the benchmarking
                                }
                            }
                        }
                    }
                }
            }
        }
    } catch (final Exception ex) {
        throw new AssertionError("Failed to complete benchmark", ex);
    } finally {
        closeOutput();
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Example 13 with RandomSeed

use of uk.ac.sussex.gdsc.test.junit5.RandomSeed in project GDSC-SMLM by aherbert.

the class SolverSpeedTest method solveLinearAndGaussJordanReturnSameSolutionAndInversionResult.

@SeededTest
void solveLinearAndGaussJordanReturnSameSolutionAndInversionResult(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, 2);
    final DoubleDoubleBiPredicate predicate = TestHelper.doublesAreClose(1e-2, 0);
    int fail = 0;
    for (int i = 0; i < adata.size(); 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.solveLinear(a2, b2);
        solver2.invertLastA(a2);
        // Assertions.assertTrue("Different solve result @ " + i, r1 == r2);
        if (r1 && r2) {
            failCounter.run(0, () -> {
                TestAssertions.assertArrayTest(b1, b2, predicate, "Different b result");
            });
            failCounter.run(0, () -> {
                TestAssertions.assertArrayTest(a1, a2, predicate, "Different a 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 14 with RandomSeed

use of uk.ac.sussex.gdsc.test.junit5.RandomSeed in project GDSC-SMLM by aherbert.

the class ChiSquaredDistributionTableTest method canPerformChiSquaredTest.

@SeededTest
void canPerformChiSquaredTest(RandomSeed seed) {
    final UniformRandomProvider rng = RngUtils.create(seed.getSeed());
    final ChiSquareTest test = new ChiSquareTest();
    for (final int n : new int[] { 10, 50, 100 }) {
        final double[] x = SimpleArrayUtils.newArray(n, 0.5, 1.0);
        final long[] l = new long[x.length];
        for (int i = 0; i < x.length; i++) {
            l[i] = GdscSmlmTestUtils.createPoissonSampler(rng, x[i]).sample();
        }
        final double chi2 = test.chiSquare(x, l);
        final double ep = test.chiSquareTest(x, l);
        final int df = x.length - 1;
        final double o = ChiSquaredDistributionTable.computeQValue(chi2, df);
        Assertions.assertEquals(ep, o, 1e-10);
        final ChiSquaredDistributionTable upperTable = ChiSquaredDistributionTable.createUpperTailed(o, df);
        final double upper = chi2 * 1.01;
        final double lower = chi2 * 0.99;
        Assertions.assertTrue(upperTable.reject(upper, df), "Upper did not reject higher");
        Assertions.assertFalse(upperTable.reject(o, df), "Upper did not reject actual value");
        Assertions.assertFalse(upperTable.reject(lower, df), "Upper did not accept lower");
    }
}
Also used : UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) ChiSquareTest(org.apache.commons.math3.stat.inference.ChiSquareTest) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Example 15 with RandomSeed

use of uk.ac.sussex.gdsc.test.junit5.RandomSeed in project GDSC-SMLM by aherbert.

the class SumFilterTest method intBlockSumNxNInternalAndStripedBlockSumNxNInternalReturnSameResult.

@SeededTest
void intBlockSumNxNInternalAndStripedBlockSumNxNInternalReturnSameResult(RandomSeed seed) {
    final UniformRandomProvider rg = RngUtils.create(seed.getSeed());
    final SumFilter filter = new SumFilter();
    for (final int width : primes) {
        for (final int height : primes) {
            for (final int boxSize : boxSizes) {
                intCompareBlockSumNxNInternalAndStripedBlockSumNxNInternal(rg, filter, width, height, boxSize);
            }
        }
    }
}
Also used : UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Aggregations

SeededTest (uk.ac.sussex.gdsc.test.junit5.SeededTest)161 UniformRandomProvider (org.apache.commons.rng.UniformRandomProvider)142 DoubleDoubleBiPredicate (uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate)17 Rectangle (java.awt.Rectangle)12 SpeedTag (uk.ac.sussex.gdsc.test.junit5.SpeedTag)12 TimingService (uk.ac.sussex.gdsc.test.utils.TimingService)11 SharedStateContinuousSampler (org.apache.commons.rng.sampling.distribution.SharedStateContinuousSampler)7 TDoubleArrayList (gnu.trove.list.array.TDoubleArrayList)6 FloatProcessor (ij.process.FloatProcessor)5 SingleFreeCircularErfGaussian2DFunction (uk.ac.sussex.gdsc.smlm.function.gaussian.erf.SingleFreeCircularErfGaussian2DFunction)5 TimingResult (uk.ac.sussex.gdsc.test.utils.TimingResult)5 ArrayList (java.util.ArrayList)4 ErfGaussian2DFunction (uk.ac.sussex.gdsc.smlm.function.gaussian.erf.ErfGaussian2DFunction)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 DoubleConvolutionValueProcedure (uk.ac.sussex.gdsc.smlm.utils.Convolution.DoubleConvolutionValueProcedure)4 FloatFloatBiPredicate (uk.ac.sussex.gdsc.test.api.function.FloatFloatBiPredicate)4 MixtureMultivariateNormalDistribution (org.apache.commons.math3.distribution.MixtureMultivariateNormalDistribution)3 MultivariateNormalMixtureExpectationMaximization (org.apache.commons.math3.distribution.fitting.MultivariateNormalMixtureExpectationMaximization)3 DoubleEquality (uk.ac.sussex.gdsc.core.utils.DoubleEquality)3