Search in sources :

Example 16 with State

use of beast.core.State in project MultiTypeTree by tgvaughan.

the class STX_NR_MTU_TS_Test method test.

@Test
public void test() throws Exception {
    System.out.println("STX_NR_MTU_TS test");
    // Test passing locally, not on Travis.  WHY!?
    // Fix seed.
    Randomizer.setSeed(53);
    // Assemble migration model:
    RealParameter rateMatrix = new RealParameter("0.1 0.1");
    RealParameter popSizes = new RealParameter("7.0 7.0");
    SCMigrationModel migModel = new SCMigrationModel();
    migModel.initByName("rateMatrix", rateMatrix, "popSizes", popSizes, "typeSet", new TypeSet("A", "B"));
    // Assemble initial MultiTypeTree
    MultiTypeTree mtTree = new StructuredCoalescentMultiTypeTree();
    mtTree.initByName("typeLabel", "deme", "migrationModel", migModel, "leafTypes", "1 1 0 0");
    // Set up state:
    State state = new State();
    state.initByName("stateNode", mtTree);
    // Assemble distribution:
    StructuredCoalescentTreeDensity distribution = new StructuredCoalescentTreeDensity();
    distribution.initByName("migrationModel", migModel, "multiTypeTree", mtTree);
    // Set up operators:
    Operator operatorSTX = new TypedSubtreeExchange();
    operatorSTX.initByName("weight", 1.0, "multiTypeTree", mtTree, "migrationModel", migModel);
    Operator operatorNR = new NodeRetype();
    operatorNR.initByName("weight", 1.0, "multiTypeTree", mtTree, "migrationModel", migModel);
    Operator operatorMTU = new MultiTypeUniform();
    operatorMTU.initByName("weight", 1.0, "migrationModel", migModel, "multiTypeTree", mtTree);
    Operator operatorMTTS = new MultiTypeTreeScale();
    operatorMTTS.initByName("weight", 1.0, "multiTypeTree", mtTree, "migrationModel", migModel, "scaleFactor", 1.5, "useOldTreeScaler", false);
    // Set up stat analysis logger:
    MultiTypeTreeStatLogger logger = new MultiTypeTreeStatLogger();
    logger.initByName("multiTypeTree", mtTree, "burninFrac", 0.1, "logEvery", 1000);
    // Set up MCMC:
    MCMC mcmc = new MCMC();
    mcmc.initByName("chainLength", "1000000", "state", state, "distribution", distribution, "operator", operatorSTX, "operator", operatorNR, "operator", operatorMTU, "operator", operatorMTTS, "logger", logger);
    // Run MCMC:
    mcmc.run();
    System.out.format("height mean = %s\n", logger.getHeightMean());
    System.out.format("height var = %s\n", logger.getHeightVar());
    System.out.format("height ESS = %s\n", logger.getHeightESS());
    // Direct simulation:
    double[] heights = UtilMethods.getSimulatedHeights(migModel, new IntegerParameter("1 1 0 0"));
    double simHeightMean = DiscreteStatistics.mean(heights);
    double simHeightVar = DiscreteStatistics.variance(heights);
    // Compare analysis results with truth:
    boolean withinTol = (logger.getHeightESS() > 500) && (Math.abs(logger.getHeightMean() - simHeightMean) < 2.0) && (Math.abs(logger.getHeightVar() - simHeightVar) < 50);
    Assert.assertTrue(withinTol);
}
Also used : Operator(beast.core.Operator) MultiTypeTreeStatLogger(multitypetree.util.MultiTypeTreeStatLogger) IntegerParameter(beast.core.parameter.IntegerParameter) MCMC(beast.core.MCMC) RealParameter(beast.core.parameter.RealParameter) StructuredCoalescentMultiTypeTree(beast.evolution.tree.StructuredCoalescentMultiTypeTree) MultiTypeTree(beast.evolution.tree.MultiTypeTree) SCMigrationModel(beast.evolution.tree.SCMigrationModel) State(beast.core.State) TypeSet(beast.evolution.tree.TypeSet) StructuredCoalescentTreeDensity(multitypetree.distributions.StructuredCoalescentTreeDensity) StructuredCoalescentMultiTypeTree(beast.evolution.tree.StructuredCoalescentMultiTypeTree) Test(org.junit.Test)

