Search in sources :

Example 31 with PeakResult

use of uk.ac.sussex.gdsc.smlm.results.PeakResult in project GDSC-SMLM by aherbert.

the class CropResults method roiCropResults.

private void roiCropResults() {
    final MemoryPeakResults newResults = createNewResults();
    // These bounds are integer. But this is because the results are meant to come from an image.
    final Rectangle integerBounds = results.getBounds(true);
    final ImagePlus imp = WindowManager.getImage(settings.getRoiImage());
    if (imp == null) {
        IJ.error(TITLE, "No ROI image: " + settings.getRoiImage());
        return;
    }
    final CoordinatePredicate roiTest = CoordinatePredicateUtils.createContainsPredicate(imp.getRoi());
    if (roiTest == null) {
        IJ.error(TITLE, "Not an area ROI");
        return;
    }
    // Scale the results to the size of the image with the ROI
    final int roiImageWidth = imp.getWidth();
    final int roiImageHeight = imp.getHeight();
    final double ox = integerBounds.getX();
    final double oy = integerBounds.getY();
    final double xscale = roiImageWidth / integerBounds.getWidth();
    final double yscale = roiImageHeight / integerBounds.getHeight();
    final Predicate<PeakResult> testZ = getZFilter();
    results.forEach(DistanceUnit.PIXEL, (XyrResultProcedure) (x, y, result) -> {
        if (roiTest.test((x - ox) * xscale, (y - oy) * yscale) && testZ.test(result)) {
            newResults.add(result);
        }
    });
    if (settings.getPreserveBounds()) {
        newResults.setBounds(integerBounds);
    } else {
        newResults.setBounds(null);
        newResults.getBounds(true);
    }
    IJ.showStatus(newResults.size() + " Cropped localisations");
}
Also used : Rectangle(java.awt.Rectangle) Rectangle2D(java.awt.geom.Rectangle2D) PassPeakResultPredicate(uk.ac.sussex.gdsc.smlm.results.predicates.PassPeakResultPredicate) IdentityTypeConverter(uk.ac.sussex.gdsc.core.data.utils.IdentityTypeConverter) WindowManager(ij.WindowManager) PeakResult(uk.ac.sussex.gdsc.smlm.results.PeakResult) OptionListener(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog.OptionListener) CoordinatePredicateUtils(uk.ac.sussex.gdsc.core.ij.roi.CoordinatePredicateUtils) PeakResultValueParameter(uk.ac.sussex.gdsc.smlm.results.PeakResultValueParameter) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults) MathUtils(uk.ac.sussex.gdsc.core.utils.MathUtils) UnitHelper(uk.ac.sussex.gdsc.smlm.data.config.UnitHelper) CropResultsSettings(uk.ac.sussex.gdsc.smlm.ij.settings.GUIProtos.CropResultsSettings) SettingsManager(uk.ac.sussex.gdsc.smlm.ij.settings.SettingsManager) CoordinatePredicate(uk.ac.sussex.gdsc.core.ij.roi.CoordinatePredicate) XyrResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.XyrResultProcedure) ConversionException(uk.ac.sussex.gdsc.core.data.utils.ConversionException) Predicate(java.util.function.Predicate) ExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog) InputSource(uk.ac.sussex.gdsc.smlm.ij.plugins.ResultsManager.InputSource) DistanceUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.DistanceUnit) TextUtils(uk.ac.sussex.gdsc.core.utils.TextUtils) CalibrationHelper(uk.ac.sussex.gdsc.smlm.data.config.CalibrationHelper) Consumer(java.util.function.Consumer) ImagePlus(ij.ImagePlus) ImageJUtils(uk.ac.sussex.gdsc.core.ij.ImageJUtils) IJ(ij.IJ) MinMaxResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.MinMaxResultProcedure) MinMaxPeakResultPredicate(uk.ac.sussex.gdsc.smlm.results.predicates.MinMaxPeakResultPredicate) PlugIn(ij.plugin.PlugIn) TypeConverter(uk.ac.sussex.gdsc.core.data.utils.TypeConverter) LocalList(uk.ac.sussex.gdsc.core.utils.LocalList) Rectangle(java.awt.Rectangle) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults) ImagePlus(ij.ImagePlus) CoordinatePredicate(uk.ac.sussex.gdsc.core.ij.roi.CoordinatePredicate) PeakResult(uk.ac.sussex.gdsc.smlm.results.PeakResult)

Example 32 with PeakResult

use of uk.ac.sussex.gdsc.smlm.results.PeakResult in project GDSC-SMLM by aherbert.

the class ClassificationMatchCalculator method getCoordinates.

