Debian Codegraph @

src/dr/inferencexml/model/MaskedMatrixParameterParser.java

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