Search in sources :

Example 76 with MemoryPeakResults

use of gdsc.smlm.results.MemoryPeakResults in project GDSC-SMLM by aherbert.

the class DensityImage method run.

/*
	 * (non-Javadoc)
	 * 
	 * @see ij.plugin.PlugIn#run(java.lang.String)
	 */
public void run(String arg) {
    SMLMUsageTracker.recordPlugin(this.getClass(), arg);
    // Require some fit results and selected regions
    int size = MemoryPeakResults.countMemorySize();
    if (size == 0) {
        IJ.error(TITLE, "There are no fitting results in memory");
        return;
    }
    if (!showDialog())
        return;
    MemoryPeakResults results = ResultsManager.loadInputResults(inputOption, false);
    if (results == null || results.size() == 0) {
        IJ.error(TITLE, "No results could be loaded");
        IJ.showStatus("");
        return;
    }
    boolean[] isWithin = new boolean[1];
    results = cropWithBorder(results, isWithin);
    if (results.size() == 0) {
        IJ.error(TITLE, "No results within the crop region");
        IJ.showStatus("");
        return;
    }
    long start = System.currentTimeMillis();
    IJ.showStatus("Calculating density ...");
    boolean useAdjustment = adjustForBorder && !isWithin[0];
    DensityManager dm = createDensityManager(results);
    int[] density = null;
    if (useSquareApproximation)
        density = dm.calculateSquareDensity(radius, resolution, useAdjustment);
    else
        density = dm.calculateDensity(radius, useAdjustment);
    density = cropBorder(results, density);
    // Convert to float
    ScoreCalculator calc = createCalculator(results);
    float[] densityScore = calc.calculate(density);
    int filtered = plotResults(results, densityScore, calc);
    logDensityResults(results, density, radius, filtered);
    if (computeRipleysPlot)
        computeRipleysPlot(results);
    double seconds = (System.currentTimeMillis() - start) / 1000.0;
    IJ.showStatus(TITLE + " complete : " + seconds + "s");
}
Also used : MemoryPeakResults(gdsc.smlm.results.MemoryPeakResults) DensityManager(gdsc.core.clustering.DensityManager)

Example 77 with MemoryPeakResults

use of gdsc.smlm.results.MemoryPeakResults in project GDSC-SMLM by aherbert.

the class BenchmarkSpotFit method run.

