Search in sources :

Example 51 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 52 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)

Example 53 with RandomGenerator

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

the class BaseFunctionSolverTest method drawGaussian.

/**
	 * Draw a Gaussian with Poisson shot noise and Gaussian read noise.
	 *
	 * @param params
	 *            The Gaussian parameters
	 * @param noise
	 *            The read noise
	 * @param noiseModel
	 *            the noise model
	 * @return The data
	 */
double[] drawGaussian(double[] params, double[] noise, NoiseModel noiseModel) {
    double[] data = new double[size * size];
    int n = params.length / 6;
    Gaussian2DFunction f = GaussianFunctionFactory.create2D(n, size, size, flags, null);
    f.initialise(params);
    // Poisson noise
    for (int i = 0; i < data.length; i++) {
        CustomPoissonDistribution dist = new CustomPoissonDistribution(randomGenerator, 1);
        double e = f.eval(i);
        if (e > 0) {
            dist.setMeanUnsafe(e);
            data[i] = dist.sample();
        }
    }
    // Simulate EM-gain
    if (noiseModel == NoiseModel.EMCCD) {
        // Use a gamma distribution
        // Since the call random.nextGamma(...) creates a Gamma distribution 
        // which pre-calculates factors only using the scale parameter we 
        // create a custom gamma distribution where the shape can be set as a property.
        CustomGammaDistribution dist = new CustomGammaDistribution(randomGenerator, 1, emGain);
        for (int i = 0; i < data.length; i++) {
            if (data[i] > 0) {
                dist.setShapeUnsafe(data[i]);
                // The sample will amplify the signal so we remap to the original scale
                data[i] = dist.sample() / emGain;
            }
        }
    }
    // Read-noise
    if (noise != null) {
        for (int i = 0; i < data.length; i++) {
            data[i] += randomGenerator.nextGaussian() * noise[i];
        }
    }
    //gdsc.core.ij.Utils.display("Spot", data, size, size);
    return data;
}
Also used : CustomGammaDistribution(org.apache.commons.math3.distribution.CustomGammaDistribution) Gaussian2DFunction(gdsc.smlm.function.gaussian.Gaussian2DFunction) CustomPoissonDistribution(org.apache.commons.math3.distribution.CustomPoissonDistribution)

Example 54 with RandomGenerator

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

the class LSQLVMGradientProcedureTest 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 55 with RandomGenerator

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

the class PulseActivationAnalysis method createShapes.

private Shape[] createShapes(RandomDataGenerator rdg, int c) {
    RandomGenerator rand = rdg.getRandomGenerator();
    Shape[] shapes;
    double min = sim_size / 20;
    double max = sim_size / 10;
    double range = max - min;
    switch(sim_distribution[c]) {
        case CIRCLE:
            shapes = new Shape[10];
            for (int i = 0; i < shapes.length; i++) {
                float x = nextCoordinate(rand);
                float y = nextCoordinate(rand);
                double radius = rand.nextDouble() * range + min;
                shapes[i] = new Circle(x, y, radius);
            }
            break;
        case LINE:
            shapes = new Shape[10];
            for (int i = 0; i < shapes.length; i++) {
                float x = nextCoordinate(rand);
                float y = nextCoordinate(rand);
                double angle = rand.nextDouble() * Math.PI;
                double radius = rand.nextDouble() * range + min;
                shapes[i] = new Line(x, y, angle, radius);
            }
            break;
        case POINT:
        default:
            shapes = new Shape[sim_nMolecules[c]];
            for (int i = 0; i < shapes.length; i++) {
                float x = nextCoordinate(rand);
                float y = nextCoordinate(rand);
                shapes[i] = new Point(x, y);
            }
    }
    return shapes;
}
Also used : RandomGenerator(org.apache.commons.math3.random.RandomGenerator)

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