Example usage for org.apache.commons.math3.fitting WeightedObservedPoints WeightedObservedPoints

List of usage examples for org.apache.commons.math3.fitting WeightedObservedPoints WeightedObservedPoints

Introduction

In this page you can find the example usage for org.apache.commons.math3.fitting WeightedObservedPoints WeightedObservedPoints.

Prototype

WeightedObservedPoints

Source Link

Usage

From source file:eu.tango.energymodeller.energypredictor.CpuAndAcceleratorEnergyPredictor.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  .j  a  va  2s .com
 */
private PredictorFunction<PolynomialFunction> retrieveCpuModel(Host host) {
    PredictorFunction<PolynomialFunction> answer;
    if (modelCache.containsKey(host)) {
        /**
         * A small cache avoids recalculating the regression so often.
         */
        return modelCache.get(host);
    }
    WeightedObservedPoints points = new WeightedObservedPoints();
    for (HostEnergyCalibrationData data : host.getCalibrationData()) {
        points.add(data.getCpuUsage(), data.getWattsUsed());
    }
    PolynomialCurveFitter fitter = PolynomialCurveFitter.create(2);
    final double[] best = fitter.fit(points.toList());
    PolynomialFunction function = new PolynomialFunction(best);
    double sse = getSumOfSquareError(function, points.toList());
    double rmse = getRootMeanSquareError(sse, points.toList().size());
    answer = new PredictorFunction<>(function, sse, rmse);
    modelCache.put(host, answer);
    return answer;
}

From source file:eu.tango.energymodeller.energypredictor.CpuAndBiModalAcceleratorEnergyPredictor.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 .  j av a 2 s  .  com*/
 */
private PredictorFunction<GroupingFunction> retrieveAcceleratorModel(Host host, String accelerator) {
    PredictorFunction<GroupingFunction> answer;
    if (modelAcceleratorCache.containsKey(host)) {
        /**
         * A small cache avoids recalculating the regression so often.
         */
        return modelAcceleratorCache.get(host);
    }
    WeightedObservedPoints points = new WeightedObservedPoints();
    for (Accelerator acc : host.getAccelerators()) {
        for (HostAcceleratorCalibrationData data : acc.getAcceleratorCalibrationData()) {
            if (data.getIdentifier().equals(accelerator)) {
                points.add(data.getParameter("clocks.current.sm [MHz]"), data.getPower());
            }
        }
    }
    GroupingFunction function = new GroupingFunction();
    function.fit(points);
    double sse = getSumOfSquareError(function, points.toList());
    double rmse = getRootMeanSquareError(sse, points.toList().size());
    answer = new PredictorFunction<>(function, sse, rmse);
    modelAcceleratorCache.put(host, answer);
    return answer;
}

From source file:org.akvo.caddisfly.sensor.colorimetry.strip.calibration.CalibrationCard.java

