Search in sources :

Example 66 with UniformRandomProvider

use of org.apache.commons.rng.UniformRandomProvider in project GDSC-SMLM by aherbert.

the class CubicSplineDataTest method canExternaliseFunction.

private static void canExternaliseFunction(RandomSeed seed, boolean singlePrecision) throws IOException {
    final UniformRandomProvider r = RngUtils.create(seed.getSeed());
    final int x = 6;
    final int y = 5;
    final int z = 4;
    final int size = x * y;
    final CustomTricubicFunction[][] splines = new CustomTricubicFunction[z][x * y];
    final double[] a = new double[64];
    for (int zz = 0; zz < z; zz++) {
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < 64; j++) {
                a[j] = r.nextDouble();
            }
            splines[zz][i] = CustomTricubicFunctionUtils.create(a);
            if (singlePrecision) {
                splines[zz][i] = splines[zz][i].toSinglePrecision();
            }
        }
    }
    final CubicSplineData f1 = new CubicSplineData(x, y, splines);
    final ByteArrayOutputStream b = new ByteArrayOutputStream();
    f1.write(b);
    final byte[] bytes = b.toByteArray();
    final CubicSplineData f2 = CubicSplineData.read(new ByteArrayInputStream(bytes));
    final double[] exp = new double[64];
    final double[] obs = new double[64];
    for (int zz = 0; zz < z; zz++) {
        for (int i = 0; i < size; i++) {
            f1.splines[zz][i].getCoefficients(exp);
            f2.splines[zz][i].getCoefficients(obs);
            Assertions.assertArrayEquals(exp, obs);
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CustomTricubicFunction(uk.ac.sussex.gdsc.core.math.interpolation.CustomTricubicFunction)

Example 67 with UniformRandomProvider

use of org.apache.commons.rng.UniformRandomProvider in project GDSC-SMLM by aherbert.

the class PrecomputedFunctionTest method precomputedValueFunctionWrapsPrecomputedValues.

@SeededTest
void precomputedValueFunctionWrapsPrecomputedValues(RandomSeed seed) {
    final UniformRandomProvider r = RngUtils.create(seed.getSeed());
    final int size = 100;
    final double[] v = GdscSmlmTestUtils.generateDoubles(size, r);
    final ValueFunction func = new PrecomputedValueFunction(v);
    final double[] vo = evaluateValueFunction(func);
    Assertions.assertArrayEquals(v, vo, "values");
}
Also used : UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Example 68 with UniformRandomProvider

use of org.apache.commons.rng.UniformRandomProvider in project GDSC-SMLM by aherbert.

the class PrecomputedFunctionTest method precomputedGradient2FunctionWrapsPrecomputedValues.

@SeededTest
void precomputedGradient2FunctionWrapsPrecomputedValues(RandomSeed seed) {
    final int n = 3;
    final UniformRandomProvider r = RngUtils.create(seed.getSeed());
    final int size = 100;
    final double[] v = GdscSmlmTestUtils.generateDoubles(size, r);
    final double[][] g1 = new double[size][];
    final double[][] g2 = new double[size][];
    for (int i = 0; i < g1.length; i++) {
        g1[i] = GdscSmlmTestUtils.generateDoubles(n, r);
        g2[i] = GdscSmlmTestUtils.generateDoubles(n, r);
    }
    final Gradient2Function func = new PrecomputedGradient2Function(v, g1, g2);
    final double[][] g1o = new double[size][];
    final double[][] g2o = new double[size][];
    final double[] vo = evaluateGradient2Function(func, g1o, g2o);
    Assertions.assertArrayEquals(v, vo, "values");
    Assertions.assertArrayEquals(g1, g1o, "g1");
    Assertions.assertArrayEquals(g2, g2o, "g2");
}
Also used : UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Example 69 with UniformRandomProvider

use of org.apache.commons.rng.UniformRandomProvider in project GDSC-SMLM by aherbert.

the class FactorialTest method testFactorialDouble.

/**
 * Test the factorial of a fractional number against Commons Math gamma(1+n).
 */
@SeededTest
void testFactorialDouble(RandomSeed seed) {
    final UniformRandomProvider rng = RngUtils.create(seed.getSeed());
    final DoubleDoubleBiPredicate tol = TestHelper.doublesAreClose(5e-15).or(TestHelper.doublesEqual());
    for (int i = 0; i < 100; i++) {
        final double n = rng.nextDouble() * 180;
        final double expected = n < 1.5 ? 1 / (1 + Gamma.invGamma1pm1(n)) : Gamma.gamma(1 + n);
        TestAssertions.assertTest(expected, Factorial.value(n), tol, () -> Double.toString(n));
    }
}
Also used : DoubleDoubleBiPredicate(uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Example 70 with UniformRandomProvider

use of org.apache.commons.rng.UniformRandomProvider in project GDSC-SMLM by aherbert.

the class FastLogTest method canTestDoubleSpeedLog1P.

@SpeedTag
@SeededTest
void canTestDoubleSpeedLog1P(RandomSeed seed) {
    // No assertions, this is just a report
    Assumptions.assumeTrue(logger.isLoggable(Level.INFO));
    Assumptions.assumeTrue(TestSettings.allow(TestComplexity.MEDIUM));
    final UniformRandomProvider rng = RngUtils.create(seed.getSeed());
    final double[] x = new double[1000000];
    for (int i = 0; i < x.length; i++) {
        x[i] = nextUniformDouble(rng);
    }
    final MathLog fl = new MathLog();
    final TimingService ts = new TimingService(5);
    // ts.execute(new DoubleTimingTask(new TestLog(fl), 0, x));
    ts.execute(new DoubleTimingTask(new Test1PLog(fl), 0, x));
    ts.execute(new DoubleTimingTask(new TestLog1P(fl), 0, x));
    ts.execute(new DoubleTimingTask(new TestLog1PApache(fl), 0, x));
    // ts.execute(new DoubleTimingTask(new TestLog(fl), 0, x));
    ts.execute(new DoubleTimingTask(new Test1PLog(fl), 0, x));
    ts.execute(new DoubleTimingTask(new TestLog1P(fl), 0, x));
    ts.execute(new DoubleTimingTask(new TestLog1PApache(fl), 0, x));
    final int size = ts.getSize();
    ts.repeat(size);
    logger.info(ts.getReport(size));
}
Also used : UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) TimingService(uk.ac.sussex.gdsc.test.utils.TimingService) SpeedTag(uk.ac.sussex.gdsc.test.junit5.SpeedTag) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Aggregations

UniformRandomProvider (org.apache.commons.rng.UniformRandomProvider)213 SeededTest (uk.ac.sussex.gdsc.test.junit5.SeededTest)145 SharedStateContinuousSampler (org.apache.commons.rng.sampling.distribution.SharedStateContinuousSampler)17 TimingService (uk.ac.sussex.gdsc.test.utils.TimingService)14 Rectangle (java.awt.Rectangle)13 DoubleDoubleBiPredicate (uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate)13 SpeedTag (uk.ac.sussex.gdsc.test.junit5.SpeedTag)12 TDoubleArrayList (gnu.trove.list.array.TDoubleArrayList)10 ArrayList (java.util.ArrayList)10 NormalizedGaussianSampler (org.apache.commons.rng.sampling.distribution.NormalizedGaussianSampler)9 StoredDataStatistics (uk.ac.sussex.gdsc.core.utils.StoredDataStatistics)8 CalibrationWriter (uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter)8 ContinuousSampler (org.apache.commons.rng.sampling.distribution.ContinuousSampler)6 ImageExtractor (uk.ac.sussex.gdsc.core.utils.ImageExtractor)6 LocalList (uk.ac.sussex.gdsc.core.utils.LocalList)6 Gaussian2DFunction (uk.ac.sussex.gdsc.smlm.function.gaussian.Gaussian2DFunction)6 FloatProcessor (ij.process.FloatProcessor)5 ErfGaussian2DFunction (uk.ac.sussex.gdsc.smlm.function.gaussian.erf.ErfGaussian2DFunction)5 TimingResult (uk.ac.sussex.gdsc.test.utils.TimingResult)5 File (java.io.File)4