Search in sources :

Example 1 with TreeLogger

use of dr.evomodel.tree.TreeLogger in project beast-mcmc by beast-dev.

the class OperatorAssert method irreducibilityTester.

private void irreducibilityTester(Tree tree, int numLabelledTopologies, int chainLength, int sampleTreeEvery) throws IOException, Importer.ImportException {
    MCMC mcmc = new MCMC("mcmc1");
    MCMCOptions options = new MCMCOptions(chainLength);
    TreeModel treeModel = new TreeModel("treeModel", tree);
    TreeLengthStatistic tls = new TreeLengthStatistic(TL, treeModel);
    TreeHeightStatistic rootHeight = new TreeHeightStatistic(TREE_HEIGHT, treeModel);
    OperatorSchedule schedule = getOperatorSchedule(treeModel);
    Parameter b = new Parameter.Default("b", 2.0, 0.0, Double.MAX_VALUE);
    Parameter d = new Parameter.Default("d", 0.0, 0.0, Double.MAX_VALUE);
    SpeciationModel speciationModel = new BirthDeathGernhard08Model(b, d, null, BirthDeathGernhard08Model.TreeType.UNSCALED, Units.Type.YEARS);
    Likelihood likelihood = new SpeciationLikelihood(treeModel, speciationModel, "yule.like");
    MCLogger[] loggers = new MCLogger[2];
    //        loggers[0] = new MCLogger(new ArrayLogFormatter(false), 100, false);
    //        loggers[0].add(likelihood);
    //        loggers[0].add(rootHeight);
    //        loggers[0].add(tls);
    loggers[0] = new MCLogger(new TabDelimitedFormatter(System.out), 10000, false);
    loggers[0].add(likelihood);
    loggers[0].add(rootHeight);
    loggers[0].add(tls);
    File file = new File("yule.trees");
    file.deleteOnExit();
    FileOutputStream out = new FileOutputStream(file);
    loggers[1] = new TreeLogger(treeModel, new TabDelimitedFormatter(out), sampleTreeEvery, true, true, false);
    mcmc.setShowOperatorAnalysis(true);
    mcmc.init(options, likelihood, schedule, loggers);
    mcmc.run();
    out.flush();
    out.close();
    Set<String> uniqueTrees = new HashSet<String>();
    HashMap<String, Integer> topologies = new HashMap<String, Integer>();
    HashMap<String, HashMap<String, Integer>> treeCounts = new HashMap<String, HashMap<String, Integer>>();
    NexusImporter importer = new NexusImporter(new FileReader(file));
    int sampleSize = 0;
    while (importer.hasTree()) {
        sampleSize++;
        Tree t = importer.importNextTree();
        String uniqueNewick = TreeUtils.uniqueNewick(t, t.getRoot());
        String topology = uniqueNewick.replaceAll("\\w+", "X");
        if (!uniqueTrees.contains(uniqueNewick)) {
            uniqueTrees.add(uniqueNewick);
        }
        HashMap<String, Integer> counts;
        if (topologies.containsKey(topology)) {
            topologies.put(topology, topologies.get(topology) + 1);
            counts = treeCounts.get(topology);
        } else {
            topologies.put(topology, 1);
            counts = new HashMap<String, Integer>();
            treeCounts.put(topology, counts);
        }
        if (counts.containsKey(uniqueNewick)) {
            counts.put(uniqueNewick, counts.get(uniqueNewick) + 1);
        } else {
            counts.put(uniqueNewick, 1);
        }
    }
    TestCase.assertEquals(numLabelledTopologies, uniqueTrees.size());
    TestCase.assertEquals(sampleSize, chainLength / sampleTreeEvery + 1);
    Set<String> keys = topologies.keySet();
    double ep = 1.0 / topologies.size();
    for (String topology : keys) {
        double ap = ((double) topologies.get(topology)) / (sampleSize);
        //          	assertExpectation(ep, ap, sampleSize);
        HashMap<String, Integer> counts = treeCounts.get(topology);
        Set<String> trees = counts.keySet();
        double MSE = 0;
        double ep1 = 1.0 / counts.size();
        for (String t : trees) {
            double ap1 = ((double) counts.get(t)) / (topologies.get(topology));
            //              	assertExpectation(ep1, ap1, topologies.get(topology));
            MSE += (ep1 - ap1) * (ep1 - ap1);
        }
        MSE /= counts.size();
        System.out.println("The Mean Square Error for the topolgy " + topology + " is " + MSE);
    }
}
Also used : HashMap(java.util.HashMap) Likelihood(dr.inference.model.Likelihood) SpeciationLikelihood(dr.evomodel.speciation.SpeciationLikelihood) BirthDeathGernhard08Model(dr.evomodel.speciation.BirthDeathGernhard08Model) MCMC(dr.inference.mcmc.MCMC) SpeciationLikelihood(dr.evomodel.speciation.SpeciationLikelihood) TreeModel(dr.evomodel.tree.TreeModel) TreeLogger(dr.evomodel.tree.TreeLogger) MCMCOptions(dr.inference.mcmc.MCMCOptions) TreeLengthStatistic(dr.evomodel.tree.TreeLengthStatistic) FlexibleTree(dr.evolution.tree.FlexibleTree) Tree(dr.evolution.tree.Tree) FileReader(java.io.FileReader) HashSet(java.util.HashSet) NexusImporter(dr.evolution.io.NexusImporter) OperatorSchedule(dr.inference.operators.OperatorSchedule) SpeciationModel(dr.evomodel.speciation.SpeciationModel) TabDelimitedFormatter(dr.inference.loggers.TabDelimitedFormatter) FileOutputStream(java.io.FileOutputStream) TreeHeightStatistic(dr.evomodel.tree.TreeHeightStatistic) Parameter(dr.inference.model.Parameter) File(java.io.File) MCLogger(dr.inference.loggers.MCLogger)

