| 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.evomodelxml.coalescent.operators; |
| 27 |
|
| 28 |
import dr.evomodel.coalescent.GMRFMultilocusSkyrideLikelihood; |
| 29 |
import dr.evomodel.coalescent.GMRFSkyrideLikelihood; |
| 30 |
import dr.evomodel.coalescent.operators.GMRFMultilocusSkyrideBlockUpdateOperator; |
| 31 |
import dr.evomodel.coalescent.operators.GMRFSkyrideBlockUpdateOperator; |
| 32 |
import dr.inference.operators.AdaptableMCMCOperator; |
| 33 |
import dr.inference.operators.AdaptationMode; |
| 34 |
import dr.inference.operators.MCMCOperator; |
| 35 |
import dr.math.MathUtils; |
| 36 |
import dr.xml.*; |
| 37 |
|
| 38 |
import java.io.IOException; |
| 39 |
import java.util.logging.*; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public class GMRFSkyrideBlockUpdateOperatorParser extends AbstractXMLObjectParser { |
| 45 |
|
| 46 |
public static final String BLOCK_UPDATE_OPERATOR = "gmrfBlockUpdateOperator"; |
| 47 |
public static final String GRID_BLOCK_UPDATE_OPERATOR = "gmrfGridBlockUpdateOperator"; |
| 48 |
public static final String SCALE_FACTOR = "scaleFactor"; |
| 49 |
public static final String MAX_ITERATIONS = "maxIterations"; |
| 50 |
public static final String STOP_VALUE = "stopValue"; |
| 51 |
public static final String KEEP_LOG_RECORD = "keepLogRecord"; |
| 52 |
public static final String OLD_SKYRIDE = "oldSkyride"; |
| 53 |
|
| 54 |
public String getParserName() { |
| 55 |
return BLOCK_UPDATE_OPERATOR; |
| 56 |
} |
| 57 |
|
| 58 |
public String[] getParserNames() { |
| 59 |
return new String[]{ |
| 60 |
getParserName(), |
| 61 |
GRID_BLOCK_UPDATE_OPERATOR, |
| 62 |
}; |
| 63 |
} |
| 64 |
|
| 65 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 66 |
|
| 67 |
boolean logRecord = xo.getAttribute(KEEP_LOG_RECORD, false); |
| 68 |
|
| 69 |
Handler gmrfHandler; |
| 70 |
Logger gmrfLogger = Logger.getLogger("dr.evomodel.coalescent.operators.GMRFSkyrideBlockUpdateOperator"); |
| 71 |
gmrfLogger.setUseParentHandlers(false); |
| 72 |
|
| 73 |
if (logRecord) { |
| 74 |
gmrfLogger.setLevel(Level.FINE); |
| 75 |
|
| 76 |
try { |
| 77 |
gmrfHandler = new FileHandler("GMRFBlockUpdate.log." + MathUtils.getSeed()); |
| 78 |
} catch (IOException e) { |
| 79 |
throw new RuntimeException(e.getMessage()); |
| 80 |
} |
| 81 |
gmrfHandler.setLevel(Level.FINE); |
| 82 |
|
| 83 |
gmrfHandler.setFormatter(new XMLFormatter() { |
| 84 |
public String format(LogRecord record) { |
| 85 |
return "<record>\n \t<message>\n\t" + record.getMessage() |
| 86 |
+ "\n\t</message>\n<record>\n"; |
| 87 |
} |
| 88 |
}); |
| 89 |
|
| 90 |
gmrfLogger.addHandler(gmrfHandler); |
| 91 |
} |
| 92 |
|
| 93 |
AdaptationMode mode = AdaptationMode.parseMode(xo); |
| 94 |
if (mode == AdaptationMode.DEFAULT) mode = AdaptationMode.ADAPTATION_ON; |
| 95 |
|
| 96 |
double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT); |
| 97 |
double scaleFactor = xo.getDoubleAttribute(SCALE_FACTOR); |
| 98 |
if (scaleFactor == 1.0) { |
| 99 |
mode = AdaptationMode.ADAPTATION_OFF; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
if (scaleFactor < 1.0) { |
| 105 |
throw new XMLParseException("scaleFactor must be greater than or equal to 1.0"); |
| 106 |
} |
| 107 |
|
| 108 |
int maxIterations = xo.getAttribute(MAX_ITERATIONS, 200); |
| 109 |
|
| 110 |
double stopValue = xo.getAttribute(STOP_VALUE, 0.01); |
| 111 |
|
| 112 |
|
| 113 |
if (xo.getAttribute(OLD_SKYRIDE, true) |
| 114 |
&& !(xo.getName().compareTo(GRID_BLOCK_UPDATE_OPERATOR) == 0) |
| 115 |
) { |
| 116 |
GMRFSkyrideLikelihood gmrfLikelihood = (GMRFSkyrideLikelihood) xo.getChild(GMRFSkyrideLikelihood.class); |
| 117 |
return new GMRFSkyrideBlockUpdateOperator(gmrfLikelihood, weight, mode, scaleFactor, |
| 118 |
maxIterations, stopValue); |
| 119 |
} else { |
| 120 |
GMRFMultilocusSkyrideLikelihood gmrfMultilocusLikelihood = (GMRFMultilocusSkyrideLikelihood) xo.getChild(GMRFMultilocusSkyrideLikelihood.class); |
| 121 |
return new GMRFMultilocusSkyrideBlockUpdateOperator(gmrfMultilocusLikelihood, weight, mode, scaleFactor, |
| 122 |
maxIterations, stopValue); |
| 123 |
} |
| 124 |
|
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public String getParserDescription() { |
| 132 |
return "This element returns a GMRF block-update operator for the joint distribution of the population sizes and precision parameter."; |
| 133 |
} |
| 134 |
|
| 135 |
public Class getReturnType() { |
| 136 |
return MCMCOperator.class; |
| 137 |
} |
| 138 |
|
| 139 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 140 |
return rules; |
| 141 |
} |
| 142 |
|
| 143 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 144 |
AttributeRule.newDoubleRule(SCALE_FACTOR), |
| 145 |
AttributeRule.newDoubleRule(MCMCOperator.WEIGHT), |
| 146 |
AttributeRule.newBooleanRule(AdaptableMCMCOperator.AUTO_OPTIMIZE, true), |
| 147 |
AttributeRule.newDoubleRule(STOP_VALUE, true), |
| 148 |
AttributeRule.newIntegerRule(MAX_ITERATIONS, true), |
| 149 |
AttributeRule.newBooleanRule(OLD_SKYRIDE, true), |
| 150 |
new ElementRule(GMRFSkyrideLikelihood.class) |
| 151 |
}; |
| 152 |
|
| 153 |
} |