Debian Codegraph @

src/dr/evomodel/continuous/RestrictedPartialsModel.java

raw symbols SCIP
1 package dr.evomodel.continuous;
2
3 import dr.evolution.tree.NodeRef;
4 import dr.evolution.tree.Tree;
5 import dr.evomodel.tree.TreeChangedEvent;
6 import dr.evomodel.tree.TreeModel;
7 import dr.evomodel.treedatalikelihood.continuous.cdi.ContinuousDiffusionIntegrator;
8 import dr.inference.model.AbstractModel;
9 import dr.inference.model.Model;
10 import dr.inference.model.Parameter;
11 import dr.inference.model.Variable;
12
13 import java.util.BitSet;
14 import java.util.HashMap;
15 import java.util.List;
16 import java.util.Map;
17
18 /**
19 * @author Marc A. Suchard
20 */
21 public class RestrictedPartialsModel extends AbstractModel {
22
23 private final TreeModel treeModel;
24 private final List<RestrictedPartials> restrictedPartialsList;
25 private final ContinuousDiffusionIntegrator cdi;
26
27 private boolean updateTreeMapping = true;
28 private boolean updateRestrictedPartials = true;
29
30 private final int startingPartialCount;
31 private final int startingMatrixCount;
32
33 private int partialsCount;
34 private int matrixCount;
35 private final int sparePartialIndex;
36
37 /**
38 * @param name Model Name
39 */
40 public RestrictedPartialsModel(String name, List<RestrictedPartials> restrictedPartialsList,
41 int startingPartialBufferCount, int startingMatrixBufferCount) {
42 super(name);
43
44 this.treeModel = validateTreeModel(restrictedPartialsList);
45 this.startingPartialCount = startingPartialBufferCount;
46 this.startingMatrixCount = startingMatrixBufferCount;
47
48 partialsCount = 0;
49 matrixCount = 0;
50
51 for (RestrictedPartials partials : restrictedPartialsList) {
52 partials.setIndex(partialsCount + startingPartialBufferCount);
53 addRestrictedPartials(partials);
54 ++partialsCount;
55 }
56 sparePartialIndex = partialsCount + startingPartialBufferCount;
57 ++partialsCount;
58
59 setupClamps();
60
61 this.restrictedPartialsList = restrictedPartialsList;
62 this.cdi = null; // likelihoodDelegate.getIntegrator();
63
64 }
65
66 public int getExtraPartialBufferCount() { return partialsCount; }
67
68 public int getExtraMatrixBufferCount() { return matrixCount; }
69
70 public void updatePartialRestrictions() {
71
72 if (updateTreeMapping) {
73 setupClamps();
74 updateTreeMapping = false;
75 }
76
77 if (updateRestrictedPartials) {
78 setupRestrictedPartials();
79 updateRestrictedPartials = false;
80 }
81 }
82
83 public boolean hasAnyPartialRestrictions() { return anyClamps; }
84
85 private TreeModel validateTreeModel(List<RestrictedPartials> restrictedPartialsList) {
86 TreeModel treeModel = restrictedPartialsList.get(0).getTreeModel();
87 for (RestrictedPartials restrictedPartials : restrictedPartialsList) {
88 if (restrictedPartials.getTreeModel() != treeModel) {
89 throw new IllegalArgumentException("All tree models must be the same");
90 }
91 }
92 return treeModel;
93 }
94
95 private void addRestrictedPartials(RestrictedPartials nodeClamp) {
96 if (clampList == null) {
97 clampList = new HashMap<BitSet, RestrictedPartials>();
98 }
99
100 clampList.put(nodeClamp.getTipBitSet(), nodeClamp);
101 addModel(nodeClamp);
102
103 System.err.println("Added a CLAMP!");
104 }
105
106 private void setupRestrictedPartials() {
107 for (RestrictedPartials clamp : clampList.values()) {
108 final int clampIndex = clamp.getIndex();
109 final double[] partials = clamp.getRestrictedPartials();
110
111 ContinuousDiffusionIntegrator cdi = null;
112 cdi.setPostOrderPartial(clampIndex, partials); // TODO Double buffering?
113 }
114
115 }
116
117 @Override
118 protected void handleModelChangedEvent(Model model, Object object, int index) {
119
120 if (model == treeModel) {
121 if (object instanceof TreeChangedEvent) {
122 TreeChangedEvent event = (TreeChangedEvent) object;
123 if (event.isTreeChanged()) {
124 updateTreeMapping = true;
125 } else if (event.isNodeChanged()) {
126 updateTreeMapping = true;
127 }
128 }
129 } else if (model instanceof RestrictedPartials) {
130 updateRestrictedPartials = true;
131 } else {
132 throw new RuntimeException("Unknown model");
133 }
134
135 }
136
137 /**
138 * Additional state information, outside of the sub-model is stored by this call.
139 */
140 @Override
141 protected void storeState() {
142
143 }
144
145 /**
146 * After this call the model is guaranteed to have returned its extra state information to
147 * the values coinciding with the last storeState call.
148 * Sub-models are handled automatically and do not need to be considered in this method.
149 */
150 @Override
151 protected void restoreState() {
152 updateTreeMapping = true; // TODO cache? Caching is still not working, see IMTL.restoreState()
153 updateRestrictedPartials = true;
154 }
155
156 /**
157 * This call specifies that the current state is accept. Most models will not need to do anything.
158 * Sub-models are handled automatically and do not need to be considered in this method.
159 */
160 @Override
161 protected void acceptState() {
162
163 }
164
165 /**
166 * This method is called whenever a parameter is changed.
167 * <p/>
168 * It is strongly recommended that the model component sets a "dirty" flag and does no
169 * further calculations. Recalculation is typically done when the model component is asked for
170 * some information that requires them. This mechanism is 'lazy' so that this method
171 * can be safely called multiple times with minimal computational cost.
172 *
173 * @param variable
174 * @param index
175 * @param type
176 */
177 @Override
178 protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
179
180 }
181
182 public void setupClamps() {
183 if (nodeToClampMap == null) {
184 nodeToClampMap = new HashMap<NodeRef, RestrictedPartials>();
185 }
186 nodeToClampMap.clear();
187
188 recursiveSetupClamp(treeModel, treeModel.getRoot(), new BitSet());
189
190 anyClamps = (nodeToClampMap.size() > 0);
191 }
192
193 private void recursiveSetupClamp(Tree tree, NodeRef node, BitSet tips) {
194
195 if (tree.isExternal(node)) {
196 tips.set(node.getNumber());
197 } else {
198 for (int i = 0; i < tree.getChildCount(node); i++) {
199 NodeRef child = tree.getChild(node, i);
200
201 BitSet childTips = new BitSet();
202 recursiveSetupClamp(tree, child, childTips);
203 tips.or(childTips);
204 }
205
206 if (clampList.containsKey(tips)) {
207 RestrictedPartials partials = clampList.get(tips);
208 partials.setNode(node);
209 nodeToClampMap.put(node, partials);
210 }
211 }
212 }
213
214 protected Map<BitSet, RestrictedPartials> clampList = null;
215 protected Map<NodeRef, RestrictedPartials> nodeToClampMap = null;
216
217 protected boolean anyClamps = false;
218
219 }