Search in sources :

Example 1 with LinearInterpolator

use of org.apache.commons.math3.analysis.interpolation.LinearInterpolator in project GDSC-SMLM by aherbert.

the class BenchmarkSpotFit method showDoubleHistogram.

private double[] showDoubleHistogram(StoredDataStatistics[][] stats, final int index, WindowOrganiser wo, double[][] matchScores) {
    final String xLabel = filterCriteria[index].name;
    LowerLimit lower = filterCriteria[index].lower;
    UpperLimit upper = filterCriteria[index].upper;
    double[] jaccard = null;
    double[] metric = null;
    double maxJaccard = 0;
    if (index <= FILTER_PRECISION && (settings.showFilterScoreHistograms || upper.requiresJaccard || lower.requiresJaccard)) {
        // Jaccard score verses the range of the metric
        for (final double[] d : matchScores) {
            if (!Double.isFinite(d[index])) {
                System.out.printf("Error in fit data [%d]: %s%n", index, d[index]);
            }
        }
        // Do not use Double.compare(double, double) so we get exceptions in the sort for inf/nan
        Arrays.sort(matchScores, (o1, o2) -> {
            if (o1[index] < o2[index]) {
                return -1;
            }
            if (o1[index] > o2[index]) {
                return 1;
            }
            return 0;
        });
        final int scoreIndex = FILTER_PRECISION + 1;
        final int n = results.size();
        double tp = 0;
        double fp = 0;
        jaccard = new double[matchScores.length + 1];
        metric = new double[jaccard.length];
        for (int k = 0; k < matchScores.length; k++) {
            final double score = matchScores[k][scoreIndex];
            tp += score;
            fp += (1 - score);
            jaccard[k + 1] = tp / (fp + n);
            metric[k + 1] = matchScores[k][index];
        }
        metric[0] = metric[1];
        maxJaccard = MathUtils.max(jaccard);
        if (settings.showFilterScoreHistograms) {
            final String title = TITLE + " Jaccard " + xLabel;
            final Plot plot = new Plot(title, xLabel, "Jaccard");
            plot.addPoints(metric, jaccard, Plot.LINE);
            // Remove outliers
            final double[] limitsx = MathUtils.limits(metric);
            final Percentile p = new Percentile();
            final double l = p.evaluate(metric, 25);
            final double u = p.evaluate(metric, 75);
            final double iqr = 1.5 * (u - l);
            limitsx[1] = Math.min(limitsx[1], u + iqr);
            plot.setLimits(limitsx[0], limitsx[1], 0, MathUtils.max(jaccard));
            ImageJUtils.display(title, plot, wo);
        }
    }
    // [0] is all
    // [1] is matches
    // [2] is no match
    final StoredDataStatistics s1 = stats[0][index];
    final StoredDataStatistics s2 = stats[1][index];
    final StoredDataStatistics s3 = stats[2][index];
    if (s1.getN() == 0) {
        return new double[4];
    }
    final DescriptiveStatistics d = s1.getStatistics();
    double median = 0;
    Plot plot = null;
    String title = null;
    if (settings.showFilterScoreHistograms) {
        median = d.getPercentile(50);
        final String label = String.format("n = %d. Median = %s nm", s1.getN(), MathUtils.rounded(median));
        final HistogramPlot histogramPlot = new HistogramPlotBuilder(TITLE, s1, xLabel).setMinBinWidth(filterCriteria[index].minBinWidth).setRemoveOutliersOption((filterCriteria[index].restrictRange) ? 1 : 0).setPlotLabel(label).build();
        final PlotWindow plotWindow = histogramPlot.show(wo);
        if (plotWindow == null) {
            IJ.log("Failed to show the histogram: " + xLabel);
            return new double[4];
        }
        title = plotWindow.getTitle();
        // Reverse engineer the histogram settings
        plot = histogramPlot.getPlot();
        final double[] xvalues = histogramPlot.getPlotXValues();
        final int bins = xvalues.length;
        final double yMin = xvalues[0];
        final double binSize = xvalues[1] - xvalues[0];
        final double yMax = xvalues[0] + (bins - 1) * binSize;
        if (s2.getN() > 0) {
            final double[] values = s2.getValues();
            final double[][] hist = HistogramPlot.calcHistogram(values, yMin, yMax, bins);
            if (hist[0].length > 0) {
                plot.setColor(Color.red);
                plot.addPoints(hist[0], hist[1], Plot.BAR);
                ImageJUtils.display(title, plot);
            }
        }
        if (s3.getN() > 0) {
            final double[] values = s3.getValues();
            final double[][] hist = HistogramPlot.calcHistogram(values, yMin, yMax, bins);
            if (hist[0].length > 0) {
                plot.setColor(Color.blue);
                plot.addPoints(hist[0], hist[1], Plot.BAR);
                ImageJUtils.display(title, plot);
            }
        }
    }
    // Do cumulative histogram
    final double[][] h1 = MathUtils.cumulativeHistogram(s1.getValues(), true);
    final double[][] h2 = MathUtils.cumulativeHistogram(s2.getValues(), true);
    final double[][] h3 = MathUtils.cumulativeHistogram(s3.getValues(), true);
    if (settings.showFilterScoreHistograms) {
        title = TITLE + " Cumul " + xLabel;
        plot = new Plot(title, xLabel, "Frequency");
        // Find limits
        double[] xlimit = MathUtils.limits(h1[0]);
        xlimit = MathUtils.limits(xlimit, h2[0]);
        xlimit = MathUtils.limits(xlimit, h3[0]);
        // Restrict using the inter-quartile range
        if (filterCriteria[index].restrictRange) {
            final double q1 = d.getPercentile(25);
            final double q2 = d.getPercentile(75);
            final double iqr = (q2 - q1) * 2.5;
            xlimit[0] = MathUtils.max(xlimit[0], median - iqr);
            xlimit[1] = MathUtils.min(xlimit[1], median + iqr);
        }
        plot.setLimits(xlimit[0], xlimit[1], 0, 1.05);
        plot.addPoints(h1[0], h1[1], Plot.LINE);
        plot.setColor(Color.red);
        plot.addPoints(h2[0], h2[1], Plot.LINE);
        plot.setColor(Color.blue);
        plot.addPoints(h3[0], h3[1], Plot.LINE);
    }
    // Determine the maximum difference between the TP and FP
    double maxx1 = 0;
    double maxx2 = 0;
    double max1 = 0;
    double max2 = 0;
    // We cannot compute the delta histogram, or use percentiles
    if (s2.getN() == 0) {
        upper = UpperLimit.ZERO;
        lower = LowerLimit.ZERO;
    }
    final boolean requireLabel = (settings.showFilterScoreHistograms && filterCriteria[index].requireLabel);
    if (requireLabel || upper.requiresDeltaHistogram() || lower.requiresDeltaHistogram()) {
        if (s2.getN() != 0 && s3.getN() != 0) {
            final LinearInterpolator li = new LinearInterpolator();
            final PolynomialSplineFunction f1 = li.interpolate(h2[0], h2[1]);
            final PolynomialSplineFunction f2 = li.interpolate(h3[0], h3[1]);
            for (final double x : h1[0]) {
                if (x < h2[0][0] || x < h3[0][0]) {
                    continue;
                }
                try {
                    final double v1 = f1.value(x);
                    final double v2 = f2.value(x);
                    final double diff = v2 - v1;
                    if (diff > 0) {
                        if (max1 < diff) {
                            max1 = diff;
                            maxx1 = x;
                        }
                    } else if (max2 > diff) {
                        max2 = diff;
                        maxx2 = x;
                    }
                } catch (final OutOfRangeException ex) {
                    // Because we reached the end
                    break;
                }
            }
        }
    }
    if (plot != null) {
        // We use bins=1 on charts where we do not need a label
        if (requireLabel) {
            final String label = String.format("Max+ %s @ %s, Max- %s @ %s", MathUtils.rounded(max1), MathUtils.rounded(maxx1), MathUtils.rounded(max2), MathUtils.rounded(maxx2));
            plot.setColor(Color.black);
            plot.addLabel(0, 0, label);
        }
        ImageJUtils.display(title, plot, wo);
    }
    // Now compute the bounds using the desired limit
    double lowerBound;
    double upperBound;
    switch(lower) {
        case MAX_NEGATIVE_CUMUL_DELTA:
            // Switch to percentiles if we have no delta histogram
            if (maxx2 < 0) {
                lowerBound = maxx2;
                break;
            }
        // fall-through
        case ONE_PERCENT:
            lowerBound = getPercentile(h2, 0.01);
            break;
        case MIN:
            lowerBound = getPercentile(h2, 0.0);
            break;
        case ZERO:
            lowerBound = 0;
            break;
        case HALF_MAX_JACCARD_VALUE:
            lowerBound = getXValue(metric, jaccard, maxJaccard * 0.5);
            break;
        default:
            throw new IllegalStateException("Missing lower limit method");
    }
    switch(upper) {
        case MAX_POSITIVE_CUMUL_DELTA:
            // Switch to percentiles if we have no delta histogram
            if (maxx1 > 0) {
                upperBound = maxx1;
                break;
            }
        // fall-through
        case NINETY_NINE_PERCENT:
            upperBound = getPercentile(h2, 0.99);
            break;
        case NINETY_NINE_NINE_PERCENT:
            upperBound = getPercentile(h2, 0.999);
            break;
        case ZERO:
            upperBound = 0;
            break;
        case MAX_JACCARD2:
            upperBound = getXValue(metric, jaccard, maxJaccard) * 2;
            // System.out.printf("MaxJ = %.4f @ %.3f\n", maxJ, u / 2);
            break;
        default:
            throw new IllegalStateException("Missing upper limit method");
    }
    final double min = getPercentile(h1, 0);
    final double max = getPercentile(h1, 1);
    return new double[] { lowerBound, upperBound, min, max };
}
Also used : DescriptiveStatistics(org.apache.commons.math3.stat.descriptive.DescriptiveStatistics) Percentile(org.apache.commons.math3.stat.descriptive.rank.Percentile) Plot(ij.gui.Plot) HistogramPlot(uk.ac.sussex.gdsc.core.ij.HistogramPlot) StoredDataStatistics(uk.ac.sussex.gdsc.core.utils.StoredDataStatistics) HistogramPlotBuilder(uk.ac.sussex.gdsc.core.ij.HistogramPlot.HistogramPlotBuilder) PlotWindow(ij.gui.PlotWindow) PolynomialSplineFunction(org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction) PeakResultPoint(uk.ac.sussex.gdsc.smlm.results.PeakResultPoint) BasePoint(uk.ac.sussex.gdsc.core.match.BasePoint) HistogramPlot(uk.ac.sussex.gdsc.core.ij.HistogramPlot) LinearInterpolator(org.apache.commons.math3.analysis.interpolation.LinearInterpolator) OutOfRangeException(org.apache.commons.math3.exception.OutOfRangeException)