private void run() {
    // Extract all the results in memory into a list per frame. This can be cached
    boolean refresh = false;
    if (lastId != simulationParameters.id) {
        // Do not get integer coordinates
        // The Coordinate objects will be PeakResultPoint objects that store the original PeakResult
        // from the MemoryPeakResults
        actualCoordinates = ResultsMatchCalculator.getCoordinates(results.getResults(), false);
        lastId = simulationParameters.id;
        refresh = true;
    }
    // Extract all the candidates into a list per frame. This can be cached if the settings have not changed
    final int width = (config.isIncludeNeighbours()) ? config.getRelativeFitting() : 0;
    final Settings settings = new Settings(BenchmarkSpotFilter.filterResult.id, fractionPositives, fractionNegativesAfterAllPositives, negativesAfterAllPositives, width);
    if (refresh || !settings.equals(lastSettings)) {
        filterCandidates = subsetFilterResults(BenchmarkSpotFilter.filterResult.filterResults, width);
        lastSettings = settings;
        lastFilterId = BenchmarkSpotFilter.filterResult.id;
    }
    stopWatch = StopWatch.createStarted();
    final ImageStack stack = imp.getImageStack();
    clearFitResults();
    // Save results to memory
    MemoryPeakResults peakResults = new MemoryPeakResults();
    peakResults.copySettings(this.results);
    peakResults.setName(TITLE);
    MemoryPeakResults.addResults(peakResults);
    // Create a pool of workers
    final int nThreads = Prefs.getThreads();
    BlockingQueue<Integer> jobs = new ArrayBlockingQueue<Integer>(nThreads * 2);
    List<Worker> workers = new LinkedList<Worker>();
    List<Thread> threads = new LinkedList<Thread>();
    for (int i = 0; i < nThreads; i++) {
        Worker worker = new Worker(jobs, stack, actualCoordinates, filterCandidates, peakResults);
        Thread t = new Thread(worker);
        workers.add(worker);
        threads.add(t);
        t.start();
    }
    // Fit the frames
    long runTime = System.nanoTime();
    totalProgress = stack.getSize();
    stepProgress = Utils.getProgressInterval(totalProgress);
    progress = 0;
    for (int i = 1; i <= totalProgress; i++) {
        put(jobs, i);
    }
    // Finish all the worker threads by passing in a null job
    for (int i = 0; i < threads.size(); i++) {
        put(jobs, -1);
    }
    // Wait for all to finish
    for (int i = 0; i < threads.size(); i++) {
        try {
            threads.get(i).join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    threads.clear();
    IJ.showProgress(1);
    runTime = System.nanoTime() - runTime;
    if (Utils.isInterrupted()) {
        return;
    }
    stopWatch.stop();
    final String timeString = stopWatch.toString();
    IJ.log("Spot fit time : " + timeString);
    IJ.showStatus("Collecting results ...");
    fitResultsId++;
    fitResults = new TIntObjectHashMap<FilterCandidates>();
    for (Worker w : workers) {
        fitResults.putAll(w.results);
    }
    // Assign a unique ID to each result
    int count = 0;
    // Materialise into an array since we use it twice
    FilterCandidates[] candidates = fitResults.values(new FilterCandidates[fitResults.size()]);
    for (FilterCandidates result : candidates) {
        for (int i = 0; i < result.fitResult.length; i++) {
            final MultiPathFitResult fitResult = result.fitResult[i];
            count += count(fitResult.getSingleFitResult());
            count += count(fitResult.getMultiFitResult());
            count += count(fitResult.getDoubletFitResult());
            count += count(fitResult.getMultiDoubletFitResult());
        }
    }
    PreprocessedPeakResult[] preprocessedPeakResults = new PreprocessedPeakResult[count];
    count = 0;
    for (FilterCandidates result : candidates) {
        for (int i = 0; i < result.fitResult.length; i++) {
            final MultiPathFitResult fitResult = result.fitResult[i];
            count = store(fitResult.getSingleFitResult(), count, preprocessedPeakResults);
            count = store(fitResult.getMultiFitResult(), count, preprocessedPeakResults);
            count = store(fitResult.getDoubletFitResult(), count, preprocessedPeakResults);
            count = store(fitResult.getMultiDoubletFitResult(), count, preprocessedPeakResults);
        }
    }
    summariseResults(fitResults, runTime, preprocessedPeakResults, count);
    IJ.showStatus("");
}
Also used : ImageStack(ij.ImageStack) PeakResultPoint(gdsc.smlm.ij.plugins.ResultsMatchCalculator.PeakResultPoint) BasePoint(gdsc.core.match.BasePoint) LinkedList(java.util.LinkedList) MultiPathFitResult(gdsc.smlm.results.filter.MultiPathFitResult) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) BasePreprocessedPeakResult(gdsc.smlm.results.filter.BasePreprocessedPeakResult) PreprocessedPeakResult(gdsc.smlm.results.filter.PreprocessedPeakResult) FitWorker(gdsc.smlm.engine.FitWorker) MemoryPeakResults(gdsc.smlm.results.MemoryPeakResults) FilterSettings(gdsc.smlm.ij.settings.FilterSettings) Settings(gdsc.core.utils.Settings) GlobalSettings(gdsc.smlm.ij.settings.GlobalSettings)

Example 78 with MemoryPeakResults

use of gdsc.smlm.results.MemoryPeakResults in project GDSC-SMLM by aherbert.

the class BenchmarkFilterAnalysis method saveResults.

/**
	 * Save the results to memory.
	 *
	 * @param filterResults
	 *            The results from running the filter (or null)
	 * @param filter
	 *            the filter
	 */
private void saveResults(PreprocessedPeakResult[] filterResults, DirectFilter filter) {
    MemoryPeakResults results = createResults(filterResults, filter, true);
    MemoryPeakResults.addResults(results);
}
Also used : MemoryPeakResults(gdsc.smlm.results.MemoryPeakResults)

Example 79 with MemoryPeakResults

use of gdsc.smlm.results.MemoryPeakResults in project GDSC-SMLM by aherbert.

the class PSFCreator method fitPSF.

/**
	 * Fit the new PSF image and show a graph of the amplitude/width
	 * 
	 * @param psf
	 * @param loess
	 * @param averageRange
	 * @param fitCom
	 * @return The width of the PSF in the z-centre
	 */
private double fitPSF(ImageStack psf, LoessInterpolator loess, int cz, double averageRange, double[][] fitCom) {
    IJ.showStatus("Fitting final PSF");
    // is not appropriate for a normalised PSF. 
    if (fitConfig.getFitSolver() == FitSolver.MLE) {
        Utils.log("  Maximum Likelihood Estimation (MLE) is not appropriate for final PSF fitting.");
        Utils.log("  Switching to Least Square Estimation");
        fitConfig.setFitSolver(FitSolver.LVM);
        if (interactiveMode) {
            GlobalSettings settings = new GlobalSettings();
            settings.setFitEngineConfiguration(config);
            PeakFit.configureFitSolver(settings, null, false, false);
        }
    }
    // Update the box radius since this is used in the fitSpot method.
    boxRadius = psf.getWidth() / 2;
    int x = boxRadius, y = boxRadius;
    FitConfiguration fitConfig = config.getFitConfiguration();
    final double shift = fitConfig.getCoordinateShiftFactor();
    fitConfig.setInitialPeakStdDev0(fitConfig.getInitialPeakStdDev0() * magnification);
    fitConfig.setInitialPeakStdDev1(fitConfig.getInitialPeakStdDev1() * magnification);
    // Need to be updated after the widths have been set
    fitConfig.setCoordinateShiftFactor(shift);
    fitConfig.setBackgroundFitting(false);
    // Since the PSF will be normalised
    fitConfig.setMinPhotons(0);
    //fitConfig.setLog(new IJLogger());
    MemoryPeakResults results = fitSpot(psf, psf.getWidth(), psf.getHeight(), x, y);
    if (results.size() < 5) {
        Utils.log("  Final PSF: Not enough fit results %d", results.size());
        return 0;
    }
    // Get the results for the spot centre and width
    double[] z = new double[results.size()];
    double[] xCoord = new double[z.length];
    double[] yCoord = new double[z.length];
    double[] sd = new double[z.length];
    double[] a = new double[z.length];
    int i = 0;
    // Set limits for the fit
    final float maxWidth = (float) (FastMath.max(fitConfig.getInitialPeakStdDev0(), fitConfig.getInitialPeakStdDev1()) * magnification * 4);
    // PSF is normalised to 1  
    final float maxSignal = 2;
    for (PeakResult peak : results.getResults()) {
        // Remove bad fits where the width/signal is above the expected
        final float w = FastMath.max(peak.getXSD(), peak.getYSD());
        if (peak.getSignal() > maxSignal || w > maxWidth)
            continue;
        z[i] = peak.getFrame();
        fitCom[0][peak.getFrame() - 1] = xCoord[i] = peak.getXPosition() - x;
        fitCom[1][peak.getFrame() - 1] = yCoord[i] = peak.getYPosition() - y;
        sd[i] = w;
        a[i] = peak.getAmplitude();
        i++;
    }
    // Truncate
    z = Arrays.copyOf(z, i);
    xCoord = Arrays.copyOf(xCoord, i);
    yCoord = Arrays.copyOf(yCoord, i);
    sd = Arrays.copyOf(sd, i);
    a = Arrays.copyOf(a, i);
    // Extract the average smoothed range from the individual fits
    int r = (int) Math.ceil(averageRange / 2);
    int start = 0, stop = z.length - 1;
    for (int j = 0; j < z.length; j++) {
        if (z[j] > cz - r) {
            start = j;
            break;
        }
    }
    for (int j = z.length; j-- > 0; ) {
        if (z[j] < cz + r) {
            stop = j;
            break;
        }
    }
    // Extract xy centre coords and smooth
    double[] smoothX = new double[stop - start + 1];
    double[] smoothY = new double[smoothX.length];
    double[] smoothSd = new double[smoothX.length];
    double[] smoothA = new double[smoothX.length];
    double[] newZ = new double[smoothX.length];
    int smoothCzIndex = 0;
    for (int j = start, k = 0; j <= stop; j++, k++) {
        smoothX[k] = xCoord[j];
        smoothY[k] = yCoord[j];
        smoothSd[k] = sd[j];
        smoothA[k] = a[j];
        newZ[k] = z[j];
        if (newZ[k] == cz)
            smoothCzIndex = k;
    }
    smoothX = loess.smooth(newZ, smoothX);
    smoothY = loess.smooth(newZ, smoothY);
    smoothSd = loess.smooth(newZ, smoothSd);
    smoothA = loess.smooth(newZ, smoothA);
    // Update the widths and positions using the magnification
    final double scale = 1.0 / magnification;
    for (int j = 0; j < xCoord.length; j++) {
        xCoord[j] *= scale;
        yCoord[j] *= scale;
        sd[j] *= scale;
    }
    for (int j = 0; j < smoothX.length; j++) {
        smoothX[j] *= scale;
        smoothY[j] *= scale;
        smoothSd[j] *= scale;
    }
    showPlots(z, a, newZ, smoothA, xCoord, yCoord, sd, newZ, smoothX, smoothY, smoothSd, cz);
    // Store the data for replotting
    this.z = z;
    this.a = a;
    this.smoothAz = newZ;
    this.smoothA = smoothA;
    this.xCoord = xCoord;
    this.yCoord = yCoord;
    this.sd = sd;
    this.newZ = newZ;
    this.smoothX = smoothX;
    this.smoothY = smoothY;
    this.smoothSd = smoothSd;
    //maximumIndex = findMinimumIndex(smoothSd, maximumIndex - start);
    return smoothSd[smoothCzIndex];
}
Also used : FitConfiguration(gdsc.smlm.fitting.FitConfiguration) GlobalSettings(gdsc.smlm.ij.settings.GlobalSettings) MemoryPeakResults(gdsc.smlm.results.MemoryPeakResults) Point(java.awt.Point) BasePoint(gdsc.core.match.BasePoint) PeakResult(gdsc.smlm.results.PeakResult)

Example 80 with MemoryPeakResults

use of gdsc.smlm.results.MemoryPeakResults 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
    MemoryPeakResults results = new MemoryPeakResults();
    results.setSortAfterEnd(true);
    results.begin();
    FitEngine engine = new FitEngine(config, results, Prefs.getThreads(), FitQueue.BLOCKING);
    List<ParameterisedFitJob> jobItems = new ArrayList<ParameterisedFitJob>(stack.getSize());
    for (int slice = 1; slice <= stack.getSize(); slice++) {
        // Extract the region from each frame
        ImageExtractor ie = new ImageExtractor((float[]) stack.getPixels(slice), width, height);
        if (regionBounds == null)
            regionBounds = ie.getBoxRegionBounds(x, y, boxRadius);
        float[] region = ie.crop(regionBounds);
        // Fit only a spot in the centre
        FitParameters params = new FitParameters();
        params.maxIndices = new int[] { boxRadius * regionBounds.width + boxRadius };
        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(gdsc.smlm.engine.FitParameters) FitEngine(gdsc.smlm.engine.FitEngine) ParameterisedFitJob(gdsc.smlm.engine.ParameterisedFitJob) Rectangle(java.awt.Rectangle) ArrayList(java.util.ArrayList) MemoryPeakResults(gdsc.smlm.results.MemoryPeakResults) ImageExtractor(gdsc.core.utils.ImageExtractor) Point(java.awt.Point) BasePoint(gdsc.core.match.BasePoint)

Aggregations

MemoryPeakResults (gdsc.smlm.results.MemoryPeakResults)86 PeakResult (gdsc.smlm.results.PeakResult)40 Rectangle (java.awt.Rectangle)16 ArrayList (java.util.ArrayList)13 ExtendedPeakResult (gdsc.smlm.results.ExtendedPeakResult)10 ImagePlus (ij.ImagePlus)10 StoredDataStatistics (gdsc.core.utils.StoredDataStatistics)8 Statistics (gdsc.core.utils.Statistics)7 IJImageSource (gdsc.smlm.ij.IJImageSource)7 Calibration (gdsc.smlm.results.Calibration)7 ExtendedGenericDialog (ij.gui.ExtendedGenericDialog)7 FractionClassificationResult (gdsc.core.match.FractionClassificationResult)6 IJImagePeakResults (gdsc.smlm.ij.results.IJImagePeakResults)6 Trace (gdsc.smlm.results.Trace)6 LinkedList (java.util.LinkedList)6 BasePoint (gdsc.core.match.BasePoint)5 ImageStack (ij.ImageStack)5 Plot2 (ij.gui.Plot2)5 Point (java.awt.Point)5 ClusterPoint (gdsc.core.clustering.ClusterPoint)4