Java Code Examples for org.apache.commons.math3.fitting.WeightedObservedPoints#add()

The following examples show how to use org.apache.commons.math3.fitting.WeightedObservedPoints#add() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HistogramChartFactory.java    From mzmine3 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Performs Gaussian fit on XYSeries
 * 
 * @param data the data
 * @param gMin lower bound of Gaussian fit
 * @param gMax upper bound of Gaussian fit
 * @param sigDigits number of significant digits
 * @return double[] {normFactor, mean, sigma} as a result of
 *         GaussianCurveFitter.create().fit(obs.toList())
 */
public static double[] gaussianFit(List<DataPoint> data, double gMin, double gMax) {
  // gaussian fit
  WeightedObservedPoints obs = new WeightedObservedPoints();

  for (int i = 0; i < data.size(); i++) {
    double x = data.get(i).getMZ();
    if (x >= gMin && x <= gMax)
      obs.add(x, data.get(i).getIntensity());
  }
  try {
    return fitter.fit(obs.toList());
  } catch (Exception e) {
    e.printStackTrace();
    logger.error("Cannot fit Gaussian from {} to {}", gMin, gMax, e);
    return null;
  }
}
 
Example 2
Source File: HistogramChartFactory.java    From old-mzmine3 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Performs Gaussian fit on XYSeries
 * 
 * @param data the data
 * @param gMin lower bound of Gaussian fit
 * @param gMax upper bound of Gaussian fit
 * @param sigDigits number of significant digits
 * @return double[] {normFactor, mean, sigma} as a result of
 *         GaussianCurveFitter.create().fit(obs.toList())
 */
public static double[] gaussianFit(List<DataPoint> data, double gMin, double gMax) {
  // gaussian fit
  WeightedObservedPoints obs = new WeightedObservedPoints();

  for (int i = 0; i < data.size(); i++) {
    double x = data.get(i).getMZ();
    if (x >= gMin && x <= gMax)
      obs.add(x, data.get(i).getIntensity());
  }
  try {
    return fitter.fit(obs.toList());
  } catch (Exception e) {
    e.printStackTrace();
    logger.error("Cannot fit Gaussian from {} to {}", gMin, gMax, e);
    return null;
  }
}
 
Example 3
Source File: HistogramChartFactory.java    From mzmine2 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Performs Gaussian fit on XYSeries
 * 
 * @param data the data
 * @param gMin lower bound of Gaussian fit
 * @param gMax upper bound of Gaussian fit
 * @param sigDigits number of significant digits
 * @return double[] {normFactor, mean, sigma} as a result of
 *         GaussianCurveFitter.create().fit(obs.toList())
 */
public static double[] gaussianFit(List<DataPoint> data, double gMin, double gMax) {
  // gaussian fit
  WeightedObservedPoints obs = new WeightedObservedPoints();

  for (int i = 0; i < data.size(); i++) {
    double x = data.get(i).getMZ();
    if (x >= gMin && x <= gMax)
      obs.add(x, data.get(i).getIntensity());
  }
  try {
    return fitter.fit(obs.toList());
  } catch (Exception e) {
    e.printStackTrace();
    logger.error("Cannot fit Gaussian from {} to {}", gMin, gMax, e);
    return null;
  }
}
 
Example 4
Source File: Filters.java    From ECG-Viewer with GNU General Public License v2.0 6 votes vote down vote up
public static void detrend(List<double[]> set, int detrendPolynomial) {
	PolynomialCurveFitter p = PolynomialCurveFitter.create(detrendPolynomial);
	WeightedObservedPoints wop = new WeightedObservedPoints();
	for(int i = 0; i < set.size(); i++) {
		wop.add(set.get(i)[0], set.get(i)[1]);
	}
	double[] coeff = p.fit(wop.toList());
	for(int h = 0; h < set.size(); h++) {
		double val = set.get(h)[0];
		double off = 0;
		for(int i = detrendPolynomial; i >= 0; i--) {
			off += coeff[i] * Math.pow(val, i);
		}
		set.set(h, new double[]{set.get(h)[0], set.get(h)[1]-off});
	}
}
 
