comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/Outlier.java @ 4795:8ee270a3ef25

Small code cleanups in S/Q outlier tests.
author Sascha L. Teichmann <teichmann@intevation.de>
date Fri, 11 Jan 2013 22:16:22 +0100
parents a7d080347ac3
children 846b0441f905
comparison
equal deleted inserted replaced
4794:a7d080347ac3 4795:8ee270a3ef25
1 package de.intevation.flys.artifacts.model.sq; 1 package de.intevation.flys.artifacts.model.sq;
2
3 import de.intevation.artifacts.common.utils.Config;
4
5 import de.intevation.flys.artifacts.math.GrubbsOutlier;
6 import de.intevation.flys.artifacts.math.StdDevOutlier;
2 7
3 import java.util.ArrayList; 8 import java.util.ArrayList;
4 import java.util.List; 9 import java.util.List;
5 10
6 import org.apache.commons.math.MathException; 11 import org.apache.commons.math.MathException;
7 12
8 import org.apache.commons.math.stat.descriptive.moment.StandardDeviation;
9
10 import org.apache.log4j.Logger; 13 import org.apache.log4j.Logger;
11
12 import de.intevation.artifacts.GlobalContext;
13 import de.intevation.artifacts.common.utils.Config;
14 import de.intevation.flys.artifacts.context.FLYSContext;
15 import de.intevation.flys.artifacts.math.GrubbsOutlier;
16 import de.intevation.flys.artifacts.math.StdDevOutlier;
17 14
18 public class Outlier 15 public class Outlier
19 { 16 {
20 private static Logger log = Logger.getLogger(Outlier.class); 17 private static Logger log = Logger.getLogger(Outlier.class);
21 18
22 private static final String OUTLIER_METHOD = 19 private static final String OUTLIER_METHOD =
23 "/artifact-database/options/minfo-sq/outlier-method/@name"; 20 "/artifact-database/options/minfo-sq/outlier-method/@name";
24 21
25 private static final String GRUBBS = "grubbs"; 22 private static final String GRUBBS = "grubbs";
26 23
27 private static final String STD_DEV = "std-dev"; 24 //private static final String STD_DEV = "std-dev";
28 25
29 public interface Callback { 26 public interface Callback {
30 27
31 void initialize(List<SQ> sqs) throws MathException; 28 void initialize(List<SQ> sqs) throws MathException;
32 29
46 ) 43 )
47 throws MathException 44 throws MathException
48 { 45 {
49 boolean debug = log.isDebugEnabled(); 46 boolean debug = log.isDebugEnabled();
50 47
51 if (debug) {
52 log.debug("stdDevFactor: " + stdDevFactor);
53 }
54 String method = Config.getStringXPath(OUTLIER_METHOD); 48 String method = Config.getStringXPath(OUTLIER_METHOD);
55 log.debug("method: " + method); 49
56 if (method == null) { 50 if (method == null) {
57 method = "std-dev"; 51 method = "std-dev";
58 } 52 }
53
54 if (debug) {
55 log.debug("stdDevFactor: " + stdDevFactor);
56 log.debug("method: " + method);
57 }
58
59 List<SQ> data = new ArrayList<SQ>(sqs); 59 List<SQ> data = new ArrayList<SQ>(sqs);
60
61 double [] stdDev = new double[1];
62
63 boolean useGrubbs = method.equals(GRUBBS);
64
65 if (useGrubbs) {
66 stdDevFactor = Math.max(0d, Math.min(stdDevFactor/100d, 1d));
67 }
68
69 List<Double> values = new ArrayList<Double>(data.size());
60 70
61 while (data.size() > 2) { 71 while (data.size() > 2) {
62 72
63 callback.initialize(data); 73 callback.initialize(data);
64 74
65 List<Double> values = new ArrayList<Double>();
66 for (SQ sq: data) { 75 for (SQ sq: data) {
67 values.add(callback.eval(sq)); 76 values.add(callback.eval(sq));
68 } 77 }
69 78
70 Integer ndx = null; 79 Integer ndx = useGrubbs
71 double[] stdDev = new double[1]; 80 ? GrubbsOutlier.findOutlier(values, stdDevFactor, stdDev)
72 if (method.equals(GRUBBS)) { 81 : StdDevOutlier.findOutlier(values, stdDevFactor, stdDev);
73 ndx = GrubbsOutlier.findOutlier(values, stdDevFactor/100, stdDev); 82
74 }
75 else {
76 ndx = StdDevOutlier.findOutlier(values, stdDevFactor, stdDev);
77 }
78 if (ndx == null) { 83 if (ndx == null) {
79 callback.iterationFinished(stdDev[0], null, data); 84 callback.iterationFinished(stdDev[0], null, data);
80 break; 85 break;
81 } 86 }
82 87
83 SQ outlier = data.remove((int)ndx); 88 SQ outlier = data.remove(ndx.intValue());
84 log.debug("stdDev: " + stdDev[0]); 89 if (debug) {
85 log.debug("removed " + ndx + "; S: " + outlier.getS() + " Q: " + outlier.getQ()); 90 log.debug("stdDev: " + stdDev[0]);
91 log.debug("removed " + ndx +
92 "; S: " + outlier.getS() + " Q: " + outlier.getQ());
93 }
86 callback.iterationFinished(stdDev[0], outlier, data); 94 callback.iterationFinished(stdDev[0], outlier, data);
95 values.clear();
87 } 96 }
88 } 97 }
89 } 98 }
90 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 99 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org