Search in sources :

Example 1 with SpeedTag

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

the class JTransformsTest method jtransforms2DDhtIsFasterThanFht2.

@SpeedTag
@SeededTest
void jtransforms2DDhtIsFasterThanFht2(RandomSeed seed) {
    Assumptions.assumeTrue(TestSettings.allow(TestComplexity.MEDIUM));
    // Test the forward DHT of data. and reverse transform or the pre-computed correlation.
    final int size = 256;
    final int w = size / 4;
    final UniformRandomProvider r = RngUtils.create(seed.getSeed());
    // Blob in the centre
    FloatProcessor fp = createProcessor(size, size / 2 - w / 2, size / 2 - w / 2, w, w, null);
    final Fht fht2 = new Fht((float[]) fp.getPixels(), size, false);
    fht2.transform();
    fht2.initialiseFastMultiply();
    // Random blobs, original and correlated
    final int N = 40;
    final float[][] data = new float[N * 2][];
    final int lower = w;
    final int upper = size - w;
    final int range = upper - lower;
    for (int i = 0, j = 0; i < N; i++) {
        final int x = lower + r.nextInt(range);
        final int y = lower + r.nextInt(range);
        fp = createProcessor(size, x, y, w, w, r);
        final float[] pixels = (float[]) fp.getPixels();
        data[j++] = pixels.clone();
        final Fht fht1 = new Fht(pixels, size, false);
        fht1.copyTables(fht2);
        fht2.transform();
        final float[] pixels2 = new float[pixels.length];
        fht2.conjugateMultiply(fht2, pixels2);
        data[j++] = pixels2;
    }
    // CommonUtils.setThreadsBeginN_1D_FFT_2Threads(Long.MAX_VALUE);
    // CommonUtils.setThreadsBeginN_1D_FFT_4Threads(Long.MAX_VALUE);
    CommonUtils.setThreadsBeginN_2D(Long.MAX_VALUE);
    final TimingService ts = new TimingService();
    ts.execute(new ImageJFhtSpeedTask(size, data));
    ts.execute(new ImageJFht2SpeedTask(size, data));
    ts.execute(new JTransformsDhtSpeedTask(size, data));
    ts.repeat();
    if (logger.isLoggable(Level.INFO)) {
        logger.info(ts.getReport());
    }
    // Assertions.assertTrue(ts.get(-1).getMean() < ts.get(-2).getMean());
    final TimingResult slow = ts.get(-2);
    final TimingResult fast = ts.get(-1);
    logger.log(TestLogUtils.getTimingRecord(slow, fast));
}
Also used : Fht(uk.ac.sussex.gdsc.core.ij.process.Fht) TimingResult(uk.ac.sussex.gdsc.test.utils.TimingResult) FloatProcessor(ij.process.FloatProcessor) 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 2 with SpeedTag

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

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

Example 4 with SpeedTag

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

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

the class MultivariateGaussianMixtureExpectationMaximizationTest method testExpectationMaximizationSpeed.

/**
 * Test the speed of implementations of the expectation maximization algorithm with a mixture of n
 * ND Gaussian distributions.
 *
 * @param seed the seed
 */
