Search in sources :

Example 1 with BasePreprocessedPeakResult

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

the class BenchmarkSpotFit method store.

private int store(gdsc.smlm.results.filter.MultiPathFitResult.FitResult fitResult, int count, PreprocessedPeakResult[] preprocessedPeakResults) {
    if (fitResult == null || fitResult.results == null)
        return count;
    for (int i = 0; i < fitResult.results.length; i++) {
        BasePreprocessedPeakResult result = (BasePreprocessedPeakResult) fitResult.results[i];
        if (result.isNewResult()) {
            result.uniqueId = count++;
            preprocessedPeakResults[result.uniqueId] = result;
        }
    }
    return count;
}
Also used : BasePreprocessedPeakResult(gdsc.smlm.results.filter.BasePreprocessedPeakResult) PeakResultPoint(gdsc.smlm.ij.plugins.ResultsMatchCalculator.PeakResultPoint) BasePoint(gdsc.core.match.BasePoint)

Example 2 with BasePreprocessedPeakResult

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

the class BenchmarkSpotFit method addToStats.

private void addToStats(gdsc.smlm.results.filter.MultiPathFitResult.FitResult fitResult, StoredDataStatistics[][] stats) {
    if (fitResult == null)
        return;
    final FitResult actualFitResult = (FitResult) fitResult.getData();
    if (fitResult.status != 0) {
        // Add the evaluations for spots that were not OK
        stats[0][FILTER_ITERATIONS].add(actualFitResult.getIterations());
        stats[0][FILTER_EVALUATIONS].add(actualFitResult.getEvaluations());
        return;
    }
    if (fitResult.results == null)
        return;
    boolean isMatch = false;
    for (int resultIndex = 0; resultIndex < fitResult.results.length; resultIndex++) {
        BasePreprocessedPeakResult result = (BasePreprocessedPeakResult) fitResult.results[resultIndex];
        // Q. Only build stats on new results?
        if (!result.isNewResult())
            continue;
        // This was fit - Get statistics
        final double precision = Math.sqrt(result.getLocationVariance());
        final double signal = result.getSignal();
        final double snr = result.getSNR();
        final double width = result.getXSDFactor();
        final double xShift = result.getXRelativeShift2();
        final double yShift = result.getYRelativeShift2();
        // Since these two are combined for filtering and the max is what matters.
        final double shift = (xShift > yShift) ? Math.sqrt(xShift) : Math.sqrt(yShift);
        final double eshift = Math.sqrt(xShift + yShift);
        stats[0][FILTER_SIGNAL].add(signal);
        stats[0][FILTER_SNR].add(snr);
        if (width < 1)
            stats[0][FILTER_MIN_WIDTH].add(width);
        else
            stats[0][FILTER_MAX_WIDTH].add(width);
        stats[0][FILTER_SHIFT].add(shift);
        stats[0][FILTER_ESHIFT].add(eshift);
        stats[0][FILTER_PRECISION].add(precision);
        if (resultIndex == 0) {
            stats[0][FILTER_ITERATIONS].add(actualFitResult.getIterations());
            stats[0][FILTER_EVALUATIONS].add(actualFitResult.getEvaluations());
        }
        // Add to the TP or FP stats 
        // If it has assignments then it was a match to something
        isMatch |= result.hasAssignments();
        final int index = (result.hasAssignments()) ? 1 : 2;
        stats[index][FILTER_SIGNAL].add(signal);
        stats[index][FILTER_SNR].add(snr);
        if (width < 1)
            stats[index][FILTER_MIN_WIDTH].add(width);
        else
            stats[index][FILTER_MAX_WIDTH].add(width);
        stats[index][FILTER_SHIFT].add(shift);
        stats[index][FILTER_ESHIFT].add(eshift);
        stats[index][FILTER_PRECISION].add(precision);
        if (resultIndex == 0) {
        }
    }
    final int index = (isMatch) ? 1 : 2;
    stats[index][FILTER_ITERATIONS].add(actualFitResult.getIterations());
    stats[index][FILTER_EVALUATIONS].add(actualFitResult.getEvaluations());
}
Also used : BasePreprocessedPeakResult(gdsc.smlm.results.filter.BasePreprocessedPeakResult) MultiPathFitResult(gdsc.smlm.results.filter.MultiPathFitResult) FitResult(gdsc.smlm.fitting.FitResult) PeakResultPoint(gdsc.smlm.ij.plugins.ResultsMatchCalculator.PeakResultPoint) BasePoint(gdsc.core.match.BasePoint)

