Search in sources :

Example 1 with RandomSeed

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

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

the class ConfigurationTemplateTest method canLoadTemplateImageFromFile.

@SeededTest
void canLoadTemplateImageFromFile(RandomSeed seed) throws IOException {
    ConfigurationTemplate.clearTemplates();
    Assertions.assertEquals(0, ConfigurationTemplate.getTemplateNamesWithImage().length);
    // Create a dummy image
    final int size = 20;
    final float[] pixels = new float[size * size];
    final UniformRandomProvider r = RngUtils.create(seed.getSeed());
    for (int i = pixels.length; i-- > 0; ) {
        pixels[i] = r.nextFloat();
    }
    final ImagePlus imp = new ImagePlus("test", new FloatProcessor(size, size, pixels));
    final File tmpFile = File.createTempFile("tmp", ".tif");
    tmpFile.deleteOnExit();
    IJ.save(imp, tmpFile.getPath());
    final String name = "canLoadTemplateImageFromFile";
    final File file = new File(FileUtils.replaceExtension(tmpFile.getPath(), ".xml"));
    ConfigurationTemplate.saveTemplate(name, TemplateSettings.getDefaultInstance(), file);
    Assertions.assertEquals(1, ConfigurationTemplate.getTemplateNamesWithImage().length);
    final ImagePlus imp2 = ConfigurationTemplate.getTemplateImage(name);
    Assertions.assertNotNull(imp2);
    final float[] data = (float[]) imp2.getProcessor().toFloat(0, null).getPixels();
    Assertions.assertArrayEquals(pixels, data);
}
Also used : FloatProcessor(ij.process.FloatProcessor) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) ImagePlus(ij.ImagePlus) File(java.io.File) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Example 3 with RandomSeed

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

the class FrcTest method canComputeMirrored.

