Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/SQRelationCalculation.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 | e7a67407dea1 |
children | ba2f0bff0697 |
comparison
equal
deleted
inserted
replaced
3551:e7f1556192b3 | 3552:1df6984628c3 |
---|---|
10 import de.intevation.flys.artifacts.model.DateRange; | 10 import de.intevation.flys.artifacts.model.DateRange; |
11 import de.intevation.flys.artifacts.model.Parameters; | 11 import de.intevation.flys.artifacts.model.Parameters; |
12 | 12 |
13 import de.intevation.flys.backend.SedDBSessionHolder; | 13 import de.intevation.flys.backend.SedDBSessionHolder; |
14 | 14 |
15 import java.util.ArrayList; | |
15 import java.util.List; | 16 import java.util.List; |
16 | 17 |
17 import org.apache.log4j.Logger; | 18 import org.apache.log4j.Logger; |
18 | 19 |
19 public class SQRelationCalculation extends Calculation { | 20 public class SQRelationCalculation extends Calculation { |
33 | 34 |
34 public SQRelationCalculation(SQRelationAccess access) { | 35 public SQRelationCalculation(SQRelationAccess access) { |
35 | 36 |
36 String river = access.getRiver(); | 37 String river = access.getRiver(); |
37 Double location = access.getLocation(); | 38 Double location = access.getLocation(); |
38 DateRange period = access.getPeriod(); | 39 DateRange period = access.getPeriod(); |
39 Double outliers = access.getOutliers(); | 40 Double outliers = access.getOutliers(); |
40 | 41 |
41 //river = "Rhein"; | 42 //river = "Rhein"; |
42 | 43 |
43 if (river == null) { | 44 if (river == null) { |
97 log.error("No '" + SQ_FUNCTION_NAME + "' function found."); | 98 log.error("No '" + SQ_FUNCTION_NAME + "' function found."); |
98 // TODO: i18n | 99 // TODO: i18n |
99 addProblem("sq.missing.sq.function"); | 100 addProblem("sq.missing.sq.function"); |
100 } | 101 } |
101 | 102 |
102 | |
103 String [] parameterNames = function.getParameterNames(); | 103 String [] parameterNames = function.getParameterNames(); |
104 | 104 |
105 Fitting fitting = new Fitting(function, outliers); | 105 Measurements measurements = |
106 | 106 MeasurementFactory.getMeasurements(river, location, period); |
107 Measurements measurements = | |
108 MeasurementFactory.getMeasurements(river, location, period); | |
109 | 107 |
110 SQFractionResult [] fractionResults = | 108 SQFractionResult [] fractionResults = |
111 new SQFractionResult[SQResult.NUMBER_FRACTIONS]; | 109 new SQFractionResult[SQResult.NUMBER_FRACTIONS]; |
112 | 110 |
113 for (int i = 0; i < fractionResults.length; ++i) { | 111 for (int i = 0; i < fractionResults.length; ++i) { |
114 List<SQ> sqs = measurements.getSQs(i); | 112 List<SQ> sqs = measurements.getSQs(i); |
115 | 113 |
116 SQFractionResult fractionResult; | 114 SQFractionResult fractionResult; |
117 | 115 |
118 if (!fitting.fit(sqs)) { | 116 List<SQFractionResult.Iteration> iterations = |
117 doFitting(function, sqs); | |
118 | |
119 if (iterations == null) { | |
119 // TODO: i18n | 120 // TODO: i18n |
120 addProblem("sq.fitting.failed." + i); | 121 addProblem("sq.fitting.failed." + i); |
121 fractionResult = new SQFractionResult(); | 122 fractionResult = new SQFractionResult(); |
122 } | 123 } |
123 else { | 124 else { |
124 Parameters parameters = createParameters(parameterNames); | 125 fractionResult = new SQFractionResult( |
125 int row = parameters.newRow(); | 126 sqs.toArray(new SQ[sqs.size()]), |
126 double [] coeffs = fitting.getParameters(); | 127 iterations); |
127 for (int j = 0; j < parameterNames.length; ++j) { | 128 } |
128 parameters.set(row, parameterNames[j], coeffs[j]); | |
129 } | |
130 parameters.set(row, "chi_sqr", fitting.getChiSqr()); | |
131 parameters.set(row, "std_dev", fitting.getStandardDeviation()); | |
132 | 129 |
133 fractionResult = new SQFractionResult( | |
134 parameters, | |
135 fitting.getRemaining(), | |
136 fitting.getOutliers()); | |
137 } | |
138 fitting.reset(); | |
139 fractionResults[i] = fractionResult; | 130 fractionResults[i] = fractionResult; |
140 } | 131 } |
141 | 132 |
142 return new CalculationResult( | 133 return new CalculationResult( |
143 new SQResult[] { new SQResult(location, fractionResults) }, | 134 new SQResult[] { new SQResult(location, fractionResults) }, |
144 this); | 135 this); |
145 } | 136 } |
146 | 137 |
147 public static final Parameters createParameters(String [] names) { | 138 protected List<SQFractionResult.Iteration> doFitting( |
139 final Function function, | |
140 List<SQ> sqs | |
141 ) { | |
142 final List<SQFractionResult.Iteration> iterations = | |
143 new ArrayList<SQFractionResult.Iteration>(); | |
148 | 144 |
145 boolean success = new Fitting(function, outliers).fit( | |
146 sqs, | |
147 new Fitting.Callback() { | |
148 @Override | |
149 public void afterIteration( | |
150 double [] coeffs, | |
151 SQ [] measurements, | |
152 SQ [] outliers, | |
153 double standardDeviation, | |
154 double chiSqr | |
155 ) { | |
156 Parameters parameters = createParameters( | |
157 function.getParameterNames(), | |
158 coeffs, | |
159 standardDeviation, | |
160 chiSqr); | |
161 iterations.add(new SQFractionResult.Iteration( | |
162 parameters, | |
163 measurements, | |
164 outliers)); | |
165 } | |
166 }); | |
167 | |
168 return success ? iterations : null; | |
169 } | |
170 | |
171 public static final Parameters createParameters( | |
172 String [] names, | |
173 double [] values, | |
174 double standardDeviation, | |
175 double chiSqr | |
176 ) { | |
149 String [] columns = new String[names.length + 2]; | 177 String [] columns = new String[names.length + 2]; |
150 columns[0] = "chi_sqr"; | 178 columns[0] = "chi_sqr"; |
151 columns[1] = "std_dev"; | 179 columns[1] = "std_dev"; |
152 System.arraycopy(names, 0, columns, 2, names.length); | 180 System.arraycopy(names, 0, columns, 2, names.length); |
153 return new Parameters(columns); | 181 Parameters parameters = new Parameters(columns); |
182 int row = parameters.newRow(); | |
183 parameters.set(row, names, values); | |
184 parameters.set(row, "chi_sqr", chiSqr); | |
185 parameters.set(row, "std_dev", standardDeviation); | |
186 return parameters; | |
154 } | 187 } |
155 } | 188 } |
156 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : | 189 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : |