Search in sources :

Example 31 with RandomDataGenerator

use of org.apache.commons.math3.random.RandomDataGenerator in project mastering-java by Kingminghuang.

the class RandomNumberTest method boundedRandomDoubleWithApache.

@Test
public void boundedRandomDoubleWithApache() {
    double leftLimit = 1;
    double rightLimit = 10;
    double generatedDouble1 = new RandomDataGenerator().nextUniform(leftLimit, rightLimit);
    double generatedDouble2 = new RandomDataGenerator().nextUniform(leftLimit, rightLimit);
    assertNotEquals(generatedDouble1, generatedDouble2);
}
Also used : RandomDataGenerator(org.apache.commons.math3.random.RandomDataGenerator) Test(org.junit.Test)

Example 32 with RandomDataGenerator

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

the class PulseActivationAnalysis method simulateActivations.

private int simulateActivations(RandomDataGenerator rdg, BinomialDistribution bd, float[][] molecules, MemoryPeakResults results, int t, double precision, int id) {
    if (bd == null)
        return 0;
    int n = molecules.length;
    int k = bd.sample();
    // Sample
    RandomGenerator rand = rdg.getRandomGenerator();
    int[] sample = Random.sample(k, n, rand);
    while (k-- > 0) {
        float[] xy = molecules[sample[k]];
        float x, y;
        do {
            x = (float) (xy[0] + rand.nextGaussian() * precision);
        } while (outOfBounds(x));
        do {
            y = (float) (xy[1] + rand.nextGaussian() * precision);
        } while (outOfBounds(y));
        results.add(createResult(t, x, y));
    }
    return sample.length;
}
Also used : RandomGenerator(org.apache.commons.math3.random.RandomGenerator)

Example 33 with RandomDataGenerator

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

the class PulseActivationAnalysis method simulateMolecules.

private float[][] simulateMolecules(RandomDataGenerator rdg, int c) {
    int n = sim_nMolecules[c];
    float[][] molecules = new float[n][];
    if (n == 0)
        return molecules;
    // Draw the shapes
    Shape[] shapes = createShapes(rdg, c);
    // Sample positions from within the shapes
    boolean canSample = shapes[0].canSample();
    RandomGenerator rand = rdg.getRandomGenerator();
    while (n-- > 0) {
        float[] coords;
        if (canSample) {
            int next = rand.nextInt(shapes.length);
            coords = shapes[next].sample(rand);
        } else {
            coords = shapes[n % shapes.length].getPosition();
        }
        // Avoid out-of-bounds positions
        if (outOfBounds(coords[0]) || outOfBounds(coords[1]))
            n++;
        else
            molecules[n] = coords;
    }
    return molecules;
}
Also used : RandomGenerator(org.apache.commons.math3.random.RandomGenerator)

Example 34 with RandomDataGenerator

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

the class ImageModel method setRandomGenerator.

/**
	 * Set the random generator for creating the image
	 * 
	 * @param random
	 */
public void setRandomGenerator(RandomGenerator random) {
    if (random == null)
        throw new NullPointerException("Random generator must not be null");
    this.random = random;
    this.randomGenerator = new RandomDataGenerator(random);
}
Also used : RandomDataGenerator(org.apache.commons.math3.random.RandomDataGenerator)

Example 35 with RandomDataGenerator

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

the class FastMLEGradient2ProcedureTest method gradientProcedureLinearIsFasterThanGradientProcedure.

