| 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.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 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 210 |
values[i] = distribution.quantile(MathUtils.nextDouble()); |
| 211 |
} while (values[i] < lowers[i] || values[i] > uppers[i]); |
| 212 |
} |
| 213 |
|
| 214 |
} else { |
| 215 |
|
| 216 |
|
| 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 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
} |