Search in sources :

Example 71 with UniformRandomProvider

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

the class FastLogTest method canTestDoubleErrorRange.

@SeededTest
void canTestDoubleErrorRange(RandomSeed seed) {
    Assumptions.assumeTrue(logger.isLoggable(Level.INFO));
    Assumptions.assumeTrue(TestSettings.allow(TestComplexity.HIGH));
    final UniformRandomProvider rng = RngUtils.create(seed.getSeed());
    final LocalList<TestFastLog> test = new LocalList<>();
    final int n = 13;
    test.add(new TestFastLog(IcsiFastLog.create(n, DataType.DOUBLE)));
    test.add(new TestFastLog(new FFastLog(n)));
    test.add(new TestFastLog(new DFastLog(n)));
    test.add(new TestFastLog(new TurboLog(n)));
    // Full range in blocks.
    // Only when the number is around 1 or min value are there significant errors
    final double[] d = new double[10000000];
    final double[] logD = null;
    // All
    // testDoubleErrorRange(test, n, d, logD, 0, 255, 0);
    // Only a problem around min value and x==1
    // testDoubleErrorRange(rng, test, n, d, logD, 0, 2, 0);
    testDoubleErrorRange(rng, test, n, d, logD, 1021, 1026, 0);
    testDoubleErrorRange(rng, test, n, d, logD, 2045, 2047, 0);
}
Also used : LocalList(uk.ac.sussex.gdsc.core.utils.LocalList) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Example 72 with UniformRandomProvider

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

the class FastLogTest method canTestDoubleErrorLog1P.

@SeededTest
void canTestDoubleErrorLog1P(RandomSeed seed) {
    Assumptions.assumeTrue(logger.isLoggable(Level.INFO));
    Assumptions.assumeTrue(TestSettings.allow(TestComplexity.HIGH));
    // All float values is a lot so we do a representative set
    final UniformRandomProvider rng = RngUtils.create(seed.getSeed());
    final double lower = Double.MIN_VALUE;
    final double upper = Double.MAX_VALUE;
    final double[] d = new double[100000];
    final double[] logD = new double[d.length];
    for (int i = 0; i < d.length; i++) {
        final double v = nextUniformDouble(rng);
        d[i] = v;
        logD[i] = Math.log1p(v);
    }
    runCanTestDoubleError(new Test1PLog(new MathLog()), d, logD);
    runCanTestDoubleError(new TestLog1P(new MathLog()), d, logD);
}
Also used : UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Example 73 with UniformRandomProvider

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

the class FastLogTest method canTestFloatSpeed.

@SpeedTag
@SeededTest
void canTestFloatSpeed(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 float[] x = new float[1000000];
    for (int i = 0; i < x.length; i++) {
        x[i] = nextUniformFloat(rng);
    }
    final TimingService ts = new TimingService(5);
    ts.execute(new FloatTimingTask(new TestLog(new MathLog()), 0, x));
    ts.execute(new FloatTimingTask(new TestLog(new FastMathLog()), 0, x));
    for (final int q : new int[] { 11 }) {
        final int n = 23 - q;
        final IcsiFastLog fl = IcsiFastLog.create(n, DataType.FLOAT);
        ts.execute(new FloatTimingTask(new TestLog(fl), q, x));
        ts.execute(new FloatTimingTask(new TestFastLog(fl), q, x));
        final FFastLog ff = new FFastLog(n);
        ts.execute(new FloatTimingTask(new TestLog(ff), q, x));
        ts.execute(new FloatTimingTask(new TestFastLog(ff), q, x));
        final DFastLog df = new DFastLog(n);
        ts.execute(new FloatTimingTask(new TestLog(df), q, x));
        ts.execute(new FloatTimingTask(new TestFastLog(df), q, x));
        final TurboLog tf = new TurboLog(n);
        ts.execute(new FloatTimingTask(new TestLog(tf), q, x));
        ts.execute(new FloatTimingTask(new TestFastLog(tf), q, x));
        // TurboLog2 tf2 = new TurboLog2(n);
        // ts.execute(new FloatTimingTask(new TestLog(tf2), q, x));
        // ts.execute(new FloatTimingTask(new TestFastLog(tf2), q, x));
        // For the same precision we can reduce n
        final TurboLog2 tf3 = new TurboLog2(n - 1);
        ts.execute(new FloatTimingTask(new TestLog(tf3), q + 1, x));
        ts.execute(new FloatTimingTask(new TestFastLog(tf3), q + 1, 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)

Example 74 with UniformRandomProvider

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

the class CmosAnalysis method simulate.

private void simulate() throws IOException {
    // Create the offset, variance and gain for each pixel
    final int n = settings.size * settings.size;
    final float[] pixelOffset = new float[n];
    final float[] pixelVariance = new float[n];
    final float[] pixelGain = new float[n];
    IJ.showStatus("Creating random per-pixel readout");
    final long start = System.currentTimeMillis();
    final UniformRandomProvider rg = UniformRandomProviders.create();
    final DiscreteSampler pd = PoissonSamplerUtils.createPoissonSampler(rg, settings.offset);
    final ContinuousSampler ed = SamplerUtils.createExponentialSampler(rg, settings.variance);
    final SharedStateContinuousSampler gauss = SamplerUtils.createGaussianSampler(rg, settings.gain, settings.gainStdDev);
    Ticker ticker = ImageJUtils.createTicker(n, 0);
    for (int i = 0; i < n; i++) {
        // Q. Should these be clipped to a sensible range?
        pixelOffset[i] = pd.sample();
        pixelVariance[i] = (float) ed.sample();
        pixelGain[i] = (float) gauss.sample();
        ticker.tick();
    }
    IJ.showProgress(1);
    // Save to the directory as a stack
    final ImageStack simulationStack = new ImageStack(settings.size, settings.size);
    simulationStack.addSlice("Offset", pixelOffset);
    simulationStack.addSlice("Variance", pixelVariance);
    simulationStack.addSlice("Gain", pixelGain);
    simulationImp = new ImagePlus("PerPixel", simulationStack);
    // Only the info property is saved to the TIFF file
    simulationImp.setProperty("Info", String.format("Offset=%s; Variance=%s; Gain=%s +/- %s", MathUtils.rounded(settings.offset), MathUtils.rounded(settings.variance), MathUtils.rounded(settings.gain), MathUtils.rounded(settings.gainStdDev)));
    IJ.save(simulationImp, new File(settings.directory, "perPixelSimulation.tif").getPath());
    // Create thread pool and workers
    final int threadCount = getThreads();
    final ExecutorService executor = Executors.newFixedThreadPool(threadCount);
    final LocalList<Future<?>> futures = new LocalList<>(numberOfThreads);
    // Simulate the exposure input.
    final int[] photons = settings.getPhotons();
    // For saving stacks
    final int blockSize = 10;
    int numberPerThread = (int) Math.ceil((double) settings.frames / numberOfThreads);
    // Convert to fit the block size
    numberPerThread = (int) Math.ceil((double) numberPerThread / blockSize) * blockSize;
    final Pcg32 rng = Pcg32.xshrs(start);
    // Note the bias is increased by 3-fold so add 2 to the length
    ticker = Ticker.createStarted(new ImageJTrackProgress(true), (long) (photons.length + 2) * settings.frames, threadCount > 1);
    for (final int p : photons) {
        ImageJUtils.showStatus(() -> "Simulating " + TextUtils.pleural(p, "photon"));
        // Create the directory
        final Path out = Paths.get(settings.directory, String.format("photon%03d", p));
        Files.createDirectories(out);
        // Increase frames for bias image
        final int frames = settings.frames * (p == 0 ? 3 : 1);
        for (int from = 0; from < frames; ) {
            final int to = Math.min(from + numberPerThread, frames);
            futures.add(executor.submit(new SimulationWorker(ticker, rng.split(), out.toString(), simulationStack, from, to, blockSize, p)));
            from = to;
        }
        ConcurrencyUtils.waitForCompletionUnchecked(futures);
        futures.clear();
    }
    final String msg = "Simulation time = " + TextUtils.millisToString(System.currentTimeMillis() - start);
    IJ.showStatus(msg);
    ImageJUtils.clearSlowProgress();
    executor.shutdown();
    ImageJUtils.log(msg);
}
Also used : ContinuousSampler(org.apache.commons.rng.sampling.distribution.ContinuousSampler) SharedStateContinuousSampler(org.apache.commons.rng.sampling.distribution.SharedStateContinuousSampler) Path(java.nio.file.Path) ImageStack(ij.ImageStack) Pcg32(uk.ac.sussex.gdsc.core.utils.rng.Pcg32) Ticker(uk.ac.sussex.gdsc.core.logging.Ticker) SharedStateContinuousSampler(org.apache.commons.rng.sampling.distribution.SharedStateContinuousSampler) ImagePlus(ij.ImagePlus) LocalList(uk.ac.sussex.gdsc.core.utils.LocalList) ImageJTrackProgress(uk.ac.sussex.gdsc.core.ij.ImageJTrackProgress) DiscreteSampler(org.apache.commons.rng.sampling.distribution.DiscreteSampler) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) File(java.io.File)

Example 75 with UniformRandomProvider

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

the class AreaAverageFilterTest method areaAverageUsingSumsInternalCorrectlyInterpolatesBetweenBlocks.

@SeededTest
void areaAverageUsingSumsInternalCorrectlyInterpolatesBetweenBlocks(RandomSeed seed) {
    final UniformRandomProvider rg = RngUtils.create(seed.getSeed());
    final int max = 50;
    final float[] data = createData(rg, max, max);
    final AreaAverageFilter filter = new AreaAverageFilter();
    filter.setSimpleInterpolation(false);
    final int n = 30;
    final float[][] results = new float[n + 1][];
    final double[] w = new double[n + 1];
    int count = 0;
    for (int i = 0; i <= n; i++) {
        w[count] = i / 10.0;
        results[count] = data.clone();
        filter.areaAverageUsingSumsInternal(results[count], max, max, w[count]);
        count++;
    }
    checkInterpolation(max, n, results, count);
}
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