Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/fixings/Fitting.java @ 3096:d7b0f52d6d04
FixA: Calculate Delta W(t) for reference points, too.
flys-artifacts/trunk@4695 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Mon, 18 Jun 2012 16:00:19 +0000 |
parents | 0ace00c0c12a |
children | cbf308f5c41b |
comparison
equal
deleted
inserted
replaced
3095:6ea299c208cd | 3096:d7b0f52d6d04 |
---|---|
23 public class Fitting | 23 public class Fitting |
24 { | 24 { |
25 private static Logger log = Logger.getLogger(Fitting.class); | 25 private static Logger log = Logger.getLogger(Fitting.class); |
26 | 26 |
27 /** Use instance of this factory to find meta infos for outliers. */ | 27 /** Use instance of this factory to find meta infos for outliers. */ |
28 public interface QWFactory { | 28 public interface QWDFactory { |
29 | 29 |
30 QW create(double q, double w); | 30 QWD create(double q, double w); |
31 | 31 |
32 } // interface QWFactory | 32 } // interface QWFactory |
33 | 33 |
34 public static final QWFactory QW_FACTORY = new QWFactory() { | 34 public static final QWDFactory QWD_FACTORY = new QWDFactory() { |
35 @Override | 35 @Override |
36 public QW create(double q, double w) { | 36 public QWD create(double q, double w) { |
37 return new QW(q, w); | 37 return new QWD(q, w); |
38 } | 38 } |
39 }; | 39 }; |
40 | 40 |
41 protected boolean checkOutliers; | 41 protected boolean checkOutliers; |
42 protected Function function; | 42 protected Function function; |
43 protected QWFactory qwFactory; | 43 protected QWDFactory qwdFactory; |
44 protected double chiSqr; | 44 protected double chiSqr; |
45 protected double [] parameters; | 45 protected double [] parameters; |
46 protected ArrayList<QW> removed; | 46 protected ArrayList<QW> removed; |
47 protected QW [] referenced; | 47 protected QWD [] referenced; |
48 | 48 |
49 | 49 |
50 public Fitting() { | 50 public Fitting() { |
51 removed = new ArrayList<QW>(); | 51 removed = new ArrayList<QW>(); |
52 } | 52 } |
53 | 53 |
54 public Fitting(Function function) { | 54 public Fitting(Function function) { |
55 this(function, QW_FACTORY); | 55 this(function, QWD_FACTORY); |
56 } | 56 } |
57 | 57 |
58 public Fitting(Function function, QWFactory qwFactory) { | 58 public Fitting(Function function, QWDFactory qwdFactory) { |
59 this(function, qwFactory, false); | 59 this(function, qwdFactory, false); |
60 } | 60 } |
61 | 61 |
62 public Fitting( | 62 public Fitting( |
63 Function function, | 63 Function function, |
64 QWFactory qwFactory, | 64 QWDFactory qwdFactory, |
65 boolean checkOutliers | 65 boolean checkOutliers |
66 ) { | 66 ) { |
67 this(); | 67 this(); |
68 this.function = function; | 68 this.function = function; |
69 this.qwFactory = qwFactory; | 69 this.qwdFactory = qwdFactory; |
70 this.checkOutliers = checkOutliers; | 70 this.checkOutliers = checkOutliers; |
71 } | 71 } |
72 | 72 |
73 public Function getFunction() { | 73 public Function getFunction() { |
74 return function; | 74 return function; |
107 | 107 |
108 public QW [] outliersToArray() { | 108 public QW [] outliersToArray() { |
109 return removed.toArray(new QW[removed.size()]); | 109 return removed.toArray(new QW[removed.size()]); |
110 } | 110 } |
111 | 111 |
112 public QW [] referencedToArray() { | 112 public QWD [] referencedToArray() { |
113 return referenced != null ? (QW [])referenced.clone() : null; | 113 return referenced != null ? (QWD [])referenced.clone() : null; |
114 } | 114 } |
115 | 115 |
116 public double getMaxQ() { | 116 public double getMaxQ() { |
117 double maxQ = -Double.MAX_VALUE; | 117 double maxQ = -Double.MAX_VALUE; |
118 if (referenced != null) { | 118 if (referenced != null) { |
151 | 151 |
152 LevenbergMarquardtOptimizer lmo = new LevenbergMarquardtOptimizer(); | 152 LevenbergMarquardtOptimizer lmo = new LevenbergMarquardtOptimizer(); |
153 | 153 |
154 List<IndexedValue> inputs = new ArrayList<IndexedValue>(xs.size()); | 154 List<IndexedValue> inputs = new ArrayList<IndexedValue>(xs.size()); |
155 | 155 |
156 de.intevation.flys.artifacts.math.Function instance = null; | |
157 | |
156 for (;;) { | 158 for (;;) { |
157 CurveFitter cf = new CurveFitter(lmo); | 159 CurveFitter cf = new CurveFitter(lmo); |
158 | 160 |
159 for (int i = 0, N = xs.size(); i < N; ++i) { | 161 for (int i = 0, N = xs.size(); i < N; ++i) { |
160 cf.addObservedPoint(xs.getQuick(i), ys.getQuick(i)); | 162 cf.addObservedPoint(xs.getQuick(i), ys.getQuick(i)); |
173 } | 175 } |
174 | 176 |
175 inputs.clear(); | 177 inputs.clear(); |
176 | 178 |
177 // This is the paraterized function for a given km. | 179 // This is the paraterized function for a given km. |
178 de.intevation.flys.artifacts.math.Function instance = | 180 instance = function.instantiate(parameters); |
179 function.instantiate(parameters); | |
180 | 181 |
181 for (int i = 0, N = xs.size(); i < N; ++i) { | 182 for (int i = 0, N = xs.size(); i < N; ++i) { |
182 double y = instance.value(xs.getQuick(i)); | 183 double y = instance.value(xs.getQuick(i)); |
183 if (Double.isNaN(y)) { | 184 if (Double.isNaN(y)) { |
184 continue; | 185 continue; |
195 List<IndexedValue> rem = outliers.getRemoved(); | 196 List<IndexedValue> rem = outliers.getRemoved(); |
196 | 197 |
197 for (int i = rem.size()-1; i >= 0; --i) { | 198 for (int i = rem.size()-1; i >= 0; --i) { |
198 int idx = rem.get(i).getIndex(); | 199 int idx = rem.get(i).getIndex(); |
199 removed.add( | 200 removed.add( |
200 qwFactory.create( | 201 qwdFactory.create( |
201 xs.getQuick(idx), ys.getQuick(idx))); | 202 xs.getQuick(idx), ys.getQuick(idx))); |
202 xs.remove(idx); | 203 xs.remove(idx); |
203 ys.remove(idx); | 204 ys.remove(idx); |
204 } | 205 } |
205 } | 206 } |
206 | 207 |
207 referenced = new QW[xs.size()]; | 208 referenced = new QWD[xs.size()]; |
208 for (int i = 0; i < referenced.length; ++i) { | 209 for (int i = 0; i < referenced.length; ++i) { |
209 QW qw = qwFactory.create(xs.getQuick(i), ys.getQuick(i)); | 210 QWD qwd = qwdFactory.create(xs.getQuick(i), ys.getQuick(i)); |
210 | 211 |
211 if (qw == null) { | 212 if (qwd == null) { |
212 log.warn("QW creation failed!"); | 213 log.warn("QW creation failed!"); |
213 } | 214 } |
214 else { | 215 else { |
215 referenced[i] = qw; | 216 referenced[i] = qwd; |
217 double dw = (qwd.getW() - instance.value(qwd.getQ()))*100.0; | |
218 qwd.setDeltaW(dw); | |
216 } | 219 } |
217 } | 220 } |
218 | 221 |
219 chiSqr = lmo.getChiSquare(); | 222 chiSqr = lmo.getChiSquare(); |
220 | 223 |