Search in sources :

Example 31 with PeakResultProcedure

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

the class DensityEstimator method run.

@Override
public void run(String arg) {
    SmlmUsageTracker.recordPlugin(this.getClass(), arg);
    // Require some fit results and selected regions
    if (MemoryPeakResults.countMemorySize() == 0) {
        IJ.error(TITLE, "There are no fitting results in memory");
        return;
    }
    if (!showDialog()) {
        return;
    }
    // Currently this only supports pixel distance units
    final MemoryPeakResults results = ResultsManager.loadInputResults(settings.inputOption, false, DistanceUnit.PIXEL, null);
    if (MemoryPeakResults.isEmpty(results)) {
        IJ.error(TITLE, "No results could be loaded");
        IJ.showStatus("");
        return;
    }
    final long start = System.currentTimeMillis();
    IJ.showStatus("Calculating density ...");
    // Scale to um^2 from px^2
    final double scale = Math.pow(results.getDistanceConverter(DistanceUnit.UM).convertBack(1), 2);
    results.sort();
    final FrameCounter counter = results.newFrameCounter();
    final double localisationsPerFrame = (double) results.size() / (results.getLastFrame() - counter.currentFrame() + 1);
    final Rectangle bounds = results.getBounds(true);
    final double globalDensity = localisationsPerFrame / bounds.width / bounds.height;
    final int border = settings.border;
    final boolean includeSingles = settings.includeSingles;
    final int size = 2 * border + 1;
    final double minDensity = Math.pow(size, -2);
    ImageJUtils.log("%s : %s : Global density %s. Minimum density in %dx%d px = %s um^-2", TITLE, results.getName(), MathUtils.rounded(globalDensity * scale), size, size, MathUtils.rounded(minDensity * scale));
    final TIntArrayList x = new TIntArrayList();
    final TIntArrayList y = new TIntArrayList();
    final ExecutorService es = Executors.newFixedThreadPool(Prefs.getThreads());
    final LocalList<FrameDensity> densities = new LocalList<>();
    final LocalList<Future<?>> futures = new LocalList<>();
    results.forEach((PeakResultProcedure) (peak) -> {
        if (counter.advance(peak.getFrame())) {
            final FrameDensity fd = new FrameDensity(peak.getFrame(), x.toArray(), y.toArray(), border, includeSingles);
            densities.add(fd);
            futures.add(es.submit(fd));
            x.resetQuick();
            y.resetQuick();
        }
        x.add((int) peak.getXPosition());
        y.add((int) peak.getYPosition());
    });
    densities.add(new FrameDensity(counter.currentFrame(), x.toArray(), y.toArray(), border, includeSingles));
    futures.add(es.submit(densities.get(densities.size() - 1)));
    es.shutdown();
    // Wait
    ConcurrencyUtils.waitForCompletionUnchecked(futures);
    densities.sort((o1, o2) -> Integer.compare(o1.frame, o2.frame));
    final int total = densities.stream().mapToInt(fd -> fd.counts.length).sum();
    // Plot density
    final Statistics stats = new Statistics();
    final float[] frame = new float[total];
    final float[] density = new float[total];
    densities.stream().forEach(fd -> {
        for (int i = 0; i < fd.counts.length; i++) {
            final double d = (fd.counts[i] / fd.values[i]) * scale;
            frame[stats.getN()] = fd.frame;
            density[stats.getN()] = (float) d;
            stats.add(d);
        }
    });
    final double mean = stats.getMean();
    final double sd = stats.getStandardDeviation();
    final String label = String.format("Density = %s +/- %s um^-2", MathUtils.rounded(mean), MathUtils.rounded(sd));
    final Plot plot = new Plot("Frame vs Density", "Frame", "Density (um^-2)");
    plot.addPoints(frame, density, Plot.CIRCLE);
    plot.addLabel(0, 0, label);
    final WindowOrganiser wo = new WindowOrganiser();
    ImageJUtils.display(plot.getTitle(), plot, wo);
    // Histogram density
    new HistogramPlotBuilder("Local", StoredData.create(density), "Density (um^-2)").setPlotLabel(label).show(wo);
    wo.tile();
    // Log the number of singles
    final int singles = densities.stream().mapToInt(fd -> fd.singles).sum();
    ImageJUtils.log("Singles %d / %d (%s%%)", singles, results.size(), MathUtils.rounded(100.0 * singles / results.size()));
    IJ.showStatus(TITLE + " complete : " + TextUtils.millisToString(System.currentTimeMillis() - start));
}
Also used : Rectangle(java.awt.Rectangle) Arrays(java.util.Arrays) TIntArrayList(gnu.trove.list.array.TIntArrayList) HistogramPlotBuilder(uk.ac.sussex.gdsc.core.ij.HistogramPlot.HistogramPlotBuilder) Prefs(ij.Prefs) StoredData(uk.ac.sussex.gdsc.core.utils.StoredData) FrameCounter(uk.ac.sussex.gdsc.smlm.results.count.FrameCounter) WindowOrganiser(uk.ac.sussex.gdsc.core.ij.plugin.WindowOrganiser) IntDoubleConsumer(uk.ac.sussex.gdsc.core.utils.function.IntDoubleConsumer) AtomicReference(java.util.concurrent.atomic.AtomicReference) Future(java.util.concurrent.Future) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults) PeakResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.PeakResultProcedure) MathUtils(uk.ac.sussex.gdsc.core.utils.MathUtils) Statistics(uk.ac.sussex.gdsc.core.utils.Statistics) ExecutorService(java.util.concurrent.ExecutorService) LocalDensity(uk.ac.sussex.gdsc.smlm.results.LocalDensity) 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) ConcurrencyUtils(uk.ac.sussex.gdsc.core.utils.concurrent.ConcurrencyUtils) TextUtils(uk.ac.sussex.gdsc.core.utils.TextUtils) Plot(ij.gui.Plot) Executors(java.util.concurrent.Executors) ImageJUtils(uk.ac.sussex.gdsc.core.ij.ImageJUtils) IJ(ij.IJ) PlugIn(ij.plugin.PlugIn) LocalList(uk.ac.sussex.gdsc.core.utils.LocalList) FrameCounter(uk.ac.sussex.gdsc.smlm.results.count.FrameCounter) Plot(ij.gui.Plot) Rectangle(java.awt.Rectangle) HistogramPlotBuilder(uk.ac.sussex.gdsc.core.ij.HistogramPlot.HistogramPlotBuilder) WindowOrganiser(uk.ac.sussex.gdsc.core.ij.plugin.WindowOrganiser) Statistics(uk.ac.sussex.gdsc.core.utils.Statistics) TIntArrayList(gnu.trove.list.array.TIntArrayList) LocalList(uk.ac.sussex.gdsc.core.utils.LocalList) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults)

