| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 77 |
String analysisType = "arithmetic"; |
| 78 |
|
| 79 |
|
| 80 |
int bootstrapLength = xo.getAttribute(BOOTSTRAP_LENGTH, 1000); |
| 81 |
|
| 82 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 154 |
new ElementRule(COLUMN_NAME, new XMLSyntaxRule[] { |
| 155 |
new StringAttributeRule(Attribute.NAME,"The column name")}), |
| 156 |
}; |
| 157 |
|
| 158 |
} |