Debian Codegraph @

src/dr/evomodel/continuous/MapDiffusionModel.java

raw symbols SCIP
1 /*
2 * MapDiffusionModel.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.evomodel.continuous;
27
28 import dr.inference.model.Parameter;
29 import dr.math.MathUtils;
30 import dr.xml.*;
31
32 import java.io.File;
33 import java.io.FileNotFoundException;
34 import java.io.FileReader;
35 import java.io.IOException;
36
37
38 public class MapDiffusionModel extends MultivariateDiffusionModel {
39 public static final String MAP_DIFFUSION_MODEL = "mapDiffusionModel";
40 public static final String GRASS_FILE_NAME = "grassMapFile";
41 public static final String STARTING_VALUES = "randomStartingValues";
42
43 public MapDiffusionModel(TopographicalMap map, Parameter graphRate) {
44 super();
45 this.map = map;
46 this.graphRate = graphRate;
47 addVariable(graphRate);
48 initializationReport();
49 }
50
51
52 public TopographicalMap getMap() {
53 return map;
54 }
55
56 private void initializationReport() {
57
58 System.out.println("Constructing map diffusion model for");
59 System.out.println("\tMap: " + map.getXDim() + "x" + map.getYDim());
60 System.out.println("\tRandom-walk order: " + map.getOrder());
61 System.out.println("\tRandom-walk non-zero size: " + map.getNonZeroSize());
62 // System.out.println("\tRW1 Matrix:\n"+map.getMatrix().sparseRepresentation());
63 System.out.println("\tRate parameter: " + graphRate.getStatisticName());
64 //System.out.println("\tTest probability: "+map.getMatrix().getExponentialEntry(0,0,1.0));
65
66 }
67
68 /**
69 * @return the log likelihood of going from start to stop in the given time
70 */
71 public double getLogLikelihood(double[] start, double[] stop, double time) {
72 // System.err.println("Calc from "+start[0]+" "+start[1]);
73 // System.err.println("Calc to "+stop[0]+" "+stop[1]);
74
75 // System.err.println("like");
76 double rtn =
77
78 // return
79 Math.log(
80 map.getCTMCProbability(start, stop, graphRate.getParameterValue(0) * time)
81 );
82
83 // System.err.println("logProb = "+rtn);
84 return rtn;
85
86 }
87
88
89 public void handleParameterChangedEvent(Parameter parameter, int index) {
90 // calculatePrecisionInfo();
91 }
92
93 // private void calculatePrecisionInfo() {
94 // }
95
96 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
97
98 public String getParserName() {
99 return MAP_DIFFUSION_MODEL;
100 }
101
102 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
103
104
105 Parameter graphRate = (Parameter) xo.getChild(Parameter.class);
106
107 String mapFileName = xo.getStringAttribute(GRASS_FILE_NAME);
108
109 File mapFile;
110
111 try {
112 File file = new File(mapFileName);
113 String name = file.getName();
114 String parent = file.getParent();
115
116 if (!file.isAbsolute()) {
117 parent = System.getProperty("user.dir");
118 }
119 mapFile = new File(parent, name);
120 new FileReader(mapFile);
121 } catch (FileNotFoundException fnfe) {
122 throw new XMLParseException("File '" + mapFileName + "' can not be opened for " + getParserName() + " element.");
123 }
124
125 double[][] mapValues;
126
127 try {
128 mapValues = TopographicalMap.readGRASSAscii(mapFileName);
129 } catch (IOException e) {
130 throw new XMLParseException("File '" + mapFileName + "' can not be read as GRASS file");
131 }
132
133 TopographicalMap map = new TopographicalMap(mapValues);
134
135 XMLObject cxo = xo.getChild(STARTING_VALUES);
136 if (cxo != null) {
137 System.err.println("Init");
138 Parameter startPosition = (Parameter) cxo.getChild(Parameter.class);
139 int numberPositions = startPosition.getDimension() / 2;
140 int dimX = map.getXDim();
141 int dimY = map.getYDim();
142 for (int i = 0; i < numberPositions; i++) {
143 int x = -1;
144 int y = -1;
145 while (map.getIndex(x, y) == -1) {
146 x = MathUtils.nextInt(dimX);
147 y = MathUtils.nextInt(dimY);
148 x = 0;
149 y = 100;
150 }
151 startPosition.setParameterValue(i * 2, x);
152
153 startPosition.setParameterValue(i * 2 + 1, y);
154 System.err.println("set: " + x + "," + y);
155 }
156 }
157
158 return new MapDiffusionModel(map, graphRate);
159
160 }
161
162 //************************************************************************
163 // AbstractXMLObjectParser implementation
164 //************************************************************************
165
166 public String getParserDescription() {
167 return "Describes a multivariate discrete diffusion process.";
168 }
169
170 public XMLSyntaxRule[] getSyntaxRules() {
171 return rules;
172 }
173
174 private final XMLSyntaxRule[] rules = {
175 AttributeRule.newStringRule(GRASS_FILE_NAME),
176 new ElementRule(Parameter.class),
177 };
178
179 public Class getReturnType() {
180 return MapDiffusionModel.class;
181 }
182
183 };
184
185 private final TopographicalMap map;
186 private final Parameter graphRate;
187
188 }