Search in sources :

Example 1 with RandomDataGenerator

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

the class RandomNumberTest method boundedRandomLongWithApache.

@Test
public void boundedRandomLongWithApache() {
    long leftLimit = 1L;
    long rightLimit = 10L;
    long generatedLong1 = new RandomDataGenerator().nextLong(leftLimit, rightLimit);
    long generatedLong2 = new RandomDataGenerator().nextLong(leftLimit, rightLimit);
    assertNotEquals(generatedLong1, generatedLong2);
}
Also used : RandomDataGenerator(org.apache.commons.math3.random.RandomDataGenerator) Test(org.junit.Test)

Example 2 with RandomDataGenerator

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

the class RandomNumberTest method boundedRandomIntWithApache.

@Test
public void boundedRandomIntWithApache() {
    int leftLimit = 1;
    int rightLimit = 10;
    int generatedInt1 = new RandomDataGenerator().nextInt(leftLimit, rightLimit);
    int generatedInt2 = new RandomDataGenerator().nextInt(leftLimit, rightLimit);
    assertNotEquals(generatedInt1, generatedInt2);
}
Also used : RandomDataGenerator(org.apache.commons.math3.random.RandomDataGenerator) Test(org.junit.Test)

Example 3 with RandomDataGenerator

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

the class PulseActivationAnalysis method showSimulationDialog.

private boolean showSimulationDialog() {
    ExtendedGenericDialog gd = new ExtendedGenericDialog(TITLE);
    SimulationDistribution[] distributionValues = SimulationDistribution.values();
    String[] distribution = SettingsManager.getNames((Object[]) distributionValues);
    // Random crosstalk if not set
    if (Maths.max(ct) == 0) {
        RandomDataGenerator rdg = getRandomDataGenerator();
        for (int i = 0; i < ct.length; i++) // Have some crosstalk
        ct[i] = rdg.nextUniform(0.05, 0.15);
    }
    // Three channel
    for (int c = 0; c < 3; c++) {
        String ch = "_C" + (c + 1);
        gd.addNumericField("Molcules" + ch, sim_nMolecules[c], 0);
        gd.addChoice("Distribution" + ch, distribution, distribution[sim_distribution[c].ordinal()]);
        gd.addNumericField("Precision_" + ch, sim_precision[c], 3);
        gd.addNumericField("Crosstalk_" + ctNames[2 * c], ct[2 * c], 3);
        gd.addNumericField("Crosstalk_" + ctNames[2 * c + 1], ct[2 * c + 1], 3);
    }
    gd.showDialog();
    if (gd.wasCanceled())
        return false;
    int count = 0;
    for (int c = 0; c < 3; c++) {
        sim_nMolecules[c] = (int) Math.abs(gd.getNextNumber());
        if (sim_nMolecules[c] > 0)
            count++;
        sim_distribution[c] = distributionValues[gd.getNextChoiceIndex()];
        sim_precision[c] = Math.abs(gd.getNextNumber());
        ct[2 * c] = Math.abs(gd.getNextNumber());
        ct[2 * c + 1] = Math.abs(gd.getNextNumber());
    }
    if (gd.invalidNumber())
        return false;
    if (count < 2) {
        IJ.error(TITLE, "Simulation requires at least 2 channels");
        return false;
    }
    try {
        for (int i = 0; i < ct.length; i += 2) {
            if (sim_nMolecules[i / 2] > 0)
                validateCrosstalk(i, i + 1);
        }
    } catch (IllegalArgumentException ex) {
        IJ.error(TITLE, ex.getMessage());
        return false;
    }
    return true;
}
Also used : RandomDataGenerator(org.apache.commons.math3.random.RandomDataGenerator) NonBlockingExtendedGenericDialog(ij.gui.NonBlockingExtendedGenericDialog) ExtendedGenericDialog(ij.gui.ExtendedGenericDialog)

Example 4 with RandomDataGenerator

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

the class PulseActivationAnalysis method simulateActivations.

