Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * TangHuLuPlot.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.stats.FrequencyCounter;
29 import dr.stats.Variate;
30
31 import java.awt.*;
32 import java.awt.geom.Rectangle2D;
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.util.Set;
36
37 /**
38 * Create a string of bubbles to visualise the joint probability
39 * between two integer/categorical random variables.
40 * The size of circle is proportional to the joint probability,
41 * the blue coloured circle is in the credible set of
42 * given a probability threshold default to 0.95,
43 * the red is in the set not in the credible set.
44 * The tile background is coloured if there is any circle,
45 * in case the very small circle wouldn't be missed out.
46 * The coloured background can also show the area of
47 * the credible set and non-credible set.
48 *
49 * TangHuLu is a traditional Chinese snack of candied fruit
50 * normally sugar-coated hawthorns on a stick.
51 *
52 * @author Walter Xie
53 *
54 * TODO: This could be generalized to a bubble plot taking 3 dimensions
55 * TODO: of data (x, y coordinates and bubble size). Better to leave the data
56 * TODO: manipulation to the calling software.
57 */
58 public class TangHuLuPlot extends ScatterPlot {
59
60 public final static Color CIRCLE_COLOR = new Color(0x2f8aa3);
61 public final static Color CIRCLE_OUTSIDE_COLOR = new Color(0xd6bd58);
62 public final static Color TILE_COLOR = adjustAlpha(CIRCLE_COLOR, 64);
63 public final static Color TILE_OUTSIDE_COLOR = adjustAlpha(CIRCLE_OUTSIDE_COLOR, 64);
64
65 // set min circle size to avoid the circle too small to see
66 private final double MIN_CIRCLE_SIZE = 5;
67
68 protected List<XY> uniqueXYList; // todo duplicate to xData yData, but it is used for key of FrequencyCounter
69 protected FrequencyCounter<XY> xyFC;
70
71 protected double credProb = 0.95;
72
73 public TangHuLuPlot(List<Double> xData, List<Double> yData) {
74 super(xData, yData);
75 }
76
77 public void setPrThreshold(double credProb){
78 this.credProb = credProb;
79 }
80
81 /**
82 * Set data
83 */
84 public void setData(List<Double> xData, List<Double> yData) {
85
86 if (xData.size() != yData.size())
87 throw new IllegalArgumentException("xData and yData must have the same size ! " +
88 xData.size() + " != " + yData.size());
89
90 List<XY> xyList = new ArrayList<XY>();
91 for (int i = 0; i < xData.size(); i++) {
92 Double x = xData.get(i);
93 Double y = yData.get(i);
94 // if (x > 0 && y == 0)
95 // System.out.println(i + " : " + x + " " + y);
96 xyList.add(new XY(x,y));
97 }
98
99 // find unique pairs
100 xyFC = new FrequencyCounter<XY>(xyList, 0.95);
101
102 List<Double> xUnique = new ArrayList<Double>();
103 List<Double> yUnique = new ArrayList<Double>();
104 uniqueXYList = new ArrayList<XY>();
105 for (XY xy : xyFC.getUniqueValues()) {
106 xUnique.add(xy.x);
107 yUnique.add(xy.y);
108 uniqueXYList.add(xy); // store counts for circle size
109 }
110
111 Variate.D xd = new Variate.D(xUnique);
112 Variate.D yd = new Variate.D(yUnique);
113
114 setData(xd, yd);
115 }
116
117 /**
118 * Paint data series.
119 * The maximum circle size is the smaller major tick space in x or y
120 * multiplying with <code>xScale</code> or <code>yScale</code>.
121 */
122 protected void paintData(Graphics2D g2, Variate.N xData, Variate.N yData) {
123 // remove ?
124 markBounds = new java.util.Vector<Rectangle2D>();
125
126 // todo do we need selectedPoints here?
127 Set<Integer> selectedPoints = getSelectedPoints();
128
129 int n = xData.getCount();
130
131 double cellWidth = Math.abs(xAxis.getMinorTickSpacing() * xScale);
132 double cellHeight = Math.abs(yAxis.getMinorTickSpacing() * yScale);
133 double maxDiameter = MIN_CIRCLE_SIZE * 2;
134
135 if (n > 1) {
136 // take the smaller gap to fit in circles
137 if (Math.min(cellWidth, cellHeight) > maxDiameter) {
138 maxDiameter = Math.min(cellWidth, cellHeight);
139 }
140 }
141
142 // if (xyFC != null) {
143 for (int i = 0; i < n; i++) {
144 float x = (float) transformX(((Number) xData.get(i)).doubleValue());
145 float y = (float) transformY(((Number) yData.get(i)).doubleValue());
146
147 XY xy = uniqueXYList.get(i);
148 // probability is proportional to area not diameter
149 double diameter = maxDiameter * Math.sqrt(xyFC.getProportionalFrequency(xy));
150
151 Set<XY> incredibleSet = xyFC.getIncredibleSet();
152
153 // background tiles
154 g2.setPaint(incredibleSet.contains(xy) ? TILE_OUTSIDE_COLOR : TILE_COLOR);
155 fillRect(g2, (float)(x - cellWidth / 2), (float)(y - cellHeight / 2), (float)(x + cellWidth / 2), (float)(y + cellHeight / 2));
156
157 // circles
158 Paint currentPaint = (incredibleSet.contains(xy) ? CIRCLE_OUTSIDE_COLOR : CIRCLE_COLOR);
159
160 if (selectedPoints.contains(i)) {
161 setHilightedMarkStyle(new BasicStroke(0), currentPaint, currentPaint);
162 drawMarkHilighted(g2, x, y);
163 } else {
164 setMarkStyle(CIRCLE_MARK, diameter, new BasicStroke(0), currentPaint, currentPaint);
165 drawMark(g2, x, y, null);
166 }
167 }
168 // }
169 }
170
171 public static Color adjustAlpha(Color color, int alpha) {
172 return new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha);
173 }
174 }
175
176