Search in sources :

Example 6 with PreprocessedPeakResult

use of gdsc.smlm.results.filter.PreprocessedPeakResult 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)

Example 7 with PreprocessedPeakResult

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

the class BenchmarkFilterAnalysis method reportResults.

private ComplexFilterScore reportResults(boolean newResults, List<ComplexFilterScore> filters) {
    if (filters.isEmpty()) {
        IJ.log("Warning: No filters pass the criteria");
        return null;
    }
    getCoordinateStore();
    Collections.sort(filters);
    FractionClassificationResult topFilterClassificationResult = null;
    ArrayList<FractionalAssignment[]> topFilterResults = null;
    String topFilterSummary = null;
    if (showSummaryTable || saveTemplate) {
        createSummaryWindow();
        int n = 0;
        final double range = (summaryDepth / simulationParameters.a) * 0.5;
        int np = 0;
        for (double depth : depthStats) {
            if (Math.abs(depth) < range)
                np++;
        }
        for (ComplexFilterScore fs : filters) {
            final ArrayList<FractionalAssignment[]> list = new ArrayList<FractionalAssignment[]>(resultsList.length);
            final FractionClassificationResult r = scoreFilter(fs.getFilter(), minimalFilter, resultsList, list, coordinateStore);
            final StringBuilder sb = createResult(fs.getFilter(), r);
            if (topFilterResults == null) {
                topFilterResults = list;
                topFilterClassificationResult = r;
            }
            // Show the recall at the specified depth. Sum the distance and signal factor of all scored spots.
            int scored = 0;
            double tp = 0, d = 0, sf = 0, rmsd = 0;
            SimpleRegression regression = new SimpleRegression(false);
            for (FractionalAssignment[] assignments : list) {
                if (assignments == null)
                    continue;
                for (int i = 0; i < assignments.length; i++) {
                    final CustomFractionalAssignment c = (CustomFractionalAssignment) assignments[i];
                    if (Math.abs(c.peak.error) <= range)
                        tp += c.getScore();
                    d += c.d;
                    sf += c.getSignalFactor();
                    rmsd += c.d * c.d;
                    regression.addData(c.peakResult.getSignal(), c.peak.getSignal());
                }
                scored += assignments.length;
            }
            final double slope = regression.getSlope();
            sb.append('\t');
            sb.append(Utils.rounded((double) tp / np)).append('\t');
            sb.append(Utils.rounded(d / scored)).append('\t');
            sb.append(Utils.rounded(sf / scored)).append('\t');
            sb.append(Utils.rounded(Math.sqrt(rmsd / scored))).append('\t');
            sb.append(Utils.rounded(slope)).append('\t');
            if (fs.atLimit() != null)
                sb.append(fs.atLimit());
            String text = sb.toString();
            if (topFilterSummary == null) {
                topFilterSummary = text;
                if (!showSummaryTable)
                    break;
            }
            if (fs.time != 0) {
                sb.append('\t');
                sb.append(fs.algorithm);
                sb.append('\t');
                sb.append(org.apache.commons.lang3.time.DurationFormatUtils.formatDurationHMS(fs.time));
            } else
                sb.append("\t\t");
            if (fs.paramTime != 0) {
                sb.append('\t');
                sb.append(fs.getParamAlgorithm());
                sb.append('\t');
                sb.append(org.apache.commons.lang3.time.DurationFormatUtils.formatDurationHMS(fs.paramTime));
            } else
                sb.append("\t\t");
            text = sb.toString();
            if (isHeadless)
                IJ.log(text);
            else
                summaryWindow.append(text);
            n++;
            if (summaryTopN > 0 && n >= summaryTopN)
                break;
        }
        // Add a spacer to the summary table if we have multiple results
        if (n > 1 && showSummaryTable) {
            if (isHeadless)
                IJ.log("");
            else
                summaryWindow.append("");
        }
    }
    DirectFilter bestFilter = filters.get(0).getFilter();
    if (saveBestFilter)
        saveFilter(bestFilter);
    if (topFilterClassificationResult == null) {
        topFilterResults = new ArrayList<FractionalAssignment[]>(resultsList.length);
        topFilterClassificationResult = scoreFilter(bestFilter, minimalFilter, resultsList, topFilterResults, coordinateStore);
    }
    if (newResults || scores.isEmpty()) {
        scores.add(new FilterResult(failCount, residualsThreshold, duplicateDistance, filters.get(0)));
    }
    if (saveTemplate)
        saveTemplate(topFilterSummary);
    showPlots();
    calculateSensitivity();
    topFilterResults = depthAnalysis(topFilterResults, bestFilter);
    topFilterResults = scoreAnalysis(topFilterResults, bestFilter);
    componentAnalysis(topFilterClassificationResult, filters.get(0));
    PreprocessedPeakResult[] filterResults = null;
    if (isShowOverlay())
        filterResults = showOverlay(topFilterResults, bestFilter);
    saveResults(filterResults, bestFilter);
    wo.tile();
    return filters.get(0);
}
Also used : IDirectFilter(gdsc.smlm.results.filter.IDirectFilter) DirectFilter(gdsc.smlm.results.filter.DirectFilter) ArrayList(java.util.ArrayList) SimpleRegression(org.apache.commons.math3.stat.regression.SimpleRegression) FractionalAssignment(gdsc.core.match.FractionalAssignment) PeakFractionalAssignment(gdsc.smlm.results.filter.PeakFractionalAssignment) FractionClassificationResult(gdsc.core.match.FractionClassificationResult) BasePreprocessedPeakResult(gdsc.smlm.results.filter.BasePreprocessedPeakResult) PreprocessedPeakResult(gdsc.smlm.results.filter.PreprocessedPeakResult)

Aggregations

PreprocessedPeakResult (gdsc.smlm.results.filter.PreprocessedPeakResult)7 BasePreprocessedPeakResult (gdsc.smlm.results.filter.BasePreprocessedPeakResult)6 FractionalAssignment (gdsc.core.match.FractionalAssignment)3 MultiPathFilter (gdsc.smlm.results.filter.MultiPathFilter)3 MultiPathFitResult (gdsc.smlm.results.filter.MultiPathFitResult)3 PeakFractionalAssignment (gdsc.smlm.results.filter.PeakFractionalAssignment)3 BasePoint (gdsc.core.match.BasePoint)2 FractionClassificationResult (gdsc.core.match.FractionClassificationResult)2 MaximaSpotFilter (gdsc.smlm.filters.MaximaSpotFilter)2 PeakResultPoint (gdsc.smlm.ij.plugins.ResultsMatchCalculator.PeakResultPoint)2 FilterSettings (gdsc.smlm.ij.settings.FilterSettings)2 GlobalSettings (gdsc.smlm.ij.settings.GlobalSettings)2 MemoryPeakResults (gdsc.smlm.results.MemoryPeakResults)2 DirectFilter (gdsc.smlm.results.filter.DirectFilter)2 TIntHashSet (gnu.trove.set.hash.TIntHashSet)2 ArrayList (java.util.ArrayList)2 SimpleRegression (org.apache.commons.math3.stat.regression.SimpleRegression)2 ImmutableFractionalAssignment (gdsc.core.match.ImmutableFractionalAssignment)1 FastCorrelator (gdsc.core.utils.FastCorrelator)1 Settings (gdsc.core.utils.Settings)1