/**
 * Build a map between the peak id (time point) and a list of coordinates that pass the filter.
 *
 * @param results the results
 * @param test the test
 * @return the coordinates
 */
public static TIntObjectHashMap<List<PeakResultPoint>> getCoordinates(MemoryPeakResults results, Predicate<PeakResult> test) {
    final TIntObjectHashMap<List<PeakResultPoint>> coords = new TIntObjectHashMap<>();
    if (results.size() > 0) {
        // Do not use HashMap directly to build the coords object since there
        // will be many calls to getEntry(). Instead sort the results and use
        // a new list for each time point
        results.sort();
        // Create list
        final LocalList<PeakResultPoint> tmpCoords = new LocalList<>();
        // Add the results for each frame
        final FrameCounter counter = results.newFrameCounter();
        results.forEach(DistanceUnit.PIXEL, (XyzrResultProcedure) (x, y, z, r) -> {
            if (counter.advance(r.getFrame()) && !tmpCoords.isEmpty()) {
                coords.put(counter.previousFrame(), tmpCoords.copy());
                tmpCoords.clear();
            }
            if (test.test(r)) {
                tmpCoords.add(new PeakResultPoint(r.getFrame(), x, y, z, r));
            }
        });
        if (!tmpCoords.isEmpty()) {
            coords.put(counter.currentFrame(), tmpCoords.copy());
        }
    }
    return coords;
}
Also used : PeakResultPoint(uk.ac.sussex.gdsc.smlm.results.PeakResultPoint) TIntObjectHashMap(gnu.trove.map.hash.TIntObjectHashMap) Arrays(java.util.Arrays) ConfigurationException(uk.ac.sussex.gdsc.smlm.data.config.ConfigurationException) Prefs(ij.Prefs) TextWindow(ij.text.TextWindow) FrameCounter(uk.ac.sussex.gdsc.smlm.results.count.FrameCounter) PeakResult(uk.ac.sussex.gdsc.smlm.results.PeakResult) AtomicReference(java.util.concurrent.atomic.AtomicReference) ConversionException(com.thoughtworks.xstream.converters.ConversionException) TIntProcedure(gnu.trove.procedure.TIntProcedure) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults) MathUtils(uk.ac.sussex.gdsc.core.utils.MathUtils) LinkedList(java.util.LinkedList) SettingsManager(uk.ac.sussex.gdsc.smlm.ij.settings.SettingsManager) RandIndex(uk.ac.sussex.gdsc.core.match.RandIndex) Predicate(java.util.function.Predicate) BufferedTextWindow(uk.ac.sussex.gdsc.core.ij.BufferedTextWindow) ExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog) InputSource(uk.ac.sussex.gdsc.smlm.ij.plugins.ResultsManager.InputSource) ToIntFunction(java.util.function.ToIntFunction) TIntIntHashMap(gnu.trove.map.hash.TIntIntHashMap) NamedObject(uk.ac.sussex.gdsc.smlm.data.NamedObject) DistanceUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.DistanceUnit) Coordinate(uk.ac.sussex.gdsc.core.match.Coordinate) TextUtils(uk.ac.sussex.gdsc.core.utils.TextUtils) TIntHashSet(gnu.trove.set.hash.TIntHashSet) XyzrResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.XyzrResultProcedure) List(java.util.List) PointPair(uk.ac.sussex.gdsc.core.match.PointPair) Resequencer(uk.ac.sussex.gdsc.core.match.Resequencer) ImageJUtils(uk.ac.sussex.gdsc.core.ij.ImageJUtils) IJ(ij.IJ) MatchCalculator(uk.ac.sussex.gdsc.core.match.MatchCalculator) PlugIn(ij.plugin.PlugIn) LocalList(uk.ac.sussex.gdsc.core.utils.LocalList) LocalList(uk.ac.sussex.gdsc.core.utils.LocalList) TIntObjectHashMap(gnu.trove.map.hash.TIntObjectHashMap) FrameCounter(uk.ac.sussex.gdsc.smlm.results.count.FrameCounter) LinkedList(java.util.LinkedList) List(java.util.List) LocalList(uk.ac.sussex.gdsc.core.utils.LocalList) PeakResultPoint(uk.ac.sussex.gdsc.smlm.results.PeakResultPoint)

Example 33 with PeakResult

use of uk.ac.sussex.gdsc.smlm.results.PeakResult in project GDSC-SMLM by aherbert.

the class Filter method filterSubset2.

