Mercurial > dive4elements > river
annotate flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/Outlier.java @ 4794:a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
* Methods can be switched as option in conf.xml.
* Methods:
- Find outliers via multiples of the standard deviation.
- Grubbs (used in Fix-Analysis)
author | Raimund Renkert <rrenkert@intevation.de> |
---|---|
date | Fri, 11 Jan 2013 13:57:38 +0100 |
parents | b8b1280606c2 |
children | 8ee270a3ef25 |
rev | line source |
---|---|
3187
1e2733f749b5
SQ: Added specialized outlier test for the S/Q relation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
1 package de.intevation.flys.artifacts.model.sq; |
1e2733f749b5
SQ: Added specialized outlier test for the S/Q relation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
2 |
1e2733f749b5
SQ: Added specialized outlier test for the S/Q relation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
3 import java.util.ArrayList; |
1e2733f749b5
SQ: Added specialized outlier test for the S/Q relation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
4 import java.util.List; |
1e2733f749b5
SQ: Added specialized outlier test for the S/Q relation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
5 |
3188
1e46ced2bb57
SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3187
diff
changeset
|
6 import org.apache.commons.math.MathException; |
1e46ced2bb57
SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3187
diff
changeset
|
7 |
3187
1e2733f749b5
SQ: Added specialized outlier test for the S/Q relation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
8 import org.apache.commons.math.stat.descriptive.moment.StandardDeviation; |
1e2733f749b5
SQ: Added specialized outlier test for the S/Q relation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
9 |
3552
1df6984628c3
S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3190
diff
changeset
|
10 import org.apache.log4j.Logger; |
1df6984628c3
S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3190
diff
changeset
|
11 |
4794
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
3572
diff
changeset
|
12 import de.intevation.artifacts.GlobalContext; |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
3572
diff
changeset
|
13 import de.intevation.artifacts.common.utils.Config; |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
3572
diff
changeset
|
14 import de.intevation.flys.artifacts.context.FLYSContext; |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
3572
diff
changeset
|
15 import de.intevation.flys.artifacts.math.GrubbsOutlier; |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
3572
diff
changeset
|
16 import de.intevation.flys.artifacts.math.StdDevOutlier; |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
3572
diff
changeset
|
17 |
3187
1e2733f749b5
SQ: Added specialized outlier test for the S/Q relation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
18 public class Outlier |
1e2733f749b5
SQ: Added specialized outlier test for the S/Q relation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
19 { |
3552
1df6984628c3
S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3190
diff
changeset
|
20 private static Logger log = Logger.getLogger(Outlier.class); |
1df6984628c3
S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3190
diff
changeset
|
21 |
4794
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
3572
diff
changeset
|
22 private static final String OUTLIER_METHOD = |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
3572
diff
changeset
|
23 "/artifact-database/options/minfo-sq/outlier-method/@name"; |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
3572
diff
changeset
|
24 |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
3572
diff
changeset
|
25 private static final String GRUBBS = "grubbs"; |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
3572
diff
changeset
|
26 |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
3572
diff
changeset
|
27 private static final String STD_DEV = "std-dev"; |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
3572
diff
changeset
|
28 |
3187
1e2733f749b5
SQ: Added specialized outlier test for the S/Q relation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
29 public interface Callback { |
1e2733f749b5
SQ: Added specialized outlier test for the S/Q relation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
30 |
3566
8d0f06b76e09
S/Q relation: Fixed flys/issue748
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3552
diff
changeset
|
31 void initialize(List<SQ> sqs) throws MathException; |
3552
1df6984628c3
S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3190
diff
changeset
|
32 |
3187
1e2733f749b5
SQ: Added specialized outlier test for the S/Q relation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
33 double eval(SQ sq); |
1e2733f749b5
SQ: Added specialized outlier test for the S/Q relation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
34 |
3566
8d0f06b76e09
S/Q relation: Fixed flys/issue748
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3552
diff
changeset
|
35 void iterationFinished( |
8d0f06b76e09
S/Q relation: Fixed flys/issue748
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3552
diff
changeset
|
36 double stdDev, |
8d0f06b76e09
S/Q relation: Fixed flys/issue748
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3552
diff
changeset
|
37 SQ outlier, |
8d0f06b76e09
S/Q relation: Fixed flys/issue748
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3552
diff
changeset
|
38 List<SQ> remaining); |
3187
1e2733f749b5
SQ: Added specialized outlier test for the S/Q relation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
39 |
1e2733f749b5
SQ: Added specialized outlier test for the S/Q relation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
40 } // interface Callback |
1e2733f749b5
SQ: Added specialized outlier test for the S/Q relation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
41 |
3552
1df6984628c3
S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3190
diff
changeset
|
42 public static void detectOutliers( |
3187
1e2733f749b5
SQ: Added specialized outlier test for the S/Q relation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
43 Callback callback, |
1e2733f749b5
SQ: Added specialized outlier test for the S/Q relation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
44 List<SQ> sqs, |
1e2733f749b5
SQ: Added specialized outlier test for the S/Q relation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
45 double stdDevFactor |
3188
1e46ced2bb57
SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3187
diff
changeset
|
46 ) |
1e46ced2bb57
SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3187
diff
changeset
|
47 throws MathException |
1e46ced2bb57
SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3187
diff
changeset
|
48 { |
3552
1df6984628c3
S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3190
diff
changeset
|
49 boolean debug = log.isDebugEnabled(); |
1df6984628c3
S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3190
diff
changeset
|
50 |
1df6984628c3
S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3190
diff
changeset
|
51 if (debug) { |
1df6984628c3
S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3190
diff
changeset
|
52 log.debug("stdDevFactor: " + stdDevFactor); |
1df6984628c3
S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3190
diff
changeset
|
53 } |
4794
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
3572
diff
changeset
|
54 String method = Config.getStringXPath(OUTLIER_METHOD); |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
3572
diff
changeset
|
55 log.debug("method: " + method); |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
3572
diff
changeset
|
56 if (method == null) { |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
3572
diff
changeset
|
57 method = "std-dev"; |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
3572
diff
changeset
|
58 } |
3566
8d0f06b76e09
S/Q relation: Fixed flys/issue748
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3552
diff
changeset
|
59 List<SQ> data = new ArrayList<SQ>(sqs); |
3187
1e2733f749b5
SQ: Added specialized outlier test for the S/Q relation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
60 |
3566
8d0f06b76e09
S/Q relation: Fixed flys/issue748
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3552
diff
changeset
|
61 while (data.size() > 2) { |
3187
1e2733f749b5
SQ: Added specialized outlier test for the S/Q relation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
62 |
3566
8d0f06b76e09
S/Q relation: Fixed flys/issue748
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3552
diff
changeset
|
63 callback.initialize(data); |
3187
1e2733f749b5
SQ: Added specialized outlier test for the S/Q relation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
64 |
4794
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
3572
diff
changeset
|
65 List<Double> values = new ArrayList<Double>(); |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
3572
diff
changeset
|
66 for (SQ sq: data) { |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
3572
diff
changeset
|
67 values.add(callback.eval(sq)); |
3187
1e2733f749b5
SQ: Added specialized outlier test for the S/Q relation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
68 } |
1e2733f749b5
SQ: Added specialized outlier test for the S/Q relation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
69 |
4794
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
3572
diff
changeset
|
70 Integer ndx = null; |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
3572
diff
changeset
|
71 double[] stdDev = new double[1]; |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
3572
diff
changeset
|
72 if (method.equals(GRUBBS)) { |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
3572
diff
changeset
|
73 ndx = GrubbsOutlier.findOutlier(values, stdDevFactor/100, stdDev); |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
3572
diff
changeset
|
74 } |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
3572
diff
changeset
|
75 else { |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
3572
diff
changeset
|
76 ndx = StdDevOutlier.findOutlier(values, stdDevFactor, stdDev); |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
3572
diff
changeset
|
77 } |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
3572
diff
changeset
|
78 if (ndx == null) { |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
3572
diff
changeset
|
79 callback.iterationFinished(stdDev[0], null, data); |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
3572
diff
changeset
|
80 break; |
3187
1e2733f749b5
SQ: Added specialized outlier test for the S/Q relation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
81 } |
1e2733f749b5
SQ: Added specialized outlier test for the S/Q relation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
82 |
4794
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
3572
diff
changeset
|
83 SQ outlier = data.remove((int)ndx); |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
3572
diff
changeset
|
84 log.debug("stdDev: " + stdDev[0]); |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
3572
diff
changeset
|
85 log.debug("removed " + ndx + "; S: " + outlier.getS() + " Q: " + outlier.getQ()); |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
3572
diff
changeset
|
86 callback.iterationFinished(stdDev[0], outlier, data); |
3566
8d0f06b76e09
S/Q relation: Fixed flys/issue748
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3552
diff
changeset
|
87 } |
3188
1e46ced2bb57
SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3187
diff
changeset
|
88 } |
3187
1e2733f749b5
SQ: Added specialized outlier test for the S/Q relation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
89 } |
1e2733f749b5
SQ: Added specialized outlier test for the S/Q relation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
90 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |