Debian Codegraph @

src/dr/inference/trace/DensityRectangle.java

raw symbols SCIP
1 /*
2 * DensityRectangle.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 javax.swing.*;
29 import java.awt.*;
30 import java.awt.geom.Point2D;
31 import java.awt.geom.Rectangle2D;
32 import java.io.File;
33 import java.io.IOException;
34 import java.util.ArrayList;
35 import java.util.List;
36
37 /**
38 * @author Alexei Drummond
39 * @version $Id: DensityRectangle.java,v 1.4 2006/08/26 19:24:41 rambaut Exp $
40 */
41 public class DensityRectangle {
42
43 Rectangle2D rectangle;
44 List<Point2D> points;
45 int pointCount;
46 double area;
47
48 static int THRESHOLD = 80;
49
50 DensityRectangle[] quadrants = null;
51
52
53 public DensityRectangle(double x, double y, double w, double h) {
54
55 rectangle = new Rectangle2D.Double(x, y, w, h);
56 area = w * h;
57 points = new ArrayList<Point2D>();
58 }
59
60 public boolean contains(Point2D point) {
61
62 return rectangle.contains(point);
63 }
64
65 public boolean contains(double x, double y) {
66
67 return rectangle.contains(x, y);
68 }
69
70 public void addPoint(Point2D point) {
71 if (contains(point)) {
72
73 pointCount += 1;
74
75 if (quadrants == null) {
76
77 points.add(point);
78
79 if (points.size() > THRESHOLD) {
80
81 // partition into quadrants
82 quadrants = new DensityRectangle[4];
83
84 double x = rectangle.getX();
85 double y = rectangle.getY();
86 double w = rectangle.getWidth() / 2.0;
87 double h = rectangle.getHeight() / 2.0;
88
89 quadrants[0] = new DensityRectangle(x, y, w, h);
90 quadrants[1] = new DensityRectangle(x + w, y, w, h);
91 quadrants[2] = new DensityRectangle(x, y + h, w, h);
92 quadrants[3] = new DensityRectangle(x + w, y + h, w, h);
93
94 for (Point2D p : points) {
95 quadrantPoint(p);
96 }
97 points.clear();
98 points = null;
99 }
100 } else {
101
102 quadrantPoint(point);
103
104 }
105
106 } else {
107 // ignore point!
108 }
109 }
110
111 private void quadrantPoint(Point2D point) {
112
113 boolean found = false;
114 for (int i = 0; i < quadrants.length && !found; i++) {
115 if (quadrants[i].contains(point)) {
116 quadrants[i].addPoint(point);
117 found = true;
118 }
119 }
120 if (!found) {
121 throw new RuntimeException("Failed to quadrantize point!");
122 }
123 }
124
125 public final double density() {
126 return getPointCount() / getArea();
127 }
128
129 public final double maximumDensity() {
130 if (quadrants == null) return density();
131
132 double maxDensity = quadrants[0].maximumDensity();
133 for (int i = 1; i < quadrants.length; i++) {
134 double d = quadrants[i].maximumDensity();
135 if (d > maxDensity) maxDensity = d;
136 }
137 return maxDensity;
138 }
139
140 public final int getPointCount() {
141 return pointCount;
142 }
143
144 public final double getArea() {
145 return area;
146 }
147
148 public final void paint(Graphics2D g2d, Rectangle2D targetRectangle) {
149
150 paint(g2d, targetRectangle, maximumDensity());
151 }
152
153 private void paint(Graphics2D g2d, Rectangle2D targetRectangle, double maxDensity) {
154
155 double density = density();
156
157 float red = (float) (density / maxDensity);
158 float blue = 1.0f - red;
159 float green = Math.min(red, blue); //(0.5f - Math.abs(red-0.5f));
160
161 Color color = new Color(red, green, blue);
162 g2d.setColor(color);
163 g2d.fill(targetRectangle);
164
165 if (quadrants != null) {
166
167 double x = targetRectangle.getX();
168 double y = targetRectangle.getY();
169 double w = targetRectangle.getWidth() / 2.0;
170 double h = targetRectangle.getHeight() / 2.0;
171
172 Rectangle2D rect[] = new Rectangle2D[4];
173
174 rect[0] = new Rectangle2D.Double(x, y, w, h);
175 rect[1] = new Rectangle2D.Double(x + w, y, w, h);
176 rect[2] = new Rectangle2D.Double(x, y + h, w, h);
177 rect[3] = new Rectangle2D.Double(x + w, y + h, w, h);
178
179 for (int i = 0; i < quadrants.length; i++) {
180 quadrants[i].paint(g2d, rect[i], maxDensity);
181 }
182 }
183 }
184
185 public static void main(String[] args) throws IOException, TraceException {
186
187 int burnin = 0;
188
189 System.out.println("usage: densityPlot <logFileName> <xIndex> <yIndex> <maxX> <maxY>");
190
191 String logFileName = args[0];
192
193 int[] indices = new int[2];
194
195 for (int i = 0; i < 2; i++) {
196 indices[i] = Integer.parseInt(args[i + 1]);
197 }
198
199 double width = Double.parseDouble(args[3]);
200 double height = Double.parseDouble(args[4]);
201
202 System.out.println(" logFileName=" + logFileName);
203 System.out.println(" xIndex=" + indices[0]);
204 System.out.println(" yIndex=" + indices[1]);
205 System.out.println(" maxX=" + width);
206 System.out.println(" maxY=" + height);
207
208 File file = new File(logFileName);
209
210 LogFileTraces traces = new LogFileTraces(logFileName, file);
211 traces.loadTraces();
212 traces.setBurnIn(burnin);
213
214 ArrayList<ArrayList> values = new ArrayList<ArrayList>();
215 for (int j = 0; j < 2; j++) {
216 values.add(new ArrayList(traces.getValues(indices[j])));
217 }
218 int totalSize = values.get(0).size();
219 System.out.println("total samples = " + totalSize);
220
221 final DensityRectangle densityRectangle = new DensityRectangle(0, 0, width, height);
222
223 for (int i = 0; i < values.get(0).size(); i++) {
224 densityRectangle.addPoint(new Point2D.Double((Double) values.get(0).get(i), (Double) values.get(1).get(i)));
225 }
226
227 System.out.println("posterior prob visualized = " + ((double) densityRectangle.getPointCount()) / totalSize);
228
229
230 JFrame frame = new JFrame();
231 frame.setSize(400, 400);
232 frame.getContentPane().add(
233 new JComponent() {
234 /**
235 *
236 */
237 private static final long serialVersionUID = -4045142094590087910L;
238
239 public void paintComponent(Graphics g) {
240
241 Rectangle2D bounds = getBounds();
242
243 densityRectangle.paint((Graphics2D) g, bounds);
244
245 }
246 }, BorderLayout.CENTER
247 );
248 frame.setVisible(true);
249 }
250
251 }