• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

js51 / SplitP / 7894856040

14 Feb 2024 12:52AM UTC coverage: 46.404% (-5.7%) from 52.085%
7894856040

push

github

web-flow
Merge pull request #38 from js51/SplitP-rewrite

Re-organise modules

403 of 880 new or added lines in 12 files covered. (45.8%)

413 of 890 relevant lines covered (46.4%)

1.39 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

16.28
/splitp/splits.py
1
import numpy as np
3✔
2
from itertools import combinations
3✔
3
from numpy.random import shuffle
3✔
4
from math import floor
3✔
5

6

7
def split_balance(s, asTuple=False):
3✔
8
    """Returns a string formatted 'X|X' which describes the balance of a given split string"""
NEW
9
    s = s.split("|")
×
NEW
10
    if not asTuple:
×
NEW
11
        return str(len(s[0])) + "|" + str(len(s[1]))
×
12
    else:
NEW
13
        return (len(s[0]), len(s[1]))
×
14

15
def format_split(tree, split):
3✔
NEW
16
    if isinstance(split, str):
×
NEW
17
        return split  # If already a string, just send it back
×
NEW
18
    if len(split[0]) + len(split[1]) > 35:
×
NEW
19
        raise ValueError(
×
20
            "Cannot produce string format for split with more than 35 taxa."
21
        )
NEW
22
    if not all(len(taxon) == 1 for taxon in tree.get_taxa()):
×
NEW
23
        raise ValueError("Cannot produce string format for split with taxa name of length > 1.")
×
24
    else:
NEW
25
        return f'{"".join(split[0])}|{"".join(split[1])}'
×
26

27
def all_splits(tree, trivial=False, size=None, randomise=False, string_format=True):
3✔
NEW
28
    taxa = tree.get_taxa()
×
NEW
29
    if string_format and len(taxa) > 35:
×
NEW
30
        raise ValueError(
×
31
            "Cannot generate splits for more than 35 taxa in string format. Use string_format=False."
32
        )
NEW
33
    if size is not None:
×
NEW
34
        sizes_to_do = [size]
×
35
    else:
NEW
36
        sizes_to_do = list(
×
37
            range(1 if trivial else 2, floor(tree.get_num_taxa() / 2) + 1)
38
        )
NEW
39
    for bal in sizes_to_do:
×
NEW
40
        even_split = bal == tree.get_num_taxa() / 2
×
NEW
41
        if not even_split:
×
NEW
42
            combos = combinations(taxa, bal)
×
43
        else: # We don't want to double up by selecting 012 and then 345
NEW
44
            combos = combinations(taxa[1:], bal - 1)  # Don't select 0
×
NEW
45
        if randomise:
×
NEW
46
            combos = list(combos)
×
NEW
47
            shuffle(combos)
×
NEW
48
        for left_taxa in combos:
×
NEW
49
            if even_split:
×
NEW
50
                left_taxa = (taxa[0],) + left_taxa
×
NEW
51
            right_taxa = sorted(tuple(set(taxa) - set(left_taxa)), key=taxa.index)
×
NEW
52
            left_taxa = sorted(left_taxa, key=taxa.index)
×
NEW
53
            left, right = tuple(left_taxa), tuple(right_taxa) 
×
NEW
54
            if (taxa[0] in right):  # Put taxa[0] on left side of split
×
NEW
55
                left, right = right, left
×
NEW
56
            if string_format:
×
NEW
57
                yield format_split(tree, (left, right))
×
58
            else:
NEW
59
                yield (left, right)
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc