Search in sources :

Example 1 with FitEngine

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

the class PeakFit method createFitEngine.

/**
 * Creates a fitting engine using the current configuration.
 *
 * @param numberOfThreads the number of threads
 * @param queue the queue
 * @param queueSize the queue size
 * @return The fiting engine
 */
public FitEngine createFitEngine(int numberOfThreads, FitQueue queue, int queueSize) {
    // Ensure thread safety
    final PeakResultsList list = (numberOfThreads > 1) ? results.getThreadSafeList() : results;
    // Reduce to single object for speed
    final PeakResults r = (results.numberOfOutputs() == 1) ? list.toArray()[0] : list;
    // Update the configuration
    if (!updateFitConfiguration(config)) {
        return null;
    }
    final FitEngine engine = FitEngine.create(config, r, numberOfThreads, queue, queueSize);
    // Write settings out to the IJ log
    if (resultsSettings.getLogProgress()) {
        IJ.log(LOG_SPACER);
        IJ.log("Peak Fit");
        IJ.log(LOG_SPACER);
        ImageJUtils.log("Initial Peak SD = %s,%s", MathUtils.rounded(fitConfig.getInitialXSd()), MathUtils.rounded(fitConfig.getInitialYSd()));
        final SpotFilter spotFilter = engine.getSpotFilter();
        IJ.log("Spot Filter = " + spotFilter.getDescription());
        final int w = 2 * engine.getFitting() + 1;
        ImageJUtils.log("Fit window = %d x %d", w, w);
        if (!fitConfig.isDisableSimpleFilter()) {
            IJ.log("Coordinate shift = " + MathUtils.rounded(config.getFitConfiguration().getCoordinateShift()));
            IJ.log("Signal strength = " + MathUtils.rounded(fitConfig.getSignalStrength()));
        }
        if (fitConfig.isDirectFilter()) {
            IJ.log("Smart filter = " + fitConfig.getSmartFilter().getDescription());
        }
        if (extraOptions) {
            IJ.log("Noise = " + MathUtils.rounded(fitConfig.getNoise()));
        }
        IJ.log("Width factor = " + MathUtils.rounded(fitConfig.getMaxWidthFactor()));
        IJ.log(LOG_SPACER);
    }
    return engine;
}
Also used : PeakResultsList(uk.ac.sussex.gdsc.smlm.results.PeakResultsList) FitEngine(uk.ac.sussex.gdsc.smlm.engine.FitEngine) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults) FilePeakResults(uk.ac.sussex.gdsc.smlm.results.FilePeakResults) ImageJTablePeakResults(uk.ac.sussex.gdsc.smlm.ij.results.ImageJTablePeakResults) ImageJImagePeakResults(uk.ac.sussex.gdsc.smlm.ij.results.ImageJImagePeakResults) PeakResults(uk.ac.sussex.gdsc.smlm.results.PeakResults) SpotFilter(uk.ac.sussex.gdsc.smlm.filters.SpotFilter)

Example 2 with FitEngine

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

the class PeakFit method runMaximaFitting.

/**
 * Load the selected results from memory. All multiple frame results are added directly to the
 * results. All single frame results are added to a list of candidate maxima per frame and fitted
 * using the configured parameters.
 */
