Search in sources :

Example 1 with FitJob

use of uk.ac.sussex.gdsc.smlm.engine.FitJob in project GDSC-SMLM by aherbert.

the class PsfEstimator method calculateStatistics.

private boolean calculateStatistics(PeakFit fitter, double[] params, double[] paramsDev) {
    debug("  Fitting PSF");
    swapStatistics();
    // Create the fit engine using the PeakFit plugin
    final FitConfiguration fitConfig = config.getFitConfiguration();
    fitConfig.setInitialPeakStdDev0((float) params[1]);
    try {
        fitConfig.setInitialPeakStdDev1((float) params[2]);
        fitConfig.setInitialAngle((float) Math.toRadians(params[0]));
    } catch (IllegalStateException ex) {
    // Ignore this as the current PSF is not a 2 axis and theta Gaussian PSF
    }
    final ImageStack stack = imp.getImageStack();
    final 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();
    final FitEngine engine = fitter.createFitEngine();
    // Use random slices
    final int[] slices = new int[stack.getSize()];
    for (int i = 0; i < slices.length; i++) {
        slices[i] = i + 1;
    }
    RandomUtils.shuffle(slices, UniformRandomProviders.create());
    IJ.showStatus("Fitting ...");
    // Use multi-threaded code for speed
    int sliceIndex;
    for (sliceIndex = 0; sliceIndex < slices.length; sliceIndex++) {
        final int slice = slices[sliceIndex];
        IJ.showProgress(size(), settings.getNumberOfPeaks());
        final ImageProcessor ip = stack.getProcessor(slice);
        // stack processor does not set the bounds required by ImageConverter
        ip.setRoi(roi);
        final FitJob job = new FitJob(slice, ImageJImageConverter.getData(ip), roi);
        engine.run(job);
        if (sampleSizeReached() || ImageJUtils.isInterrupted()) {
            break;
        }
    }
    if (ImageJUtils.isInterrupted()) {
        IJ.showProgress(1);
        engine.end(true);
        return false;
    }
    // Wait until we have enough results
    while (!sampleSizeReached() && !engine.isQueueEmpty()) {
        IJ.showProgress(size(), settings.getNumberOfPeaks());
        try {
            Thread.sleep(50);
        } catch (final InterruptedException ex) {
            Thread.currentThread().interrupt();
            throw new ConcurrentRuntimeException("Unexpected interruption", ex);
        }
    }
    // End now if we have enough samples
    engine.end(sampleSizeReached());
    ImageJUtils.finished();
    // 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)", sliceIndex, slices.length, size());
    setParams(ANGLE, params, paramsDev, sampleNew[ANGLE]);
    setParams(X, params, paramsDev, sampleNew[X]);
    setParams(Y, params, paramsDev, sampleNew[Y]);
    if (settings.getShowHistograms()) {
        final HistogramPlotBuilder builder = new HistogramPlotBuilder(TITLE).setNumberOfBins(settings.getHistogramBins());
        final WindowOrganiser wo = new WindowOrganiser();
        for (int ii = 0; ii < 3; ii++) {
            if (sampleNew[ii].getN() == 0) {
                continue;
            }
            final StoredDataStatistics stats = StoredDataStatistics.create(sampleNew[ii].getValues());
            builder.setData(stats).setName(NAMES[ii]).setPlotLabel("Mean = " + MathUtils.rounded(stats.getMean()) + ". Median = " + MathUtils.rounded(sampleNew[ii].getPercentile(50))).show(wo);
        }
        wo.tile();
    }
    if (size() < 2) {
        log("ERROR: Insufficient number of fitted peaks, terminating ...");
        return false;
    }
    return true;
}
Also used : InterlacedImageSource(uk.ac.sussex.gdsc.smlm.results.InterlacedImageSource) AggregatedImageSource(uk.ac.sussex.gdsc.smlm.results.AggregatedImageSource) ImageStack(ij.ImageStack) Rectangle(java.awt.Rectangle) StoredDataStatistics(uk.ac.sussex.gdsc.core.utils.StoredDataStatistics) HistogramPlotBuilder(uk.ac.sussex.gdsc.core.ij.HistogramPlot.HistogramPlotBuilder) WindowOrganiser(uk.ac.sussex.gdsc.core.ij.plugin.WindowOrganiser) IJImageSource(uk.ac.sussex.gdsc.smlm.ij.IJImageSource) ImageProcessor(ij.process.ImageProcessor) FitEngine(uk.ac.sussex.gdsc.smlm.engine.FitEngine) ConcurrentRuntimeException(org.apache.commons.lang3.concurrent.ConcurrentRuntimeException) FitConfiguration(uk.ac.sussex.gdsc.smlm.engine.FitConfiguration) ImageSource(uk.ac.sussex.gdsc.smlm.results.ImageSource) IJImageSource(uk.ac.sussex.gdsc.smlm.ij.IJImageSource) InterlacedImageSource(uk.ac.sussex.gdsc.smlm.results.InterlacedImageSource) AggregatedImageSource(uk.ac.sussex.gdsc.smlm.results.AggregatedImageSource) FitJob(uk.ac.sussex.gdsc.smlm.engine.FitJob)

