Search in sources :

Example 1 with SplineInterpolator

use of org.apache.commons.math3.analysis.interpolation.SplineInterpolator in project GDSC-SMLM by aherbert.

the class TraceMolecules method interpolateZeroCrossingPoints.

@SuppressWarnings("unused")
private void interpolateZeroCrossingPoints() {
    double[] x = new double[zeroCrossingPoints.size()];
    double[] y = new double[zeroCrossingPoints.size()];
    for (int i = 0; i < x.length; i++) {
        double[] point = zeroCrossingPoints.get(i);
        x[i] = point[0];
        y[i] = point[1];
    }
    PolynomialSplineFunction fx = new SplineInterpolator().interpolate(x, y);
    double minX = x[0];
    double maxX = x[x.length - 1];
    double xinc = (maxX - minX) / 50;
    for (minX = minX + xinc; minX < maxX; minX += xinc) {
        zeroCrossingPoints.add(new double[] { minX, fx.value(minX) });
    }
    sortPoints();
}
Also used : SplineInterpolator(org.apache.commons.math3.analysis.interpolation.SplineInterpolator) PolynomialSplineFunction(org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction) ClusterPoint(gdsc.core.clustering.ClusterPoint)

Example 2 with SplineInterpolator

use of org.apache.commons.math3.analysis.interpolation.SplineInterpolator in project GDSC-SMLM by aherbert.

the class DriftCalculator method interpolate.

private void interpolate(double[] dx, double[] dy, double[] originalDriftTimePoints) {
    // Interpolator can only create missing values within the range provided by the input values.
    // The two ends have to be extrapolated.
    // TODO: Perform extrapolation. Currently the end values are used.
    // Find end points
    int startT = 0;
    while (originalDriftTimePoints[startT] == 0) startT++;
    int endT = originalDriftTimePoints.length - 1;
    while (originalDriftTimePoints[endT] == 0) endT--;
    // Extrapolate using a constant value
    for (int t = startT; t-- > 0; ) {
        dx[t] = dx[startT];
        dy[t] = dy[startT];
    }
    for (int t = endT; ++t < dx.length; ) {
        dx[t] = dx[endT];
        dy[t] = dy[endT];
    }
    double[][] values = extractValues(originalDriftTimePoints, startT, endT, dx, dy);
    PolynomialSplineFunction fx, fy;
    if (values[0].length < 3) {
        fx = new LinearInterpolator().interpolate(values[0], values[1]);
        fy = new LinearInterpolator().interpolate(values[0], values[2]);
    } else {
        fx = new SplineInterpolator().interpolate(values[0], values[1]);
        fy = new SplineInterpolator().interpolate(values[0], values[2]);
    }
    for (int t = startT; t <= endT; t++) {
        if (originalDriftTimePoints[t] == 0) {
            dx[t] = fx.value(t);
            dy[t] = fy.value(t);
        }
    }
    this.interpolationStart = startT;
    this.interpolationEnd = endT;
}
Also used : LinearInterpolator(org.apache.commons.math3.analysis.interpolation.LinearInterpolator) SplineInterpolator(org.apache.commons.math3.analysis.interpolation.SplineInterpolator) PolynomialSplineFunction(org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction) Point(java.awt.Point)

Example 3 with SplineInterpolator

use of org.apache.commons.math3.analysis.interpolation.SplineInterpolator in project GDSC-SMLM by aherbert.

the class AiryPSFModel method createAiryDistribution.

private static synchronized void createAiryDistribution() {
    if (spline != null)
        return;
    final double relativeAccuracy = 1e-4;
    final double absoluteAccuracy = 1e-8;
    final int minimalIterationCount = 3;
    final int maximalIterationCount = 32;
    UnivariateIntegrator integrator = new SimpsonIntegrator(relativeAccuracy, absoluteAccuracy, minimalIterationCount, maximalIterationCount);
    UnivariateFunction f = new UnivariateFunction() {

        public double value(double x) {
            //return AiryPattern.intensity(x) * 2 * Math.PI * x / (4 * Math.PI);
            return AiryPattern.intensity(x) * 0.5 * x;
        }
    };
    // Integrate up to a set number of dark rings
    int samples = 1000;
    final double step = RINGS[SAMPLE_RINGS] / samples;
    double to = 0, from = 0;
    r = new double[samples + 1];
    sum = new double[samples + 1];
    for (int i = 1; i < sum.length; i++) {
        from = to;
        r[i] = to = step * i;
        sum[i] = integrator.integrate(2000, f, from, to) + sum[i - 1];
    }
    if (DoubleEquality.relativeError(sum[samples], POWER[SAMPLE_RINGS]) > 1e-3)
        throw new RuntimeException("Failed to create the Airy distribution");
    SplineInterpolator si = new SplineInterpolator();
    spline = si.interpolate(sum, r);
}
Also used : SimpsonIntegrator(org.apache.commons.math3.analysis.integration.SimpsonIntegrator) UnivariateIntegrator(org.apache.commons.math3.analysis.integration.UnivariateIntegrator) UnivariateFunction(org.apache.commons.math3.analysis.UnivariateFunction) SplineInterpolator(org.apache.commons.math3.analysis.interpolation.SplineInterpolator)