Example 2 with LinearInterpolator

use of org.apache.commons.math3.analysis.interpolation.LinearInterpolator in project GDSC-SMLM by aherbert.

the class DriftCalculator method interpolate.

private void interpolate(double[] dx, double[] dy, double[] originalDriftTimePoints) {
    // Interpolator can only create missing values within the range provided by the input values.
    // The two ends have to be extrapolated.
    // TODO: Perform extrapolation. Currently the end values are used.
    // Find end points
    int startT = 0;
    while (originalDriftTimePoints[startT] == 0) startT++;
    int endT = originalDriftTimePoints.length - 1;
    while (originalDriftTimePoints[endT] == 0) endT--;
    // Extrapolate using a constant value
    for (int t = startT; t-- > 0; ) {
        dx[t] = dx[startT];
        dy[t] = dy[startT];
    }
    for (int t = endT; ++t < dx.length; ) {
        dx[t] = dx[endT];
        dy[t] = dy[endT];
    }
    double[][] values = extractValues(originalDriftTimePoints, startT, endT, dx, dy);
    PolynomialSplineFunction fx, fy;
    if (values[0].length < 3) {
        fx = new LinearInterpolator().interpolate(values[0], values[1]);
        fy = new LinearInterpolator().interpolate(values[0], values[2]);
    } else {
        fx = new SplineInterpolator().interpolate(values[0], values[1]);
        fy = new SplineInterpolator().interpolate(values[0], values[2]);
    }
    for (int t = startT; t <= endT; t++) {
        if (originalDriftTimePoints[t] == 0) {
            dx[t] = fx.value(t);
            dy[t] = fy.value(t);
        }
    }
    this.interpolationStart = startT;
    this.interpolationEnd = endT;
}
Also used : LinearInterpolator(org.apache.commons.math3.analysis.interpolation.LinearInterpolator) SplineInterpolator(org.apache.commons.math3.analysis.interpolation.SplineInterpolator) PolynomialSplineFunction(org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction) Point(java.awt.Point)

