Search in sources :

Example 1 with Random

use of gdsc.core.utils.Random in project GDSC-SMLM by aherbert.

the class PSFEstimator method calculateStatistics.

private boolean calculateStatistics(PeakFit fitter, double[] params, double[] params_dev) {
    debug("  Fitting PSF");
    swapStatistics();
    // Create the fit engine using the PeakFit plugin
    FitConfiguration fitConfig = config.getFitConfiguration();
    fitConfig.setInitialAngle((float) params[0]);
    fitConfig.setInitialPeakStdDev0((float) params[1]);
    fitConfig.setInitialPeakStdDev1((float) params[2]);
    ImageStack stack = imp.getImageStack();
    Rectangle roi = stack.getProcessor(1).getRoi();
    ImageSource source = new IJImageSource(imp);
    // Allow interlaced data by wrapping the image source
    if (interlacedData) {
        source = new InterlacedImageSource(source, dataStart, dataBlock, dataSkip);
    }
    // Allow frame aggregation by wrapping the image source
    if (integrateFrames > 1) {
        source = new AggregatedImageSource(source, integrateFrames);
    }
    fitter.initialiseImage(source, roi, true);
    fitter.addPeakResults(this);
    fitter.initialiseFitting();
    FitEngine engine = fitter.createFitEngine();
    // Use random slices
    int[] slices = new int[stack.getSize()];
    for (int i = 0; i < slices.length; i++) slices[i] = i + 1;
    Random rand = new Random();
    rand.shuffle(slices);
    IJ.showStatus("Fitting ...");
    // Use multi-threaded code for speed
    int i;
    for (i = 0; i < slices.length; i++) {
        int slice = slices[i];
        //debug("  Processing slice = %d\n", slice);
        IJ.showProgress(size(), settings.numberOfPeaks);
        ImageProcessor ip = stack.getProcessor(slice);
        // stack processor does not set the bounds required by ImageConverter
        ip.setRoi(roi);
        FitJob job = new FitJob(slice, ImageConverter.getData(ip), roi);
        engine.run(job);
        if (sampleSizeReached() || Utils.isInterrupted()) {
            break;
        }
    }
    if (Utils.isInterrupted()) {
        IJ.showProgress(1);
        engine.end(true);
        return false;
    }
    // Wait until we have enough results
    while (!sampleSizeReached() && !engine.isQueueEmpty()) {
        IJ.showProgress(size(), settings.numberOfPeaks);
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            break;
        }
    }
    // End now if we have enough samples
    engine.end(sampleSizeReached());
    IJ.showStatus("");
    IJ.showProgress(1);
    // This count will be an over-estimate given that the provider is ahead of the consumer
    // in this multi-threaded system
    debug("  Processed %d/%d slices (%d peaks)", i, slices.length, size());
    setParams(ANGLE, params, params_dev, sampleNew[ANGLE]);
    setParams(X, params, params_dev, sampleNew[X]);
    setParams(Y, params, params_dev, sampleNew[Y]);
    if (settings.showHistograms) {
        int[] idList = new int[NAMES.length];
        int count = 0;
        boolean requireRetile = false;
        for (int ii = 0; ii < 3; ii++) {
            if (sampleNew[ii].getN() == 0)
                continue;
            StoredDataStatistics stats = new StoredDataStatistics(sampleNew[ii].getValues());
            idList[count++] = Utils.showHistogram(TITLE, stats, NAMES[ii], 0, 0, settings.histogramBins, "Mean = " + Utils.rounded(stats.getMean()) + ". Median = " + Utils.rounded(sampleNew[ii].getPercentile(50)));
            requireRetile = requireRetile || Utils.isNewWindow();
        }
        if (requireRetile && count > 0) {
            new WindowOrganiser().tileWindows(Arrays.copyOf(idList, count));
        }
    }
    if (size() < 2) {
        log("ERROR: Insufficient number of fitted peaks, terminating ...");
        return false;
    }
    return true;
}
Also used : InterlacedImageSource(gdsc.smlm.results.InterlacedImageSource) AggregatedImageSource(gdsc.smlm.results.AggregatedImageSource) ImageStack(ij.ImageStack) Rectangle(java.awt.Rectangle) StoredDataStatistics(gdsc.core.utils.StoredDataStatistics) WindowOrganiser(ij.plugin.WindowOrganiser) IJImageSource(gdsc.smlm.ij.IJImageSource) ImageProcessor(ij.process.ImageProcessor) FitEngine(gdsc.smlm.engine.FitEngine) Random(gdsc.core.utils.Random) FitConfiguration(gdsc.smlm.fitting.FitConfiguration) InterlacedImageSource(gdsc.smlm.results.InterlacedImageSource) ImageSource(gdsc.smlm.results.ImageSource) AggregatedImageSource(gdsc.smlm.results.AggregatedImageSource) IJImageSource(gdsc.smlm.ij.IJImageSource) FitJob(gdsc.smlm.engine.FitJob)

