Search in sources :

Example 6 with BooleanParameter

use of beast.core.parameter.BooleanParameter in project beast2 by CompEvol.

the class CompoundPopulationFunction method prepare.

// why do we need this additional level on top of initAndValidate - does not seem to do anything?
@Override
public void prepare() {
    getParams();
    // is that safe???
    trees = treesInput.get();
    useMid = useMiddleInput.get();
    // used to work without upper case ???
    // errors?
    type = Type.valueOf(demographicTypeInput.get().toUpperCase());
    // set lengths
    int events = 0;
    for (TreeIntervals ti : trees) {
        // number of coalescent events
        events += ti.treeInput.get().getLeafNodeCount() - 1;
    }
    // all trees share time 0, need fixing for serial data
    events += type == Type.STEPWISE ? 0 : 1;
    try {
        if (popSizeParameter.getDimension() != events) {
            final RealParameter p = new RealParameter();
            p.initByName("value", popSizeParameter.getValue() + "", "upper", popSizeParameter.getUpper(), "lower", popSizeParameter.getLower(), "dimension", events);
            p.setID(popSizeParameter.getID());
            popSizeParameter.assignFromWithoutID(p);
        }
        if (indicatorsParameter.getDimension() != events - 1) {
            final BooleanParameter p = new BooleanParameter();
            p.initByName("value", "" + indicatorsParameter.getValue(), "dimension", events - 1);
            p.setID(indicatorsParameter.getID());
            indicatorsParameter.assignFrom(p);
        }
    } catch (Exception e) {
        // what to do?
        e.printStackTrace();
    }
    initInternals();
    for (int nt = 0; nt < trees.size(); ++nt) {
        setTreeTimes(nt);
    }
    mergeTreeTimes();
    setDemographicArrays();
    shadow = new Shadow();
}
Also used : RealParameter(beast.core.parameter.RealParameter) BooleanParameter(beast.core.parameter.BooleanParameter)

Example 7 with BooleanParameter

use of beast.core.parameter.BooleanParameter in project beast2 by CompEvol.

the class SampleOffValues method proposal.

@Override
public double proposal() {
    final BooleanParameter indicators = indicatorsInput.get(this);
    final RealParameter data = valuesInput.get(this);
    final ParametricDistribution distribution = distInput.get();
    final int idim = indicators.getDimension();
    final int offset = (data.getDimension() - 1) == idim ? 1 : 0;
    assert offset == 1 || data.getDimension() == idim : "" + idim + " (?+1) != " + data.getDimension();
    double hr = Double.NEGATIVE_INFINITY;
    if (scaleAll.get()) {
        for (int i = offset; i < idim; ++i) {
            if (!indicators.getValue(i - offset)) {
                try {
                    final double val = distribution.inverseCumulativeProbability(Randomizer.nextDouble());
                    hr += distribution.logDensity(data.getValue(i));
                    data.setValue(i, val);
                } catch (Exception e) {
                    // some distributions fail on extreme values - currently gamma
                    return Double.NEGATIVE_INFINITY;
                }
            }
        }
    } else {
        // available locations for direct sampling
        int[] loc = new int[idim];
        int locIndex = 0;
        for (int i = 0; i < idim; ++i) {
            if (!indicators.getValue(i)) {
                loc[locIndex] = i + offset;
                ++locIndex;
            }
        }
        if (locIndex > 0) {
            final int index = loc[Randomizer.nextInt(locIndex)];
            try {
                final double val = distribution.inverseCumulativeProbability(Randomizer.nextDouble());
                hr = distribution.logDensity(data.getValue(index));
                data.setValue(index, val);
            } catch (Exception e) {
                // some distributions fail on extreme values - currently gamma
                return Double.NEGATIVE_INFINITY;
            // throw new OperatorFailedException(e.getMessage());
            }
        } else {
        // no non-active indicators
        // return Double.NEGATIVE_INFINITY;
        }
    }
    return hr;
}
Also used : ParametricDistribution(beast.math.distributions.ParametricDistribution) RealParameter(beast.core.parameter.RealParameter) BooleanParameter(beast.core.parameter.BooleanParameter)

Example 8 with BooleanParameter

