Search in sources :

Example 1 with ParameterisedFitJob

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

the class PeakFit method createMaximaFitJob.

/**
 * Creates the fit job to fit all the given candidate maxima.
 *
 * @param sliceCandidates the slice candidates
 * @param startFrame the start frame
 * @param endFrame the end frame
 * @param data the data
 * @param bounds the bounds
 * @param noise the noise
 * @return the fit job
 */
private static FitJob createMaximaFitJob(int[] maxIndices, int startFrame, int endFrame, float[] data, Rectangle bounds, float noise) {
    final FitParameters fitParams = new FitParameters();
    fitParams.maxIndices = maxIndices;
    if (startFrame != endFrame) {
        fitParams.endT = endFrame;
    }
    fitParams.fitTask = FitTask.PSF_FITTING;
    fitParams.noise = noise;
    return new ParameterisedFitJob(fitParams, startFrame, data, bounds);
}
Also used : FitParameters(uk.ac.sussex.gdsc.smlm.engine.FitParameters) ParameterisedFitJob(uk.ac.sussex.gdsc.smlm.engine.ParameterisedFitJob)

Example 2 with ParameterisedFitJob

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

the class FailCountManager method createData.

/**
 * Creates the fail count data by running fitting on the current image.
 */
private void createData() {
    final ImagePlus imp = WindowManager.getCurrentImage();
    if (imp == null) {
        IJ.error(TITLE, "No image for fitting");
        return;
    }
    if (!showCreateDataDialog(imp)) {
        return;
    }
    // Get the current fit configuration
    final Configuration c = new Configuration();
    if (!c.showDialog(false)) {
        return;
    }
    final FitEngineConfiguration fitConfig = c.getFitEngineConfiguration();
    // Update stopping criteria.
    fitConfig.resetFailCounter();
    fitConfig.setFailuresLimit(settings.getFailCountLimit());
    final ImageSource source = new IJImageSource(imp);
    final PeakFit peakFit = new PeakFit(fitConfig, ResultsSettings.getDefaultInstance());
    peakFit.setResultsSuffix("(FailCountAnalysis)");
    if (!peakFit.initialise(source, null, false)) {
        IJ.error(TITLE, "Failed to initialise the fit engine");
        return;
    }
    final FitEngine engine = peakFit.createFitEngine();
    final Rectangle bounds = new Rectangle(source.getWidth(), source.getHeight());
    // Run
    final int totalFrames = Math.min(source.getFrames(), settings.getMaxFrames());
    final int step = ImageJUtils.getProgressInterval(totalFrames);
    IJ.showProgress(0);
    boolean shutdown = false;
    int slice = 0;
    final LocalList<ParameterisedFitJob> jobs = new LocalList<>(totalFrames);
    while (!shutdown && slice < totalFrames) {
        final float[] data = source.next();
        if (data == null) {
            break;
        }
        if (slice++ % step == 0) {
            final int frames = slice;
            if (ImageJUtils.showStatus(() -> "Fitting slice: " + frames + " / " + totalFrames)) {
                IJ.showProgress(slice, totalFrames);
            }
        }
        final ParameterisedFitJob job = createJob(source.getStartFrameNumber(), data, bounds);
        jobs.push(job);
        engine.run(job);
        shutdown = escapePressed();
    }
    ImageJUtils.showStatus("Extracting fail count data");
    engine.end(shutdown);
    IJ.showProgress(1);
    source.close();
    // Extract the fail count data
    final LocalList<FailCountData> failCountData = new LocalList<>(jobs.size());
    for (int i = 0; i < jobs.size(); i++) {
        final ParameterisedFitJob job = jobs.unsafeGet(i);
        if (job.getStatus() == Status.FINISHED) {
            final FitParameters fitParams = job.getFitParameters();
            // Find the last success
            boolean[] results = fitParams.pass;
            int end = results.length - 1;
            while (end > 0 && !results[end]) {
                end--;
            }
            // Add on the configured fail count limit
            end = Math.min(end + 1 + settings.getFailCountLimit(), results.length);
            results = Arrays.copyOf(results, end);
            failCountData.add(new FailCountData(job.getSlice(), results));
        }
    }
    failCountDataRef.set(failCountData);
    ImageJUtils.showStatus("");
    // Save for the future
    if (settings.getSaveAfterFitting()) {
        saveData();
    }
}
Also used : FitParameters(uk.ac.sussex.gdsc.smlm.engine.FitParameters) ParameterisedFitJob(uk.ac.sussex.gdsc.smlm.engine.ParameterisedFitJob) FitEngineConfiguration(uk.ac.sussex.gdsc.smlm.engine.FitEngineConfiguration) FitEngineConfiguration(uk.ac.sussex.gdsc.smlm.engine.FitEngineConfiguration) Rectangle(java.awt.Rectangle) ImagePlus(ij.ImagePlus) IJImageSource(uk.ac.sussex.gdsc.smlm.ij.IJImageSource) LocalList(uk.ac.sussex.gdsc.core.utils.LocalList) FitEngine(uk.ac.sussex.gdsc.smlm.engine.FitEngine) ImageSource(uk.ac.sussex.gdsc.smlm.results.ImageSource) IJImageSource(uk.ac.sussex.gdsc.smlm.ij.IJImageSource)