@NonNull
private static Mat do1D_3DCorrection(@NonNull Mat imgMat, @Nullable CalibrationData calData)
        throws CalibrationException {

    if (calData == null) {
        throw new CalibrationException("no calibration data.");
    }/*ww w .  j  a  v  a2  s .  c o m*/

    final WeightedObservedPoints obsL = new WeightedObservedPoints();
    final WeightedObservedPoints obsA = new WeightedObservedPoints();
    final WeightedObservedPoints obsB = new WeightedObservedPoints();

    Map<String, double[]> calResultIllumination = new HashMap<>();
    // iterate over all patches
    try {
        for (String label : calData.getCalValues().keySet()) {
            CalibrationData.CalValue cal = calData.getCalValues().get(label);
            CalibrationData.Location loc = calData.getLocations().get(label);
            float[] LAB_color = measurePatch(imgMat, loc.x, loc.y, calData); // measure patch color
            obsL.add(LAB_color[0], cal.getL());
            obsA.add(LAB_color[1], cal.getA());
            obsB.add(LAB_color[2], cal.getB());
            calResultIllumination.put(label, new double[] { LAB_color[0], LAB_color[1], LAB_color[2] });
        }
    } catch (Exception e) {
        throw new CalibrationException("1D calibration: error iterating over all patches.", e);
    }

    // Instantiate a second-degree polynomial fitter.
    final PolynomialCurveFitter fitter = PolynomialCurveFitter.create(2);

    // Retrieve fitted parameters (coefficients of the polynomial function).
    // order of coefficients is (c + bx + ax^2), so [c,b,a]
    try {
        final double[] coefficientL = fitter.fit(obsL.toList());
        final double[] coefficientA = fitter.fit(obsA.toList());
        final double[] coefficientB = fitter.fit(obsB.toList());

        double[] valIllumination;
        double L_orig, A_orig, B_orig, L_new, A_new, B_new;

        // transform patch values using the 1d calibration results
        Map<String, double[]> calResult1D = new HashMap<>();
        for (String label : calData.getCalValues().keySet()) {
            valIllumination = calResultIllumination.get(label);

            L_orig = valIllumination[0];
            A_orig = valIllumination[1];
            B_orig = valIllumination[2];

            L_new = coefficientL[2] * L_orig * L_orig + coefficientL[1] * L_orig + coefficientL[0];
            A_new = coefficientA[2] * A_orig * A_orig + coefficientA[1] * A_orig + coefficientA[0];
            B_new = coefficientB[2] * B_orig * B_orig + coefficientB[1] * B_orig + coefficientB[0];

            calResult1D.put(label, new double[] { L_new, A_new, B_new });
        }

        // use the 1D calibration result for the second calibration step
        // Following http://docs.scipy.org/doc/scipy/reference/tutorial/linalg.html#solving-linear-least-squares-problems-and-pseudo-inverses
        // we will solve P = M x
        int total = calData.getLocations().keySet().size();
        RealMatrix coefficient = new Array2DRowRealMatrix(total, 3);
        RealMatrix cal = new Array2DRowRealMatrix(total, 3);
        int index = 0;

        // create coefficient and calibration vectors
        for (String label : calData.getCalValues().keySet()) {
            CalibrationData.CalValue calv = calData.getCalValues().get(label);
            double[] cal1dResult = calResult1D.get(label);
            coefficient.setEntry(index, 0, cal1dResult[0]);
            coefficient.setEntry(index, 1, cal1dResult[1]);
            coefficient.setEntry(index, 2, cal1dResult[2]);

            cal.setEntry(index, 0, calv.getL());
            cal.setEntry(index, 1, calv.getA());
            cal.setEntry(index, 2, calv.getB());
            index++;
        }

        DecompositionSolver solver = new SingularValueDecomposition(coefficient).getSolver();
        RealMatrix sol = solver.solve(cal);

        float a_L, b_L, c_L, a_A, b_A, c_A, a_B, b_B, c_B;
        a_L = (float) sol.getEntry(0, 0);
        b_L = (float) sol.getEntry(1, 0);
        c_L = (float) sol.getEntry(2, 0);
        a_A = (float) sol.getEntry(0, 1);
        b_A = (float) sol.getEntry(1, 1);
        c_A = (float) sol.getEntry(2, 1);
        a_B = (float) sol.getEntry(0, 2);
        b_B = (float) sol.getEntry(1, 2);
        c_B = (float) sol.getEntry(2, 2);

        //use the solution to correct the image
        double L_temp, A_temp, B_temp, L_mid, A_mid, B_mid;
        int L_fin, A_fin, B_fin;
        int ii3;
        byte[] temp = new byte[imgMat.cols() * imgMat.channels()];
        for (int i = 0; i < imgMat.rows(); i++) { // y
            imgMat.get(i, 0, temp);
            ii3 = 0;
            for (int ii = 0; ii < imgMat.cols(); ii++) { //x
                L_temp = temp[ii3] & 0xFF;
                A_temp = temp[ii3 + 1] & 0xFF;
                B_temp = temp[ii3 + 2] & 0xFF;

                L_mid = coefficientL[2] * L_temp * L_temp + coefficientL[1] * L_temp + coefficientL[0];
                A_mid = coefficientA[2] * A_temp * A_temp + coefficientA[1] * A_temp + coefficientA[0];
                B_mid = coefficientB[2] * B_temp * B_temp + coefficientB[1] * B_temp + coefficientB[0];

                L_fin = (int) Math.round(a_L * L_mid + b_L * A_mid + c_L * B_mid);
                A_fin = (int) Math.round(a_A * L_mid + b_A * A_mid + c_A * B_mid);
                B_fin = (int) Math.round(a_B * L_mid + b_B * A_mid + c_B * B_mid);

                // cap values
                L_fin = capValue(L_fin, 0, 255);
                A_fin = capValue(A_fin, 0, 255);
                B_fin = capValue(B_fin, 0, 255);

                temp[ii3] = (byte) L_fin;
                temp[ii3 + 1] = (byte) A_fin;
                temp[ii3 + 2] = (byte) B_fin;

                ii3 += 3;
            }
            imgMat.put(i, 0, temp);
        }

        return imgMat;
    } catch (Exception e) {
        throw new CalibrationException("error while performing calibration: ", e);
    }
}

From source file:org.akvo.caddisfly.sensor.colorimetry.strip.util.OpenCVUtil.java

