use of org.apache.commons.math3.random.RandomGenerator in project GDSC-SMLM by aherbert.
the class ConfigurationTemplateTest method canLoadTemplateImageFromFile.
@Test
public void canLoadTemplateImageFromFile() throws IOException {
ConfigurationTemplate.clearTemplates();
Assert.assertEquals(0, ConfigurationTemplate.getTemplateNamesWithImage().length);
// Create a dummy image
int size = 20;
float[] pixels = new float[size * size];
RandomGenerator r = new Well19937c();
for (int i = pixels.length; i-- > 0; ) pixels[i] = r.nextFloat();
ImagePlus imp = new ImagePlus("test", new FloatProcessor(size, size, pixels));
File tmpFile = File.createTempFile("tmp", ".tif");
tmpFile.deleteOnExit();
IJ.save(imp, tmpFile.getPath());
String name = "canLoadTemplateImageFromFile";
File file = new File(Utils.replaceExtension(tmpFile.getPath(), ".xml"));
ConfigurationTemplate.saveTemplate(name, new GlobalSettings(), file);
Assert.assertEquals(1, ConfigurationTemplate.getTemplateNamesWithImage().length);
ImagePlus imp2 = ConfigurationTemplate.getTemplateImage(name);
Assert.assertNotNull(imp2);
float[] data = (float[]) imp2.getProcessor().toFloat(0, null).getPixels();
Assert.assertArrayEquals(pixels, data, 0);
}
use of org.apache.commons.math3.random.RandomGenerator in project GDSC-SMLM by aherbert.
the class PulseActivationAnalysisTest method canLinearlyUnmix2Channels.
private void canLinearlyUnmix2Channels(int n, int m) {
RandomGenerator r = new Well19937c(30051977);
try {
for (int loop = 0; loop < 10; loop++) {
// A rough mix of each channel
double[] d = create(2, r, 1, 100);
// Crosstalk should be below 50%
double[] c = create(2, r, 0, 0.5);
// Enumerate
Iterator<int[]> it = CombinatoricsUtils.combinationsIterator(2, n);
while (it.hasNext()) {
final int[] channels = it.next();
double[] dd = new double[2];
for (int i : channels) dd[i] = d[i];
Iterator<int[]> it2 = CombinatoricsUtils.combinationsIterator(2, m);
while (it2.hasNext()) {
final int[] crosstalk = it2.next();
double[] cc = new double[2];
for (int i : crosstalk) cc[i] = c[i];
canLinearlyUnmix2Channels(dd[0], dd[1], cc[0], cc[1]);
}
}
}
} catch (AssertionError e) {
throw new AssertionError(String.format("channels=%d, crosstalk=%d", n, m), e);
}
}
use of org.apache.commons.math3.random.RandomGenerator in project GDSC-SMLM by aherbert.
the class SimpleRecombiner method recombine.
private ChromosomePair<T> recombine(Chromosome<T> chromosome1, Chromosome<T> chromosome2, double[] s1, double[] s2) {
int nCrossovers = 1;
if (fraction > 0)
nCrossovers = Math.max(1, (int) random.nextPoisson(fraction * chromosome1.length()));
// Randomly select positions using a partial Fischer-Yates shuffle
int[] positions = new int[s1.length];
for (int i = 0; i < positions.length; i++) positions[i] = i;
RandomGenerator ran = random.getRandomGenerator();
for (int i = positions.length, n = nCrossovers; i-- > 1 && n-- > 0; ) {
int j = (int) (ran.nextInt(i + 1));
int tmp = positions[i];
positions[i] = positions[j];
positions[j] = tmp;
}
// Reverse the array because the end is random
Sort.reverse(positions);
positions = Arrays.copyOf(positions, nCrossovers);
// Get the positions in order
if (nCrossovers != 1)
Arrays.sort(positions);
int nextSwap = 0;
// Create the children by copying the parent, swapping at each crossover position
double[] n1 = new double[s1.length];
double[] n2 = new double[n1.length];
for (int i = 0; i < n1.length; i++) {
if (positions[nextSwap] == i) {
double[] tmp = s1;
s1 = s2;
s2 = tmp;
nextSwap++;
// Avoid index out of bounds
if (nextSwap == nCrossovers)
nextSwap--;
}
n1[i] = s1[i];
n2[i] = s2[i];
}
// Create the new chromosome using the correct parent, i.e.
// If the first swap position was at the start then reverse them.
Chromosome<T> c1 = (positions[0] == 0) ? chromosome2 : chromosome1;
Chromosome<T> c2 = (positions[0] == 0) ? chromosome1 : chromosome2;
c1 = c1.newChromosome(n1);
c2 = c2.newChromosome(n2);
// Ensure the child order is random
return (ran.nextDouble() < 0.5) ? new ChromosomePair<T>(c1, c2) : new ChromosomePair<T>(c2, c1);
}
use of org.apache.commons.math3.random.RandomGenerator in project GDSC-SMLM by aherbert.
the class EMGainAnalysis method simulateFromPoissonGammaGaussian.
/**
* Randomly generate a histogram from poisson-gamma-gaussian samples
*
* @return The histogram
*/
private int[] simulateFromPoissonGammaGaussian() {
// Randomly sample
RandomGenerator random = new Well44497b(System.currentTimeMillis() + System.identityHashCode(this));
PoissonDistribution poisson = new PoissonDistribution(random, _photons, PoissonDistribution.DEFAULT_EPSILON, PoissonDistribution.DEFAULT_MAX_ITERATIONS);
CustomGammaDistribution gamma = new CustomGammaDistribution(random, _photons, _gain, GammaDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
final int steps = simulationSize;
int[] sample = new int[steps];
for (int n = 0; n < steps; n++) {
if (n % 64 == 0)
IJ.showProgress(n, steps);
// Poisson
double d = poisson.sample();
// Gamma
if (d > 0) {
gamma.setShapeUnsafe(d);
d = gamma.sample();
}
// Gaussian
d += _noise * random.nextGaussian();
// Convert the sample to a count
sample[n] = (int) Math.round(d + _bias);
}
int max = Maths.max(sample);
int[] h = new int[max + 1];
for (int s : sample) h[s]++;
return h;
}
use of org.apache.commons.math3.random.RandomGenerator in project GDSC-SMLM by aherbert.
the class EMGainAnalysis method simulateFromPDF.
/**
* Random sample from the cumulative probability distribution function that is used during fitting
*
* @return The histogram
*/
private int[] simulateFromPDF() {
final double[] g = pdf(0, _photons, _gain, _noise, (int) _bias);
// Debug this
double[] x = Utils.newArray(g.length, 0, 1.0);
Utils.display(TITLE + " PDF", new Plot(TITLE + " PDF", "ADU", "P", x, Arrays.copyOf(g, g.length)));
// Get cumulative probability
double sum = 0;
for (int i = 0; i < g.length; i++) {
final double p = g[i];
g[i] += sum;
sum += p;
}
for (int i = 0; i < g.length; i++) g[i] /= sum;
// Ensure value of 1 at the end
g[g.length - 1] = 1;
// Randomly sample
RandomGenerator random = new Well44497b(System.currentTimeMillis() + System.identityHashCode(this));
int[] h = new int[g.length];
final int steps = simulationSize;
for (int n = 0; n < steps; n++) {
if (n % 64 == 0)
IJ.showProgress(n, steps);
final double p = random.nextDouble();
for (int i = 0; i < g.length; i++) if (p <= g[i]) {
h[i]++;
break;
}
}
return h;
}
Aggregations