@SeededTest
void canComputeMirrored(RandomSeed seed) {
    // Sample lines through an image to create a structure.
    final int size = 1024;
    final double[][] data = new double[size * 2][];
    final UniformRandomProvider r = RngUtils.create(seed.getSeed());
    final SharedStateContinuousSampler gs = SamplerUtils.createGaussianSampler(r, 0, 5);
    for (int x = 0, y = 0, y2 = size, i = 0; x < size; x++, y++, y2--) {
        data[i++] = new double[] { x + gs.sample(), y + gs.sample() };
        data[i++] = new double[] { x + gs.sample(), y2 + gs.sample() };
    }
    // Create 2 images
    final Rectangle bounds = new Rectangle(0, 0, size, size);
    ImageJImagePeakResults i1 = createImage(bounds);
    ImageJImagePeakResults i2 = createImage(bounds);
    final int[] indices = SimpleArrayUtils.natural(data.length);
    PermutationSampler.shuffle(r, indices);
    for (final int i : indices) {
        final ImageJImagePeakResults image = i1;
        i1 = i2;
        i2 = image;
        image.add((float) data[i][0], (float) data[i][1], 1);
    }
    i1.end();
    i2.end();
    final ImageProcessor ip1 = i1.getImagePlus().getProcessor();
    final ImageProcessor ip2 = i2.getImagePlus().getProcessor();
    // Test
    final Frc frc = new Frc();
    FloatProcessor[] fft1;
    FloatProcessor[] fft2;
    fft1 = frc.getComplexFft(ip1);
    fft2 = frc.getComplexFft(ip2);
    final float[] dataA1 = (float[]) fft1[0].getPixels();
    final float[] dataB1 = (float[]) fft1[1].getPixels();
    final float[] dataA2 = (float[]) fft2[0].getPixels();
    final float[] dataB2 = (float[]) fft2[1].getPixels();
    final float[] numeratorE = new float[dataA1.length];
    final float[] absFft1E = new float[dataA1.length];
    final float[] absFft2E = new float[dataA1.length];
    Frc.compute(numeratorE, absFft1E, absFft2E, dataA1, dataB1, dataA2, dataB2);
    Assertions.assertTrue(checkSymmetry(numeratorE, size), "numeratorE");
    Assertions.assertTrue(checkSymmetry(absFft1E, size), "absFft1E");
    Assertions.assertTrue(checkSymmetry(absFft2E, size), "absFft2E");
    final float[] numeratorA = new float[dataA1.length];
    final float[] absFft1A = new float[dataA1.length];
    final float[] absFft2A = new float[dataA1.length];
    Frc.computeMirrored(size, numeratorA, absFft1A, absFft2A, dataA1, dataB1, dataA2, dataB2);
    // for (int y=0, i=0; y<size; y++)
    // for (int x=0; x<size; x++, i++)
    // {
    // logger.fine(FunctionUtils.getSupplier("[%d,%d = %d] %f ?= %f", x, y, i, numeratorE[i],
    // numeratorA[i]);
    // }
    Assertions.assertArrayEquals(numeratorE, numeratorA, "numerator");
    Assertions.assertArrayEquals(absFft1E, absFft1A, "absFft1");
    Assertions.assertArrayEquals(absFft2E, absFft2A, "absFft2");
    Frc.computeMirroredFast(size, numeratorA, absFft1A, absFft2A, dataA1, dataB1, dataA2, dataB2);
    // Check this.
    for (int y = 1; y < size; y++) {
        for (int x = 1, i = y * size + 1; x < size; x++, i++) {
            Assertions.assertEquals(numeratorE[i], numeratorA[i], "numerator");
            Assertions.assertEquals(absFft1E[i], absFft1A[i], "absFft1");
            Assertions.assertEquals(absFft2E[i], absFft2A[i], "absFft2");
        }
    }
}
Also used : ImageProcessor(ij.process.ImageProcessor) FloatProcessor(ij.process.FloatProcessor) SharedStateContinuousSampler(org.apache.commons.rng.sampling.distribution.SharedStateContinuousSampler) Rectangle(java.awt.Rectangle) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) ImageJImagePeakResults(uk.ac.sussex.gdsc.smlm.ij.results.ImageJImagePeakResults) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Example 4 with RandomSeed

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

the class FrcTest method computeMirroredIsFaster.