Example 17 with State

use of beast.core.State in project MultiTypeTree by tgvaughan.

the class TWB_TS_Test method test1.

@Test
public void test1() throws Exception {
    System.out.println("TWB_test 1");
    // Fix seed.
    Randomizer.setSeed(42);
    // Assemble initial MultiTypeTree
    String newickStr = "((1[&deme=0]:1,2[&deme=0]:1)[&deme=0]:1," + "3[&deme=0]:2)[&deme=0]:0;";
    MultiTypeTreeFromNewick mtTree = new MultiTypeTreeFromNewick();
    mtTree.initByName("value", newickStr, "typeLabel", "deme");
    // Assemble migration model:
    RealParameter rateMatrix = new RealParameter("0.1 0.1");
    RealParameter popSizes = new RealParameter("7.0 7.0");
    SCMigrationModel migModel = new SCMigrationModel();
    migModel.initByName("rateMatrix", rateMatrix, "popSizes", popSizes, "typeSet", new TypeSet("A", "B"));
    // Assemble distribution:
    StructuredCoalescentTreeDensity distribution = new StructuredCoalescentTreeDensity();
    distribution.initByName("migrationModel", migModel, "multiTypeTree", mtTree);
    // Set up state:
    State state = new State();
    state.initByName("stateNode", mtTree);
    // Set up operator:
    TypedWilsonBalding operatorTWB = new TypedWilsonBalding();
    operatorTWB.initByName("weight", 1.0, "multiTypeTree", mtTree, "migrationModel", migModel, "alpha", 0.2);
    Operator operatorMTTS = new MultiTypeTreeScale();
    operatorMTTS.initByName("weight", 1.0, "multiTypeTree", mtTree, "migrationModel", migModel, "scaleFactor", 0.8, "useOldTreeScaler", false);
    // Set up stat analysis logger:
    MultiTypeTreeStatLogger logger = new MultiTypeTreeStatLogger();
    logger.initByName("multiTypeTree", mtTree, "burninFrac", 0.2, "logEvery", 1000);
    // Set up MCMC:
    MCMC mcmc = new MCMC();
    mcmc.initByName("chainLength", "1000000", "state", state, "distribution", distribution, "operator", operatorTWB, "operator", operatorMTTS, "logger", logger);
    // Run MCMC:
    mcmc.run();
    System.out.format("height mean = %s\n", logger.getHeightMean());
    System.out.format("height var = %s\n", logger.getHeightVar());
    System.out.format("height ESS = %s\n", logger.getHeightESS());
    // Direct simulation:
    double[] heights = UtilMethods.getSimulatedHeights(migModel, new IntegerParameter("0 0 0"));
    double simHeightMean = DiscreteStatistics.mean(heights);
    double simHeightVar = DiscreteStatistics.variance(heights);
    // Compare results with simulation results:
    boolean withinTol = (logger.getHeightESS() > 400) && (Math.abs(logger.getHeightMean() - simHeightMean) < 1.0) && (Math.abs(logger.getHeightVar() - simHeightVar) < 30);
    Assert.assertTrue(withinTol);
}
Also used : Operator(beast.core.Operator) MultiTypeTreeStatLogger(multitypetree.util.MultiTypeTreeStatLogger) IntegerParameter(beast.core.parameter.IntegerParameter) MCMC(beast.core.MCMC) RealParameter(beast.core.parameter.RealParameter) SCMigrationModel(beast.evolution.tree.SCMigrationModel) State(beast.core.State) MultiTypeTreeFromNewick(beast.evolution.tree.MultiTypeTreeFromNewick) TypeSet(beast.evolution.tree.TypeSet) StructuredCoalescentTreeDensity(multitypetree.distributions.StructuredCoalescentTreeDensity) Test(org.junit.Test)

Example 18 with State

use of beast.core.State in project MultiTypeTree by tgvaughan.

the class TWB_TS_Test method testTWB2.