private void gradientProcedureLinearIsFasterThanGradientProcedure(final int nparams) {
    org.junit.Assume.assumeTrue(speedTests || TestSettings.RUN_SPEED_TESTS);
    final int iter = 100;
    rdg = new RandomDataGenerator(new Well19937c(30051977));
    final ArrayList<double[]> paramsList = new ArrayList<double[]>(iter);
    final ArrayList<double[]> yList = new ArrayList<double[]>(iter);
    createData(1, iter, paramsList, yList);
    // Remove the timing of the function call by creating a dummy function
    final Gradient2Function func = new FakeGradientFunction(blockWidth, nparams);
    for (int i = 0; i < paramsList.size(); i++) {
        FastMLEGradient2Procedure p1 = new FastMLEGradient2Procedure(yList.get(i), func);
        p1.computeSecondDerivative(paramsList.get(i));
        p1.computeSecondDerivative(paramsList.get(i));
        FastMLEGradient2Procedure p2 = FastMLEGradient2ProcedureFactory.createUnrolled(yList.get(i), func);
        p2.computeSecondDerivative(paramsList.get(i));
        p2.computeSecondDerivative(paramsList.get(i));
        // Check they are the same
        Assert.assertArrayEquals("D1 " + i, p1.d1, p2.d1, 0);
        Assert.assertArrayEquals("D2 " + i, p1.d2, p2.d2, 0);
    }
    // Realistic loops for an optimisation
    final int loops = 15;
    // Run till stable timing
    Timer t1 = new Timer() {

        @Override
        void run() {
            for (int i = 0, k = 0; i < paramsList.size(); i++) {
                FastMLEGradient2Procedure p1 = new FastMLEGradient2Procedure(yList.get(i), func);
                for (int j = loops; j-- > 0; ) p1.computeSecondDerivative(paramsList.get(k++ % iter));
            }
        }
    };
    long time1 = t1.getTime();
    Timer t2 = new Timer(t1.loops) {

        @Override
        void run() {
            for (int i = 0, k = 0; i < paramsList.size(); i++) {
                FastMLEGradient2Procedure p2 = FastMLEGradient2ProcedureFactory.createUnrolled(yList.get(i), func);
                for (int j = loops; j-- > 0; ) p2.computeSecondDerivative(paramsList.get(k++ % iter));
            }
        }
    };
    long time2 = t2.getTime();
    log("Standard = %d : Unrolled %d = %d : %fx\n", time1, nparams, time2, (1.0 * time1) / time2);
    Assert.assertTrue(time2 < time1 * 1.5);
}
Also used : RandomDataGenerator(org.apache.commons.math3.random.RandomDataGenerator) Gradient2Function(gdsc.smlm.function.Gradient2Function) PrecomputedGradient2Function(gdsc.smlm.function.PrecomputedGradient2Function) ArrayList(java.util.ArrayList) Well19937c(org.apache.commons.math3.random.Well19937c) FakeGradientFunction(gdsc.smlm.function.FakeGradientFunction)

Aggregations

RandomDataGenerator (org.apache.commons.math3.random.RandomDataGenerator)55 Well19937c (org.apache.commons.math3.random.Well19937c)41 ArrayList (java.util.ArrayList)32 FakeGradientFunction (gdsc.smlm.function.FakeGradientFunction)17 Test (org.junit.Test)10 DoubleEquality (gdsc.core.utils.DoubleEquality)6 Gaussian2DFunction (gdsc.smlm.function.gaussian.Gaussian2DFunction)6 RandomGenerator (org.apache.commons.math3.random.RandomGenerator)6 DenseMatrix64F (org.ejml.data.DenseMatrix64F)6 Gradient1Function (gdsc.smlm.function.Gradient1Function)4 ValueProcedure (gdsc.smlm.function.ValueProcedure)4 ErfGaussian2DFunction (gdsc.smlm.function.gaussian.erf.ErfGaussian2DFunction)4 Statistics (gdsc.core.utils.Statistics)3 GradientCalculator (gdsc.smlm.fitting.nonlinear.gradient.GradientCalculator)3 PrecomputedGradient1Function (gdsc.smlm.function.PrecomputedGradient1Function)3 EllipticalGaussian2DFunction (gdsc.smlm.function.gaussian.EllipticalGaussian2DFunction)3 SingleEllipticalGaussian2DFunction (gdsc.smlm.function.gaussian.SingleEllipticalGaussian2DFunction)3 SingleFreeCircularGaussian2DFunction (gdsc.smlm.function.gaussian.SingleFreeCircularGaussian2DFunction)3 SingleFreeCircularErfGaussian2DFunction (gdsc.smlm.function.gaussian.erf.SingleFreeCircularErfGaussian2DFunction)3 MemoryPeakResults (gdsc.smlm.results.MemoryPeakResults)3