@SuppressWarnings("UnusedParameters")
public static Mat detectStrip(Mat stripArea, StripTest.Brand brand, double ratioW, double ratioH) {
    List<Mat> channels = new ArrayList<>();
    Mat sArea = stripArea.clone();//from  ww w .  ja  v a2s.c o m

    // Gaussian blur
    Imgproc.medianBlur(sArea, sArea, 3);
    Core.split(sArea, channels);

    // create binary image
    Mat binary = new Mat();

    // determine min and max NOT USED
    Imgproc.threshold(channels.get(0), binary, 128, MAX_RGB_INT_VALUE, Imgproc.THRESH_BINARY);

    // compute first approximation of line through length of the strip
    final WeightedObservedPoints points = new WeightedObservedPoints();
    final WeightedObservedPoints corrPoints = new WeightedObservedPoints();

    double tot, yTot;
    for (int i = 0; i < binary.cols(); i++) { // iterate over cols
        tot = 0;
        yTot = 0;
        for (int j = 0; j < binary.rows(); j++) { // iterate over rows
            if (binary.get(j, i)[0] > 128) {
                yTot += j;
                tot++;
            }
        }
        if (tot > 0) {
            points.add((double) i, yTot / tot);
        }
    }

    // order of coefficients is (b + ax), so [b, a]
    final PolynomialCurveFitter fitter = PolynomialCurveFitter.create(1);
    List<WeightedObservedPoint> pointsList = points.toList();
    final double[] coefficient = fitter.fit(pointsList);

    // second pass, remove outliers
    double estimate, actual;

    for (int i = 0; i < pointsList.size(); i++) {
        estimate = coefficient[1] * pointsList.get(i).getX() + coefficient[0];
        actual = pointsList.get(i).getY();
        if (actual > LOWER_PERCENTAGE_BOUND * estimate && actual < UPPER_PERCENTAGE_BOUND * estimate) {
            //if the point differs less than +/- 10%, keep the point
            corrPoints.add(pointsList.get(i).getX(), pointsList.get(i).getY());
        }
    }

    final double[] coefficientCorr = fitter.fit(corrPoints.toList());
    double slope = coefficientCorr[1];
    double offset = coefficientCorr[0];

    // compute rotation angle
    double rotAngleDeg = Math.atan(slope) * 180 / Math.PI;

    //determine a point on the line, in the middle of strip, in the horizontal middle of the whole image
    int midPointX = binary.cols() / 2;
    int midPointY = (int) Math.round(midPointX * slope + offset);

    // rotate around the midpoint, to straighten the binary strip
    Mat dstBinary = new Mat(binary.rows(), binary.cols(), binary.type());
    Point center = new Point(midPointX, midPointY);
    Mat rotMat = Imgproc.getRotationMatrix2D(center, rotAngleDeg, 1.0);
    Imgproc.warpAffine(binary, dstBinary, rotMat, binary.size(),
            Imgproc.INTER_CUBIC + Imgproc.WARP_FILL_OUTLIERS);

    // also apply rotation to colored strip
    Mat dstStrip = new Mat(stripArea.rows(), stripArea.cols(), stripArea.type());
    Imgproc.warpAffine(stripArea, dstStrip, rotMat, binary.size(),
            Imgproc.INTER_CUBIC + Imgproc.WARP_FILL_OUTLIERS);

    // Compute white points in each row
    double[] rowCount = new double[dstBinary.rows()];
    int rowTot;
    for (int i = 0; i < dstBinary.rows(); i++) { // iterate over rows
        rowTot = 0;
        for (int j = 0; j < dstBinary.cols(); j++) { // iterate over cols
            if (dstBinary.get(i, j)[0] > 128) {
                rowTot++;
            }
        }
        rowCount[i] = rowTot;
    }

    // find width by finding rising and dropping edges
    // rising edge  = largest positive difference
    // falling edge = largest negative difference
    int risePos = 0;
    int fallPos = 0;
    double riseVal = 0;
    double fallVal = 0;
    for (int i = 0; i < dstBinary.rows() - 1; i++) {
        if (rowCount[i + 1] - rowCount[i] > riseVal) {
            riseVal = rowCount[i + 1] - rowCount[i];
            risePos = i + 1;
        }
        if (rowCount[i + 1] - rowCount[i] < fallVal) {
            fallVal = rowCount[i + 1] - rowCount[i];
            fallPos = i;
        }
    }

    // cut out binary strip
    Point stripTopLeft = new Point(0, risePos);
    Point stripBottomRight = new Point(dstBinary.cols(), fallPos);

    org.opencv.core.Rect stripAreaRect = new org.opencv.core.Rect(stripTopLeft, stripBottomRight);
    Mat binaryStrip = dstBinary.submat(stripAreaRect);

    // also cut out colored strip
    Mat colorStrip = dstStrip.submat(stripAreaRect);

    // now right end of strip
    // method: first rising edge

    double[] colCount = new double[binaryStrip.cols()];
    int colTotal;
    for (int i = 0; i < binaryStrip.cols(); i++) { // iterate over cols
        colTotal = 0;
        for (int j = 0; j < binaryStrip.rows(); j++) { // iterate over rows
            if (binaryStrip.get(j, i)[0] > 128) {
                colTotal++;
            }
        }

        //Log.d("Caddisfly", String.valueOf(colTotal));
        colCount[i] = colTotal;
    }

    stripAreaRect = getStripRectangle(binaryStrip, colCount, brand.getStripLength(), ratioW);

    Mat resultStrip = colorStrip.submat(stripAreaRect).clone();

    // release Mat objects
    stripArea.release();
    sArea.release();
    binary.release();
    dstBinary.release();
    dstStrip.release();
    binaryStrip.release();
    colorStrip.release();

    return resultStrip;
}

