| 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.geo.operators; |
| 27 |
|
| 28 |
import dr.geo.AbstractPolygon2D; |
| 29 |
import dr.geo.GeoSpatialDistribution; |
| 30 |
import dr.geo.MultiRegionGeoSpatialDistribution; |
| 31 |
import dr.geo.Polygon2D; |
| 32 |
import dr.inference.distribution.AbstractDistributionLikelihood; |
| 33 |
import dr.inference.distribution.CachedDistributionLikelihood; |
| 34 |
import dr.inference.distribution.MultivariateDistributionLikelihood; |
| 35 |
import dr.inference.model.Parameter; |
| 36 |
import dr.inference.operators.MCMCOperator; |
| 37 |
import dr.inference.operators.UniformOperator; |
| 38 |
import dr.xml.*; |
| 39 |
|
| 40 |
import java.util.ArrayList; |
| 41 |
import java.util.List; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public class UniformGeoSpatialOperatorParser extends AbstractXMLObjectParser { |
| 46 |
public final static String UNIFORM_OPERATOR = "uniformGeoSpatialOperator"; |
| 47 |
public static final String LOWER = "lower"; |
| 48 |
public static final String UPPER = "upper"; |
| 49 |
|
| 50 |
public String getParserName() { |
| 51 |
return UNIFORM_OPERATOR; |
| 52 |
} |
| 53 |
|
| 54 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 55 |
|
| 56 |
double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT); |
| 57 |
Parameter parameter = (Parameter) xo.getChild(Parameter.class); |
| 58 |
|
| 59 |
if( parameter.getDimension() == 0 ) { |
| 60 |
throw new XMLParseException("parameter with 0 dimension."); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
MultivariateDistributionLikelihood likelihood = (MultivariateDistributionLikelihood) |
| 66 |
xo.getChild(MultivariateDistributionLikelihood.class); |
| 67 |
|
| 68 |
if (likelihood == null) { |
| 69 |
CachedDistributionLikelihood cached = (CachedDistributionLikelihood) xo.getChild(CachedDistributionLikelihood.class); |
| 70 |
AbstractDistributionLikelihood ab = cached.getDistributionLikelihood(); |
| 71 |
if (!(ab instanceof MultivariateDistributionLikelihood)) { |
| 72 |
throw new XMLParseException("invalid likelihood type in " + xo.getId()); |
| 73 |
} |
| 74 |
|
| 75 |
likelihood = (MultivariateDistributionLikelihood) ab; |
| 76 |
} |
| 77 |
|
| 78 |
List<AbstractPolygon2D> polygonList = new ArrayList<AbstractPolygon2D>(); |
| 79 |
|
| 80 |
if (likelihood.getDistribution() instanceof MultiRegionGeoSpatialDistribution) { |
| 81 |
for (GeoSpatialDistribution spatial : ((MultiRegionGeoSpatialDistribution) likelihood.getDistribution()).getRegions()) { |
| 82 |
polygonList.add(spatial.getRegion()); |
| 83 |
} |
| 84 |
} else if (likelihood.getDistribution() instanceof GeoSpatialDistribution) { |
| 85 |
polygonList.add( |
| 86 |
((GeoSpatialDistribution) likelihood.getDistribution()).getRegion() |
| 87 |
); |
| 88 |
} else { |
| 89 |
throw new XMLParseException("Multivariate distribution must be either a GeoSpatialDistribution " + |
| 90 |
"or a MultiRegionGeoSpatialDistribution"); |
| 91 |
} |
| 92 |
|
| 93 |
return new UniformGeoSpatialOperator(parameter, weight, polygonList); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public String getParserDescription() { |
| 101 |
return "An operator that picks new parameter values uniformly at random."; |
| 102 |
} |
| 103 |
|
| 104 |
public Class getReturnType() { |
| 105 |
return UniformOperator.class; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 110 |
return rules; |
| 111 |
} |
| 112 |
|
| 113 |
private final XMLSyntaxRule[] rules = { |
| 114 |
AttributeRule.newDoubleRule(MCMCOperator.WEIGHT), |
| 115 |
|
| 116 |
|
| 117 |
new ElementRule(Parameter.class), |
| 118 |
new XORRule( |
| 119 |
new ElementRule(MultivariateDistributionLikelihood.class), |
| 120 |
new ElementRule(CachedDistributionLikelihood.class) |
| 121 |
), |
| 122 |
}; |
| 123 |
} |