| 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.model; |
| 27 |
|
| 28 |
import dr.inference.model.MaskedParameter; |
| 29 |
import dr.inference.model.MatrixParameter; |
| 30 |
import dr.inference.model.Parameter; |
| 31 |
import dr.xml.*; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public class MaskedMatrixParameterParser extends AbstractXMLObjectParser { |
| 37 |
|
| 38 |
public static final String MASKED_MATRIX_PARAMETER = "maskedMatrixParameter"; |
| 39 |
public static final String MASKING = "mask"; |
| 40 |
public static final String COMPLEMENT = "complement"; |
| 41 |
public static final String FROM = "from"; |
| 42 |
public static final String TO = "to"; |
| 43 |
public static final String EVERY = "every"; |
| 44 |
|
| 45 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 46 |
|
| 47 |
MatrixParameter matrix = (MatrixParameter) xo.getChild(MatrixParameter.class); |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
Parameter mask; |
| 56 |
|
| 57 |
XMLObject cxo = xo.getChild(MASKING); |
| 58 |
if (cxo != null) { |
| 59 |
mask = (Parameter) cxo.getChild(Parameter.class); |
| 60 |
} else { |
| 61 |
int from = xo.getAttribute(FROM, 1) - 1; |
| 62 |
int to = xo.getAttribute(TO, matrix.getRowDimension()) - 1; |
| 63 |
int every = xo.getAttribute(EVERY, 1); |
| 64 |
|
| 65 |
if (from < 0) throw new XMLParseException("illegal 'from' attribute in " + MASKED_MATRIX_PARAMETER |
| 66 |
+ " element"); |
| 67 |
if (to < 0 || to < from) throw new XMLParseException("illegal 'to' attribute in " |
| 68 |
+ MASKED_MATRIX_PARAMETER + " element"); |
| 69 |
if (every <= 0) throw new XMLParseException("illegal 'every' attribute in " + MASKED_MATRIX_PARAMETER |
| 70 |
+ " element"); |
| 71 |
|
| 72 |
mask = new Parameter.Default(matrix.getRowDimension(), 0.0); |
| 73 |
for (int i = from; i <= to; i += every) { |
| 74 |
mask.setParameterValue(i, 1.0); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
if (mask.getDimension() != matrix.getRowDimension()) |
| 79 |
throw new XMLParseException("rowDim(" + matrix.getId() + ") != dim(" + mask.getId() + ")"); |
| 80 |
|
| 81 |
boolean ones = !xo.getAttribute(COMPLEMENT, false); |
| 82 |
|
| 83 |
MaskedParameter[] maskedParameters = new MaskedParameter[matrix.getColumnDimension()]; |
| 84 |
for (int col = 0; col < matrix.getColumnDimension(); ++col) { |
| 85 |
maskedParameters[col] = new MaskedParameter(matrix.getParameter(col)); |
| 86 |
maskedParameters[col].addMask(mask, ones); |
| 87 |
} |
| 88 |
|
| 89 |
MatrixParameter maskedMatrix = new MatrixParameter(matrix.getId() + ".masked", maskedParameters); |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
return maskedMatrix; |
| 96 |
} |
| 97 |
|
| 98 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 99 |
return rules; |
| 100 |
} |
| 101 |
|
| 102 |
private final XMLSyntaxRule[] rules = { |
| 103 |
new ElementRule(MatrixParameter.class), |
| 104 |
new ElementRule(MASKING, |
| 105 |
new XMLSyntaxRule[]{ |
| 106 |
new ElementRule(Parameter.class) |
| 107 |
}, true), |
| 108 |
AttributeRule.newBooleanRule(COMPLEMENT, true), |
| 109 |
AttributeRule.newIntegerRule(FROM, true), |
| 110 |
AttributeRule.newIntegerRule(TO, true), |
| 111 |
AttributeRule.newIntegerRule(EVERY, true), |
| 112 |
}; |
| 113 |
|
| 114 |
public String getParserDescription() { |
| 115 |
return "A masked matrix parameter."; |
| 116 |
} |
| 117 |
|
| 118 |
public Class getReturnType() { |
| 119 |
return MatrixParameter.class; |
| 120 |
} |
| 121 |
|
| 122 |
public String getParserName() { |
| 123 |
return MASKED_MATRIX_PARAMETER; |
| 124 |
} |
| 125 |
} |