Debian Codegraph @

src/dr/inferencexml/trace/ArithmeticMeanAnalysisParser.java

raw symbols SCIP
1 /*
2 * ArithmeticMeanAnalysisParser.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.trace;
27
28 import java.io.File;
29 import java.io.FileNotFoundException;
30 import java.util.List;
31
32 import dr.inference.trace.LogFileTraces;
33 import dr.inference.trace.MarginalLikelihoodAnalysis;
34 import dr.inference.trace.TraceException;
35 import dr.util.Attribute;
36 import dr.xml.AbstractXMLObjectParser;
37 import dr.xml.AttributeRule;
38 import dr.xml.ElementRule;
39 import dr.xml.StringAttributeRule;
40 import dr.xml.XMLObject;
41 import dr.xml.XMLParseException;
42 import dr.xml.XMLSyntaxRule;
43
44 /**
45 *
46 * @author Guy Baele
47 *
48 */
49 public class ArithmeticMeanAnalysisParser extends AbstractXMLObjectParser {
50
51 public static final String ARITHMETIC_MEAN_ANALYSIS = "arithmeticMeanAnalysis";
52 public static final String FILE_NAME = "fileName";
53 public static final String BURN_IN = "burnIn";
54 public static final String COLUMN_NAME = "likelihoodColumn";
55 public static final String BOOTSTRAP_LENGTH = "bootstrapLength";
56
57 public String getParserName() {
58 return ARITHMETIC_MEAN_ANALYSIS;
59 }
60
61 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
62
63 String fileName = xo.getStringAttribute(FILE_NAME);
64 try {
65
66 // Open file
67 File file = new File(fileName);
68 String name = file.getName();
69 String parent = file.getParent();
70 if (!file.isAbsolute()) {
71 parent = System.getProperty("user.dir");
72 }
73 file = new File(parent, name);
74 fileName = file.getAbsolutePath();
75
76 // Set analysisType
77 String analysisType = "arithmetic";
78
79 // Set bootstrapLength
80 int bootstrapLength = xo.getAttribute(BOOTSTRAP_LENGTH, 1000);
81
82 // Load traces and remove burnin
83 LogFileTraces traces = new LogFileTraces(fileName, file);
84 traces.loadTraces();
85 long maxState = traces.getMaxState();
86
87 long burnin = xo.getAttribute(BURN_IN, maxState / 10);
88
89 if (burnin < 0 || burnin >= maxState) {
90 burnin = maxState / 10;
91 System.out.println("WARNING: Burn-in larger than total number of states - using to 10%");
92 }
93
94 traces.setBurnIn(burnin);
95
96 // Find likelihood column
97 XMLObject cxo = xo.getChild(COLUMN_NAME);
98 String likelihoodName = cxo.getStringAttribute(Attribute.NAME);
99 int traceIndex = -1;
100
101 for (int i = 0; i < traces.getTraceCount(); i++) {
102 String traceName = traces.getTraceName(i);
103 if (traceName.equals(likelihoodName)) {
104 traceIndex = i;
105 break;
106 }
107 }
108
109 if (traceIndex == -1) {
110 throw new XMLParseException("Column '" + likelihoodName + "' can not be found for " + getParserName() + " element.");
111 }
112
113 // Get samples and perform analysis
114 List<Double> sample = (List)traces.getValues(traceIndex);
115
116 MarginalLikelihoodAnalysis analysis = new MarginalLikelihoodAnalysis(sample,
117 traces.getTraceName(traceIndex), (int)burnin, analysisType, bootstrapLength);
118
119 System.out.println(analysis.toString());
120
121 return analysis;
122
123 } catch (FileNotFoundException fnfe) {
124 throw new XMLParseException("File '" + fileName + "' can not be opened for " + getParserName() + " element.");
125 } catch (java.io.IOException ioe) {
126 throw new XMLParseException(ioe.getMessage());
127 } catch (TraceException e) {
128 throw new XMLParseException(e.getMessage());
129 }
130
131 }
132
133 //************************************************************************
134 // AbstractXMLObjectParser implementation
135 //************************************************************************
136
137 public String getParserDescription() {
138 return "Performs a trace analysis. Estimates the mean of the various statistics in the given log file.";
139 }
140
141 public Class getReturnType() {
142 return MarginalLikelihoodAnalysis.class;
143 }
144
145 public XMLSyntaxRule[] getSyntaxRules() {
146 return rules;
147 }
148
149 private final XMLSyntaxRule[] rules = {
150 new StringAttributeRule(FILE_NAME, "The traceName of a BEAST log file (can not include trees, which should be logged separately"),
151 AttributeRule.newIntegerRule(BURN_IN, true),
152 AttributeRule.newIntegerRule(BOOTSTRAP_LENGTH, true),
153 //, "The number of states (not sampled states, but actual states) that are discarded from the beginning of the trace before doing the analysis" ),
154 new ElementRule(COLUMN_NAME, new XMLSyntaxRule[] {
155 new StringAttributeRule(Attribute.NAME,"The column name")}),
156 };
157
158 }