Search in sources :

Example 36 with UniformRandomProvider

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

the class FisherInformationMatrixTest method inversionDoesNotMatchReciprocal.

@SeededTest
void inversionDoesNotMatchReciprocal(RandomSeed seed) {
    final UniformRandomProvider rg = RngUtils.create(seed.getSeed());
    for (int n = 1; n < 10; n++) {
        final FisherInformationMatrix m = createFisherInformationMatrix(rg, n, 0);
        final double[] crlb = m.crlb();
        final double[] crlb2 = m.crlbReciprocal();
        // These increasingly do not match with increasing number of parameters.
        if (logger.isLoggable(level)) {
            logger.log(level, FunctionUtils.getSupplier("%s =? %s", Arrays.toString(crlb), Arrays.toString(crlb2)));
        }
        if (n > 1) {
            // Just do a sum so we have a test
            Assertions.assertThrows(AssertionFailedError.class, () -> {
                Assertions.assertEquals(MathUtils.sum(crlb), MathUtils.sum(crlb2));
            });
        }
    }
}
Also used : UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Example 37 with UniformRandomProvider

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

the class GaussianFilterTest method filter1IsSameAsFilter2.

private void filter1IsSameAsFilter2(RandomSeed seed, GFilter f1, GFilter f2, boolean weighted, double tolerance) {
    final UniformRandomProvider rand = RngUtils.create(seed.getSeed());
    final float[] data = createData(rand, size, size);
    float[] weights = null;
    if (weighted) {
        final ContinuousSampler ed = SamplerUtils.createExponentialSampler(rand, 57);
        weights = new float[data.length];
        for (int i = 0; i < weights.length; i++) {
            weights[i] = (float) (1.0 / Math.max(0.01, ed.sample()));
        }
        // w[i] = (float) (1.0 / Math.max(0.01, rand.nextGaussian() * 0.2 + 2));
        // w[i] = 0.5f;
        f1.setWeights(weights);
        f2.setWeights(weights);
    }
    for (final double sigma : sigmas) {
        final float[] e = data.clone();
        f2.run(e, sigma);
        final float[] o = data.clone();
        f1.run(o, sigma);
        double max = 0;
        for (int i = 0; i < e.length; i++) {
            final double d = DoubleEquality.relativeError(e[i], o[i]);
            if (max < d) {
                max = d;
            }
        }
        logger.fine(FunctionUtils.getSupplier("%s vs %s w=%b @ %.1f = %g", f1.getName(), f2.getName(), weighted, sigma, max));
        Assertions.assertTrue(max < tolerance);
    }
}
Also used : ContinuousSampler(org.apache.commons.rng.sampling.distribution.ContinuousSampler) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider)

Example 38 with UniformRandomProvider

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

the class GaussianFilterTest method floatFilterInternalIsFasterThanDoubleFilterInternal.

@SpeedTag
@SeededTest
void floatFilterInternalIsFasterThanDoubleFilterInternal(RandomSeed seed) {
    Assumptions.assumeTrue(TestSettings.allow(TestComplexity.HIGH));
    final UniformRandomProvider rg = RngUtils.create(seed.getSeed());
    final float[][] data = new float[10][];
    for (int i = 0; i < data.length; i++) {
        data[i] = createData(rg, size, size);
    }
    final TimingService ts = new TimingService();
    for (final double sigma : sigmas) {
        ts.execute(new MyTimingTask(new FloatFilter(true), data, sigma));
        ts.execute(new MyTimingTask(new DpFilter(false), data, sigma));
        ts.execute(new MyTimingTask(new DoubleFilter(true), data, sigma));
    }
    final int size = ts.getSize();
    ts.repeat();
    if (logger.isLoggable(Level.INFO)) {
        logger.info(ts.getReport(size));
    }
    final int n = size / sigmas.length;
    for (int i = 0, j = size; i < sigmas.length; i++, j += n) {
        for (int k = 1; k < n; k++) {
            final TimingResult slow = ts.get(j + k);
            final TimingResult fast = ts.get(j);
            logger.log(TestLogUtils.getTimingRecord(slow, fast));
        }
    }
}
Also used : TimingResult(uk.ac.sussex.gdsc.test.utils.TimingResult) 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)

Example 39 with UniformRandomProvider

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

the class MedianFilterTest method floatRollingMedian3x3InternalAndRollingMedianNxNInternalReturnSameResult.

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

Example 40 with UniformRandomProvider

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

the class MedianFilterTest method floatBlockMedianNxNInternalAndRollingMedianNxNInternalReturnSameResult.

@SeededTest
public void floatBlockMedianNxNInternalAndRollingMedianNxNInternalReturnSameResult(RandomSeed seed) {
    final UniformRandomProvider rg = RngUtils.create(seed.getSeed());
    final MedianFilter filter = new MedianFilter();
    for (final int width : primes) {
        for (final int height : primes) {
            for (final int boxSize : boxSizes) {
                floatCompareBlockMedianNxNInternalAndRollingMedianNxNInternal(rg, filter, width, height, boxSize);
            }
        }
    }
}
Also used : UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) 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