use of dr.inference.loggers.Logger in project beast-mcmc by beast-dev.
the class MCMC method chain.
/**
* This method actually initiates the MCMC analysis.
*/
public void chain() {
stopping = false;
currentState = 0;
timer.start();
if (loggers != null) {
for (Logger logger : loggers) {
logger.startLogging();
}
}
if (!stopping) {
long loadedState = 0;
if (Factory.INSTANCE != null) {
StateLoader initialStateLoader = Factory.INSTANCE.getInitialStateLoader();
if (initialStateLoader != null) {
double[] savedLnL = new double[1];
loadedState = initialStateLoader.loadState(mc, savedLnL);
mc.setCurrentLength(loadedState);
double lnL = mc.evaluate();
initialStateLoader.checkLoadState(savedLnL[0], lnL);
}
}
mc.addMarkovChainListener(chainListener);
long chainLength = getChainLength();
// this also potentially gets the new adaptationDelay of a possibly increased chain length
final long adaptationDelay = getAdaptationDelay();
// System.out.println("adaptationDelay = " + adaptationDelay + " vs. loadedState = " +loadedState);
if (adaptationDelay > loadedState) {
mc.runChain(adaptationDelay - loadedState, true);
chainLength -= adaptationDelay;
for (int i = 0; i < schedule.getOperatorCount(); i++) {
schedule.getOperator(i).reset();
}
}
mc.runChain(chainLength, false);
mc.terminateChain();
mc.removeMarkovChainListener(chainListener);
}
timer.stop();
}
use of dr.inference.loggers.Logger in project beast-mcmc by beast-dev.
the class MLOptimizer method chain.
/**
* This method actually intiates the MCMC analysis.
* the site patterns have been dropped from the site model
* to reduce the footprint of the sample.
*/
public void chain() {
currentState = 0;
if (loggers != null) {
for (Logger logger : loggers) {
logger.startLogging();
}
}
timer.start();
mc.reset();
timer.start();
mc.addMarkovChainListener(chainListener);
mc.runChain(getChainLength(), true);
mc.removeMarkovChainListener(chainListener);
timer.stop();
}
use of dr.inference.loggers.Logger in project beast-mcmc by beast-dev.
the class MLOptimizerParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
int chainLength = xo.getIntegerAttribute(CHAIN_LENGTH);
OperatorSchedule opsched = null;
dr.inference.model.Likelihood likelihood = null;
ArrayList<Logger> loggers = new ArrayList<Logger>();
for (int i = 0; i < xo.getChildCount(); i++) {
Object child = xo.getChild(i);
if (child instanceof dr.inference.model.Likelihood) {
likelihood = (dr.inference.model.Likelihood) child;
} else if (child instanceof OperatorSchedule) {
opsched = (OperatorSchedule) child;
} else if (child instanceof Logger) {
loggers.add((Logger) child);
} else {
throw new XMLParseException("Unrecognized element found in optimizer element:" + child);
}
}
Logger[] loggerArray = new Logger[loggers.size()];
loggers.toArray(loggerArray);
return new MLOptimizer("optimizer1", chainLength, likelihood, opsched, loggerArray);
}
use of dr.inference.loggers.Logger in project beast-mcmc by beast-dev.
the class MCMCParser method parseSMC.
/**
* Parse the SMC variant of MCMC.
* @param xo the XML object
* @return the SMC object
* @throws XMLParseException an exception of there is an XML parse error
*/
private SMC parseSMC(String id, XMLObject xo) throws XMLParseException {
List<StateLoaderSaver> particleStates = new ArrayList<StateLoaderSaver>();
String particleFolder = System.getProperty("smc.particle_folder");
File folder = new File(particleFolder);
if (!folder.isDirectory()) {
throw new XMLParseException("Specified particle folder is not a folder");
}
File[] particleFiles = folder.listFiles();
if (particleFiles == null || particleFiles.length == 0) {
throw new XMLParseException("Specified particle folder is empty");
}
for (final File particleFile : particleFiles) {
if (particleFile.isFile() && particleFile.getName().endsWith(".part")) {
final File saveFile = new File(particleFile.getAbsolutePath() + ".out");
particleStates.add(Factory.INSTANCE.getStateLoaderSaver(particleFile, saveFile));
}
}
if (particleStates.size() == 0) {
throw new XMLParseException("No particle files were found in the folder");
}
SMC smc = new SMC(id, particleStates);
long chainLength = xo.getLongIntegerAttribute(CHAIN_LENGTH);
SMCOptions options = new SMCOptions(chainLength);
OperatorSchedule opsched = (OperatorSchedule) xo.getChild(OperatorSchedule.class);
Likelihood likelihood = (Likelihood) xo.getChild(Likelihood.class);
likelihood.setUsed();
ArrayList<Logger> loggers = new ArrayList<Logger>();
for (int i = 0; i < xo.getChildCount(); i++) {
Object child = xo.getChild(i);
if (child instanceof Logger) {
loggers.add((Logger) child);
}
}
Logger[] loggerArray = new Logger[loggers.size()];
loggers.toArray(loggerArray);
java.util.logging.Logger.getLogger("dr.inference").info("\nCreating the SMC chain set:" + "\n particles = " + particleStates.size() + "\n chain length = " + options.getChainLength());
smc.init(options, likelihood, opsched, loggerArray);
return smc;
}
use of dr.inference.loggers.Logger in project beast-mcmc by beast-dev.
the class MCMCParser method parseMCMC.
/**
* Parse the MCMC object.
* @param xo the XML object
* @return the MXMX object
* @throws XMLParseException an exception of there is an XML parse error
*/
private MCMC parseMCMC(String id, XMLObject xo) throws XMLParseException {
MCMC mcmc = new MCMC(id);
long chainLength = xo.getLongIntegerAttribute(CHAIN_LENGTH);
boolean useAdaptation = xo.getAttribute(ADAPTATION, true) || xo.getAttribute(AUTO_OPTIMIZE, true);
if (System.getProperty("mcmc.use_adaptation") != null) {
useAdaptation = Boolean.parseBoolean(System.getProperty("mcmc.use_adaptation"));
}
long adaptationDelay = chainLength / 100;
adaptationDelay = xo.getAttribute(ADAPTATION_DELAY, xo.getAttribute(AUTO_OPTIMIZE_DELAY, xo.getAttribute(PRE_BURNIN, adaptationDelay)));
double adaptationTarget = 0.234;
if (System.getProperty("mcmc.adaptation_target") != null) {
adaptationTarget = Double.parseDouble(System.getProperty("mcmc.adaptation_target"));
}
boolean useSmoothAcceptanceRatio = xo.getAttribute(SMOOTHED_ACCEPTANCE_RATIO, false);
double temperature = xo.getAttribute(TEMPERATURE, 1.0);
long fullEvaluationCount = xo.getAttribute(FULL_EVALUATION, DEFAULT_FULL_EVALUATION_COUNT);
if (System.getProperty("mcmc.evaluation.count") != null) {
fullEvaluationCount = Long.parseLong(System.getProperty("mcmc.evaluation.count"));
}
double evaluationTestThreshold = MarkovChain.EVALUATION_TEST_THRESHOLD;
evaluationTestThreshold = xo.getAttribute(EVALUATION_THRESHOLD, evaluationTestThreshold);
if (System.getProperty("mcmc.evaluation.threshold") != null) {
evaluationTestThreshold = Double.parseDouble(System.getProperty("mcmc.evaluation.threshold"));
}
int minOperatorCountForFullEvaluation = xo.getAttribute(MIN_OPS_EVALUATIONS, 1);
MCMCOptions options = new MCMCOptions(chainLength, fullEvaluationCount, minOperatorCountForFullEvaluation, evaluationTestThreshold, useAdaptation, adaptationDelay, adaptationTarget, useSmoothAcceptanceRatio, temperature);
OperatorSchedule opsched = (OperatorSchedule) xo.getChild(OperatorSchedule.class);
Likelihood likelihood = (Likelihood) xo.getChild(Likelihood.class);
likelihood.setUsed();
if (Boolean.valueOf(System.getProperty("show_warnings", "false"))) {
// check that all models, parameters and likelihoods are being used
for (Likelihood l : Likelihood.FULL_LIKELIHOOD_SET) {
if (!l.isUsed()) {
java.util.logging.Logger.getLogger("dr.inference").warning("Likelihood, " + l.getId() + ", of class " + l.getClass().getName() + " is not being handled by the MCMC.");
}
}
for (Model m : Model.FULL_MODEL_SET) {
if (!m.isUsed()) {
java.util.logging.Logger.getLogger("dr.inference").warning("Model, " + m.getId() + ", of class " + m.getClass().getName() + " is not being handled by the MCMC.");
}
}
for (Parameter p : Parameter.FULL_PARAMETER_SET) {
if (!p.isUsed()) {
java.util.logging.Logger.getLogger("dr.inference").warning("Parameter, " + p.getId() + ", of class " + p.getClass().getName() + " is not being handled by the MCMC.");
}
}
}
ArrayList<Logger> loggers = new ArrayList<Logger>();
for (int i = 0; i < xo.getChildCount(); i++) {
Object child = xo.getChild(i);
if (child instanceof Logger) {
loggers.add((Logger) child);
}
}
mcmc.setShowOperatorAnalysis(true);
if (xo.hasAttribute(OPERATOR_ANALYSIS)) {
mcmc.setOperatorAnalysisFile(XMLParser.getLogFile(xo, OPERATOR_ANALYSIS));
}
Logger[] loggerArray = new Logger[loggers.size()];
loggers.toArray(loggerArray);
java.util.logging.Logger.getLogger("dr.inference").info("\nCreating the MCMC chain:" + "\n chain length = " + options.getChainLength() + "\n operator adaption = " + options.useAdaptation() + (options.useAdaptation() ? "\n adaptation delayed for " + options.getAdaptationDelay() + " steps" : "") + (options.getFullEvaluationCount() == 0 ? "\n full evaluation test off" : ""));
mcmc.init(options, likelihood, opsched, loggerArray);
MarkovChain mc = mcmc.getMarkovChain();
double initialScore = mc.getCurrentScore();
if (initialScore == Double.NEGATIVE_INFINITY) {
String message = "The initial posterior is zero";
if (likelihood instanceof CompoundLikelihood) {
message += ": " + ((CompoundLikelihood) likelihood).getDiagnosis(2);
} else {
message += "!";
}
throw new IllegalArgumentException(message);
}
if (!xo.getAttribute(SPAWN, true))
mcmc.setSpawnable(false);
return mcmc;
}
Aggregations