Search in sources :

Example 1 with Runnable

use of beast.core.Runnable in project beast2 by CompEvol.

the class JSONParser method parseRunElement.

void parseRunElement(JSONObject topNode) throws JSONParserException {
    // find beast element
    try {
        Object o = doc.get(XMLParser.BEAST_ELEMENT);
        if (o == null) {
            throw new JSONParserException(topNode, "Expected " + XMLParser.BEAST_ELEMENT + " top level object in file", 102);
        }
        if (!(o instanceof JSONArray)) {
            throw new JSONParserException(topNode, "Expected " + XMLParser.BEAST_ELEMENT + " to be a list", 1020);
        }
        JSONArray analysis = (JSONArray) o;
        runnable = null;
        for (int i = 0; i < analysis.length(); i++) {
            o = analysis.get(i);
            if (!(o instanceof JSONObject)) {
                throw new JSONParserException(topNode, XMLParser.BEAST_ELEMENT + " should only contain objects", 1021);
            }
            JSONObject node = (JSONObject) o;
            o = createObject(node, RUNNABLE_CLASS);
            if (o instanceof Runnable) {
                if (runnable != null) {
                    throw new JSONParserException(node, "Expected only one runnable element in file", 103);
                }
                runnable = (Runnable) o;
            }
        }
        if (runnable == null) {
            throw new JSONParserException(topNode, "Expected at least one runnable element in file", 1030);
        }
    } catch (JSONException e) {
        throw new JSONParserException(topNode, e.getMessage(), 1004);
    }
}
Also used : JSONObject(org.json.JSONObject) Runnable(beast.core.Runnable) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject)

Example 2 with Runnable

use of beast.core.Runnable in project bacter by tgvaughan.

the class OperatorTester method main.

public static void main(String[] args) {
    if (args.length < 4) {
        System.out.println("Usage: OperatorTester model.xml operator_ID state_file iterations");
        System.exit(0);
    }
    String modelFileName = args[0];
    String operatorID = args[1];
    String stateFileName = args[2];
    int Niter = Integer.parseInt(args[3]);
    XMLParser xmlParser = new XMLParser();
    Runnable runnable = null;
    try {
        runnable = xmlParser.parseFile(new File(modelFileName));
    } catch (Exception e) {
        System.out.println("Error parsing model XML file.");
        e.printStackTrace();
    }
    if (runnable == null)
        System.exit(1);
    if (!(runnable instanceof MCMC)) {
        System.out.println("XML file does not describe MCMC analysis.");
        System.exit(1);
    }
    MCMC mcmc = (MCMC) runnable;
    Operator operator = null;
    for (Operator thisOperator : mcmc.operatorsInput.get()) {
        if (thisOperator.getID().equals(operatorID)) {
            operator = thisOperator;
            break;
        }
    }
    if (operator == null) {
        System.out.println("Model does not include operator with ID " + operatorID + ".");
        System.exit(1);
    }
    State state = mcmc.startStateInput.get();
    state.setStateFileName(stateFileName);
    try {
        state.restoreFromFile();
    } catch (Exception e) {
        System.out.println("Error loading state from file.");
        e.printStackTrace();
        System.exit(1);
    }
    try {
        state.robustlyCalcPosterior(mcmc.posteriorInput.get());
    } catch (Exception e) {
        System.out.println("Failed to calculate posterior.");
        System.exit(1);
    }
    Logger.FILE_MODE = Logger.LogFileMode.overwrite;
    for (Logger logger : mcmc.loggersInput.get()) {
        try {
            logger.everyInput.setValue(1, logger);
            logger.initAndValidate();
            logger.init();
            logger.log(0);
        } catch (Exception e) {
            System.out.println("Error initializing logger.");
            System.exit(1);
        }
    }
    try {
        state.robustlyCalcPosterior(mcmc.posteriorInput.get());
    } catch (Exception e) {
        System.out.println("Failed to calculate posterior.");
        System.exit(1);
    }
    state.storeCalculationNodes();
    for (int i = 1; i < Niter; i++) {
        state.store(i);
        // for (Logger logger : mcmc.loggersInput.get())
        // logger.log(i);
        Double hgf = operator.proposal();
        if (hgf > Double.NEGATIVE_INFINITY) {
            state.checkCalculationNodesDirtiness();
            try {
                mcmc.posteriorInput.get().calculateLogP();
            } catch (Exception e) {
                System.out.println("Failed to calculate posterior.");
                System.exit(1);
            }
            for (Logger logger : mcmc.loggersInput.get()) logger.log(i);
            state.restore();
            state.restoreCalculationNodes();
            state.setEverythingDirty(false);
        } else {
            state.restore();
        }
    }
    for (Logger logger : mcmc.loggersInput.get()) logger.close();
}
Also used : Runnable(beast.core.Runnable) XMLParser(beast.util.XMLParser) File(java.io.File)

Example 3 with Runnable

use of beast.core.Runnable in project beast2 by CompEvol.

the class Document method isValidModel.

int isValidModel() {
    BEASTObjectSet pluginSet = calcPluginSet();
    if (pluginSet.m_plugins.get().size() == 0) {
        return STATUS_EMPTY_MODEL;
    }
    if (pluginSet.m_plugins.get().size() > 1) {
        return STATUS_ORPHANS_IN_MODEL;
    }
    boolean hasRunable = false;
    for (BEASTInterface beastObject : pluginSet.m_plugins.get()) {
        if (beastObject instanceof Runnable) {
            hasRunable = true;
        }
    }
    if (!hasRunable) {
        return STATUS_NOT_RUNNABLE;
    }
    return STATUS_OK;
}
Also used : Runnable(beast.core.Runnable) BEASTInterface(beast.core.BEASTInterface)

Aggregations

Runnable (beast.core.Runnable)3 BEASTInterface (beast.core.BEASTInterface)1 XMLParser (beast.util.XMLParser)1 File (java.io.File)1 JSONArray (org.json.JSONArray)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1