Search in sources :

Example 6 with WindowOrganiser

use of uk.ac.sussex.gdsc.core.ij.plugin.WindowOrganiser in project GDSC-SMLM by aherbert.

the class CmosAnalysis method computeError.

private void computeError() {
    // Assume the simulation stack and measured stack are not null.
    ImageJUtils.log("Comparison to simulation: %s (%dx%dpx)", simulationImp.getInfoProperty(), simulationImp.getWidth(), simulationImp.getHeight());
    WindowOrganiser wo = new WindowOrganiser();
    final ImageStack simulationStack = simulationImp.getImageStack();
    for (int slice = 1; slice <= 3; slice++) {
        computeError(slice, simulationStack, wo);
    }
    wo.tile();
}
Also used : ImageStack(ij.ImageStack) WindowOrganiser(uk.ac.sussex.gdsc.core.ij.plugin.WindowOrganiser)

Example 7 with WindowOrganiser

use of uk.ac.sussex.gdsc.core.ij.plugin.WindowOrganiser in project GDSC-SMLM by aherbert.

the class ConfigurationTemplate method showTemplateImage.

/**
 * Show template image.
 *
 * @param name the name
 */
private void showTemplateImage(String name) {
    final ImagePlus tmpImp = getTemplateImage(name);
    if (tmpImp == null) {
        IJ.error(title, "Failed to load example image for template: " + name);
    } else {
        final WindowOrganiser windowOrganiser = new WindowOrganiser();
        this.imp = displayTemplate(title, tmpImp, null);
        if (windowOrganiser.isNotEmpty()) {
            // Zoom a bit
            final ImageWindow iw = this.imp.getWindow();
            for (int i = 7; i-- > 0 && Math.max(iw.getWidth(), iw.getHeight()) < 512; ) {
                iw.getCanvas().zoomIn(0, 0);
            }
        }
        createResults(this.imp);
        showTemplateInfo(name);
    }
}
Also used : ImageWindow(ij.gui.ImageWindow) WindowOrganiser(uk.ac.sussex.gdsc.core.ij.plugin.WindowOrganiser) ImagePlus(ij.ImagePlus) Point(java.awt.Point)

Example 8 with WindowOrganiser

use of uk.ac.sussex.gdsc.core.ij.plugin.WindowOrganiser in project GDSC-SMLM by aherbert.

the class CameraModelAnalysis method execute.

/**
 * Execute the analysis.
 */
private boolean execute() {
    dirty = false;
    final CameraModelAnalysisSettings settings = this.settings.build();
    if (!(getGain(settings) > 0)) {
        ImageJUtils.log(TITLE + "Error: No total gain");
        return false;
    }
    if (!(settings.getPhotons() > 0)) {
        ImageJUtils.log(TITLE + "Error: No photons");
        return false;
    }
    // Avoid repeating the same analysis
    if (settings.equals(lastSettings)) {
        return true;
    }
    lastSettings = settings;
    final IntHistogram h = getHistogram(settings);
    // Build cumulative distribution
    final double[][] cdf1 = cumulativeHistogram(h);
    final double[] x1 = cdf1[0];
    final double[] y1 = cdf1[1];
    // Interpolate to 300 steps faster evaluation?
    // Get likelihood function
    final LikelihoodFunction f = getLikelihoodFunction(settings);
    // Create likelihood cumulative distribution
    final double[][] cdf2 = cumulativeDistribution(settings, cdf1, f);
    // Compute Komolgorov distance
    final double[] distanceAndValue = getDistance(cdf1, cdf2);
    final double distance = distanceAndValue[0];
    final double value = distanceAndValue[1];
    final double area = distanceAndValue[2];
    final double[] x2 = cdf2[0];
    final double[] y2 = cdf2[1];
    // Fill y1
    int offset = 0;
    while (x2[offset] < x1[0]) {
        offset++;
    }
    final double[] y1b = new double[y2.length];
    System.arraycopy(y1, 0, y1b, offset, y1.length);
    Arrays.fill(y1b, offset + y1.length, y2.length, y1[y1.length - 1]);
    // KolmogorovSmirnovTest
    // n is the number of samples used to build the probability distribution.
    final int n = (int) MathUtils.sum(h.histogramCounts);
    // From KolmogorovSmirnovTest.kolmogorovSmirnovTest(RealDistribution distribution, double[]
    // data, boolean exact):
    // Returns the p-value associated with the null hypothesis that data is a sample from
    // distribution.
    // E.g. If p<0.05 then the null hypothesis is rejected and the data do not match the
    // distribution.
    double pvalue = Double.NaN;
    try {
        pvalue = 1d - kolmogorovSmirnovTest.cdf(distance, n);
    } catch (final MathArithmeticException ex) {
    // Cannot be computed to leave at NaN
    }
    // Plot
    final WindowOrganiser wo = new WindowOrganiser();
    String title = TITLE + " CDF";
    Plot plot = new Plot(title, "Count", "CDF");
    final double max = 1.05 * MathUtils.maxDefault(1, y2);
    plot.setLimits(x2[0], x2[x2.length - 1], 0, max);
    plot.setColor(Color.blue);
    plot.addPoints(x2, y1b, Plot.BAR);
    plot.setColor(Color.red);
    plot.addPoints(x2, y2, Plot.BAR);
    plot.setColor(Color.magenta);
    plot.drawLine(value, 0, value, max);
    plot.setColor(Color.black);
    plot.addLegend("CDF\nModel");
    plot.addLabel(0, 0, String.format("Distance=%s @ %.0f (Mean=%s) : p=%s", MathUtils.rounded(distance), value, MathUtils.rounded(area), MathUtils.rounded(pvalue)));
    ImageJUtils.display(title, plot, ImageJUtils.NO_TO_FRONT, wo);
    // Show the histogram
    title = TITLE + " Histogram";
    plot = new Plot(title, "Count", "Frequency");
    plot.setLimits(x1[0] - 0.5, x1[x1.length - 1] + 1.5, 0, MathUtils.max(h.histogramCounts) * 1.05);
    plot.setColor(Color.blue);
    plot.addPoints(x1, SimpleArrayUtils.toDouble(h.histogramCounts), Plot.BAR);
    plot.setColor(Color.red);
    final double[] x = floatHistogram[0].clone();
    final double[] y = floatHistogram[1].clone();
    final double scale = n / (MathUtils.sum(y) * (x[1] - x[0]));
    for (int i = 0; i < y.length; i++) {
        y[i] *= scale;
    }
    plot.addPoints(x, y, Plot.LINE);
    plot.setColor(Color.black);
    plot.addLegend("Sample\nExpected");
    ImageJUtils.display(title, plot, ImageJUtils.NO_TO_FRONT, wo);
    wo.tile();
    return true;
}
Also used : CameraModelAnalysisSettings(uk.ac.sussex.gdsc.smlm.ij.settings.GUIProtos.CameraModelAnalysisSettings) MathArithmeticException(org.apache.commons.math3.exception.MathArithmeticException) Plot(ij.gui.Plot) WindowOrganiser(uk.ac.sussex.gdsc.core.ij.plugin.WindowOrganiser) LikelihoodFunction(uk.ac.sussex.gdsc.smlm.function.LikelihoodFunction) IntHistogram(uk.ac.sussex.gdsc.core.threshold.IntHistogram)

Example 9 with WindowOrganiser

use of uk.ac.sussex.gdsc.core.ij.plugin.WindowOrganiser in project GDSC-SMLM by aherbert.

the class BackgroundEstimator method drawPlot.

/**
 * Build a plot of the noise estimate from the current frame. Limit the preview to 100 frames.
 */
private void drawPlot() {
    IJ.showStatus("Estimating background ...");
    final int start = imp.getCurrentSlice();
    final int end = Math.min(imp.getStackSize(), start + 100);
    final int size = end - start + 1;
    final double[] xValues = new double[size];
    final double[] noise1 = new double[size];
    final double[] noise2 = new double[size];
    final double[] background = new double[size];
    final double[] threshold = new double[size];
    final double[] percentile = new double[size];
    final ImageStack stack = imp.getImageStack();
    final Rectangle bounds = imp.getProcessor().getRoi();
    float[] buffer = null;
    for (int slice = start, i = 0; slice <= end; slice++, i++) {
        IJ.showProgress(i, size);
        final ImageProcessor ip = stack.getProcessor(slice);
        buffer = ImageJImageConverter.getData(ip.getPixels(), ip.getWidth(), ip.getHeight(), bounds, buffer);
        final DataEstimator de = new DataEstimator(buffer, bounds.width, bounds.height);
        de.setFraction(settings.fraction);
        de.setHistogramSize(settings.histogramSize);
        de.setThresholdMethod(settings.thresholdMethod);
        xValues[i] = slice;
        try {
            noise1[i] = de.getNoise();
            noise2[i] = de.getNoise(FitProtosHelper.convertNoiseEstimatorMethod(settings.noiseMethod));
            background[i] = de.getBackground();
            threshold[i] = de.getThreshold();
            percentile[i] = de.getPercentile(settings.percentile);
        } catch (final Exception ex) {
            throw new DataException("Failed to estimate the background", ex);
        }
    }
    IJ.showProgress(1);
    IJ.showStatus("Plotting background ...");
    final WindowOrganiser wo = new WindowOrganiser();
    plot(wo, xValues, noise1, noise2, null, "Noise", "Background Noise", "Global Noise", null);
    plot(wo, xValues, background, threshold, percentile, "Background", "Background", "Threshold", "Percentile");
    wo.tile();
    IJ.showStatus("");
}
Also used : ImageProcessor(ij.process.ImageProcessor) DataException(uk.ac.sussex.gdsc.core.data.DataException) ImageStack(ij.ImageStack) Rectangle(java.awt.Rectangle) WindowOrganiser(uk.ac.sussex.gdsc.core.ij.plugin.WindowOrganiser) DataEstimator(uk.ac.sussex.gdsc.smlm.engine.DataEstimator) DataException(uk.ac.sussex.gdsc.core.data.DataException)

