Search in sources :

Example 6 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
 */
@Nullable
private ArrayList<FractionalAssignment[]> depthAnalysis(ArrayList<FractionalAssignment[]> allAssignments, DirectFilter filter) {
    if (!settings.depthRecallAnalysis || simulationParameters.fixedDepth) {
        return null;
    }
    // Build a histogram of the number of spots at different depths
    final double[] depths = fitResultData.depthStats.getValues();
    double[] limits = MathUtils.limits(depths);
    final int bins = HistogramPlot.getBinsSqrtRule(depths.length);
    final double[][] h1 = HistogramPlot.calcHistogram(depths, limits[0], limits[1], bins);
    final double[][] h2 = HistogramPlot.calcHistogram(fitResultData.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 (final 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.getZPosition();
        }
    }
    depths2 = Arrays.copyOf(depths2, count);
    // Build a histogram using the same limits
    final double[][] h3 = HistogramPlot.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.pixelPitch;
    }
    limits[0] *= simulationParameters.pixelPitch;
    limits[1] *= simulationParameters.pixelPitch;
    // Produce a histogram of the number of spots at each depth
    final String title1 = TITLE + " Depth Histogram";
    final Plot plot1 = new Plot(title1, "Depth (nm)", "Frequency");
    plot1.setLimits(limits[0], limits[1], 0, MathUtils.max(h1[1]));
    plot1.setColor(Color.black);
    plot1.addPoints(h1[0], h1[1], Plot.BAR);
    plot1.addLabel(0, 0, "Black = Spots; Blue = Fitted; Red = Filtered");
    plot1.setColor(Color.blue);
    plot1.addPoints(h1[0], h2[1], Plot.BAR);
    plot1.setColor(Color.red);
    plot1.addPoints(h1[0], h3[1], Plot.BAR);
    plot1.setColor(Color.magenta);
    ImageJUtils.display(title1, plot1, wo);
    // 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
    final double bandwidth = Math.max(3.0 / h1[0].length, 0.15);
    final LoessInterpolator loess = new LoessInterpolator(bandwidth, 1);
    final PolynomialSplineFunction spline1 = loess.interpolate(h1[0], h1[1]);
    final PolynomialSplineFunction spline2 = loess.interpolate(h1[0], h2[1]);
    final PolynomialSplineFunction spline3 = loess.interpolate(h1[0], h3[1]);
    // Use a second interpolator in case the LOESS fails
    final LinearInterpolator lin = new LinearInterpolator();
    final PolynomialSplineFunction spline1b = lin.interpolate(h1[0], h1[1]);
    final PolynomialSplineFunction spline2b = lin.interpolate(h1[0], h2[1]);
    final PolynomialSplineFunction spline3b = lin.interpolate(h1[0], h3[1]);
    // Increase the number of points to show a smooth curve
    final double[] points = new double[bins * 5];
    limits = MathUtils.limits(h1[0]);
    final double interval = (limits[1] - limits[0]) / (points.length - 1);
    final double[] v = new double[points.length];
    final double[] v2 = new double[points.length];
    final 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
    final 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 = settings.summaryDepth * 0.5;
    final String title2 = TITLE + " Depth Histogram (normalised)";
    final Plot plot2 = new Plot(title2, "Depth (nm)", "Recall");
    plot2.setLimits(limits[0] + halfBinWidth, limits[1] + halfBinWidth, 0, MathUtils.min(1, MathUtils.max(v2)));
    plot2.setColor(Color.black);
    plot2.addLabel(0, 0, "Blue = Fitted; Red = Filtered");
    plot2.setColor(Color.blue);
    plot2.addPoints(points, v2, Plot.LINE);
    plot2.setColor(Color.red);
    plot2.addPoints(points, v3, Plot.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));
    }
    ImageJUtils.display(title2, plot2, wo);
    return allAssignments;
}
Also used : LoessInterpolator(org.apache.commons.math3.analysis.interpolation.LoessInterpolator) PeakFractionalAssignment(uk.ac.sussex.gdsc.smlm.results.filter.PeakFractionalAssignment) FractionalAssignment(uk.ac.sussex.gdsc.core.match.FractionalAssignment) LinearInterpolator(org.apache.commons.math3.analysis.interpolation.LinearInterpolator) Plot(ij.gui.Plot) HistogramPlot(uk.ac.sussex.gdsc.core.ij.HistogramPlot) PolynomialSplineFunction(org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction) Nullable(uk.ac.sussex.gdsc.core.annotation.Nullable)

Example 7 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];
    }
    final double[][] values = extractValues(originalDriftTimePoints, startT, endT, dx, dy);
    PolynomialSplineFunction fx;
    PolynomialSplineFunction 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 8 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