Debian Codegraph @

src/dr/inferencexml/distribution/EmpiricalDistributionLikelihoodParser.java

raw symbols SCIP
1 /*
2 * EmpiricalDistributionLikelihoodParser.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.EmpiricalDistributionLikelihood;
29 import dr.inference.distribution.SplineInterpolatedLikelihood;
30 import dr.inference.model.Likelihood;
31 import dr.inference.model.Statistic;
32 import dr.xml.*;
33
34 /**
35 * @author Andrew Rambaut
36 * @author Marc A. Suchard
37 */
38 public class EmpiricalDistributionLikelihoodParser extends AbstractXMLObjectParser {
39
40 public static final String FILE_NAME = "fileName";
41 public static final String DATA = "data";
42 public static final String FROM = "from";
43 public static final String TO = "to";
44 public static final String SPLINE_INTERPOLATION = "splineInterpolation";
45 public static final String DEGREE = "degree";
46 public static final String INVERSE = "inverse";
47 public static final String READ_BY_COLUMN = "readByColumn";
48 public static final String OFFSET="offset";
49 public static final String LOWER = "lower";
50 public static final String UPPER = "upper";
51
52 public String getParserName() {
53 return EmpiricalDistributionLikelihood.EMPIRICAL_DISTRIBUTION_LIKELIHOOD;
54 }
55
56 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
57
58 String fileName = xo.getStringAttribute(FILE_NAME);
59
60 boolean splineInterpolation = xo.getAttribute(SPLINE_INTERPOLATION,false);
61 int degree = xo.getAttribute(DEGREE,3); // Default is cubic-spline
62
63 boolean inverse = xo.getAttribute(INVERSE,false);
64
65 boolean byColumn = xo.getAttribute(READ_BY_COLUMN,true);
66
67 EmpiricalDistributionLikelihood likelihood;
68
69 if (splineInterpolation) {
70 if( degree < 1 )
71 throw new XMLParseException("Spline degree must be greater than zero!");
72 likelihood = new SplineInterpolatedLikelihood(fileName,degree,inverse,byColumn);
73 } else
74 //likelihood = new EmpiricalDistributionLikelihood(fileName,inverse,byColumn);
75 throw new XMLParseException("Only spline-interpolated empirical distributions are currently support");
76
77 XMLObject cxo1 = xo.getChild(DATA);
78 final int from = cxo1.getAttribute(FROM, -1);
79 int to = cxo1.getAttribute(TO, -1);
80 if (from >= 0 || to >= 0) {
81 if (to < 0) {
82 to = Integer.MAX_VALUE;
83 }
84 if (!(from >= 0 && to >= 0 && from < to)) {
85 throw new XMLParseException("ill formed from-to");
86 }
87 likelihood.setRange(from, to);
88 }
89
90 for (int j = 0; j < cxo1.getChildCount(); j++) {
91 if (cxo1.getChild(j) instanceof Statistic) {
92
93 likelihood.addData((Statistic) cxo1.getChild(j));
94 } else {
95 throw new XMLParseException("illegal element in " + cxo1.getName() + " element");
96 }
97 }
98
99 double offset = cxo1.getAttribute(OFFSET,0);
100 likelihood.setOffset(offset);
101
102 if (cxo1.hasAttribute(LOWER) || cxo1.hasAttribute(UPPER)) {
103 likelihood.setBounds(
104 cxo1.getAttribute(LOWER, Double.NEGATIVE_INFINITY),
105 cxo1.getAttribute(UPPER, Double.POSITIVE_INFINITY)
106 );
107 }
108
109 return likelihood;
110 }
111
112 //************************************************************************
113 // AbstractXMLObjectParser implementation
114 //************************************************************************
115
116 public XMLSyntaxRule[] getSyntaxRules() {
117 return rules;
118 }
119
120 private final XMLSyntaxRule[] rules = {
121 AttributeRule.newStringRule(FILE_NAME),
122 AttributeRule.newBooleanRule(SPLINE_INTERPOLATION,true),
123 AttributeRule.newIntegerRule(DEGREE,true),
124 AttributeRule.newBooleanRule(INVERSE,true),
125 AttributeRule.newBooleanRule(READ_BY_COLUMN,true),
126 new ElementRule(DATA, new XMLSyntaxRule[]{
127 AttributeRule.newIntegerRule(FROM, true),
128 AttributeRule.newIntegerRule(TO, true),
129 AttributeRule.newDoubleRule(OFFSET,true),
130 AttributeRule.newDoubleRule(LOWER, true),
131 AttributeRule.newDoubleRule(UPPER, true),
132 new ElementRule(Statistic.class, 1, Integer.MAX_VALUE)
133 })
134 };
135
136 public String getParserDescription() {
137 return "Calculates the likelihood of some data given some empirically-generated distribution.";
138 }
139
140 public Class getReturnType() {
141 return Likelihood.class;
142 }
143 }