MtasDataItemFull.java
7.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package mtas.codec.util.collector;
import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
import mtas.codec.util.CodecUtil;
/**
* The Class MtasDataItemFull.
*
* @param <T1> the generic type
* @param <T2> the generic type
*/
abstract class MtasDataItemFull<T1 extends Number & Comparable<T1>, T2 extends Number & Comparable<T2>>
extends MtasDataItem<T1, T2> implements Serializable {
/** The Constant serialVersionUID. */
private static final long serialVersionUID = 1L;
/** The full values. */
public T1[] fullValues;
/** The operations. */
protected MtasDataOperations<T1, T2> operations;
/** The stats. */
protected DescriptiveStatistics stats = null;
/** The fp stats function items. */
private Pattern fpStatsFunctionItems = Pattern
.compile("(([^\\(,]+)(\\(([^\\)]*)\\))?)");
/**
* Instantiates a new mtas data item full.
*
* @param value the value
* @param sub the sub
* @param statsItems the stats items
* @param sortType the sort type
* @param sortDirection the sort direction
* @param errorNumber the error number
* @param errorList the error list
* @param operations the operations
* @param sourceNumber the source number
*/
public MtasDataItemFull(T1[] value, MtasDataCollector<?, ?> sub,
Set<String> statsItems, String sortType, String sortDirection,
int errorNumber, Map<String, Integer> errorList,
MtasDataOperations<T1, T2> operations, int sourceNumber) {
super(sub, statsItems, sortType, sortDirection, errorNumber, errorList,
sourceNumber);
this.fullValues = value;
this.operations = operations;
}
/*
* (non-Javadoc)
*
* @see mtas.codec.util.DataCollector.MtasDataItem#add(mtas.codec.util.
* DataCollector.MtasDataItem)
*/
@Override
public void add(MtasDataItem<T1, T2> newItem) throws IOException {
if (newItem instanceof MtasDataItemFull) {
MtasDataItemFull<T1, T2> newTypedItem = (MtasDataItemFull<T1, T2>) newItem;
T1[] tmpValue = operations
.createVector1(fullValues.length + newTypedItem.fullValues.length);
System.arraycopy(fullValues, 0, tmpValue, 0, fullValues.length);
System.arraycopy(newTypedItem.fullValues, 0, tmpValue, fullValues.length,
newTypedItem.fullValues.length);
fullValues = tmpValue;
recomputeComparableSortValue = true;
} else {
throw new IOException("can only add MtasDataItemFull");
}
}
/**
* Creates the stats.
*/
protected void createStats() {
if (stats == null) {
stats = new DescriptiveStatistics();
for (T1 value : fullValues) {
stats.addValue(value.doubleValue());
}
}
}
/**
* Gets the distribution.
*
* @param arguments the arguments
* @return the distribution
*/
abstract protected HashMap<String, Object> getDistribution(String arguments);
/*
* (non-Javadoc)
*
* @see mtas.codec.util.DataCollector.MtasDataItem#rewrite()
*/
@Override
public Map<String, Object> rewrite(boolean showDebugInfo) throws IOException {
createStats();
Map<String, Object> response = new HashMap<>();
for (String statsItem : getStatsItems()) {
if (statsItem.equals(CodecUtil.STATS_TYPE_SUM)) {
response.put(statsItem, stats.getSum());
} else if (statsItem.equals(CodecUtil.STATS_TYPE_N)) {
response.put(statsItem, stats.getN());
} else if (statsItem.equals(CodecUtil.STATS_TYPE_MAX)) {
response.put(statsItem, stats.getMax());
} else if (statsItem.equals(CodecUtil.STATS_TYPE_MIN)) {
response.put(statsItem, stats.getMin());
} else if (statsItem.equals(CodecUtil.STATS_TYPE_SUMSQ)) {
response.put(statsItem, stats.getSumsq());
} else if (statsItem.equals(CodecUtil.STATS_TYPE_SUMOFLOGS)) {
response.put(statsItem,
stats.getN() * Math.log(stats.getGeometricMean()));
} else if (statsItem.equals(CodecUtil.STATS_TYPE_MEAN)) {
response.put(statsItem, stats.getMean());
} else if (statsItem.equals(CodecUtil.STATS_TYPE_GEOMETRICMEAN)) {
response.put(statsItem, stats.getGeometricMean());
} else if (statsItem.equals(CodecUtil.STATS_TYPE_STANDARDDEVIATION)) {
response.put(statsItem, stats.getStandardDeviation());
} else if (statsItem.equals(CodecUtil.STATS_TYPE_VARIANCE)) {
response.put(statsItem, stats.getVariance());
} else if (statsItem.equals(CodecUtil.STATS_TYPE_POPULATIONVARIANCE)) {
response.put(statsItem, stats.getPopulationVariance());
} else if (statsItem.equals(CodecUtil.STATS_TYPE_QUADRATICMEAN)) {
response.put(statsItem, Math.sqrt(stats.getSumsq()/stats.getN()));
} else if (statsItem.equals(CodecUtil.STATS_TYPE_KURTOSIS)) {
response.put(statsItem, stats.getKurtosis());
} else if (statsItem.equals(CodecUtil.STATS_TYPE_MEDIAN)) {
response.put(statsItem, stats.getPercentile(50));
} else if (statsItem.equals(CodecUtil.STATS_TYPE_SKEWNESS)) {
response.put(statsItem, stats.getSkewness());
} else {
Matcher m = fpStatsFunctionItems.matcher(statsItem);
if (m.find()) {
String function = m.group(2).trim();
if (function.equals(CodecUtil.STATS_FUNCTION_DISTRIBUTION)) {
response.put(statsItem, getDistribution(m.group(4)));
}
}
}
}
if (errorNumber > 0) {
Map<String, Object> errorResponse = new HashMap<>();
for (Entry<String, Integer> entry : getErrorList().entrySet()) {
errorResponse.put(entry.getKey(), entry.getValue());
}
response.put("errorNumber", errorNumber);
response.put("errorList", errorResponse);
}
if (showDebugInfo) {
response.put("sourceNumber", sourceNumber);
response.put("stats", "full");
}
return response;
}
/*
* (non-Javadoc)
*
* @see mtas.codec.util.collector.MtasDataItem#getCompareValueType()
*/
@Override
public final int getCompareValueType() throws IOException {
switch (sortType) {
case CodecUtil.STATS_TYPE_N:
return 0;
case CodecUtil.STATS_TYPE_SUM:
case CodecUtil.STATS_TYPE_MAX:
case CodecUtil.STATS_TYPE_MIN:
case CodecUtil.STATS_TYPE_SUMSQ:
return 1;
case CodecUtil.STATS_TYPE_SUMOFLOGS:
case CodecUtil.STATS_TYPE_MEAN:
case CodecUtil.STATS_TYPE_GEOMETRICMEAN:
case CodecUtil.STATS_TYPE_STANDARDDEVIATION:
case CodecUtil.STATS_TYPE_VARIANCE:
case CodecUtil.STATS_TYPE_POPULATIONVARIANCE:
case CodecUtil.STATS_TYPE_QUADRATICMEAN:
case CodecUtil.STATS_TYPE_KURTOSIS:
case CodecUtil.STATS_TYPE_MEDIAN:
case CodecUtil.STATS_TYPE_SKEWNESS:
return 2;
default:
throw new IOException("sortType " + sortType + " not supported");
}
}
/*
* (non-Javadoc)
*
* @see mtas.codec.util.collector.MtasDataItem#getCompareValue0()
*/
public final MtasDataItemNumberComparator<Long> getCompareValue0() {
createStats();
switch (sortType) {
case CodecUtil.STATS_TYPE_N:
return new MtasDataItemNumberComparator<>(stats.getN(),
sortDirection);
default:
return null;
}
}
}