Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/artifacts/model/fixings/Fitting.java @ 9348:a3f318347707
Show wq outliers within same thems with different symbol: not ready yet
author | gernotbelger |
---|---|
date | Tue, 31 Jul 2018 11:25:38 +0200 |
parents | 83bb52fa0c32 |
children | ddcd52d239cd |
comparison
equal
deleted
inserted
replaced
9344:c08003a68478 | 9348:a3f318347707 |
---|---|
20 import org.apache.log4j.Logger; | 20 import org.apache.log4j.Logger; |
21 | 21 |
22 import org.dive4elements.river.artifacts.math.GrubbsOutlier; | 22 import org.dive4elements.river.artifacts.math.GrubbsOutlier; |
23 import org.dive4elements.river.artifacts.math.fitting.Function; | 23 import org.dive4elements.river.artifacts.math.fitting.Function; |
24 | 24 |
25 public class Fitting | 25 public class Fitting { |
26 { | |
27 private static Logger log = Logger.getLogger(Fitting.class); | 26 private static Logger log = Logger.getLogger(Fitting.class); |
28 | 27 |
29 /** Use instance of this factory to find meta infos for outliers. */ | 28 /** Use instance of this factory to find meta infos for outliers. */ |
30 public interface QWDFactory { | 29 public interface QWDFactory { |
31 | 30 QWD create(double q, double w, boolean isOutlier); |
32 QWD create(double q, double w); | 31 } |
33 | 32 |
34 } // interface QWFactory | 33 private final boolean checkOutliers; |
35 | 34 |
36 public static final QWDFactory QWD_FACTORY = new QWDFactory() { | 35 private final Function function; |
37 @Override | 36 |
38 public QWD create(double q, double w) { | 37 private final QWDFactory qwdFactory; |
39 return new QWD(q, w); | 38 |
40 } | 39 private double chiSqr; |
41 }; | 40 |
42 | 41 private double[] parameters; |
43 protected boolean checkOutliers; | 42 |
44 protected Function function; | 43 private final List<QWD> removed = new ArrayList<>(); |
45 protected QWDFactory qwdFactory; | 44 |
46 protected double chiSqr; | 45 private QWD[] referenced; |
47 protected double [] parameters; | 46 |
48 protected ArrayList<QWI> removed; | 47 private double standardDeviation; |
49 protected QWD [] referenced; | 48 |
50 protected double standardDeviation; | 49 public Fitting(Function function, QWDFactory qwdFactory, boolean checkOutliers) { |
51 | 50 this.function = function; |
52 | 51 this.qwdFactory = qwdFactory; |
53 public Fitting() { | |
54 removed = new ArrayList<QWI>(); | |
55 } | |
56 | |
57 public Fitting(Function function) { | |
58 this(function, QWD_FACTORY); | |
59 } | |
60 | |
61 public Fitting(Function function, QWDFactory qwdFactory) { | |
62 this(function, qwdFactory, false); | |
63 } | |
64 | |
65 public Fitting( | |
66 Function function, | |
67 QWDFactory qwdFactory, | |
68 boolean checkOutliers | |
69 ) { | |
70 this(); | |
71 this.function = function; | |
72 this.qwdFactory = qwdFactory; | |
73 this.checkOutliers = checkOutliers; | 52 this.checkOutliers = checkOutliers; |
74 } | 53 } |
75 | 54 |
76 public Function getFunction() { | 55 public Function getFunction() { |
77 return function; | 56 return function; |
78 } | 57 } |
79 | 58 |
80 public void setFunction(Function function) { | |
81 this.function = function; | |
82 } | |
83 | |
84 public boolean getCheckOutliers() { | 59 public boolean getCheckOutliers() { |
85 return checkOutliers; | 60 return checkOutliers; |
86 } | 61 } |
87 | 62 |
88 public void setCheckOutliers(boolean checkOutliers) { | |
89 this.checkOutliers = checkOutliers; | |
90 } | |
91 | |
92 public double getChiSquare() { | 63 public double getChiSquare() { |
93 return chiSqr; | 64 return chiSqr; |
94 } | 65 } |
95 | 66 |
96 public void reset() { | 67 public void reset() { |
97 chiSqr = 0.0; | 68 chiSqr = 0.0; |
98 parameters = null; | 69 parameters = null; |
99 removed.clear(); | 70 removed.clear(); |
100 referenced = null; | 71 referenced = null; |
101 standardDeviation = 0.0; | 72 standardDeviation = 0.0; |
102 } | 73 } |
103 | 74 |
104 public boolean hasOutliers() { | 75 public boolean hasOutliers() { |
105 return !removed.isEmpty(); | 76 return !removed.isEmpty(); |
106 } | 77 } |
107 | 78 |
108 public List<QWI> getOutliers() { | 79 public QWD[] outliersToArray() { |
109 return removed; | 80 return removed.toArray(new QWD[removed.size()]); |
110 } | 81 } |
111 | 82 |
112 public QWI [] outliersToArray() { | 83 public QWD[] referencedToArray() { |
113 return removed.toArray(new QWI[removed.size()]); | 84 return referenced != null ? (QWD[]) referenced.clone() : null; |
114 } | |
115 | |
116 public QWD [] referencedToArray() { | |
117 return referenced != null ? (QWD [])referenced.clone() : null; | |
118 } | 85 } |
119 | 86 |
120 public double getMaxQ() { | 87 public double getMaxQ() { |
121 double maxQ = -Double.MAX_VALUE; | 88 double maxQ = -Double.MAX_VALUE; |
122 if (referenced != null) { | 89 if (referenced != null) { |
123 for (QWI qw: referenced) { | 90 for (QWI qw : referenced) { |
124 double q = qw.getQ(); | 91 double q = qw.getQ(); |
125 if (q > maxQ) { | 92 if (q > maxQ) { |
126 maxQ = q; | 93 maxQ = q; |
127 } | 94 } |
128 } | 95 } |
129 } | 96 } |
130 return maxQ; | 97 return maxQ; |
131 } | 98 } |
132 | 99 |
133 public double [] getParameters() { | 100 public double[] getParameters() { |
134 return parameters; | 101 return parameters; |
135 } | 102 } |
136 | 103 |
137 public double getStandardDeviation() { | 104 public double getStandardDeviation() { |
138 return standardDeviation; | 105 return standardDeviation; |
139 } | 106 } |
140 | 107 |
141 public boolean fit(double [] qs, double [] ws) { | 108 public boolean fit(double[] qs, double[] ws) { |
142 | 109 |
143 TDoubleArrayList xs = new TDoubleArrayList(qs.length); | 110 TDoubleArrayList xs = new TDoubleArrayList(qs.length); |
144 TDoubleArrayList ys = new TDoubleArrayList(ws.length); | 111 TDoubleArrayList ys = new TDoubleArrayList(ws.length); |
145 | 112 |
146 for (int i = 0; i < qs.length; ++i) { | 113 for (int i = 0; i < qs.length; ++i) { |
186 } | 153 } |
187 } | 154 } |
188 } | 155 } |
189 if (parameters == null) { | 156 if (parameters == null) { |
190 /* | 157 /* |
191 log.debug("Parameters is null"); | 158 * log.debug("Parameters is null"); |
192 for (int i = 0, N = xs.size(); i < N; ++i) { | 159 * for (int i = 0, N = xs.size(); i < N; ++i) { |
193 log.debug("DATA: " + xs.getQuick(i) + " " + ys.getQuick(i)); | 160 * log.debug("DATA: " + xs.getQuick(i) + " " + ys.getQuick(i)); |
194 }*/ | 161 * } |
162 */ | |
195 return false; | 163 return false; |
196 } | 164 } |
197 | 165 |
198 // This is the paraterized function for a given km. | 166 // This is the paraterized function for a given km. |
199 instance = function.instantiate(parameters); | 167 instance = function.instantiate(parameters); |
217 if (outlier == null) { | 185 if (outlier == null) { |
218 break; | 186 break; |
219 } | 187 } |
220 | 188 |
221 int idx = outlier.intValue(); | 189 int idx = outlier.intValue(); |
222 removed.add( | 190 removed.add(qwdFactory.create(xs.getQuick(idx), ys.getQuick(idx), true)); |
223 qwdFactory.create( | |
224 xs.getQuick(idx), ys.getQuick(idx))); | |
225 xs.remove(idx); | 191 xs.remove(idx); |
226 ys.remove(idx); | 192 ys.remove(idx); |
227 } | 193 } |
228 | 194 |
229 StandardDeviation stdDev = new StandardDeviation(); | 195 StandardDeviation stdDev = new StandardDeviation(); |
230 | 196 |
231 referenced = new QWD[xs.size()]; | 197 referenced = new QWD[xs.size()]; |
232 for (int i = 0; i < referenced.length; ++i) { | 198 for (int i = 0; i < referenced.length; ++i) { |
233 QWD qwd = qwdFactory.create(xs.getQuick(i), ys.getQuick(i)); | 199 QWD qwd = qwdFactory.create(xs.getQuick(i), ys.getQuick(i), false); |
234 | 200 |
235 if (qwd == null) { | 201 if (qwd == null) { |
236 log.warn("QW creation failed!"); | 202 log.warn("QW creation failed!"); |
237 } | 203 } else { |
238 else { | |
239 referenced[i] = qwd; | 204 referenced[i] = qwd; |
240 double dw = (qwd.getW() - instance.value(qwd.getQ()))*100.0; | 205 double dw = (qwd.getW() - instance.value(qwd.getQ())) * 100.0; |
241 qwd.setDeltaW(dw); | 206 qwd.setDeltaW(dw); |
242 stdDev.increment(dw); | 207 stdDev.increment(dw); |
243 } | 208 } |
244 } | 209 } |
245 | 210 |
248 chiSqr = lmo.getChiSquare(); | 213 chiSqr = lmo.getChiSquare(); |
249 | 214 |
250 return true; | 215 return true; |
251 } | 216 } |
252 } | 217 } |
253 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |