| 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.inferencexml.operators; |
| 27 |
|
| 28 |
import dr.inference.model.Parameter; |
| 29 |
import dr.inference.model.Statistic; |
| 30 |
import dr.inference.operators.BitSwapOperator; |
| 31 |
import dr.inference.operators.MCMCOperator; |
| 32 |
import dr.inferencexml.distribution.MixedDistributionLikelihoodParser; |
| 33 |
import dr.xml.*; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class BitSwapOperatorParser extends AbstractXMLObjectParser { |
| 39 |
|
| 40 |
public static final String BIT_SWAP_OPERATOR = "bitSwapOperator"; |
| 41 |
public static final String RADIUS = "radius"; |
| 42 |
|
| 43 |
private static final String DATA = MixedDistributionLikelihoodParser.DATA; |
| 44 |
private static final String INDICATORS = MixedDistributionLikelihoodParser.INDICATORS; |
| 45 |
|
| 46 |
public String getParserName() { |
| 47 |
return BIT_SWAP_OPERATOR; |
| 48 |
} |
| 49 |
|
| 50 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 51 |
|
| 52 |
final double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT); |
| 53 |
Parameter data = (Parameter) ((XMLObject) xo.getChild(DATA)).getChild(Parameter.class); |
| 54 |
Parameter indicators = (Parameter) ((XMLObject) xo.getChild(INDICATORS)).getChild(Parameter.class); |
| 55 |
int radius = -1; |
| 56 |
|
| 57 |
if (xo.hasAttribute(RADIUS)) { |
| 58 |
double rd = xo.getDoubleAttribute(RADIUS); |
| 59 |
|
| 60 |
if (rd > 0) { |
| 61 |
if (rd < 1) { |
| 62 |
rd = Math.round(rd * indicators.getDimension()); |
| 63 |
} |
| 64 |
radius = (int) Math.round(rd); |
| 65 |
if (!(radius >= 1 && radius < indicators.getDimension() - 1)) { |
| 66 |
radius = -1; |
| 67 |
} |
| 68 |
} |
| 69 |
if (radius < 1) { |
| 70 |
throw new XMLParseException("invalid radius " + rd); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
return new BitSwapOperator(data, indicators, radius, weight); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
public String getParserDescription() { |
| 82 |
return "This element returns a bit-swap operator on a given parameter and data."; |
| 83 |
} |
| 84 |
|
| 85 |
public Class getReturnType() { |
| 86 |
return BitSwapOperator.class; |
| 87 |
} |
| 88 |
|
| 89 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 90 |
return rules; |
| 91 |
} |
| 92 |
|
| 93 |
private final XMLSyntaxRule[] rules = { |
| 94 |
AttributeRule.newDoubleRule(MCMCOperator.WEIGHT), |
| 95 |
AttributeRule.newDoubleRule(RADIUS, true), |
| 96 |
new ElementRule(DATA, new XMLSyntaxRule[]{new ElementRule(Statistic.class)}), |
| 97 |
new ElementRule(INDICATORS, new XMLSyntaxRule[]{new ElementRule(Statistic.class)}), |
| 98 |
}; |
| 99 |
} |