annotate artifacts/src/main/java/org/dive4elements/river/artifacts/model/sq/Outlier.java @ 9801:1d7a72a50183 3.2.x tip

Assume Compose V2, consistently
author Tom Gottfried <tom@intevation.de>
date Thu, 23 Nov 2023 10:14:13 +0100
parents 0a5239a1e46e
children
rev   line source
5863
4897a58c8746 River artifacts: Added new copyright headers.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
1 /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
4897a58c8746 River artifacts: Added new copyright headers.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
2 * Software engineering by Intevation GmbH
4897a58c8746 River artifacts: Added new copyright headers.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
3 *
5994
af13ceeba52a Removed trailing whitespace.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5863
diff changeset
4 * This file is Free Software under the GNU AGPL (>=v3)
5863
4897a58c8746 River artifacts: Added new copyright headers.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
5 * and comes with ABSOLUTELY NO WARRANTY! Check out the
5994
af13ceeba52a Removed trailing whitespace.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5863
diff changeset
6 * documentation coming with Dive4Elements River for details.
5863
4897a58c8746 River artifacts: Added new copyright headers.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
7 */
4897a58c8746 River artifacts: Added new copyright headers.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
8
5831
bd047b71ab37 Repaired internal references
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5431
diff changeset
9 package org.dive4elements.river.artifacts.model.sq;
3187
1e2733f749b5 SQ: Added specialized outlier test for the S/Q relation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
10
5831
bd047b71ab37 Repaired internal references
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5431
diff changeset
11 import org.dive4elements.river.artifacts.math.GrubbsOutlier;
bd047b71ab37 Repaired internal references
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5431
diff changeset
12 import org.dive4elements.river.artifacts.math.StdDevOutlier;
4795
8ee270a3ef25 Small code cleanups in S/Q outlier tests.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4794
diff changeset
13
3187
1e2733f749b5 SQ: Added specialized outlier test for the S/Q relation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
14 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
15 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
16
3188
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3187
diff changeset
17 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
18
9726
0a5239a1e46e Upgrade to Log4j 2
Tom Gottfried <tom@intevation.de>
parents: 5994
diff changeset
19 import org.apache.logging.log4j.Logger;
0a5239a1e46e Upgrade to Log4j 2
Tom Gottfried <tom@intevation.de>
parents: 5994
diff changeset
20 import org.apache.logging.log4j.LogManager;
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
21
3187
1e2733f749b5 SQ: Added specialized outlier test for the S/Q relation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
22 public class Outlier
1e2733f749b5 SQ: Added specialized outlier test for the S/Q relation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
23 {
9726
0a5239a1e46e Upgrade to Log4j 2
Tom Gottfried <tom@intevation.de>
parents: 5994
diff changeset
24 private static Logger log = LogManager.getLogger(Outlier.class);
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
25
5396
aaf810d4ec82 Use new state 'outlier mehtod selection' in SQ-relation.
Raimund Renkert <rrenkert@intevation.de>
parents: 4816
diff changeset
26 private static final String GRUBBS = "outlier.method.grubbs";
4794
a7d080347ac3 MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents: 3572
diff changeset
27
4795
8ee270a3ef25 Small code cleanups in S/Q outlier tests.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4794
diff changeset
28 //private static final String STD_DEV = "std-dev";
4794
a7d080347ac3 MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents: 3572
diff changeset
29
3187
1e2733f749b5 SQ: Added specialized outlier test for the S/Q relation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
30 public interface Callback {
1e2733f749b5 SQ: Added specialized outlier test for the S/Q relation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
31
3566
8d0f06b76e09 S/Q relation: Fixed flys/issue748
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3552
diff changeset
32 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
33
3187
1e2733f749b5 SQ: Added specialized outlier test for the S/Q relation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
34 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
35
3566
8d0f06b76e09 S/Q relation: Fixed flys/issue748
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3552
diff changeset
36 void iterationFinished(
8d0f06b76e09 S/Q relation: Fixed flys/issue748
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3552
diff changeset
37 double stdDev,
8d0f06b76e09 S/Q relation: Fixed flys/issue748
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3552
diff changeset
38 SQ outlier,
8d0f06b76e09 S/Q relation: Fixed flys/issue748
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3552
diff changeset
39 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
40
1e2733f749b5 SQ: Added specialized outlier test for the S/Q relation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
41 } // interface Callback
1e2733f749b5 SQ: Added specialized outlier test for the S/Q relation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
42
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
43 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
44 Callback callback,
1e2733f749b5 SQ: Added specialized outlier test for the S/Q relation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
45 List<SQ> sqs,
5396
aaf810d4ec82 Use new state 'outlier mehtod selection' in SQ-relation.
Raimund Renkert <rrenkert@intevation.de>
parents: 4816
diff changeset
46 double stdDevFactor,
aaf810d4ec82 Use new state 'outlier mehtod selection' in SQ-relation.
Raimund Renkert <rrenkert@intevation.de>
parents: 4816
diff changeset
47 String method
3188
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3187
diff changeset
48 )
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3187
diff changeset
49 throws MathException
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3187
diff changeset
50 {
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
51 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
52
4794
a7d080347ac3 MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents: 3572
diff changeset
53 if (method == null) {
a7d080347ac3 MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents: 3572
diff changeset
54 method = "std-dev";
a7d080347ac3 MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents: 3572
diff changeset
55 }
4795
8ee270a3ef25 Small code cleanups in S/Q outlier tests.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4794
diff changeset
56
8ee270a3ef25 Small code cleanups in S/Q outlier tests.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4794
diff changeset
57 if (debug) {
8ee270a3ef25 Small code cleanups in S/Q outlier tests.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4794
diff changeset
58 log.debug("stdDevFactor: " + stdDevFactor);
8ee270a3ef25 Small code cleanups in S/Q outlier tests.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4794
diff changeset
59 log.debug("method: " + method);
8ee270a3ef25 Small code cleanups in S/Q outlier tests.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4794
diff changeset
60 }
8ee270a3ef25 Small code cleanups in S/Q outlier tests.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4794
diff changeset
61
3566
8d0f06b76e09 S/Q relation: Fixed flys/issue748
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3552
diff changeset
62 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
63
4795
8ee270a3ef25 Small code cleanups in S/Q outlier tests.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4794
diff changeset
64 double [] stdDev = new double[1];
8ee270a3ef25 Small code cleanups in S/Q outlier tests.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4794
diff changeset
65
8ee270a3ef25 Small code cleanups in S/Q outlier tests.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4794
diff changeset
66 boolean useGrubbs = method.equals(GRUBBS);
8ee270a3ef25 Small code cleanups in S/Q outlier tests.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4794
diff changeset
67
8ee270a3ef25 Small code cleanups in S/Q outlier tests.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4794
diff changeset
68 if (useGrubbs) {
8ee270a3ef25 Small code cleanups in S/Q outlier tests.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4794
diff changeset
69 stdDevFactor = Math.max(0d, Math.min(stdDevFactor/100d, 1d));
8ee270a3ef25 Small code cleanups in S/Q outlier tests.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4794
diff changeset
70 }
8ee270a3ef25 Small code cleanups in S/Q outlier tests.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4794
diff changeset
71
8ee270a3ef25 Small code cleanups in S/Q outlier tests.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4794
diff changeset
72 List<Double> values = new ArrayList<Double>(data.size());
8ee270a3ef25 Small code cleanups in S/Q outlier tests.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4794
diff changeset
73
3566
8d0f06b76e09 S/Q relation: Fixed flys/issue748
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3552
diff changeset
74 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
75
3566
8d0f06b76e09 S/Q relation: Fixed flys/issue748
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3552
diff changeset
76 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
77
4794
a7d080347ac3 MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents: 3572
diff changeset
78 for (SQ sq: data) {
a7d080347ac3 MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents: 3572
diff changeset
79 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
80 }
1e2733f749b5 SQ: Added specialized outlier test for the S/Q relation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
81
4795
8ee270a3ef25 Small code cleanups in S/Q outlier tests.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4794
diff changeset
82 Integer ndx = useGrubbs
8ee270a3ef25 Small code cleanups in S/Q outlier tests.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4794
diff changeset
83 ? GrubbsOutlier.findOutlier(values, stdDevFactor, stdDev)
8ee270a3ef25 Small code cleanups in S/Q outlier tests.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4794
diff changeset
84 : StdDevOutlier.findOutlier(values, stdDevFactor, stdDev);
8ee270a3ef25 Small code cleanups in S/Q outlier tests.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4794
diff changeset
85
4794
a7d080347ac3 MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents: 3572
diff changeset
86 if (ndx == null) {
a7d080347ac3 MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents: 3572
diff changeset
87 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
88 break;
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
4795
8ee270a3ef25 Small code cleanups in S/Q outlier tests.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4794
diff changeset
91 SQ outlier = data.remove(ndx.intValue());
8ee270a3ef25 Small code cleanups in S/Q outlier tests.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4794
diff changeset
92 if (debug) {
8ee270a3ef25 Small code cleanups in S/Q outlier tests.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4794
diff changeset
93 log.debug("stdDev: " + stdDev[0]);
4816
846b0441f905 Removed trailing whitespace.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4795
diff changeset
94 log.debug("removed " + ndx +
4795
8ee270a3ef25 Small code cleanups in S/Q outlier tests.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4794
diff changeset
95 "; S: " + outlier.getS() + " Q: " + outlier.getQ());
8ee270a3ef25 Small code cleanups in S/Q outlier tests.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4794
diff changeset
96 }
4794
a7d080347ac3 MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents: 3572
diff changeset
97 callback.iterationFinished(stdDev[0], outlier, data);
4795
8ee270a3ef25 Small code cleanups in S/Q outlier tests.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4794
diff changeset
98 values.clear();
3566
8d0f06b76e09 S/Q relation: Fixed flys/issue748
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3552
diff changeset
99 }
3188
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3187
diff changeset
100 }
3187
1e2733f749b5 SQ: Added specialized outlier test for the S/Q relation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
101 }
1e2733f749b5 SQ: Added specialized outlier test for the S/Q relation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
102 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org