Search in sources :

Example 96 with UniformRandomProvider

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

the class PoissonDistributionTest method canComputeCumulativeProbability.

@SeededTest
void canComputeCumulativeProbability(RandomSeed seed) {
    final UniformRandomProvider rng = RngUtils.create(seed.getSeed());
    final PoissonDistribution fpd = new PoissonDistribution(1);
    for (int i = 1; i <= 100; i++) {
        final double mean = rng.nextDouble() * i;
        final org.apache.commons.math3.distribution.PoissonDistribution pd = createReferencePoissonDistribution(mean);
        fpd.setMean(mean);
        final int lower = (int) Math.floor(Math.max(0, mean - 5 * Math.sqrt(mean)));
        final int upper = (int) Math.ceil((mean + 5 * Math.sqrt(mean)));
        for (int x = lower; x <= upper; x++) {
            Assertions.assertEquals(pd.cumulativeProbability(x), fpd.cumulativeProbability(x), 1e-16);
        }
    }
}
Also used : UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Example 97 with UniformRandomProvider

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

the class DynamicMultipleTargetTracingTest method testTraceMoleculesDisableIntensityModel.

/**
 * Test trace molecules using 2 molecules. One is fixed and the other moves past it. The tracing
 * should assign the fixed molecule correctly as it has a low local diffusion rate.
 */
@Test
void testTraceMoleculesDisableIntensityModel() {
    final UniformRandomProvider rng = RngUtils.create(125631236L);
    final NormalizedGaussianSampler gauss = SamplerUtils.createNormalizedGaussianSampler(rng);
    // localisation precision (in pixels)
    final double s = 0.1;
    final SharedStateContinuousSampler intensity = SamplerUtils.createGaussianSampler(rng, 1000, 100);
    final MemoryPeakResults results = new MemoryPeakResults(100);
    final CalibrationWriter writer = results.getCalibrationWriterSafe();
    // 0.1 um pixels, 1 second exposure time
    writer.setDistanceUnit(DistanceUnit.PIXEL);
    writer.setNmPerPixel(100);
    writer.setExposureTime(1000);
    results.setCalibration(writer.getCalibration());
    // First molecule diffuses roughly across the field from top-left to bottom-right.
    // 5 frames is the default for local stats, 15 frames for trajectory removal.
    // Use 20 so we build local stats and can expire a trajectory.
    final int size = 20;
    final float x1 = size / 2 + 0.5f;
    for (int i = 0; i < size; i++) {
        results.add(new PeakResult(i, (float) (x1 + gauss.sample() * s), (float) (i + gauss.sample() * s), (float) (intensity.sample())));
    }
    // Second molecule is fixed in the centre with a same intensity
    final int x = size / 2;
    for (int i = 0; i < size; i++) {
        results.add(new PeakResult(i, (float) (x + gauss.sample() * s), (float) (x + gauss.sample() * s), (float) (intensity.sample())));
    }
    // Add a single molecule that will not connect to anything in the second frame.
    // This should create a trajectory that will expire.
    results.add(new PeakResult(1, x1, size, (float) (intensity.sample())));
    // Move centre to centre each jump => 0.1 um or 0.01 um^2
    // MSD = 4D => D = 0.01 / 4 = 0.0025
    final DmttConfiguration config = DmttConfiguration.newBuilder(0.0025).setDisableIntensityModel(true).setTemporalWindow(10).build();
    final List<Trace> traces = new DynamicMultipleTargetTracing(results).traceMolecules(config);
    // Should have 3 traces
    Assertions.assertEquals(3, traces.size());
    // Assert ids start from 1
    for (int i = 0; i < traces.size(); i++) {
        Assertions.assertEquals(i + 1, traces.get(i).getId());
    }
    // Traces should be 2 full length and 1 single peak
    Assertions.assertEquals(size, traces.get(0).size());
    Assertions.assertEquals(size, traces.get(1).size());
    Assertions.assertEquals(1, traces.get(2).size());
    // Do an analysis on the actual tracks.
    // One should be based in the centre and the other should have parts close to position (i,i)
    // for each frame i.
    final PeakResult[] peaks = results.toArray();
    // Assume traces are initially created using the input order of the results.
    final Trace t1 = traces.get(0);
    final Trace t2 = traces.get(1);
    for (int i = 0; i < size; i++) {
        Assertions.assertSame(peaks[i], t1.get(i));
        Assertions.assertSame(peaks[i + size], t2.get(i));
    }
}
Also used : SharedStateContinuousSampler(org.apache.commons.rng.sampling.distribution.SharedStateContinuousSampler) DmttConfiguration(uk.ac.sussex.gdsc.smlm.results.DynamicMultipleTargetTracing.DmttConfiguration) CalibrationWriter(uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) NormalizedGaussianSampler(org.apache.commons.rng.sampling.distribution.NormalizedGaussianSampler) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest) Test(org.junit.jupiter.api.Test)

Example 98 with UniformRandomProvider

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

the class PeakResultsReaderTest method canReadAndSimplifyGaussian2DPsf.

