<commit_before>"""
methods to calculate merger models for a time tree
"""
from __future__ import print_function, division
import numpy as np
from Bio import AlignIO, Phylo


def coalescent(tree, Tc=None):
	'''
	assigns coalescent merger rates to all branches in the tree
	'''
	# determine the time intervals with constant merger rates
	branch_times = np.sort(np.unique([n.abs_t for n in tree.find_clades()]))
	branch_counts = np.zeros_like(branch_times, dtype=int)
	for n in tree.find_clades():
		if n.up  is None:
			continue
		lw = branch_times.searchsorted(n.abs_t)
		up = branch_times.searchsorted(n.up.abs_t)
		branch_counts[lw:up]+=1

	# calculate the merger rates in each interval
	merger_rates = branch_counts*(branch_counts-1.0)*0.5/Tc

	# assign those rates to all nodes in the tree
	for n in tree.find_clades():
		if n.up  is None:
			continue
		lw = branch_times.searchsorted(n.abs_t)
		up = branch_times.searchsorted(n.up.abs_t)
		if lw==up:
			lw = min(lw,up-1)
		n.merger_rate = merger_rates[lw:up].mean()
<commit_msg>changed total merger rate into per branch merger rate<commit_after>"""
methods to calculate merger models for a time tree
"""
from __future__ import print_function, division
import numpy as np
from Bio import AlignIO, Phylo


def coalescent(tree, Tc=None):
	'''
	assigns coalescent merger rates to all branches in the tree
	'''
	# determine the time intervals with constant merger rates
	branch_times = np.sort(np.unique([n.abs_t for n in tree.find_clades()]))
	branch_counts = np.zeros_like(branch_times, dtype=int)
	for n in tree.find_clades():
		if n.up  is None:
			continue
		lw = branch_times.searchsorted(n.abs_t)
		up = branch_times.searchsorted(n.up.abs_t)
		branch_counts[lw:up]+=1

	# calculate the merger rates in each interval
	merger_rates = (branch_counts-1.0)*0.5/Tc

	# assign those rates to all nodes in the tree
	for n in tree.find_clades():
		if n.up  is None:
			continue
		lw = branch_times.searchsorted(n.abs_t)
		up = branch_times.searchsorted(n.up.abs_t)
		if lw==up:
			lw = min(lw,up-1)
		n.merger_rate = merger_rates[lw:up].mean()

{'fraction_non_alphanumeric': 0.10277246653919694, 'fraction_numerical': 0.0057361376673040155, 'mean_word_length': 4.380462724935732, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4908651', 'n_tokens_mistral': 817, 'n_tokens_neox': 758, 'n_words': 218}