Search in sources :

Example 41 with RandomGenerator

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

the class GaussianPSFModel method sample.

private double[] sample(final int n, final double mu, final double sigma) {
    final double[] x = new double[n];
    final RandomGenerator random = rand.getRandomGenerator();
    for (int i = 0; i < n; i++) {
        x[i] = sigma * random.nextGaussian() + mu;
    }
    return x;
}
Also used : RandomGenerator(org.apache.commons.math3.random.RandomGenerator)

Example 42 with RandomGenerator

use of org.apache.commons.math3.random.RandomGenerator 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 43 with RandomGenerator

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

the class ImagePSFModel method sample.

private double[][] sample(final int n, double x0, double x1, double x2) {
    final int slice = getSlice(x2);
    if (slice < 0 || slice >= sumImage.length)
        return new double[][] { null, null };
    final double[] sumPsf = cumulativeImage[slice];
    final RandomGenerator randomX, randomY;
    // Use the input generator
    randomX = rand.getRandomGenerator();
    randomY = rand.getRandomGenerator();
    //// Debugging - Use a uniform distribution to sample x
    //randomX = new AbstractRandomGenerator()
    //{
    //	int pos = 0;
    //
    //	@Override
    //	public double nextDouble()
    //	{
    //		double p = (double) pos / n;
    //		if (pos++ >= n)
    //			pos = 0;
    //		return p;
    //	}
    //
    //	@Override
    //	public void setSeed(long seed)
    //	{
    //		pos = Math.abs((int) seed) % n;
    //	}
    //};
    //// Debugging - Use a fixed distribution to sample y
    //randomY = new AbstractRandomGenerator()
    //{
    //	public double nextDouble()
    //	{
    //		return 0.5;
    //	}
    //
    //	@Override
    //	public void setSeed(long seed)
    //	{
    //	}
    //};
    // Ensure the generated index is adjusted to the correct position
    // The index will be generated at 0,0 of a pixel in the PSF image.
    // We must subtract the PSF centre so that the middle coords are zero.
    x0 -= xyCentre[slice][0] * unitsPerPixel;
    x1 -= xyCentre[slice][1] * unitsPerPixel;
    //x0 -= 0.5 * psfWidth * unitsPerPixel;
    //x1 -= 0.5 * psfWidth * unitsPerPixel;
    final double max = sumPsf[sumPsf.length - 1];
    double[] x = new double[n];
    double[] y = new double[n];
    int count = 0;
    double sx = 0, sy = 0, s = 0;
    for (int i = 0; i < n; i++) {
        final double p = randomX.nextDouble();
        // If outside the observed PSF then skip 
        if (p > max)
            continue;
        final int index = findIndex(sumPsf, p);
        // Interpolate xi using the fraction of the pixel
        double xi = index % psfWidth;
        xi += (p - sumPsf[index]) / (sumPsf[index + 1] - sumPsf[index]);
        // Add random dither within pixel for y
        final double yi = randomY.nextDouble() + (index / psfWidth);
        if (COM_CHECK) {
            final double v = 1;
            sx += xi * v;
            sy += yi * v;
            s += v;
        }
        x[count] = x0 + (xi * this.unitsPerPixel);
        y[count] = x1 + (yi * this.unitsPerPixel);
        count++;
    }
    if (COM_CHECK) {
        sx = sx / s - xyCentre[slice][0];
        sy = sy / s - xyCentre[slice][1];
        System.out.printf("%dx%d sample centre [ %f %f ] ( %f %f )\n", psfWidth, psfWidth, sx, sy, sx / unitsPerPixel, sy / unitsPerPixel);
    }
    x = Arrays.copyOf(x, count);
    y = Arrays.copyOf(y, count);
    return new double[][] { x, y };
}
Also used : RandomGenerator(org.apache.commons.math3.random.RandomGenerator)

Example 44 with RandomGenerator

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

the class PoissonGradientProcedureTest method createFakeData.

private double[] createFakeData(double[] params) {
    int n = blockWidth * blockWidth;
    RandomGenerator r = rdg.getRandomGenerator();
    for (int i = 0; i < params.length; i++) {
        params[i] = r.nextDouble();
    }
    double[] y = new double[n];
    for (int i = 0; i < y.length; i++) {
        y[i] = r.nextDouble();
    }
    return y;
}
Also used : RandomGenerator(org.apache.commons.math3.random.RandomGenerator)

Example 45 with RandomGenerator

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

the class BaseFunctionSolverTest method computeSCMOSWeights.

private static void computeSCMOSWeights(double[] weights, double[] noise) {
    // Per observation read noise.
    weights = new double[size * size];
    RandomGenerator randomGenerator = new Well19937c(42);
    ExponentialDistribution ed = new ExponentialDistribution(randomGenerator, variance, ExponentialDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
    for (int i = 0; i < weights.length; i++) {
        double pixelVariance = ed.sample();
        double pixelGain = Math.max(0.1, gain + randomGenerator.nextGaussian() * gainSD);
        // weights = var / g^2
        weights[i] = pixelVariance / (pixelGain * pixelGain);
    }
    // Convert to standard deviation for simulation
    noise = new double[weights.length];
    for (int i = 0; i < weights.length; i++) noise[i] = Math.sqrt(weights[i]);
}
Also used : ExponentialDistribution(org.apache.commons.math3.distribution.ExponentialDistribution) Well19937c(org.apache.commons.math3.random.Well19937c) RandomGenerator(org.apache.commons.math3.random.RandomGenerator)

Aggregations

RandomGenerator (org.apache.commons.math3.random.RandomGenerator)70 Well19937c (org.apache.commons.math3.random.Well19937c)25 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 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 Test (org.junit.Test)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 BinomialDistribution (org.apache.commons.math3.distribution.BinomialDistribution)5