Example 3 with LinearInterpolator

use of org.apache.commons.math3.analysis.interpolation.LinearInterpolator in project GDSC-SMLM by aherbert.

the class BenchmarkSpotFit method showDoubleHistogram.

private double[] showDoubleHistogram(StoredDataStatistics[][] stats, final int i, WindowOrganiser wo, double[][] matchScores, double nPredicted) {
    String xLabel = filterCriteria[i].name;
    LowerLimit lower = filterCriteria[i].lower;
    UpperLimit upper = filterCriteria[i].upper;
    double[] j = null;
    double[] metric = null;
    double maxJ = 0;
    if (i <= FILTER_PRECISION && (showFilterScoreHistograms || upper.requiresJaccard || lower.requiresJaccard)) {
        // Jaccard score verses the range of the metric
        Arrays.sort(matchScores, new Comparator<double[]>() {

            public int compare(double[] o1, double[] o2) {
                if (o1[i] < o2[i])
                    return -1;
                if (o1[i] > o2[i])
                    return 1;
                return 0;
            }
        });
        final int scoreIndex = FILTER_PRECISION + 1;
        int n = results.size();
        double tp = 0;
        double fp = 0;
        j = new double[matchScores.length + 1];
        metric = new double[j.length];
        for (int k = 0; k < matchScores.length; k++) {
            final double score = matchScores[k][scoreIndex];
            tp += score;
            fp += (1 - score);
            j[k + 1] = tp / (fp + n);
            metric[k + 1] = matchScores[k][i];
        }
        metric[0] = metric[1];
        maxJ = Maths.max(j);
        if (showFilterScoreHistograms) {
            String title = TITLE + " Jaccard " + xLabel;
            Plot plot = new Plot(title, xLabel, "Jaccard", metric, j);
            // Remove outliers
            double[] limitsx = Maths.limits(metric);
            Percentile p = new Percentile();
            double l = p.evaluate(metric, 25);
            double u = p.evaluate(metric, 75);
            double iqr = 1.5 * (u - l);
            limitsx[1] = Math.min(limitsx[1], u + iqr);
            plot.setLimits(limitsx[0], limitsx[1], 0, Maths.max(j));
            PlotWindow pw = Utils.display(title, plot);
            if (Utils.isNewWindow())
                wo.add(pw);
        }
    }
    // [0] is all
    // [1] is matches
    // [2] is no match
    StoredDataStatistics s1 = stats[0][i];
    StoredDataStatistics s2 = stats[1][i];
    StoredDataStatistics s3 = stats[2][i];
    if (s1.getN() == 0)
        return new double[4];
    DescriptiveStatistics d = s1.getStatistics();
    double median = 0;
    Plot2 plot = null;
    String title = null;
    if (showFilterScoreHistograms) {
        median = d.getPercentile(50);
        String label = String.format("n = %d. Median = %s nm", s1.getN(), Utils.rounded(median));
        int id = Utils.showHistogram(TITLE, s1, xLabel, filterCriteria[i].minBinWidth, (filterCriteria[i].restrictRange) ? 1 : 0, 0, label);
        if (id == 0) {
            IJ.log("Failed to show the histogram: " + xLabel);
            return new double[4];
        }
        if (Utils.isNewWindow())
            wo.add(id);
        title = WindowManager.getImage(id).getTitle();
        // Reverse engineer the histogram settings
        plot = Utils.plot;
        double[] xValues = Utils.xValues;
        int bins = xValues.length;
        double yMin = xValues[0];
        double binSize = xValues[1] - xValues[0];
        double yMax = xValues[0] + (bins - 1) * binSize;
        if (s2.getN() > 0) {
            double[] values = s2.getValues();
            double[][] hist = Utils.calcHistogram(values, yMin, yMax, bins);
            if (hist[0].length > 0) {
                plot.setColor(Color.red);
                plot.addPoints(hist[0], hist[1], Plot2.BAR);
                Utils.display(title, plot);
            }
        }
        if (s3.getN() > 0) {
            double[] values = s3.getValues();
            double[][] hist = Utils.calcHistogram(values, yMin, yMax, bins);
            if (hist[0].length > 0) {
                plot.setColor(Color.blue);
                plot.addPoints(hist[0], hist[1], Plot2.BAR);
                Utils.display(title, plot);
            }
        }
    }
    // Do cumulative histogram
    double[][] h1 = Maths.cumulativeHistogram(s1.getValues(), true);
    double[][] h2 = Maths.cumulativeHistogram(s2.getValues(), true);
    double[][] h3 = Maths.cumulativeHistogram(s3.getValues(), true);
    if (showFilterScoreHistograms) {
        title = TITLE + " Cumul " + xLabel;
        plot = new Plot2(title, xLabel, "Frequency");
        // Find limits
        double[] xlimit = Maths.limits(h1[0]);
        xlimit = Maths.limits(xlimit, h2[0]);
        xlimit = Maths.limits(xlimit, h3[0]);
        // Restrict using the inter-quartile range 
        if (filterCriteria[i].restrictRange) {
            double q1 = d.getPercentile(25);
            double q2 = d.getPercentile(75);
            double iqr = (q2 - q1) * 2.5;
            xlimit[0] = Maths.max(xlimit[0], median - iqr);
            xlimit[1] = Maths.min(xlimit[1], median + iqr);
        }
        plot.setLimits(xlimit[0], xlimit[1], 0, 1.05);
        plot.addPoints(h1[0], h1[1], Plot.LINE);
        plot.setColor(Color.red);
        plot.addPoints(h2[0], h2[1], Plot.LINE);
        plot.setColor(Color.blue);
        plot.addPoints(h3[0], h3[1], Plot.LINE);
    }
    // Determine the maximum difference between the TP and FP
    double maxx1 = 0;
    double maxx2 = 0;
    double max1 = 0;
    double max2 = 0;
    // We cannot compute the delta histogram, or use percentiles
    if (s2.getN() == 0) {
        upper = UpperLimit.ZERO;
        lower = LowerLimit.ZERO;
    }
    final boolean requireLabel = (showFilterScoreHistograms && filterCriteria[i].requireLabel);
    if (requireLabel || upper.requiresDeltaHistogram() || lower.requiresDeltaHistogram()) {
        if (s2.getN() != 0 && s3.getN() != 0) {
            LinearInterpolator li = new LinearInterpolator();
            PolynomialSplineFunction f1 = li.interpolate(h2[0], h2[1]);
            PolynomialSplineFunction f2 = li.interpolate(h3[0], h3[1]);
            for (double x : h1[0]) {
                if (x < h2[0][0] || x < h3[0][0])
                    continue;
                try {
                    double v1 = f1.value(x);
                    double v2 = f2.value(x);
                    double diff = v2 - v1;
                    if (diff > 0) {
                        if (max1 < diff) {
                            max1 = diff;
                            maxx1 = x;
                        }
                    } else {
                        if (max2 > diff) {
                            max2 = diff;
                            maxx2 = x;
                        }
                    }
                } catch (OutOfRangeException e) {
                    // Because we reached the end
                    break;
                }
            }
        } else {
            // Switch to percentiles if we have no delta histogram
            if (upper.requiresDeltaHistogram())
                upper = UpperLimit.NINETY_NINE_PERCENT;
            if (lower.requiresDeltaHistogram())
                lower = LowerLimit.ONE_PERCENT;
        }
    //			System.out.printf("Bounds %s : %s, pos %s, neg %s, %s\n", xLabel, Utils.rounded(getPercentile(h2, 0.01)),
    //					Utils.rounded(maxx1), Utils.rounded(maxx2), Utils.rounded(getPercentile(h1, 0.99)));
    }
    if (showFilterScoreHistograms) {
        // We use bins=1 on charts where we do not need a label
        if (requireLabel) {
            String label = String.format("Max+ %s @ %s, Max- %s @ %s", Utils.rounded(max1), Utils.rounded(maxx1), Utils.rounded(max2), Utils.rounded(maxx2));
            plot.setColor(Color.black);
            plot.addLabel(0, 0, label);
        }
        PlotWindow pw = Utils.display(title, plot);
        if (Utils.isNewWindow())
            wo.add(pw.getImagePlus().getID());
    }
    // Now compute the bounds using the desired limit
    double l, u;
    switch(lower) {
        case ONE_PERCENT:
            l = getPercentile(h2, 0.01);
            break;
        case MAX_NEGATIVE_CUMUL_DELTA:
            l = maxx2;
            break;
        case ZERO:
            l = 0;
            break;
        case HALF_MAX_JACCARD_VALUE:
            l = getValue(metric, j, maxJ * 0.5);
            break;
        default:
            throw new RuntimeException("Missing lower limit method");
    }
    switch(upper) {
        case MAX_POSITIVE_CUMUL_DELTA:
            u = maxx1;
            break;
        case NINETY_NINE_PERCENT:
            u = getPercentile(h2, 0.99);
            break;
        case NINETY_NINE_NINE_PERCENT:
            u = getPercentile(h2, 0.999);
            break;
        case ZERO:
            u = 0;
            break;
        case MAX_JACCARD2:
            u = getValue(metric, j, maxJ) * 2;
            //System.out.printf("MaxJ = %.4f @ %.3f\n", maxJ, u / 2);
            break;
        default:
            throw new RuntimeException("Missing upper limit method");
    }
    double min = getPercentile(h1, 0);
    double max = getPercentile(h1, 1);
    return new double[] { l, u, min, max };
}
Also used : DescriptiveStatistics(org.apache.commons.math3.stat.descriptive.DescriptiveStatistics) Percentile(org.apache.commons.math3.stat.descriptive.rank.Percentile) Plot(ij.gui.Plot) StoredDataStatistics(gdsc.core.utils.StoredDataStatistics) PlotWindow(ij.gui.PlotWindow) Plot2(ij.gui.Plot2) PolynomialSplineFunction(org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction) PeakResultPoint(gdsc.smlm.ij.plugins.ResultsMatchCalculator.PeakResultPoint) BasePoint(gdsc.core.match.BasePoint) LinearInterpolator(org.apache.commons.math3.analysis.interpolation.LinearInterpolator) OutOfRangeException(org.apache.commons.math3.exception.OutOfRangeException)

