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