@Test
public void testTWB2() throws Exception {
    System.out.println("TWB_test 2");
    // Fix seed.
    Randomizer.setSeed(42);
    // Assemble initial MultiTypeTree
    String newickStr = "((1[&deme=1]:1,2[&deme=0]:1)[&deme=0]:1," + "3[&deme=0]:2)[&deme=0]:0;";
    MultiTypeTreeFromNewick mtTree = new MultiTypeTreeFromNewick();
    mtTree.initByName("value", newickStr, "typeLabel", "deme");
    // Assemble migration model:
    RealParameter rateMatrix = new RealParameter("0.1 0.1");
    RealParameter popSizes = new RealParameter("7.0 7.0");
    SCMigrationModel migModel = new SCMigrationModel();
    migModel.initByName("rateMatrix", rateMatrix, "popSizes", popSizes, "typeSet", new TypeSet("A", "B"));
    // Assemble distribution:
    StructuredCoalescentTreeDensity distribution = new StructuredCoalescentTreeDensity();
    distribution.initByName("migrationModel", migModel, "multiTypeTree", mtTree);
    // Set up state:
    State state = new State();
    state.initByName("stateNode", mtTree);
    // Set up operator:
    TypedWilsonBalding operatorTWB = new TypedWilsonBalding();
    operatorTWB.initByName("weight", 1.0, "multiTypeTree", mtTree, "migrationModel", migModel, "alpha", 0.2);
    Operator operatorMTTS = new MultiTypeTreeScale();
    operatorMTTS.initByName("weight", 1.0, "multiTypeTree", mtTree, "migrationModel", migModel, "scaleFactor", 0.8, "useOldTreeScaler", false);
    // Set up stat analysis logger:
    MultiTypeTreeStatLogger logger = new MultiTypeTreeStatLogger();
    logger.initByName("multiTypeTree", mtTree, "burninFrac", 0.1, "logEvery", 1000);
    // Set up MCMC:
    MCMC mcmc = new MCMC();
    mcmc.initByName("chainLength", "1000000", "state", state, "distribution", distribution, "operator", operatorTWB, "logger", logger);
    // Run MCMC:
    mcmc.run();
    System.out.format("height mean = %s\n", logger.getHeightMean());
    System.out.format("height var = %s\n", logger.getHeightVar());
    System.out.format("height ESS = %s\n", logger.getHeightESS());
    // Direct simulation:
    double[] heights = UtilMethods.getSimulatedHeights(migModel, new IntegerParameter("1 0 0"));
    double simHeightMean = DiscreteStatistics.mean(heights);
    double simHeightVar = DiscreteStatistics.variance(heights);
    // Compare analysis results with truth:
    boolean withinTol = (logger.getHeightESS() > 400) && (Math.abs(logger.getHeightMean() - simHeightMean) < 1.0) && (Math.abs(logger.getHeightVar() - simHeightVar) < 30);
    Assert.assertTrue(withinTol);
}
Also used : Operator(beast.core.Operator) MultiTypeTreeStatLogger(multitypetree.util.MultiTypeTreeStatLogger) IntegerParameter(beast.core.parameter.IntegerParameter) MCMC(beast.core.MCMC) RealParameter(beast.core.parameter.RealParameter) SCMigrationModel(beast.evolution.tree.SCMigrationModel) State(beast.core.State) MultiTypeTreeFromNewick(beast.evolution.tree.MultiTypeTreeFromNewick) TypeSet(beast.evolution.tree.TypeSet) StructuredCoalescentTreeDensity(multitypetree.distributions.StructuredCoalescentTreeDensity) Test(org.junit.Test)

Example 19 with State

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

the class XMLParser method createBeastObject.

// createObject
/**
 * create BEASTInterface either using Inputs, or using annotated constructor *
 */
