Mercurial > dive4elements > river
view artifacts/src/main/java/org/dive4elements/river/artifacts/model/sq/Outlier.java @ 8705:682a26e4158b
(issue1787) Globally sorting WST-columns over avg(Q)+max(Q) might give unexpected results at some kms. We thus sort locally since Rev 93a31cfb18c0.
author | Tom Gottfried <tom@intevation.de> |
---|---|
date | Thu, 23 Apr 2015 19:20:40 +0200 |
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 :