From source file:org.apache.solr.client.solrj.io.eval.HarmonicFitEvaluator.java

@Override
public Object doWork(Object... objects) throws IOException {

    if (objects.length > 3) {
        throw new IOException("harmonicFit function takes a maximum of 2 arguments.");
    }/* w w w .  j  a  v a  2 s  . com*/

    Object first = objects[0];

    double[] x = null;
    double[] y = null;

    if (objects.length == 1) {
        //Only the y values passed

        y = ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();
        x = new double[y.length];
        for (int i = 0; i < y.length; i++) {
            x[i] = i;
        }

    } else if (objects.length == 2) {
        // x and y passed
        Object second = objects[1];
        x = ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();
        y = ((List) second).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();

    }

    HarmonicCurveFitter curveFitter = HarmonicCurveFitter.create();

    WeightedObservedPoints points = new WeightedObservedPoints();
    for (int i = 0; i < x.length; i++) {
        points.add(x[i], y[i]);
    }

    double[] coef = curveFitter.fit(points.toList());
    HarmonicOscillator pf = new HarmonicOscillator(coef[0], coef[1], coef[2]);

    List list = new ArrayList();
    for (double xvalue : x) {
        double yvalue = pf.value(xvalue);
        list.add(yvalue);
    }

    return list;
}

From source file:org.apache.solr.client.solrj.io.eval.PolyFitDerivativeEvaluator.java

@Override
public Object doWork(Object... objects) throws IOException {

    if (objects.length > 3) {
        throw new IOException("polyfitDerivative function takes a maximum of 3 arguments.");
    }//from w  w  w. j  av a2s .  c  om

    Object first = objects[0];

    double[] x = null;
    double[] y = null;
    int degree = 3;

    if (objects.length == 1) {
        //Only the y values passed

        y = ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();
        x = new double[y.length];
        for (int i = 0; i < y.length; i++) {
            x[i] = i;
        }

    } else if (objects.length == 3) {
        // x, y and degree passed

        Object second = objects[1];
        x = ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();
        y = ((List) second).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();
        degree = ((Number) objects[2]).intValue();
    } else if (objects.length == 2) {
        if (objects[1] instanceof List) {
            // x and y passed
            Object second = objects[1];
            x = ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();
            y = ((List) second).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();
        } else {
            // y and degree passed
            y = ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();
            x = new double[y.length];
            for (int i = 0; i < y.length; i++) {
                x[i] = i;
            }

            degree = ((Number) objects[1]).intValue();
        }
    }

    PolynomialCurveFitter curveFitter = PolynomialCurveFitter.create(degree);
    WeightedObservedPoints points = new WeightedObservedPoints();
    for (int i = 0; i < x.length; i++) {
        points.add(x[i], y[i]);
    }

    double[] coef = curveFitter.fit(points.toList());
    PolynomialFunction pf = new PolynomialFunction(coef);
    UnivariateFunction univariateFunction = pf.derivative();

    List list = new ArrayList();
    for (double xvalue : x) {
        double yvalue = univariateFunction.value(xvalue);
        list.add(yvalue);
    }

    return list;
}

From source file:org.apache.solr.client.solrj.io.eval.PolyFitEvaluator.java