private void runMaximaFitting() {
    final MemoryPeakResults memoryResults = ResultsManager.loadInputResults(settings.inputOption, false, DistanceUnit.PIXEL);
    if (memoryResults == null || memoryResults.size() == 0) {
        log("No results for maxima fitting");
        return;
    }
    // The total frames (for progress reporting)
    int totalFrames;
    // A function that can convert a frame into a set of candidate indices
    final IntFunction<int[]> frameToMaxIndices;
    // The frames to process (should be sorted ascending)
    Supplier<IntStream> frames;
    // Support fitting all time frames with the same results.
    if (settings.fitAcrossAllFrames) {
        // Check if the input spans multiple frames
        if (getSingleFrame(memoryResults) == 0) {
            final int min = memoryResults.getMinFrame();
            final int max = memoryResults.getMaxFrame();
            final GenericDialog gd = new GenericDialog(TITLE);
            gd.enableYesNoCancel();
            gd.hideCancelButton();
            ImageJUtils.addMessage(gd, "Candidate maxima for fitting span multiple frames (%d-%d).\n \n" + "Please confirm the %s are correct.", min, max, TextUtils.pleural(memoryResults.size(), "candidate"));
            gd.showDialog();
            if (!gd.wasOKed()) {
                return;
            }
        }
        final int[] maxIndices = getMaxIndices(Arrays.asList(memoryResults.toArray()));
        // This may not work correctly if using for example a series image source that
        // incorrectly estimates the number of frames
        totalFrames = source.getFrames();
        frameToMaxIndices = frame -> maxIndices;
        frames = () -> IntStream.rangeClosed(1, totalFrames);
    } else {
        // Build a map between the time-frame and the results in that frame.
        final Map<Integer, List<PeakResult>> map = Arrays.stream(memoryResults.toArray()).parallel().filter(peakResult -> peakResult.getFrame() == peakResult.getEndFrame()).collect(Collectors.groupingBy(PeakResult::getFrame));
        totalFrames = map.size();
        // Build a function that can convert a frame into a set of candidate indices
        frameToMaxIndices = frame -> getMaxIndices(map.get(frame));
        frames = () -> map.keySet().stream().mapToInt(Integer::intValue).sorted();
    }
    final ImageStack stack = (extraSettings.showProcessedFrames) ? new ImageStack(bounds.width, bounds.height) : null;
    // Use the FitEngine to allow multi-threading.
    final FitEngine engine = createFitEngine(getNumberOfThreads(totalFrames));
    if (engine == null) {
        return;
    }
    final int step = ImageJUtils.getProgressInterval(totalFrames);
    // No crop bounds are supported.
    // To pre-process data for noise estimation
    final boolean isFitCameraCounts = fitConfig.isFitCameraCounts();
    final CameraModel cameraModel = fitConfig.getCameraModel();
    runTime = System.nanoTime();
    final AtomicBoolean shutdown = new AtomicBoolean();
    final String format = String.format("Slice: %%d / %d (Results=%%d)", totalFrames);
    frames.get().forEachOrdered(slice -> {
        if (shutdown.get() || escapePressed()) {
            shutdown.set(true);
            return;
        }
        final float[] data = source.get(slice);
        if (data == null) {
            shutdown.set(true);
            return;
        }
        if (slice % step == 0) {
            if (ImageJUtils.showStatus(() -> String.format(format, slice, results.size()))) {
                IJ.showProgress(slice, totalFrames);
            }
        }
        // We must pre-process the data before noise estimation
        final float[] data2 = data.clone();
        if (isFitCameraCounts) {
            cameraModel.removeBias(data2);
        } else {
            cameraModel.removeBiasAndGain(data2);
        }
        final float noise = FitWorker.estimateNoise(data2, source.getWidth(), source.getHeight(), config.getNoiseMethod());
        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(createMaximaFitJob(frameToMaxIndices.apply(slice), source.getStartFrameNumber(), source.getEndFrameNumber(), data, bounds, noise));
    });
    engine.end(shutdown.get());
    time = engine.getTime();
    runTime = System.nanoTime() - runTime;
    if (stack != null) {
        ImageJUtils.display("Processed frames", stack);
    }
    showResults();
    source.close();
}
Also used : Color(java.awt.Color) Choice(java.awt.Choice) Arrays(java.util.Arrays) Calibration(uk.ac.sussex.gdsc.smlm.data.config.CalibrationProtos.Calibration) UnitConverterUtils(uk.ac.sussex.gdsc.smlm.data.config.UnitConverterUtils) ImageProcessor(ij.process.ImageProcessor) ImageSource(uk.ac.sussex.gdsc.smlm.results.ImageSource) Filter(uk.ac.sussex.gdsc.smlm.results.filter.Filter) PSFType(uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.PSFType) StringUtils(org.apache.commons.lang3.StringUtils) ResultsSettings(uk.ac.sussex.gdsc.smlm.data.config.ResultsProtos.ResultsSettings) Panel(java.awt.Panel) Vector(java.util.Vector) Matcher(java.util.regex.Matcher) YesNoCancelDialog(ij.gui.YesNoCancelDialog) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults) Map(java.util.Map) FitProtosHelper(uk.ac.sussex.gdsc.smlm.data.config.FitProtosHelper) ImageJImageConverter(uk.ac.sussex.gdsc.smlm.ij.utils.ImageJImageConverter) FilePeakResults(uk.ac.sussex.gdsc.smlm.results.FilePeakResults) EnumSet(java.util.EnumSet) LutHelper(uk.ac.sussex.gdsc.core.ij.process.LutHelper) InputSource(uk.ac.sussex.gdsc.smlm.ij.plugins.ResultsManager.InputSource) DistanceUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.DistanceUnit) GuiProtosHelper(uk.ac.sussex.gdsc.smlm.ij.settings.GuiProtosHelper) TextUtils(uk.ac.sussex.gdsc.core.utils.TextUtils) Scrollbar(java.awt.Scrollbar) ImagePlus(ij.ImagePlus) CalibrationProtosHelper(uk.ac.sussex.gdsc.smlm.data.config.CalibrationProtosHelper) FitEngineSettings(uk.ac.sussex.gdsc.smlm.data.config.FitProtos.FitEngineSettings) PeakResultProcedureX(uk.ac.sussex.gdsc.smlm.results.procedures.PeakResultProcedureX) Prefs(ij.Prefs) FrameCounter(uk.ac.sussex.gdsc.smlm.results.count.FrameCounter) WindowManager(ij.WindowManager) PeakResult(uk.ac.sussex.gdsc.smlm.results.PeakResult) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) PointRoi(ij.gui.PointRoi) DataFilterMethod(uk.ac.sussex.gdsc.smlm.data.config.FitProtos.DataFilterMethod) GenericDialog(ij.gui.GenericDialog) FitConfiguration(uk.ac.sussex.gdsc.smlm.engine.FitConfiguration) Overlay(ij.gui.Overlay) IntFunction(java.util.function.IntFunction) SeriesOpener(uk.ac.sussex.gdsc.core.ij.SeriesOpener) FitWorker(uk.ac.sussex.gdsc.smlm.engine.FitWorker) FitEngine(uk.ac.sussex.gdsc.smlm.engine.FitEngine) File(java.io.File) AggregatedImageSource(uk.ac.sussex.gdsc.smlm.results.AggregatedImageSource) DirectFilter(uk.ac.sussex.gdsc.smlm.results.filter.DirectFilter) ImageJTablePeakResults(uk.ac.sussex.gdsc.smlm.ij.results.ImageJTablePeakResults) ImageStack(ij.ImageStack) CameraModel(uk.ac.sussex.gdsc.smlm.model.camera.CameraModel) PsfHelper(uk.ac.sussex.gdsc.smlm.data.config.PsfHelper) FitJob(uk.ac.sussex.gdsc.smlm.engine.FitJob) ResultsTableSettings(uk.ac.sussex.gdsc.smlm.data.config.ResultsProtos.ResultsTableSettings) FitTask(uk.ac.sussex.gdsc.smlm.engine.FitParameters.FitTask) ItemListener(java.awt.event.ItemListener) PSFParameter(uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.PSFParameter) FitSolver(uk.ac.sussex.gdsc.smlm.data.config.FitProtos.FitSolver) ResultsImageSettings(uk.ac.sussex.gdsc.smlm.data.config.ResultsProtos.ResultsImageSettings) ImageJPluginLoggerHelper(uk.ac.sussex.gdsc.core.ij.ImageJPluginLoggerHelper) XyResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.XyResultProcedure) InterlacedImageSource(uk.ac.sussex.gdsc.smlm.results.InterlacedImageSource) ImageJImagePeakResults(uk.ac.sussex.gdsc.smlm.ij.results.ImageJImagePeakResults) PeakResults(uk.ac.sussex.gdsc.smlm.results.PeakResults) MathUtils(uk.ac.sussex.gdsc.core.utils.MathUtils) CalibrationWriter(uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter) PlugInFilter(ij.plugin.filter.PlugInFilter) PsfProtosHelper(uk.ac.sussex.gdsc.smlm.data.config.PsfProtosHelper) FitParameters(uk.ac.sussex.gdsc.smlm.engine.FitParameters) SettingsManager(uk.ac.sussex.gdsc.smlm.ij.settings.SettingsManager) ItemEvent(java.awt.event.ItemEvent) CameraType(uk.ac.sussex.gdsc.smlm.data.config.CalibrationProtos.CameraType) ExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog) PeakResultsList(uk.ac.sussex.gdsc.smlm.results.PeakResultsList) TrackProgressAdaptor(uk.ac.sussex.gdsc.core.logging.TrackProgressAdaptor) ResultsImageType(uk.ac.sussex.gdsc.smlm.data.config.ResultsProtos.ResultsImageType) FitEngineConfiguration(uk.ac.sussex.gdsc.smlm.engine.FitEngineConfiguration) OffsetPointRoi(uk.ac.sussex.gdsc.core.ij.gui.OffsetPointRoi) GridBagConstraints(java.awt.GridBagConstraints) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) BitFlagUtils(uk.ac.sussex.gdsc.core.utils.BitFlagUtils) List(java.util.List) SpotFilter(uk.ac.sussex.gdsc.smlm.filters.SpotFilter) LUT(ij.process.LUT) Pattern(java.util.regex.Pattern) FitQueue(uk.ac.sussex.gdsc.smlm.engine.FitQueue) SeriesImageSource(uk.ac.sussex.gdsc.smlm.ij.SeriesImageSource) TypeConverter(uk.ac.sussex.gdsc.core.data.utils.TypeConverter) Roi(ij.gui.Roi) ParameterisedFitJob(uk.ac.sussex.gdsc.smlm.engine.ParameterisedFitJob) IntStream(java.util.stream.IntStream) Rectangle(java.awt.Rectangle) Insets(java.awt.Insets) PrecisionMethod(uk.ac.sussex.gdsc.smlm.data.config.FitProtos.PrecisionMethod) PSFCalculatorSettings(uk.ac.sussex.gdsc.smlm.ij.settings.GUIProtos.PSFCalculatorSettings) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) PSF(uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.PSF) ResultsFileSettings(uk.ac.sussex.gdsc.smlm.data.config.ResultsProtos.ResultsFileSettings) AtomicReference(java.util.concurrent.atomic.AtomicReference) TextField(java.awt.TextField) OptionListener(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog.OptionListener) IJImageSource(uk.ac.sussex.gdsc.smlm.ij.IJImageSource) NoiseEstimatorMethod(uk.ac.sussex.gdsc.smlm.data.config.FitProtos.NoiseEstimatorMethod) ResultsProtosHelper(uk.ac.sussex.gdsc.smlm.data.config.ResultsProtosHelper) TemplateSettings(uk.ac.sussex.gdsc.smlm.data.config.TemplateProtos.TemplateSettings) FastMleSteppingFunctionSolver(uk.ac.sussex.gdsc.smlm.fitting.nonlinear.FastMleSteppingFunctionSolver) SystemColor(java.awt.SystemColor) AstigmatismModel(uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.AstigmatismModel) Iterator(java.util.Iterator) Checkbox(java.awt.Checkbox) Label(java.awt.Label) LutColour(uk.ac.sussex.gdsc.core.ij.process.LutHelper.LutColour) CalibrationReader(uk.ac.sussex.gdsc.smlm.data.config.CalibrationReader) TimeUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.TimeUnit) Counter(uk.ac.sussex.gdsc.smlm.results.count.Counter) ImageJUtils(uk.ac.sussex.gdsc.core.ij.ImageJUtils) IJ(ij.IJ) PerPixelCameraModel(uk.ac.sussex.gdsc.smlm.model.camera.PerPixelCameraModel) CameraModel(uk.ac.sussex.gdsc.smlm.model.camera.CameraModel) PerPixelCameraModel(uk.ac.sussex.gdsc.smlm.model.camera.PerPixelCameraModel) ImageStack(ij.ImageStack) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FitEngine(uk.ac.sussex.gdsc.smlm.engine.FitEngine) GenericDialog(ij.gui.GenericDialog) ExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults) ArrayList(java.util.ArrayList) PeakResultsList(uk.ac.sussex.gdsc.smlm.results.PeakResultsList) List(java.util.List) IntStream(java.util.stream.IntStream)

Example 3 with FitEngine

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

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

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

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