Example 3 with ParameterisedFitJob

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

the class AstigmatismModelManager method fitRegion.

private boolean fitRegion() {
    final int radius = config.getFittingWidth();
    if (pluginSettings.getLogFitProgress()) {
        fitConfig.setLog(ImageJPluginLoggerHelper.getLogger(getClass()));
    }
    // Create a fit engine
    results = new MemoryPeakResults();
    results.setCalibration(fitConfig.getCalibration());
    results.setPsf(fitConfig.getPsf());
    results.setSortAfterEnd(true);
    results.begin();
    final int threadCount = Prefs.getThreads();
    final FitEngine engine = FitEngine.create(config, SynchronizedPeakResults.create(results, threadCount), threadCount, FitQueue.BLOCKING);
    final IJImageSource source = new IJImageSource(imp);
    source.open();
    final Rectangle r1 = new Rectangle(cx - radius, cy - radius, 2 * radius + 1, 2 * radius + 1);
    final Rectangle regionBounds = r1.intersection(new Rectangle(source.getWidth(), source.getHeight()));
    // Fit only a spot in the centre
    final int x = cx - regionBounds.x;
    final int y = cy - regionBounds.y;
    final int[] maxIndices = new int[] { y * regionBounds.width + x };
    final Ticker ticker = ImageJUtils.createTicker(source.getFrames(), threadCount);
    IJ.showStatus("Fitting ...");
    boolean shutdown = false;
    while (!shutdown) {
        // Extract the region from each frame
        final float[] region = source.next(regionBounds);
        if (region == null) {
            break;
        }
        final FitParameters params = new FitParameters();
        params.maxIndices = maxIndices.clone();
        final int slice = (int) ticker.getCurrent();
        final ParameterisedFitJob job = new ParameterisedFitJob(slice, params, slice, region, regionBounds);
        engine.run(job);
        ticker.tick();
        shutdown = IJ.escapePressed();
    }
    if (shutdown) {
        IJ.showStatus("Cancelled");
    }
    engine.end(shutdown);
    results.end();
    IJ.showProgress(1);
    if (!shutdown) {
        ImageJUtils.log("Fit %d/%s", results.size(), TextUtils.pleural(source.getFrames(), "spot"));
    }
    return !shutdown;
}
Also used : FitParameters(uk.ac.sussex.gdsc.smlm.engine.FitParameters) IJImageSource(uk.ac.sussex.gdsc.smlm.ij.IJImageSource) FitEngine(uk.ac.sussex.gdsc.smlm.engine.FitEngine) ParameterisedFitJob(uk.ac.sussex.gdsc.smlm.engine.ParameterisedFitJob) Ticker(uk.ac.sussex.gdsc.core.logging.Ticker) Rectangle(java.awt.Rectangle) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults)

Example 4 with ParameterisedFitJob

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

the class FailCountManager method createJob.