Example 2 with TreeLogger

use of dr.evomodel.tree.TreeLogger in project beast-mcmc by beast-dev.

the class TreeLoggerParser method parseXMLObject.

/**
     * @return an object based on the XML element it was passed.
     */
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    parseXMLParameters(xo);
    TreeLogger logger = new TreeLogger(tree, branchRates, treeAttributeProviders, treeTraitProviders, formatter, logEvery, nexusFormat, sortTranslationTable, mapNames, format, condition);
    if (title != null) {
        logger.setTitle(title);
    }
    return logger;
}
Also used : TreeLogger(dr.evomodel.tree.TreeLogger)

Aggregations

TreeLogger (dr.evomodel.tree.TreeLogger)2 NexusImporter (dr.evolution.io.NexusImporter)1 FlexibleTree (dr.evolution.tree.FlexibleTree)1 Tree (dr.evolution.tree.Tree)1 BirthDeathGernhard08Model (dr.evomodel.speciation.BirthDeathGernhard08Model)1 SpeciationLikelihood (dr.evomodel.speciation.SpeciationLikelihood)1 SpeciationModel (dr.evomodel.speciation.SpeciationModel)1 TreeHeightStatistic (dr.evomodel.tree.TreeHeightStatistic)1 TreeLengthStatistic (dr.evomodel.tree.TreeLengthStatistic)1 TreeModel (dr.evomodel.tree.TreeModel)1 MCLogger (dr.inference.loggers.MCLogger)1 TabDelimitedFormatter (dr.inference.loggers.TabDelimitedFormatter)1 MCMC (dr.inference.mcmc.MCMC)1 MCMCOptions (dr.inference.mcmc.MCMCOptions)1 Likelihood (dr.inference.model.Likelihood)1 Parameter (dr.inference.model.Parameter)1 OperatorSchedule (dr.inference.operators.OperatorSchedule)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 FileReader (java.io.FileReader)1