Search in sources :

Example 56 with RandomGenerator

use of org.apache.commons.math3.random.RandomGenerator in project GDSC-SMLM by aherbert.

the class FRCTest method canComputeMirrored.

@Test
public void canComputeMirrored() {
    // Sample lines through an image to create a structure.
    int size = 1024;
    double[][] data = new double[size * 2][];
    RandomGenerator r = new Well19937c(30051977);
    for (int x = 0, y = 0, y2 = size, i = 0; x < size; x++, y++, y2--) {
        data[i++] = new double[] { x + r.nextGaussian() * 5, y + r.nextGaussian() * 5 };
        data[i++] = new double[] { x + r.nextGaussian() * 5, y2 + r.nextGaussian() * 5 };
    }
    // Create 2 images
    Rectangle bounds = new Rectangle(0, 0, size, size);
    IJImagePeakResults i1 = createImage(bounds);
    IJImagePeakResults i2 = createImage(bounds);
    int[] indices = Utils.newArray(data.length, 0, 1);
    MathArrays.shuffle(indices, r);
    for (int i : indices) {
        IJImagePeakResults image = i1;
        i1 = i2;
        i2 = image;
        image.add((float) data[i][0], (float) data[i][1], 1);
    }
    i1.end();
    i2.end();
    ImageProcessor ip1 = i1.getImagePlus().getProcessor();
    ImageProcessor ip2 = i2.getImagePlus().getProcessor();
    // Test
    FRC frc = new FRC();
    FloatProcessor[] fft1, fft2;
    fft1 = frc.getComplexFFT(ip1);
    fft2 = frc.getComplexFFT(ip2);
    float[] dataA1 = (float[]) fft1[0].getPixels();
    float[] dataB1 = (float[]) fft1[1].getPixels();
    float[] dataA2 = (float[]) fft2[0].getPixels();
    float[] dataB2 = (float[]) fft2[1].getPixels();
    float[] numeratorE = new float[dataA1.length];
    float[] absFFT1E = new float[dataA1.length];
    float[] absFFT2E = new float[dataA1.length];
    FRC.compute(numeratorE, absFFT1E, absFFT2E, dataA1, dataB1, dataA2, dataB2);
    Assert.assertTrue("numeratorE", FRC.checkSymmetry(numeratorE, size));
    Assert.assertTrue("absFFT1E", FRC.checkSymmetry(absFFT1E, size));
    Assert.assertTrue("absFFT2E", FRC.checkSymmetry(absFFT2E, size));
    float[] numeratorA = new float[dataA1.length];
    float[] absFFT1A = new float[dataA1.length];
    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++)
    //	{
    //		System.out.printf("[%d,%d = %d] %f ?= %f\n", x, y, i, numeratorE[i], numeratorA[i]);
    //	}
    Assert.assertArrayEquals("numerator", numeratorE, numeratorA, 0);
    Assert.assertArrayEquals("absFFT1", absFFT1E, absFFT1A, 0);
    Assert.assertArrayEquals("absFFT2", absFFT2E, absFFT2A, 0);
    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++) {
        Assert.assertEquals("numerator", numeratorE[i], numeratorA[i], 0);
        Assert.assertEquals("absFFT1", absFFT1E[i], absFFT1A[i], 0);
        Assert.assertEquals("absFFT2", absFFT2E[i], absFFT2A[i], 0);
    }
}
Also used : ImageProcessor(ij.process.ImageProcessor) FloatProcessor(ij.process.FloatProcessor) Rectangle(java.awt.Rectangle) Well19937c(org.apache.commons.math3.random.Well19937c) IJImagePeakResults(gdsc.smlm.ij.results.IJImagePeakResults) RandomGenerator(org.apache.commons.math3.random.RandomGenerator) Test(org.junit.Test)

Example 57 with RandomGenerator

use of org.apache.commons.math3.random.RandomGenerator in project GDSC-SMLM by aherbert.

the class FRCTest method computeMirroredIsFaster.