private void simulateActivations(RandomDataGenerator rdg, float[][][] molecules, int c, MemoryPeakResults[] results) {
    int n = molecules[c].length;
    if (n == 0)
        return;
    // Compute desired number per frame
    double umPerPixel = sim_nmPerPixel / 1000;
    double um2PerPixel = umPerPixel * umPerPixel;
    double area = sim_size * sim_size * um2PerPixel;
    double nPerFrame = area * sim_activationDensity;
    // Compute the activation probability (but set an upper limit so not all are on in every frame)
    double p = Math.min(0.5, nPerFrame / n);
    // Determine the other channels activation probability using crosstalk
    double[] p0 = { p, p, p };
    int index1, index2, c1, c2;
    switch(c) {
        case 0:
            index1 = C12;
            index2 = C13;
            c1 = 1;
            c2 = 2;
            break;
        case 1:
            index1 = C21;
            index2 = C23;
            c1 = 0;
            c2 = 2;
            break;
        case 2:
        default:
            index1 = C31;
            index2 = C32;
            c1 = 0;
            c2 = 1;
            break;
    }
    p0[c1] *= ct[index1];
    p0[c2] *= ct[index2];
    // Assume 10 frames after each channel pulse => 30 frames per cycle
    double precision = sim_precision[c] / sim_nmPerPixel;
    int id = c + 1;
    RandomGenerator rand = rdg.getRandomGenerator();
    BinomialDistribution[] bd = new BinomialDistribution[4];
    for (int i = 0; i < 3; i++) bd[i] = createBinomialDistribution(rand, n, p0[i]);
    int[] frames = new int[27];
    for (int i = 1, j = 0; i <= 30; i++) {
        if (i % 10 == 1)
            // Skip specific activation frames 
            continue;
        frames[j++] = i;
    }
    bd[3] = createBinomialDistribution(rand, n, p * sim_nonSpecificFrequency);
    // Count the actual cross talk
    int[] count = new int[3];
    for (int i = 0, t = 1; i < sim_cycles; i++, t += 30) {
        count[0] += simulateActivations(rdg, bd[0], molecules[c], results[c], t, precision, id);
        count[1] += simulateActivations(rdg, bd[1], molecules[c], results[c], t + 10, precision, id);
        count[2] += simulateActivations(rdg, bd[2], molecules[c], results[c], t + 20, precision, id);
        // Add non-specific activations
        if (bd[3] != null) {
            for (int t2 : frames) simulateActivations(rdg, bd[3], molecules[c], results[c], t2, precision, id);
        }
    }
    // Report simulated cross talk
    double[] crosstalk = computeCrosstalk(count, c);
    Utils.log("Simulated crosstalk C%s  %s=>%s, C%s  %s=>%s", ctNames[index1], Utils.rounded(ct[index1]), Utils.rounded(crosstalk[c1]), ctNames[index2], Utils.rounded(ct[index2]), Utils.rounded(crosstalk[c2]));
}
Also used : BinomialDistribution(org.apache.commons.math3.distribution.BinomialDistribution) RandomGenerator(org.apache.commons.math3.random.RandomGenerator)

Example 5 with RandomDataGenerator

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

the class PulseActivationAnalysis method runSimulation.

private void runSimulation() {
    TITLE += " Simulation";
    if (!showSimulationDialog())
        return;
    long start = System.currentTimeMillis();
    RandomDataGenerator rdg = getRandomDataGenerator();
    // Draw the molecule positions
    Utils.showStatus("Simulating molecules ...");
    float[][][] molecules = new float[3][][];
    MemoryPeakResults[] results = new MemoryPeakResults[3];
    Rectangle bounds = new Rectangle(0, 0, sim_size, sim_size);
    for (int c = 0; c < 3; c++) {
        molecules[c] = simulateMolecules(rdg, c);
        // Create a dataset to store the activations
        MemoryPeakResults r = new MemoryPeakResults();
        r.setCalibration(new Calibration(sim_nmPerPixel, 1, 100));
        r.setBounds(bounds);
        r.setName(TITLE + " C" + (c + 1));
        results[c] = r;
    }
    // Simulate activation
    Utils.showStatus("Simulating activations ...");
    for (int c = 0; c < 3; c++) simulateActivations(rdg, molecules, c, results);
    // Combine
    Utils.showStatus("Producing simulation output ...");
    MemoryPeakResults r = new MemoryPeakResults();
    r.setCalibration(new Calibration(sim_nmPerPixel, 1, 100));
    r.setBounds(new Rectangle(0, 0, sim_size, sim_size));
    r.setName(TITLE);
    ImageProcessor[] images = new ImageProcessor[3];
    for (int c = 0; c < 3; c++) {
        ArrayList<PeakResult> list = (ArrayList<PeakResult>) results[c].getResults();
        r.addAllf(list);
        // Draw the unmixed activations
        IJImagePeakResults image = ImagePeakResultsFactory.createPeakResultsImage(ResultsImage.LOCALISATIONS, true, true, TITLE, bounds, sim_nmPerPixel, 1, 1024.0 / sim_size, 0, ResultsMode.ADD);
        image.setLiveImage(false);
        image.setDisplayImage(false);
        image.begin();
        image.addAll(list);
        image.end();
        images[c] = image.getImagePlus().getProcessor();
    }
    displayComposite(images, TITLE);
    // Add to memory. Set the composite dataset first.
    MemoryPeakResults.addResults(r);
    for (int c = 0; c < 3; c++) MemoryPeakResults.addResults(results[c]);
    // TODO:
    // Show an image of what it looks like with no unmixing, i.e. colours allocated 
    // from the frame
    Utils.showStatus("Simulation complete: " + Utils.timeToString(System.currentTimeMillis() - start));
}
Also used : RandomDataGenerator(org.apache.commons.math3.random.RandomDataGenerator) Rectangle(java.awt.Rectangle) ArrayList(java.util.ArrayList) Calibration(gdsc.smlm.results.Calibration) IJImagePeakResults(gdsc.smlm.ij.results.IJImagePeakResults) PeakResult(gdsc.smlm.results.PeakResult) IdPeakResult(gdsc.smlm.results.IdPeakResult) ImageProcessor(ij.process.ImageProcessor) MemoryPeakResults(gdsc.smlm.results.MemoryPeakResults)

Aggregations

RandomDataGenerator (org.apache.commons.math3.random.RandomDataGenerator)53 Well19937c (org.apache.commons.math3.random.Well19937c)41 ArrayList (java.util.ArrayList)31 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)5 PrecomputedGradient1Function (gdsc.smlm.function.PrecomputedGradient1Function)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 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