Debian Codegraph @

src/dr/inferencexml/trace/MarginalLikelihoodAnalysisParser.java

raw symbols SCIP
1 /*
2 * MarginalLikelihoodAnalysisParser.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 MarginalLikelihoodAnalysisParser extends AbstractXMLObjectParser {
42
43 public static final String ML_ANALYSIS = "marginalLikelihoodAnalysis";
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 DO_BOOTSTRAP = "bootstrap";
48 public static final String ONLY_HARMONIC = "harmonicOnly";
49 public static final String BOOTSTRAP_LENGTH = "bootstrapLength";
50
51 public String getParserName() {
52 return ML_ANALYSIS;
53 }
54
55 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
56
57 String fileName = xo.getStringAttribute(FILE_NAME);
58 try {
59
60 File file = new File(fileName);
61 String name = file.getName();
62 String parent = file.getParent();
63
64 if (!file.isAbsolute()) {
65 parent = System.getProperty("user.dir");
66 }
67
68 file = new File(parent, name);
69
70 fileName = file.getAbsolutePath();
71
72 XMLObject cxo = xo.getChild(COLUMN_NAME);
73 String likelihoodName = cxo.getStringAttribute(Attribute.NAME);
74
75 LogFileTraces traces = new LogFileTraces(fileName, file);
76 traces.loadTraces();
77 long maxState = traces.getMaxState();
78
79 // leaving the burnin attribute off will result in 10% being used
80 long burnin = xo.getAttribute(BURN_IN, maxState / 10);
81
82 if (burnin < 0 || burnin >= maxState) {
83 burnin = maxState / 10;
84 System.out.println("WARNING: Burn-in larger than total number of states - using to 10%");
85 }
86
87 traces.setBurnIn(burnin);
88
89 int traceIndex = -1;
90 for (int i = 0; i < traces.getTraceCount(); i++) {
91 String traceName = traces.getTraceName(i);
92 if (traceName.equals(likelihoodName)) {
93 traceIndex = i;
94 break;
95 }
96 }
97
98 if (traceIndex == -1) {
99 throw new XMLParseException("Column '" + likelihoodName + "' can not be found for " + getParserName() + " element.");
100 }
101
102 // Set analysisType
103 String analysisType = "smoothed";
104 boolean harmonicOnly = false;
105 if (xo.hasAttribute(ONLY_HARMONIC))
106 harmonicOnly = xo.getBooleanAttribute(ONLY_HARMONIC);
107 if (harmonicOnly)
108 analysisType = "harmonic";
109
110
111 int bootstrapLength = cxo.getAttribute(BOOTSTRAP_LENGTH, 1000);
112
113 List<Double> sample = (List)traces.getValues(traceIndex);
114
115 MarginalLikelihoodAnalysis analysis = new MarginalLikelihoodAnalysis(sample,
116 traces.getTraceName(traceIndex), (int)burnin, analysisType, bootstrapLength);
117
118 System.out.println(analysis.toString());
119
120 return analysis;
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.getMessage());
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 MarginalLikelihoodAnalysis.class;
141 }
142
143 public XMLSyntaxRule[] getSyntaxRules() {
144 return rules;
145 }
146
147 private final XMLSyntaxRule[] rules = {
148 new StringAttributeRule(FILE_NAME, "The traceName of a BEAST log file (can not include trees, which should be logged separately"),
149 AttributeRule.newIntegerRule("burnIn", true),
150 //, "The number of states (not sampled states, but actual states) that are discarded from the beginning of the trace before doing the analysis" ),
151 new ElementRule(COLUMN_NAME, new XMLSyntaxRule[] {
152 new StringAttributeRule(Attribute.NAME,"The column name")}),
153 };
154
155 }