Debian Codegraph @

src/dr/inferencexml/trace/AICMAnalysisParser.java

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