Debian Codegraph @

src/dr/inferencexml/model/MaskedParameterParser.java

raw symbols SCIP
1 /*
2 * MaskedParameterParser.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.inferencexml.model;
27
28 import dr.inference.model.MaskedParameter;
29 import dr.inference.model.Parameter;
30 import dr.xml.*;
31
32 /**
33 * @author Marc A. Suchard
34 */
35 public class MaskedParameterParser extends AbstractXMLObjectParser {
36
37 public static final String MASKED_PARAMETER = "maskedParameter";
38 public static final String MASKING = "mask";
39 public static final String COMPLEMENT = "complement";
40 public static final String FROM = "from";
41 public static final String TO = "to";
42 public static final String EVERY = "every";
43 public static final String BUILD = "build";
44 private static final String SIGNAL_DEPENDENTS = "signalDependents";
45
46 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
47
48 Parameter parameter = (Parameter) xo.getChild(Parameter.class);
49 Parameter mask;
50
51 XMLObject cxo = xo.getChild(MASKING);
52 if (cxo != null) {
53 mask = (Parameter) cxo.getChild(Parameter.class);
54 }
55 else if(xo.getAttribute(BUILD, false)){
56 mask = new Parameter.Default(parameter.getDimension(), 0.0);
57 for (int i = 0; i < parameter.getDimension(); i++) {
58 if(parameter.getParameterValue(i) == 0){
59 mask.setParameterValue(i, 1.0);
60 }
61 }
62 }
63 else {
64 int from = xo.getAttribute(FROM, 1) - 1;
65 int to = xo.getAttribute(TO, parameter.getDimension()) - 1;
66 int every = xo.getAttribute(EVERY, 1);
67
68 if (from < 0) throw new XMLParseException("illegal 'from' attribute in " + MASKED_PARAMETER
69 + " element");
70 if (to < 0 || to < from) throw new XMLParseException("illegal 'to' attribute in "
71 + MASKED_PARAMETER + " element");
72 if (every <= 0) throw new XMLParseException("illegal 'every' attribute in " + MASKED_PARAMETER
73 + " element");
74
75 mask = new Parameter.Default(parameter.getDimension(), 0.0);
76 for (int i = from; i <= to; i += every) {
77 mask.setParameterValue(i, 1.0);
78 }
79 }
80
81 if (mask.getDimension() != parameter.getDimension())
82 throw new XMLParseException("dim(" + parameter.getId() + ":" + parameter.getDimension()
83 + ") != dim(" + mask.getId() + ":" + mask.getDimension() + ")");
84
85 boolean signal = xo.getAttribute(SIGNAL_DEPENDENTS, true);
86
87 MaskedParameter maskedParameter = new MaskedParameter(parameter,
88 signal ? MaskedParameter.Signaling.NORMAL : MaskedParameter.Signaling.NO_DEPENDENT);
89 boolean ones = !xo.getAttribute(COMPLEMENT, false);
90 maskedParameter.addMask(mask, ones);
91
92 return maskedParameter;
93 }
94
95 public XMLSyntaxRule[] getSyntaxRules() {
96 return rules;
97 }
98
99 private final XMLSyntaxRule[] rules = {
100 new ElementRule(Parameter.class),
101 new ElementRule(MASKING,
102 new XMLSyntaxRule[]{
103 new ElementRule(Parameter.class)
104 }, true),
105 AttributeRule.newBooleanRule(COMPLEMENT, true),
106 AttributeRule.newBooleanRule(BUILD, true),
107 AttributeRule.newIntegerRule(FROM, true),
108 AttributeRule.newIntegerRule(TO, true),
109 AttributeRule.newIntegerRule(EVERY, true),
110 AttributeRule.newBooleanRule(SIGNAL_DEPENDENTS, true),
111 };
112
113 public String getParserDescription() {
114 return "A masked parameter.";
115 }
116
117 public Class getReturnType() {
118 return Parameter.class;
119 }
120
121 public String getParserName() {
122 return MASKED_PARAMETER;
123 }
124 }