Debian Codegraph @

src/dr/inference/loggers/NumberColumn.java

raw symbols SCIP
1 /*
2 * NumberColumn.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 java.text.DecimalFormat;
29
30 /**
31 * An interface for a numerical column in a log.
32 *
33 * @version $Id: NumberColumn.java,v 1.4 2005/05/24 20:25:59 rambaut Exp $
34 *
35 * @author Andrew Rambaut
36 * @author Alexei Drummond
37 */
38
39 public abstract class NumberColumn extends LogColumn.Abstract {
40
41 private int sf = -1;
42 private int dp = -1;
43
44 private double upperCutoff;
45 private double[] cutoffTable;
46 private final DecimalFormat decimalFormat = new DecimalFormat();
47 private DecimalFormat scientificFormat = null;
48
49
50 public NumberColumn(String label) {
51 super(label);
52 decimalFormat.setGroupingUsed(false); // not use comma
53 }
54
55 public NumberColumn(String label, int sf) {
56 this(label);
57 setSignificantFigures(sf);
58 }
59
60 /**
61 * Set the number of significant figures to display when formatted.
62 * Setting this overrides the decimal places option.
63 */
64 public void setSignificantFigures(int sf) {
65 this.sf = sf;
66 this.dp = -1;
67
68 upperCutoff = Math.pow(10,sf-1);
69 cutoffTable = new double[sf];
70 long num = 10;
71 for (int i =0; i < cutoffTable.length; i++) {
72 cutoffTable[i] = (double)num;
73 num *= 10;
74 }
75 decimalFormat.setGroupingUsed(false);
76 decimalFormat.setMinimumIntegerDigits(1);
77 decimalFormat.setMaximumFractionDigits(sf-1);
78 decimalFormat.setMinimumFractionDigits(sf-1);
79 scientificFormat = new DecimalFormat(getPattern(sf));
80 }
81
82 /**
83 * Get the number of significant figures to display when formatted.
84 * Returns -1 if maximum s.f. are to be used.
85 */
86 public int getSignificantFigures() { return sf; }
87
88 /**
89 * Set the number of decimal places to display when formatted.
90 * Setting this overrides the significant figures option.
91 */
92 public void setDecimalPlaces(int dp) {
93 this.dp = dp;
94 this.sf = -1;
95 }
96
97 /**
98 * Get the number of decimal places to display when formatted.
99 * Returns -1 if maximum d.p. are to be used.
100 */
101 public int getDecimalPlaces() { return dp; }
102
103
104 public String formatValue(double value) {
105 if (dp < 0 && sf < 0) {
106 // return it at full precision
107 return Double.toString(value);
108 }
109
110 int numFractionDigits = 0;
111
112 if (dp < 0) {
113
114 double absValue = Math.abs(value);
115
116 if ((absValue > upperCutoff) || (absValue < 0.1)) {
117
118 return scientificFormat.format(value);
119
120 } else {
121
122 numFractionDigits = getNumFractionDigits(value);
123 }
124
125 } else {
126
127 numFractionDigits = dp;
128 }
129
130 decimalFormat.setMaximumFractionDigits(numFractionDigits);
131 decimalFormat.setMinimumFractionDigits(numFractionDigits);
132 return decimalFormat.format(value);
133 }
134
135 /**
136 * Returns a string containing the current value for this column with
137 * appropriate formatting.
138 *
139 * @return the formatted string.
140 */
141 protected String getFormattedValue() {
142 return formatValue(getDoubleValue());
143 }
144
145 private int getNumFractionDigits(double value) {
146 value = Math.abs(value);
147 for (int i = 0; i < cutoffTable.length; i++) {
148 if (value < cutoffTable[i]) return sf-i-1;
149 }
150 return sf - 1;
151 }
152
153 private String getPattern(int sf) {
154 String pattern = "0.";
155 for (int i =0; i < sf-1; i++) {
156 pattern += "#";
157 }
158 pattern += "E0";
159 return pattern;
160 }
161
162 /**
163 * Returns the current value as a double.
164 */
165 public abstract double getDoubleValue();
166
167 }