<commit_before>package dr.evomodel.sitemodel;

import dr.inference.model.*;
import dr.inference.distribution.ParametricDistributionModel;

/**
 * @author <NAME>
 *
 * This class models the relative loci rates using the given discritized parametric distribution. 
 */
public class DiscretizedLociRates extends AbstractModel {

    private CompoundParameter lociRates;
    private Parameter rateCategoryParameter;
    private ParametricDistributionModel distrModel;
    private double normalizeRateTo;
    private double[] rates;
    private boolean normalize;
    private int categoryCount;
    private double scaleFactor;


    public DiscretizedLociRates(
            CompoundParameter lociRates,
            Parameter rateCategoryParameter,
            ParametricDistributionModel model,
            boolean normalize,
            double normalizeLociRateTo,

            int categoryCount) {
        super("DiscretizedLociRatesModel");
        this.lociRates = lociRates;

        this.rateCategoryParameter = rateCategoryParameter;
        //Force the boundaries of rateCategoryParameter to match the category count
        Parameter.DefaultBounds bound = new Parameter.DefaultBounds(categoryCount - 1, 0, rateCategoryParameter.getDimension());
        this.rateCategoryParameter.addBounds(bound);
        
        this.distrModel = model;
        this.normalizeRateTo = normalizeLociRateTo;
        this.normalize = normalize;
        this.categoryCount = categoryCount;
        rates = new double[categoryCount];
        setupRates();


        addModel(distrModel);
        addVariable(this.rateCategoryParameter);

    }

    private void setupRates(){
        double categoryIntervalSize = 1.0/categoryCount;
        for(int i = 0; i < categoryCount; i++){
            rates[i]= distrModel.quantile((i+0.5)*categoryIntervalSize);
        }

        if(normalize){
           computeFactor();
        }
        int lociCount = rateCategoryParameter.getDimension();
        for(int i = 0; i < lociCount; i ++){
            lociRates.setParameterValue(i,rates[(int)rateCategoryParameter.getParameterValue(i)]*scaleFactor);
        }

    }
    public void handleModelChangedEvent(Model model, Object object, int index) {
        if (model == distrModel) {
            setupRates();
            fireModelChanged();
        }
    }

    protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
        setupRates();
    }

    protected void storeState() {
    }
    protected void acceptState() {
    }
    protected void restoreState() {
        setupRates();
    }
    private void computeFactor(){
        double sumRates = 0.0;
        int lociCount = rateCategoryParameter.getDimension();
        for(int i = 0; i < lociCount; i++){
            sumRates += rates[(int)rateCategoryParameter.getParameterValue(i)];
        }
        scaleFactor = normalizeRateTo/(sumRates/lociCount);
    }

}
<commit_msg>Prevent repetitive rate calculations.<commit_after>package dr.evomodel.sitemodel;

import dr.inference.model.*;
import dr.inference.distribution.ParametricDistributionModel;

/**
 * @author <NAME>
 *
 * This class models the relative loci rates using the given discritized parametric distribution. 
 */
public class DiscretizedLociRates extends AbstractModel {

    private CompoundParameter lociRates;
    private Parameter rateCategoryParameter;
    private ParametricDistributionModel distrModel;
    private double normalizeRateTo;
    private double[] rates;
    private boolean normalize;
    private int categoryCount;
    private double scaleFactor;
    private boolean completeSetup;


    public DiscretizedLociRates(
            CompoundParameter lociRates,
            Parameter rateCategoryParameter,
            ParametricDistributionModel model,
            boolean normalize,
            double normalizeLociRateTo,

            int categoryCount) {
        super("DiscretizedLociRatesModel");
        this.lociRates = lociRates;

        this.rateCategoryParameter = rateCategoryParameter;
        //Force the boundaries of rateCategoryParameter to match the category count
        Parameter.DefaultBounds bound = new Parameter.DefaultBounds(categoryCount - 1, 0, rateCategoryParameter.getDimension());
        this.rateCategoryParameter.addBounds(bound);
        
        this.distrModel = model;
        this.normalizeRateTo = normalizeLociRateTo;
        this.normalize = normalize;
        this.categoryCount = categoryCount;
        rates = new double[categoryCount];
        completeSetup = true;
        setupRates();


        addModel(distrModel);
        addVariable(this.rateCategoryParameter);

    }

    private void setupRates(){
        if(completeSetup){

            double categoryIntervalSize = 1.0/categoryCount;
            for(int i = 0; i < categoryCount; i++){
                rates[i]= distrModel.quantile((i+0.5)*categoryIntervalSize);
            }
        }
        if(normalize){
           computeFactor();
        }

        completeSetup = false;
        int lociCount = rateCategoryParameter.getDimension();
        for(int i = 0; i < lociCount; i ++){
            lociRates.setParameterValue(i,rates[(int)rateCategoryParameter.getParameterValue(i)]*scaleFactor);
        }

    }
    public void handleModelChangedEvent(Model model, Object object, int index) {
        if (model == distrModel) {
            completeSetup = true;
            setupRates();
            fireModelChanged();
        }else if (model == rateCategoryParameter) {
            //if just the rate categories have changed the rates will be the same
            setupRates();
            fireModelChanged(null, index);
        }

    }

    protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
        setupRates();
    }

    protected void storeState() {
    }
    protected void acceptState() {
    }
    protected void restoreState() {
        setupRates();
    }
    private void computeFactor(){
        double sumRates = 0.0;
        int lociCount = rateCategoryParameter.getDimension();
        for(int i = 0; i < lociCount; i++){
            sumRates += rates[(int)rateCategoryParameter.getParameterValue(i)];
        }
        scaleFactor = normalizeRateTo/(sumRates/lociCount);
    }

}

{'fraction_non_alphanumeric': 0.08058103975535168, 'fraction_numerical': 0.003363914373088685, 'mean_word_length': 2.4014560582423297, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '6714722', 'n_tokens_mistral': 1727, 'n_tokens_neox': 1598, 'n_words': 391}