Debian Codegraph @

src/dr/app/tools/LogAnalyser.java

raw symbols SCIP
1 /*
2 * LogAnalyser.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.app.tools;
27
28 import dr.app.beast.BeastVersion;
29 import dr.app.util.Arguments;
30 import dr.app.util.Utils;
31 import dr.inference.trace.TraceAnalysis;
32 import dr.inference.trace.TraceException;
33 import dr.util.Version;
34
35 import java.io.File;
36 import java.io.FileOutputStream;
37 import java.io.IOException;
38 import java.io.PrintStream;
39 import java.util.Locale;
40
41 public class LogAnalyser {
42
43 private final static Version version = new BeastVersion();
44
45
46 public LogAnalyser(int burnin, String inputFileName, String outputFileName, boolean verbose,
47 boolean hpds, boolean ess, boolean stdErr,
48 String marginalLikelihood) throws java.io.IOException, TraceException {
49
50 File parentFile = new File(inputFileName);
51
52 if (parentFile.isDirectory()) {
53 System.out.println("Analysing all log files below directory: " + inputFileName);
54 } else if (parentFile.isFile()) {
55 System.out.println("Analysing log file: " + inputFileName);
56 } else {
57 System.err.println(inputFileName + " does not exist!");
58 System.exit(0);
59 }
60
61 if (outputFileName != null) {
62 FileOutputStream outputStream = new FileOutputStream(outputFileName);
63 System.setOut(new PrintStream(outputStream));
64 }
65
66 analyze(parentFile, burnin, verbose, new boolean[]{true}, hpds, ess, stdErr, marginalLikelihood);
67 }
68
69 public LogAnalyser(int burnin, File[] files, String outputFileName, boolean verbose,
70 boolean hpds, boolean ess, boolean stdErr,
71 String marginalLikelihood) throws java.io.IOException, TraceException {
72
73 for (File f : files) {
74 if (f.isFile()) {
75 System.out.println("Analysing log file: " + f.getAbsoluteFile());
76 } else {
77 System.err.println(f.getAbsoluteFile() + " does not exist!");
78 System.exit(0);
79 }
80
81 if (outputFileName != null) {
82 FileOutputStream outputStream = new FileOutputStream(outputFileName);
83 System.setOut(new PrintStream(outputStream));
84 }
85
86 // setDefaultDir(f);
87 analyze(f, burnin, verbose, new boolean[]{true}, hpds, ess, stdErr, marginalLikelihood);
88 }
89 }
90 //
91 private static File openDefaultDirectory = null;
92 //
93 // private void setDefaultDir(File file) {
94 // final String s = file.getAbsolutePath();
95 // String p = s.substring(0, s.length() - file.getName().length());
96 // openDefaultDirectory = new File(p);
97 // if (!openDefaultDirectory.isDirectory()) {
98 // openDefaultDirectory = null;
99 // }
100 // }
101
102 /**
103 * Recursively analyzes log files.
104 *
105 * @param file the file to analyze (if this is a directory then the files within it are analyzed)
106 * @param burnin the burnin to use
107 * @param verbose if true then a full report is done on each log file, otherwise only a single line report is made
108 * @param drawHeader if boolean value in the zeroth position of this array is true then a head is drawn for the short reports.
109 * @param hpds if true then report 95% hpds
110 * @param stdErr if true then report standard error of the mean
111 * @throws dr.inference.trace.TraceException
112 * if the trace file is in the wrong format or corrupted
113 */
114 private void analyze(File file, int burnin, boolean verbose, boolean[] drawHeader,
115 boolean hpds, boolean ess, boolean stdErr,
116 String marginalLikelihood) throws TraceException {
117
118 if (file.isFile()) {
119 try {
120
121 String name = file.getCanonicalPath();
122 if (verbose) {
123 TraceAnalysis.report(name, burnin, marginalLikelihood);
124 } else {
125 TraceAnalysis.shortReport(name, burnin, drawHeader[0], hpds, ess, stdErr, marginalLikelihood);
126 drawHeader[0] = false;
127 }
128 } catch (IOException e) {
129 //e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
130 }
131 } else {
132 File[] files = file.listFiles();
133 for (File f : files) {
134 if (f.isDirectory()) {
135 analyze(f, burnin, verbose, drawHeader, hpds, ess, stdErr, marginalLikelihood);
136 } else if (f.getName().endsWith(".log") || f.getName().endsWith(".p")) {
137 analyze(f, burnin, verbose, drawHeader, hpds, ess, stdErr, marginalLikelihood);
138 } else {
139 if (verbose) System.out.println("Ignoring file: " + f);
140 }
141 }
142 }
143 }
144
145 public static void printTitle() {
146 System.out.println();
147 centreLine("LogAnalyser " + version.getVersionString() + ", " + version.getDateString(), 60);
148 centreLine("MCMC Output analysis", 60);
149 centreLine("by", 60);
150 centreLine("Andrew Rambaut and Alexei J. Drummond", 60);
151 System.out.println();
152 centreLine("Institute of Evolutionary Biology", 60);
153 centreLine("University of Edinburgh", 60);
154 centreLine("[email protected]", 60);
155 System.out.println();
156 centreLine("Department of Computer Science", 60);
157 centreLine("University of Auckland", 60);
158 centreLine("[email protected]", 60);
159 System.out.println();
160 System.out.println();
161 }
162
163 public static void centreLine(String line, int pageWidth) {
164 int n = pageWidth - line.length();
165 int n1 = n / 2;
166 for (int i = 0; i < n1; i++) {
167 System.out.print(" ");
168 }
169 System.out.println(line);
170 }
171
172
173 public static void printUsage(Arguments arguments) {
174
175 arguments.printUsage("loganalyser", "[-burnin <burnin>] [-short][-hpd] [-std] [<input-file-name> [<output-file-name>]]");
176 System.out.println();
177 System.out.println(" Example: loganalyser test.log");
178 System.out.println(" Example: loganalyser -burnin 10000 trees.log out.txt");
179 System.out.println();
180
181 }
182
183 //Main method
184 public static void main(String[] args) throws java.io.IOException, TraceException {
185
186 // There is a major issue with languages that use the comma as a decimal separator.
187 // To ensure compatibility between programs in the package, enforce the US locale.
188 Locale.setDefault(Locale.US);
189
190 printTitle();
191
192 Arguments arguments = new Arguments(
193 new Arguments.Option[]{
194 new Arguments.IntegerOption("burnin", "the number of states to be considered as 'burn-in'"),
195 new Arguments.Option("short", "use this option to produce a short report"),
196 new Arguments.Option("hpd", "use this option to produce hpds for each trace"),
197 new Arguments.Option("ess", "use this option to produce ESSs for each trace"),
198 new Arguments.Option("stdErr", "use this option to produce standard Error"),
199 new Arguments.StringOption("marginal", "trace_name", "specify the trace to use to calculate the marginal likelihood"),
200 // new Arguments.Option("html", "format output as html"),
201 // new Arguments.Option("svg", "generate svg graphics"),
202 new Arguments.Option("help", "option to print this message")
203 });
204
205 try {
206 arguments.parseArguments(args);
207 } catch (Arguments.ArgumentException ae) {
208 System.out.println(ae);
209 printUsage(arguments);
210 System.exit(1);
211 }
212
213 if (arguments.hasOption("help")) {
214 printUsage(arguments);
215 System.exit(0);
216 }
217
218 int burnin = -1;
219 if (arguments.hasOption("burnin")) {
220 burnin = arguments.getIntegerOption("burnin");
221 }
222
223 boolean hpds = arguments.hasOption("hpd");
224 boolean ess = arguments.hasOption("ess");
225 boolean stdErr = arguments.hasOption("stdErr");
226 boolean shortReport = arguments.hasOption("short");
227
228 String marginalLikelihood = null;
229 if (arguments.hasOption("marginal")) {
230 marginalLikelihood = arguments.getStringOption("marginal");
231 }
232
233 String inputFileName = null;
234 String outputFileName = null;
235
236 String[] args2 = arguments.getLeftoverArguments();
237
238 if (args2.length > 2) {
239 System.err.println("Unknown option: " + args2[2]);
240 System.err.println();
241 printUsage(arguments);
242 System.exit(1);
243 }
244
245 if (args2.length > 0) {
246 inputFileName = args2[0];
247 }
248 if (args2.length > 1) {
249 outputFileName = args2[1];
250 }
251
252 if (inputFileName == null) {
253 // No input file name was given so throw up a dialog box...
254 // inputFileName = Utils.getLoadFileName("LogAnalyser " + version.getVersionString() + " - Select log file to analyse");
255 File[] files = Utils.getLoadFiles("LogAnalyser " + version.getVersionString() + " - Select log file to analyse",
256 openDefaultDirectory, "BEAST log (*.log) Files", "log", "txt");
257 new LogAnalyser(burnin, files, outputFileName, !shortReport, hpds, ess, stdErr, marginalLikelihood);
258
259 } else {
260
261 new LogAnalyser(burnin, inputFileName, outputFileName, !shortReport, hpds, ess, stdErr, marginalLikelihood);
262 }
263
264 System.exit(0);
265 }
266 }
267