<commit_before>/*
 * Utils.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.app.bss;

import java.awt.Desktop;
import java.awt.Frame;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.Charset;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;

import dr.evolution.datatype.HiddenDataType;
import dr.evolution.tree.TreeUtils;
import org.apache.commons.math.random.MersenneTwister;

import dr.app.bss.test.AncestralSequenceTrait;
import dr.evolution.datatype.Codons;
import dr.evolution.datatype.DataType;
import dr.evolution.io.Importer.ImportException;
import dr.evolution.io.NewickImporter;
import dr.evolution.io.NexusImporter;
import dr.evolution.sequence.Sequence;
import dr.evolution.tree.NodeRef;
import dr.evolution.tree.Tree;
import dr.evolution.tree.TreeTraitProvider;
import dr.evolution.util.MutableTaxonList;
import dr.evolution.util.Taxa;
import dr.evolution.util.Taxon;
import dr.evomodel.tree.TreeModel;
import dr.inference.model.Parameter;
import dr.math.MathUtils;

/**
 * @author <NAME>
 * @version $Id$
 */
public class Utils {

	// ////////////////////
	// ---THREAD UTILS---//
	// ////////////////////

	public static void sleep(int seconds) {
		try {

			Thread.sleep(seconds * 1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}// END: sleep
	
	// ////////////////////////////////
	// ---RANDOM NUMB3R GENERATION---//
	// ////////////////////////////////
	
	private static MersenneTwister random = new MersenneTwister(
			MathUtils.nextLong());

    public static double rLogNormal(double stdev, double mean) {
    	
    	double rNorm = random.nextGaussian() * stdev + mean;
    	double rLognormal = Math.exp(rNorm);
    	
    	return rLognormal;
	}// END: drawRandom
	
	public static int rMultinom(double[] probabilities) {

		int range = probabilities.length + 1;
		double[] distribution = new double[range];
		double sumProb = 0;

		for (double value : probabilities) {
			sumProb += value;
		}// END: probabilities loop

		distribution[0] = 0;
		for (int i = 1; i < range; ++i) {

			distribution[i] = distribution[i - 1]
					+ (probabilities[i - 1] / sumProb);

		}// END: i loop

		distribution[range - 1] = 1.0;

		double key = random.nextDouble();

		int mindex = 1;
		int maxdex = range - 1;
		int midpoint = mindex + (maxdex - mindex) / 2;
		while (mindex <= maxdex) {

			if (key < distribution[midpoint - 1]) {
				
				maxdex = midpoint - 1;
				
			} else if (key > distribution[midpoint]) {
				
				mindex = midpoint + 1;
				
			} else {
				
				return midpoint - 1;

			}
			
			midpoint = mindex + (int) Math.ceil((maxdex - mindex) / 2);

		}//END: mindex loop
		
		System.out.println("Error in rMultinom!");
		
		return range - 1;
	}//END: rMultinom
    
	// ////////////
	// ---MATH---//
	// ////////////
    
	public static int sample(double[] probabilities) {

		int samplePos = -Integer.MAX_VALUE;
		double cumProb = 0.0;
		double u = random.nextDouble();

		for (int i = 0; i < probabilities.length; i++) {

			cumProb += probabilities[i];

			if (u <= cumProb) {
				samplePos = i;
				break;
			}
		}

		return samplePos;
	}// END: randomChoicePDF
	
    public static void rescale(double[] logX) {
        double max = max(logX);
        for (int i = 0; i < logX.length; i++) {
            logX[i] -= max;
		}
	}// END: rescale

	public double getParameterVariance(Parameter param) {
		
		int n = param.getSize();
		double mean = getParameterMean(param);
		double var = 0;
		
		for (int i = 0; i < n; i++) {

			var+= Math.pow( (param.getValue(i) - mean), 2);
<commit_msg>no need for a second random generator<commit_after>/*
 * Utils.java
 *
 * Copyright (c) 2002-2020 <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.app.bss;

import java.awt.Desktop;
import java.awt.Frame;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.Charset;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;

import dr.evolution.datatype.HiddenDataType;
import dr.evolution.tree.TreeUtils;

import dr.app.bss.test.AncestralSequenceTrait;
import dr.evolution.datatype.Codons;
import dr.evolution.datatype.DataType;
import dr.evolution.io.Importer.ImportException;
import dr.evolution.io.NewickImporter;
import dr.evolution.io.NexusImporter;
import dr.evolution.sequence.Sequence;
import dr.evolution.tree.NodeRef;
import dr.evolution.tree.Tree;
import dr.evolution.tree.TreeTraitProvider;
import dr.evolution.util.MutableTaxonList;
import dr.evolution.util.Taxa;
import dr.evolution.util.Taxon;
import dr.evomodel.tree.TreeModel;
import dr.inference.model.Parameter;
import dr.math.MathUtils;

/**
 * @author <NAME>
 * @version $Id$
 */
public class Utils {

	// ////////////////////
	// ---THREAD UTILS---//
	// ////////////////////

	public static void sleep(int seconds) {
		try {

			Thread.sleep(seconds * 1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}// END: sleep
	
	// ////////////////////////////////
	// ---RANDOM NUMB3R GENERATION---//
	// ////////////////////////////////

    public static double rLogNormal(double stdev, double mean) {
    	
    	double rNorm = MathUtils.nextGaussian() * stdev + mean;
    	double rLognormal = Math.exp(rNorm);
    	
    	return rLognormal;
	}// END: drawRandom
	
	public static int rMultinom(double[] probabilities) {

		int range = probabilities.length + 1;
		double[] distribution = new double[range];
		double sumProb = 0;

		for (double value : probabilities) {
			sumProb += value;
		}// END: probabilities loop

		distribution[0] = 0;
		for (int i = 1; i < range; ++i) {

			distribution[i] = distribution[i - 1]
					+ (probabilities[i - 1] / sumProb);

		}// END: i loop

		distribution[range - 1] = 1.0;

		double key = MathUtils.nextDouble();

		int mindex = 1;
		int maxdex = range - 1;
		int midpoint = mindex + (maxdex - mindex) / 2;
		while (mindex <= maxdex) {

			if (key < distribution[midpoint - 1]) {
				
				maxdex = midpoint - 1;
				
			} else if (key > distribution[midpoint]) {
				
				mindex = midpoint + 1;
				
			} else {
				
				return midpoint - 1;

			}
			
			midpoint = mindex + (int) Math.ceil((maxdex - mindex) / 2);

		}//END: mindex loop
		
		System.out.println("Error in rMultinom!");
		
		return range - 1;
	}//END: rMultinom
    
	// ////////////
	// ---MATH---//
	// ////////////
    
	public static int sample(double[] probabilities) {

		int samplePos = -Integer.MAX_VALUE;
		double cumProb = 0.0;
		double u = MathUtils.nextDouble();

		for (int i = 0; i < probabilities.length; i++) {

			cumProb += probabilities[i];

			if (u <= cumProb) {
				samplePos = i;
				break;
			}
		}

		return samplePos;
	}// END: randomChoicePDF
	
    public static void rescale(double[] logX) {
        double max = max(logX);
        for (int i = 0; i < logX.length; i++) {
            logX[i] -= max;
		}
	}// END: rescale

	public double getParameterVariance(Parameter param) {
		
		int n = param.getSize();
		double mean = getParameterMean(param);
		double var = 0;
		
		for (int i = 0; i < n; i++) {

			var+= Math.pow( (param.getValue(i) - mean), 2);

{'fraction_non_alphanumeric': 0.14135021097046413, 'fraction_numerical': 0.010247136829415311, 'mean_word_length': 2.9054531188701453, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 13, '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': '6479846', 'n_tokens_mistral': 3516, 'n_tokens_neox': 3089, 'n_words': 925}