Search in sources :

Example 11 with Counter

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

the class TraceMolecules method convertToClusterPoints.

/**
 * Convert a list of peak results into points for the clustering engine.
 *
 * @param results the results
 * @return the list of clusters
 */
public static List<ClusterPoint> convertToClusterPoints(MemoryPeakResults results) {
    final ArrayList<ClusterPoint> points = new ArrayList<>(results.size());
    final Counter counter = new Counter();
    results.forEach((PeakResultProcedure) result -> points.add(ClusterPoint.newTimeClusterPoint(counter.getAndIncrement(), result.getXPosition(), result.getYPosition(), result.getIntensity(), result.getFrame(), result.getEndFrame())));
    return points;
}
Also used : Arrays(java.util.Arrays) ClusteringEngine(uk.ac.sussex.gdsc.core.clustering.ClusteringEngine) UnitConverterUtils(uk.ac.sussex.gdsc.smlm.data.config.UnitConverterUtils) HistogramPlotBuilder(uk.ac.sussex.gdsc.core.ij.HistogramPlot.HistogramPlotBuilder) TextWindow(ij.text.TextWindow) CalibrationOrBuilder(uk.ac.sussex.gdsc.smlm.data.config.CalibrationProtos.CalibrationOrBuilder) StoredDataStatistics(uk.ac.sussex.gdsc.core.utils.StoredDataStatistics) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults) ClusteringAlgorithm(uk.ac.sussex.gdsc.core.clustering.ClusteringAlgorithm) MathUtils(uk.ac.sussex.gdsc.core.utils.MathUtils) SettingsManager(uk.ac.sussex.gdsc.smlm.ij.settings.SettingsManager) ClusteringSettings(uk.ac.sussex.gdsc.smlm.ij.settings.GUIProtos.ClusteringSettings) 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) TraceMode(uk.ac.sussex.gdsc.smlm.results.TraceManager.TraceMode) Logger(java.util.logging.Logger) TIntHashSet(gnu.trove.set.hash.TIntHashSet) CalibrationHelper(uk.ac.sussex.gdsc.smlm.data.config.CalibrationHelper) ImagePlus(ij.ImagePlus) LutLoader(ij.plugin.LutLoader) List(java.util.List) Converter(uk.ac.sussex.gdsc.core.data.utils.Converter) SimpleArrayUtils(uk.ac.sussex.gdsc.core.utils.SimpleArrayUtils) FileUtils(uk.ac.sussex.gdsc.core.utils.FileUtils) PlugIn(ij.plugin.PlugIn) TypeConverter(uk.ac.sussex.gdsc.core.data.utils.TypeConverter) Roi(ij.gui.Roi) PrecisionResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.PrecisionResultProcedure) SplineInterpolator(org.apache.commons.math3.analysis.interpolation.SplineInterpolator) TraceManager(uk.ac.sussex.gdsc.smlm.results.TraceManager) PolygonRoi(ij.gui.PolygonRoi) PolynomialSplineFunction(org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction) Prefs(ij.Prefs) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WindowManager(ij.WindowManager) WindowOrganiser(uk.ac.sussex.gdsc.core.ij.plugin.WindowOrganiser) ClusterPoint(uk.ac.sussex.gdsc.core.clustering.ClusterPoint) DmttConfiguration(uk.ac.sussex.gdsc.smlm.results.DynamicMultipleTargetTracing.DmttConfiguration) AtomicReference(java.util.concurrent.atomic.AtomicReference) TextField(java.awt.TextField) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) Trace(uk.ac.sussex.gdsc.smlm.results.Trace) PeakResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.PeakResultProcedure) Statistics(uk.ac.sussex.gdsc.core.utils.Statistics) SimpleImageJTrackProgress(uk.ac.sussex.gdsc.core.ij.SimpleImageJTrackProgress) Files(java.nio.file.Files) Calibration(ij.measure.Calibration) Checkbox(java.awt.Checkbox) BufferedWriter(java.io.BufferedWriter) DynamicMultipleTargetTracing(uk.ac.sussex.gdsc.smlm.results.DynamicMultipleTargetTracing) TimeUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.TimeUnit) Consumer(java.util.function.Consumer) SummaryStatistics(org.apache.commons.math3.stat.descriptive.SummaryStatistics) FloatProcessor(ij.process.FloatProcessor) Counter(uk.ac.sussex.gdsc.smlm.results.count.Counter) Paths(java.nio.file.Paths) Cluster(uk.ac.sussex.gdsc.core.clustering.Cluster) ImageJUtils(uk.ac.sussex.gdsc.core.ij.ImageJUtils) TextFilePeakResults(uk.ac.sussex.gdsc.smlm.results.TextFilePeakResults) IJ(ij.IJ) Collections(java.util.Collections) Counter(uk.ac.sussex.gdsc.smlm.results.count.Counter) ArrayList(java.util.ArrayList) ClusterPoint(uk.ac.sussex.gdsc.core.clustering.ClusterPoint)

Example 12 with Counter

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

the class Filter method filter.

/**
 * Filter the results.
 *
 * <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.
 *
 * @param results the results
 * @param failures the number of failures to allow per frame before all peaks are rejected
 * @return the filtered results
 */