Example 32 with PeakResultProcedure

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

the class AstigmatismModelManager method plotData.

private boolean plotData() {
    if (results.size() <= imp.getStackSize() / 2) {
        IJ.error(TITLE, "Not enough fit results " + results.size());
        return false;
    }
    final double umPerSlice = pluginSettings.getNmPerSlice() / 1000.0;
    // final double nmPerPixel = results.getNmPerPixel();
    z = new double[results.size()];
    x = new double[z.length];
    y = new double[z.length];
    intensity = new double[z.length];
    final Counter counter = new Counter();
    // We have fit the results so they will be in the preferred units
    results.forEach(new PeakResultProcedure() {

        @Override
        public void execute(PeakResult peak) {
            final int i = counter.getAndIncrement();
            z[i] = peak.getFrame() * umPerSlice;
            x[i] = (peak.getXPosition() - cx);
            y[i] = (peak.getYPosition() - cy);
            intensity[i] = peak.getIntensity();
        }
    });
    final WidthResultProcedure wp = new WidthResultProcedure(results, DistanceUnit.PIXEL);
    wp.getWxWy();
    sx = SimpleArrayUtils.toDouble(wp.wx);
    sy = SimpleArrayUtils.toDouble(wp.wy);
    final WindowOrganiser wo = new WindowOrganiser();
    plot(wo, z, "Intensity (photon)", intensity, "Intensity", null, null);
    xyPlot = plot(wo, z, "Position (px)", x, "X", y, "Y");
    widthPlot = plot(wo, z, "Width (px)", sx, "Sx", sy, "Sy");
    wo.tile();
    return true;
}
Also used : Counter(uk.ac.sussex.gdsc.smlm.results.count.Counter) PeakResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.PeakResultProcedure) WidthResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.WidthResultProcedure) WindowOrganiser(uk.ac.sussex.gdsc.core.ij.plugin.WindowOrganiser) PeakResult(uk.ac.sussex.gdsc.smlm.results.PeakResult)

Example 33 with PeakResultProcedure

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

the class FilterAnalysis method showDialog.

