Debian Codegraph @

src/dr/geo/Polygon2DSampling.java

raw symbols SCIP
1 /*
2 * Polygon2DFill.java
3 *
4 * Copyright (c) 2002-2017 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.geo;
27
28 import dr.inference.distribution.MultivariateDistributionLikelihood;
29 import dr.inference.model.Parameter;
30 import dr.xml.XMLParseException;
31 import org.jdom.Element;
32 import java.awt.geom.Point2D;
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.util.Random;
36
37 /**
38 * 2D Polygon that uses a samplingProbability attribute as inclusion probability.
39 * These polygons are typically non-overlapping and a list of such polygons currently needs its fillValues to sum to one.
40 * @author Guy Baele
41 */
42 public class Polygon2DSampling extends Polygon2D {
43
44 public Polygon2DSampling(double[] x, double[] y, double fillValue) {
45 super(x,y);
46 this.fillValue = fillValue;
47 }
48
49 public Polygon2DSampling(Element e, double fillValue) {
50 super(e);
51
52 this.fillValue = fillValue;
53 //no need to recompute this all the time
54 this.logFillValue = Math.log(fillValue);
55 //this.hasFillValue = true;
56 }
57
58 public double getProbability(Point2D Point2D, boolean outside) {
59 boolean contains = containsPoint2D(Point2D);
60 if (contains) {
61 return this.fillValue;
62 } else {
63 return 0.0;
64 }
65 }
66
67 public double getLogProbability(Point2D Point2D, boolean outside) {
68 boolean contains = containsPoint2D(Point2D);
69 if (contains) {
70 return this.logFillValue;
71 } else {
72 return Double.NEGATIVE_INFINITY;
73 }
74 }
75
76 @Override
77 public void setFillValue(double value) {
78 fillValue = value;
79 logFillValue = Math.log(value);
80 //hasFillValue = true;
81 }
82
83 @Override
84 public double getFillValue() {
85 return fillValue;
86 }
87
88 @Override
89 public double getLogFillValue() {
90 return logFillValue;
91 }
92
93 @Override
94 public boolean hasFillValue() {
95 return true;
96 }
97
98 private double fillValue;
99 private double logFillValue;
100 //private boolean hasFillValue;
101
102 public static void main(String[] args) {
103
104 List<AbstractPolygon2D> polygons = Polygon2D.readKMLFile(args[0]);
105 System.out.println("Reading from file: " + args[0] + "\n");
106
107 List<GeoSpatialDistribution> geoSpatialDistributions = new ArrayList<GeoSpatialDistribution>();
108 //check if all the polygons have either a fillValue or do not have this fillValue
109 boolean overAllFill = false;
110 AbstractPolygon2D first = polygons.get(0);
111 System.out.println("Polygon 0: " + first.getFillValue());
112 boolean equalFills = first.hasFillValue();
113 if (polygons.size() > 1) {
114 for (int i = 1; i < polygons.size(); i++) {
115 if (!(equalFills == polygons.get(i).hasFillValue())) {
116 throw new RuntimeException("Inconsistent fillValue attributes provided.");
117 } else {
118 System.out.println("Polygon " + i + ": " + polygons.get(i).getFillValue());
119 }
120 }
121 }
122 if (equalFills) {
123 double sum = 0.0;
124 for (AbstractPolygon2D region : polygons) {
125 sum += region.getFillValue();
126 }
127 if (Math.abs(sum - 1.0) > 1E-12) {
128 throw new RuntimeException("Fill values in " + args[0] + " do not sum to 1 : " + sum);
129 } else {
130 System.out.println("Provided sampling probabilities sum to 1.0");
131 }
132 }
133 overAllFill = equalFills;
134 for (AbstractPolygon2D region : polygons) {
135 geoSpatialDistributions.add(new GeoSpatialDistribution("", region, true));
136 }
137
138 System.out.println("\nTesting sampling probabilities:");
139
140 MultivariateDistributionLikelihood likelihood = new MultivariateDistributionLikelihood(
141 new MultiRegionGeoSpatialDistribution("", geoSpatialDistributions, false, overAllFill));
142 double[] coordinates = new double[2];
143 coordinates[0] = 12.68379879;
144 coordinates[1] = 108.12982178;
145 Parameter parameter = new Parameter.Default(coordinates);
146 likelihood.addData(parameter);
147
148 System.out.println("logL = " + likelihood.getLogLikelihood());
149 System.out.println("L = " + Math.exp(likelihood.getLogLikelihood()));
150
151 parameter.setParameterValue(0,13.68379879);
152 System.out.println("logL = " + likelihood.getLogLikelihood());
153 System.out.println("L = " + Math.exp(likelihood.getLogLikelihood()));
154
155 parameter.setParameterValue(0,12.68);
156 System.out.println("logL = " + likelihood.getLogLikelihood());
157 System.out.println("L = " + Math.exp(likelihood.getLogLikelihood()));
158
159 parameter.setParameterValue(0, 13.03387928);
160 parameter.setParameterValue(1, 108.51604462);
161 System.out.println("logL = " + likelihood.getLogLikelihood());
162 System.out.println("L = " + Math.exp(likelihood.getLogLikelihood()));
163
164 parameter.setParameterValue(0, 13.01971245);
165 parameter.setParameterValue(1, 108.56764221);
166 System.out.println("logL = " + likelihood.getLogLikelihood());
167 System.out.println("L = " + Math.exp(likelihood.getLogLikelihood()));
168
169 parameter.setParameterValue(0, 13.018);
170 System.out.println("logL = " + likelihood.getLogLikelihood());
171 System.out.println("L = " + Math.exp(likelihood.getLogLikelihood()));
172
173 //from 8 to 14 and from 104 to 110
174 /*System.out.println("\n\n\nplot(0,0,xlim=c(9,14),ylim=c(105,110))");
175 Random rand = new Random();
176 for (int i = 0; i < 100000; i++) {
177 parameter.setParameterValue(0, rand.nextDouble()*6+9);
178 parameter.setParameterValue(1, rand.nextDouble()*6+105);
179 if (!Double.isInfinite(likelihood.getLogLikelihood())) {
180 double color = Math.exp(likelihood.getLogLikelihood());
181 //System.out.println(color);
182 String col = "black";
183 if (Math.abs(color - 0.106508875739645) < 1E-5) {
184 col = "red";
185 } else if (Math.abs(color - 0.029585798816568) < 1E-5) {
186 col = "blue";
187 } else if (Math.abs(color - 0.0769230769230769) < 1E-5) {
188 col = "green";
189 } else if (Math.abs(color - 0.0355029585798817) < 1E-5) {
190 col = "grey";
191 } else if (Math.abs(color - 0.124260355029586) < 1E-5) {
192 col = "purple";
193 } else if (Math.abs(color - 0.112426035502959) < 1E-5) {
194 col = "orange";
195 } else if (Math.abs(color - 0.136094674556213) < 1E-5) {
196 col = "yellow";
197 } else if (Math.abs(color - 0.0946745562130177) < 1E-5) {
198 col = "burlywood";
199 } else if (Math.abs(color - 0.284023668639053) < 1E-5) {
200 col = "deepskyblue";
201 }
202 System.out.println("points(" + parameter.getParameterValue(0) + "," + parameter.getParameterValue(1) + ",col=\"" + col + "\",pch=20,bg=\""+ col +"\")");
203 }
204 }*/
205
206 }
207
208 }