Example 4 with SplineInterpolator

use of org.apache.commons.math3.analysis.interpolation.SplineInterpolator in project dhis2-core by dhis2.

the class DefaultChartService method getJFreeChartHistory.

@Override
public JFreeChart getJFreeChartHistory(DataElement dataElement, DataElementCategoryOptionCombo categoryOptionCombo, DataElementCategoryOptionCombo attributeOptionCombo, Period lastPeriod, OrganisationUnit organisationUnit, int historyLength, I18nFormat format) {
    lastPeriod = periodService.reloadPeriod(lastPeriod);
    List<Period> periods = periodService.getPeriods(lastPeriod, historyLength);
    MinMaxDataElement minMax = minMaxDataElementService.getMinMaxDataElement(organisationUnit, dataElement, categoryOptionCombo);
    UnivariateInterpolator interpolator = new SplineInterpolator();
    Integer periodCount = 0;
    List<Double> x = new ArrayList<>();
    List<Double> y = new ArrayList<>();
    // ---------------------------------------------------------------------
    // DataValue, MinValue and MaxValue DataSets
    // ---------------------------------------------------------------------
    DefaultCategoryDataset dataValueDataSet = new DefaultCategoryDataset();
    DefaultCategoryDataset metaDataSet = new DefaultCategoryDataset();
    for (Period period : periods) {
        ++periodCount;
        period.setName(format.formatPeriod(period));
        DataValue dataValue = dataValueService.getDataValue(dataElement, period, organisationUnit, categoryOptionCombo, attributeOptionCombo);
        double value = 0;
        if (dataValue != null && dataValue.getValue() != null && MathUtils.isNumeric(dataValue.getValue())) {
            value = Double.parseDouble(dataValue.getValue());
            x.add(periodCount.doubleValue());
            y.add(value);
        }
        dataValueDataSet.addValue(value, dataElement.getShortName(), period.getName());
        if (minMax != null) {
            metaDataSet.addValue(minMax.getMin(), "Min value", period.getName());
            metaDataSet.addValue(minMax.getMax(), "Max value", period.getName());
        }
    }
    if (// minimum 3 points required for interpolation
    x.size() >= 3) {
        periodCount = 0;
        double[] xa = getArray(x);
        int min = MathUtils.getMin(xa).intValue();
        int max = MathUtils.getMax(xa).intValue();
        try {
            UnivariateFunction function = interpolator.interpolate(xa, getArray(y));
            for (Period period : periods) {
                if (++periodCount >= min && periodCount <= max) {
                    metaDataSet.addValue(function.value(periodCount), "Regression value", period.getName());
                }
            }
        } catch (MathRuntimeException ex) {
            throw new RuntimeException("Failed to interpolate", ex);
        }
    }
    // ---------------------------------------------------------------------
    // Plots
    // ---------------------------------------------------------------------
    CategoryPlot plot = getCategoryPlot(dataValueDataSet, getBarRenderer(), PlotOrientation.VERTICAL, CategoryLabelPositions.UP_45);
    plot.setDataset(1, metaDataSet);
    plot.setRenderer(1, getLineRenderer());
    JFreeChart jFreeChart = getBasicJFreeChart(plot);
    return jFreeChart;
}
Also used : MathRuntimeException(org.apache.commons.math3.exception.MathRuntimeException) UnivariateFunction(org.apache.commons.math3.analysis.UnivariateFunction) DataValue(org.hisp.dhis.datavalue.DataValue) Period(org.hisp.dhis.period.Period) JFreeChart(org.jfree.chart.JFreeChart) MathRuntimeException(org.apache.commons.math3.exception.MathRuntimeException) MinMaxDataElement(org.hisp.dhis.minmax.MinMaxDataElement) SplineInterpolator(org.apache.commons.math3.analysis.interpolation.SplineInterpolator) UnivariateInterpolator(org.apache.commons.math3.analysis.interpolation.UnivariateInterpolator) DefaultCategoryDataset(org.jfree.data.category.DefaultCategoryDataset)

Aggregations

SplineInterpolator (org.apache.commons.math3.analysis.interpolation.SplineInterpolator)4 UnivariateFunction (org.apache.commons.math3.analysis.UnivariateFunction)2 PolynomialSplineFunction (org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction)2 ClusterPoint (gdsc.core.clustering.ClusterPoint)1 Point (java.awt.Point)1 SimpsonIntegrator (org.apache.commons.math3.analysis.integration.SimpsonIntegrator)1 UnivariateIntegrator (org.apache.commons.math3.analysis.integration.UnivariateIntegrator)1 LinearInterpolator (org.apache.commons.math3.analysis.interpolation.LinearInterpolator)1 UnivariateInterpolator (org.apache.commons.math3.analysis.interpolation.UnivariateInterpolator)1 MathRuntimeException (org.apache.commons.math3.exception.MathRuntimeException)1 DataValue (org.hisp.dhis.datavalue.DataValue)1 MinMaxDataElement (org.hisp.dhis.minmax.MinMaxDataElement)1 Period (org.hisp.dhis.period.Period)1 JFreeChart (org.jfree.chart.JFreeChart)1 DefaultCategoryDataset (org.jfree.data.category.DefaultCategoryDataset)1