def test_unique(self): self.assertEqual( sorted( util.unique([ 1, 2, 2, 4, 2, 2, 3, 4, 3, 2, 1, 2, 4, 1, 3, 2, 1, 4, 1, 2, 4, 1, 2, 1, 1, 1 ])), [1, 2, 3, 4]) self.assertEqual( util.unique([ 1, 2, 2, 4, 2, 2, 3, 4, 3, 2, 1, 2, 4, 1, 3, 2, 1, 4, 1, 2, 4, 1, 2, 1, 1, 1 ], sort=True), [1, 2, 3, 4]) self.assertEqual( sorted(util.unique([[], [1, 2, 3], [], [1, 2], [1, 2, 3]])), sorted([[], [1, 2], [1, 2, 3]])) self.assertEqual(util.unique([]), []) self.assertEqual(util.unique([], sort=True), [])
def cherry_picking(tree, k): if tree.is_leaf(): return [tree] elif tree == Shape.CHERRY: return [Shape.LEAF] else: def go(t, i, d): # for binary eyes only if d != k and not (t.is_leaf() or t.is_cherry()): chs = [go(ch, i, d + 1) for ch in t.children] if any(ch is None for ch in chs): return None else: return PhyloTree(None, sorted(chs)) elif d != k and (t == PhyloTree( None, [PhyloTree(i), PhyloTree(i + 1)]) or t == PhyloTree( None, [PhyloTree(i - 1), PhyloTree(i)])): return None elif d == k and (t == PhyloTree( None, [PhyloTree(i), PhyloTree(i + 1)]) or t == PhyloTree( None, [PhyloTree(i - 1), PhyloTree(i)])): return PhyloTree(i) elif t == PhyloTree(i): return None else: return t n = count_leaves(tree) phyl = shape_to_phylotree(tree, gen=int) ts = [] for i in range(n): t = go(phyl, i, 1) if t: ts.append(phylotree_to_shape(t)) return unique(sorted(ts))
def min_sackin(n): """ Returns all the `Shape` instances that attain the minimum Sackin index with `int` n leaves. :param n: `int` instance. :return: `list` instance. """ k = log2(n) nk = 2**k if n == nk: return [binary_max_balanced(nk)] else: ts = [] for t in min_sackin(n + 1): ts = ts + cherry_picking(t, k) return unique(sorted(ts))
def min_colless(n): """ Returns all the `Shape` instances that attain the minimum Colless index with `int` n leaves. :param n: `int` instance. :return: `list` instance. """ if n == 0: return [] elif n == 1: return [Shape.LEAF] elif n == 2: return [Shape.CHERRY] else: tss = [] for n1, n2 in min_colless_root(n): ts = unique([ Shape(sorted([t1, t2])) for t1 in min_colless(n1) for t2 in min_colless(n2) ]) tss.extend(ts) return tss