<commit_before>/*
 * ArbitraryBranchRatesParser.java
 *
 * Copyright (c) 2002-2015 <NAME>, <NAME> and <NAME>
 *
 * This file is part of BEAST.
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership and licensing.
 *
 * BEAST is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 *  BEAST is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with BEAST; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */

package dr.evomodelxml.branchratemodel;

import dr.evomodel.branchratemodel.ArbitraryBranchRates;
import dr.evomodel.branchratemodel.BranchSpecificFixedEffects;
import dr.evomodel.tree.TreeModel;
import dr.inference.model.Parameter;
import dr.math.MathUtils;
import dr.xml.*;

import java.util.logging.Logger;

import static dr.evomodel.branchratemodel.ArbitraryBranchRates.make;

/**
 */
public class ArbitraryBranchRatesParser extends AbstractXMLObjectParser {

    public static final String ARBITRARY_BRANCH_RATES = "arbitraryBranchRates";
    private static final String RATES = "rates";
    static final String RECIPROCAL = "reciprocal";
    static final String EXP = "exp";
    private static final String MULTIPLIER = "multiplier";
    private static final String CENTER_AT_ONE = "centerAtOne";
    private static final String RANDOMIZE_RATES = "randomizeRates";
    private static final String RANDOM_SCALE = "randomScale";
    static final String LOCATION = "location";
    static final String SCALE = "scale";

    public String getParserName() {
        return ARBITRARY_BRANCH_RATES;
    }

    public Object parseXMLObject(XMLObject xo) throws XMLParseException {

        TreeModel tree = (TreeModel) xo.getChild(TreeModel.class);

        XMLObject cxo = xo.getChild(RATES);

        Parameter rateCategoryParameter = (Parameter) cxo.getChild(Parameter.class);

        boolean centerAtOne = xo.getAttribute(CENTER_AT_ONE, true);

        boolean randomizeRates = xo.getAttribute(RANDOMIZE_RATES, false);

        if(centerAtOne && randomizeRates) {
            throw new XMLParseException("Cannot centerAtOne and randomize the starting rates");
        }

        final int numBranches = tree.getNodeCount() - 1;
        if (rateCategoryParameter.getDimension() > 1 && (rateCategoryParameter.getDimension() != numBranches)) {
            throw new XMLParseException("Incorrect number of rate parameters");
        }

        if (rateCategoryParameter.getDimension() != numBranches) {
            rateCategoryParameter.setDimension(numBranches);
        }

        Logger.getLogger("dr.evomodel").info("\nUsing an scaled mixture of normals model.");
        Logger.getLogger("dr.evomodel").info("  rates = " + rateCategoryParameter.getDimension());
        Logger.getLogger("dr.evomodel").info("  NB: Make sure you have a prior on "
                + rateCategoryParameter.getId());


        ArbitraryBranchRates.BranchRateTransform transform = parseTransform(xo);

        double scale = xo.getAttribute(RANDOM_SCALE, 1.0);
        if (randomizeRates) {
            for (int i = 0; i < rateCategoryParameter.getDimension(); i++) {
                rateCategoryParameter.setValue(i, Math.exp(MathUtils.nextGaussian() * scale));
            }
        }

        return new ArbitraryBranchRates(tree, rateCategoryParameter, transform, centerAtOne);
    }

    //************************************************************************
    // AbstractXMLObjectParser implementation
    //************************************************************************

    public String getParserDescription() {
        return "This element returns an arbitrary rate model." +
                "The branch rates are drawn from an arbitrary distribution determine by the prior.";
    }

