Debian Codegraph @

src/dr/inferencexml/trace/TraceAnalysisParser.java

raw symbols SCIP
1 /*
2 * TraceAnalysisParser.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 dr.inference.trace.*;
29 import dr.util.Attribute;
30 import dr.util.NumberFormatter;
31 import dr.xml.*;
32
33 import java.io.File;
34 import java.io.FileNotFoundException;
35 import java.util.List;
36
37 /**
38 * @author Alexei Drummond
39 * @version $Id: TraceAnalysisParser.java,v 1.18 2005/05/24 20:26:00 rambaut Exp $
40 */
41 public class TraceAnalysisParser extends AbstractXMLObjectParser {
42
43 public static final String TRACE_ANALYSIS = "traceAnalysis";
44 public static final String FILE_NAME = "fileName";
45 public static final String BURN_IN = "burnIn";
46 public static final String STD_ERROR = "stdError";
47 public static final String EXPECTATION = "expectation";
48 public static final String COMPUTE_MSE = "computeMSE";
49
50 public String getParserName() {
51 return TRACE_ANALYSIS;
52 }
53
54 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
55
56 String fileName = xo.getStringAttribute(FILE_NAME);
57 boolean withStdError = xo.getAttribute(STD_ERROR, false);
58 boolean computeMSE = xo.getAttribute(COMPUTE_MSE, false);
59 try {
60
61 File file = new File(fileName);
62 String name = file.getName();
63 String parent = file.getParent();
64
65 if (!file.isAbsolute()) {
66 parent = System.getProperty("user.dir");
67 }
68
69 file = new File(parent + File.separator, name);
70 if (file.exists()) {
71 fileName = file.getName();
72
73 // leaving the burnin attribute off will result in 10% being used
74 int burnin = xo.getAttribute(BURN_IN, -1);
75
76 TraceList traces = TraceAnalysis.report(fileName, burnin, null, withStdError);
77 for (int x = 0; x < xo.getChildCount(); x++) {
78 XMLObject child = (XMLObject) xo.getChild(x);
79 String statName = child.getStringAttribute(Attribute.NAME);
80 double expectation = child.getDoubleAttribute(Attribute.VALUE);
81 NumberFormatter formatter = new NumberFormatter(6);
82 formatter.setPadding(true);
83 formatter.setFieldWidth(14);
84
85 for (int i = 0; i < traces.getTraceCount(); i++) {
86 TraceDistribution distribution = traces.getDistributionStatistics(i);
87 TraceCorrelation corr = traces.getCorrelationStatistics(i);
88 if (traces.getTraceName(i).equals(statName)) {
89 double estimate = distribution.getMean();
90 double error = corr.getStdErrorOfMean();
91
92 System.out.print("E[" + statName + "] = " + formatter.format(expectation));
93
94 if (computeMSE) {
95 List values = traces.getValues(i);
96 double[] dv = new double[values.size()];
97 for (int j = 0; j < dv.length; j++) {
98 dv[j] = ((Number) values.get(j)).doubleValue();
99 }
100 double MSE = distribution.getMeanSquaredError(dv, expectation);
101 System.out.println(" MSE = " + formatter.format(MSE));
102 } else {
103 System.out.println("");
104
105
106 if (expectation > (estimate - (2 * error)) && expectation < (estimate + (2 * error))) {
107 System.out.println("OK: " + formatter.format(estimate) + " +- " + formatter.format(error) + "\n");
108 } else {
109 System.out.print("WARNING: " + formatter.format(estimate) + " +- " + formatter.format(error) + "\n");
110 }
111 }
112 }
113 }
114 }
115
116 System.out.println();
117 System.out.flush();
118 return traces;
119 } else {
120 throw new XMLParseException("Log file, " + parent + File.separator + name + " does not exist.");
121 }
122 } catch (FileNotFoundException fnfe) {
123 throw new XMLParseException("File '" + fileName + "' can not be opened for " + getParserName() + " element.");
124 } catch (java.io.IOException ioe) {
125 throw new XMLParseException(ioe.getMessage());
126 } catch (TraceException e) {
127 throw new XMLParseException(e.toString());
128 }
129 }
130
131 //************************************************************************
132 // AbstractXMLObjectParser implementation
133 //************************************************************************
134
135 public String getParserDescription() {
136 return "Performs a trace analysis. Estimates the mean of the various statistics in the given log file.";
137 }
138
139 public Class getReturnType() {
140 return TraceAnalysis[].class;
141 }
142
143 public XMLSyntaxRule[] getSyntaxRules() {
144 return rules;
145 }
146
147 private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{
148 new StringAttributeRule(FILE_NAME, "The name of a BEAST log file (can not include trees, which should be logged separately"),
149 AttributeRule.newIntegerRule(BURN_IN, true),
150 new ElementRule(EXPECTATION, new XMLSyntaxRule[]{AttributeRule.newStringRule("name"), AttributeRule.newStringRule("value")}, 0, Integer.MAX_VALUE),
151 AttributeRule.newBooleanRule(STD_ERROR, true),
152 AttributeRule.newBooleanRule(COMPUTE_MSE, true),
153 };
154 }