public MemoryPeakResults filter(MemoryPeakResults results, final int failures) {
    final MemoryPeakResults newResults = new MemoryPeakResults();
    final FrameCounter counter = new FrameCounter();
    newResults.copySettings(results);
    setup(results);
    results.forEach((PeakResultProcedure) peak -> {
        counter.advanceAndReset(peak.getFrame());
        final boolean isPositive;
        if (counter.getCount() > failures) {
            isPositive = false;
        } else {
            isPositive = accept(peak);
        }
        if (isPositive) {
            counter.reset();
            newResults.add(peak);
        } else {
            counter.increment();
        }
    });
    end();
    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) FrameCounter(uk.ac.sussex.gdsc.smlm.results.count.FrameCounter) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults)

Example 13 with Counter

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

the class Filter method fractionScoreSubset.

/**
 * Filter the results and return the performance score. Allows benchmarking the filter by marking
 * the results as true or false.
 *
 * <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 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)
 * @param initialNegatives The initial negatives (used when the results have been pre-filtered)
 * @return the score
 */
public FractionClassificationResult fractionScoreSubset(List<MemoryPeakResults> resultsList, final int failures, double tn, double fn, int initialNegatives) {
    final double[] s = new double[4];
    s[TN] = tn;
    s[FN] = fn;
    final Counter p = new Counter();
    int negatives = initialNegatives;
    for (final MemoryPeakResults peakResults : resultsList) {
        setup(peakResults);
        final FrameCounter counter = new FrameCounter();
        peakResults.forEach((PeakResultProcedure) peak -> {
            counter.advanceAndReset(peak.getFrame());
            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 (isPositive) {
                p.increment();
                s[TP] += peak.getTruePositiveScore();
                s[FP] += peak.getFalsePositiveScore();
            } else {
                s[FN] += peak.getFalseNegativeScore();
                s[TN] += peak.getTrueNegativeScore();
            }
        });
        negatives += peakResults.size();
        end();
    }
    negatives -= p.getCount();
    return new FractionClassificationResult(s[TP], s[FP], s[TN], s[FN], p.getCount(), negatives);
}
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) FractionClassificationResult(uk.ac.sussex.gdsc.core.match.FractionClassificationResult) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults)

Example 14 with Counter

use of uk.ac.sussex.gdsc.smlm.results.count.Counter 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 15 with Counter

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

the class Filter method fractionScore.

/**
 * Filter the results and return the performance score. Allows benchmarking the filter by marking
 * the results as true or false.
 *
 * <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.
 *
 * @param resultsList a list of results to analyse
 * @param failures the number of failures to allow per frame before all peaks are rejected
 * @return the score
 */
public FractionClassificationResult fractionScore(List<MemoryPeakResults> resultsList, final int failures) {
    final double[] s = new double[4];
    final Counter p = new Counter();
    int negatives = 0;
    for (final MemoryPeakResults peakResults : resultsList) {
        setup(peakResults);
        final FrameCounter counter = new FrameCounter();
        peakResults.forEach((PeakResultProcedure) peak -> {
            counter.advanceAndReset(peak.getFrame());
            final boolean isPositive;
            if (counter.getCount() > failures) {
                isPositive = false;
            } else {
                isPositive = accept(peak);
            }
            if (isPositive) {
                counter.reset();
            } else {
                counter.increment();
            }
            if (isPositive) {
                p.increment();
                s[TP] += peak.getTruePositiveScore();
                s[FP] += peak.getFalsePositiveScore();
            } else {
                s[FN] += peak.getFalseNegativeScore();
                s[TN] += peak.getTrueNegativeScore();
            }
        });
        negatives += peakResults.size();
        end();
    }
    negatives -= p.getCount();
    return new FractionClassificationResult(s[TP], s[FP], s[TN], s[FN], p.getCount(), negatives);
}
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) FractionClassificationResult(uk.ac.sussex.gdsc.core.match.FractionClassificationResult) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults)

Aggregations

Counter (uk.ac.sussex.gdsc.smlm.results.count.Counter)32 MemoryPeakResults (uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults)27 List (java.util.List)25 PeakResult (uk.ac.sussex.gdsc.smlm.results.PeakResult)23 PeakResultProcedure (uk.ac.sussex.gdsc.smlm.results.procedures.PeakResultProcedure)22 SimpleArrayUtils (uk.ac.sussex.gdsc.core.utils.SimpleArrayUtils)17 FrameCounter (uk.ac.sussex.gdsc.smlm.results.count.FrameCounter)17 IJ (ij.IJ)15 AtomicReference (java.util.concurrent.atomic.AtomicReference)15 ImageJUtils (uk.ac.sussex.gdsc.core.ij.ImageJUtils)15 ExtendedGenericDialog (uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog)14 ArrayList (java.util.ArrayList)13 Nullable (uk.ac.sussex.gdsc.core.annotation.Nullable)13 ClassificationResult (uk.ac.sussex.gdsc.core.match.ClassificationResult)13 DistanceUnit (uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.DistanceUnit)13 PlugIn (ij.plugin.PlugIn)12 FractionClassificationResult (uk.ac.sussex.gdsc.core.match.FractionClassificationResult)12 Chromosome (uk.ac.sussex.gdsc.smlm.ga.Chromosome)12 XStreamOmitField (com.thoughtworks.xstream.annotations.XStreamOmitField)11 Rectangle (java.awt.Rectangle)11