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