Example 2 with FitJob

use of uk.ac.sussex.gdsc.smlm.engine.FitJob in project GDSC-SMLM by aherbert.

the class PeakFit method createJob.

private FitJob createJob(int startFrame, int endFrame, float[] data, Rectangle bounds, float noise) {
    FitParameters fitParams = null;
    if (startFrame != endFrame) {
        fitParams = new FitParameters();
        fitParams.endT = endFrame;
    }
    if (maximaIdentification) {
        if (fitParams == null) {
            fitParams = new FitParameters();
        }
        fitParams.fitTask = FitTask.MAXIMA_IDENITIFICATION;
        fitParams.noise = noise;
    } else if (!Float.isNaN(noise)) {
        if (fitParams == null) {
            fitParams = new FitParameters();
        }
        fitParams.fitTask = FitTask.PSF_FITTING;
        fitParams.noise = noise;
    }
    if (fitParams != null) {
        return new ParameterisedFitJob(fitParams, startFrame, data, bounds);
    }
    return new FitJob(startFrame, data, bounds);
}
Also used : FitParameters(uk.ac.sussex.gdsc.smlm.engine.FitParameters) ParameterisedFitJob(uk.ac.sussex.gdsc.smlm.engine.ParameterisedFitJob) FitJob(uk.ac.sussex.gdsc.smlm.engine.FitJob) ParameterisedFitJob(uk.ac.sussex.gdsc.smlm.engine.ParameterisedFitJob)

Aggregations

FitJob (uk.ac.sussex.gdsc.smlm.engine.FitJob)2 ImageStack (ij.ImageStack)1 ImageProcessor (ij.process.ImageProcessor)1 Rectangle (java.awt.Rectangle)1 ConcurrentRuntimeException (org.apache.commons.lang3.concurrent.ConcurrentRuntimeException)1 HistogramPlotBuilder (uk.ac.sussex.gdsc.core.ij.HistogramPlot.HistogramPlotBuilder)1 WindowOrganiser (uk.ac.sussex.gdsc.core.ij.plugin.WindowOrganiser)1 StoredDataStatistics (uk.ac.sussex.gdsc.core.utils.StoredDataStatistics)1 FitConfiguration (uk.ac.sussex.gdsc.smlm.engine.FitConfiguration)1 FitEngine (uk.ac.sussex.gdsc.smlm.engine.FitEngine)1 FitParameters (uk.ac.sussex.gdsc.smlm.engine.FitParameters)1 ParameterisedFitJob (uk.ac.sussex.gdsc.smlm.engine.ParameterisedFitJob)1 IJImageSource (uk.ac.sussex.gdsc.smlm.ij.IJImageSource)1 AggregatedImageSource (uk.ac.sussex.gdsc.smlm.results.AggregatedImageSource)1 ImageSource (uk.ac.sussex.gdsc.smlm.results.ImageSource)1 InterlacedImageSource (uk.ac.sussex.gdsc.smlm.results.InterlacedImageSource)1