<s> import io <EOL> from unittest import TestCase , main <EOL> import numpy as np <EOL> from skbio import TreeNode <EOL> from skbio . tree import majority_rule <EOL> from skbio . tree . _majority_rule import ( _walk_clades , _filter_clades , <EOL> _build_trees ) <EOL> class MajorityRuleTests ( TestCase ) : <EOL> def test_majority_rule ( self ) : <EOL> trees = [ <EOL> TreeNode . read ( <EOL> io . StringIO ( "<STR_LIT>" ) ) , <EOL> TreeNode . read ( <EOL> io . StringIO ( "<STR_LIT>" ) ) , <EOL> TreeNode . read ( <EOL> io . StringIO ( "<STR_LIT>" ) ) , <EOL> TreeNode . read ( <EOL> io . StringIO ( "<STR_LIT>" ) ) , <EOL> TreeNode . read ( <EOL> io . StringIO ( "<STR_LIT>" ) ) , <EOL> TreeNode . read ( <EOL> io . StringIO ( "<STR_LIT>" ) ) , <EOL> TreeNode . read ( <EOL> io . StringIO ( "<STR_LIT>" ) ) , <EOL> TreeNode . read ( <EOL> io . StringIO ( "<STR_LIT>" ) ) , <EOL> TreeNode . read ( <EOL> io . StringIO ( "<STR_LIT>" ) ) ] <EOL> exp = TreeNode . read ( io . StringIO ( "<STR_LIT>" ) ) <EOL> obs = majority_rule ( trees ) <EOL> self . assertEqual ( exp . compare_subsets ( obs [ <NUM_LIT:0> ] ) , <NUM_LIT:0.0> ) <EOL> self . assertEqual ( len ( obs ) , <NUM_LIT:1> ) <EOL> tree = obs [ <NUM_LIT:0> ] <EOL> exp_supports = sorted ( [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) <EOL> obs_supports = sorted ( [ n . support for n in tree . non_tips ( ) ] ) <EOL> self . assertEqual ( obs_supports , exp_supports ) <EOL> obs = majority_rule ( trees , weights = np . ones ( len ( trees ) ) * <NUM_LIT:2> ) <EOL> self . assertEqual ( exp . compare_subsets ( obs [ <NUM_LIT:0> ] ) , <NUM_LIT:0.0> ) <EOL> self . assertEqual ( len ( obs ) , <NUM_LIT:1> ) <EOL> tree = obs [ <NUM_LIT:0> ] <EOL> exp_supports = sorted ( [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) <EOL> obs_supports = sorted ( [ n . support for n in tree . non_tips ( ) ] ) <EOL> with self . assertRaises ( ValueError ) : <EOL> majority_rule ( trees , weights = [ <NUM_LIT:1> , <NUM_LIT:2> ] ) <EOL> def test_majority_rule_multiple_trees ( self ) : <EOL> trees = [ <EOL> TreeNode . read ( io . StringIO ( "<STR_LIT>" ) ) , <EOL> TreeNode . read ( io . StringIO ( "<STR_LIT>" ) ) , <EOL> TreeNode . read ( io . StringIO ( "<STR_LIT>" ) ) , <EOL> TreeNode . read ( io . StringIO ( "<STR_LIT>" ) ) ] <EOL> trees = majority_rule ( trees ) <EOL> self . assertEqual ( len ( trees ) , <NUM_LIT:4> ) <EOL> exp = set ( [ <EOL> frozenset ( [ '<STR_LIT:a>' ] ) , <EOL> frozenset ( [ '<STR_LIT:b>' ] ) , <EOL> frozenset ( [ None , '<STR_LIT:c>' , '<STR_LIT:d>' ] ) , <EOL> frozenset ( [ None , '<STR_LIT:e>' , '<STR_LIT:f>' ] ) ] ) <EOL> obs = set ( [ frozenset ( [ n . name for n in t . traverse ( ) ] ) for t in trees ] ) <EOL> self . assertEqual ( obs , exp ) <EOL> def test_walk_clades ( self ) : <EOL> trees = [ TreeNode . read ( io . StringIO ( "<STR_LIT>" ) ) , <EOL> TreeNode . read ( io . StringIO ( "<STR_LIT>" ) ) ] <EOL> exp_clades = [ <EOL> ( frozenset ( [ '<STR_LIT:A>' ] ) , <NUM_LIT> ) , <EOL> ( frozenset ( [ '<STR_LIT:B>' ] ) , <NUM_LIT> ) , <EOL> ( frozenset ( [ '<STR_LIT:A>' , '<STR_LIT:B>' ] ) , <NUM_LIT> ) , <EOL> ( frozenset ( [ '<STR_LIT:D>' , '<STR_LIT:E>' ] ) , <NUM_LIT:1.0> ) , <EOL> ( frozenset ( [ '<STR_LIT:D>' , '<STR_LIT:E>' , '<STR_LIT:A>' , '<STR_LIT:B>' ] ) , <NUM_LIT:1.0> ) , <EOL> ( frozenset ( [ '<STR_LIT:D>' ] ) , <NUM_LIT> ) , <EOL> ( frozenset ( [ '<STR_LIT:E>' ] ) , <NUM_LIT> ) , <EOL> ( frozenset ( [ '<STR_LIT:X>' ] ) , <NUM_LIT:1.0> ) , <EOL> ( frozenset ( [ '<STR_LIT:E>' , '<STR_LIT:X>' ] ) , <NUM_LIT:1.0> ) , <EOL> ( frozenset ( [ '<STR_LIT:D>' , '<STR_LIT:E>' , '<STR_LIT:X>' ] ) , <NUM_LIT:1.0> ) , <EOL> ( frozenset ( [ '<STR_LIT:A>' , '<STR_LIT:B>' , '<STR_LIT:D>' , '<STR_LIT:E>' , '<STR_LIT:X>' ] ) , <NUM_LIT:1.0> ) ] <EOL> exp_lengths_nolength = { <EOL> frozenset ( [ '<STR_LIT:A>' ] ) : None , <EOL> frozenset ( [ '<STR_LIT:B>' ] ) : None , <EOL> frozenset ( [ '<STR_LIT:A>' , '<STR_LIT:B>' ] ) : None , <EOL> frozenset ( [ '<STR_LIT:D>' , '<STR_LIT:E>' ] ) : None , <EOL> frozenset ( [ '<STR_LIT:D>' , '<STR_LIT:E>' , '<STR_LIT:A>' , '<STR_LIT:B>' ] ) : None , <EOL> frozenset ( [ '<STR_LIT:D>' ] ) : None , <EOL> frozenset ( [ '<STR_LIT:E>' ] ) : None , <EOL> frozenset ( [ '<STR_LIT:X>' ] ) : None , <EOL> frozenset ( [ '<STR_LIT:E>' , '<STR_LIT:X>' ] ) : None , <EOL> frozenset ( [ '<STR_LIT:D>' , '<STR_LIT:E>' , '<STR_LIT:X>' ] ) : None , <EOL> frozenset ( [ '<STR_LIT:A>' , '<STR_LIT:B>' , '<STR_LIT:D>' , '<STR_LIT:E>' , '<STR_LIT:X>' ] ) : None } <EOL> exp_lengths = { <EOL> frozenset ( [ '<STR_LIT:A>' ] ) : <NUM_LIT> , <EOL> frozenset ( [ '<STR_LIT:B>' ] ) : <NUM_LIT> , <EOL> frozenset ( [ '<STR_LIT:A>' , '<STR_LIT:B>' ] ) : <NUM_LIT> , <EOL> frozenset ( [ '<STR_LIT:D>' , '<STR_LIT:E>' ] ) : <NUM_LIT:1.0> , <EOL> frozenset ( [ '<STR_LIT:D>' , '<STR_LIT:E>' , '<STR_LIT:A>' , '<STR_LIT:B>' ] ) : <NUM_LIT:1.0> , <EOL> frozenset ( [ '<STR_LIT:D>' ] ) : <NUM_LIT> , <EOL> frozenset ( [ '<STR_LIT:E>' ] ) : <NUM_LIT> , <EOL> frozenset ( [ '<STR_LIT:X>' ] ) : <NUM_LIT:1.0> , <EOL> frozenset ( [ '<STR_LIT:E>' , '<STR_LIT:X>' ] ) : <NUM_LIT:1.0> , <EOL> frozenset ( [ '<STR_LIT:D>' , '<STR_LIT:E>' , '<STR_LIT:X>' ] ) : <NUM_LIT:1.0> , <EOL> frozenset ( [ '<STR_LIT:A>' , '<STR_LIT:B>' , '<STR_LIT:D>' , '<STR_LIT:E>' , '<STR_LIT:X>' ] ) : <NUM_LIT:1.0> } <EOL> obs_clades , obs_lengths = _walk_clades ( trees , np . ones ( len ( trees ) ) ) <EOL> self . assertEqual ( set ( obs_clades ) , set ( exp_clades ) ) <EOL> self . assertEqual ( obs_lengths , exp_lengths_nolength ) <EOL> for t in trees : <EOL> for n in t . traverse ( include_self = True ) : <EOL> n . length = <NUM_LIT> <EOL> obs_clades , obs_lengths = _walk_clades ( trees , np . ones ( len ( trees ) ) ) <EOL> self . assertEqual ( set ( obs_clades ) , set ( exp_clades ) ) <EOL> self . assertEqual ( obs_lengths , exp_lengths ) <EOL> def test_filter_clades ( self ) : <EOL> clade_counts = [ ( frozenset ( [ '<STR_LIT:A>' , '<STR_LIT:B>' ] ) , <NUM_LIT:8> ) , <EOL> ( frozenset ( [ '<STR_LIT:A>' , '<STR_LIT:C>' ] ) , <NUM_LIT:7> ) , <EOL> ( frozenset ( [ '<STR_LIT:A>' ] ) , <NUM_LIT:6> ) , <EOL> ( frozenset ( [ '<STR_LIT:B>' ] ) , <NUM_LIT:5> ) ] <EOL> obs = _filter_clades ( clade_counts , <NUM_LIT:2> ) <EOL> exp = { frozenset ( [ '<STR_LIT:A>' , '<STR_LIT:B>' ] ) : <NUM_LIT:8> , <EOL> frozenset ( [ '<STR_LIT:A>' ] ) : <NUM_LIT:6> , <EOL> frozenset ( [ '<STR_LIT:B>' ] ) : <NUM_LIT:5> } <EOL> self . assertEqual ( obs , exp ) <EOL> clade_counts = [ ( frozenset ( [ '<STR_LIT:A>' ] ) , <NUM_LIT:8> ) , <EOL> ( frozenset ( [ '<STR_LIT:B>' ] ) , <NUM_LIT:7> ) , <EOL> ( frozenset ( [ '<STR_LIT:C>' ] ) , <NUM_LIT:7> ) , <EOL> ( frozenset ( [ '<STR_LIT:A>' , '<STR_LIT:B>' ] ) , <NUM_LIT:6> ) , <EOL> ( frozenset ( [ '<STR_LIT:A>' , '<STR_LIT:B>' , '<STR_LIT:C>' ] ) , <NUM_LIT:5> ) , <EOL> ( frozenset ( [ '<STR_LIT:D>' ] ) , <NUM_LIT:2> ) ] <EOL> obs = _filter_clades ( clade_counts , <NUM_LIT:4> ) <EOL> exp = { frozenset ( [ '<STR_LIT:A>' ] ) : <NUM_LIT:8> , <EOL> frozenset ( [ '<STR_LIT:B>' ] ) : <NUM_LIT:7> , <EOL> frozenset ( [ '<STR_LIT:C>' ] ) : <NUM_LIT:7> , <EOL> frozenset ( [ '<STR_LIT:A>' , '<STR_LIT:B>' ] ) : <NUM_LIT:6> , <EOL> frozenset ( [ '<STR_LIT:A>' , '<STR_LIT:B>' , '<STR_LIT:C>' ] ) : <NUM_LIT:5> } <EOL> self . assertEqual ( obs , exp ) <EOL> def test_build_trees ( self ) : <EOL> clade_counts = { frozenset ( [ '<STR_LIT:A>' , '<STR_LIT:B>' ] ) : <NUM_LIT:6> , <EOL> frozenset ( [ '<STR_LIT:A>' ] ) : <NUM_LIT:7> , <EOL> frozenset ( [ '<STR_LIT:B>' ] ) : <NUM_LIT:8> } <EOL> edge_lengths = { frozenset ( [ '<STR_LIT:A>' , '<STR_LIT:B>' ] ) : <NUM_LIT:1> , <EOL> frozenset ( [ '<STR_LIT:A>' ] ) : <NUM_LIT:2> , <EOL> frozenset ( [ '<STR_LIT:B>' ] ) : <NUM_LIT:3> } <EOL> tree = _build_trees ( clade_counts , edge_lengths , '<STR_LIT:foo>' ) [ <NUM_LIT:0> ] <EOL> self . assertEqual ( tree . foo , <NUM_LIT:6> ) <EOL> tree_foos = set ( [ c . foo for c in tree . children ] ) <EOL> tree_lens = set ( [ c . length for c in tree . children ] ) <EOL> self . assertEqual ( tree_foos , set ( [ <NUM_LIT:7> , <NUM_LIT:8> ] ) ) <EOL> self . assertEqual ( tree_lens , set ( [ <NUM_LIT:2> , <NUM_LIT:3> ] ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
