Example usage for org.apache.commons.math3.analysis.interpolation LoessInterpolator interpolate

List of usage examples for org.apache.commons.math3.analysis.interpolation LoessInterpolator interpolate

Introduction

In this page you can find the example usage for org.apache.commons.math3.analysis.interpolation LoessInterpolator interpolate.

Prototype

public final PolynomialSplineFunction interpolate(final double[] xval, final double[] yval)
        throws NonMonotonicSequenceException, DimensionMismatchException, NoDataException,
        NotFiniteNumberException, NumberIsTooSmallException 

Source Link

Document

Compute an interpolating function by performing a loess fit on the data at the original abscissae and then building a cubic spline with a org.apache.commons.math3.analysis.interpolation.SplineInterpolator on the resulting fit.

Usage

From source file:eu.tango.energymodeller.energypredictor.CpuOnlySplinePolynomialEnergyPredictor.java

/**
 * This calculates the mathematical function that predicts the power
 * consumption given the cpu utilisation.
 *
 * @param host The host to get the function for
 * @return The mathematical function that predicts the power consumption
 * given the cpu utilisation./*from ww w  .  ja v  a  2s .c  o m*/
 */
private PredictorFunction<PolynomialSplineFunction> retrieveModel(Host host) {
    PredictorFunction<PolynomialSplineFunction> answer;
    if (modelCache.containsKey(host)) {
        /**
         * A small cache avoids recalculating the regression so often.
         */
        return modelCache.get(host);
    }
    ArrayList<HostEnergyCalibrationData> dataSet = cleanData(host.getCalibrationData());
    double[] xval = new double[dataSet.size()];
    double[] yval = new double[dataSet.size()];
    int i = 0;
    for (HostEnergyCalibrationData data : dataSet) {
        xval[i] = data.getCpuUsage();
        yval[i] = data.getWattsUsed();
        i++;
    }
    LoessInterpolator fitter = new LoessInterpolator();
    PolynomialSplineFunction function = fitter.interpolate(xval, yval);
    double sse = getSumOfSquareError(function, xval, yval);
    double rmse = getRootMeanSquareError(sse, xval.length);
    answer = new PredictorFunction<>(function, sse, rmse);
    modelCache.put(host, answer);
    return answer;
}

From source file:jp.ac.tohoku.ecei.sb.metabolomeqc.basiccorrector.LoessIntensityCorrectorTest.java

@Test
public void createSampleLoessFlat() throws Exception {
    DataManager dataManager = new DataManager();
    CSVDataLoader.loadFromCSVData(new File(getClass().getResource("flat-measure-nodrift").getFile()),
            dataManager);/*  w ww . j ava2 s .  c o m*/

    File buildDir = new File(System.getProperty("user.dir"), "build");
    File testOutput = new File(buildDir, "test-data");
    testOutput.mkdirs();

    IntensityMatrixImpl intensityMatrix = new IntensityMatrixImpl(dataManager.getIntensityMatrix());
    List<Sample> globalQC = intensityMatrix.getGlobalQCSamples();
    if (globalQC.size() == 0)
        throw new UnsupportedOperationException("No Global QC");
    Sample selectedGlobalQC = globalQC.get(0);

    LoessInterpolator loessInterpolator = new LoessInterpolator();

    for (HashMap.Entry<Plate, List<Injection>> oneplate : intensityMatrix.getInjectionsByPlate().entrySet()) {
        for (Compound compound : intensityMatrix.getRowKeys()) {
            List<Double> xlist = new ArrayList<>();
            List<Double> ylist = new ArrayList<>();
            for (Injection injection : oneplate.getValue()) {
                if (injection.isIgnored())
                    continue;
                if (!injection.getSample().equals(selectedGlobalQC))
                    continue;
                xlist.add((double) injection.getRunIndex());
                ylist.add(intensityMatrix.get(compound, injection));
            }

            PolynomialSplineFunction fn = loessInterpolator.interpolate(
                    xlist.stream().mapToDouble(x -> x).toArray(), ylist.stream().mapToDouble(x -> x).toArray());
            for (Injection injection : oneplate.getValue()) {
                if (injection.getSample().getSampleType() != Sample.SampleType.NORMAL)
                    continue;
                intensityMatrix.put(compound, injection, fn.value(injection.getRunIndex()));
            }
        }
    }

    dataManager.setIntensityMatrix(intensityMatrix);
    CSVDataLoader.storeToCSVData(new File(testOutput, "loess-flat"), dataManager);
}