private static ParameterisedFitJob createJob(int startFrame, float[] data, Rectangle bounds) {
    final FitParameters fitParams = new FitParameters();
    fitParams.fitTask = FitTask.PSF_FITTING;
    // Signal that the fail count should be recorded
    fitParams.pass = new boolean[0];
    return new ParameterisedFitJob(fitParams, startFrame, data, bounds);
}
Also used : FitParameters(uk.ac.sussex.gdsc.smlm.engine.FitParameters) ParameterisedFitJob(uk.ac.sussex.gdsc.smlm.engine.ParameterisedFitJob)

Example 5 with ParameterisedFitJob

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

the class PsfCreator method fitSpot.

private MemoryPeakResults fitSpot(ImageStack stack, final int width, final int height, final int x, final int y) {
    Rectangle regionBounds = null;
    // Create a fit engine
    final MemoryPeakResults results = new MemoryPeakResults();
    final FitConfiguration fitConfig = config.getFitConfiguration();
    results.setCalibration(fitConfig.getCalibration());
    results.setPsf(fitConfig.getPsf());
    results.setSortAfterEnd(true);
    results.begin();
    final int threadCount = Prefs.getThreads();
    final FitEngine engine = FitEngine.create(config, SynchronizedPeakResults.create(results, threadCount), threadCount, FitQueue.BLOCKING);
    for (int slice = 1; slice <= stack.getSize(); slice++) {
        // Extract the region from each frame
        final ImageExtractor ie = ImageExtractor.wrap((float[]) stack.getPixels(slice), width, height);
        if (regionBounds == null) {
            regionBounds = ie.getBoxRegionBounds(x, y, boxRadius);
        }
        final float[] region = ie.crop(regionBounds);
        // Fit only a spot in the centre
        final FitParameters params = new FitParameters();
        params.maxIndices = new int[] { boxRadius * regionBounds.width + boxRadius };
        final ParameterisedFitJob job = new ParameterisedFitJob(slice, params, slice, region, regionBounds);
        // jobItems.add(job);
        engine.run(job);
    }
    engine.end(false);
    results.end();
    return results;
}
Also used : FitParameters(uk.ac.sussex.gdsc.smlm.engine.FitParameters) FitEngine(uk.ac.sussex.gdsc.smlm.engine.FitEngine) ParameterisedFitJob(uk.ac.sussex.gdsc.smlm.engine.ParameterisedFitJob) FitConfiguration(uk.ac.sussex.gdsc.smlm.engine.FitConfiguration) Rectangle(java.awt.Rectangle) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults) ImageExtractor(uk.ac.sussex.gdsc.core.utils.ImageExtractor) Point(java.awt.Point) BasePoint(uk.ac.sussex.gdsc.core.match.BasePoint)

Aggregations

FitParameters (uk.ac.sussex.gdsc.smlm.engine.FitParameters)6 ParameterisedFitJob (uk.ac.sussex.gdsc.smlm.engine.ParameterisedFitJob)6 Rectangle (java.awt.Rectangle)3 FitEngine (uk.ac.sussex.gdsc.smlm.engine.FitEngine)3 IJImageSource (uk.ac.sussex.gdsc.smlm.ij.IJImageSource)2 MemoryPeakResults (uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults)2 ImagePlus (ij.ImagePlus)1 Point (java.awt.Point)1 Ticker (uk.ac.sussex.gdsc.core.logging.Ticker)1 BasePoint (uk.ac.sussex.gdsc.core.match.BasePoint)1 ImageExtractor (uk.ac.sussex.gdsc.core.utils.ImageExtractor)1 LocalList (uk.ac.sussex.gdsc.core.utils.LocalList)1 FitConfiguration (uk.ac.sussex.gdsc.smlm.engine.FitConfiguration)1 FitEngineConfiguration (uk.ac.sussex.gdsc.smlm.engine.FitEngineConfiguration)1 FitJob (uk.ac.sussex.gdsc.smlm.engine.FitJob)1 ImageSource (uk.ac.sussex.gdsc.smlm.results.ImageSource)1