use of beast.core.parameter.BooleanParameter in project beast2 by CompEvol.

the class Sum method initAndValidate.

@Override
public void initAndValidate() {
    List<Function> valuable = functionInput.get();
    mode = Mode.integer_mode;
    for (Function v : valuable) {
        if (!(v instanceof IntegerParameter || v instanceof BooleanParameter)) {
            mode = Mode.double_mode;
        }
    }
}
Also used : Function(beast.core.Function) IntegerParameter(beast.core.parameter.IntegerParameter) BooleanParameter(beast.core.parameter.BooleanParameter)

Example 9 with BooleanParameter

use of beast.core.parameter.BooleanParameter in project beast2 by CompEvol.

the class RandomLocalClockModel method initAndValidate.

@Override
public void initAndValidate() {
    m_tree = treeInput.get();
    BooleanParameter indicators = indicatorParamInput.get();
    if (indicators.getDimension() != m_tree.getNodeCount() - 1) {
        Log.warning.println("RandomLocalClockModel::Setting dimension of indicators to " + (m_tree.getNodeCount() - 1));
        indicators.setDimension(m_tree.getNodeCount() - 1);
    }
    unscaledBranchRates = new double[m_tree.getNodeCount()];
    RealParameter rates = rateParamInput.get();
    if (rates.lowerValueInput.get() == null || rates.lowerValueInput.get() < 0.0) {
        rates.setLower(0.0);
    }
    if (rates.upperValueInput.get() == null || rates.upperValueInput.get() < 0.0) {
        rates.setUpper(Double.MAX_VALUE);
    }
    if (rates.getDimension() != m_tree.getNodeCount() - 1) {
        Log.warning.println("RandomLocalClockModel::Setting dimension of rates to " + (m_tree.getNodeCount() - 1));
        rates.setDimension(m_tree.getNodeCount() - 1);
    }
    ratesAreMultipliers = ratesAreMultipliersInput.get();
    meanRate = meanRateInput.get();
    if (meanRate == null) {
        meanRate = new RealParameter("1.0");
    }
}
Also used : RealParameter(beast.core.parameter.RealParameter) BooleanParameter(beast.core.parameter.BooleanParameter)

Example 10 with BooleanParameter

use of beast.core.parameter.BooleanParameter in project beast2 by CompEvol.

the class BooleanParameterListTest method test1.

