| 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.app.gui.chart; |
| 27 |
|
| 28 |
import dr.inference.trace.TraceDistribution; |
| 29 |
import dr.inference.trace.TraceType; |
| 30 |
import dr.stats.Variate; |
| 31 |
import dr.util.FrequencyDistribution; |
| 32 |
|
| 33 |
import java.awt.*; |
| 34 |
import java.util.Collections; |
| 35 |
import java.util.List; |
| 36 |
import java.util.Map; |
| 37 |
import java.util.Set; |
| 38 |
|
| 39 |
public class HistogramPlot extends Plot.AbstractPlot { |
| 40 |
|
| 41 |
protected Variate raw = null; |
| 42 |
|
| 43 |
|
| 44 |
private Paint barPaint = new Color(124, 164, 221); |
| 45 |
private Paint quantilePaint = new Color(232, 114, 103); |
| 46 |
|
| 47 |
private boolean hasQuantiles = false; |
| 48 |
private double quantiles = 0; |
| 49 |
|
| 50 |
private boolean hasIntervals = false; |
| 51 |
private double upperInterval = 0.0; |
| 52 |
private double lowerInterval = 0.0; |
| 53 |
|
| 54 |
private Set<Integer> incredibleSet = Collections.EMPTY_SET; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public HistogramPlot(List<Double> data, int minimumBinCount) { |
| 61 |
super(); |
| 62 |
setData(data, minimumBinCount); |
| 63 |
} |
| 64 |
|
| 65 |
public HistogramPlot(Variate.D data, int minimumBinCount) { |
| 66 |
super(); |
| 67 |
setData(data, minimumBinCount); |
| 68 |
} |
| 69 |
|
| 70 |
public HistogramPlot(List<Double> data, int minimumBinCount, TraceDistribution traceDistribution) { |
| 71 |
super(); |
| 72 |
setData(data, minimumBinCount); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public void setData(List<Double> data, int minimumBinCount) { |
| 90 |
Variate.D d = new Variate.D(data); |
| 91 |
setData(d, minimumBinCount); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public void setPaints(Paint barPaint, Paint quantilePaint) { |
| 98 |
this.barPaint = barPaint; |
| 99 |
this.quantilePaint = quantilePaint; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public void setData(Variate.D data, int minimumBinCount) { |
| 106 |
|
| 107 |
setRawData(data); |
| 108 |
FrequencyDistribution frequency = getFrequencyDistribution(data, minimumBinCount); |
| 109 |
|
| 110 |
Variate.D xData = new Variate.D(); |
| 111 |
Variate.D yData = new Variate.D(); |
| 112 |
|
| 113 |
double x = frequency.getLowerBound(); |
| 114 |
|
| 115 |
for (int i = 0; i < frequency.getBinCount(); i++) { |
| 116 |
|
| 117 |
xData.add(x); |
| 118 |
yData.add(0.0); |
| 119 |
|
| 120 |
x += frequency.getBinSize(); |
| 121 |
|
| 122 |
xData.add(x); |
| 123 |
yData.add((double) frequency.getFrequency(i)); |
| 124 |
|
| 125 |
} |
| 126 |
setData(xData, yData); |
| 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 |
protected Variate getRawData() { |
| 157 |
return raw; |
| 158 |
} |
| 159 |
|
| 160 |
protected void setRawData(Variate raw) { |
| 161 |
this.raw = raw; |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
protected FrequencyDistribution getFrequencyDistribution(Variate data, int minimumBinCount) { |
| 168 |
double min = ((Number) data.getMin()).doubleValue(); |
| 169 |
double max = ((Number) data.getMax()).doubleValue(); |
| 170 |
|
| 171 |
if (min == max) { |
| 172 |
if (min == 0) { |
| 173 |
min = -1.0; |
| 174 |
} else { |
| 175 |
min -= Math.abs(min / 10.0); |
| 176 |
} |
| 177 |
if (max == 0) { |
| 178 |
max = 1.0; |
| 179 |
} else { |
| 180 |
max += Math.abs(max / 10.0); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
Axis axis = new LinearAxis(Axis.AT_MAJOR_TICK, Axis.AT_MAJOR_TICK); |
| 185 |
if (minimumBinCount <= 0) { |
| 186 |
axis = new LinearAxis(Axis.AT_MAJOR_TICK_PLUS, Axis.AT_MAJOR_TICK_PLUS); |
| 187 |
} |
| 188 |
axis.setRange(min, max); |
| 189 |
|
| 190 |
int majorTickCount = axis.getMajorTickCount(); |
| 191 |
axis.setPrefNumTicks(majorTickCount, 4); |
| 192 |
|
| 193 |
double binSize = axis.getMinorTickSpacing(); |
| 194 |
int binCount = (int) ((axis.getMaxAxis() - axis.getMinAxis()) / binSize) + 2; |
| 195 |
|
| 196 |
if (minimumBinCount > 0) { |
| 197 |
|
| 198 |
while (binCount < minimumBinCount && majorTickCount < 1000) { |
| 199 |
majorTickCount++; |
| 200 |
axis.setPrefNumTicks(majorTickCount, 4); |
| 201 |
|
| 202 |
binSize = axis.getMinorTickSpacing(); |
| 203 |
|
| 204 |
binCount = (int) ((axis.getMaxAxis() - axis.getMinAxis()) / binSize) + 2; |
| 205 |
} |
| 206 |
} else if (binSize > 1) { |
| 207 |
|
| 208 |
binSize = 0.5; |
| 209 |
binCount = (int) ((axis.getMaxAxis() - axis.getMinAxis()) / binSize) + 2; |
| 210 |
} |
| 211 |
|
| 212 |
double start = axis.getMinAxis(); |
| 213 |
if (minimumBinCount < 0) |
| 214 |
start = Math.round(start); |
| 215 |
FrequencyDistribution frequency = new FrequencyDistribution(start, binCount, binSize); |
| 216 |
|
| 217 |
for (int i = 0; i < raw.getCount(); i++) { |
| 218 |
frequency.addValue(((Number) raw.get(i)).doubleValue()); |
| 219 |
} |
| 220 |
|
| 221 |
return frequency; |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
public void setQuantiles(double quantiles) { |
| 255 |
this.quantiles = quantiles; |
| 256 |
hasQuantiles = (quantiles > 0.0); |
| 257 |
hasIntervals = false; |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
public void setIntervals(double upper, double lower) { |
| 264 |
hasQuantiles = false; |
| 265 |
hasIntervals = (upper > 0.0 || lower > 0.0); |
| 266 |
upperInterval = upper; |
| 267 |
lowerInterval = lower; |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
public void setIncredibleSet(Set<Integer> incredibleSet) { |
| 274 |
this.incredibleSet = incredibleSet; |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
public void setBarFillStyle(Paint barPaint) { |
| 282 |
this.barPaint = barPaint; |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
protected void paintData(Graphics2D g2, Variate.N xData, Variate.N yData) { |
| 289 |
|
| 290 |
double x1, y1, x2, y2; |
| 291 |
double lower = 0.0, upper = 0.0; |
| 292 |
int n = xData.getCount(); |
| 293 |
|
| 294 |
if (hasQuantiles) { |
| 295 |
lower = getRawData().getQuantile(quantiles); |
| 296 |
upper = getRawData().getQuantile(1.0 - quantiles); |
| 297 |
} else if (hasIntervals) { |
| 298 |
lower = lowerInterval; |
| 299 |
upper = upperInterval; |
| 300 |
} |
| 301 |
|
| 302 |
g2.setStroke(lineStroke); |
| 303 |
for (int i = 0; i < n; i += 2) { |
| 304 |
|
| 305 |
x1 = (Double) xData.get(i); |
| 306 |
y1 = (Double) yData.get(i); |
| 307 |
x2 = (Double) xData.get(i + 1); |
| 308 |
y2 = (Double) yData.get(i + 1); |
| 309 |
|
| 310 |
if (y1 != y2) { |
| 311 |
if (barPaint != null) { |
| 312 |
if (hasQuantiles || hasIntervals) { |
| 313 |
if (x1 < lower) { |
| 314 |
if (x2 <= lower) { |
| 315 |
g2.setPaint(quantilePaint); |
| 316 |
fillRect(g2, x1, y1, x2, y2); |
| 317 |
} else { |
| 318 |
g2.setPaint(quantilePaint); |
| 319 |
fillRect(g2, x1, y1, lower, y2); |
| 320 |
g2.setPaint(barPaint); |
| 321 |
fillRect(g2, lower, y1, x2, y2); |
| 322 |
} |
| 323 |
} else if (x2 > upper) { |
| 324 |
if (x1 >= upper) { |
| 325 |
g2.setPaint(quantilePaint); |
| 326 |
fillRect(g2, x1, y1, x2, y2); |
| 327 |
} else { |
| 328 |
g2.setPaint(barPaint); |
| 329 |
fillRect(g2, x1, y1, upper, y2); |
| 330 |
g2.setPaint(quantilePaint); |
| 331 |
fillRect(g2, upper, y1, x2, y2); |
| 332 |
} |
| 333 |
} else { |
| 334 |
g2.setPaint(barPaint); |
| 335 |
fillRect(g2, x1, y1, x2, y2); |
| 336 |
} |
| 337 |
} else { |
| 338 |
g2.setPaint(barPaint); |
| 339 |
fillRect(g2, x1, y1, x2, y2); |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
if (lineStroke != null && linePaint != null) { |
| 344 |
g2.setStroke(lineStroke); |
| 345 |
g2.setPaint(linePaint); |
| 346 |
drawRect(g2, x1, y1, x2, y2); |
| 347 |
} |
| 348 |
} |
| 349 |
} |
| 350 |
} |
| 351 |
} |