Example 4 with LinearInterpolator

use of org.apache.commons.math3.analysis.interpolation.LinearInterpolator in project GDSC-SMLM by aherbert.

the class BenchmarkFilterAnalysis method depthAnalysis.

/**
	 * Depth analysis.
	 *
	 * @param allAssignments
	 *            The assignments generated from running the filter (or null)
	 * @param filter
	 *            the filter
	 * @return the assignments
	 */
private ArrayList<FractionalAssignment[]> depthAnalysis(ArrayList<FractionalAssignment[]> allAssignments, DirectFilter filter) {
    if (!depthRecallAnalysis || simulationParameters.fixedDepth)
        return null;
    // Build a histogram of the number of spots at different depths
    final double[] depths = depthStats.getValues();
    double[] limits = Maths.limits(depths);
    //final int bins = Math.max(10, nActual / 100);
    //final int bins = Utils.getBinsSturges(depths.length);
    final int bins = Utils.getBinsSqrt(depths.length);
    double[][] h1 = Utils.calcHistogram(depths, limits[0], limits[1], bins);
    double[][] h2 = Utils.calcHistogram(depthFitStats.getValues(), limits[0], limits[1], bins);
    // manually to get the results that pass.
    if (allAssignments == null)
        allAssignments = getAssignments(filter);
    double[] depths2 = new double[results.size()];
    int count = 0;
    for (FractionalAssignment[] assignments : allAssignments) {
        if (assignments == null)
            continue;
        for (int i = 0; i < assignments.length; i++) {
            final CustomFractionalAssignment c = (CustomFractionalAssignment) assignments[i];
            depths2[count++] = c.peak.error;
        }
    }
    depths2 = Arrays.copyOf(depths2, count);
    // Build a histogram using the same limits
    double[][] h3 = Utils.calcHistogram(depths2, limits[0], limits[1], bins);
    // Convert pixel depth to nm
    for (int i = 0; i < h1[0].length; i++) h1[0][i] *= simulationParameters.a;
    limits[0] *= simulationParameters.a;
    limits[1] *= simulationParameters.a;
    // Produce a histogram of the number of spots at each depth
    String title1 = TITLE + " Depth Histogram";
    Plot2 plot1 = new Plot2(title1, "Depth (nm)", "Frequency");
    plot1.setLimits(limits[0], limits[1], 0, Maths.max(h1[1]));
    plot1.setColor(Color.black);
    plot1.addPoints(h1[0], h1[1], Plot2.BAR);
    plot1.addLabel(0, 0, "Black = Spots; Blue = Fitted; Red = Filtered");
    plot1.setColor(Color.blue);
    plot1.addPoints(h1[0], h2[1], Plot2.BAR);
    plot1.setColor(Color.red);
    plot1.addPoints(h1[0], h3[1], Plot2.BAR);
    plot1.setColor(Color.magenta);
    PlotWindow pw1 = Utils.display(title1, plot1);
    if (Utils.isNewWindow())
        wo.add(pw1);
    // Interpolate
    final double halfBinWidth = (h1[0][1] - h1[0][0]) * 0.5;
    // Remove final value of the histogram as this is at the upper limit of the range (i.e. count zero)
    h1[0] = Arrays.copyOf(h1[0], h1[0].length - 1);
    h1[1] = Arrays.copyOf(h1[1], h1[0].length);
    h2[1] = Arrays.copyOf(h2[1], h1[0].length);
    h3[1] = Arrays.copyOf(h3[1], h1[0].length);
    // TODO : Fix the smoothing since LOESS sometimes does not work.
    // Perhaps allow configuration of the number of histogram bins and the smoothing bandwidth.
    // Use minimum of 3 points for smoothing
    // Ensure we use at least x% of data
    double bandwidth = Math.max(3.0 / h1[0].length, 0.15);
    LoessInterpolator loess = new LoessInterpolator(bandwidth, 1);
    PolynomialSplineFunction spline1 = loess.interpolate(h1[0], h1[1]);
    PolynomialSplineFunction spline2 = loess.interpolate(h1[0], h2[1]);
    PolynomialSplineFunction spline3 = loess.interpolate(h1[0], h3[1]);
    // Use a second interpolator in case the LOESS fails
    LinearInterpolator lin = new LinearInterpolator();
    PolynomialSplineFunction spline1b = lin.interpolate(h1[0], h1[1]);
    PolynomialSplineFunction spline2b = lin.interpolate(h1[0], h2[1]);
    PolynomialSplineFunction spline3b = lin.interpolate(h1[0], h3[1]);
    // Increase the number of points to show a smooth curve
    double[] points = new double[bins * 5];
    limits = Maths.limits(h1[0]);
    final double interval = (limits[1] - limits[0]) / (points.length - 1);
    double[] v = new double[points.length];
    double[] v2 = new double[points.length];
    double[] v3 = new double[points.length];
    for (int i = 0; i < points.length - 1; i++) {
        points[i] = limits[0] + i * interval;
        v[i] = getSplineValue(spline1, spline1b, points[i]);
        v2[i] = getSplineValue(spline2, spline2b, points[i]);
        v3[i] = getSplineValue(spline3, spline3b, points[i]);
        points[i] += halfBinWidth;
    }
    // Final point on the limit of the spline range
    int ii = points.length - 1;
    v[ii] = getSplineValue(spline1, spline1b, limits[1]);
    v2[ii] = getSplineValue(spline2, spline2b, limits[1]);
    v3[ii] = getSplineValue(spline3, spline3b, limits[1]);
    points[ii] = limits[1] + halfBinWidth;
    // Calculate recall
    for (int i = 0; i < v.length; i++) {
        v2[i] = v2[i] / v[i];
        v3[i] = v3[i] / v[i];
    }
    final double halfSummaryDepth = summaryDepth * 0.5;
    String title2 = TITLE + " Depth Histogram (normalised)";
    Plot2 plot2 = new Plot2(title2, "Depth (nm)", "Recall");
    plot2.setLimits(limits[0] + halfBinWidth, limits[1] + halfBinWidth, 0, Maths.min(1, Maths.max(v2)));
    plot2.setColor(Color.black);
    plot2.addLabel(0, 0, "Blue = Fitted; Red = Filtered");
    plot2.setColor(Color.blue);
    plot2.addPoints(points, v2, Plot2.LINE);
    plot2.setColor(Color.red);
    plot2.addPoints(points, v3, Plot2.LINE);
    plot2.setColor(Color.magenta);
    if (-halfSummaryDepth - halfBinWidth >= limits[0]) {
        plot2.drawLine(-halfSummaryDepth, 0, -halfSummaryDepth, getSplineValue(spline3, spline3b, -halfSummaryDepth - halfBinWidth) / getSplineValue(spline1, spline1b, -halfSummaryDepth - halfBinWidth));
    }
    if (halfSummaryDepth - halfBinWidth <= limits[1]) {
        plot2.drawLine(halfSummaryDepth, 0, halfSummaryDepth, getSplineValue(spline3, spline3b, halfSummaryDepth - halfBinWidth) / getSplineValue(spline1, spline1b, halfSummaryDepth - halfBinWidth));
    }
    PlotWindow pw2 = Utils.display(title2, plot2);
    if (Utils.isNewWindow())
        wo.add(pw2);
    return allAssignments;
}
Also used : PlotWindow(ij.gui.PlotWindow) Plot2(ij.gui.Plot2) PolynomialSplineFunction(org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction) LoessInterpolator(org.apache.commons.math3.analysis.interpolation.LoessInterpolator) FractionalAssignment(gdsc.core.match.FractionalAssignment) PeakFractionalAssignment(gdsc.smlm.results.filter.PeakFractionalAssignment) LinearInterpolator(org.apache.commons.math3.analysis.interpolation.LinearInterpolator)

