view artifacts/src/main/java/org/dive4elements/river/artifacts/model/sq/Outlier.java @ 8578:4eb1a3c71579

(issue1753) Implement PDF export of SQ Measurement data. The SQRelationExporter now consists of two reports (to avoid subreport woes). The old SQReleation PDF only takes the Page count of the Measurement attachment as argument to correctly print the page numbers. Otherwise it is unchanged. The new SQMeasurements report lists the measured data points on which the calculation was based.
author Andre Heinecke <andre.heinecke@intevation.de>
date Mon, 16 Mar 2015 11:35:19 +0100
parents af13ceeba52a
children 0a5239a1e46e
line wrap: on
line source
/* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
 * Software engineering by Intevation GmbH
 *
 * This file is Free Software under the GNU AGPL (>=v3)
 * and comes with ABSOLUTELY NO WARRANTY! Check out the
 * documentation coming with Dive4Elements River for details.
 */

package org.dive4elements.river.artifacts.model.sq;

import org.dive4elements.river.artifacts.math.GrubbsOutlier;
import org.dive4elements.river.artifacts.math.StdDevOutlier;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.math.MathException;

import org.apache.log4j.Logger;

public class Outlier
{
    private static Logger log = Logger.getLogger(Outlier.class);

    private static final String GRUBBS = "outlier.method.grubbs";

    //private static final String STD_DEV = "std-dev";

    public interface Callback {

        void initialize(List<SQ> sqs) throws MathException;

        double eval(SQ sq);

        void iterationFinished(
            double   stdDev,
            SQ       outlier,
            List<SQ> remaining);

    } // interface Callback

    public static void detectOutliers(
        Callback callback,
        List<SQ> sqs,
        double   stdDevFactor,
        String   method
    )
    throws MathException
    {
        boolean debug = log.isDebugEnabled();

        if (method == null) {
            method = "std-dev";
        }

        if (debug) {
            log.debug("stdDevFactor: " + stdDevFactor);
            log.debug("method: " + method);
        }

        List<SQ> data = new ArrayList<SQ>(sqs);

        double [] stdDev = new double[1];

        boolean useGrubbs = method.equals(GRUBBS);

        if (useGrubbs) {
            stdDevFactor = Math.max(0d, Math.min(stdDevFactor/100d, 1d));
        }

        List<Double> values = new ArrayList<Double>(data.size());

        while (data.size() > 2) {

            callback.initialize(data);

            for (SQ sq: data) {
                values.add(callback.eval(sq));
            }

            Integer ndx = useGrubbs
                ? GrubbsOutlier.findOutlier(values, stdDevFactor, stdDev)
                : StdDevOutlier.findOutlier(values, stdDevFactor, stdDev);

            if (ndx == null) {
                callback.iterationFinished(stdDev[0], null, data);
                break;
            }

            SQ outlier = data.remove(ndx.intValue());
            if (debug) {
                log.debug("stdDev: " + stdDev[0]);
                log.debug("removed " + ndx +
                    "; S: " + outlier.getS() + " Q: " + outlier.getQ());
            }
            callback.iterationFinished(stdDev[0], outlier, data);
            values.clear();
        }
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org