private boolean showDialog(List<MemoryPeakResults> resultsList, boolean fileInput) {
    final GenericDialog gd = new GenericDialog(TITLE);
    String helpKey = "filter-analysis";
    if (fileInput) {
        helpKey += "-file";
    }
    gd.addHelp(HelpUrls.getUrl(helpKey));
    int total = 0;
    final Counter tp = new Counter();
    for (final MemoryPeakResults r : resultsList) {
        total += r.size();
        r.forEach((PeakResultProcedure) result -> {
            if (result.getOrigValue() != 0) {
                tp.increment();
            }
        });
    }
    ImageJUtils.addMessage(gd, "%d files, %d results, %d True-Positives", resultsList.size(), total, tp.getCount());
    settings = Settings.load();
    settings.save();
    if (!fileInput) {
        gd.addCheckbox("SNR_filter", settings.snrFilter);
        gd.addNumericField("Min_SNR", settings.minSnr, 0);
        gd.addNumericField("Max_SNR", settings.maxSnr, 0);
        gd.addNumericField("Min_Width", settings.minWidth, 2);
        gd.addNumericField("Max_Width", settings.maxWidth, 2);
        gd.addNumericField("Increment_Width", settings.incWidth, 2);
        gd.addCheckbox("Precision_filter", settings.precisionFilter);
        gd.addNumericField("Min_Precision", settings.minPrecision, 0);
        gd.addNumericField("Max_Precision", settings.maxPrecision, 0);
        gd.addCheckbox("Trace_filter", settings.traceFilter);
        gd.addNumericField("Min_distance", settings.minDistance, 2);
        gd.addNumericField("Max_distance", settings.maxDistance, 2);
        gd.addNumericField("Increment_distance", settings.incDistance, 2);
        gd.addNumericField("Min_time", settings.minTime, 0);
        gd.addNumericField("Max_time", settings.maxTime, 0);
        gd.addNumericField("Increment_time", settings.incTime, 0);
        gd.addCheckbox("Hysteresis_SNR_filter", settings.hysteresisSnrFilter);
        gd.addNumericField("Min_SNR_gap", settings.minSnrGap, 0);
        gd.addNumericField("Max_SNR_gap", settings.maxSnrGap, 0);
        gd.addNumericField("Increment_SNR_gap", settings.incSnrGap, 0);
        gd.addCheckbox("Hysteresis_Precision_filter", settings.hysteresisPrecisionFilter);
        gd.addNumericField("Min_Precision_gap", settings.minPrecisionGap, 0);
        gd.addNumericField("Max_Precision_gap", settings.maxPrecisionGap, 0);
        gd.addNumericField("Increment_Precision_gap", settings.incPrecisionGap, 0);
        gd.addCheckbox("Save_filters", settings.saveFilterSets);
    }
    gd.addCheckbox("Show_table", settings.showResultsTable);
    gd.addSlider("Plot_top_n", 0, 20, settings.plotTopN);
    gd.addCheckbox("Calculate_sensitivity", settings.calculateSensitivity);
    gd.addSlider("Delta", 0.01, 1, settings.delta);
    gd.showDialog();
    return !gd.wasCanceled() && readDialog(gd, fileInput);
}
Also used : Color(java.awt.Color) TextWindow(ij.text.TextWindow) HashMap(java.util.HashMap) WindowOrganiser(uk.ac.sussex.gdsc.core.ij.plugin.WindowOrganiser) Filter(uk.ac.sussex.gdsc.smlm.results.filter.Filter) AtomicReference(java.util.concurrent.atomic.AtomicReference) OpenDialog(ij.io.OpenDialog) ArrayList(java.util.ArrayList) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults) AndFilter(uk.ac.sussex.gdsc.smlm.results.filter.AndFilter) GenericDialog(ij.gui.GenericDialog) PeakResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.PeakResultProcedure) PlotWindow(ij.gui.PlotWindow) MathUtils(uk.ac.sussex.gdsc.core.utils.MathUtils) PrecisionFilter(uk.ac.sussex.gdsc.smlm.results.filter.PrecisionFilter) LinkedList(java.util.LinkedList) PeakResultsReader(uk.ac.sussex.gdsc.smlm.results.PeakResultsReader) SnrHysteresisFilter(uk.ac.sussex.gdsc.smlm.results.filter.SnrHysteresisFilter) SettingsManager(uk.ac.sussex.gdsc.smlm.ij.settings.SettingsManager) WidthFilter(uk.ac.sussex.gdsc.smlm.results.filter.WidthFilter) GUIFilterSettings(uk.ac.sussex.gdsc.smlm.ij.settings.GUIProtos.GUIFilterSettings) Files(java.nio.file.Files) BufferedWriter(java.io.BufferedWriter) FilterSet(uk.ac.sussex.gdsc.smlm.results.filter.FilterSet) ClassificationResult(uk.ac.sussex.gdsc.core.match.ClassificationResult) Plot(ij.gui.Plot) File(java.io.File) SnrFilter(uk.ac.sussex.gdsc.smlm.results.filter.SnrFilter) Consumer(java.util.function.Consumer) FilterXStreamUtils(uk.ac.sussex.gdsc.smlm.results.filter.FilterXStreamUtils) OrFilter(uk.ac.sussex.gdsc.smlm.results.filter.OrFilter) List(java.util.List) Counter(uk.ac.sussex.gdsc.smlm.results.count.Counter) Paths(java.nio.file.Paths) ImageJUtils(uk.ac.sussex.gdsc.core.ij.ImageJUtils) IJ(ij.IJ) PrecisionHysteresisFilter(uk.ac.sussex.gdsc.smlm.results.filter.PrecisionHysteresisFilter) BufferedReader(java.io.BufferedReader) PlugIn(ij.plugin.PlugIn) Collections(java.util.Collections) TraceFilter(uk.ac.sussex.gdsc.smlm.results.filter.TraceFilter) Counter(uk.ac.sussex.gdsc.smlm.results.count.Counter) GenericDialog(ij.gui.GenericDialog) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults)

