| 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.inference.multidimensionalscaling; |
| 27 |
|
| 28 |
import dr.evomodel.antigenic.MultidimensionalScalingLikelihood; |
| 29 |
import dr.inference.hmc.GradientWrtParameterProvider; |
| 30 |
import dr.inference.model.*; |
| 31 |
import dr.util.DataTable; |
| 32 |
import dr.xml.*; |
| 33 |
|
| 34 |
import java.io.FileReader; |
| 35 |
import java.io.IOException; |
| 36 |
import java.util.HashMap; |
| 37 |
import java.util.Map; |
| 38 |
import java.util.logging.Logger; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public class MultiDimensionalScalingLikelihood extends AbstractModelLikelihood implements Reportable, |
| 46 |
GradientWrtParameterProvider { |
| 47 |
|
| 48 |
private final static String REQUIRED_FLAGS_PROPERTY = "mds.required.flags"; |
| 49 |
|
| 50 |
@Override |
| 51 |
public String getReport() { |
| 52 |
return getId() + ": " + getLogLikelihood(); |
| 53 |
} |
| 54 |
|
| 55 |
@Override |
| 56 |
public Likelihood getLikelihood() { |
| 57 |
return this; |
| 58 |
} |
| 59 |
|
| 60 |
@Override |
| 61 |
public Parameter getParameter() { |
| 62 |
return locationsParameter; |
| 63 |
} |
| 64 |
|
| 65 |
@Override |
| 66 |
public int getDimension() { |
| 67 |
return locationsParameter.getDimension(); |
| 68 |
} |
| 69 |
|
| 70 |
@Override |
| 71 |
public double[] getGradientLogDensity() { |
| 72 |
|
| 73 |
if (gradient == null) { |
| 74 |
gradient = new double[locationsParameter.getDimension()]; |
| 75 |
} |
| 76 |
|
| 77 |
mdsCore.getGradient(gradient); |
| 78 |
|
| 79 |
return gradient; |
| 80 |
} |
| 81 |
|
| 82 |
public enum ObservationType { |
| 83 |
POINT, |
| 84 |
UPPER_BOUND, |
| 85 |
LOWER_BOUND, |
| 86 |
MISSING |
| 87 |
} |
| 88 |
|
| 89 |
private final static String MULTIDIMENSIONAL_SCALING_LIKELIHOOD = "multiDimensionalScalingLikelihood"; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public MultiDimensionalScalingLikelihood( |
| 101 |
int mdsDimension, |
| 102 |
Parameter mdsPrecision, |
| 103 |
MatrixParameterInterface locationsParameter, |
| 104 |
DataTable<double[]> dataTable, |
| 105 |
boolean isLeftTruncated, |
| 106 |
boolean reorderData) { |
| 107 |
|
| 108 |
super(MULTIDIMENSIONAL_SCALING_LIKELIHOOD); |
| 109 |
|
| 110 |
this.mdsDimension = mdsDimension; |
| 111 |
|
| 112 |
|
| 113 |
String[] rowLabelsOriginal = dataTable.getRowLabels(); |
| 114 |
|
| 115 |
|
| 116 |
int rowCount = dataTable.getRowCount(); |
| 117 |
locationCount = rowCount; |
| 118 |
|
| 119 |
int[] permute; |
| 120 |
if (reorderData) { |
| 121 |
permute = getPermutation(rowLabelsOriginal, locationsParameter); |
| 122 |
} else { |
| 123 |
permute = new int[locationCount]; |
| 124 |
for (int i = 0; i < locationCount; ++i) { |
| 125 |
permute[i] = i; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
String[] rowLabels = new String[locationCount]; |
| 130 |
|
| 131 |
int observationCount = rowCount * rowCount; |
| 132 |
|
| 133 |
observations = new double[observationCount]; |
| 134 |
ObservationType[] observationTypes = new ObservationType[observationCount]; |
| 135 |
|
| 136 |
double[][] tmp = new double[rowCount][rowCount]; |
| 137 |
|
| 138 |
for (int i = 0; i < rowCount; i++) { |
| 139 |
rowLabels[i] = rowLabelsOriginal[permute[i]]; |
| 140 |
|
| 141 |
double[] dataRow = dataTable.getRow(permute[i]); |
| 142 |
|
| 143 |
for (int j = i + 1; j < rowCount; j++) { |
| 144 |
tmp[i][j] = tmp[j][i] = dataRow[permute[j]]; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
int u = 0; |
| 149 |
for (int i = 0; i < rowCount; i++) { |
| 150 |
for (int j = 0; j < rowCount; j++) { |
| 151 |
observations[u] = (i == j ? 0 : tmp[i][j]); |
| 152 |
observationTypes[u] = ObservationType.POINT; |
| 153 |
u++; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
initialize(mdsDimension, mdsPrecision, isLeftTruncated, locationsParameter, |
| 158 |
rowLabels, observations, observationTypes); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
public double[] getObservations() { return observations; } |
| 174 |
|
| 175 |
public MatrixParameterInterface getMatrixParameter() { return locationsParameter; } |
| 176 |
|
| 177 |
private int[] getPermutation(String[] source, MatrixParameterInterface destination) { |
| 178 |
|
| 179 |
if (source.length != destination.getColumnDimension()) { |
| 180 |
throw new IllegalArgumentException("Dimension mismatch"); |
| 181 |
} |
| 182 |
|
| 183 |
final int length = source.length; |
| 184 |
|
| 185 |
Map<String,Integer> map = new HashMap<String, Integer>(destination.getColumnDimension()); |
| 186 |
for (int i = 0; i < length; ++i) { |
| 187 |
map.put(source[i],i); |
| 188 |
} |
| 189 |
|
| 190 |
int[] permute = new int[length]; |
| 191 |
for (int i = 0; i < length; ++i) { |
| 192 |
Integer p = map.get(destination.getParameter(i).getParameterName()); |
| 193 |
if (p == null) { |
| 194 |
Logger.getLogger("dr.app.beagle").info("Missing label!!!"); |
| 195 |
} else { |
| 196 |
permute[i] = p; |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
return permute; |
| 201 |
} |
| 202 |
|
| 203 |
private MultiDimensionalScalingCore getCore() { |
| 204 |
long computeMode = 0; |
| 205 |
String r = System.getProperty(REQUIRED_FLAGS_PROPERTY); |
| 206 |
if (r != null) { |
| 207 |
computeMode = Long.parseLong(r.trim()); |
| 208 |
} |
| 209 |
|
| 210 |
MultiDimensionalScalingCore core; |
| 211 |
if (computeMode >= MultiDimensionalScalingCore.USE_NATIVE_MDS) { |
| 212 |
System.err.println("Attempting to use a native MDS core with flag: " + computeMode + "; may the force be with you ...."); |
| 213 |
core = new MassivelyParallelMDSImpl(); |
| 214 |
flags = computeMode; |
| 215 |
} else { |
| 216 |
System.err.println("Computer mode found: " + computeMode + " vs. " + r); |
| 217 |
core = new MultiDimensionalScalingCoreImpl(); |
| 218 |
} |
| 219 |
return core; |
| 220 |
} |
| 221 |
|
| 222 |
public int getMdsDimension() { return mdsDimension; } |
| 223 |
|
| 224 |
public int getLocationCount() { return locationCount; } |
| 225 |
|
| 226 |
protected void initialize( |
| 227 |
final int mdsDimension, |
| 228 |
final Parameter mdsPrecision, |
| 229 |
final boolean isLeftTruncated, |
| 230 |
final MatrixParameterInterface locationsParameter, |
| 231 |
final String[] locationLabels, |
| 232 |
final double[] observations, |
| 233 |
@SuppressWarnings("unused") final ObservationType[] observationTypes) { |
| 234 |
|
| 235 |
this.mdsCore = getCore(); |
| 236 |
|
| 237 |
if (isLeftTruncated) { |
| 238 |
flags |= MultiDimensionalScalingCore.LEFT_TRUNCATION; |
| 239 |
} |
| 240 |
|
| 241 |
System.err.println("Initializing with flags: " + flags); |
| 242 |
|
| 243 |
this.mdsCore.initialize(mdsDimension, locationCount, flags); |
| 244 |
this.locationLabels = locationLabels; |
| 245 |
|
| 246 |
this.locationsParameter = locationsParameter; |
| 247 |
setupLocationsParameter(this.locationsParameter); |
| 248 |
addVariable(locationsParameter); |
| 249 |
|
| 250 |
this.mdsPrecisionParameter = mdsPrecision; |
| 251 |
addVariable(mdsPrecision); |
| 252 |
|
| 253 |
mdsCore.setParameters(mdsPrecisionParameter.getParameterValues()); |
| 254 |
mdsCore.setPairwiseData(observations); |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
mdsCore.updateLocation(-1, locationsParameter.getParameterValues()); |
| 259 |
|
| 260 |
|
| 261 |
makeDirty(); |
| 262 |
} |
| 263 |
|
| 264 |
private void setupLocationsParameter(MatrixParameterInterface locationsParameter) { |
| 265 |
final boolean exisitingParameter = locationsParameter.getColumnDimension() > 0; |
| 266 |
|
| 267 |
if (exisitingParameter){ |
| 268 |
if (locationsParameter.getColumnDimension() != locationCount){ |
| 269 |
throw new RuntimeException("locationsParameter column dimension ("+locationsParameter.getColumnDimension()+") is not equal to the locationCount ("+locationCount+")"); |
| 270 |
} |
| 271 |
if (locationsParameter.getRowDimension() != mdsDimension){ |
| 272 |
throw new RuntimeException("locationsParameter row dimension ("+locationsParameter.getRowDimension()+") is not equal to the mdsDimension ("+mdsDimension+")"); |
| 273 |
} |
| 274 |
} else { |
| 275 |
throw new IllegalArgumentException("Dimensions on matrix must be set"); |
| 276 |
} |
| 277 |
|
| 278 |
for (int i = 0; i < locationLabels.length; i++) { |
| 279 |
if (locationsParameter.getParameter(i).getParameterName().compareTo(locationLabels[i]) != 0) { |
| 280 |
throw new RuntimeException("Mismatched trait parameter name (" + locationsParameter.getParameter(i).getParameterName() + |
| 281 |
") and data dimension name (" + locationLabels[i] + ")"); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
for (int i = 0; i < locationsParameter.getColumnDimension(); ++i) { |
| 286 |
Parameter param = locationsParameter.getParameter(i); |
| 287 |
try { |
| 288 |
param.getBounds(); |
| 289 |
} catch (NullPointerException exception) { |
| 290 |
param.addBounds(new Parameter.DefaultBounds( |
| 291 |
Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, param.getDimension())); |
| 292 |
} |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
@Override |
| 297 |
protected void handleModelChangedEvent(Model model, Object object, int index) { } |
| 298 |
|
| 299 |
@Override |
| 300 |
protected void handleVariableChangedEvent(Variable variable, int index, Variable.ChangeType type) { |
| 301 |
|
| 302 |
|
| 303 |
if (variable == locationsParameter) { |
| 304 |
|
| 305 |
if (index == -1) { |
| 306 |
|
| 307 |
mdsCore.updateLocation(-1, locationsParameter.getParameterValues()); |
| 308 |
} else { |
| 309 |
|
| 310 |
int locationIndex = index / mdsDimension; |
| 311 |
mdsCore.updateLocation(locationIndex, locationsParameter.getColumnValues(locationIndex)); |
| 312 |
} |
| 313 |
} else if (variable == mdsPrecisionParameter) { |
| 314 |
mdsCore.setParameters(mdsPrecisionParameter.getParameterValues()); |
| 315 |
} |
| 316 |
|
| 317 |
likelihoodKnown = false; |
| 318 |
} |
| 319 |
|
| 320 |
@Override |
| 321 |
protected void storeState() { |
| 322 |
storedLogLikelihood = logLikelihood; |
| 323 |
mdsCore.storeState(); |
| 324 |
} |
| 325 |
|
| 326 |
@Override |
| 327 |
protected void restoreState() { |
| 328 |
logLikelihood = storedLogLikelihood; |
| 329 |
likelihoodKnown = true; |
| 330 |
mdsCore.restoreState(); |
| 331 |
} |
| 332 |
|
| 333 |
@Override |
| 334 |
protected void acceptState() { |
| 335 |
mdsCore.acceptState(); |
| 336 |
|
| 337 |
} |
| 338 |
|
| 339 |
public void makeDirty() { |
| 340 |
likelihoodKnown = false; |
| 341 |
mdsCore.makeDirty(); |
| 342 |
} |
| 343 |
|
| 344 |
public Model getModel() { |
| 345 |
return this; |
| 346 |
} |
| 347 |
|
| 348 |
public double getLogLikelihood() { |
| 349 |
if (!likelihoodKnown) { |
| 350 |
logLikelihood = mdsCore.calculateLogLikelihood(); |
| 351 |
likelihoodKnown = true; |
| 352 |
} |
| 353 |
return logLikelihood; |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 361 |
|
| 362 |
final static String FILE_NAME = "fileName"; |
| 363 |
final static String LOCATIONS = "locations"; |
| 364 |
final static String MDS_DIMENSION = "mdsDimension"; |
| 365 |
final static String MDS_PRECISION = "mdsPrecision"; |
| 366 |
final static String INCLUDE_TRUNCATION = "includeTruncation"; |
| 367 |
final static String USE_OLD = "useOld"; |
| 368 |
final static String FORCE_REORDER = "forceReorder"; |
| 369 |
|
| 370 |
public String getParserName() { |
| 371 |
return MULTIDIMENSIONAL_SCALING_LIKELIHOOD; |
| 372 |
} |
| 373 |
|
| 374 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 375 |
|
| 376 |
String fileName = xo.getStringAttribute(FILE_NAME); |
| 377 |
DataTable<double[]> distanceTable; |
| 378 |
try { |
| 379 |
distanceTable = DataTable.Double.parse(new FileReader(fileName)); |
| 380 |
} catch (IOException e) { |
| 381 |
throw new XMLParseException("Unable to read assay data from file: " + e.getMessage()); |
| 382 |
} |
| 383 |
|
| 384 |
if (distanceTable.getRowCount() != distanceTable.getColumnCount()) { |
| 385 |
throw new XMLParseException("Data table is not symmetrical."); |
| 386 |
} |
| 387 |
|
| 388 |
int mdsDimension = xo.getIntegerAttribute(MDS_DIMENSION); |
| 389 |
|
| 390 |
MatrixParameterInterface locationsParameter = (MatrixParameterInterface) xo.getElementFirstChild(LOCATIONS); |
| 391 |
|
| 392 |
Parameter mdsPrecision = (Parameter) xo.getElementFirstChild(MDS_PRECISION); |
| 393 |
|
| 394 |
boolean useOld = xo.getAttribute(USE_OLD, false); |
| 395 |
|
| 396 |
boolean includeTruncation = xo.getAttribute(INCLUDE_TRUNCATION, false); |
| 397 |
|
| 398 |
boolean forceReorder = xo.getAttribute(FORCE_REORDER, false); |
| 399 |
|
| 400 |
if (useOld) { |
| 401 |
System.err.println("USE OLD"); |
| 402 |
return new MultidimensionalScalingLikelihood(mdsDimension, includeTruncation, mdsPrecision, (MatrixParameter)locationsParameter, distanceTable); |
| 403 |
} else { |
| 404 |
return new MultiDimensionalScalingLikelihood(mdsDimension, mdsPrecision, locationsParameter, |
| 405 |
distanceTable, includeTruncation, forceReorder); |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
public String getParserDescription() { |
| 414 |
return "Provides the likelihood of pairwise distance given vectors of coordinates" + |
| 415 |
"for points according to the multidimensional scaling scheme of XXX & Rafferty (to fill in)."; |
| 416 |
} |
| 417 |
|
| 418 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 419 |
return rules; |
| 420 |
} |
| 421 |
|
| 422 |
private final XMLSyntaxRule[] rules = { |
| 423 |
AttributeRule.newStringRule(FILE_NAME, false, "The name of the file containing the assay table"), |
| 424 |
AttributeRule.newIntegerRule(MDS_DIMENSION, false, "The dimension of the space for MDS"), |
| 425 |
new ElementRule(LOCATIONS, MatrixParameterInterface.class), |
| 426 |
AttributeRule.newBooleanRule(USE_OLD, true), |
| 427 |
AttributeRule.newBooleanRule(INCLUDE_TRUNCATION, true), |
| 428 |
AttributeRule.newBooleanRule(FORCE_REORDER, true), |
| 429 |
new ElementRule(MDS_PRECISION, Parameter.class) |
| 430 |
}; |
| 431 |
|
| 432 |
public Class getReturnType() { |
| 433 |
return MultiDimensionalScalingLikelihood.class; |
| 434 |
} |
| 435 |
}; |
| 436 |
|
| 437 |
public double getMDSPrecision() { |
| 438 |
return mdsPrecisionParameter.getParameterValue(0); |
| 439 |
} |
| 440 |
|
| 441 |
private final int mdsDimension; |
| 442 |
private final int locationCount; |
| 443 |
|
| 444 |
private MultiDimensionalScalingCore mdsCore; |
| 445 |
|
| 446 |
private String[] locationLabels; |
| 447 |
|
| 448 |
private Parameter mdsPrecisionParameter; |
| 449 |
private MatrixParameterInterface locationsParameter; |
| 450 |
|
| 451 |
private boolean likelihoodKnown = false; |
| 452 |
private double logLikelihood; |
| 453 |
private double storedLogLikelihood; |
| 454 |
|
| 455 |
private long flags = 0; |
| 456 |
|
| 457 |
private double[] observations; |
| 458 |
private double[] gradient; |
| 459 |
} |