| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 63 |
System.out.println("\tRate parameter: " + graphRate.getStatisticName()); |
| 64 |
|
| 65 |
|
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public double getLogLikelihood(double[] start, double[] stop, double time) { |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
double rtn = |
| 77 |
|
| 78 |
|
| 79 |
Math.log( |
| 80 |
map.getCTMCProbability(start, stop, graphRate.getParameterValue(0) * time) |
| 81 |
); |
| 82 |
|
| 83 |
|
| 84 |
return rtn; |
| 85 |
|
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
public void handleParameterChangedEvent(Parameter parameter, int index) { |
| 90 |
|
| 91 |
} |
| 92 |
|
| 93 |
|
| 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 |
|
| 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 |
} |