Debian Codegraph @

src/dr/inference/model/VectorSliceParameter.java

raw symbols SCIP
1 /*
2 * VectorSliceParameter.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 dr.xml.*;
29
30 /**
31 * @author Marc Suchard
32 */
33 public class VectorSliceParameter extends CompoundParameter {
34
35 public final static String VECTOR_SLICE_PARAMETER = "vectorSlice";
36 public static final String SLICE_DIMENSION = "sliceDimension";
37
38 private final int sliceDimension;
39 private Bounds<Double> bounds;
40
41 public Bounds<Double> getBounds() {
42
43 if (bounds == null) {
44 bounds = new sliceBounds();
45 }
46 return bounds;
47 }
48
49 //TODO test add bounds function
50
51 public VectorSliceParameter(String name, int sliceDimension) {
52 super(name);
53 this.sliceDimension = sliceDimension;
54 }
55
56 public int getDimension() {
57 return getParameterCount();
58 }
59
60 public double getParameterValue(int dim) {
61 Parameter parameter = getParameter(dim);
62 return parameter.getParameterValue(sliceDimension);
63 }
64
65 public void addDimension(int index, double value) {
66 throw new RuntimeException("Not yet implemented.");
67 }
68
69 public double removeDimension(int index) {
70 throw new RuntimeException("Not yet implemented.");
71 }
72
73 public void setParameterValue(int dim, double value) {
74 Parameter parameter = getParameter(dim);
75 parameter.setParameterValue(sliceDimension, value);
76 }
77
78 public void setParameterValueQuietly(int dim, double value) {
79 Parameter parameter = getParameter(dim);
80 parameter.setParameterValueQuietly(sliceDimension, value);
81 }
82
83 public void setParameterValueNotifyChangedAll(int dim, double value){
84 Parameter parameter = getParameter(dim);
85 parameter.setParameterValueNotifyChangedAll(sliceDimension, value);
86 }
87
88 public String getDimensionName(int dim) {
89
90 return getParameter(dim).getVariableName() + Integer.toString(sliceDimension + 1);
91 }
92
93 private class sliceBounds implements Bounds<Double>{
94
95
96 @Override
97 public Double getUpperLimit(int dimension) {
98 return getParameter(dimension).getBounds().getUpperLimit(sliceDimension);
99 }
100
101 @Override
102 public Double getLowerLimit(int dimension) {
103 return getParameter(dimension).getBounds().getLowerLimit(sliceDimension);
104 }
105
106 @Override
107 public int getBoundsDimension() {
108 return getDimension();
109 }
110 }
111
112 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
113
114 public String getParserName() {
115 return VECTOR_SLICE_PARAMETER;
116 }
117
118 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
119
120
121 int sliceDimension = xo.getIntegerAttribute(SLICE_DIMENSION);
122 VectorSliceParameter vectorSlice = new VectorSliceParameter(xo.getId(), sliceDimension - 1);
123
124 for (int i = 0; i < xo.getChildCount(); i++) {
125 Parameter parameter = (Parameter) xo.getChild(i);
126 if (parameter instanceof MatrixParameter) {
127 MatrixParameter mp = (MatrixParameter) parameter;
128 for (int j = 0; j < mp.getParameterCount(); ++j) {
129 checkAndAdd(vectorSlice, mp.getParameter(j), sliceDimension);
130 }
131 } else {
132 checkAndAdd(vectorSlice, parameter, sliceDimension);
133 }
134 }
135 return vectorSlice;
136 }
137
138 private void checkAndAdd(final VectorSliceParameter vectorSlice, final Parameter parameter,
139 final int sliceDimension) throws XMLParseException {
140 vectorSlice.addParameter(parameter);
141 if (sliceDimension < 1 || sliceDimension > parameter.getDimension()) {
142 throw new XMLParseException("Slice dimension " + sliceDimension + " is invalid for a parameter" +
143 " with dimension = " + parameter.getDimension());
144 }
145 }
146
147 //************************************************************************
148 // AbstractXMLObjectParser implementation
149 //************************************************************************
150
151 public String getParserDescription() {
152 return "A vector parameter constructed from a slice of component parameters.";
153 }
154
155 public XMLSyntaxRule[] getSyntaxRules() {
156 return rules;
157 }
158
159 private final XMLSyntaxRule[] rules = {
160 new ElementRule(Parameter.class, 1, Integer.MAX_VALUE),
161 AttributeRule.newIntegerRule(SLICE_DIMENSION),
162 };
163
164 public Class getReturnType() {
165 return VectorSliceParameter.class;
166 }
167 };
168
169
170 }