Debian Codegraph @

src/dr/inference/trace/Trace.java

raw symbols SCIP
1 /*
2 * Trace.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
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 * A simple class that stores a trace for a single statistic
35 *
36 * @author Andrew Rambaut
37 * @author Alexei Drummond
38 * @version $Id: Trace.java,v 1.11 2005/07/11 14:07:26 rambaut Exp $
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; // the maximum allowed number of unique values
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) { // traceType = TraceFactory.TraceType.DOUBLE;
64 this.name = name;
65 }
66
67 public Trace(String name, TraceType traceType) {
68 this.name = name;
69 setTraceType(traceType);
70 }
71
72 /**
73 * @param value the valued to be added
74 */
75 public void add(Double value) {
76 if (uniqueValues.size() < MAX_UNIQUE_VALUES) {
77 // unique values are treated as integers
78 uniqueValues.add(value.intValue());
79 }
80
81 // check if the trace is still constant
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 * @param valuesArray the values to be added
93 */
94 public void add(Double[] valuesArray) {
95 for (Double value : valuesArray) {
96 add(value);
97 }
98 }
99
100 /**
101 * @param value the valued to be added
102 */
103 public void add(Integer value) {
104 if (uniqueValues.size() < MAX_UNIQUE_VALUES) {
105 // unique values are treated as integers
106 uniqueValues.add(value);
107 }
108
109 double d = value.doubleValue();
110
111 // check if the trace is still constant
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 * Add a categorical value
123 * @param value the valued to be added
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 * @param valuesArray the values to be added
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() { // Double => bounds; Integer and String => unique values
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 * @param fromIndex low endpoint (inclusive) of the subList.
251 * @param toIndex high endpoint (exclusive) of the subList.
252 * @return The list of values (which are selected values if filter applied)
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 //******************** TraceCorrelation ****************************
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 //******************** Filter ****************************
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 }