Search in sources :

Example 6 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];
    }
    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 7 with PolynomialSplineFunction

use of org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction 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 8 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
	 */
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 9 with PolynomialSplineFunction

use of org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction in project xDrip by NightscoutFoundation.

the class LibreAlarmReceiver method CalculateFromDataTransferObject.

public static void CalculateFromDataTransferObject(ReadingData.TransferObject object, boolean use_raw) {
    // insert any recent data we can
    final List<GlucoseData> mTrend = object.data.trend;
    if (mTrend != null) {
        Collections.sort(mTrend);
        final long thisSensorAge = mTrend.get(mTrend.size() - 1).sensorTime;
        sensorAge = Pref.getInt("nfc_sensor_age", 0);
        if (thisSensorAge > sensorAge) {
            sensorAge = thisSensorAge;
            Pref.setInt("nfc_sensor_age", (int) sensorAge);
            Pref.setBoolean("nfc_age_problem", false);
            Log.d(TAG, "Sensor age advanced to: " + thisSensorAge);
        } else if (thisSensorAge == sensorAge) {
            Log.wtf(TAG, "Sensor age has not advanced: " + sensorAge);
            JoH.static_toast_long("Sensor clock has not advanced!");
            Pref.setBoolean("nfc_age_problem", true);
            // do not try to insert again
            return;
        } else {
            Log.wtf(TAG, "Sensor age has gone backwards!!! " + sensorAge);
            JoH.static_toast_long("Sensor age has gone backwards!!");
            sensorAge = thisSensorAge;
            Pref.setInt("nfc_sensor_age", (int) sensorAge);
            Pref.setBoolean("nfc_age_problem", true);
        }
        if (d)
            Log.d(TAG, "Oldest cmp: " + JoH.dateTimeText(oldest_cmp) + " Newest cmp: " + JoH.dateTimeText(newest_cmp));
        long shiftx = 0;
        if (mTrend.size() > 0) {
            shiftx = getTimeShift(mTrend);
            if (shiftx != 0)
                Log.d(TAG, "Lag Timeshift: " + shiftx);
            for (GlucoseData gd : mTrend) {
                if (d)
                    Log.d(TAG, "DEBUG: sensor time: " + gd.sensorTime);
                if ((timeShiftNearest > 0) && ((timeShiftNearest - gd.realDate) < segmentation_timeslice) && (timeShiftNearest - gd.realDate != 0)) {
                    if (d)
                        Log.d(TAG, "Skipping record due to closeness: " + JoH.dateTimeText(gd.realDate));
                    continue;
                }
                if (use_raw) {
                    // not quick for recent
                    createBGfromGD(gd, false);
                } else {
                    BgReading.bgReadingInsertFromInt(gd.glucoseLevel, gd.realDate, true);
                }
            }
        } else {
            Log.e(TAG, "Trend data was empty!");
        }
        // munge and insert the history data if any is missing
        final List<GlucoseData> mHistory = object.data.history;
        if ((mHistory != null) && (mHistory.size() > 1)) {
            Collections.sort(mHistory);
            // applyTimeShift(mTrend, shiftx);
            final List<Double> polyxList = new ArrayList<Double>();
            final List<Double> polyyList = new ArrayList<Double>();
            for (GlucoseData gd : mHistory) {
                if (d)
                    Log.d(TAG, "history : " + JoH.dateTimeText(gd.realDate) + " " + gd.glucose(false));
                polyxList.add((double) gd.realDate);
                if (use_raw) {
                    polyyList.add((double) gd.glucoseLevelRaw);
                    createBGfromGD(gd, true);
                } else {
                    polyyList.add((double) gd.glucoseLevel);
                    // add in the actual value
                    BgReading.bgReadingInsertFromInt(gd.glucoseLevel, gd.realDate, false);
                }
            }
            // ConstrainedSplineInterpolator splineInterp = new ConstrainedSplineInterpolator();
            final SplineInterpolator splineInterp = new SplineInterpolator();
            try {
                PolynomialSplineFunction polySplineF = splineInterp.interpolate(Forecast.PolyTrendLine.toPrimitiveFromList(polyxList), Forecast.PolyTrendLine.toPrimitiveFromList(polyyList));
                final long startTime = mHistory.get(0).realDate;
                final long endTime = mHistory.get(mHistory.size() - 1).realDate;
                for (long ptime = startTime; ptime <= endTime; ptime += 300000) {
                    if (d)
                        Log.d(TAG, "Spline: " + JoH.dateTimeText((long) ptime) + " value: " + (int) polySplineF.value(ptime));
                    if (use_raw) {
                        createBGfromGD(new GlucoseData((int) polySplineF.value(ptime), ptime), true);
                    } else {
                        BgReading.bgReadingInsertFromInt((int) polySplineF.value(ptime), ptime, false);
                    }
                }
            } catch (org.apache.commons.math3.exception.NonMonotonicSequenceException e) {
                Log.e(TAG, "NonMonotonicSequenceException: " + e);
            }
        } else {
            Log.e(TAG, "no librealarm history data");
        }
    } else {
        Log.d(TAG, "Trend data is null!");
    }
}
Also used : ArrayList(java.util.ArrayList) SplineInterpolator(org.apache.commons.math3.analysis.interpolation.SplineInterpolator) PolynomialSplineFunction(org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction) GlucoseData(com.eveningoutpost.dexdrip.Models.GlucoseData)

Example 10 with PolynomialSplineFunction

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

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