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;
}
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);
}
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 };
}
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;
}
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]);
}
Aggregations