From source file:be.ugent.maf.cellmissy.analysis.singlecell.processing.impl.interpolation.TrackLoessInterpolator.java

@Override
public InterpolatedTrack interpolateTrack(double[] time, double[] x, double[] y) {
    // create a new interpolator
    LoessInterpolator loessInterpolator = new LoessInterpolator();
    int interpolationPoints = PropertiesConfigurationHolder.getInstance().getInt("numberOfInterpolationPoints");

    // create arrays to hold the interpolant time, the interpolated X and the interpolated Y
    double[] interpolantTime = new double[interpolationPoints];
    double[] interpolatedX = new double[interpolationPoints];
    double[] interpolatedY = new double[interpolationPoints];
    // the step used for the interpolation in both direction
    double interpolationStep = (time[time.length - 1] - time[0]) / interpolationPoints;

    // check for monotonicity
    boolean monotonic = MathArrays.isMonotonic(time, MathArrays.OrderDirection.INCREASING, false);
    // in case time is not monotonic, sort in place time, x and y coordinates
    if (!monotonic) {
        MathArrays.sortInPlace(time, x, y);
    }//www  .  j  a  v  a 2s  .c  o m

    // call the interpolator, and actually do the interpolation
    try {
        PolynomialSplineFunction functionX = loessInterpolator.interpolate(time, x);
        PolynomialSplineFunction functionY = loessInterpolator.interpolate(time, y);

        // get the polynomial functions in both directions
        PolynomialFunction polynomialFunctionX = functionX.getPolynomials()[0];
        PolynomialFunction polynomialFunctionY = functionY.getPolynomials()[0];

        for (int i = 0; i < interpolationPoints; i++) {
            interpolantTime[i] = time[0] + (i * interpolationStep);
            interpolatedX[i] = functionX.value(interpolantTime[i]);
            interpolatedY[i] = functionY.value(interpolantTime[i]);
        }

        for (int k = 0; k < interpolationPoints; k++) {
            if (Double.isNaN(interpolatedX[k]) | Double.isNaN(interpolatedY[k])) {
                return null;
            }
        }
        return new InterpolatedTrack(interpolantTime, interpolatedX, interpolatedY, polynomialFunctionX,
                polynomialFunctionY);
    } catch (NumberIsTooSmallException e) {
        LOG.error(e.getMessage());
        return null;
    }
}

From source file:jp.ac.tohoku.ecei.sb.metabolomeqc.basiccorrector.LoessIntensityCorrector.java

@Override
public IntensityMatrix doCorrection(IntensityMatrix original) {
    updateBadInjections(original);//from   ww  w.  ja va2 s  .c o  m
    List<Injection> normalSamples = original.getNormalInjections();

    List<Sample> globalQCSamples = original.getGlobalQCSamples();
    if (globalQCSamples.size() == 0)
        throw new UnsupportedOperationException("No global QC");
    Sample selectedGlobalQC = globalQCSamples.get(0);

    // Calculate SQC median
    Map<Compound, Double> compoundIntensityBase = GlobalQCMedianCalculator.calcGlobalQCMedian(original,
            badInjections);

    // Main Correction
    IntensityMatrix corrected = new IntensityMatrixImpl(original.getSize()[0], normalSamples.size());
    corrected.setRowKeys(original.getRowKeys());
    corrected.setColumnKeys(normalSamples);
    //corrected.getAttributes().putAll(original.getAttributes());

    LoessInterpolator loessInterpolator = new LoessInterpolator(bandwidth, robustnessIters, accuracy);

    for (Compound oneCompound : corrected.getRowKeys()) {
        double base = compoundIntensityBase.get(oneCompound);
        for (Map.Entry<Plate, List<Injection>> plateInjection : original.getInjectionsByPlate().entrySet()) {
            List<Double> xlist = new ArrayList<>();
            List<Double> ylist = new ArrayList<>();
            for (Injection injection : plateInjection.getValue()) {
                //log.info("injection: {}", injection);
                if (!injection.getSample().equals(selectedGlobalQC))
                    continue;
                if (injection.isIgnored())
                    continue;
                if (badInjections != null && badInjections.contains(injection))
                    continue;

                xlist.add((double) injection.getRunIndex());
                ylist.add(original.get(oneCompound, injection));
            }

            //log.info("X : {}", xlist.size());

            PolynomialSplineFunction fn = loessInterpolator.interpolate(
                    xlist.stream().mapToDouble(x -> x).toArray(), ylist.stream().mapToDouble(x -> x).toArray());

            for (Injection injection : plateInjection.getValue()) {
                if (!normalSamples.contains(injection))
                    continue;
                corrected.put(oneCompound, injection,
                        original.get(oneCompound, injection) - base + fn.value(injection.getRunIndex()));
            }
        }
    }

    return corrected;
}