private BEASTInterface createBeastObject(Node node, String ID, String clazzName, List<NameValuePair> inputInfo) throws XMLParserException {
    BEASTInterface beastObject = useAnnotatedConstructor(node, ID, clazzName, inputInfo);
    if (beastObject != null) {
        return beastObject;
    }
    // create new instance using class name
    Object o = null;
    try {
        Class<?> c = Class.forName(clazzName);
        o = c.newInstance();
    } catch (InstantiationException e) {
        // created for instance because it is abstract
        throw new XMLParserException(node, "Cannot instantiate class. Please check the spec attribute.", 1006);
    } catch (ClassNotFoundException e) {
    // ignore -- class was found in beastObjectNames before
    } catch (IllegalAccessException e) {
        // T O D O Auto-generated catch block
        e.printStackTrace();
        throw new XMLParserException(node, "Cannot access class. Please check the spec attribute.", 1011);
    }
    // set id
    beastObject = (BEASTInterface) o;
    beastObject.setID(ID);
    // hack required to make log-parsing easier
    if (o instanceof State) {
        m_state = (State) o;
    }
    // process inputs for annotated constructors
    for (NameValuePair pair : inputInfo) {
        if (pair.value instanceof BEASTInterface) {
            setInput(node, beastObject, pair.name, (BEASTInterface) pair.value);
        } else if (pair.value instanceof String) {
            setInput(node, beastObject, pair.name, (String) pair.value);
        } else {
            throw new RuntimeException("Programmer error: value should be String or BEASTInterface");
        }
    }
    // fill in missing inputs, if an input provider is available
    try {
        if (requiredInputProvider != null) {
            for (Input<?> input : beastObject.listInputs()) {
                if (input.get() == null && input.getRule() == Validate.REQUIRED) {
                    Object o2 = requiredInputProvider.createInput(beastObject, input, partitionContext);
                    if (o2 != null) {
                        input.setValue(o2, beastObject);
                    }
                }
            }
        }
    } catch (Exception e) {
        throw new XMLParserException(node, e.getMessage(), 1008);
    }
    // sanity check: all attributes should be valid input names
    if (!(beastObject instanceof Map)) {
        for (NameValuePair pair : inputInfo) {
            String name = pair.name;
            if (!(name.equals("id") || name.equals("idref") || name.equals("spec") || name.equals("name"))) {
                try {
                    beastObject.getInput(name);
                } catch (Exception e) {
                    throw new XMLParserException(node, e.getMessage(), 1009);
                }
            }
        }
    }
    // make sure object o is in outputs of inputs
    for (NameValuePair pair : inputInfo) {
        if (pair.value instanceof BEASTInterface) {
            ((BEASTInterface) pair.value).getOutputs().add((BEASTInterface) o);
        }
    }
    register(node, beastObject);
    return beastObject;
}
Also used : IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) State(beast.core.State) BEASTInterface(beast.core.BEASTInterface) HashMap(java.util.HashMap) Map(beast.core.parameter.Map) NamedNodeMap(org.w3c.dom.NamedNodeMap)

Example 20 with State

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

the class BeautiBase method stateAsString.

String stateAsString() {
    State state = (State) doc.pluginmap.get("state");
    List<StateNode> stateNodes = state.stateNodeInput.get();
    return "assertStateEquals" + pluginListAsString(stateNodes);
}
Also used : State(beast.core.State) StateNode(beast.core.StateNode)

Aggregations

State (beast.core.State)27 RealParameter (beast.core.parameter.RealParameter)14 Test (org.junit.Test)14 MCMC (beast.core.MCMC)10 SCMigrationModel (beast.evolution.tree.SCMigrationModel)9 TypeSet (beast.evolution.tree.TypeSet)9 StructuredCoalescentTreeDensity (multitypetree.distributions.StructuredCoalescentTreeDensity)9 MultiTypeTreeStatLogger (multitypetree.util.MultiTypeTreeStatLogger)9 Operator (beast.core.Operator)7 IntegerParameter (beast.core.parameter.IntegerParameter)6 MultiTypeTreeFromNewick (beast.evolution.tree.MultiTypeTreeFromNewick)6 StateNode (beast.core.StateNode)5 BEASTInterface (beast.core.BEASTInterface)4 MultiTypeTree (beast.evolution.tree.MultiTypeTree)3 StructuredCoalescentMultiTypeTree (beast.evolution.tree.StructuredCoalescentMultiTypeTree)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Map (beast.core.parameter.Map)2 UniformOperator (beast.evolution.operators.UniformOperator)2 SiteModel (beast.evolution.sitemodel.SiteModel)2 IOException (java.io.IOException)2