Search in sources :

Example 31 with SeededTest

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

the class WeightedKernelFilterTest method filterPerformsWeightedKernelFiltering.

@SeededTest
void filterPerformsWeightedKernelFiltering(RandomSeed seed) {
    final DataFilter filter = createDataFilter();
    final UniformRandomProvider rg = RngUtils.create(seed.getSeed());
    final SharedStateContinuousSampler gs = SamplerUtils.createGaussianSampler(rg, 2, 0.2);
    final float[] offsets = getOffsets(filter);
    final int[] boxSizes = getBoxSizes(filter);
    final TDoubleArrayList l1 = new TDoubleArrayList();
    final FloatFloatBiPredicate equality = TestHelper.floatsAreClose(1e-6, 0);
    for (final int width : primes) {
        for (final int height : new int[] { 29 }) {
            final float[] data = createData(width, height, rg);
            l1.reset();
            // Uniform weights
            final float[] w1 = new float[width * height];
            Arrays.fill(w1, 0.5f);
            // Weights simulating the variance of sCMOS pixels
            final float[] w2 = new float[width * height];
            for (int i = 0; i < w2.length; i++) {
                w2[i] = (float) (1.0 / Math.max(0.01, gs.sample()));
            }
            for (final int boxSize : boxSizes) {
                for (final float offset : offsets) {
                    for (final boolean internal : checkInternal) {
                        // For each pixel over the range around the pixel (vi).
                        // kernel filter: sum(vi * ki) / sum(ki)
                        // Weighted kernel filter: sum(vi * wi * ki) / sum(ki * wi)
                        // Note: The kernel filter is like a weighted filter
                        // (New kernel = wi * ki)
                        filter.setWeights(null, width, height);
                        // Uniform weights
                        testfilterPerformsWeightedFiltering(filter, width, height, data, w1, boxSize, offset, internal, equality);
                        // Random weights.
                        testfilterPerformsWeightedFiltering(filter, width, height, data, w2, boxSize, offset, internal, equality);
                    }
                }
            }
        }
    }
}
Also used : TDoubleArrayList(gnu.trove.list.array.TDoubleArrayList) SharedStateContinuousSampler(org.apache.commons.rng.sampling.distribution.SharedStateContinuousSampler) FloatFloatBiPredicate(uk.ac.sussex.gdsc.test.api.function.FloatFloatBiPredicate) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Example 32 with SeededTest

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

the class FisherInformationMatrixTest method computeWithSubsetReducesTheCrlb.

@SeededTest
void computeWithSubsetReducesTheCrlb(RandomSeed seed) {
    final UniformRandomProvider rg = RngUtils.create(seed.getSeed());
    final Gaussian2DFunction f = createFunction(10, 1);
    final int perPeak = f.getGradientParametersPerPeak();
    // Create a matrix with 2 peaks + background
    final FisherInformationMatrix m = createFisherInformationMatrix(rg, 1 + 2 * perPeak, 0);
    // Subset each peak
    final int[] indices = SimpleArrayUtils.natural(1 + perPeak);
    final FisherInformationMatrix m1 = m.subset(indices);
    for (int i = 1; i < indices.length; i++) {
        indices[i] += perPeak;
    }
    final FisherInformationMatrix m2 = m.subset(indices);
    // TestLog.fine(logger,m.getMatrix());
    // TestLog.fine(logger,m1.getMatrix());
    // TestLog.fine(logger,m2.getMatrix());
    final double[] crlb = m.crlb();
    final double[] crlb1 = m1.crlb();
    final double[] crlb2 = m2.crlb();
    final double[] crlbB = Arrays.copyOf(crlb1, crlb.length);
    System.arraycopy(crlb2, 1, crlbB, crlb1.length, perPeak);
    // Removing the interaction between fit parameters lowers the bounds
    for (int i = 0; i < crlb.length; i++) {
        Assertions.assertTrue(crlbB[i] < crlb[i]);
    }
}
Also used : Gaussian2DFunction(uk.ac.sussex.gdsc.smlm.function.gaussian.Gaussian2DFunction) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Example 33 with SeededTest

use of uk.ac.sussex.gdsc.test.junit5.SeededTest 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 34 with SeededTest

use of uk.ac.sussex.gdsc.test.junit5.SeededTest 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 35 with SeededTest

use of uk.ac.sussex.gdsc.test.junit5.SeededTest 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)

Aggregations

SeededTest (uk.ac.sussex.gdsc.test.junit5.SeededTest)172 UniformRandomProvider (org.apache.commons.rng.UniformRandomProvider)142 DoubleDoubleBiPredicate (uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate)18 Rectangle (java.awt.Rectangle)12 SpeedTag (uk.ac.sussex.gdsc.test.junit5.SpeedTag)12 TimingService (uk.ac.sussex.gdsc.test.utils.TimingService)12 SharedStateContinuousSampler (org.apache.commons.rng.sampling.distribution.SharedStateContinuousSampler)7 TDoubleArrayList (gnu.trove.list.array.TDoubleArrayList)6 FloatProcessor (ij.process.FloatProcessor)6 ErfGaussian2DFunction (uk.ac.sussex.gdsc.smlm.function.gaussian.erf.ErfGaussian2DFunction)6 DummyGradientFunction (uk.ac.sussex.gdsc.smlm.function.DummyGradientFunction)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 Gaussian2DFunction (uk.ac.sussex.gdsc.smlm.function.gaussian.Gaussian2DFunction)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 FloatFloatBiPredicate (uk.ac.sussex.gdsc.test.api.function.FloatFloatBiPredicate)4 MultivariateNormalMixtureExpectationMaximization (org.apache.commons.math3.distribution.fitting.MultivariateNormalMixtureExpectationMaximization)3 DoubleEquality (uk.ac.sussex.gdsc.core.utils.DoubleEquality)3