Debian Codegraph @

src/dr/inference/model/ParameterParser.java

raw symbols SCIP
1 /*
2 * ParameterParser.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.model;
27
28 import java.io.File;
29 import java.io.IOException;
30 import java.util.List;
31
32 import dr.inference.trace.LogFileTraces;
33 import dr.inference.trace.TraceException;
34 import dr.math.MathUtils;
35 import dr.math.distributions.Distribution;
36 import dr.xml.*;
37
38 /**
39 * Parses a multi-dimensional continuous parameter.
40 *
41 * @author Alexei Drummond
42 * @author Andrew Rambaut
43 *
44 * @version $Id: ParameterParser.java,v 1.12 2005/05/24 20:26:00 rambaut Exp $
45 */
46 public class ParameterParser extends dr.xml.AbstractXMLObjectParser {
47
48 public static final String UPPER = "upper";
49 public static final String LOWER = "lower";
50 public static final String DIMENSION = "dimension";
51 public static final String VALUE = "value";
52 public static final String PARAMETER = "parameter";
53 public static final String RANDOMIZE = "randomize";
54 public static final String FILENAME = "fileName";
55 public static final String BURNIN = "burnin";
56 public static final String PARAMETERCOLUMN = "parameterColumn";
57
58 public String getParserName() {
59 return PARAMETER;
60 }
61
62 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
63
64 double[] values = null;
65 double[] uppers;
66 double[] lowers;
67
68 if( xo.hasAttribute(DIMENSION) ) {
69 values = new double[xo.getIntegerAttribute(DIMENSION)];
70 }
71
72 if ( xo.hasAttribute(FILENAME)) {
73 //read samples from a file (used for a reference prior) and calculate the mean
74 String fileName = xo.getStringAttribute(FILENAME);
75 File file = new File(fileName);
76 fileName = file.getName();
77 String parent = file.getParent();
78 if (!file.isAbsolute()) {
79 parent = System.getProperty("user.dir");
80 }
81 file = new File(parent, fileName);
82 fileName = file.getAbsolutePath();
83 String columnName = "";
84 if ( xo.hasAttribute(PARAMETERCOLUMN)) {
85 columnName = xo.getStringAttribute(PARAMETERCOLUMN);
86 } else {
87 throw new XMLParseException("when providing a file name you must provide a parameter column as well");
88 }
89 //if a number for the burnin is not given, use 0 ...
90 int burnin;
91 if ( xo.hasAttribute(BURNIN)) {
92 burnin = xo.getIntegerAttribute(BURNIN);
93 } else {
94 burnin = 0;
95 }
96 LogFileTraces traces = new LogFileTraces(fileName, file);
97 List parameterSamples = null;
98 try {
99 traces.loadTraces();
100 traces.setBurnIn(burnin);
101 int traceIndexParameter = -1;
102 for (int i = 0; i < traces.getTraceCount(); i++) {
103 String traceName = traces.getTraceName(i);
104 if (traceName.trim().equals(columnName)) {
105 traceIndexParameter = i;
106 }
107 }
108 if (traceIndexParameter == -1) {
109 throw new XMLParseException("Column '" + columnName + "' can not be found for " + getParserName() + " element.");
110 }
111 parameterSamples = traces.getValues(traceIndexParameter);
112 } catch (TraceException e) {
113 throw new XMLParseException(e.getMessage());
114 } catch (IOException ioe) {
115 throw new XMLParseException(ioe.getMessage());
116 }
117 values = new double[1];
118 for (int i = 0, stop = parameterSamples.size(); i < stop; i++) {
119 values[0] += ((Double)parameterSamples.get(i))/((double)stop);
120 }
121 System.out.println("Number of samples: " + parameterSamples.size());
122 System.out.println("Parameter mean: " + values[0]);
123 } else if( xo.hasAttribute(VALUE) ) {
124 if( values == null ) {
125 values = xo.getDoubleArrayAttribute(VALUE);
126 } else {
127 double[] v = xo.getDoubleArrayAttribute(VALUE);
128 if( v.length == values.length ) {
129 System.arraycopy(v, 0, values, 0, v.length);
130 } else if( v.length == 1 ) {
131 for(int i = 0; i < values.length; i++) {
132 values[i] = v[0];
133 }
134 } else {
135 throw new XMLParseException("value string must have 1 value or dimension values");
136 }
137 }
138 } else {
139 if( xo.hasAttribute(DIMENSION) ) {
140 values = new double[xo.getIntegerAttribute(DIMENSION)];
141 } else {
142 // parameter dimension will get set correctly by TreeModel presumably.
143 if (!xo.hasChildNamed(RANDOMIZE)) {
144 return new Parameter.Default(1);
145 }
146 values = new double[1];
147 values[0] = 1.0;
148 }
149 }
150
151 uppers = new double[values.length];
152 for(int i = 0; i < values.length; i++) {
153 uppers[i] = Double.POSITIVE_INFINITY;
154 }
155
156 lowers = new double[values.length];
157 for(int i = 0; i < values.length; i++) {
158 lowers[i] = Double.NEGATIVE_INFINITY;
159 }
160
161 if( xo.hasAttribute(UPPER) ) {
162 double[] v = xo.getDoubleArrayAttribute(UPPER);
163 if( v.length == uppers.length ) {
164 System.arraycopy(v, 0, uppers, 0, v.length);
165 } else if( v.length == 1 ) {
166 for(int i = 0; i < uppers.length; i++) {
167 uppers[i] = v[0];
168 }
169 } else {
170 throw new XMLParseException("uppers string must have 1 value or dimension values");
171 }
172 }
173
174 if( xo.hasAttribute(LOWER) ) {
175 double[] v = xo.getDoubleArrayAttribute(LOWER);
176 if( v.length == lowers.length ) {
177 System.arraycopy(v, 0, lowers, 0, v.length);
178 } else if( v.length == 1 ) {
179 for(int i = 0; i < lowers.length; i++) {
180 lowers[i] = v[0];
181 }
182 } else {
183 throw new XMLParseException("lowers string must have 1 value or dimension values");
184 }
185 }
186
187 // assert uppers != null && lowers != null;
188
189 if( (uppers.length != values.length) ) {
190 throw new XMLParseException("value and upper limit strings have different dimension, in parameter");
191 }
192
193 if( (lowers.length != values.length) ) {
194 throw new XMLParseException("value and lower limit strings have different dimension, in parameter");
195 }
196
197 // check if uppers and lowers are consistent
198 for(int i = 0; i < values.length; i++) {
199 if( uppers[i] < lowers[i] ) {
200 throw new XMLParseException("upper is lower than lower, in parameter");
201 }
202 }
203
204 if (xo.hasChildNamed(RANDOMIZE)) {
205
206 Distribution distribution = (Distribution) xo.getChild(RANDOMIZE).getChild(Distribution.class);
207 for (int i = 0; i < values.length; i++) {
208 do {
209 // Not an efficient way to draw random variables, but this is currently the only general interface
210 values[i] = distribution.quantile(MathUtils.nextDouble());
211 } while (values[i] < lowers[i] || values[i] > uppers[i]);
212 }
213
214 } else {
215
216 // make values consistent with bounds
217 for(int i = 0; i < values.length; i++) {
218 if( uppers[i] < values[i] ) values[i] = uppers[i];
219 }
220
221 for(int i = 0; i < values.length; i++) {
222 if (lowers[i] > values[i]) values[i] = lowers[i];
223 }
224 }
225
226 Parameter param = new Parameter.Default(values.length);
227 for(int i = 0; i < values.length; i++) {
228 param.setParameterValue(i, values[i]);
229 }
230 param.addBounds(new Parameter.DefaultBounds(uppers, lowers));
231 return param;
232 }
233
234 public XMLSyntaxRule[] getSyntaxRules() {
235 return rules;
236 }
237
238 private final XMLSyntaxRule[] rules = {
239 AttributeRule.newDoubleArrayRule(VALUE, true),
240 AttributeRule.newIntegerRule(DIMENSION, true),
241 AttributeRule.newStringRule(FILENAME, true),
242 AttributeRule.newStringRule(PARAMETERCOLUMN, true),
243 AttributeRule.newIntegerRule(BURNIN, true),
244 AttributeRule.newDoubleArrayRule(UPPER, true),
245 AttributeRule.newDoubleArrayRule(LOWER, true),
246 new ElementRule(RANDOMIZE, new XMLSyntaxRule[] {
247 new ElementRule(Distribution.class),
248 },true),
249 };
250
251
252 public String getParserDescription() {
253 return "A real-valued parameter of one or more dimensions.";
254 }
255
256 public Class getReturnType() {
257 return Parameter.class;
258 }
259
260 static public void replaceParameter(XMLObject xo, Parameter newParam) throws XMLParseException {
261
262 for (int i = 0; i < xo.getChildCount(); i++) {
263
264 if (xo.getChild(i) instanceof Parameter) {
265
266 XMLObject rxo;
267 Object obj = xo.getRawChild(i);
268
269 if (obj instanceof Reference ) {
270 rxo = ((Reference) obj).getReferenceObject();
271 } else if (obj instanceof XMLObject) {
272 rxo = (XMLObject) obj;
273 } else {
274 throw new XMLParseException("object reference not available");
275 }
276
277 if (rxo.getChildCount() > 0) {
278 throw new XMLParseException("No child elements allowed in parameter element.");
279 }
280
281 if (rxo.hasAttribute(XMLParser.IDREF)) {
282 throw new XMLParseException("References to " + xo.getName() + " parameters are not allowed in treeModel.");
283 }
284
285 if (rxo.hasAttribute(VALUE)) {
286 throw new XMLParseException("Parameters in " + xo.getName() + " have values set automatically.");
287 }
288
289 if (rxo.hasAttribute(UPPER)) {
290 throw new XMLParseException("Parameters in " + xo.getName() + " have bounds set automatically.");
291 }
292
293 if (rxo.hasAttribute(LOWER)) {
294 throw new XMLParseException("Parameters in " + xo.getName() + " have bounds set automatically.");
295 }
296
297 if (rxo.hasAttribute(XMLParser.ID)) {
298 newParam.setId(rxo.getStringAttribute(XMLParser.ID));
299 }
300
301 rxo.setNativeObject(newParam);
302
303 return;
304 }
305 }
306 }
307
308 // static public Parameter getParameter(XMLObject xo) throws XMLParseException {
309 //
310 // int paramCount = 0;
311 // Parameter param = null;
312 // for (int i = 0; i < xo.getChildCount(); i++) {
313 // if (xo.getChild(i) instanceof Parameter) {
314 // param = (Parameter) xo.getChild(i);
315 // paramCount += 1;
316 // }
317 // }
318 //
319 // if (paramCount == 0) {
320 // throw new XMLParseException("no parameter element in treeModel " + xo.getName() + " element");
321 // } else if (paramCount > 1) {
322 // throw new XMLParseException("More than one parameter element in treeModel " + xo.getName() + " element");
323 // }
324 //
325 // return param;
326 // }
327 }