From source file:MSUmpire.LCMSPeakStructure.LCMSPeakMS1.java

public void GenerateMassCalibrationRTMap() throws IOException {
    String pngfile = FilenameUtils.getFullPath(ScanCollectionName) + "/"
            + FilenameUtils.getBaseName(ScanCollectionName) + "_masscaliRT.png";
    XYSeries series = new XYSeries("PSM");
    XYSeriesCollection xySeriesCollection = new XYSeriesCollection();
    LoessInterpolator loessInterpolator = new LoessInterpolator(0.75, //bandwidth,
            2//robustnessIters
    );//from  ww  w.  j  a v  a 2 s.c om

    for (PSM psm : this.IDsummary.PSMList.values()) {
        float ppm = InstrumentParameter.CalcSignedPPM(psm.ObserPrecursorMass, psm.NeutralPepMass);
        series.add(new XYDataItem(psm.RetentionTime, ppm));
    }
    double x[] = new double[IDsummary.PSMList.size()];
    double y[] = new double[x.length];
    double currentmin = 0f;
    for (int i = 0; i < series.getItemCount(); i++) {
        x[i] = (double) series.getX(i);
        if (x[i] <= currentmin) {
            x[i] = currentmin + 0.0001f;
        }
        currentmin = x[i];
        y[i] = (double) series.getY(i);
    }

    Masscalibrationfunction = loessInterpolator.interpolate(x, y);
    XYSeries smoothline = new XYSeries("Loess Regression");

    double xvalue = x[0];

    while (xvalue < x[x.length - 1]) {
        smoothline.add(xvalue, Masscalibrationfunction.value(xvalue));
        xvalue += 0.05d;
    }
    xySeriesCollection.addSeries(smoothline);
    xySeriesCollection.addSeries(series);

    JFreeChart chart = ChartFactory.createScatterPlot("Mass calibration", "RT", "Mass error (ppm)",
            xySeriesCollection, PlotOrientation.VERTICAL, true, true, false);
    XYPlot xyPlot = (XYPlot) chart.getPlot();
    xyPlot.setDomainCrosshairVisible(true);
    xyPlot.setRangeCrosshairVisible(true);

    XYItemRenderer renderer = xyPlot.getRenderer();
    renderer.setSeriesPaint(1, Color.blue);
    renderer.setSeriesPaint(0, Color.BLACK);
    renderer.setSeriesShape(1, new Ellipse2D.Double(0, 0, 3, 3));
    renderer.setSeriesStroke(1, new BasicStroke(1.0f));
    xyPlot.setBackgroundPaint(Color.white);
    ChartUtilities.saveChartAsPNG(new File(pngfile), chart, 1000, 600);
}

From source file:gdsc.smlm.ij.plugins.BenchmarkFilterAnalysis.java

/**
 * @param filter// www.j  a v a 2s.com
 */
