[Dive4elements-commits] [PATCH] Small code cleanups in S/Q outlier tests

Wald Commits scm-commit at wald.intevation.org
Fri Jan 11 22:18:44 CET 2013


# HG changeset patch
# User Sascha L. Teichmann <teichmann at intevation.de>
# Date 1357938982 -3600
# Node ID 8ee270a3ef252c8e5d685d34901e54c1217bd435
# Parent  a7d080347ac3e2d82af94f78b1518f711ce602d8
Small code cleanups in S/Q outlier tests.

diff -r a7d080347ac3 -r 8ee270a3ef25 flys-artifacts/src/main/java/de/intevation/flys/artifacts/math/StdDevOutlier.java
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/math/StdDevOutlier.java	Fri Jan 11 13:57:38 2013 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/math/StdDevOutlier.java	Fri Jan 11 22:16:22 2013 +0100
@@ -2,21 +2,12 @@
 
 import java.util.List;
 
-import org.apache.commons.math.MathException;
-
-import org.apache.commons.math.distribution.TDistributionImpl;
-
-import org.apache.commons.math.stat.descriptive.moment.Mean;
 import org.apache.commons.math.stat.descriptive.moment.StandardDeviation;
 
 import org.apache.log4j.Logger;
 
-import de.intevation.flys.artifacts.model.sq.SQ;
-
 public class StdDevOutlier
 {
-    public static final double EPSILON = 1e-5;
-
     public static final double DEFAULT_FACTOR = 3;
 
     private static Logger log = Logger.getLogger(StdDevOutlier.class);
@@ -28,7 +19,11 @@
         return findOutlier(values, DEFAULT_FACTOR, null);
     }
 
-    public static Integer findOutlier(List<Double> values, double factor, double[] stdDevResult) {
+    public static Integer findOutlier(
+        List<Double> values,
+        double       factor, 
+        double []    stdDevResult
+    ) {
         boolean debug = log.isDebugEnabled();
 
         if (debug) {
@@ -41,7 +36,7 @@
             log.debug("Values to check: " + N);
         }
 
-        if (values.size() < 3) {
+        if (N < 3) {
             return null;
         }
 
@@ -49,15 +44,13 @@
 
         double maxValue = -Double.MAX_VALUE;
         int    maxIndex = -1;
-        int    ndx = 0;
-        for (int i = values.size()-1; i >= 0; --i) {
+        for (int i = N-1; i >= 0; --i) {
             double value = Math.abs(values.get(i));
             stdDev.increment(value);
             if (value > maxValue) {
                 maxValue = value;
-                maxIndex = ndx;
+                maxIndex = i;
             }
-            ++ndx;
         }
 
         double sd = stdDev.getResult();
@@ -69,9 +62,11 @@
             log.debug("accepted: " + accepted);
             log.debug("max value: " + maxValue);
         }
+
         if (stdDevResult != null) {
             stdDevResult[0] = sd;
         }
+
         return maxValue > accepted ? maxIndex : null;
     }
 }
diff -r a7d080347ac3 -r 8ee270a3ef25 flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/Outlier.java
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/Outlier.java	Fri Jan 11 13:57:38 2013 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/Outlier.java	Fri Jan 11 22:16:22 2013 +0100
@@ -1,20 +1,17 @@
 package de.intevation.flys.artifacts.model.sq;
 
+import de.intevation.artifacts.common.utils.Config;
+
+import de.intevation.flys.artifacts.math.GrubbsOutlier;
+import de.intevation.flys.artifacts.math.StdDevOutlier;
+
 import java.util.ArrayList;
 import java.util.List;
 
 import org.apache.commons.math.MathException;
 
-import org.apache.commons.math.stat.descriptive.moment.StandardDeviation;
-
 import org.apache.log4j.Logger;
 
-import de.intevation.artifacts.GlobalContext;
-import de.intevation.artifacts.common.utils.Config;
-import de.intevation.flys.artifacts.context.FLYSContext;
-import de.intevation.flys.artifacts.math.GrubbsOutlier;
-import de.intevation.flys.artifacts.math.StdDevOutlier;
-
 public class Outlier
 {
     private static Logger log = Logger.getLogger(Outlier.class);
@@ -24,7 +21,7 @@
 
     private static final String GRUBBS = "grubbs";
 
-    private static final String STD_DEV = "std-dev";
+    //private static final String STD_DEV = "std-dev";
 
     public interface Callback {
 
@@ -48,42 +45,54 @@
     {
         boolean debug = log.isDebugEnabled();
 
-        if (debug) {
-            log.debug("stdDevFactor: " + stdDevFactor);
-        }
         String method = Config.getStringXPath(OUTLIER_METHOD);
-        log.debug("method: " + method);
+
         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);
 
-            List<Double> values = new ArrayList<Double>();
             for (SQ sq: data) {
                 values.add(callback.eval(sq));
             }
 
-            Integer ndx = null;
-            double[] stdDev = new double[1];
-            if (method.equals(GRUBBS)) {
-                ndx = GrubbsOutlier.findOutlier(values, stdDevFactor/100, stdDev);
-            }
-            else {
-                ndx = StdDevOutlier.findOutlier(values, stdDevFactor, stdDev);
-            }
+            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((int)ndx);
-            log.debug("stdDev: " + stdDev[0]);
-            log.debug("removed " + ndx + "; S: " + outlier.getS() + " Q: " + outlier.getQ());
+            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();
         }
     }
 }


More information about the Dive4elements-commits mailing list