Debian Codegraph @

src/dr/inferencexml/trace/HarmonicMeanAnalysisParser.java

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