Example usage for org.jfree.data.xy XYSeries getItemCount

List of usage examples for org.jfree.data.xy XYSeries getItemCount

Introduction

In this page you can find the example usage for org.jfree.data.xy XYSeries getItemCount.

Prototype

@Override
public int getItemCount() 

Source Link

Document

Returns the number of items in the series.

Usage

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

/**
 * Returns the average of the ys in a XYSeries
 * @param data input data/*ww w.j a v  a2 s . c  o m*/
 * @return y average
 */
public static double getYAvg(XYSeries data) {
    double avg = 0;
    for (int i = 0; i < data.getItemCount(); i++) {
        avg += data.getY(i).doubleValue();
    }
    avg = avg / data.getItemCount();
    return avg;
}

From source file:org.micromanager.saim.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  ww  w . j a v  a 2s  .  c  o m
 * 
 * Various weightmethods are implemented and can be selected using the 
 * weightMethods parameter.
 * 
 * @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
 * @param weightMethod One of the methods in the WeightMethod enum
        
 * @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, WeightMethod weightMethod) {

    if (type == FunctionType.NoFit) {
        return null;
    }
    // create the commons math data object from the JFreeChart data object
    final WeightedObservedPoints obs = new WeightedObservedPoints();
    // range is used in weigt calculations
    double range = data.getMaxY() - data.getMinY();
    for (int i = 0; i < data.getItemCount(); i++) {
        // add weight based on y intensity and selected weight method
        double weight = 1.0; // used in Equal method
        if (weightMethod != WeightMethod.Equal) {
            double valueMinusMin = data.getY(i).doubleValue() - data.getMinY();
            weight = valueMinusMin / range;
            switch (weightMethod) {
            case Equal:
                break; // weight is already linear
            case Quadratic:
                weight *= weight;
                break;
            case Top50Linear:
                if (valueMinusMin < (0.5 * range))
                    weight = 0.0;
                break;
            case Top80Linear:
                if (valueMinusMin < (0.8 * range))
                    weight = 0.0;
                break;
            }
        }

        obs.add(weight, data.getX(i).doubleValue(), data.getY(i).doubleValue());
    }

    // Carry out the actual fit
    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:
        GaussianWithOffsetCurveFitter gf = GaussianWithOffsetCurveFitter.create();
        gf = gf.withMaxIterations(50);
        if (guess != null) {
            gf.withStartPoint(guess);
        }
        result = gf.fit(obs.toList());
    }

    return result;
}

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

/**
 * Given a JFreeChart dataset and a commons math function, return a JFreeChart
 * dataset in which the original x values are now accompanied by the y values
 * predicted by the function//from  w w w. ja  va  2s.  co  m
 * 
 * @param data input JFreeChart data set
 * @param type one of the Fitter.FunctionType predefined functions
 * @param parms parameters describing the function.  These need to match the
 *             selected function or an IllegalArgumentEception will be thrown
 * 
 * @return JFreeChart dataset with original x values and fitted y values.
 */
public static XYSeries getFittedSeries(XYSeries data, FunctionType type, double[] parms) {

    XYSeries result = new XYSeries((String) data.getKey() + "-Fit", false, true);
    double minRange = data.getMinX();
    double maxRange = data.getMaxX();
    double xStep = (maxRange - minRange) / (data.getItemCount() * 10);
    switch (type) {
    case NoFit: {
        try {
            XYSeries resCopy = data.createCopy(0, data.getItemCount() - 1);
            return resCopy;
        } catch (CloneNotSupportedException ex) {
            return null;
        }
    }
    case Pol1:
    case Pol2:
    case Pol3:
        checkParms(type, parms);
        PolynomialFunction polFunction = new PolynomialFunction(parms);
        for (int i = 0; i < data.getItemCount() * 10; i++) {
            double x = minRange + i * xStep;
            double y = polFunction.value(x);
            result.add(x, y);
        }
        break;
    case Gaussian:
        checkParms(type, parms);
        Gaussian.Parametric gf = new Gaussian.Parametric();
        for (int i = 0; i < data.getItemCount() * 10; i++) {
            double x = minRange + i * xStep;
            double[] gparms = new double[3];
            System.arraycopy(parms, 0, gparms, 0, 3);
            double y = gf.value(x, gparms) + parms[3];
            result.add(x, y);
        }
        break;
    }

    return result;
}