| 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.evomodel.continuous; |
| 27 |
|
| 28 |
import dr.inference.model.Parameter; |
| 29 |
import dr.xml.*; |
| 30 |
import dr.geo.cartogram.CartogramMapping; |
| 31 |
|
| 32 |
import java.awt.geom.Point2D; |
| 33 |
import java.awt.geom.Rectangle2D; |
| 34 |
import java.io.IOException; |
| 35 |
import java.util.logging.Logger; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public class CartogramDiffusionModel extends MultivariateDiffusionModel { |
| 42 |
|
| 43 |
public static final String DIFFUSION_PROCESS = "cartogramDiffusionModel"; |
| 44 |
public static final String FILENAME = "cartogramFileName"; |
| 45 |
public static final String DENSITY = "density"; |
| 46 |
|
| 47 |
public static final String BOUNDING_BOX = "boundingBox"; |
| 48 |
public static final String MIN_X = "minX"; |
| 49 |
public static final String MAX_X = "maxX"; |
| 50 |
public static final String MIN_Y = "minY"; |
| 51 |
public static final String MAX_Y = "maxY"; |
| 52 |
|
| 53 |
public static final String XSIZE = "xGridSize"; |
| 54 |
public static final String YSIZE = "yGridSize"; |
| 55 |
|
| 56 |
public CartogramDiffusionModel(String name, Parameter precision) { |
| 57 |
super(); |
| 58 |
this.precision = precision; |
| 59 |
addVariable(precision); |
| 60 |
setId(name); |
| 61 |
Logger.getLogger("dr.evomodel.continuous").info( |
| 62 |
"Constructing cartogram diffusion model '"+ getId() +"': \n" + |
| 63 |
"\tIf you use this model, please reference: Lemey, Drummond and Suchard (in preparation)\n" + |
| 64 |
"\tPrecision: " + precision.getId() |
| 65 |
); |
| 66 |
} |
| 67 |
|
| 68 |
public void addMapping(CartogramMapping mapping) { |
| 69 |
this.mapping = mapping; |
| 70 |
Logger.getLogger("dr.evomodel.continuous").info( |
| 71 |
"\tMapping : " + mapping.toString() + "\n" |
| 72 |
); |
| 73 |
} |
| 74 |
|
| 75 |
protected CartogramMapping getMapping() { return mapping; } |
| 76 |
|
| 77 |
protected double calculateLogDensity(double[] start, double[] stop, double time) { |
| 78 |
|
| 79 |
Point2D realStart = new Point2D.Double(start[0],start[1]); |
| 80 |
Point2D realStop = new Point2D.Double(stop[0], stop[1]); |
| 81 |
|
| 82 |
final CartogramMapping mapping = getMapping(); |
| 83 |
|
| 84 |
Point2D mappedStart = mapping.map(realStart); |
| 85 |
Point2D mappedStop = mapping.map(realStop); |
| 86 |
|
| 87 |
if (mappedStart == null || mappedStop == null) |
| 88 |
return Double.NEGATIVE_INFINITY; |
| 89 |
|
| 90 |
final double factor = mapping.getAverageDensity(); |
| 91 |
final double distance = mappedStop.distance(mappedStart); |
| 92 |
final double inverseVariance = precision.getParameterValue(0) / time / Math.pow(factor,0.25); |
| 93 |
|
| 94 |
|
| 95 |
return -LOG2PI + Math.log(inverseVariance) - 0.00 * Math.log(factor) - 0.5*(distance * distance * inverseVariance); |
| 96 |
} |
| 97 |
|
| 98 |
protected void calculatePrecisionInfo() { |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
public static Rectangle2D parseRectangle2D(XMLObject xo) throws XMLParseException { |
| 103 |
|
| 104 |
XMLObject cxo = (XMLObject) xo.getChild(BOUNDING_BOX); |
| 105 |
double minX = cxo.getAttribute(MIN_X, 0.0); |
| 106 |
double maxX = cxo.getAttribute(MAX_X, 0.0); |
| 107 |
double minY = cxo.getAttribute(MIN_Y, 0.0); |
| 108 |
double maxY = cxo.getAttribute(MAX_Y, 0.0); |
| 109 |
|
| 110 |
if (maxX - minX <= 0 || maxY - minY <= 0) |
| 111 |
throw new XMLParseException("Bounding box must contain volume"); |
| 112 |
|
| 113 |
return new Rectangle2D.Double(minX, minY, maxX - minX, maxY - minY); |
| 114 |
} |
| 115 |
|
| 116 |
public static CartogramMapping parseCartogramMapping(XMLObject xo, Rectangle2D boundingBox) throws XMLParseException { |
| 117 |
int xGridSize = xo.getAttribute(XSIZE, 0); |
| 118 |
int yGridSize = xo.getAttribute(YSIZE, 0); |
| 119 |
|
| 120 |
if (xGridSize <= 1 || yGridSize <= 1) |
| 121 |
throw new XMLParseException("Strictly positive grid sizes required"); |
| 122 |
|
| 123 |
CartogramMapping mapping = new CartogramMapping(xGridSize, yGridSize, boundingBox); |
| 124 |
|
| 125 |
String fileName = xo.getAttribute(FILENAME, "NONE"); |
| 126 |
|
| 127 |
Logger.getLogger("dr.evomodel.continuous").info( |
| 128 |
"Loading cartogram file: " + fileName + "\n"); |
| 129 |
|
| 130 |
if (xo.hasAttribute(FILENAME)) { |
| 131 |
|
| 132 |
try { |
| 133 |
mapping.readCartogramOutput(fileName); |
| 134 |
} catch (IOException e) { |
| 135 |
throw new XMLParseException(e.getMessage()); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
double density = xo.getAttribute(DENSITY,1.0); |
| 140 |
mapping.setAverageDensity(density); |
| 141 |
|
| 142 |
return mapping; |
| 143 |
} |
| 144 |
|
| 145 |
protected static ElementRule boundingBoxRules = new ElementRule(BOUNDING_BOX, new XMLSyntaxRule[]{ |
| 146 |
AttributeRule.newDoubleRule(MIN_X), |
| 147 |
AttributeRule.newDoubleRule(MAX_X), |
| 148 |
AttributeRule.newDoubleRule(MIN_Y), |
| 149 |
AttributeRule.newDoubleRule(MAX_Y), |
| 150 |
}); |
| 151 |
|
| 152 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 153 |
|
| 154 |
public String getParserName() { |
| 155 |
return DIFFUSION_PROCESS; |
| 156 |
} |
| 157 |
|
| 158 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 159 |
|
| 160 |
Rectangle2D boundingBox = parseRectangle2D(xo); |
| 161 |
|
| 162 |
CartogramMapping mapping = parseCartogramMapping(xo, boundingBox); |
| 163 |
|
| 164 |
Parameter diffusionParam = (Parameter) xo.getChild(Parameter.class); |
| 165 |
|
| 166 |
CartogramDiffusionModel model = new CartogramDiffusionModel(xo.getId(), diffusionParam); |
| 167 |
model.addMapping(mapping); |
| 168 |
|
| 169 |
return model; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
public String getParserDescription() { |
| 177 |
return "Describes a bivariate diffusion process using cartogram distances."; |
| 178 |
} |
| 179 |
|
| 180 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 181 |
return rules; |
| 182 |
} |
| 183 |
|
| 184 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 185 |
new ElementRule(Parameter.class), |
| 186 |
AttributeRule.newStringRule(FILENAME,true), |
| 187 |
AttributeRule.newIntegerRule(XSIZE), |
| 188 |
AttributeRule.newIntegerRule(YSIZE), |
| 189 |
boundingBoxRules, |
| 190 |
}; |
| 191 |
|
| 192 |
public Class getReturnType() { |
| 193 |
return MultivariateDiffusionModel.class; |
| 194 |
} |
| 195 |
}; |
| 196 |
|
| 197 |
private Parameter precision; |
| 198 |
private CartogramMapping mapping; |
| 199 |
|
| 200 |
} |