Debian Codegraph @

src/dr/inferencexml/distribution/GammaDistributionModelParser.java

raw symbols SCIP
1 /*
2 * GammaDistributionModelParser.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.inferencexml.distribution;
27
28 import dr.inference.distribution.GammaDistributionModel;
29 import dr.inference.model.Parameter;
30 import dr.xml.*;
31
32 public class GammaDistributionModelParser extends AbstractXMLObjectParser {
33
34 public static final String MEAN = "mean";
35 public static final String SHAPE = "shape";
36 public static final String SCALE = "scale";
37 public static final String RATE = "rate";
38 public static final String OFFSET = "offset";
39
40 public String getParserName() {
41 return GammaDistributionModel.GAMMA_DISTRIBUTION_MODEL;
42 }
43
44 private Parameter getParameterOrValue(String name, XMLObject xo) throws XMLParseException {
45 Parameter parameter;
46 if (xo.hasChildNamed(name)) {
47 XMLObject cxo = xo.getChild(name);
48
49 parameter = (Parameter)cxo.getChild(Parameter.class);
50 if (parameter == null) {
51 if (cxo.getChildCount() < 1) {
52 throw new XMLParseException("Distribution parameter, " + name + ", is missing a value or parameter element");
53 }
54 try {
55 double value = cxo.getDoubleChild(0);
56 parameter = new Parameter.Default(value);
57 } catch (XMLParseException xpe) {
58 throw new XMLParseException("Distribution parameter, " + name + ", has bad value: " + xpe.getMessage());
59 }
60 }
61
62 return parameter;
63 } else {
64 return null;
65 }
66
67 }
68
69 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
70
71 double offset = xo.getAttribute(OFFSET, 0.0);
72
73 Parameter shapeParameter = getParameterOrValue(SHAPE, xo);
74
75 Parameter parameter2;
76 GammaDistributionModel.GammaParameterizationType parameterization;
77
78 if (xo.hasChildNamed(SCALE)) {
79 parameter2 = getParameterOrValue(SCALE, xo);
80 parameterization = GammaDistributionModel.GammaParameterizationType.ShapeScale;
81 } else if (xo.hasChildNamed(RATE)) {
82 parameter2 = getParameterOrValue(RATE, xo);
83 parameterization = GammaDistributionModel.GammaParameterizationType.ShapeRate;
84 } else if (xo.hasChildNamed(MEAN)) {
85 parameter2 = getParameterOrValue(MEAN, xo);
86 parameterization = GammaDistributionModel.GammaParameterizationType.ShapeMean;
87 } else {
88 parameter2 = null;
89 parameterization = GammaDistributionModel.GammaParameterizationType.OneParameter;
90 }
91
92 return new GammaDistributionModel(parameterization, shapeParameter, parameter2, offset);
93 }
94
95 //************************************************************************
96 // AbstractXMLObjectParser implementation
97 //************************************************************************
98
99 public XMLSyntaxRule[] getSyntaxRules() {
100 return rules;
101 }
102
103 private final XMLSyntaxRule[] rules = {
104 new ElementRule(SHAPE, new XMLSyntaxRule[]{new ElementRule(Parameter.class, true)}, "Shape parameter"),
105 new ElementRule(SCALE, new XMLSyntaxRule[]{new ElementRule(Parameter.class, true)}, "Scale parameter", true),
106 new ElementRule(RATE, new XMLSyntaxRule[]{new ElementRule(Parameter.class, true)}, "Rate parameter", true),
107 new ElementRule(MEAN, new XMLSyntaxRule[]{new ElementRule(Parameter.class, true)}, "Mean parameter", true),
108 AttributeRule.newDoubleRule(OFFSET, true)
109 };
110
111 public String getParserDescription() {
112 return "The gamma probability distribution.";
113 }
114
115 public Class getReturnType() {
116 return GammaDistributionModel.class;
117 }
118 }