Debian Codegraph @

src/dr/app/gui/chart/ViolinPlot.java

raw symbols SCIP
1 /*
2 * NumericalDensityPlot.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.app.gui.chart;
27
28 import dr.inference.trace.TraceDistribution;
29 import dr.math.distributions.GammaKDEDistribution;
30 import dr.math.distributions.KernelDensityEstimatorDistribution;
31 import dr.math.distributions.LogTransformedNormalKDEDistribution;
32 import dr.math.distributions.NormalKDEDistribution;
33 import dr.stats.Variate;
34 import dr.util.FrequencyDistribution;
35
36 import java.awt.*;
37 import java.awt.geom.GeneralPath;
38 import java.awt.geom.Rectangle2D;
39 import java.util.*;
40
41 /**
42 * @author Andrew Rambaut
43 */
44 public class ViolinPlot extends KDENumericalDensityPlot { //Plot.AbstractPlot {
45
46 private final boolean isVertical;
47 private final double violinWidth;
48 private final boolean showTails;
49 private final boolean showQuantiles;
50 private final double upper, lower;
51 private double y1, y2;
52
53 public ViolinPlot(double violinWidth, java.util.List<Double> data, int minimumBinCount) {
54 this(true, violinWidth, true, data, minimumBinCount);
55 }
56
57 public ViolinPlot(boolean isVertical, double violinWidth, boolean showTails, java.util.List<Double> data) {
58 this(true, violinWidth, showTails, data, DEFAULT_KDE_BINS);
59 }
60
61 public ViolinPlot(boolean isVertical, double violinWidth, boolean showTails, java.util.List<Double> data, int minimumBinCount) {
62 super(data, minimumBinCount);
63
64 this.isVertical = isVertical;
65 this.violinWidth = violinWidth;
66 this.showQuantiles = showTails;
67 this.showTails = showTails;
68
69 // setData again because in super, the width weren't set.
70 setData(data, minimumBinCount);
71
72 if (showQuantiles) {
73 lower = getQuantile(0.025);
74 upper = getQuantile(0.975);
75 } else {
76 lower = Collections.min(data);
77 upper = Collections.max(data);
78 }
79 }
80
81 public ViolinPlot(boolean isVertical, double violinWidth, double lower, double upper, boolean showTails, java.util.List<Double> data) {
82 this(isVertical, violinWidth, lower, upper, showTails, data, DEFAULT_KDE_BINS);
83 }
84
85 public ViolinPlot(boolean isVertical, double violinWidth, double lower, double upper, boolean showTails, java.util.List<Double> data, int minimumBinCount) {
86 super(data, minimumBinCount);
87
88 this.isVertical = isVertical;
89 this.violinWidth = violinWidth;
90 this.showQuantiles = true;
91
92 this.lower = lower;
93 this.upper = upper;
94
95 this.showTails = showTails;
96
97 // setData again because in super, the width weren't set.
98 setData(data, minimumBinCount);
99
100 }
101
102 /**
103 * Set data
104 * X data is values, y data is density
105 */
106 public void setData(Variate.N xData, Variate.N yData) {
107
108 double scale = (0.5 * violinWidth) / ((Number)yData.getMax()).doubleValue();
109 this.xData = new Variate.D();
110 this.yData = new Variate.D();
111 for (int i = 0; i < yData.getCount(); i++) {
112 this.xData.add(((Number) xData.get(i)).doubleValue());
113 this.yData.add(((Number) yData.get(i)).doubleValue() * scale);
114 }
115 for (int i = yData.getCount() - 1; i >= 0; i--) {
116 this.xData.add(((Number) xData.get(i)).doubleValue());
117 this.yData.add(-((Number) yData.get(i)).doubleValue() * scale);
118 }
119
120 y1 = getDensity(lower) * scale;
121 y2 = getDensity(upper) * scale;
122 }
123
124 /**
125 * Set up the axis with some data
126 */
127 public void setupAxis(Axis xAxis, Axis yAxis, Variate xData, Variate yData) {
128 Axis valueAxis = (isVertical ? yAxis : xAxis);
129 Axis densityAxis = (isVertical ? xAxis : yAxis);
130 Variate valueData = xData;
131 Variate densityData = yData;
132
133 if (valueData != null) {
134 if (valueAxis instanceof LogAxis) {
135 double minValue = java.lang.Double.POSITIVE_INFINITY;
136
137 for (int i = 0; i < valueData.getCount(); i++) {
138 double value = (Double) valueData.get(i);
139 if (value > 0.0 && value < minValue)
140 minValue = value;
141 }
142
143 valueAxis.addRange(minValue, (Double) valueData.getMax());
144 } else {
145 valueAxis.addRange((Double) valueData.getMin(), (Double) valueData.getMax());
146 }
147 }
148
149 if (densityData != null) {
150 if (densityAxis instanceof LogAxis) {
151 throw new IllegalArgumentException("The density axis shouldn't be log scale");
152 } else {
153 // do nothing - the range will be set up by the chart
154 }
155 }
156 }
157
158 /**
159 * Paint data series
160 */
161 protected void paintData(Graphics2D g2, Variate.N xData, Variate.N yData) {
162
163 int n = xData.getCount();
164
165 GeneralPath path = new GeneralPath();
166 GeneralPath intervalPath = new GeneralPath();
167
168 if (isVertical) {
169 // TODO this code is replicated below
170
171 // rotate -90
172 float y = (float) transformY(((Number)xData.get(0)).doubleValue());
173 float x = (float) transformX(((Number)yData.get(0)).doubleValue() + getPlotNumber() + 1);
174
175 if (showTails) {
176 path.moveTo(x, y);
177 }
178
179 // create the quantile cropped path
180 float v1 = (float) transformY(lower);
181 float v2 = (float) transformY(upper);
182
183 y = v1;
184 x = (float) transformX(getPlotNumber() + 1);
185 intervalPath.moveTo(x, y);
186
187 x = (float) transformX(y1 + getPlotNumber() + 1);
188 intervalPath.lineTo(x, y);
189
190 if (!showTails) {
191 path.moveTo(x, y);
192 }
193
194 boolean crossedAxis = false;
195
196 for (int i = 1; i < n; i++) {
197 y = (float) transformY(((Number) xData.get(i)).doubleValue());
198 x = (float) transformX(((Number) yData.get(i)).doubleValue() + getPlotNumber() + 1);
199
200 if (showTails) {
201 path.lineTo(x, y);
202 }
203
204 if (y < v1) {
205 if (y > v2) {
206 if (!showTails) {
207 path.lineTo(x, y);
208 }
209
210 intervalPath.lineTo(x, y);
211 } else {
212 if (!crossedAxis) {
213 y = v2;
214 x = (float) transformX(y2 + getPlotNumber() + 1);
215 intervalPath.lineTo(x, y);
216
217 if (!showTails) {
218 path.lineTo(x, y);
219 }
220
221 x = (float) transformX(-y2 + getPlotNumber() + 1);
222 intervalPath.lineTo(x, y);
223
224 if (!showTails) {
225 path.moveTo(x, y);
226 }
227
228 crossedAxis = true;
229 }
230 }
231 }
232 }
233
234 // finish the quantile cropped path
235 y = v1;
236 x = (float) transformX(-y1 + getPlotNumber() + 1);
237 intervalPath.lineTo(x, y);
238
239 if (!showTails) {
240 path.lineTo(x, y);
241 }
242
243 x = (float) transformX(getPlotNumber() + 1);
244 intervalPath.lineTo(x, y);
245
246 } else {
247 // TODO this code is replicated above
248
249 float x = (float) transformX(((Number)xData.get(0)).doubleValue());
250 float y = (float) transformY(((Number)yData.get(0)).doubleValue() + getPlotNumber() + 1);
251
252 path.moveTo(x, y);
253
254 // create the quantile cropped path
255 float v1 = (float) transformX(lower);
256 float v2 = (float) transformX(upper);
257
258 x = v1;
259 y = (float) transformY(getPlotNumber() + 1);
260 intervalPath.moveTo(x, y);
261
262 y = (float) transformY(y1 + getPlotNumber() + 1);
263 intervalPath.lineTo(x, y);
264
265 boolean crossedAxis = false;
266
267 for (int i = 1; i < n; i++) {
268 x = (float) transformX(((Number) xData.get(i)).doubleValue());
269 y = (float) transformY(((Number) yData.get(i)).doubleValue() + getPlotNumber() + 1);
270
271 path.lineTo(x, y);
272
273 if (x < v1) {
274 if (x > v2) {
275 intervalPath.lineTo(x, y);
276 } else if (!crossedAxis) {
277 x = v2;
278 y = (float) transformY(y2 + getPlotNumber() + 1);
279 intervalPath.lineTo(x, y);
280 y = (float) transformY(-y2 + getPlotNumber() + 1);
281 intervalPath.lineTo(x, y);
282 crossedAxis = true;
283 }
284 }
285 }
286
287 // finish the quantile cropped path
288 x = v1;
289 y = (float) transformY(-y1 + getPlotNumber() + 1);
290 intervalPath.lineTo(x, y);
291
292 y = (float) transformY(getPlotNumber() + 1);
293 intervalPath.lineTo(x, y);
294
295 }
296
297 if (solid) {
298 Paint fillPaint = new Color(
299 ((Color) linePaint).getRed(),
300 ((Color) linePaint).getGreen(),
301 ((Color) linePaint).getBlue(), 32);
302 g2.setPaint(fillPaint);
303
304 if (showQuantiles) {
305 intervalPath.closePath();
306 g2.fill(intervalPath);
307 }
308 }
309
310 if (showTails) {
311 path.closePath();
312 }
313
314 g2.setStroke(lineStroke);
315 g2.setPaint(linePaint);
316 g2.draw(path);
317 }
318
319
320 }