Debian Codegraph @

src/dr/geo/operators/UniformGeoSpatialOperatorParser.java

raw symbols SCIP
1 /*
2 * UniformGeoSpatialOperatorParser.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.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 // AbstractXMLObjectParser implementation
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 // AttributeRule.newDoubleRule(LOWER, true),
116 // AttributeRule.newDoubleRule(UPPER, true),
117 new ElementRule(Parameter.class),
118 new XORRule(
119 new ElementRule(MultivariateDistributionLikelihood.class),
120 new ElementRule(CachedDistributionLikelihood.class)
121 ),
122 };
123 }