Example 5
Source File: ExtremumComputer.java    From RipplePower with Apache License 2.0 6 votes vote down vote up
private boolean isLinearCoeffDiff(double[] input, int centerIndex) {
	double xStep = 2.0 / (input.length + 1);

	WeightedObservedPoints firstHalf = new WeightedObservedPoints();
	WeightedObservedPoints secondHalf = new WeightedObservedPoints();

	for (int i = 0; i < input.length; i++) {
		if (i <= centerIndex) {
			firstHalf.add(i * xStep, input[i]);
		}

		if (i >= centerIndex) {
			secondHalf.add((i - centerIndex) * xStep, input[i]);
		}
	}

	PolynomialCurveFitter fitter = PolynomialCurveFitter.create(1);

	double[] firstHalfCoeffs = fitter.fit(firstHalf.toList());
	double[] secondHalfCoeffs = fitter.fit(secondHalf.toList());

	double firstHalfCoeff = firstHalfCoeffs[1];
	double secondHalfCoeff = secondHalfCoeffs[1];

	return firstHalfCoeff * secondHalfCoeff < 0;
}
 
Example 6
Source File: HistogramChartFactory.java    From mzmine3 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Performs Gaussian fit on XYSeries
 * 
 * @param series the data
 * @param gMin lower bound of Gaussian fit
 * @param gMax upper bound of Gaussian fit
 * @param sigDigits number of significant digits
 * @return double[] {normFactor, mean, sigma} as a result of
 *         GaussianCurveFitter.create().fit(obs.toList())
 */
public static double[] gaussianFit(XYSeries series, double gMin, double gMax) {
  // gaussian fit
  WeightedObservedPoints obs = new WeightedObservedPoints();

  for (int i = 0; i < series.getItemCount(); i++) {
    double x = series.getX(i).doubleValue();
    if (x >= gMin && x <= gMax)
      obs.add(x, series.getY(i).doubleValue());
  }

  return fitter.fit(obs.toList());
}
 
Example 7
Source File: HistogramChartFactory.java    From mzmine3 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Performs Gaussian fit on XYSeries
 * 
 * @param data the data
 * @param series the series index
 * @param gMin lower bound of Gaussian fit
 * @param gMax upper bound of Gaussian fit
 * @param sigDigits number of significant digits
 * @return double[] {normFactor, mean, sigma} as a result of
 *         GaussianCurveFitter.create().fit(obs.toList())
 */
public static double[] gaussianFit(XYDataset data, int series, double gMin, double gMax) {
  // gaussian fit
  WeightedObservedPoints obs = new WeightedObservedPoints();

  for (int i = 0; i < data.getItemCount(series); i++) {
    double x = data.getXValue(series, i);
    if (x >= gMin && x <= gMax)
      obs.add(x, data.getYValue(series, i));
  }
  return fitter.fit(obs.toList());
}
 
Example 8
Source File: GaussFitEvaluator.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings({"unchecked", "rawtypes"})
public Object doWork(Object... objects) throws IOException{

  if(objects.length >= 3) {
    throw new IOException("gaussfit function takes a maximum of 2 arguments.");
  }

  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 -> ((Number) 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 -> ((Number) value).doubleValue()).toArray();
    y = ((List) second).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray();


  }

  GaussianCurveFitter curveFitter = GaussianCurveFitter.create();

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

  List<WeightedObservedPoint> pointList = points.toList();

  double[] guess = new GaussianCurveFitter.ParameterGuesser(pointList).guess();
  curveFitter = curveFitter.withStartPoint(guess);

  double[] coef = curveFitter.fit(pointList);
  Gaussian gaussian = new Gaussian(coef[0], coef[1], coef[2]);
  List list = new ArrayList();
  for(double xvalue : x) {
    double yvalue= gaussian.value(xvalue);
    list.add(yvalue);
  }

  return new VectorFunction(gaussian, list);
}
 
Example 9
Source File: HistogramChartFactory.java    From old-mzmine3 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Performs Gaussian fit on XYSeries
 * 
 * @param series the data
 * @param gMin lower bound of Gaussian fit
 * @param gMax upper bound of Gaussian fit
 * @param sigDigits number of significant digits
 * @return double[] {normFactor, mean, sigma} as a result of
 *         GaussianCurveFitter.create().fit(obs.toList())
 */
public static double[] gaussianFit(XYSeries series, double gMin, double gMax) {
  // gaussian fit
  WeightedObservedPoints obs = new WeightedObservedPoints();

  for (int i = 0; i < series.getItemCount(); i++) {
    double x = series.getX(i).doubleValue();
    if (x >= gMin && x <= gMax)
      obs.add(x, series.getY(i).doubleValue());
  }

  return fitter.fit(obs.toList());
}
 