@Override
public Object doWork(Object... objects) throws IOException {

    if (objects.length > 3) {
        throw new IOException("polyfit function takes a maximum of 3 arguments.");
    }//  w  w w  .  ja v  a2s.c  o m

    Object first = objects[0];

    double[] x = null;
    double[] y = null;
    int degree = 3;

    if (objects.length == 1) {
        //Only the y values passed

        y = ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();
        x = new double[y.length];
        for (int i = 0; i < y.length; i++) {
            x[i] = i;
        }

    } else if (objects.length == 3) {
        // x, y and degree passed

        Object second = objects[1];
        x = ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();
        y = ((List) second).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();
        degree = ((Number) objects[2]).intValue();
    } else if (objects.length == 2) {
        if (objects[1] instanceof List) {
            // x and y passed
            Object second = objects[1];
            x = ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();
            y = ((List) second).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();
        } else {
            // y and degree passed
            y = ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();
            x = new double[y.length];
            for (int i = 0; i < y.length; i++) {
                x[i] = i;
            }

            degree = ((Number) objects[1]).intValue();
        }
    }

    PolynomialCurveFitter curveFitter = PolynomialCurveFitter.create(degree);
    WeightedObservedPoints points = new WeightedObservedPoints();
    for (int i = 0; i < x.length; i++) {
        points.add(x[i], y[i]);
    }

    double[] coef = curveFitter.fit(points.toList());
    PolynomialFunction pf = new PolynomialFunction(coef);

    List list = new ArrayList();
    for (double xvalue : x) {
        double yvalue = pf.value(xvalue);
        list.add(yvalue);
    }

    return list;
}

From source file:org.autobet.ai.TeamRatersStatsApproximation.java

private Polynomial approximate(TeamRaterStats teamRaterStats, GameResult gameResult) {
    Map<Integer, RateStats> stats = teamRaterStats.getHomeStats();
    List<Integer> rates = teamRaterStats.getRates();
    final WeightedObservedPoints obs = new WeightedObservedPoints();

    for (int rate : rates) {
        RateStats rateStats = stats.get(rate);
        int stat = rateStats.get(gameResult);
        int count = rateStats.getCount();
        obs.add(count, rate, (double) stat / count);
    }/*from w ww. j av  a 2  s . co  m*/
    return new Polynomial(fitter.fit(obs.toList()));
}

From source file:org.bonej.wrapperPlugins.FractalDimensionWrapper.java

private WeightedObservedPoints toWeightedObservedPoints(
        final Iterable<ValuePair<DoubleType, DoubleType>> pairs) {
    final WeightedObservedPoints points = new WeightedObservedPoints();
    pairs.forEach(pair -> points.add(pair.a.get(), pair.b.get()));
    return points;
}

From source file:org.micromanager.asidispim.fit.Fitter.java

/**
 * Utility to facilitate fitting data plotted in JFreeChart
 * Provide data in JFReeChart format (XYSeries), and retrieve univariate
 * function parameters that best fit (using least squares) the data. All data
 * points will be weighted equally.//from   w  w  w .  j av a2 s.c  o  m
 * 
 * TODO: investigate whether weighting (possibly automatic weighting) can
 * improve accuracy
 * 
 * @param data xy series in JFReeChart format
 * @param type one of the Fitter.FunctionType predefined functions
 * @param guess initial guess for the fit.  The number and meaning of these
     parameters depends on the FunctionType.  Implemented:
     Gaussian: 0: Normalization, 1: Mean 2: Sigma
        
 * @return array with parameters, whose meaning depends on the FunctionType.
 *          Use the function getXYSeries to retrieve the XYDataset predicted 
 *          by this fit
 */
public static double[] fit(XYSeries data, FunctionType type, double[] guess) {

    if (type == FunctionType.NoFit) {
        return null;
    }
    // create the commons math data object from the JFreeChart data object
    final WeightedObservedPoints obs = new WeightedObservedPoints();
    for (int i = 0; i < data.getItemCount(); i++) {
        obs.add(1.0, data.getX(i).doubleValue(), data.getY(i).doubleValue());
    }

    double[] result = null;
    switch (type) {
    case Pol1:
        final PolynomialCurveFitter fitter1 = PolynomialCurveFitter.create(1);
        result = fitter1.fit(obs.toList());
        break;
    case Pol2:
        final PolynomialCurveFitter fitter2 = PolynomialCurveFitter.create(2);
        result = fitter2.fit(obs.toList());
        break;
    case Pol3:
        final PolynomialCurveFitter fitter3 = PolynomialCurveFitter.create(3);
        result = fitter3.fit(obs.toList());
        break;
    case Gaussian:
        final GaussianWithOffsetCurveFitter gf = GaussianWithOffsetCurveFitter.create();
        if (guess != null) {
            gf.withStartPoint(guess);
        }
        result = gf.fit(obs.toList());
    default:
        break;
    }

    return result;
}