comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/Outlier.java @ 3552:1df6984628c3

S/Q: Extented the result data model of the S/Q calculation to store the curve coefficients for each iteration step of the outlier elimination. flys-artifacts/trunk@5146 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 27 Jul 2012 12:36:09 +0000
parents 49fe2ed03c12
children 8d0f06b76e09
comparison
equal deleted inserted replaced
3551:e7f1556192b3 3552:1df6984628c3
6 6
7 import org.apache.commons.math.MathException; 7 import org.apache.commons.math.MathException;
8 8
9 import org.apache.commons.math.stat.descriptive.moment.StandardDeviation; 9 import org.apache.commons.math.stat.descriptive.moment.StandardDeviation;
10 10
11 import org.apache.log4j.Logger;
12
11 public class Outlier 13 public class Outlier
12 { 14 {
15 private static Logger log = Logger.getLogger(Outlier.class);
16
13 public static final int MAX_ITERATIONS = 1000; 17 public static final int MAX_ITERATIONS = 1000;
14 18
15 public interface Callback { 19 public interface Callback {
16 20
21 void initialize(Iterator<SQ> good) throws MathException;
22
17 double eval(SQ sq); 23 double eval(SQ sq);
18
19 void iteration(int i);
20 24
21 void outlier(SQ sq); 25 void outlier(SQ sq);
22 26
27 void remaining(SQ sq);
28
23 void standardDeviation(double stdDev); 29 void standardDeviation(double stdDev);
24 30
25 void reinitialize(Iterator<SQ> good) throws MathException; 31 void iterationFinished();
26
27 void finished();
28 32
29 } // interface Callback 33 } // interface Callback
30 34
31 private static final class EvalSQ { 35 private static final class EvalSQ {
32 protected SQ sq; 36 protected SQ sq;
35 public EvalSQ(SQ sq) { 39 public EvalSQ(SQ sq) {
36 this.sq = sq; 40 this.sq = sq;
37 } 41 }
38 } // class EvalSQ 42 } // class EvalSQ
39 43
40 public static SQ [] detectOutliers( 44 public static void detectOutliers(
41 Callback callback, 45 Callback callback,
42 List<SQ> sqs, 46 List<SQ> sqs,
43 double stdDevFactor 47 double stdDevFactor
44 ) 48 )
45 throws MathException 49 throws MathException
46 { 50 {
51 boolean debug = log.isDebugEnabled();
52
53 if (debug) {
54 log.debug("stdDevFactor: " + stdDevFactor);
55 }
56
47 List<EvalSQ> data = new ArrayList<EvalSQ>(sqs.size()); 57 List<EvalSQ> data = new ArrayList<EvalSQ>(sqs.size());
48 58
49 for (SQ sq: sqs) { 59 for (SQ sq: sqs) {
50 data.add(new EvalSQ(sq)); 60 data.add(new EvalSQ(sq));
51 } 61 }
52 62
53 List<EvalSQ> good = new ArrayList<EvalSQ>(sqs.size()); 63 List<EvalSQ> good = new ArrayList<EvalSQ>(sqs.size());
54 64
55 for (int i = 1; i <= MAX_ITERATIONS && data.size() > 2; ++i) { 65 for (int i = 0; i < MAX_ITERATIONS && data.size() > 2; ++i) {
66
67 callback.initialize(asSQIterator(data));
56 68
57 StandardDeviation stdDev = new StandardDeviation(); 69 StandardDeviation stdDev = new StandardDeviation();
58 70
59 for (EvalSQ esq: data) { 71 for (EvalSQ esq: data) {
60 stdDev.increment(esq.value = callback.eval(esq.sq)); 72 stdDev.increment(esq.value = callback.eval(esq.sq));
64 76
65 callback.standardDeviation(sd); 77 callback.standardDeviation(sd);
66 78
67 double accepted = stdDevFactor * sd; 79 double accepted = stdDevFactor * sd;
68 80
69 callback.iteration(i); 81 if (debug) {
82 log.debug("accepted: " + accepted);
83 }
70 84
71 for (EvalSQ esq: data) { 85 for (EvalSQ esq: data) {
86 if (debug) {
87 log.debug(" value: " + Math.abs(esq.value));
88 }
89
72 if (Math.abs(esq.value) > accepted) { 90 if (Math.abs(esq.value) > accepted) {
73 callback.outlier(esq.sq); 91 callback.outlier(esq.sq);
74 } 92 }
75 else { 93 else {
94 callback.remaining(esq.sq);
76 good.add(esq); 95 good.add(esq);
77 } 96 }
78 } 97 }
79 98
99 callback.iterationFinished();
100
80 if (good.size() == data.size()) { 101 if (good.size() == data.size()) {
81 break; 102 break;
82 } 103 }
83
84 callback.reinitialize(asSQIterator(good));
85 104
86 List<EvalSQ> tmp = good; 105 List<EvalSQ> tmp = good;
87 good = data; 106 good = data;
88 data = tmp; 107 data = tmp;
89 good.clear(); 108 good.clear();
90 } 109 }
91
92 callback.finished();
93
94 SQ [] result = new SQ[good.size()];
95
96 for (int i = 0; i < result.length; ++i) {
97 result[i] = good.get(i).sq;
98 }
99
100 return result;
101 } 110 }
102 111
103 protected static Iterator<SQ> asSQIterator(List<EvalSQ> esqs) { 112 protected static Iterator<SQ> asSQIterator(List<EvalSQ> esqs) {
104 final Iterator<EvalSQ> parent = esqs.iterator(); 113 final Iterator<EvalSQ> parent = esqs.iterator();
105 return new Iterator<SQ>() { 114 return new Iterator<SQ>() {

http://dive4elements.wald.intevation.org