| 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.distribution; |
| 27 |
|
| 28 |
import dr.inference.distribution.InverseGaussianDistributionModel; |
| 29 |
import dr.inference.model.Parameter; |
| 30 |
import dr.xml.*; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public class InverseGaussianDistributionModelParser extends AbstractXMLObjectParser { |
| 36 |
|
| 37 |
public static final String INVERSEGAUSSIAN_DISTRIBUTION_MODEL = "inverseGaussianDistributionModel"; |
| 38 |
public static final String MEAN = "mean"; |
| 39 |
public static final String STDEV = "stdev"; |
| 40 |
public static final String SHAPE = "shape"; |
| 41 |
public static final String OFFSET = "offset"; |
| 42 |
|
| 43 |
public String getParserName() { |
| 44 |
return INVERSEGAUSSIAN_DISTRIBUTION_MODEL; |
| 45 |
} |
| 46 |
|
| 47 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 48 |
|
| 49 |
Parameter meanParam; |
| 50 |
|
| 51 |
double offset = xo.getAttribute(OFFSET, 0.0); |
| 52 |
|
| 53 |
XMLObject cxo = xo.getChild(MEAN); |
| 54 |
if (cxo.getChild(0) instanceof Parameter) { |
| 55 |
meanParam = (Parameter) cxo.getChild(Parameter.class); |
| 56 |
} else { |
| 57 |
meanParam = new Parameter.Default(cxo.getDoubleChild(0)); |
| 58 |
} |
| 59 |
|
| 60 |
if(xo.hasChildNamed(STDEV) && xo.hasChildNamed(SHAPE)) { |
| 61 |
throw new RuntimeException("XML has both standard deviation and shape for Inverse Gaussian distribution"); |
| 62 |
} |
| 63 |
else if(xo.hasChildNamed(STDEV)) { |
| 64 |
Parameter stdevParam; |
| 65 |
cxo = xo.getChild(STDEV); |
| 66 |
if (cxo.getChild(0) instanceof Parameter) { |
| 67 |
stdevParam = (Parameter) cxo.getChild(Parameter.class); |
| 68 |
} else { |
| 69 |
stdevParam = new Parameter.Default(cxo.getDoubleChild(0)); |
| 70 |
} |
| 71 |
return new InverseGaussianDistributionModel(meanParam, stdevParam, offset, false); |
| 72 |
} |
| 73 |
else if(xo.hasChildNamed(SHAPE)) { |
| 74 |
Parameter shapeParam; |
| 75 |
cxo = xo.getChild(SHAPE); |
| 76 |
if (cxo.getChild(0) instanceof Parameter) { |
| 77 |
shapeParam = (Parameter) cxo.getChild(Parameter.class); |
| 78 |
} else { |
| 79 |
shapeParam = new Parameter.Default(cxo.getDoubleChild(0)); |
| 80 |
} |
| 81 |
return new InverseGaussianDistributionModel(meanParam, shapeParam, offset, true); |
| 82 |
} |
| 83 |
else { |
| 84 |
throw new RuntimeException("XML has neither standard deviation nor shape for Inverse Gaussian distribution"); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 93 |
return rules; |
| 94 |
} |
| 95 |
|
| 96 |
private final XMLSyntaxRule[] rules = { |
| 97 |
AttributeRule.newDoubleRule(OFFSET, true), |
| 98 |
new ElementRule(MEAN, |
| 99 |
new XMLSyntaxRule[]{ |
| 100 |
new XORRule( |
| 101 |
new ElementRule(Parameter.class), |
| 102 |
new ElementRule(Double.class) |
| 103 |
)} |
| 104 |
, false), |
| 105 |
new ElementRule(STDEV, |
| 106 |
new XMLSyntaxRule[]{ |
| 107 |
new XORRule( |
| 108 |
new ElementRule(Parameter.class), |
| 109 |
new ElementRule(Double.class) |
| 110 |
)} |
| 111 |
, true), |
| 112 |
|
| 113 |
new ElementRule(SHAPE, |
| 114 |
new XMLSyntaxRule[]{ |
| 115 |
new XORRule( |
| 116 |
new ElementRule(Parameter.class), |
| 117 |
new ElementRule(Double.class) |
| 118 |
)} |
| 119 |
, true) |
| 120 |
}; |
| 121 |
|
| 122 |
public String getParserDescription() { |
| 123 |
return "Describes a inverse gaussian distribution with a given mean and shape (or standard deviation) " + |
| 124 |
"that can be used in a distributionLikelihood element"; |
| 125 |
} |
| 126 |
|
| 127 |
public Class getReturnType() { |
| 128 |
return InverseGaussianDistributionModel.class; |
| 129 |
} |
| 130 |
|
| 131 |
} |