private static void canReadAndSimplifyGaussian2DPsf(RandomSeed seed, ResultsFileFormat fileFormat) {
    final UniformRandomProvider rg = RngUtils.create(seed.getSeed());
    final MemoryPeakResults out = createResults(rg, 1, false, false, false, false, false);
    final CalibrationWriter cal = new CalibrationWriter(out.getCalibration());
    cal.setDistanceUnit(MemoryPeakResults.PREFERRED_DISTANCE_UNIT);
    cal.setIntensityUnit(MemoryPeakResults.PREFERRED_INTENSITY_UNIT);
    cal.setAngleUnit(MemoryPeakResults.PREFERRED_ANGLE_UNIT);
    if (fileFormat == ResultsFileFormat.TSF) {
        // For now just support using the native float TSF value
        cal.setNmPerPixel((float) cal.getNmPerPixel());
    }
    out.setCalibration(cal.getCalibration());
    // Remove angle
    final int ia = PsfHelper.getGaussian2DAngleIndex(out.getPsf());
    out.forEach(new PeakResultProcedure() {

        @Override
        public void execute(PeakResult peakResult) {
            peakResult.getParameters()[ia] = 0;
        }
    });
    final String filename = createFile();
    writeFile(false, fileFormat, false, false, false, false, false, false, out, filename);
    MemoryPeakResults in = readFile(filename, false, false);
    // Change to two-axis PSF
    out.setPsf(PsfHelper.create(PSFType.TWO_AXIS_GAUSSIAN_2D));
    final int twoAxisLength = PsfHelper.getParameterCount(out.getPsf()) + PeakResult.STANDARD_PARAMETERS;
    out.forEach(new PeakResultProcedure() {

        @Override
        public void execute(PeakResult peakResult) {
            peakResult.resizeParameters(twoAxisLength);
        }
    });
    checkEqual(fileFormat, false, false, false, false, false, false, out, in);
    // Remove sy
    final int[] indices = PsfHelper.getGaussian2DWxWyIndices(out.getPsf());
    final int isx = indices[0];
    final int isy = indices[1];
    out.forEach(new PeakResultProcedure() {

        @Override
        public void execute(PeakResult peakResult) {
            final float[] p = peakResult.getParameters();
            p[isy] = p[isx];
        }
    });
    writeFile(false, fileFormat, false, false, false, false, false, false, out, filename);
    in = readFile(filename, false, false);
    // Change to one-axis PSF
    out.setPsf(PsfHelper.create(PSFType.ONE_AXIS_GAUSSIAN_2D));
    final int oneAxisLength = PsfHelper.getParameterCount(out.getPsf()) + PeakResult.STANDARD_PARAMETERS;
    out.forEach(new PeakResultProcedure() {

        @Override
        public void execute(PeakResult peakResult) {
            peakResult.resizeParameters(oneAxisLength);
        }
    });
    checkEqual(fileFormat, false, false, false, false, false, false, out, in);
}
Also used : PeakResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.PeakResultProcedure) CalibrationWriter(uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider)

Example 99 with UniformRandomProvider

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

the class PeakResultsReaderTest method readWith2IsFasterThan1.

private static void readWith2IsFasterThan1(RandomSeed seed, boolean showDeviations, boolean showEndFrame, boolean showId, boolean showPrecision, boolean showCategory, ResultsFileFormat f1, boolean useScanner1, ResultsFileFormat f2, boolean useScanner2, int loops) {
    Assumptions.assumeTrue(logger.isLoggable(Level.INFO));
    Assumptions.assumeTrue(TestSettings.allow(TestComplexity.HIGH));
    final UniformRandomProvider rg = RngUtils.create(seed.getSeed());
    final MemoryPeakResults out = createResults(rg, 20000, showDeviations, showEndFrame, showId, showPrecision, showCategory);
    final String filename = createFile();
    writeFile(false, f1, showDeviations, showEndFrame, showId, showPrecision, showCategory, false, out, filename);
    final long time1 = getReadTime(filename, useScanner1, loops);
    writeFile(false, f2, showDeviations, showEndFrame, showId, showPrecision, showCategory, false, out, filename);
    final long time2 = getReadTime(filename, useScanner2, loops);
    if (useScanner1 != useScanner2) {
        logger.info(FunctionUtils.getSupplier("%s (scan=%b) is %.2fx faster than %s (scan=%b)", f2, useScanner2, (double) time1 / time2, f1, useScanner1));
    } else {
        logger.info(FunctionUtils.getSupplier("%s is %.2fx faster than %s", f2, (double) time1 / time2, f1));
    }
    Assertions.assertTrue(time2 < time1, () -> String.format("%s (%d) is not faster than %s (%d)", f2, time2, f1, time1));
}
Also used : UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider)

Example 100 with UniformRandomProvider

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

the class FilterTest method canCompareMultiFilter.

@SeededTest
void canCompareMultiFilter(RandomSeed seed) {
    final UniformRandomProvider UniformRandomProvider = RngUtils.create(seed.getSeed());
    final MultiFilter f = new MultiFilter(0, 0, 0, 0, 0, 0, 0, 0, 0);
    for (int i = 1000; i-- > 0; ) {
        final MultiFilter f1 = (MultiFilter) f.create(random(f.getNumberOfParameters(), UniformRandomProvider));
        final MultiFilter f2 = (MultiFilter) f.create(random(f.getNumberOfParameters(), UniformRandomProvider));
        final int e = f1.weakest((Filter) f2);
        final int o = f1.weakest(f2);
        Assertions.assertEquals(e, o);
    }
}
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