@Test
public void computeMirroredIsFaster() {
    // Sample lines through an image to create a structure.
    final int size = 2048;
    double[][] data = new double[size * 2][];
    RandomGenerator r = new Well19937c(30051977);
    for (int x = 0, y = 0, y2 = size, i = 0; x < size; x++, y++, y2--) {
        data[i++] = new double[] { x + r.nextGaussian() * 5, y + r.nextGaussian() * 5 };
        data[i++] = new double[] { x + r.nextGaussian() * 5, y2 + r.nextGaussian() * 5 };
    }
    // Create 2 images
    Rectangle bounds = new Rectangle(0, 0, size, size);
    IJImagePeakResults i1 = createImage(bounds);
    IJImagePeakResults i2 = createImage(bounds);
    int[] indices = Utils.newArray(data.length, 0, 1);
    MathArrays.shuffle(indices, r);
    for (int i : indices) {
        IJImagePeakResults image = i1;
        i1 = i2;
        i2 = image;
        image.add((float) data[i][0], (float) data[i][1], 1);
    }
    i1.end();
    i2.end();
    ImageProcessor ip1 = i1.getImagePlus().getProcessor();
    ImageProcessor ip2 = i2.getImagePlus().getProcessor();
    // Test
    FRC frc = new FRC();
    FloatProcessor[] fft1, 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];
    TimingService ts = new TimingService(10);
    ts.execute(new MyTimingTask("compute") {

        public Object run(Object data) {
            FRC.compute(numerator, absFFT1, absFFT2, dataA1, dataB1, dataA2, dataB2);
            return null;
        }
    });
    ts.execute(new MyTimingTask("computeMirrored") {

        public Object run(Object data) {
            FRC.computeMirrored(size, numerator, absFFT1, absFFT2, dataA1, dataB1, dataA2, dataB2);
            return null;
        }
    });
    ts.execute(new MyTimingTask("computeMirroredFast") {

        public Object run(Object data) {
            FRC.computeMirroredFast(size, numerator, absFFT1, absFFT2, dataA1, dataB1, dataA2, dataB2);
            return null;
        }
    });
    ts.repeat(ts.getSize());
    ts.report();
}
Also used : FloatProcessor(ij.process.FloatProcessor) Rectangle(java.awt.Rectangle) Well19937c(org.apache.commons.math3.random.Well19937c) IJImagePeakResults(gdsc.smlm.ij.results.IJImagePeakResults) RandomGenerator(org.apache.commons.math3.random.RandomGenerator) ImageProcessor(ij.process.ImageProcessor) TimingService(gdsc.core.test.TimingService) Test(org.junit.Test)

Example 58 with RandomGenerator

use of org.apache.commons.math3.random.RandomGenerator in project GDSC-SMLM by aherbert.

the class PrecomputedFunctionTest method precomputedGradient1FunctionWrapsPrecomputedValues.

@Test
public void precomputedGradient1FunctionWrapsPrecomputedValues() {
    int n = 3;
    RandomGenerator r = new Well19937c(30051977);
    Gradient1Function f0 = new FakeGradientFunction(3, n);
    int size = f0.size();
    double[] b1 = new PseudoRandomGenerator(size, r).getSequence();
    double[] b2 = new PseudoRandomGenerator(size, r).getSequence();
    Gradient1Function f1 = PrecomputedGradient1Function.wrapGradient1Function(f0, b1);
    Gradient1Function f2 = PrecomputedGradient1Function.wrapGradient1Function(f1, b2);
    double[] p = new double[n];
    for (int i = 0; i < n; i++) p[i] = r.nextDouble();
    double[] d0 = new double[n];
    double[] d1 = new double[n];
    double[] d2 = new double[n];
    double[] v0 = evaluateGradient1Function(f0, p, d0);
    double[] v1 = evaluateGradient1Function(f1, p, d1);
    double[] v2 = evaluateGradient1Function(f2, p, d2);
    for (int i = 0; i < v0.length; i++) {
        double e = v0[i] + b1[i] + b2[i];
        double o1 = v1[i] + b2[i];
        double o2 = v2[i];
        Assert.assertEquals("o1", e, o1, 0);
        Assert.assertEquals("o2", e, o2, 1e-6);
    }
    Assert.assertArrayEquals("d1", d0, d1, 0);
    Assert.assertArrayEquals("d2", d0, d2, 0);
}
Also used : Well19937c(org.apache.commons.math3.random.Well19937c) RandomGenerator(org.apache.commons.math3.random.RandomGenerator) PseudoRandomGenerator(gdsc.core.utils.PseudoRandomGenerator) PseudoRandomGenerator(gdsc.core.utils.PseudoRandomGenerator) Test(org.junit.Test)

Example 59 with RandomGenerator

use of org.apache.commons.math3.random.RandomGenerator in project GDSC-SMLM by aherbert.

the class PrecomputedFunctionTest method precomputedValueFunctionWrapsPrecomputedValues.

