Search in sources :

Example 11 with Operator

use of beast.core.Operator 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 12 with Operator

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

the class BeautiConnector method isActivated.

/**
 * check that conditions in the 'if' input are met *
 */
public boolean isActivated(PartitionContext partitionContext, List<BEASTInterface> posteriorPredecessors, List<BEASTInterface> likelihoodPredecessors, BeautiDoc doc) {
    if (atInitialisationOnly()) {
        return false;
    }
    if (methodnput.get() != null) {
        // if (method != null) {
        try {
            String fullMethod = methodnput.get();
            String className = fullMethod.substring(0, fullMethod.lastIndexOf('.'));
            String methodName = fullMethod.substring(fullMethod.lastIndexOf('.') + 1);
            Class<?> class_ = Class.forName(className);
            method = class_.getMethod(methodName, BeautiDoc.class);
            method.invoke(null, doc);
        } catch (Exception e) {
        // ignore
        }
    }
    boolean isActive = true;
    for (int i = 0; i < conditionIDs.length; i++) {
        // String id = conditionIDs[i].replaceAll("\\$\\(n\\)", partition);
        String id = BeautiDoc.translatePartitionNames(conditionIDs[i], partitionContext);
        BEASTInterface beastObject = doc.pluginmap.get(id);
        if (beastObject == null) {
            if (conditionOperations[i] != Operation.IS_NOT_AN_OPERTOR) {
                return false;
            }
        // System.err.println("isActivated::no beastObject found");
        }
        // System.err.println("isActivated::found " + id);
        try {
            switch(conditionOperations[i]) {
                case IS_IN_POSTERIOR:
                    if (!posteriorPredecessors.contains(beastObject)) {
                        // System.err.println("isActivated::is not in posterior, return false");
                        return false;
                    }
                    break;
                case IS_IN_LIKELIHOOD:
                    if (!likelihoodPredecessors.contains(beastObject)) {
                        // System.err.println("isActivated::is not in posterior, return false");
                        return false;
                    }
                    break;
                // System.err.println("isActivated::is in posterior");
                case IS_NOT_AN_OPERTOR:
                    List<Operator> operators = ((MCMC) doc.mcmc.get()).operatorsInput.get();
                    if (operators.contains(beastObject)) {
                        return false;
                    }
                    break;
                case EQUALS:
                    final Input<?> input = beastObject.getInput(conditionInputs[i]);
                    // System.err.println("isActivated::input " + input.get().toString() + " expected " + conditionValues[i]);
                    if (input.get() == null) {
                        if (!conditionValues[i].equals("null")) {
                            return false;
                        }
                    } else if (!input.get().toString().equals(conditionValues[i])) {
                        // System.err.println("isActivated::return false");
                        return false;
                    }
                    break;
                case NOT_EQUALS:
                    final Input<?> input2 = beastObject.getInput(conditionInputs[i]);
                    // System.err.println("isActivated::input " + input.get().toString() + " expected " + conditionValues[i]);
                    if (input2.get() == null) {
                        if (conditionValues[i].equals("null")) {
                            return false;
                        }
                    } else if (input2.get().toString().equals(conditionValues[i])) {
                        // System.err.println("isActivated::return false");
                        return false;
                    }
                    break;
                default:
                    throw new IllegalArgumentException("Unexpected operation: " + conditionOperations[i]);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    // }
    return isActive;
}
Also used : Operator(beast.core.Operator) BEASTInterface(beast.core.BEASTInterface)

Example 13 with Operator

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

the class BeautiBase method assertOperatorsEqual.

void assertOperatorsEqual(String... ids) {
    System.err.println("assertOperatorsEqual");
    MCMC mcmc = (MCMC) doc.mcmc.get();
    List<Operator> operators = mcmc.operatorsInput.get();
    asserListsEqual(operators, ids);
}
Also used : Operator(beast.core.Operator) MCMC(beast.core.MCMC)

Example 14 with Operator

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

the class ClockModelListInputEditor method init.

@Override
public void init(Input<?> input, BEASTInterface beastObject, int itemNr, ExpandOption isExpandOption, boolean addButtons) {
    fixMeanRatesCheckBox = new JCheckBox("Fix mean rate of clock models");
    m_buttonStatus = ButtonStatus.NONE;
    super.init(input, beastObject, itemNr, isExpandOption, addButtons);
    List<Operator> operators = ((MCMC) doc.mcmc.get()).operatorsInput.get();
    fixMeanRatesCheckBox.addActionListener(e -> {
        JCheckBox averageRatesBox = (JCheckBox) e.getSource();
        boolean averageRates = averageRatesBox.isSelected();
        List<Operator> operators2 = ((MCMC) doc.mcmc.get()).operatorsInput.get();
        if (averageRates) {
            // connect DeltaExchangeOperator
            if (!operators2.contains(operator)) {
                operators2.add(operator);
            }
            // set up relative weights
            setUpOperator();
        } else {
            operators2.remove(operator);
            fixMeanRatesValidateLabel.setVisible(false);
            repaint();
        }
    });
    operator = (DeltaExchangeOperator) doc.pluginmap.get("FixMeanRatesOperator");
    if (operator == null) {
        operator = new DeltaExchangeOperator();
        try {
            operator.setID("FixMeanRatesOperator");
            operator.initByName("weight", 2.0, "delta", 0.75);
        } catch (Exception e1) {
        // ignore initAndValidate exception
        }
        doc.addPlugin(operator);
    }
    fixMeanRatesCheckBox.setSelected(operators.contains(operator));
    Box box = Box.createHorizontalBox();
    box.add(fixMeanRatesCheckBox);
    box.add(Box.createHorizontalGlue());
    fixMeanRatesValidateLabel = new SmallLabel("x", Color.GREEN);
    fixMeanRatesValidateLabel.setVisible(false);
    box.add(fixMeanRatesValidateLabel);
    if (((List<?>) input.get()).size() > 1 && operator != null) {
        add(box);
    }
    setUpOperator();
}
Also used : JCheckBox(javax.swing.JCheckBox) Operator(beast.core.Operator) DeltaExchangeOperator(beast.evolution.operators.DeltaExchangeOperator) SmallLabel(beast.app.draw.SmallLabel) Box(javax.swing.Box) JCheckBox(javax.swing.JCheckBox) ArrayList(java.util.ArrayList) List(java.util.List) DeltaExchangeOperator(beast.evolution.operators.DeltaExchangeOperator)

Aggregations

Operator (beast.core.Operator)14 MCMC (beast.core.MCMC)9 State (beast.core.State)7 RealParameter (beast.core.parameter.RealParameter)7 SCMigrationModel (beast.evolution.tree.SCMigrationModel)7 TypeSet (beast.evolution.tree.TypeSet)7 StructuredCoalescentTreeDensity (multitypetree.distributions.StructuredCoalescentTreeDensity)7 MultiTypeTreeStatLogger (multitypetree.util.MultiTypeTreeStatLogger)7 Test (org.junit.Test)7 MultiTypeTreeFromNewick (beast.evolution.tree.MultiTypeTreeFromNewick)4 BEASTInterface (beast.core.BEASTInterface)3 IntegerParameter (beast.core.parameter.IntegerParameter)3 MultiTypeTree (beast.evolution.tree.MultiTypeTree)3 StructuredCoalescentMultiTypeTree (beast.evolution.tree.StructuredCoalescentMultiTypeTree)3 List (java.util.List)3 ArrayList (java.util.ArrayList)2 Box (javax.swing.Box)2 JCheckBox (javax.swing.JCheckBox)2 PartitionContext (beast.app.beauti.PartitionContext)1 SmallLabel (beast.app.draw.SmallLabel)1