Search in sources :

Example 6 with FitEngine

use of uk.ac.sussex.gdsc.smlm.engine.FitEngine 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)

Example 7 with FitEngine

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

the class PeakFit method run.

/**
 * Locate the peaks in the configured image source. Results are saved to the configured output.
 *
 * <p>This must be called after initialisation with an image source. Note that each call to this
 * method must be preceded with initialisation to prepare the image and output options.
 */
@SuppressWarnings("null")
public void run() {
    if (source == null) {
        return;
    }
    final int totalFrames = source.getFrames();
    final ImageStack stack = (extraSettings.showProcessedFrames) ? new ImageStack(bounds.width, bounds.height) : null;
    // Do not crop the region from the source if the bounds match the source dimensions
    final Rectangle cropBounds = (bounds.x == 0 && bounds.y == 0 && bounds.width == source.getWidth() && bounds.height == source.getHeight()) ? null : bounds;
    // Use the FitEngine to allow multi-threading.
    final FitEngine engine = createFitEngine(getNumberOfThreads(totalFrames));
    if (engine == null) {
        return;
    }
    final int step = ImageJUtils.getProgressInterval(totalFrames);
    // To pre-process data for noise estimation
    boolean isFitCameraCounts = false;
    CameraModel cameraModel = null;
    if (ignoreBoundsForNoise) {
        isFitCameraCounts = fitConfig.isFitCameraCounts();
        cameraModel = fitConfig.getCameraModel();
    }
    runTime = System.nanoTime();
    boolean shutdown = false;
    int slice = 0;
    final String format = String.format("Slice: %%d / %d (Results=%%d)", totalFrames);
    while (!shutdown) {
        // Noise can optionally be estimated from the entire frame
        float[] data = (ignoreBoundsForNoise) ? source.next() : source.next(cropBounds);
        if (data == null) {
            break;
        }
        if (++slice % step == 0) {
            final int frames = slice;
            if (ImageJUtils.showStatus(() -> String.format(format, frames, results.size()))) {
                IJ.showProgress(slice, totalFrames);
            }
        }
        float noise = Float.NaN;
        if (ignoreBoundsForNoise) {
            // We must pre-process the data before noise estimation
            final float[] data2 = data.clone();
            if (isFitCameraCounts) {
                cameraModel.removeBias(data2);
            } else {
                cameraModel.removeBiasAndGain(data2);
            }
            noise = FitWorker.estimateNoise(data2, source.getWidth(), source.getHeight(), config.getNoiseMethod());
            // Crop the data to the region
            data = ImageJImageConverter.getData(data, source.getWidth(), source.getHeight(), bounds, null);
        }
        if (stack != null) {
            stack.addSlice(String.format("Frame %d - %d", source.getStartFrameNumber(), source.getEndFrameNumber()), data);
        }
        // Get the frame number from the source to allow for interlaced and aggregated data
        engine.run(createJob(source.getStartFrameNumber(), source.getEndFrameNumber(), data, bounds, noise));
        shutdown = escapePressed();
    }
    engine.end(shutdown);
    time = engine.getTime();
    runTime = System.nanoTime() - runTime;
    if (stack != null) {
        ImageJUtils.display("Processed frames", stack);
    }
    showResults();
    source.close();
}
Also used : FitEngine(uk.ac.sussex.gdsc.smlm.engine.FitEngine) CameraModel(uk.ac.sussex.gdsc.smlm.model.camera.CameraModel) PerPixelCameraModel(uk.ac.sussex.gdsc.smlm.model.camera.PerPixelCameraModel) ImageStack(ij.ImageStack) Rectangle(java.awt.Rectangle)

Aggregations

FitEngine (uk.ac.sussex.gdsc.smlm.engine.FitEngine)7 Rectangle (java.awt.Rectangle)6 FitParameters (uk.ac.sussex.gdsc.smlm.engine.FitParameters)4 ParameterisedFitJob (uk.ac.sussex.gdsc.smlm.engine.ParameterisedFitJob)4 IJImageSource (uk.ac.sussex.gdsc.smlm.ij.IJImageSource)4 MemoryPeakResults (uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults)4 ImageStack (ij.ImageStack)3 ImageSource (uk.ac.sussex.gdsc.smlm.results.ImageSource)3 ImagePlus (ij.ImagePlus)2 ImageProcessor (ij.process.ImageProcessor)2 FitConfiguration (uk.ac.sussex.gdsc.smlm.engine.FitConfiguration)2 FitEngineConfiguration (uk.ac.sussex.gdsc.smlm.engine.FitEngineConfiguration)2 SpotFilter (uk.ac.sussex.gdsc.smlm.filters.SpotFilter)2 ImageJImagePeakResults (uk.ac.sussex.gdsc.smlm.ij.results.ImageJImagePeakResults)2 ImageJTablePeakResults (uk.ac.sussex.gdsc.smlm.ij.results.ImageJTablePeakResults)2 FilePeakResults (uk.ac.sussex.gdsc.smlm.results.FilePeakResults)2 PeakResults (uk.ac.sussex.gdsc.smlm.results.PeakResults)2 PeakResultsList (uk.ac.sussex.gdsc.smlm.results.PeakResultsList)2 IJ (ij.IJ)1 Prefs (ij.Prefs)1