Debian Codegraph @

src/dr/inferencexml/distribution/CompoundGaussianProcessParser.java

raw symbols SCIP
1 /*
2 * CompoundGaussianProcessParser.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.AbstractDistributionLikelihood;
29 import dr.inference.distribution.CachedDistributionLikelihood;
30 import dr.inference.distribution.DistributionLikelihood;
31 import dr.inference.distribution.MultivariateDistributionLikelihood;
32 import dr.inference.model.Likelihood;
33 import dr.inference.model.Variable;
34 import dr.math.distributions.CompoundGaussianProcess;
35 import dr.math.distributions.GaussianProcessRandomGenerator;
36 import dr.util.Attribute;
37 import dr.xml.*;
38
39 import java.util.ArrayList;
40 import java.util.List;
41 import java.util.logging.Logger;
42
43 /**
44 * @author Marc Suchard
45 */
46 public class CompoundGaussianProcessParser extends AbstractXMLObjectParser {
47
48 public static final String NAME = "compoundGaussianProcess";
49
50 public String getParserName() {
51 return NAME;
52 }
53
54 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
55
56 List<GaussianProcessRandomGenerator> gpList = new ArrayList<GaussianProcessRandomGenerator>();
57 List<Likelihood> likelihoodList = new ArrayList<Likelihood>();
58 List<Integer> copyList = new ArrayList<Integer>();
59
60 for (int i = 0; i < xo.getChildCount(); ++i) {
61 Object obj = xo.getChild(i);
62 GaussianProcessRandomGenerator gp = null;
63 Likelihood likelihood = null;
64 int copies = -1;
65 if (obj instanceof DistributionLikelihood) {
66 DistributionLikelihood dl = (DistributionLikelihood) obj;
67 if (!(dl.getDistribution() instanceof GaussianProcessRandomGenerator)) {
68 throw new XMLParseException("Not a Gaussian process");
69 }
70 likelihood = dl;
71 gp = (GaussianProcessRandomGenerator) dl.getDistribution();
72 copies = 0;
73 for (Attribute<double[]> datum : dl.getDataList()) {
74 // Double draw = (Double) gp.nextRandom();
75 // System.err.println("DL: " + datum.getAttributeName() + " " + datum.getAttributeValue().length + " " + "1");
76 copies += datum.getAttributeValue().length;
77 }
78 } else if (obj instanceof MultivariateDistributionLikelihood) {
79 MultivariateDistributionLikelihood mdl = (MultivariateDistributionLikelihood) obj;
80 if (!(mdl.getDistribution() instanceof GaussianProcessRandomGenerator)) {
81 throw new XMLParseException("Not a Gaussian process");
82 }
83 likelihood = mdl;
84 gp = (GaussianProcessRandomGenerator) mdl.getDistribution();
85 copies = 0;
86 double[] draw = (double[]) gp.nextRandom();
87 for (Attribute<double[]> datum : mdl.getDataList()) {
88 // System.err.println("ML: " + datum.getAttributeName() + " " + datum.getAttributeValue().length + " " + draw.length);
89 copies += datum.getAttributeValue().length / draw.length;
90 }
91 } else if (obj instanceof GaussianProcessRandomGenerator) {
92 gp = (GaussianProcessRandomGenerator) obj;
93 likelihood = gp.getLikelihood();
94 copies = 1;
95 } else {
96 throw new XMLParseException("Not a Gaussian process");
97 }
98 gpList.add(gp);
99 likelihoodList.add(likelihood);
100 copyList.add(copies);
101 }
102
103 // System.exit(-1);
104 return new CompoundGaussianProcess(gpList, likelihoodList, copyList);
105 }
106
107 //************************************************************************
108 // AbstractXMLObjectParser implementation
109 //************************************************************************
110
111 public XMLSyntaxRule[] getSyntaxRules() {
112 return rules;
113 }
114
115 private final XMLSyntaxRule[] rules = {
116 new ElementRule(GaussianProcessRandomGenerator.class, 1, Integer.MAX_VALUE),
117 };
118
119 public String getParserDescription() {
120 return "Returned a Gaussian process formed from an ordered list of independent Gaussian processes";
121 }
122
123 public Class getReturnType() {
124 return GaussianProcessRandomGenerator.class;
125 }
126 }