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

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

Introduction

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

Prototype

public LoessInterpolator() 

Source Link

Document

Constructs a new LoessInterpolator with a bandwidth of #DEFAULT_BANDWIDTH , #DEFAULT_ROBUSTNESS_ITERS robustness iterations and an accuracy of {#link #DEFAULT_ACCURACY}.

Usage

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);
    }/*from  ww  w  .  j a v a2s.co  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.LoessIntensityCorrectorTest.java

@Test
public void createSampleLoessFlat() throws Exception {
    DataManager dataManager = new DataManager();
    CSVDataLoader.loadFromCSVData(new File(getClass().getResource("flat-measure-nodrift").getFile()),
            dataManager);/*  w  w w.jav  a  2s. co 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: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./*  w w  w. jav 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:org.meteoinfo.math.interpolate.InterpUtil.java

/**
 * Make interpolation function/* ww  w.  j  a v  a2s  .co  m*/
 * @param x X data
 * @param y Y data
 * @param kind Specifies the kind of interpolation as a string (linear, 'spline').
 * @return Interpolation function
 */
public static UnivariateFunction getInterpFunc(Array x, Array y, String kind) {
    double[] xd = (double[]) ArrayUtil.copyToNDJavaArray(x);
    double[] yd = (double[]) ArrayUtil.copyToNDJavaArray(y);
    UnivariateInterpolator li;
    switch (kind) {
    case "spline":
    case "cubic":
        li = new SplineInterpolator();
        break;
    case "akima":
        li = new AkimaSplineInterpolator();
        break;
    case "divided":
        li = new DividedDifferenceInterpolator();
        break;
    case "loess":
        li = new LoessInterpolator();
        break;
    case "neville":
        li = new NevilleInterpolator();
        break;
    default:
        li = new LinearInterpolator();
        break;
    }
    UnivariateFunction psf = li.interpolate(xd, yd);

    return psf;
}