private void depthAnalysis(Filter filter) {
    // TODO : This analysis ignores the partial match distance.
    // Use the score for each result to get a weighted histogram. 

    if (!depthRecallAnalysis || simulationParameters.fixedDepth)
        return;

    // Build a histogram of the number of spots at different depths
    final double[] depths = depthStats.getValues();
    final double range = simulationParameters.depth / simulationParameters.a / 2;
    double[] limits = { -range, range };

    final int bins = Math.max(10, simulationParameters.molecules / 50);
    double[][] h = Utils.calcHistogram(depths, limits[0], limits[1], bins);
    double[][] h2 = Utils.calcHistogram(depthFitStats.getValues(), limits[0], limits[1], bins);

    // To get the number of TP at each depth will require that the filter is run 
    // manually to get the results that pass.
    MemoryPeakResults results = filter.filter(resultsList.get(0), failCount);

    double[] depths2 = new double[results.size()];
    int count = 0;
    for (PeakResult r : results.getResults()) {
        if (r.origValue != 0)
            depths2[count++] = ((DepthPeakResult) r).depth;
    }
    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 < h[0].length; i++)
        h[0][i] *= simulationParameters.a;
    limits[0] *= simulationParameters.a;
    limits[1] *= simulationParameters.a;

    // Produce a histogram of the number of spots at each depth
    String title = TITLE + " Depth Histogram";
    Plot2 plot = new Plot2(title, "Depth (nm)", "Frequency");
    plot.setLimits(limits[0], limits[1], 0, Maths.max(h[1]));
    plot.setColor(Color.black);
    plot.addPoints(h[0], h[1], Plot2.BAR);
    plot.setColor(Color.blue);
    plot.addPoints(h[0], h2[1], Plot2.BAR);
    plot.setColor(Color.red);
    plot.addPoints(h[0], h3[1], Plot2.BAR);
    plot.addLabel(0, 0, "Black = Spots; Blue = Fitted; Red = Filtered");
    PlotWindow pw = Utils.display(title, plot);

    // Interpolate
    final double halfBinWidth = (h[0][1] - h[0][0]) * 0.5;
    // Remove final value of the histogram as this is at the upper limit of the range (i.e. count zero)
    h[0] = Arrays.copyOf(h[0], h[0].length - 1);
    h[1] = Arrays.copyOf(h[1], h[0].length);
    h2[1] = Arrays.copyOf(h2[1], h[0].length);
    h3[1] = Arrays.copyOf(h3[1], h[0].length);

    // Use minimum of 3 points for smoothing
    // Ensure we use at least 15% of data
    double bandwidth = Math.max(3.0 / h[0].length, 0.15);
    LoessInterpolator l = new LoessInterpolator(bandwidth, 1);
    PolynomialSplineFunction spline = l.interpolate(h[0], h[1]);
    PolynomialSplineFunction spline2 = l.interpolate(h[0], h2[1]);
    PolynomialSplineFunction spline3 = l.interpolate(h[0], h3[1]);

    // Increase the number of points to show a smooth curve
    double[] points = new double[bins * 5];
    limits = Maths.limits(h[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] = spline.value(points[i]);
        v2[i] = spline2.value(points[i]);
        v3[i] = spline3.value(points[i]);
        points[i] += halfBinWidth;
    }
    // Final point on the limit of the spline range
    int ii = points.length - 1;
    v[ii] = spline.value(limits[1]);
    v2[ii] = spline2.value(limits[1]);
    v3[ii] = spline3.value(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];
    }

    title = TITLE + " Depth Histogram (normalised)";
    plot = new Plot2(title, "Depth (nm)", "Recall");
    plot.setLimits(limits[0] + halfBinWidth, limits[1] + halfBinWidth, 0, Maths.max(v2));
    plot.setColor(Color.blue);
    plot.addPoints(points, v2, Plot2.LINE);
    plot.setColor(Color.red);
    plot.addPoints(points, v3, Plot2.LINE);
    plot.addLabel(0, 0, "Blue = Fitted; Red = Filtered");
    PlotWindow pw2 = Utils.display(title, plot);
    if (Utils.isNewWindow()) {
        Point p = pw.getLocation();
        p.y += pw.getHeight();
        pw2.setLocation(p);
    }
}

From source file:gdsc.smlm.ij.plugins.SpotAnalysis.java

private double[] interpolate(double[] xValues2, 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 (Spot s : onFrames) {
        newX[s.frame - 1] = -1;/*w  w w.ja v  a 2  s . co m*/
    }
    int c = 0;
    for (int i = 0; i < newX.length; i++) {
        if (newX[i] == -1)
            continue;
        newX[c] = newX[i];
        newY[c] = newY[i];
        c++;
    }
    newX = Arrays.copyOf(newX, c);
    newY = Arrays.copyOf(newY, c);
    double smoothing = 0.25;
    try {
        smoothing = Double.parseDouble(smoothingTextField.getText());
        if (smoothing < 0.01 || smoothing > 0.9)
            smoothing = 0.25;
    } catch (NumberFormatException e) {
    }
    LoessInterpolator loess = new LoessInterpolator(smoothing, 1);
    PolynomialSplineFunction f = loess.interpolate(newX, newY);

    // Interpolate
    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;
}