Example 10 with WindowOrganiser

use of uk.ac.sussex.gdsc.core.ij.plugin.WindowOrganiser in project GDSC-SMLM by aherbert.

the class TcPalmAnalysis method runPlot.

/**
 * Create the activations and total activations plots.
 *
 * @param settings the settings
 */
private void runPlot(TcPalmAnalysisSettings settings) {
    final WindowOrganiser wo = new WindowOrganiser();
    String timeLabel = "Time";
    UnaryOperator<float[]> timeConverter;
    double timeScale;
    if (settings.getTimeInSeconds()) {
        timeLabel += " (s)";
        timeScale = 1.0 / results.getCalibration().getTimeCalibration().getExposureTime();
        timeConverter = frames -> {
            SimpleArrayUtils.apply(frames, f -> f *= timeScale);
            return frames;
        };
    } else {
        timeLabel += " (frame)";
        timeConverter = UnaryOperator.identity();
        timeScale = 1;
    }
    final CumulativeCountData data = this.countData;
    String title = TITLE + " Activations vs Time";
    final Plot plot1 = new Plot(title, timeLabel, "Count");
    plot1.addLabel(0, 0, TextUtils.pleural(clusters.size(), "cluster"));
    for (int i = 0; i < data.frames.length; i++) {
        final int t = data.frames[i];
        final int c = data.counts[i];
        plot1.addPoints(timeConverter.apply(new float[] { t, t }), new float[] { 0, c }, Plot.LINE);
    }
    plot1.draw();
    plot1.setLimitsToFit(true);
    if (settings.getFixedTimeAxis()) {
        final double[] limits = plot1.getLimits();
        limits[0] = timeScale * (minT - 1);
        limits[1] = timeScale * (maxT + 1);
        plot1.setLimits(limits);
        plot1.updateImage();
    }
    final PlotWindow pw1 = ImageJUtils.display(title, plot1, ImageJUtils.NO_TO_FRONT, wo);
    title = TITLE + " Total Activations vs Time";
    final Plot plot2 = new Plot(title, timeLabel, "Cumulative count");
    final int localisations = data.counts.length == 0 ? 0 : data.plotCounts[data.plotCounts.length - 1];
    final int clashes = localisations - data.counts.length;
    plot2.addLabel(0, 0, TextUtils.pleural(localisations, "localisation") + " : " + clashes + TextUtils.pleuralise(clashes, " clash", " clashes"));
    plot2.addPoints(timeConverter.apply(SimpleArrayUtils.toFloat(data.plotFrames)), SimpleArrayUtils.toFloat(data.plotCounts), Plot.LINE);
    if (settings.getFixedTimeAxis()) {
        plot2.setLimits(timeScale * (minT - 1), timeScale * (maxT + 1), Double.NaN, Double.NaN);
    }
    final PlotWindow pw2 = ImageJUtils.display(title, plot2, ImageJUtils.NO_TO_FRONT, wo);
    activationsPlotData = new ActivationsPlotData(plot2, timeConverter, data);
    // Simple tile one window above the other only if both are new.
    if (wo.size() == 2) {
        final Point p = pw1.getLocation();
        p.y += pw1.getHeight();
        pw2.setLocation(p);
    }
}
Also used : Plot(ij.gui.Plot) PlotWindow(ij.gui.PlotWindow) WindowOrganiser(uk.ac.sussex.gdsc.core.ij.plugin.WindowOrganiser) Point(java.awt.Point) Point(java.awt.Point)

Aggregations

WindowOrganiser (uk.ac.sussex.gdsc.core.ij.plugin.WindowOrganiser)25 Plot (ij.gui.Plot)12 HistogramPlotBuilder (uk.ac.sussex.gdsc.core.ij.HistogramPlot.HistogramPlotBuilder)10 ImagePlus (ij.ImagePlus)8 Rectangle (java.awt.Rectangle)8 Statistics (uk.ac.sussex.gdsc.core.utils.Statistics)8 ImageStack (ij.ImageStack)7 Point (java.awt.Point)7 LocalList (uk.ac.sussex.gdsc.core.utils.LocalList)7 PlotWindow (ij.gui.PlotWindow)6 ExtendedGenericDialog (uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog)6 StoredDataStatistics (uk.ac.sussex.gdsc.core.utils.StoredDataStatistics)6 MemoryPeakResults (uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults)6 Color (java.awt.Color)5 ExecutorService (java.util.concurrent.ExecutorService)5 IJ (ij.IJ)4 PlugIn (ij.plugin.PlugIn)4 LUT (ij.process.LUT)4 Future (java.util.concurrent.Future)4 HistogramPlot (uk.ac.sussex.gdsc.core.ij.HistogramPlot)4