Debian Codegraph @

src/dr/inference/loggers/MCLogger.java

raw symbols SCIP
1 /*
2 * MCLogger.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.inference.loggers;
27
28 import dr.util.Keywordable;
29
30 import java.io.FileWriter;
31 import java.io.IOException;
32 import java.io.PrintWriter;
33 import java.text.NumberFormat;
34 import java.util.ArrayList;
35 import java.util.HashSet;
36 import java.util.List;
37 import java.util.Set;
38
39 /**
40 * A class for a general purpose logger.
41 *
42 * @author Andrew Rambaut
43 * @author Alexei Drummond
44 * @version $Id: MCLogger.java,v 1.18 2005/05/24 20:25:59 rambaut Exp $
45 */
46 public class MCLogger implements Logger {
47
48 /**
49 * Output performance stats in this log
50 */
51 private final boolean performanceReport;
52 private final int performanceReportDelay;
53
54 /**
55 * Constructor. Will log every logEvery.
56 *
57 * @param formatter the formatter of this logger
58 * @param logEvery logging frequency
59 */
60 public MCLogger(LogFormatter formatter, long logEvery, boolean performanceReport, int performanceReportDelay) {
61
62 addFormatter(formatter);
63 this.logEvery = logEvery;
64 this.performanceReport = performanceReport;
65 this.performanceReportDelay = performanceReportDelay;
66 }
67
68 /**
69 * Constructor. Will log every logEvery.
70 *
71 * @param formatter the formatter of this logger
72 * @param logEvery logging frequency
73 */
74 public MCLogger(LogFormatter formatter, long logEvery, boolean performanceReport) {
75 this(formatter, logEvery, performanceReport, 0);
76 }
77
78 /**
79 * Constructor. Will log every logEvery.
80 *
81 * @param logEvery logging frequency
82 */
83 public MCLogger(String fileName, long logEvery, boolean performanceReport, int performanceReportDelay) throws IOException {
84 this(new TabDelimitedFormatter(new PrintWriter(new FileWriter(fileName))), logEvery, performanceReport, performanceReportDelay);
85 }
86
87 /**
88 * Additional constructor that does not add a provided Formatter
89 * @param performanceReport
90 * @param performanceReportDelay
91 */
92 public MCLogger(boolean performanceReport, int performanceReportDelay) {
93 this.performanceReport = performanceReport;
94 this.performanceReportDelay = performanceReportDelay;
95 }
96
97 /**
98 * Constructor. Will log every logEvery.
99 *
100 * @param logEvery logging frequency
101 */
102 public MCLogger(long logEvery) {
103 this(new TabDelimitedFormatter(System.out), logEvery, true, 0);
104 }
105
106 public final void setTitle(String title) {
107 this.title = title;
108 }
109
110 public final String getTitle() {
111 return title;
112 }
113
114 public long getLogEvery() {
115 return logEvery;
116 }
117
118 public void setLogEvery(long logEvery) {
119 this.logEvery = logEvery;
120 }
121
122 public final void addFormatter(LogFormatter formatter) {
123
124 formatters.add(formatter);
125 }
126
127 public final void add(Loggable loggable) {
128
129 LogColumn[] columns = loggable.getColumns();
130
131 for (LogColumn column : columns) {
132 addColumn(column);
133 }
134 }
135
136 public final void addColumn(LogColumn column) {
137
138 if (column instanceof Keywordable) {
139 keywords.addAll(((Keywordable)column).getKeywords());
140 }
141
142 columns.add(column);
143 }
144
145 public final void addColumns(LogColumn[] columns) {
146
147 for (LogColumn column : columns) {
148 addColumn(column);
149 }
150 }
151
152 public final int getColumnCount() {
153 return columns.size();
154 }
155
156 public final LogColumn getColumn(int index) {
157
158 return columns.get(index);
159 }
160
161 public final String getColumnLabel(int index) {
162
163 return columns.get(index).getLabel();
164 }
165
166 public final String getColumnFormatted(int index) {
167
168 return columns.get(index).getFormatted();
169 }
170
171 protected void logHeading(String heading) {
172 for (LogFormatter formatter : formatters) {
173 formatter.logHeading(heading);
174 }
175 }
176
177 protected void logLine(String line) {
178 for (LogFormatter formatter : formatters) {
179 formatter.logLine(line);
180 }
181 }
182
183 protected void logLabels(String[] labels) {
184 for (LogFormatter formatter : formatters) {
185 formatter.logLabels(labels);
186 }
187 }
188
189 protected void logValues(String[] values) {
190 for (LogFormatter formatter : formatters) {
191 formatter.logValues(values);
192 }
193 }
194
195 public void startLogging() {
196
197 for (LogFormatter formatter : formatters) {
198 formatter.startLogging(title);
199 }
200
201 if (title != null) {
202 logHeading(title);
203 }
204 if (keywords.size() > 0) {
205 StringBuffer sb = new StringBuffer("keywords:");
206 for (String keyword: keywords) {
207 sb.append(" ");
208 sb.append(keyword);
209 }
210 logHeading(sb.toString());
211 }
212
213 if (logEvery > 0) {
214 final int columnCount = getColumnCount();
215 String[] labels = new String[columnCount + 1];
216
217 labels[0] = "state";
218
219 for (int i = 0; i < columnCount; i++) {
220 labels[i + 1] = getColumnLabel(i);
221 }
222
223 logLabels(labels);
224 }
225 }
226
227 public final void log(int state) {
228 // just to prevent overriding of the old 32 bit signature
229 }
230
231 @Override
232 public void log(long state) {
233
234 if (performanceReport && !performanceReportStarted && state >= performanceReportDelay) {
235 startTime = System.currentTimeMillis();
236 startState = state;
237 formatter.setMaximumFractionDigits(2);
238 }
239
240 if (logEvery > 0 && (state % logEvery == 0)) {
241
242 final int columnCount = getColumnCount();
243
244 String[] values = new String[columnCount + (performanceReport ? 2 : 1)];
245
246 values[0] = Long.toString(state);
247
248 for (int i = 0; i < columnCount; i++) {
249 values[i + 1] = getColumnFormatted(i);
250 }
251
252 if (performanceReport) {
253 if (performanceReportStarted) {
254
255 long time = System.currentTimeMillis();
256
257 double hoursPerMillionStates = (double) (time - startTime) / (3.6 * (double) (state - startState));
258
259 String timePerMillion = formatter.format(hoursPerMillionStates);
260 String units = " hours/million states";
261 if (hoursPerMillionStates < 0.1) {
262 double minutesPerMillionStates = hoursPerMillionStates * 60;
263 timePerMillion = formatter.format(minutesPerMillionStates);
264 units = " minutes/million states";
265 if (minutesPerMillionStates < 0.1) {
266 double secondsPerMillionStates = minutesPerMillionStates * 60;
267 timePerMillion = formatter.format(secondsPerMillionStates);
268 units = " seconds/million states";
269 }
270 }
271 values[columnCount + 1] = timePerMillion + units;
272
273 } else {
274 values[columnCount + 1] = "-";
275 }
276 }
277
278 logValues(values);
279 }
280
281 if (performanceReport && !performanceReportStarted && state >= performanceReportDelay) {
282 performanceReportStarted = true;
283 }
284
285 }
286
287 public void stopLogging() {
288
289 for (LogFormatter formatter : formatters) {
290 formatter.stopLogging();
291 }
292 }
293
294 private String title = null;
295
296 private Set<String> keywords = new HashSet<String>();
297
298 private List<LogColumn> columns = new ArrayList<LogColumn>();
299
300 protected long logEvery = 0;
301
302 public List<LogFormatter> getFormatters() {
303 return formatters;
304 }
305
306 public void setFormatters(List<LogFormatter> formatters) {
307 this.formatters = formatters;
308 }
309
310 protected List<LogFormatter> formatters = new ArrayList<LogFormatter>();
311
312 private boolean performanceReportStarted = false;
313 private long startTime;
314 private long startState;
315
316 private final NumberFormat formatter = NumberFormat.getNumberInstance();
317
318 }