| 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 |
package dr.inference.trace; |
| 27 |
|
| 28 |
import dr.stats.FrequencyCounter; |
| 29 |
import dr.util.Pair; |
| 30 |
|
| 31 |
import java.util.*; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public class Trace { |
| 41 |
public enum OrderType { |
| 42 |
DEFAULT, |
| 43 |
NATURAL, |
| 44 |
FREQUENCY |
| 45 |
} |
| 46 |
|
| 47 |
private static final int MAX_UNIQUE_VALUES = 100; |
| 48 |
|
| 49 |
private TraceType traceType = TraceType.REAL; |
| 50 |
private List<Double> values = new ArrayList<Double>(); |
| 51 |
private String name; |
| 52 |
|
| 53 |
private boolean isConstant = true; |
| 54 |
private double constantValue = Double.NaN; |
| 55 |
|
| 56 |
private List<String> categoryValueList = new ArrayList<String>(); |
| 57 |
private Map<Integer, String> categoryLabelMap = null; |
| 58 |
private OrderType orderType = OrderType.DEFAULT; |
| 59 |
private List<Integer> categoryOrder = null; |
| 60 |
|
| 61 |
private Set<Integer> uniqueValues = new TreeSet<Integer>(); |
| 62 |
|
| 63 |
public Trace(String name) { |
| 64 |
this.name = name; |
| 65 |
} |
| 66 |
|
| 67 |
public Trace(String name, TraceType traceType) { |
| 68 |
this.name = name; |
| 69 |
setTraceType(traceType); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public void add(Double value) { |
| 76 |
if (uniqueValues.size() < MAX_UNIQUE_VALUES) { |
| 77 |
|
| 78 |
uniqueValues.add(value.intValue()); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
if (isConstant && value != constantValue) { |
| 83 |
isConstant = false; |
| 84 |
} else if (Double.isNaN(constantValue)) { |
| 85 |
constantValue = value; |
| 86 |
} |
| 87 |
|
| 88 |
values.add(value); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public void add(Double[] valuesArray) { |
| 95 |
for (Double value : valuesArray) { |
| 96 |
add(value); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public void add(Integer value) { |
| 104 |
if (uniqueValues.size() < MAX_UNIQUE_VALUES) { |
| 105 |
|
| 106 |
uniqueValues.add(value); |
| 107 |
} |
| 108 |
|
| 109 |
double d = value.doubleValue(); |
| 110 |
|
| 111 |
|
| 112 |
if (isConstant && d != constantValue) { |
| 113 |
isConstant = false; |
| 114 |
} else if (Double.isNaN(constantValue)) { |
| 115 |
constantValue = d; |
| 116 |
} |
| 117 |
|
| 118 |
values.add(d); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public void add(String value) { |
| 126 |
int index = categoryValueList.indexOf(value); |
| 127 |
if (index < 0) { |
| 128 |
categoryValueList.add(value); |
| 129 |
index = categoryValueList.size() - 1; |
| 130 |
if (categoryLabelMap == null) { |
| 131 |
categoryLabelMap = new HashMap<Integer, String>(); |
| 132 |
} |
| 133 |
categoryLabelMap.put(index, value); |
| 134 |
} |
| 135 |
add(index); |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public void add(String[] valuesArray) { |
| 142 |
for (String value : valuesArray) { |
| 143 |
add(value); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
public FrequencyCounter<Integer> getFrequencyCounter() { |
| 148 |
assert traceType.isDiscrete(); |
| 149 |
return getTraceStatistics().getFrequencyCounter(); |
| 150 |
} |
| 151 |
|
| 152 |
public List<Integer> getCategoryOrder() { |
| 153 |
return categoryOrder; |
| 154 |
} |
| 155 |
|
| 156 |
public void setOrderType(OrderType orderType) { |
| 157 |
this.orderType = orderType; |
| 158 |
switch (orderType) { |
| 159 |
case DEFAULT: |
| 160 |
categoryOrder = new ArrayList<Integer>(); |
| 161 |
for (int i = 0; i < getUniqueValueCount(); i ++) { |
| 162 |
categoryOrder.add(i); |
| 163 |
} |
| 164 |
break; |
| 165 |
case NATURAL: |
| 166 |
categoryOrder = getNaturalOrder(); |
| 167 |
break; |
| 168 |
case FREQUENCY: |
| 169 |
categoryOrder = getFrequencyCounter().getOrderByFrequency(); |
| 170 |
break; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
private List<Integer> getNaturalOrder() { |
| 175 |
List<Integer> order; |
| 176 |
if (traceType == TraceType.CATEGORICAL) { |
| 177 |
List<Pair<Comparable, Integer>> values = new ArrayList<Pair<Comparable, Integer>>(); |
| 178 |
int i = 0; |
| 179 |
for (Integer value : getFrequencyCounter().getUniqueValues()) { |
| 180 |
if (categoryLabelMap != null) { |
| 181 |
values.add(new Pair<Comparable, Integer>(categoryLabelMap.get(value), i)); |
| 182 |
} else { |
| 183 |
values.add(new Pair<Comparable, Integer>(value, i)); |
| 184 |
} |
| 185 |
i++; |
| 186 |
} |
| 187 |
Collections.sort(values, new Comparator<Pair<Comparable, Integer>>() { |
| 188 |
public int compare(Pair<Comparable, Integer> value1, Pair<Comparable, Integer> value2) { |
| 189 |
return value1.fst.compareTo(value2.fst); |
| 190 |
} |
| 191 |
}); |
| 192 |
order = new ArrayList<Integer>(); |
| 193 |
for (Pair<Comparable, Integer> value : values) { |
| 194 |
order.add(value.snd); |
| 195 |
} |
| 196 |
} else { |
| 197 |
order = new ArrayList<Integer>(getFrequencyCounter().getUniqueValues()); |
| 198 |
Collections.sort(order); |
| 199 |
} |
| 200 |
return order; |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
public int getValueCount() { |
| 205 |
return values.size(); |
| 206 |
} |
| 207 |
|
| 208 |
public int getUniqueValueCount() { |
| 209 |
return uniqueValues.size(); |
| 210 |
} |
| 211 |
|
| 212 |
public double getValue(int index) { |
| 213 |
return values.get(index); |
| 214 |
} |
| 215 |
|
| 216 |
public int getCategory(int index) { |
| 217 |
return values.get(index).intValue(); |
| 218 |
} |
| 219 |
|
| 220 |
public String getCategoryLabel(int index) { |
| 221 |
return categoryLabelMap.get(getCategory(index)); |
| 222 |
} |
| 223 |
|
| 224 |
public Map<Integer, String> getCategoryLabelMap() { |
| 225 |
return categoryLabelMap; |
| 226 |
} |
| 227 |
|
| 228 |
public double[] getRange() { |
| 229 |
|
| 230 |
if (getValueCount() < 1) throw new IllegalArgumentException("Cannot find values in trace " + getName()); |
| 231 |
|
| 232 |
if (getTraceType().isNumber()) { |
| 233 |
|
| 234 |
Double min = Double.MAX_VALUE; |
| 235 |
Double max = Double.MIN_VALUE; |
| 236 |
for (Double value : values) { |
| 237 |
if ( value < min) { |
| 238 |
min = value; |
| 239 |
} else if (value > max) { |
| 240 |
max = value; |
| 241 |
} |
| 242 |
} |
| 243 |
return new double[] {min, max}; |
| 244 |
} else { |
| 245 |
throw new UnsupportedOperationException("cannot call getRange for categorical data"); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
public List<Double> getValues(int fromIndex, int toIndex) { |
| 255 |
return getValues(fromIndex, toIndex, null); |
| 256 |
} |
| 257 |
|
| 258 |
public List<Double> getValues(int fromIndex, int toIndex, boolean[] filtered) { |
| 259 |
if (toIndex > getValueCount() || fromIndex > toIndex) { |
| 260 |
throw new RuntimeException("Invalid index : fromIndex = " + fromIndex + "; toIndex = " + toIndex |
| 261 |
+ "; List size = " + getValueCount() + "; in Trace " + name); |
| 262 |
} |
| 263 |
|
| 264 |
if (filtered == null || filtered.length < 1) { |
| 265 |
return values.subList(fromIndex, toIndex); |
| 266 |
} else { |
| 267 |
List<Double> valuesList = new ArrayList<Double>(); |
| 268 |
for (int i = fromIndex; i < toIndex; i++) { |
| 269 |
if (!filtered[i]) |
| 270 |
valuesList.add(values.get(i)); |
| 271 |
} |
| 272 |
if (valuesList.size() < 1) |
| 273 |
throw new RuntimeException("There is no value left after all filters are applied !"); |
| 274 |
|
| 275 |
return valuesList; |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
public String getName() { |
| 280 |
return name; |
| 281 |
} |
| 282 |
|
| 283 |
public void setName(String name) { |
| 284 |
this.name = name; |
| 285 |
} |
| 286 |
|
| 287 |
public TraceType getTraceType() { |
| 288 |
return traceType; |
| 289 |
} |
| 290 |
|
| 291 |
public void setTraceType(TraceType traceType) { |
| 292 |
this.traceType = traceType; |
| 293 |
} |
| 294 |
|
| 295 |
public boolean isConstant() { |
| 296 |
return isConstant; |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
private TraceCorrelation traceStatistics; |
| 301 |
|
| 302 |
public TraceCorrelation getTraceStatistics() { |
| 303 |
return traceStatistics; |
| 304 |
} |
| 305 |
|
| 306 |
public void setTraceStatistics(TraceCorrelation traceStatistics) { |
| 307 |
this.traceStatistics = traceStatistics; |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
protected Filter filter = null; |
| 312 |
|
| 313 |
public void setFilter(Filter filter) { |
| 314 |
this.filter = filter; |
| 315 |
} |
| 316 |
|
| 317 |
public Filter getFilter() { |
| 318 |
return filter; |
| 319 |
} |
| 320 |
} |