Debian Codegraph @

src/dr/inferencexml/distribution/InverseGaussianDistributionModelParser.java

raw symbols SCIP
1 /*
2 * InverseGaussianDistributionModelParser.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.InverseGaussianDistributionModel;
29 import dr.inference.model.Parameter;
30 import dr.xml.*;
31
32 /**
33 * Reads an inverse gaussian distribution model from a DOM Document element.
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 // AbstractXMLObjectParser implementation
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 }