| 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.operators.MCMCOperator; |
| 30 |
import dr.inference.operators.MaskMoveOperator; |
| 31 |
import dr.xml.*; |
| 32 |
|
| 33 |
import java.util.ArrayList; |
| 34 |
import java.util.List; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public class MaskMoveOperatorParser extends AbstractXMLObjectParser { |
| 40 |
|
| 41 |
public static final String MASK_FLIP_OPERATOR = "maskMoveOperator"; |
| 42 |
public static final String CUT_POINT = "cutPoint"; |
| 43 |
public static final String CUT_MASK = "cutMask"; |
| 44 |
public static final String SELECT_BEFORE = "selectBefore"; |
| 45 |
public static final String SELECT_AFTER = "selectAfter"; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public String getParserName() { |
| 50 |
return MASK_FLIP_OPERATOR; |
| 51 |
} |
| 52 |
|
| 53 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 54 |
|
| 55 |
double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT); |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
List<Parameter> masks = new ArrayList<Parameter>(); |
| 60 |
for (int i = 0; i < xo.getChildCount(); ++i) { |
| 61 |
if (xo.getChild(i) instanceof Parameter) { |
| 62 |
masks.add((Parameter) xo.getChild(i)); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
Parameter cutPoint = (Parameter) xo.getElementFirstChild(CUT_POINT); |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
double[] before = xo.getChild(CUT_MASK).getDoubleArrayAttribute(SELECT_BEFORE); |
| 71 |
double[] after = xo.getChild(CUT_MASK).getDoubleArrayAttribute(SELECT_AFTER); |
| 72 |
|
| 73 |
int[] beforeList = new int[before.length]; |
| 74 |
for (int i = 0; i < before.length; ++i) { |
| 75 |
beforeList[i] = ((int) (before[i] -1.0 + 0.5)); |
| 76 |
} |
| 77 |
|
| 78 |
int[] afterList = new int[after.length]; |
| 79 |
for (int i = 0; i < before.length; ++i) { |
| 80 |
afterList[i] = ((int) (after[i] -1.0 + 0.5)); |
| 81 |
} |
| 82 |
|
| 83 |
if (beforeList.length != afterList.length) { |
| 84 |
throw new XMLParseException("selectBefore length != selectAfter length"); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
if (!MaskMoveOperator.checkMaskValues(masks, cutPoint, beforeList, afterList)) { |
| 89 |
String name = xo.hasId() ? xo.getId() : "null"; |
| 90 |
throw new XMLParseException("Bad initialization parameter values in " + name); |
| 91 |
} |
| 92 |
|
| 93 |
return new MaskMoveOperator(masks, cutPoint, beforeList, afterList, weight); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public String getParserDescription() { |
| 101 |
return "This element returns a mask-flip operator on a set of given parameters."; |
| 102 |
} |
| 103 |
|
| 104 |
public Class getReturnType() { |
| 105 |
return MaskMoveOperator.class; |
| 106 |
} |
| 107 |
|
| 108 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 109 |
return rules; |
| 110 |
} |
| 111 |
|
| 112 |
private final XMLSyntaxRule[] rules = { |
| 113 |
AttributeRule.newDoubleRule(MCMCOperator.WEIGHT), |
| 114 |
new ElementRule(Parameter.class, 1, Integer.MAX_VALUE), |
| 115 |
new ElementRule(CUT_POINT, new XMLSyntaxRule[] { |
| 116 |
new ElementRule(Parameter.class), |
| 117 |
}), |
| 118 |
new ElementRule(CUT_MASK, new XMLSyntaxRule[] { |
| 119 |
AttributeRule.newDoubleArrayRule(SELECT_BEFORE), |
| 120 |
AttributeRule.newDoubleArrayRule(SELECT_AFTER), |
| 121 |
}) |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
}; |
| 129 |
} |