@Test
public void precomputedValueFunctionWrapsPrecomputedValues() {
    int n = 3;
    RandomGenerator r = new Well19937c(30051977);
    ValueFunction f0 = new FakeGradientFunction(3, n);
    int size = f0.size();
    double[] b1 = new PseudoRandomGenerator(size, r).getSequence();
    double[] b2 = new PseudoRandomGenerator(size, r).getSequence();
    ValueFunction f1 = PrecomputedValueFunction.wrapValueFunction(f0, b1);
    ValueFunction f2 = PrecomputedValueFunction.wrapValueFunction(f1, b2);
    double[] p = new double[n];
    for (int i = 0; i < n; i++) p[i] = r.nextDouble();
    double[] v0 = evaluateValueFunction(f0, p);
    double[] v1 = evaluateValueFunction(f1, p);
    double[] v2 = evaluateValueFunction(f2, p);
    for (int i = 0; i < v0.length; i++) {
        double e = v0[i] + b1[i] + b2[i];
        double o1 = v1[i] + b2[i];
        double o2 = v2[i];
        Assert.assertEquals("o1", e, o1, 0);
        Assert.assertEquals("o2", e, o2, 1e-6);
    }
}
Also used : Well19937c(org.apache.commons.math3.random.Well19937c) RandomGenerator(org.apache.commons.math3.random.RandomGenerator) PseudoRandomGenerator(gdsc.core.utils.PseudoRandomGenerator) PseudoRandomGenerator(gdsc.core.utils.PseudoRandomGenerator) Test(org.junit.Test)

Example 60 with RandomGenerator

use of org.apache.commons.math3.random.RandomGenerator in project GDSC-SMLM by aherbert.

the class PrecomputedFunctionTest method precomputedGradient2FunctionWrapsPrecomputedValues.

@Test
public void precomputedGradient2FunctionWrapsPrecomputedValues() {
    int n = 3;
    RandomGenerator r = new Well19937c(30051977);
    Gradient2Function f0 = new FakeGradientFunction(3, n);
    int size = f0.size();
    double[] b1 = new PseudoRandomGenerator(size, r).getSequence();
    double[] b2 = new PseudoRandomGenerator(size, r).getSequence();
    Gradient2Function f1 = PrecomputedGradient2Function.wrapGradient2Function(f0, b1);
    Gradient2Function f2 = PrecomputedGradient2Function.wrapGradient2Function(f1, b2);
    double[] p = new double[n];
    for (int i = 0; i < n; i++) p[i] = r.nextDouble();
    double[] d0 = new double[n];
    double[] d1 = new double[n];
    double[] d2 = new double[n];
    double[] d20 = new double[n];
    double[] d21 = new double[n];
    double[] d22 = new double[n];
    double[] v0 = evaluateGradient2Function(f0, p, d0, d20);
    double[] v1 = evaluateGradient2Function(f1, p, d1, d21);
    double[] v2 = evaluateGradient2Function(f2, p, d2, d22);
    for (int i = 0; i < v0.length; i++) {
        double e = v0[i] + b1[i] + b2[i];
        double o1 = v1[i] + b2[i];
        double o2 = v2[i];
        Assert.assertEquals("o1", e, o1, 0);
        Assert.assertEquals("o2", e, o2, 1e-6);
    }
    Assert.assertArrayEquals("d1", d0, d1, 0);
    Assert.assertArrayEquals("d2", d0, d2, 0);
    Assert.assertArrayEquals("d21", d20, d21, 0);
    Assert.assertArrayEquals("d22", d20, d22, 0);
}
Also used : Well19937c(org.apache.commons.math3.random.Well19937c) RandomGenerator(org.apache.commons.math3.random.RandomGenerator) PseudoRandomGenerator(gdsc.core.utils.PseudoRandomGenerator) PseudoRandomGenerator(gdsc.core.utils.PseudoRandomGenerator) Test(org.junit.Test)

Aggregations

RandomGenerator (org.apache.commons.math3.random.RandomGenerator)82 Well19937c (org.apache.commons.math3.random.Well19937c)27 Random (java.util.Random)20 Test (org.testng.annotations.Test)18 RandomGeneratorFactory (org.apache.commons.math3.random.RandomGeneratorFactory)16 Assert (org.testng.Assert)16 SimpleInterval (org.broadinstitute.hellbender.utils.SimpleInterval)14 Test (org.junit.Test)14 Collectors (java.util.stream.Collectors)12 IntStream (java.util.stream.IntStream)12 Arrays (java.util.Arrays)10 List (java.util.List)10 Array2DRowRealMatrix (org.apache.commons.math3.linear.Array2DRowRealMatrix)10 ArrayList (java.util.ArrayList)9 NormalDistribution (org.apache.commons.math3.distribution.NormalDistribution)8 ModeledSegment (org.broadinstitute.hellbender.tools.exome.ModeledSegment)8 AllelicCountCollection (org.broadinstitute.hellbender.tools.exome.alleliccount.AllelicCountCollection)8 java.util (java.util)6 GammaDistribution (org.apache.commons.math3.distribution.GammaDistribution)6 ConvergenceException (org.apache.commons.math3.exception.ConvergenceException)6