Example 10
Source File: HistogramChartFactory.java    From old-mzmine3 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Performs Gaussian fit on XYSeries
 * 
 * @param data the data
 * @param series the series index
 * @param gMin lower bound of Gaussian fit
 * @param gMax upper bound of Gaussian fit
 * @param sigDigits number of significant digits
 * @return double[] {normFactor, mean, sigma} as a result of
 *         GaussianCurveFitter.create().fit(obs.toList())
 */
public static double[] gaussianFit(XYDataset data, int series, double gMin, double gMax) {
  // gaussian fit
  WeightedObservedPoints obs = new WeightedObservedPoints();

  for (int i = 0; i < data.getItemCount(series); i++) {
    double x = data.getXValue(series, i);
    if (x >= gMin && x <= gMax)
      obs.add(x, data.getYValue(series, i));
  }
  return fitter.fit(obs.toList());
}
 
Example 11
Source File: HistogramChartFactory.java    From mzmine2 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Performs Gaussian fit on XYSeries
 * 
 * @param series the data
 * @param gMin lower bound of Gaussian fit
 * @param gMax upper bound of Gaussian fit
 * @param sigDigits number of significant digits
 * @return double[] {normFactor, mean, sigma} as a result of
 *         GaussianCurveFitter.create().fit(obs.toList())
 */
public static double[] gaussianFit(XYSeries series, double gMin, double gMax) {
  // gaussian fit
  WeightedObservedPoints obs = new WeightedObservedPoints();

  for (int i = 0; i < series.getItemCount(); i++) {
    double x = series.getX(i).doubleValue();
    if (x >= gMin && x <= gMax)
      obs.add(x, series.getY(i).doubleValue());
  }

  return fitter.fit(obs.toList());
}
 
Example 12
Source File: HistogramChartFactory.java    From mzmine2 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Performs Gaussian fit on XYSeries
 * 
 * @param data the data
 * @param series the series index
 * @param gMin lower bound of Gaussian fit
 * @param gMax upper bound of Gaussian fit
 * @param sigDigits number of significant digits
 * @return double[] {normFactor, mean, sigma} as a result of
 *         GaussianCurveFitter.create().fit(obs.toList())
 */
public static double[] gaussianFit(XYDataset data, int series, double gMin, double gMax) {
  // gaussian fit
  WeightedObservedPoints obs = new WeightedObservedPoints();

  for (int i = 0; i < data.getItemCount(series); i++) {
    double x = data.getXValue(series, i);
    if (x >= gMin && x <= gMax)
      obs.add(x, data.getYValue(series, i));
  }
  return fitter.fit(obs.toList());
}
 
Example 13
Source File: Filters.java    From ECG-Viewer with GNU General Public License v2.0 5 votes vote down vote up
public static void harmonicDetrend(List<double[]> set) {
	HarmonicCurveFitter p = HarmonicCurveFitter.create();
	WeightedObservedPoints wop = new WeightedObservedPoints();
	for(int i = 0; i < set.size(); i++) {
		wop.add(set.get(i)[0], set.get(i)[1]);
	}
	double[] coeff = p.fit(wop.toList());
	for(int h = 0; h < set.size(); h++) {
		double val = set.get(h)[0];
		double off = coeff[0] * Math.sin(2*Math.PI*coeff[1]*val + coeff[2]);
		set.set(h, new double[]{val, set.get(h)[1]-off});
	}
}
 
Example 14
Source File: HarmonicFitEvaluator.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings({"unchecked"})
public Object doWork(Object... objects) throws IOException{

  if(objects.length > 3) {
    throw new IOException("harmonicFit function takes a maximum of 2 arguments.");
  }

  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 -> ((Number) 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 -> ((Number) value).doubleValue()).toArray();
      y = ((List) second).stream().mapToDouble(value -> ((Number) 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[] guess = new HarmonicCurveFitter.ParameterGuesser(points.toList()).guess();
  curveFitter = curveFitter.withStartPoint(guess);

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

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

  @SuppressWarnings({"unchecked"})
  VectorFunction vectorFunction =  new VectorFunction(pf, list);
  vectorFunction.addToContext("amplitude", coef[0]);
  vectorFunction.addToContext("angularFrequency", coef[1]);
  vectorFunction.addToContext("phase", coef[2]);

  return vectorFunction;

}