Mercurial > dive4elements > gnv-client
comparison gnv-artifacts/src/main/java/de/intevation/gnv/statistics/TimeseriesStatistics.java @ 253:07650fc6014c
Put a name to each Statistic group which is similar to the name of the
Series in the Charts. issue83
gnv-artifacts/trunk@326 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Tim Englich <tim.englich@intevation.de> |
---|---|
date | Thu, 12 Nov 2009 10:15:05 +0000 |
parents | 7fb9441dd8af |
children | 5403452c06fc |
comparison
equal
deleted
inserted
replaced
252:f1e7ddeef5bc | 253:07650fc6014c |
---|---|
24 import org.apache.commons.math.stat.regression.SimpleRegression; | 24 import org.apache.commons.math.stat.regression.SimpleRegression; |
25 import org.apache.log4j.Logger; | 25 import org.apache.log4j.Logger; |
26 | 26 |
27 import de.intevation.gnv.geobackend.base.Result; | 27 import de.intevation.gnv.geobackend.base.Result; |
28 import de.intevation.gnv.statistics.exception.StatisticsException; | 28 import de.intevation.gnv.statistics.exception.StatisticsException; |
29 import de.intevation.gnv.transition.describedata.KeyValueDescibeData; | |
29 | 30 |
30 /** | 31 /** |
31 * The class <code>TimeseriesStatistics</code> fulfills the following purposes: | 32 * The class <code>TimeseriesStatistics</code> fulfills the following purposes: |
32 * <ol> | 33 * <ol> |
33 * <li></li> | 34 * <li></li> |
46 * Default Logging instance | 47 * Default Logging instance |
47 */ | 48 */ |
48 private static Logger log = Logger.getLogger(TimeseriesStatistics.class); | 49 private static Logger log = Logger.getLogger(TimeseriesStatistics.class); |
49 private static boolean sDebug = log.isDebugEnabled(); | 50 private static boolean sDebug = log.isDebugEnabled(); |
50 | 51 |
51 private Collection<Statistic> statistics = null; | |
52 | 52 |
53 public TimeseriesStatistics() { | 53 public TimeseriesStatistics() { |
54 super(); | 54 super(); |
55 } | 55 } |
56 | 56 |
57 /** | 57 /** |
58 * @see de.intevation.gnv.statistics.Statistics#calculateStatistics(java.util.Collection) | 58 * @see de.intevation.gnv.statistics.Statistics#calculateStatistics(java.util.Collection, java.util.Collection, java.util.Collection, java.util.Collection) |
59 */ | 59 */ |
60 public Collection<Statistic> calculateStatistics( | 60 public Collection<StatisticSet> calculateStatistics( |
61 Collection<Result> resultSet) | 61 Collection<Result> resultSet, |
62 throws StatisticsException { | 62 Collection<KeyValueDescibeData> parameters, |
63 Collection<KeyValueDescibeData> measurements, | |
64 Collection<KeyValueDescibeData> dates | |
65 ) | |
66 throws StatisticsException { | |
63 DescriptiveStatistics lStatistics = null; | 67 DescriptiveStatistics lStatistics = null; |
64 SimpleRegression lRegression = null; | 68 SimpleRegression lRegression = null; |
65 statistics = new ArrayList<Statistic>(); | 69 Collection<StatisticSet> statisticSets = new ArrayList<StatisticSet>(); |
66 String break1, break2; | 70 String break1, break2, break3; |
67 int lSeries = 1; | 71 int lSeries = 1; |
68 try { | 72 try { |
69 | 73 |
70 Iterator<Result> resultIterator = resultSet.iterator(); | 74 Iterator<Result> resultIterator = resultSet.iterator(); |
71 if (resultIterator.hasNext()) { | 75 if (resultIterator.hasNext()) { |
72 Result row = resultIterator.next(); | 76 Result row = resultIterator.next(); |
73 break1 = row.getString("GROUP1"); | 77 break1 = row.getString("GROUP1"); |
74 break2 = row.getString("GROUP2"); | 78 break2 = row.getString("GROUP2"); |
79 break3 = row.getString("GROUP3"); | |
75 lRegression = new SimpleRegression(); | 80 lRegression = new SimpleRegression(); |
76 lStatistics = DescriptiveStatistics.newInstance(); | 81 lStatistics = DescriptiveStatistics.newInstance(); |
77 while (resultIterator.hasNext()) { | 82 while (resultIterator.hasNext()) { |
78 | 83 |
79 if (!break1.equals(row.getString("GROUP1")) | 84 if (!break1.equals(row.getString("GROUP1")) |
80 || !break2.equals(row.getString("GROUP2"))) { | 85 || !break2.equals(row.getString("GROUP2")) |
81 addStatisticsValues(lStatistics, lRegression, lSeries); | 86 || !break3.equals(row.getString("GROUP3")) |
87 ) { | |
88 String statisticsName = this.generateStatisticsName(break1, break2, | |
89 break3, parameters, | |
90 measurements, dates); | |
91 statisticSets.add(this.generateStatisticsValues(lStatistics, | |
92 lRegression, | |
93 statisticsName)); | |
82 | 94 |
83 lStatistics.clear(); | 95 lStatistics.clear(); |
84 lRegression.clear(); | 96 lRegression.clear(); |
85 | 97 |
86 lStatistics.addValue(row.getDouble("YORDINATE")); | 98 lStatistics.addValue(row.getDouble("YORDINATE")); |
87 Double x = this.calculateXOrdinateValue(row); | 99 Double x = this.calculateXOrdinateValue(row); |
88 lRegression.addData(x, row.getDouble("YORDINATE")); | 100 lRegression.addData(x, row.getDouble("YORDINATE")); |
89 | 101 |
90 break1 = row.getString("GROUP1"); | 102 break1 = row.getString("GROUP1"); |
91 break2 = row.getString("GROUP2"); | 103 break2 = row.getString("GROUP2"); |
104 break3 = row.getString("GROUP3"); | |
92 row = resultIterator.next(); | 105 row = resultIterator.next(); |
93 lSeries++; | 106 lSeries++; |
94 } else { | 107 } else { |
95 | 108 |
96 lStatistics.addValue(row.getDouble("YORDINATE")); | 109 lStatistics.addValue(row.getDouble("YORDINATE")); |
98 lRegression.addData(x, row.getDouble("YORDINATE")); | 111 lRegression.addData(x, row.getDouble("YORDINATE")); |
99 row = resultIterator.next(); | 112 row = resultIterator.next(); |
100 } | 113 } |
101 | 114 |
102 } | 115 } |
103 addStatisticsValues(lStatistics, lRegression, lSeries); | 116 String statisticsName = this.generateStatisticsName(break1, break2, |
117 break3, parameters, | |
118 measurements, dates); | |
119 | |
120 statisticSets.add(this.generateStatisticsValues(lStatistics, | |
121 lRegression, | |
122 statisticsName)); | |
104 lStatistics.clear(); | 123 lStatistics.clear(); |
105 lRegression.clear(); | 124 lRegression.clear(); |
106 } | 125 } |
107 } catch (Exception e) { | 126 } catch (Exception e) { |
108 log.error(e.getMessage(), e); | 127 log.error(e.getMessage(), e); |
109 } | 128 } |
110 | 129 |
111 return statistics; | 130 return statisticSets; |
112 } | 131 } |
113 | 132 |
133 /** | |
134 * | |
135 * @param break1 | |
136 * @param break2 | |
137 * @param break3 | |
138 * @param parameters | |
139 * @param measurements | |
140 * @param dates | |
141 * @return | |
142 */ | |
143 protected String generateStatisticsName(String break1, | |
144 String break2, | |
145 String break3, | |
146 Collection<KeyValueDescibeData> parameters, | |
147 Collection<KeyValueDescibeData> measurements, | |
148 Collection<KeyValueDescibeData> dates){ | |
149 log.debug("TimeseriesStatistics.generateStatisticsName"); | |
150 return this.findValueTitle(parameters,break1)+ " "+ | |
151 this.findValueTitle(measurements,break2) + "m"; | |
152 } | |
153 | |
154 | |
155 protected String findValueTitle(Collection<KeyValueDescibeData> values, | |
156 String id) { | |
157 log.debug("TimeseriesStatistics.findValueTitle "+ id); | |
158 if (values != null) { | |
159 Iterator<KeyValueDescibeData> it = values.iterator(); | |
160 while (it.hasNext()) { | |
161 KeyValueDescibeData data = it.next(); | |
162 if (id.equals(data.getKey())) { | |
163 return data.getValue(); | |
164 } | |
165 } | |
166 } | |
167 return ""; | |
168 } | |
114 protected double calculateXOrdinateValue(Result row) throws SQLException { | 169 protected double calculateXOrdinateValue(Result row) throws SQLException { |
115 return new Double((row.getDate("XORDINATE")).getTime() / 1000 / 3600); | 170 return new Double((row.getDate("XORDINATE")).getTime() / 1000 / 3600); |
116 } | 171 } |
117 | 172 |
118 /** | 173 /** |
119 * @param lStatistics | 174 * @param lStatistics |
120 * @param lRegression | 175 * @param lRegression |
121 * @param lStats | 176 * @param lStats |
122 * @param lSeries | 177 * @param lSeries |
123 */ | 178 */ |
124 private void addStatisticsValues(DescriptiveStatistics lStatistics, | 179 private StatisticSet generateStatisticsValues(DescriptiveStatistics lStatistics, |
125 SimpleRegression lRegression, int lSeries) { | 180 SimpleRegression lRegression, String statisticName) { |
126 statistics.add(new Statistic("gnviewer.statistics.series", | 181 |
127 lSeries)); | 182 StatisticSet statisticSet = new StatisticSet(statisticName); |
128 statistics.add(new Statistic( | 183 |
184 statisticSet.addStatistic(new Statistic( | |
129 "gnviewer.statistics.descriptive.arithmeticMean", lStatistics | 185 "gnviewer.statistics.descriptive.arithmeticMean", lStatistics |
130 .getMean())); | 186 .getMean())); |
131 statistics.add(new Statistic( | 187 statisticSet.addStatistic(new Statistic( |
132 "gnviewer.statistics.descriptive.kurtosis", lStatistics | 188 "gnviewer.statistics.descriptive.kurtosis", lStatistics |
133 .getKurtosis())); | 189 .getKurtosis())); |
134 statistics.add(new Statistic("gnviewer.statistics.descriptive.max", | 190 statisticSet.addStatistic(new Statistic("gnviewer.statistics.descriptive.max", |
135 lStatistics.getMax())); | 191 lStatistics.getMax())); |
136 statistics.add(new Statistic("gnviewer.statistics.descriptive.min", | 192 statisticSet.addStatistic(new Statistic("gnviewer.statistics.descriptive.min", |
137 lStatistics.getMin())); | 193 lStatistics.getMin())); |
138 statistics.add(new Statistic("gnviewer.statistics.descriptive.n", | 194 statisticSet.addStatistic(new Statistic("gnviewer.statistics.descriptive.n", |
139 lStatistics.getN())); | 195 lStatistics.getN())); |
140 statistics.add(new Statistic( | 196 statisticSet.addStatistic(new Statistic( |
141 "gnviewer.statistics.descriptive.percentile.90", lStatistics | 197 "gnviewer.statistics.descriptive.percentile.90", lStatistics |
142 .getPercentile(90))); | 198 .getPercentile(90))); |
143 statistics.add(new Statistic( | 199 statisticSet.addStatistic(new Statistic( |
144 "gnviewer.statistics.descriptive.percentile.75", lStatistics | 200 "gnviewer.statistics.descriptive.percentile.75", lStatistics |
145 .getPercentile(75))); | 201 .getPercentile(75))); |
146 statistics.add(new Statistic( | 202 statisticSet.addStatistic(new Statistic( |
147 "gnviewer.statistics.descriptive.percentile.50", lStatistics | 203 "gnviewer.statistics.descriptive.percentile.50", lStatistics |
148 .getPercentile(50))); | 204 .getPercentile(50))); |
149 statistics.add(new Statistic( | 205 statisticSet.addStatistic(new Statistic( |
150 "gnviewer.statistics.descriptive.percentile.10", lStatistics | 206 "gnviewer.statistics.descriptive.percentile.10", lStatistics |
151 .getPercentile(10))); | 207 .getPercentile(10))); |
152 statistics.add(new Statistic( | 208 statisticSet.addStatistic(new Statistic( |
153 "gnviewer.statistics.descriptive.deviation", lStatistics | 209 "gnviewer.statistics.descriptive.deviation", lStatistics |
154 .getStandardDeviation())); | 210 .getStandardDeviation())); |
155 statistics.add(new Statistic( | 211 statisticSet.addStatistic(new Statistic( |
156 "gnviewer.statistics.descriptive.variance", lStatistics | 212 "gnviewer.statistics.descriptive.variance", lStatistics |
157 .getVariance())); | 213 .getVariance())); |
158 statistics.add(new Statistic( | 214 statisticSet.addStatistic(new Statistic( |
159 "gnviewer.statistics.descriptive.intercept", lRegression | 215 "gnviewer.statistics.descriptive.intercept", lRegression |
160 .getIntercept())); | 216 .getIntercept())); |
161 statistics.add(new Statistic("gnviewer.statistics.descriptive.slope", | 217 statisticSet.addStatistic(new Statistic("gnviewer.statistics.descriptive.slope", |
162 lRegression.getSlope())); | 218 lRegression.getSlope())); |
219 return statisticSet; | |
163 } | 220 } |
164 } | 221 } |