Example 5 with LinearInterpolator

use of org.apache.commons.math3.analysis.interpolation.LinearInterpolator in project mafscaling by vimsh.

the class BilinearInterpolator method interpolate.

/**
 * Method returns an interpolated/extrapolated value, based on
 * @param x, array of x values
 * @param y, array of y values
 * @param xi, is x value you want to interpolate at
 * @param type, interpolation method type
 * @return interpolated value
 * @throws Exception
 */
public static double interpolate(double[] x, double[] y, double xi, InterpolatorType type) throws Exception {
    UnivariateInterpolator interpolator = null;
    switch(type) {
        case AkimaCubicSpline:
            interpolator = new AkimaSplineInterpolator();
            break;
        case Linear:
            interpolator = new LinearInterpolator();
            break;
        case Regression:
            interpolator = new LoessInterpolator();
            break;
        case CubicSpline:
            interpolator = new SplineInterpolator();
            break;
        default:
            throw new Exception("Invalid interpolator type for this function");
    }
    UnivariateFunction function = interpolator.interpolate(x, y);
    PolynomialFunction[] polynomials = ((PolynomialSplineFunction) function).getPolynomials();
    if (xi > x[x.length - 1])
        return polynomials[polynomials.length - 1].value(xi - x[x.length - 2]);
    if (xi < x[0])
        return polynomials[0].value(xi - x[0]);
    return function.value(xi);
}
Also used : LoessInterpolator(org.apache.commons.math3.analysis.interpolation.LoessInterpolator) UnivariateFunction(org.apache.commons.math3.analysis.UnivariateFunction) LinearInterpolator(org.apache.commons.math3.analysis.interpolation.LinearInterpolator) AkimaSplineInterpolator(org.apache.commons.math3.analysis.interpolation.AkimaSplineInterpolator) SplineInterpolator(org.apache.commons.math3.analysis.interpolation.SplineInterpolator) PiecewiseBicubicSplineInterpolator(org.apache.commons.math3.analysis.interpolation.PiecewiseBicubicSplineInterpolator) AkimaSplineInterpolator(org.apache.commons.math3.analysis.interpolation.AkimaSplineInterpolator) PolynomialFunction(org.apache.commons.math3.analysis.polynomials.PolynomialFunction) UnivariateInterpolator(org.apache.commons.math3.analysis.interpolation.UnivariateInterpolator) PolynomialSplineFunction(org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction) NumberIsTooSmallException(org.apache.commons.math3.exception.NumberIsTooSmallException) OutOfRangeException(org.apache.commons.math3.exception.OutOfRangeException) NonMonotonicSequenceException(org.apache.commons.math3.exception.NonMonotonicSequenceException) DimensionMismatchException(org.apache.commons.math3.exception.DimensionMismatchException) NoDataException(org.apache.commons.math3.exception.NoDataException)