@Test
public void test1() throws Exception {
    BooleanParameterList parameterList = new BooleanParameterList();
    // Parameters with which to initialise list
    BooleanParameter param1 = new BooleanParameter();
    param1.initByName("value", "true false true");
    BooleanParameter param2 = new BooleanParameter();
    param2.initByName("value", "false true false");
    // Initialise parameter list
    parameterList.initByName("dimension", 3, "initialParam", param1, "initialParam", param2);
    // Create dummy state to allow statenode editing
    State state = new State();
    state.initByName("stateNode", parameterList);
    state.initialise();
    // Test parameter value modification
    parameterList.get(0).setValue(0, false);
    // Test parameter creation and modification
    Parameter<Boolean> newParam = parameterList.addNewParam();
    newParam.setValue(0, true);
    newParam.setValue(1, true);
    newParam.setValue(2, true);
    assertTrue(parameterList.get(0).getValue(0) == false);
    assertTrue(parameterList.get(0).getValue(1) == false);
    assertTrue(parameterList.get(0).getValue(2) == true);
    assertTrue(parameterList.get(0).getKey() == 0);
    assertTrue(parameterList.get(1).getValue(0) == false);
    assertTrue(parameterList.get(1).getValue(1) == true);
    assertTrue(parameterList.get(1).getValue(2) == false);
    assertTrue(parameterList.get(1).getKey() == 1);
    assertTrue(parameterList.get(2).getValue(0) == true);
    assertTrue(parameterList.get(2).getValue(1) == true);
    assertTrue(parameterList.get(2).getValue(2) == true);
    assertTrue(parameterList.get(2).getKey() == 2);
    assertTrue(parameterList.size() == 3);
    parameterList.remove(1);
    newParam = parameterList.addNewParam();
    newParam.setValue(0, false);
    newParam.setValue(1, false);
    newParam.setValue(2, false);
    assertTrue(parameterList.get(0).getValue(0) == false);
    assertTrue(parameterList.get(0).getValue(1) == false);
    assertTrue(parameterList.get(0).getValue(2) == true);
    assertTrue(parameterList.get(0).getKey() == 0);
    assertTrue(parameterList.get(1).getValue(0) == true);
    assertTrue(parameterList.get(1).getValue(1) == true);
    assertTrue(parameterList.get(1).getValue(2) == true);
    assertTrue(parameterList.get(1).getKey() == 2);
    assertTrue(parameterList.get(2).getValue(0) == false);
    assertTrue(parameterList.get(2).getValue(1) == false);
    assertTrue(parameterList.get(2).getValue(2) == false);
    assertTrue(parameterList.get(2).getKey() == 1);
    assertTrue(parameterList.size() == 3);
    // Test state restore
    parameterList.restore();
    assertTrue(parameterList.get(0).getValue(0) == true);
    assertTrue(parameterList.get(0).getValue(1) == false);
    assertTrue(parameterList.get(0).getValue(2) == true);
    assertTrue(parameterList.get(0).getKey() == 0);
    assertTrue(parameterList.get(1).getValue(0) == false);
    assertTrue(parameterList.get(1).getValue(1) == true);
    assertTrue(parameterList.get(1).getValue(2) == false);
    assertTrue(parameterList.get(1).getKey() == 1);
    assertTrue(parameterList.size() == 2);
    // Test serialization
    parameterList.addNewParam(newParam);
    String xmlStr = parameterList.toXML();
    assertEquals(xmlStr, "<statenode id='null'>" + "Dimension: [3, 1], " + "Bounds: [false,true], " + "AvailableKeys: [], " + "NextKey: 3, " + "Parameters: [[true,false,true],[false,true,false],[false,false,false]], " + "ParameterKeys: [0,1,2]" + "</statenode>\n");
    // Test deserialization
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    Document doc = factory.newDocumentBuilder().parse(new ByteArrayInputStream(xmlStr.getBytes()));
    doc.normalize();
    NodeList nodes = doc.getElementsByTagName("*");
    org.w3c.dom.Node docNode = nodes.item(0);
    BooleanParameterList newParameterList = new BooleanParameterList();
    newParameterList.initAndValidate();
    newParameterList.fromXML(docNode);
    assertTrue(newParameterList.get(0).getValue(0) == true);
    assertTrue(newParameterList.get(0).getValue(1) == false);
    assertTrue(newParameterList.get(0).getValue(2) == true);
    assertTrue(newParameterList.get(0).getKey() == 0);
    assertTrue(newParameterList.get(1).getValue(0) == false);
    assertTrue(newParameterList.get(1).getValue(1) == true);
    assertTrue(newParameterList.get(1).getValue(2) == false);
    assertTrue(newParameterList.get(1).getKey() == 1);
    assertTrue(newParameterList.get(2).getValue(0) == false);
    assertTrue(newParameterList.get(2).getValue(1) == false);
    assertTrue(newParameterList.get(2).getValue(2) == false);
    assertTrue(newParameterList.get(2).getKey() == 2);
    assertTrue(newParameterList.size() == 3);
}
Also used : BooleanParameterList(beast.core.parameter.BooleanParameterList) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document) BooleanParameter(beast.core.parameter.BooleanParameter) ByteArrayInputStream(java.io.ByteArrayInputStream) State(beast.core.State) Test(org.junit.Test)

Aggregations

BooleanParameter (beast.core.parameter.BooleanParameter)10 RealParameter (beast.core.parameter.RealParameter)6 IntegerParameter (beast.core.parameter.IntegerParameter)2 Node (beast.evolution.tree.Node)2 Test (org.junit.Test)2 Function (beast.core.Function)1 State (beast.core.State)1 BooleanParameterList (beast.core.parameter.BooleanParameterList)1 Sum (beast.core.util.Sum)1 Tree (beast.evolution.tree.Tree)1 ParametricDistribution (beast.math.distributions.ParametricDistribution)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 Document (org.w3c.dom.Document)1 NodeList (org.w3c.dom.NodeList)1