Example 2 with Random

use of gdsc.core.utils.Random in project GDSC-SMLM by aherbert.

the class TraceManagerTest method canTraceMultiplePulseWithMovingCoords.

@Test
public void canTraceMultiplePulseWithMovingCoords() {
    Random rand = new Random(30051977);
    float distance = 0.5f;
    int maxOffTime = 5;
    float[] params = createParams(rand);
    Trace trace = new Trace();
    for (int i = 0; i < 5; i++) {
        move(rand, params, distance);
        trace.add(new PeakResult(i, 0, 0, 0, 0, 0, params, null));
    }
    for (int i = 0; i < 5; i++) {
        move(rand, params, distance);
        trace.add(new PeakResult(i + maxOffTime, 0, 0, 0, 0, 0, params, null));
    }
    runTracing(distance, maxOffTime + 1, trace);
}
Also used : Random(gdsc.core.utils.Random) Test(org.junit.Test)

Example 3 with Random

use of gdsc.core.utils.Random in project GDSC-SMLM by aherbert.

the class TraceManagerTest method canTraceSinglePulseWithMovingCoords.

@Test
public void canTraceSinglePulseWithMovingCoords() {
    Random rand = new Random(30051977);
    float distance = 0.5f;
    float[] params = createParams(rand);
    Trace trace = new Trace();
    for (int i = 0; i < 5; i++) {
        move(rand, params, distance);
        trace.add(new PeakResult(i, 0, 0, 0, 0, 0, params, null));
    }
    runTracing(distance, 1, trace);
}
Also used : Random(gdsc.core.utils.Random) Test(org.junit.Test)

Example 4 with Random

use of gdsc.core.utils.Random in project GDSC-SMLM by aherbert.

the class ImageSourceTest method interlacedImageSourceCanReturnDataWithGet.

@Test
public void interlacedImageSourceCanReturnDataWithGet() {
    int w = 5;
    int h = 3;
    float[][] data = createData(w, h, 15);
    int start = 4;
    int size = 2;
    int skip = 1;
    ImageSource source = new InterlacedImageSource(new MemoryImageSource(w, h, data), start, size, skip);
    int[] frames = new int[data.length];
    for (int i = 0; i < data.length; i++) frames[i] = i + 1;
    Random rand = new Random();
    rand.shuffle(frames);
    int[] expected = new int[] { 4, 5, 7, 8, 10, 11, 13, 14 };
    Assert.assertTrue(source.open());
    for (int i = 0; i < data.length; i++) {
        int frame = frames[i];
        Assert.assertTrue(source.isValid(frame));
        float[] d = source.get(frame);
        if (isExpected(frame, expected))
            Assert.assertArrayEquals(data[frame - 1], d, 0);
        else
            Assert.assertNull(d);
    }
    Assert.assertFalse(source.isValid(0));
    Assert.assertFalse(source.isValid(data.length + 1));
}
Also used : Random(gdsc.core.utils.Random) Test(org.junit.Test)