Aggregations

LinearInterpolator (org.apache.commons.math3.analysis.interpolation.LinearInterpolator)7 PolynomialSplineFunction (org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction)7 SplineInterpolator (org.apache.commons.math3.analysis.interpolation.SplineInterpolator)4 Plot (ij.gui.Plot)3 PlotWindow (ij.gui.PlotWindow)3 LoessInterpolator (org.apache.commons.math3.analysis.interpolation.LoessInterpolator)3 OutOfRangeException (org.apache.commons.math3.exception.OutOfRangeException)3 Plot2 (ij.gui.Plot2)2 Point (java.awt.Point)2 UnivariateFunction (org.apache.commons.math3.analysis.UnivariateFunction)2 UnivariateInterpolator (org.apache.commons.math3.analysis.interpolation.UnivariateInterpolator)2 DescriptiveStatistics (org.apache.commons.math3.stat.descriptive.DescriptiveStatistics)2 Percentile (org.apache.commons.math3.stat.descriptive.rank.Percentile)2 HistogramPlot (uk.ac.sussex.gdsc.core.ij.HistogramPlot)2 BasePoint (gdsc.core.match.BasePoint)1 FractionalAssignment (gdsc.core.match.FractionalAssignment)1 StoredDataStatistics (gdsc.core.utils.StoredDataStatistics)1 PeakResultPoint (gdsc.smlm.ij.plugins.ResultsMatchCalculator.PeakResultPoint)1 PeakFractionalAssignment (gdsc.smlm.results.filter.PeakFractionalAssignment)1 AkimaSplineInterpolator (org.apache.commons.math3.analysis.interpolation.AkimaSplineInterpolator)1