Example 3 with BasePreprocessedPeakResult

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

the class BenchmarkFilterAnalysis method showOverlay.

/**
	 * Show overlay.
	 *
	 * @param allAssignments
	 *            The assignments generated from running the filter (or null)
	 * @param filter
	 *            the filter
	 * @return The results from running the filter (or null)
	 */
private PreprocessedPeakResult[] showOverlay(ArrayList<FractionalAssignment[]> allAssignments, DirectFilter filter) {
    ImagePlus imp = CreateData.getImage();
    if (imp == null)
        return null;
    // Run the filter manually to get the results that pass.
    if (allAssignments == null)
        allAssignments = getAssignments(filter);
    final Overlay o = new Overlay();
    // Do TP
    final TIntHashSet actual = new TIntHashSet();
    final TIntHashSet predicted = new TIntHashSet();
    //int tp = 0, fp = 0, fn = 0;
    for (FractionalAssignment[] assignments : allAssignments) {
        if (assignments == null || assignments.length == 0)
            continue;
        float[] tx = null, ty = null;
        int t = 0;
        //tp += assignments.length;
        if (showTP) {
            tx = new float[assignments.length];
            ty = new float[assignments.length];
        }
        int frame = 0;
        for (int i = 0; i < assignments.length; i++) {
            CustomFractionalAssignment c = (CustomFractionalAssignment) assignments[i];
            IdPeakResult peak = (IdPeakResult) c.peak;
            BasePreprocessedPeakResult spot = (BasePreprocessedPeakResult) c.peakResult;
            actual.add(peak.uniqueId);
            predicted.add(spot.getUniqueId());
            frame = spot.getFrame();
            if (showTP) {
                tx[t] = spot.getX();
                ty[t++] = spot.getY();
            }
        }
        if (showTP)
            SpotFinderPreview.addRoi(frame, o, tx, ty, t, Color.green);
    }
    float[] x = new float[10];
    float[] y = new float[x.length];
    float[] x2 = new float[10];
    float[] y2 = new float[x2.length];
    // Do FP (all remaining results that are not a TP)
    PreprocessedPeakResult[] filterResults = null;
    if (showFP) {
        final MultiPathFilter multiPathFilter = createMPF(filter, minimalFilter);
        //multiPathFilter.setDebugFile("/tmp/filter.txt");
        filterResults = filterResults(multiPathFilter);
        int frame = 0;
        int c = 0;
        int c2 = 0;
        for (int i = 0; i < filterResults.length; i++) {
            if (frame != filterResults[i].getFrame()) {
                if (c != 0)
                    SpotFinderPreview.addRoi(frame, o, x, y, c, Color.red);
                if (c2 != 0)
                    SpotFinderPreview.addRoi(frame, o, x2, y2, c2, Color.magenta);
                c = c2 = 0;
            }
            frame = filterResults[i].getFrame();
            if (predicted.contains(filterResults[i].getUniqueId()))
                continue;
            if (filterResults[i].ignore()) {
                if (x2.length == c2) {
                    x2 = Arrays.copyOf(x2, c2 * 2);
                    y2 = Arrays.copyOf(y2, c2 * 2);
                }
                x2[c2] = filterResults[i].getX();
                y2[c2++] = filterResults[i].getY();
            } else {
                if (x.length == c) {
                    x = Arrays.copyOf(x, c * 2);
                    y = Arrays.copyOf(y, c * 2);
                }
                x[c] = filterResults[i].getX();
                y[c++] = filterResults[i].getY();
            }
        }
        //fp += c;
        if (c != 0)
            SpotFinderPreview.addRoi(frame, o, x, y, c, Color.red);
        if (c2 != 0)
            SpotFinderPreview.addRoi(frame, o, x2, y2, c2, Color.magenta);
    }
    // Do TN (all remaining peaks that have not been matched)
    if (showFN) {
        final boolean checkBorder = (BenchmarkSpotFilter.lastAnalysisBorder != null && BenchmarkSpotFilter.lastAnalysisBorder.x != 0);
        final float border, xlimit, ylimit;
        if (checkBorder) {
            final Rectangle lastAnalysisBorder = BenchmarkSpotFilter.lastAnalysisBorder;
            border = lastAnalysisBorder.x;
            xlimit = lastAnalysisBorder.x + lastAnalysisBorder.width;
            ylimit = lastAnalysisBorder.y + lastAnalysisBorder.height;
        } else
            border = xlimit = ylimit = 0;
        // Add the results to the lists
        actualCoordinates.forEachEntry(new CustomTIntObjectProcedure(x, y, x2, y2) {

            public boolean execute(int frame, IdPeakResult[] results) {
                int c = 0, c2 = 0;
                if (x.length <= results.length) {
                    x = new float[results.length];
                    y = new float[results.length];
                }
                if (x2.length <= results.length) {
                    x2 = new float[results.length];
                    y2 = new float[results.length];
                }
                for (int i = 0; i < results.length; i++) {
                    // Ignore those that were matched by TP
                    if (actual.contains(results[i].uniqueId))
                        continue;
                    if (checkBorder && outsideBorder(results[i], border, xlimit, ylimit)) {
                        x2[c2] = results[i].getXPosition();
                        y2[c2++] = results[i].getYPosition();
                    } else {
                        x[c] = results[i].getXPosition();
                        y[c++] = results[i].getYPosition();
                    }
                }
                //fn += c;
                if (c != 0)
                    SpotFinderPreview.addRoi(frame, o, x, y, c, Color.yellow);
                if (c2 != 0)
                    SpotFinderPreview.addRoi(frame, o, x2, y2, c2, Color.orange);
                return true;
            }
        });
    }
    //System.out.printf("TP=%d, FP=%d, FN=%d, N=%d (%d)\n", tp, fp, fn, tp + fn, results.size());
    imp.setOverlay(o);
    return filterResults;
}
Also used : BasePreprocessedPeakResult(gdsc.smlm.results.filter.BasePreprocessedPeakResult) Rectangle(java.awt.Rectangle) ImagePlus(ij.ImagePlus) TIntHashSet(gnu.trove.set.hash.TIntHashSet) FractionalAssignment(gdsc.core.match.FractionalAssignment) PeakFractionalAssignment(gdsc.smlm.results.filter.PeakFractionalAssignment) BasePreprocessedPeakResult(gdsc.smlm.results.filter.BasePreprocessedPeakResult) PreprocessedPeakResult(gdsc.smlm.results.filter.PreprocessedPeakResult) MultiPathFilter(gdsc.smlm.results.filter.MultiPathFilter) Overlay(ij.gui.Overlay)

Aggregations

BasePreprocessedPeakResult (gdsc.smlm.results.filter.BasePreprocessedPeakResult)3 BasePoint (gdsc.core.match.BasePoint)2 PeakResultPoint (gdsc.smlm.ij.plugins.ResultsMatchCalculator.PeakResultPoint)2 FractionalAssignment (gdsc.core.match.FractionalAssignment)1 FitResult (gdsc.smlm.fitting.FitResult)1 MultiPathFilter (gdsc.smlm.results.filter.MultiPathFilter)1 MultiPathFitResult (gdsc.smlm.results.filter.MultiPathFitResult)1 PeakFractionalAssignment (gdsc.smlm.results.filter.PeakFractionalAssignment)1 PreprocessedPeakResult (gdsc.smlm.results.filter.PreprocessedPeakResult)1 TIntHashSet (gnu.trove.set.hash.TIntHashSet)1 ImagePlus (ij.ImagePlus)1 Overlay (ij.gui.Overlay)1 Rectangle (java.awt.Rectangle)1