Example 34 with PeakResultProcedure

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

the class Filter method filterSubset.

/**
 * 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 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 filterSubset(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());
        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 35 with PeakResultProcedure

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

the class Filter method filterSubset.

/**
 * 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>The number of failures before each peak is stored in the origX property of the PeakResult.
 *
 * @param results the results
 * @param failures the number of failures to allow per frame before all peaks are rejected
 * @param score If not null will be populated with the fraction score [ tp, fp, tn, fn, p, n ]
 * @return the filtered results
 */
public MemoryPeakResults filterSubset(MemoryPeakResults results, final int failures, double[] score) {
    final MemoryPeakResults newResults = new MemoryPeakResults();
    final FrameCounter counter = new FrameCounter();
    newResults.copySettings(results);
    setup(results);
    final double[] s = new double[4];
    results.forEach((PeakResultProcedure) peak -> {
        counter.advanceAndReset(peak.getFrame());
        final boolean isPositive;
        if (counter.getCount() > failures) {
            isPositive = false;
        } else {
            isPositive = accept(peak);
        }
        if (isPositive) {
            peak.setOrigX(counter.getCount());
            counter.reset();
            newResults.add(peak);
        } else {
            counter.increment();
        }
        if (isPositive) {
            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] = newResults.size();
        score[5] = (double) results.size() - newResults.size();
    }
    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)

Aggregations

PeakResultProcedure (uk.ac.sussex.gdsc.smlm.results.procedures.PeakResultProcedure)40 MemoryPeakResults (uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults)35 PeakResult (uk.ac.sussex.gdsc.smlm.results.PeakResult)33 List (java.util.List)29 Counter (uk.ac.sussex.gdsc.smlm.results.count.Counter)26 SimpleArrayUtils (uk.ac.sussex.gdsc.core.utils.SimpleArrayUtils)24 FrameCounter (uk.ac.sussex.gdsc.smlm.results.count.FrameCounter)20 IJ (ij.IJ)19 Nullable (uk.ac.sussex.gdsc.core.annotation.Nullable)19 PlugIn (ij.plugin.PlugIn)18 AtomicReference (java.util.concurrent.atomic.AtomicReference)18 ImageJUtils (uk.ac.sussex.gdsc.core.ij.ImageJUtils)18 ExtendedGenericDialog (uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog)18 DistanceUnit (uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.DistanceUnit)16 ClassificationResult (uk.ac.sussex.gdsc.core.match.ClassificationResult)15 MathUtils (uk.ac.sussex.gdsc.core.utils.MathUtils)15 TextUtils (uk.ac.sussex.gdsc.core.utils.TextUtils)15 Chromosome (uk.ac.sussex.gdsc.smlm.ga.Chromosome)15 XStreamOmitField (com.thoughtworks.xstream.annotations.XStreamOmitField)14 ArrayList (java.util.ArrayList)14