| 1 |
package dr.evomodel.antigenic.phyloclustering.misc.obsolete; |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
import dr.inference.model.Parameter; |
| 6 |
import dr.inference.operators.AdaptableMCMCOperator; |
| 7 |
import dr.inference.operators.AdaptationMode; |
| 8 |
import dr.inference.operators.MCMCOperator; |
| 9 |
import dr.xml.*; |
| 10 |
|
| 11 |
|
| 12 |
public class ClusterWalkOperatorParser extends AbstractXMLObjectParser { |
| 13 |
|
| 14 |
|
| 15 |
public static final String CLUSTER_WALK_OPERATOR = "ClusterWalkOperator"; |
| 16 |
public static final String WINDOW_SIZE = "windowSize"; |
| 17 |
public static final String UPDATE_INDEX = "updateIndex"; |
| 18 |
public static final String UPPER = "upper"; |
| 19 |
public static final String LOWER = "lower"; |
| 20 |
|
| 21 |
public static final String BOUNDARY_CONDITION = "boundaryCondition"; |
| 22 |
|
| 23 |
public String getParserName() { |
| 24 |
System.out.println("Yo!"); |
| 25 |
|
| 26 |
return CLUSTER_WALK_OPERATOR; |
| 27 |
} |
| 28 |
|
| 29 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 30 |
|
| 31 |
AdaptationMode mode = AdaptationMode.parseMode(xo); |
| 32 |
|
| 33 |
double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT); |
| 34 |
double windowSize = xo.getDoubleAttribute(WINDOW_SIZE); |
| 35 |
Parameter parameter = (Parameter) xo.getChild(Parameter.class); |
| 36 |
|
| 37 |
Double lower = null; |
| 38 |
Double upper = null; |
| 39 |
|
| 40 |
if (xo.hasAttribute(LOWER)) { |
| 41 |
lower = xo.getDoubleAttribute(LOWER); |
| 42 |
} |
| 43 |
|
| 44 |
if (xo.hasAttribute(UPPER)) { |
| 45 |
upper = xo.getDoubleAttribute(UPPER); |
| 46 |
} |
| 47 |
|
| 48 |
ClusterWalkOperator.BoundaryCondition condition = ClusterWalkOperator.BoundaryCondition.valueOf( |
| 49 |
xo.getAttribute(BOUNDARY_CONDITION, ClusterWalkOperator.BoundaryCondition.reflecting.name())); |
| 50 |
|
| 51 |
if (xo.hasChildNamed(UPDATE_INDEX)) { |
| 52 |
XMLObject cxo = xo.getChild(UPDATE_INDEX); |
| 53 |
Parameter updateIndex = (Parameter) cxo.getChild(Parameter.class); |
| 54 |
if (updateIndex.getDimension() != parameter.getDimension()) |
| 55 |
throw new RuntimeException("Parameter to update and missing indices must have the same dimension"); |
| 56 |
return new ClusterWalkOperator(parameter, updateIndex, windowSize, condition, |
| 57 |
weight, mode, lower, upper); |
| 58 |
} |
| 59 |
|
| 60 |
return new ClusterWalkOperator(parameter, null, windowSize, condition, weight, mode, lower, upper); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public String getParserDescription() { |
| 68 |
return "This element returns a cluster walk operator on a given parameter."; |
| 69 |
} |
| 70 |
|
| 71 |
public Class getReturnType() { |
| 72 |
return MCMCOperator.class; |
| 73 |
} |
| 74 |
|
| 75 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 76 |
return rules; |
| 77 |
} |
| 78 |
|
| 79 |
private final XMLSyntaxRule[] rules = { |
| 80 |
AttributeRule.newDoubleRule(WINDOW_SIZE), |
| 81 |
AttributeRule.newDoubleRule(MCMCOperator.WEIGHT), |
| 82 |
AttributeRule.newDoubleRule(LOWER, true), |
| 83 |
AttributeRule.newDoubleRule(UPPER, true), |
| 84 |
AttributeRule.newBooleanRule(AdaptableMCMCOperator.AUTO_OPTIMIZE, true), |
| 85 |
new ElementRule(UPDATE_INDEX, |
| 86 |
new XMLSyntaxRule[] { |
| 87 |
new ElementRule(Parameter.class), |
| 88 |
},true), |
| 89 |
new StringAttributeRule(BOUNDARY_CONDITION, null, ClusterWalkOperator.BoundaryCondition.values(), true), |
| 90 |
new ElementRule(Parameter.class) |
| 91 |
}; |
| 92 |
} |