Example 5 with Random

use of gdsc.core.utils.Random in project GDSC-SMLM by aherbert.

the class ImageSourceTest method aggregatedInterlacedImageSourceCanReturnDataWithGet.

@Test
public void aggregatedInterlacedImageSourceCanReturnDataWithGet() {
    int w = 5;
    int h = 3;
    int n = 15;
    float[][] data = createData(w, h, n);
    int aggregate = 3;
    int start = 4;
    int size = 2;
    int skip = 1;
    ImageSource source = new AggregatedImageSource(new InterlacedImageSource(new MemoryImageSource(w, h, data), start, size, skip), aggregate);
    // Set the expected frames returned by the interlacing
    int[] expected = new int[] { 4, 5, 7, 8, 10, 11, 13, 14 };
    // Randomly pick points from the positions used by next()
    int[] frames = new int[source.getFrames()];
    for (int i = 0, ii = 0; ii < expected.length; i++, ii += 3) frames[i] = ii;
    Random rand = new Random();
    rand.shuffle(frames);
    Assert.assertTrue(source.open());
    for (int i = 0; i < frames.length; i++) {
        // Get the range for the data
        int e = frames[i];
        int endE = FastMath.min(e + 2, expected.length - 1);
        int startFrame = expected[e];
        int endFrame = expected[endE];
        // Get the data
        float[] d = source.get(startFrame);
        // Check the correct range is returned
        Assert.assertEquals(startFrame, source.getStartFrameNumber());
        Assert.assertEquals(endFrame, source.getEndFrameNumber());
        // Check the data is collated correctly
        float[] all = new float[data[0].length];
        for (; e <= endE; e++) {
            int frame = expected[e] - 1;
            for (int j = 0; j < all.length; j++) all[j] += data[frame][j];
        }
        Assert.assertArrayEquals(all, d, 0);
    }
    // Check all the data is valid but skipped interlaced points return null 
    for (int i = 0; i < data.length; i++) {
        int frame = i + 1;
        Assert.assertTrue(source.isValid(frame));
        if (!isExpected(frame, expected))
            Assert.assertNull(source.get(frame));
    }
    Assert.assertFalse(source.isValid(0));
    Assert.assertFalse(source.isValid(data.length + 1));
}
Also used : Random(gdsc.core.utils.Random) Test(org.junit.Test)

Aggregations

Random (gdsc.core.utils.Random)13 Test (org.junit.Test)11 Rectangle (java.awt.Rectangle)4 StoredDataStatistics (gdsc.core.utils.StoredDataStatistics)2 FitEngine (gdsc.smlm.engine.FitEngine)1 FitJob (gdsc.smlm.engine.FitJob)1 FitConfiguration (gdsc.smlm.fitting.FitConfiguration)1 IJImageSource (gdsc.smlm.ij.IJImageSource)1 ActivationEnergyImageModel (gdsc.smlm.model.ActivationEnergyImageModel)1 CompoundMoleculeModel (gdsc.smlm.model.CompoundMoleculeModel)1 FixedLifetimeImageModel (gdsc.smlm.model.FixedLifetimeImageModel)1 FluorophoreSequenceModel (gdsc.smlm.model.FluorophoreSequenceModel)1 ImageModel (gdsc.smlm.model.ImageModel)1 LocalisationModel (gdsc.smlm.model.LocalisationModel)1 LocalisationModelSet (gdsc.smlm.model.LocalisationModelSet)1 SpatialDistribution (gdsc.smlm.model.SpatialDistribution)1 SpatialIllumination (gdsc.smlm.model.SpatialIllumination)1 AggregatedImageSource (gdsc.smlm.results.AggregatedImageSource)1 ImageSource (gdsc.smlm.results.ImageSource)1 InterlacedImageSource (gdsc.smlm.results.InterlacedImageSource)1