/**
 * Filter the results.
 *
 * <p>Input PeakResults must be allocated a score for true positive, false positive, true negative
 * and false negative (accessed via the object property get methods). The filter is run and
 * results that pass accumulate scores for true positive and false positive, otherwise the scores
 * are accumulated for true negative and false negative. The simplest scoring scheme is to mark
 * valid results as tp=fn=1 and fp=tn=0 and invalid results the opposite.
 *
 * <p>The number of consecutive rejections are counted per frame. When the configured number of
 * failures is reached all remaining results for the frame are rejected. This assumes the results
 * are ordered by the frame.
 *
 * <p>Note that this method is to be used to score a set of results that may have been extracted
 * from a larger set since the number of consecutive failures before each peak are expected to be
 * stored in the origY property. Set this to zero and the results should be identical to
 * {@link #filterSubset(MemoryPeakResults, double[])}.
 *
 * <p>The number of failures before each peak is stored in the origX property of the PeakResult.
 *
 * @param results the results
 * @param score If not null will be populated with the fraction score [ tp, fp, tn, fn, p, n ]
 * @return the filtered results
 */
public MemoryPeakResults filterSubset2(MemoryPeakResults results, double[] score) {
    final MemoryPeakResults newResults = new MemoryPeakResults();
    final FrameCounter counter = new FrameCounter();
    newResults.copySettings(results);
    setup(results);
    final double[] s = new double[4];
    final Counter p = new Counter();
    results.forEach((PeakResultProcedure) peak -> {
        counter.advanceAndReset(peak.getFrame());
        counter.increment(peak.getOrigY());
        final boolean isPositive = accept(peak);
        if (isPositive) {
            peak.setOrigX(counter.getCount());
            counter.reset();
            newResults.add(peak);
        } else {
            counter.increment();
        }
        if (isPositive) {
            p.increment();
            s[TP] += peak.getTruePositiveScore();
            s[FP] += peak.getFalsePositiveScore();
        } else {
            s[FN] += peak.getFalseNegativeScore();
            s[TN] += peak.getTrueNegativeScore();
        }
    });
    end();
    if (score != null && score.length > 5) {
        score[0] = s[TP];
        score[1] = s[FP];
        score[2] = s[TN];
        score[3] = s[FN];
        score[4] = p.getCount();
        score[5] = (double) results.size() - p.getCount();
    }
    return newResults;
}
Also used : Chromosome(uk.ac.sussex.gdsc.smlm.ga.Chromosome) List(java.util.List) Counter(uk.ac.sussex.gdsc.smlm.results.count.Counter) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults) SimpleArrayUtils(uk.ac.sussex.gdsc.core.utils.SimpleArrayUtils) FrameCounter(uk.ac.sussex.gdsc.smlm.results.count.FrameCounter) Nullable(uk.ac.sussex.gdsc.core.annotation.Nullable) ClassificationResult(uk.ac.sussex.gdsc.core.match.ClassificationResult) PeakResult(uk.ac.sussex.gdsc.smlm.results.PeakResult) PeakResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.PeakResultProcedure) XStreamOmitField(com.thoughtworks.xstream.annotations.XStreamOmitField) FractionClassificationResult(uk.ac.sussex.gdsc.core.match.FractionClassificationResult) Counter(uk.ac.sussex.gdsc.smlm.results.count.Counter) FrameCounter(uk.ac.sussex.gdsc.smlm.results.count.FrameCounter) FrameCounter(uk.ac.sussex.gdsc.smlm.results.count.FrameCounter) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults)

Example 34 with PeakResult

use of uk.ac.sussex.gdsc.smlm.results.PeakResult in project GDSC-SMLM by aherbert.

the class Filter method scoreSubset.

/**
 * Filter the results and return the performance score. Allows benchmarking the filter by marking
 * the results as true or false.
 *
 * <p>Any input PeakResult with an original value that is not zero will be treated as a true
 * result, all other results are false. The filter is run and the results are marked as true
 * positive, false negative and false positive.
 *
 * <p>The number of consecutive rejections are counted per frame. When the configured number of
 * failures is reached all remaining results for the frame are rejected. This assumes the results
 * are ordered by the frame.
 *
 * <p>Note that this method is to be used to score a subset that was generated using
 * {@link #filterSubset(MemoryPeakResults, int, double[])} since the number of consecutive
 * failures before each peak are expected to be stored in the origX property.
 *
 * @param resultsList a list of results to analyse
 * @param failures the number of failures to allow per frame before all peaks are rejected
 * @param tn The initial true negatives (used when the results have been pre-filtered)
 * @param fn The initial false negatives (used when the results have been pre-filtered)
 * @return the score
 */
public ClassificationResult scoreSubset(List<MemoryPeakResults> resultsList, final int failures, int tn, int fn) {
    final int[] s = new int[4];
    s[TN] = tn;
    s[FN] = fn;
    for (final MemoryPeakResults peakResults : resultsList) {
        setup(peakResults);
        final FrameCounter counter = new FrameCounter();
        peakResults.forEach((PeakResultProcedure) peak -> {
            counter.advanceAndReset(peak.getFrame());
            final boolean isTrue = peak.getOrigValue() != 0;
            counter.increment(peak.getOrigX());
            final boolean isPositive;
            if (counter.getCount() > failures) {
                isPositive = false;
            } else {
                isPositive = accept(peak);
            }
            if (isPositive) {
                counter.reset();
            } else {
                counter.increment();
            }
            if (isTrue) {
                if (isPositive) {
                    s[TP]++;
                } else {
                    s[FN]++;
                }
            } else if (isPositive) {
                s[FP]++;
            } else {
                s[TN]++;
            }
        });
        end();
    }
    return new ClassificationResult(s[TP], s[FP], s[TN], s[FN]);
}
Also used : Chromosome(uk.ac.sussex.gdsc.smlm.ga.Chromosome) List(java.util.List) Counter(uk.ac.sussex.gdsc.smlm.results.count.Counter) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults) SimpleArrayUtils(uk.ac.sussex.gdsc.core.utils.SimpleArrayUtils) FrameCounter(uk.ac.sussex.gdsc.smlm.results.count.FrameCounter) Nullable(uk.ac.sussex.gdsc.core.annotation.Nullable) ClassificationResult(uk.ac.sussex.gdsc.core.match.ClassificationResult) PeakResult(uk.ac.sussex.gdsc.smlm.results.PeakResult) PeakResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.PeakResultProcedure) XStreamOmitField(com.thoughtworks.xstream.annotations.XStreamOmitField) FractionClassificationResult(uk.ac.sussex.gdsc.core.match.FractionClassificationResult) FrameCounter(uk.ac.sussex.gdsc.smlm.results.count.FrameCounter) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults) ClassificationResult(uk.ac.sussex.gdsc.core.match.ClassificationResult) FractionClassificationResult(uk.ac.sussex.gdsc.core.match.FractionClassificationResult)

Example 35 with PeakResult

use of uk.ac.sussex.gdsc.smlm.results.PeakResult in project GDSC-SMLM by aherbert.

the class HysteresisFilter method getSearchDistanceUsingCandidates.

/**
 * Find average precision of the candidates and use it for the search distance.
 *
 * @param peakResults the peak results
 * @param candidates the candidates
 * @return the search distance using candidates
 */
private double getSearchDistanceUsingCandidates(MemoryPeakResults peakResults, LinkedList<PeakResult> candidates) {
    final Gaussian2DPeakResultCalculator calculator = Gaussian2DPeakResultHelper.create(peakResults.getPsf(), peakResults.getCalibration(), Gaussian2DPeakResultHelper.LSE_PRECISION);
    double sum = 0;
    for (final PeakResult peakResult : candidates) {
        sum += calculator.getLsePrecision(peakResult.getParameters(), peakResult.getNoise());
    }
    final double nmPerPixel = peakResults.getNmPerPixel();
    return (sum / candidates.size()) * searchDistance / nmPerPixel;
}
Also used : Gaussian2DPeakResultCalculator(uk.ac.sussex.gdsc.smlm.results.Gaussian2DPeakResultCalculator) PeakResult(uk.ac.sussex.gdsc.smlm.results.PeakResult)

Aggregations

PeakResult (uk.ac.sussex.gdsc.smlm.results.PeakResult)64 MemoryPeakResults (uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults)37 List (java.util.List)18 LocalList (uk.ac.sussex.gdsc.core.utils.LocalList)18 Rectangle (java.awt.Rectangle)17 Counter (uk.ac.sussex.gdsc.smlm.results.count.Counter)17 FrameCounter (uk.ac.sussex.gdsc.smlm.results.count.FrameCounter)17 PeakResultProcedure (uk.ac.sussex.gdsc.smlm.results.procedures.PeakResultProcedure)17 ImagePlus (ij.ImagePlus)14 ExtendedGenericDialog (uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog)14 DistanceUnit (uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.DistanceUnit)14 IJ (ij.IJ)13 ImageJUtils (uk.ac.sussex.gdsc.core.ij.ImageJUtils)12 PlugIn (ij.plugin.PlugIn)11 AtomicReference (java.util.concurrent.atomic.AtomicReference)10 SimpleArrayUtils (uk.ac.sussex.gdsc.core.utils.SimpleArrayUtils)10 SettingsManager (uk.ac.sussex.gdsc.smlm.ij.settings.SettingsManager)10 PointRoi (ij.gui.PointRoi)9 ArrayList (java.util.ArrayList)9 TypeConverter (uk.ac.sussex.gdsc.core.data.utils.TypeConverter)9