@SpeedTag
@SeededTest
void testExpectationMaximizationSpeed(RandomSeed seed) {
    Assumptions.assumeTrue(TestSettings.allow(TestComplexity.HIGH));
    final MultivariateGaussianMixtureExpectationMaximization.DoubleDoubleBiPredicate relChecker = TestHelper.doublesAreClose(1e-6)::test;
    // Create data
    final UniformRandomProvider rng = RngUtils.create(seed.getSeed());
    for (int n = 2; n <= 3; n++) {
        for (int dim = 2; dim <= 4; dim++) {
            final double[][][] data = new double[10][][];
            final int nCorrelations = dim - 1;
            for (int i = 0; i < data.length; i++) {
                final double[] sampleWeights = createWeights(n, rng);
                final double[][] sampleMeans = create(n, dim, rng, -5, 5);
                final double[][] sampleStdDevs = create(n, dim, rng, 1, 10);
                final double[][] sampleCorrelations = IntStream.range(0, n).mapToObj(component -> create(nCorrelations, rng, -0.9, 0.9)).toArray(double[][]::new);
                data[i] = createDataNd(1000, rng, sampleWeights, sampleMeans, sampleStdDevs, sampleCorrelations);
            }
            final int numComponents = n;
            // Time initial estimation and fitting
            final TimingService ts = new TimingService();
            ts.execute(new FittingSpeedTask("Commons n=" + n + " " + dim + "D", data) {

                @Override
                Object run(double[][] data) {
                    final MultivariateNormalMixtureExpectationMaximization fitter = new MultivariateNormalMixtureExpectationMaximization(data);
                    fitter.fit(MultivariateNormalMixtureExpectationMaximization.estimate(data, numComponents));
                    return fitter.getLogLikelihood();
                }
            });
            ts.execute(new FittingSpeedTask("GDSC n=" + n + " " + dim + "D", data) {

                @Override
                Object run(double[][] data) {
                    final MultivariateGaussianMixtureExpectationMaximization fitter = new MultivariateGaussianMixtureExpectationMaximization(data);
                    fitter.fit(MultivariateGaussianMixtureExpectationMaximization.estimate(data, numComponents));
                    return fitter.getLogLikelihood();
                }
            });
            ts.execute(new FittingSpeedTask("GDSC rel 1e-6 n=" + n + " " + dim + "D", data) {

                @Override
                Object run(double[][] data) {
                    final MultivariateGaussianMixtureExpectationMaximization fitter = new MultivariateGaussianMixtureExpectationMaximization(data);
                    fitter.fit(MultivariateGaussianMixtureExpectationMaximization.estimate(data, numComponents), 1000, relChecker);
                    return fitter.getLogLikelihood();
                }
            });
            if (logger.isLoggable(Level.INFO)) {
                logger.info(ts.getReport());
            }
            // More than twice as fast
            Assertions.assertTrue(ts.get(-2).getMean() < ts.get(-3).getMean() / 2);
        }
    }
}
Also used : IntStream(java.util.stream.IntStream) RandomUtils(uk.ac.sussex.gdsc.core.utils.rng.RandomUtils) Arrays(java.util.Arrays) MultivariateGaussianDistribution(uk.ac.sussex.gdsc.smlm.math3.distribution.fitting.MultivariateGaussianMixtureExpectationMaximization.MixtureMultivariateGaussianDistribution.MultivariateGaussianDistribution) BaseTimingTask(uk.ac.sussex.gdsc.test.utils.BaseTimingTask) RngUtils(uk.ac.sussex.gdsc.test.rng.RngUtils) Covariance(org.apache.commons.math3.stat.correlation.Covariance) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) MultivariateNormalMixtureExpectationMaximization(org.apache.commons.math3.distribution.fitting.MultivariateNormalMixtureExpectationMaximization) AfterAll(org.junit.jupiter.api.AfterAll) Mean(org.apache.commons.math3.stat.descriptive.moment.Mean) TimingService(uk.ac.sussex.gdsc.test.utils.TimingService) BeforeAll(org.junit.jupiter.api.BeforeAll) ContinuousUniformSampler(org.apache.commons.rng.sampling.distribution.ContinuousUniformSampler) MultivariateNormalDistribution(org.apache.commons.math3.distribution.MultivariateNormalDistribution) TestComplexity(uk.ac.sussex.gdsc.test.utils.TestComplexity) MixtureMultivariateNormalDistribution(org.apache.commons.math3.distribution.MixtureMultivariateNormalDistribution) MathUtils(uk.ac.sussex.gdsc.core.utils.MathUtils) TestAssertions(uk.ac.sussex.gdsc.test.api.TestAssertions) RandomSeed(uk.ac.sussex.gdsc.test.junit5.RandomSeed) Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) SpeedTag(uk.ac.sussex.gdsc.test.junit5.SpeedTag) Pair(org.apache.commons.math3.util.Pair) DoubleDoubleBiPredicate(uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate) RandomGeneratorAdapter(uk.ac.sussex.gdsc.core.utils.rng.RandomGeneratorAdapter) Logger(java.util.logging.Logger) SamplerUtils(uk.ac.sussex.gdsc.core.utils.rng.SamplerUtils) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest) Test(org.junit.jupiter.api.Test) List(java.util.List) Assumptions(org.junit.jupiter.api.Assumptions) TestSettings(uk.ac.sussex.gdsc.test.utils.TestSettings) SimpleArrayUtils(uk.ac.sussex.gdsc.core.utils.SimpleArrayUtils) SharedStateContinuousSampler(org.apache.commons.rng.sampling.distribution.SharedStateContinuousSampler) Assertions(org.junit.jupiter.api.Assertions) TestHelper(uk.ac.sussex.gdsc.test.api.TestHelper) MixtureMultivariateGaussianDistribution(uk.ac.sussex.gdsc.smlm.math3.distribution.fitting.MultivariateGaussianMixtureExpectationMaximization.MixtureMultivariateGaussianDistribution) NormalizedGaussianSampler(org.apache.commons.rng.sampling.distribution.NormalizedGaussianSampler) LocalList(uk.ac.sussex.gdsc.core.utils.LocalList) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) TimingService(uk.ac.sussex.gdsc.test.utils.TimingService) MultivariateNormalMixtureExpectationMaximization(org.apache.commons.math3.distribution.fitting.MultivariateNormalMixtureExpectationMaximization) SpeedTag(uk.ac.sussex.gdsc.test.junit5.SpeedTag) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Aggregations

SpeedTag (uk.ac.sussex.gdsc.test.junit5.SpeedTag)16 SeededTest (uk.ac.sussex.gdsc.test.junit5.SeededTest)13 TimingService (uk.ac.sussex.gdsc.test.utils.TimingService)13 UniformRandomProvider (org.apache.commons.rng.UniformRandomProvider)12 TimingResult (uk.ac.sussex.gdsc.test.utils.TimingResult)6 Test (org.junit.jupiter.api.Test)5 LocalList (uk.ac.sussex.gdsc.core.utils.LocalList)3 MultivariateNormalMixtureExpectationMaximization (org.apache.commons.math3.distribution.fitting.MultivariateNormalMixtureExpectationMaximization)2 Gaussian2DFunctionTest (uk.ac.sussex.gdsc.smlm.function.gaussian.Gaussian2DFunctionTest)2 FloatProcessor (ij.process.FloatProcessor)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Level (java.util.logging.Level)1 Logger (java.util.logging.Logger)1 IntStream (java.util.stream.IntStream)1 MixtureMultivariateNormalDistribution (org.apache.commons.math3.distribution.MixtureMultivariateNormalDistribution)1 MultivariateNormalDistribution (org.apache.commons.math3.distribution.MultivariateNormalDistribution)1 Array2DRowRealMatrix (org.apache.commons.math3.linear.Array2DRowRealMatrix)1 Covariance (org.apache.commons.math3.stat.correlation.Covariance)1