Skip to content

Commit 3ef304f

Browse files
SQUASHME some updates.
1 parent c09c1d7 commit 3ef304f

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

python/tests/test_topology.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7954,6 +7954,15 @@ def test_star_branch_length(self):
79547954
assert ts.kc_distance(topological_equiv_ts) == 0
79557955

79567956

7957+
def num_leaf_labelled_binary_trees(n):
7958+
"""
7959+
Returns the number of leaf labelled binary trees with n leaves.
7960+
7961+
https://oeis.org/A005373/
7962+
"""
7963+
return int(np.math.factorial(2 * n - 3) / (2 ** (n - 2) * np.math.factorial(n - 2)))
7964+
7965+
79577966
class TestPolytomySplitting:
79587967
"""
79597968
Test the ability to randomly split polytomies
@@ -7999,15 +8008,17 @@ def ts_polytomy_44344(self):
79998008
strict=False,
80008009
)
80018010

8002-
def test_all_topologies_n4(self):
8011+
@pytest.mark.parametrize("n", [2, 3, 4, 5])
8012+
def test_all_topologies(self, n):
8013+
N = num_leaf_labelled_binary_trees(n)
80038014
ranks = collections.Counter()
8004-
t4 = self.tree_polytomy_4()
8005-
for _ in range(150):
8006-
tree = t4.split_polytomies()
8007-
ranks[tree.rank()] += 1
8008-
# There are 15 possible binary trees here, we should have seen them
8009-
# all with high probability after 150 attempts.
8010-
assert len(ranks) == 15
8015+
tree = tskit.Tree.generate_star(n)
8016+
for seed in range(20 * N):
8017+
split_tree = tree.split_polytomies(random_seed=seed)
8018+
ranks[split_tree.rank()] += 1
8019+
# There are N possible binary trees here, we should have seen them
8020+
# all with high probability after 20 N attempts.
8021+
assert len(ranks) == N
80118022

80128023
def test_simple_examples(self):
80138024
tree = self.tree_polytomy_4().split_polytomies(random_seed=12)
@@ -8084,11 +8095,12 @@ def test_epsilon_for_mutations(self):
80848095
site=site, time=root_time - mut_diff, node=0, derived_state="1"
80858096
)
80868097
tree = tables.tree_sequence().first()
8098+
# FIXME this test is brittle and fails for some seeds
80878099
with pytest.raises(
80888100
_tskit.LibraryError,
80898101
match="not small enough to create new nodes below a polytomy",
80908102
):
8091-
tree.split_polytomies(epsilon=mut_diff * 2, random_seed=13)
8103+
tree.split_polytomies(epsilon=mut_diff * 2, random_seed=123)
80928104
# A 4-tomy creates 2 new nodes => epsilon of ~ 1/3 of mut_diff should work
80938105
self.tree_polytomy_4().split_polytomies(epsilon=mut_diff / 3, random_seed=13)
80948106

python/tskit/combinatorics.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def random_binary_tree(leaf_labels, rng):
5555
implementation of Algorithm R, because we are interested in
5656
the leaf node labellings.
5757
58-
The pre-fasicle text is available here, page 16:
58+
The pre-fascicle text is available here, page 16:
5959
http://www.cs.utsa.edu/~wagner/knuth/fasc4a.pdf
6060
"""
6161
nodes = [TreeNode(label=leaf_labels[0])]
@@ -66,6 +66,7 @@ def random_binary_tree(leaf_labels, rng):
6666
if x.parent is not None:
6767
index = x.parent.children.index(x)
6868
x.parent.children[index] = internal
69+
# TODO it's not clear if this shuffle is actually needed.
6970
rng.shuffle(internal.children)
7071
x.parent = internal
7172
n.parent = internal

python/tskit/trees.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2387,11 +2387,9 @@ def split_polytomies(
23872387
23882388
If the ``method`` is ``"random"`` (currently the only option, and the default
23892389
when no method is specified), then for a node with :math:`n` children, the
2390-
:math:`(2n - 3)!!` possible bifurcating topologies are produced with equal
2390+
:math:`(2n - 3)! / (2^(n - 2) (n - 2!))` possible binary trees with equal
23912391
probability.
23922392
2393-
.. fixme:: Looks like this function is wrong
2394-
23952393
The returned :class`.Tree` will have the same genomic span as this tree,
23962394
and node IDs will be converved (that is, node ``u`` in this tree will
23972395
also exist in the returned tree). The returned tree is derived from a

0 commit comments

Comments
 (0)