Search in sources :

Example 11 with PolynomialSplineFunction

use of org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction in project triplea by triplea-game.

the class RouteTest method testCurve.

@Test
public void testCurve() {
    final double[] testYValues = new double[] { 20, 40, 90 };
    final PolynomialSplineFunction testFunction = new SplineInterpolator().interpolate(dummyIndex, testYValues);
    final double[] coords = spyRouteDrawer.getCoords(testFunction, dummyIndex);
    final double stepSize = testFunction.getKnots()[testFunction.getKnots().length - 1] / coords.length;
    assertEquals(testYValues[0] * stepSize, coords[(int) Math.round(dummyIndex[0])], 1);
    assertEquals(testYValues[1] * stepSize, coords[(int) Math.round(dummyIndex[1])], 1);
    assertEquals(testYValues[2] * stepSize, coords[(int) Math.round(dummyIndex[2])], 1);
// TODO change the calculation so that delta = 0;
}
Also used : SplineInterpolator(org.apache.commons.math3.analysis.interpolation.SplineInterpolator) PolynomialSplineFunction(org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction) Test(org.junit.jupiter.api.Test)

Example 12 with PolynomialSplineFunction

use of org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction in project GDSC-SMLM by aherbert.

the class TraceMolecules method interpolateZeroCrossingPoints.

@SuppressWarnings("unused")
private void interpolateZeroCrossingPoints() {
    final double[] x = new double[zeroCrossingPoints.size()];
    final double[] y = new double[zeroCrossingPoints.size()];
    for (int i = 0; i < x.length; i++) {
        final double[] point = zeroCrossingPoints.get(i);
        x[i] = point[0];
        y[i] = point[1];
    }
    final PolynomialSplineFunction fx = new SplineInterpolator().interpolate(x, y);
    double minX = x[0];
    final double maxX = x[x.length - 1];
    final double xinc = (maxX - minX) / 50;
    for (minX = minX + xinc; minX < maxX; minX += xinc) {
        zeroCrossingPoints.add(new double[] { minX, fx.value(minX) });
    }
    sortPoints();
}
Also used : SplineInterpolator(org.apache.commons.math3.analysis.interpolation.SplineInterpolator) PolynomialSplineFunction(org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction) ClusterPoint(uk.ac.sussex.gdsc.core.clustering.ClusterPoint)

Example 13 with PolynomialSplineFunction

use of org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction in project GDSC-SMLM by aherbert.

the class SpotAnalysis method interpolate.

private double[] interpolate(double[] xValues, double[] yValues) {
    // Smooth the values not in the current on-frames
    double[] newX = Arrays.copyOf(xValues, xValues.length);
    double[] newY = Arrays.copyOf(yValues, yValues.length);
    for (final Spot s : onFrames) {
        newX[s.frame - 1] = -1;
    }
    int count = 0;
    for (int i = 0; i < newX.length; i++) {
        if (newX[i] == -1) {
            continue;
        }
        newX[count] = newX[i];
        newY[count] = newY[i];
        count++;
    }
    newX = Arrays.copyOf(newX, count);
    newY = Arrays.copyOf(newY, count);
    double smoothing = 0.25;
    try {
        smoothing = Double.parseDouble(smoothingTextField.getText());
        if (smoothing < 0.01 || smoothing > 0.9) {
            smoothing = 0.25;
        }
    } catch (final NumberFormatException ex) {
    // Ignore
    }
    final LoessInterpolator loess = new LoessInterpolator(smoothing, 1);
    final PolynomialSplineFunction f = loess.interpolate(newX, newY);
    // Interpolate
    final double[] plotSmooth = new double[xValues.length];
    for (int i = 0; i < xValues.length; i++) {
        // Cannot interpolate outside the bounds of the input data
        if (xValues[i] < newX[0]) {
            plotSmooth[i] = newY[0];
        } else if (xValues[i] > newX[newX.length - 1]) {
            plotSmooth[i] = newY[newX.length - 1];
        } else {
            plotSmooth[i] = f.value(xValues[i]);
        }
    }
    return plotSmooth;
}
Also used : LoessInterpolator(org.apache.commons.math3.analysis.interpolation.LoessInterpolator) PolynomialSplineFunction(org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction) Point(java.awt.Point)

Example 14 with PolynomialSplineFunction

use of org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction 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 15 with PolynomialSplineFunction

use of org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction 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)

Aggregations

PolynomialSplineFunction (org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction)15 SplineInterpolator (org.apache.commons.math3.analysis.interpolation.SplineInterpolator)8 LinearInterpolator (org.apache.commons.math3.analysis.interpolation.LinearInterpolator)7 LoessInterpolator (org.apache.commons.math3.analysis.interpolation.LoessInterpolator)5 Point (java.awt.Point)4 Plot (ij.gui.Plot)3 PlotWindow (ij.gui.PlotWindow)3 OutOfRangeException (org.apache.commons.math3.exception.OutOfRangeException)3 GlucoseData (com.eveningoutpost.dexdrip.Models.GlucoseData)2 Plot2 (ij.gui.Plot2)2 ArrayList (java.util.ArrayList)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 ClusterPoint (gdsc.core.clustering.ClusterPoint)1 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