@SeededTest
void computeMirroredIsFaster(RandomSeed seed) {
    Assumptions.assumeTrue(TestSettings.allow(TestComplexity.MEDIUM));
    // Sample lines through an image to create a structure.
    final int N = 2048;
    final double[][] data = new double[N * 2][];
    final UniformRandomProvider r = RngUtils.create(seed.getSeed());
    final SharedStateContinuousSampler gs = SamplerUtils.createGaussianSampler(r, 0, 5);
    for (int x = 0, y = 0, y2 = N, i = 0; x < N; x++, y++, y2--) {
        data[i++] = new double[] { x + gs.sample(), y + gs.sample() };
        data[i++] = new double[] { x + gs.sample(), y2 + gs.sample() };
    }
    // Create 2 images
    final Rectangle bounds = new Rectangle(0, 0, N, N);
    ImageJImagePeakResults i1 = createImage(bounds);
    ImageJImagePeakResults i2 = createImage(bounds);
    final int[] indices = SimpleArrayUtils.natural(data.length);
    PermutationSampler.shuffle(r, indices);
    for (final int i : indices) {
        final ImageJImagePeakResults image = i1;
        i1 = i2;
        i2 = image;
        image.add((float) data[i][0], (float) data[i][1], 1);
    }
    i1.end();
    i2.end();
    final ImageProcessor ip1 = i1.getImagePlus().getProcessor();
    final ImageProcessor ip2 = i2.getImagePlus().getProcessor();
    // Test
    final Frc frc = new Frc();
    FloatProcessor[] fft1;
    FloatProcessor[] fft2;
    fft1 = frc.getComplexFft(ip1);
    fft2 = frc.getComplexFft(ip2);
    final float[] dataA1 = (float[]) fft1[0].getPixels();
    final float[] dataB1 = (float[]) fft1[1].getPixels();
    final float[] dataA2 = (float[]) fft2[0].getPixels();
    final float[] dataB2 = (float[]) fft2[1].getPixels();
    final float[] numerator = new float[dataA1.length];
    final float[] absFft1 = new float[dataA1.length];
    final float[] absFft2 = new float[dataA1.length];
    final TimingService ts = new TimingService(10);
    ts.execute(new MyTimingTask("compute") {

        @Override
        public Object run(Object data) {
            Frc.compute(numerator, absFft1, absFft2, dataA1, dataB1, dataA2, dataB2);
            return null;
        }
    });
    ts.execute(new MyTimingTask("computeMirrored") {

        @Override
        public Object run(Object data) {
            Frc.computeMirrored(N, numerator, absFft1, absFft2, dataA1, dataB1, dataA2, dataB2);
            return null;
        }
    });
    ts.execute(new MyTimingTask("computeMirroredFast") {

        @Override
        public Object run(Object data) {
            Frc.computeMirroredFast(N, numerator, absFft1, absFft2, dataA1, dataB1, dataA2, dataB2);
            return null;
        }
    });
    final int size = ts.getSize();
    ts.repeat(size);
    if (logger.isLoggable(Level.INFO)) {
        logger.info(ts.getReport(size));
    }
    // The 'Fast' method may not always be faster so just log results
    final TimingResult slow = ts.get(-3);
    final TimingResult fast = ts.get(-2);
    final TimingResult fastest = ts.get(-1);
    logger.log(TestLogUtils.getTimingRecord(slow, fastest));
    logger.log(TestLogUtils.getTimingRecord(fast, fastest));
    // It should be faster than the non mirrored version
    Assertions.assertTrue(ts.get(-1).getMean() <= ts.get(-3).getMean());
}
Also used : TimingResult(uk.ac.sussex.gdsc.test.utils.TimingResult) FloatProcessor(ij.process.FloatProcessor) SharedStateContinuousSampler(org.apache.commons.rng.sampling.distribution.SharedStateContinuousSampler) Rectangle(java.awt.Rectangle) ImageJImagePeakResults(uk.ac.sussex.gdsc.smlm.ij.results.ImageJImagePeakResults) ImageProcessor(ij.process.ImageProcessor) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) TimingService(uk.ac.sussex.gdsc.test.utils.TimingService) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Example 5 with RandomSeed

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

the class ImageConverterTest method canGetDataWithFullBounds.

@SeededTest
void canGetDataWithFullBounds(RandomSeed seed) {
    final ImageConverterTestData data = (ImageConverterTestData) dataCache.computeIfAbsent(seed, ImageConverterTest::createData);
    final byte[] bdata = data.bdata;
    final short[] sdata = data.sdata;
    final float[] fdata = data.fdata;
    final Rectangle bounds = new Rectangle(0, 0, w, h);
    final float[] fe = fdata;
    Assertions.assertArrayEquals(fe, ImageJImageConverter.getData(bdata, w, h, bounds, null));
    Assertions.assertArrayEquals(fe, ImageJImageConverter.getData(sdata, w, h, bounds, null));
    Assertions.assertArrayEquals(fe, ImageJImageConverter.getData(fdata, w, h, bounds, null));
    // Check the double format
    final double[] de = SimpleArrayUtils.toDouble(fe);
    Assertions.assertArrayEquals(de, ImageJImageConverter.getDoubleData(bdata, w, h, bounds, null));
    Assertions.assertArrayEquals(de, ImageJImageConverter.getDoubleData(sdata, w, h, bounds, null));
    Assertions.assertArrayEquals(de, ImageJImageConverter.getDoubleData(fdata, w, h, bounds, null));
}
Also used : Rectangle(java.awt.Rectangle) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Aggregations

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