| 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.inferencexml.model; |
| 27 |
|
| 28 |
import dr.inference.model.CompoundLikelihood; |
| 29 |
import dr.inference.model.Likelihood; |
| 30 |
import dr.xml.*; |
| 31 |
|
| 32 |
import java.util.ArrayList; |
| 33 |
import java.util.List; |
| 34 |
import java.util.logging.Logger; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class CompoundLikelihoodParser extends AbstractXMLObjectParser { |
| 39 |
public static final String COMPOUND_LIKELIHOOD = "compoundLikelihood"; |
| 40 |
public static final String THREADS = "threads"; |
| 41 |
@Deprecated public static final String POSTERIOR = "posterior"; |
| 42 |
public static final String JOINT = "joint"; |
| 43 |
public static final String PRIOR = "prior"; |
| 44 |
public static final String LIKELIHOOD = "likelihood"; |
| 45 |
public static final String PSEUDO_PRIOR = "pseudoPrior"; |
| 46 |
public static final String REFERENCE_PRIOR = "referencePrior"; |
| 47 |
public static final String WORKING_PRIOR = "workingPrior"; |
| 48 |
|
| 49 |
public String getParserName() { |
| 50 |
return COMPOUND_LIKELIHOOD; |
| 51 |
} |
| 52 |
|
| 53 |
public String[] getParserNames() { |
| 54 |
return new String[]{getParserName(), POSTERIOR, JOINT, PRIOR, LIKELIHOOD, PSEUDO_PRIOR, REFERENCE_PRIOR, WORKING_PRIOR}; |
| 55 |
} |
| 56 |
|
| 57 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 58 |
|
| 59 |
|
| 60 |
int threads = xo.getAttribute(THREADS, -1); |
| 61 |
|
| 62 |
|
| 63 |
if (System.getProperty("thread.count") != null) { |
| 64 |
|
| 65 |
threads = Integer.parseInt(System.getProperty("thread.count")); |
| 66 |
if (threads < -1 || threads > 1000) { |
| 67 |
|
| 68 |
threads = -1; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
List<Likelihood> likelihoods = new ArrayList<Likelihood>(); |
| 74 |
for (int i = 0; i < xo.getChildCount(); i++) { |
| 75 |
final Object child = xo.getChild(i); |
| 76 |
if (child instanceof Likelihood) { |
| 77 |
|
| 78 |
if (likelihoods.contains(child)) { |
| 79 |
throw new XMLParseException("The likelihood element, '" + ((Likelihood) child).getId() + "', is already present in the likelihood or prior density."); |
| 80 |
} |
| 81 |
likelihoods.add((Likelihood) child); |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
} else { |
| 89 |
|
| 90 |
throw new XMLParseException("An element (" + child + ") which is not a likelihood has been added to a " |
| 91 |
+ COMPOUND_LIKELIHOOD + " element"); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
CompoundLikelihood compoundLikelihood; |
| 96 |
|
| 97 |
if (xo.getName().equalsIgnoreCase(LIKELIHOOD)) { |
| 98 |
compoundLikelihood = new CompoundLikelihood(threads, likelihoods); |
| 99 |
switch (threads) { |
| 100 |
case -1: |
| 101 |
Logger.getLogger("dr.evomodel").info("\nLikelihood computation is using an auto sizing thread pool."); |
| 102 |
break; |
| 103 |
case 0: |
| 104 |
Logger.getLogger("dr.evomodel").info("\nLikelihood computation is using a single thread."); |
| 105 |
break; |
| 106 |
default: |
| 107 |
Logger.getLogger("dr.evomodel").info("\nLikelihood computation is using a pool of " + threads + " threads."); |
| 108 |
break; |
| 109 |
} |
| 110 |
} else { |
| 111 |
compoundLikelihood = new CompoundLikelihood(likelihoods); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
return compoundLikelihood; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public String getParserDescription() { |
| 126 |
return "A likelihood function which is simply the product of its component likelihood functions."; |
| 127 |
} |
| 128 |
|
| 129 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 130 |
return rules; |
| 131 |
} |
| 132 |
|
| 133 |
private final XMLSyntaxRule[] rules = { |
| 134 |
AttributeRule.newIntegerRule(THREADS, true), |
| 135 |
new ElementRule(Likelihood.class, -1, Integer.MAX_VALUE) |
| 136 |
}; |
| 137 |
|
| 138 |
public Class getReturnType() { |
| 139 |
return CompoundLikelihood.class; |
| 140 |
} |
| 141 |
} |