    static ArbitraryBranchRates.BranchRateTransform parseTransform (XMLObject xo) throws XMLParseException {

        boolean reciprocal = xo.getAttribute(RECIPROCAL, false);
        Logger.getLogger("dr.evomodel").info("  reciprocal = " + reciprocal);<commit_msg>mask randomize all rates (no arbitrarily bad increments)<commit_after>/*
 * ArbitraryBranchRatesParser.java
 *
 * Copyright (c) 2002-2015 <NAME>, <NAME> and <NAME>
 *
 * This file is part of BEAST.
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership and licensing.
 *
 * BEAST is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 *  BEAST is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with BEAST; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */

package dr.evomodelxml.branchratemodel;

import dr.evomodel.branchratemodel.ArbitraryBranchRates;
import dr.evomodel.branchratemodel.BranchSpecificFixedEffects;
import dr.evomodel.tree.TreeModel;
import dr.inference.model.Parameter;
import dr.math.MathUtils;
import dr.xml.*;

import java.util.logging.Logger;

import static dr.evomodel.branchratemodel.ArbitraryBranchRates.make;

/**
 */
public class ArbitraryBranchRatesParser extends AbstractXMLObjectParser {

    public static final String ARBITRARY_BRANCH_RATES = "arbitraryBranchRates";
    private static final String RATES = "rates";
    static final String RECIPROCAL = "reciprocal";
    static final String EXP = "exp";
    private static final String MULTIPLIER = "multiplier";
    private static final String CENTER_AT_ONE = "centerAtOne";
    private static final String RANDOMIZE_RATES = "randomizeRates";
    private static final String RANDOM_SCALE = "randomScale";
    private static final String RANDOM_INDICATOR = "randomIndicator"; // keep some rates fixed but randomize others
    static final String LOCATION = "location";
    static final String SCALE = "scale";

    public String getParserName() {
        return ARBITRARY_BRANCH_RATES;
    }

    public Object parseXMLObject(XMLObject xo) throws XMLParseException {

        TreeModel tree = (TreeModel) xo.getChild(TreeModel.class);

        XMLObject cxo = xo.getChild(RATES);

        Parameter rateCategoryParameter = (Parameter) cxo.getChild(Parameter.class);

        boolean centerAtOne = xo.getAttribute(CENTER_AT_ONE, true);

        boolean randomizeRates = xo.getAttribute(RANDOMIZE_RATES, false);

        if(centerAtOne && randomizeRates) {
            throw new XMLParseException("Cannot centerAtOne and randomize the starting rates");
        }

        final int numBranches = tree.getNodeCount() - 1;
        if (rateCategoryParameter.getDimension() > 1 && (rateCategoryParameter.getDimension() != numBranches)) {
            throw new XMLParseException("Incorrect number of rate parameters");
        }

        if (rateCategoryParameter.getDimension() != numBranches) {
            rateCategoryParameter.setDimension(numBranches);
        }

        Parameter randomIndicator;

        if(xo.hasChildNamed(RANDOM_INDICATOR)) {
            randomIndicator = (Parameter) xo.getElementFirstChild(RANDOM_INDICATOR);

            if(!randomizeRates) {
                throw new XMLParseException("Cannot provide indicator for randomized rates without randomizeRates=true");
            }

            if (randomIndicator.getDimension() != rateCategoryParameter.getDimension()) {
                throw new XMLParseException("randomIndicator (" + randomIndicator.getDimension()
                        + ") must be the same dimension as the rate parameter (" + rateCategoryParameter.getDimension() + ")");
            }
        }
        else {
            randomIndicator = new Parameter.Default(rateCategoryParameter.getDimension());
        }

        Logger.getLogger("dr.evomodel").info("\nUsing an scaled mixture of normals model.");
        Logger.getLogger("dr.evomodel").info("  rates = " + rateCategoryParameter.getDimension());
        Logger.getLogger("dr.evomodel").info("  NB: Make sure you have a prior on "
                + rateCategoryParameter.getId());


        ArbitraryBranchRates.BranchRateTransform transform = parseTransform(xo);

        double scale = xo.getAttribute(RANDOM_SCALE, 1.0);
        if (randomizeRates) {
            for (int i = 0; i < rateCategoryParameter.getDimension(); i++) {
                if(randomIndicator.getParameterValue(i) == 1.0) {
                    rateCategoryParameter.setValue(i, Math.exp(MathUtils.nextGaussian() * scale));
                }
            }
        }

        return new ArbitraryBranchRates(tree, rateCategoryParameter, transform, centerAtOne);
    }

    //************************************************************************
    // AbstractXMLObjectParser implementation
    //************************************************************************

    public String getParserDescription() {
        return "This element returns an arbitrary rate model." +
                "The branch rates are drawn from an arbitrary distribution determine by the prior.";
    }

    static ArbitraryBranchRates.BranchRateTransform parseTransform (XMLObject xo) throws XMLParseException {

        boolean reciprocal = xo.getAttribute(RECIPROCAL, false);
        Logger.getLogger("dr.evomodel").info("  reciprocal = " + reciprocal);
{'fraction_non_alphanumeric': 0.11084835944428023, 'fraction_numerical': 0.005123657503202286, 'mean_word_length': 3.48519664162616, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2276619', 'n_tokens_mistral': 2759, 'n_tokens_neox': 2635, 'n_words': 801}