<s> import io <EOL> from unittest import TestCase , main <EOL> import pandas as pd <EOL> import numpy as np <EOL> import numpy . testing as npt <EOL> from skbio import DistanceMatrix , TreeNode <EOL> from skbio . util . _testing import assert_series_almost_equal <EOL> from skbio . diversity import ( alpha_diversity , beta_diversity , <EOL> get_alpha_diversity_metrics , <EOL> get_beta_diversity_metrics ) <EOL> from skbio . diversity . alpha import faith_pd , observed_otus <EOL> from skbio . diversity . beta import unweighted_unifrac , weighted_unifrac <EOL> from skbio . tree import DuplicateNodeError , MissingNodeError <EOL> class AlphaDiversityTests ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . table1 = np . array ( [ [ <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:0> , <NUM_LIT:4> , <NUM_LIT:4> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:6> , <NUM_LIT:2> , <NUM_LIT:1> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] ] ) <EOL> self . sids1 = list ( '<STR_LIT>' ) <EOL> self . oids1 = [ '<STR_LIT>' % i for i in range ( <NUM_LIT:1> , <NUM_LIT:6> ) ] <EOL> self . tree1 = TreeNode . read ( io . StringIO ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> self . table2 = np . array ( [ [ <NUM_LIT:1> , <NUM_LIT:3> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:2> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> ] ] ) <EOL> self . sids2 = list ( '<STR_LIT>' ) <EOL> self . oids2 = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . tree2 = TreeNode . read ( io . StringIO ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> def test_invalid_input ( self ) : <EOL> self . assertRaises ( ValueError , alpha_diversity , '<STR_LIT>' , <EOL> self . table1 , list ( '<STR_LIT>' ) ) <EOL> self . assertRaises ( ValueError , alpha_diversity , '<STR_LIT>' , <EOL> self . table1 ) <EOL> self . assertRaises ( ValueError , alpha_diversity , '<STR_LIT>' , <EOL> [ [ [ <NUM_LIT> ] ] ] ) <EOL> self . assertRaises ( ValueError , alpha_diversity , '<STR_LIT>' , <EOL> [ <NUM_LIT:0> , <NUM_LIT:3> , - <NUM_LIT:12> , <NUM_LIT> ] ) <EOL> self . assertRaises ( TypeError , alpha_diversity , '<STR_LIT>' , <EOL> [ <NUM_LIT:0> , <NUM_LIT:1> ] , not_a_real_kwarg = <NUM_LIT> ) <EOL> self . assertRaises ( TypeError , alpha_diversity , '<STR_LIT>' , <EOL> [ <NUM_LIT:0> , <NUM_LIT:1> ] , tree = self . tree1 , otu_ids = [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> not_a_real_kwarg = <NUM_LIT> ) <EOL> self . assertRaises ( TypeError , alpha_diversity , faith_pd , <EOL> [ <NUM_LIT:0> , <NUM_LIT:1> ] , tree = self . tree1 , otu_ids = [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> not_a_real_kwarg = <NUM_LIT> ) <EOL> def test_invalid_input_phylogenetic ( self ) : <EOL> self . assertRaises ( ValueError , alpha_diversity , '<STR_LIT>' , self . table1 , <EOL> list ( '<STR_LIT>' ) , tree = self . tree1 ) <EOL> self . assertRaises ( ValueError , alpha_diversity , '<STR_LIT>' , self . table1 , <EOL> list ( '<STR_LIT>' ) , otu_ids = self . oids1 ) <EOL> t = TreeNode . read ( <EOL> io . StringIO ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> counts = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] <EOL> otu_ids = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . assertRaises ( DuplicateNodeError , alpha_diversity , '<STR_LIT>' , <EOL> counts , otu_ids = otu_ids , tree = t ) <EOL> t = TreeNode . read ( io . StringIO ( <EOL> '<STR_LIT>' ) ) <EOL> counts = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] <EOL> otu_ids = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . assertRaises ( ValueError , alpha_diversity , '<STR_LIT>' , <EOL> counts , otu_ids = otu_ids , tree = t ) <EOL> t = TreeNode . read ( <EOL> io . StringIO ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> counts = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] <EOL> otu_ids = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . assertRaises ( ValueError , alpha_diversity , '<STR_LIT>' , <EOL> counts , otu_ids = otu_ids , tree = t ) <EOL> t = TreeNode . read ( <EOL> io . StringIO ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> counts = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] <EOL> otu_ids = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . assertRaises ( ValueError , alpha_diversity , '<STR_LIT>' , <EOL> counts , otu_ids = otu_ids , tree = t ) <EOL> t = TreeNode . read ( <EOL> io . StringIO ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> counts = [ <NUM_LIT:1> , <NUM_LIT:2> ] <EOL> otu_ids = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . assertRaises ( ValueError , alpha_diversity , '<STR_LIT>' , <EOL> counts , otu_ids = otu_ids , tree = t ) <EOL> t = TreeNode . read ( <EOL> io . StringIO ( '<STR_LIT>' ) ) <EOL> counts = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] <EOL> otu_ids = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . assertRaises ( ValueError , alpha_diversity , '<STR_LIT>' , <EOL> counts , otu_ids = otu_ids , tree = t ) <EOL> t = TreeNode . read ( <EOL> io . StringIO ( '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> counts = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] <EOL> otu_ids = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . assertRaises ( ValueError , alpha_diversity , '<STR_LIT>' , <EOL> counts , otu_ids = otu_ids , tree = t ) <EOL> t = TreeNode . read ( <EOL> io . StringIO ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> counts = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] <EOL> otu_ids = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . assertRaises ( MissingNodeError , alpha_diversity , '<STR_LIT>' , <EOL> counts , otu_ids = otu_ids , tree = t ) <EOL> def test_empty ( self ) : <EOL> actual = alpha_diversity ( '<STR_LIT>' , np . array ( [ ] , dtype = np . int64 ) ) <EOL> expected = pd . Series ( [ <NUM_LIT:0> ] ) <EOL> assert_series_almost_equal ( actual , expected ) <EOL> actual = alpha_diversity ( '<STR_LIT>' , <EOL> np . array ( [ [ ] ] , dtype = np . int64 ) ) <EOL> expected = pd . Series ( [ <NUM_LIT:0> ] ) <EOL> assert_series_almost_equal ( actual , expected ) <EOL> actual = alpha_diversity ( '<STR_LIT>' , <EOL> np . array ( [ [ ] , [ ] ] , dtype = np . int64 ) ) <EOL> expected = pd . Series ( [ <NUM_LIT:0> , <NUM_LIT:0> ] ) <EOL> assert_series_almost_equal ( actual , expected ) <EOL> actual = alpha_diversity ( '<STR_LIT>' , np . array ( [ ] , dtype = np . int64 ) , <EOL> tree = self . tree1 , otu_ids = [ ] ) <EOL> expected = pd . Series ( [ <NUM_LIT:0.> ] ) <EOL> assert_series_almost_equal ( actual , expected ) <EOL> actual = alpha_diversity ( '<STR_LIT>' , <EOL> np . array ( [ [ ] ] , dtype = np . int64 ) , <EOL> tree = self . tree1 , otu_ids = [ ] ) <EOL> expected = pd . Series ( [ <NUM_LIT:0.> ] ) <EOL> assert_series_almost_equal ( actual , expected ) <EOL> actual = alpha_diversity ( '<STR_LIT>' , <EOL> np . array ( [ [ ] , [ ] ] , dtype = np . int64 ) , <EOL> tree = self . tree1 , otu_ids = [ ] ) <EOL> expected = pd . Series ( [ <NUM_LIT:0.> , <NUM_LIT:0.> ] ) <EOL> assert_series_almost_equal ( actual , expected ) <EOL> def test_single_count_vector ( self ) : <EOL> actual = alpha_diversity ( '<STR_LIT>' , np . array ( [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:2> ] ) ) <EOL> expected = pd . Series ( [ <NUM_LIT:2> ] ) <EOL> assert_series_almost_equal ( actual , expected ) <EOL> actual = alpha_diversity ( '<STR_LIT>' , np . array ( [ <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ] ) , <EOL> tree = self . tree1 , otu_ids = self . oids1 ) <EOL> self . assertAlmostEqual ( actual [ <NUM_LIT:0> ] , <NUM_LIT> ) <EOL> def test_input_types ( self ) : <EOL> list_result = alpha_diversity ( '<STR_LIT>' , [ <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ] ) <EOL> array_result = alpha_diversity ( '<STR_LIT>' , <EOL> np . array ( [ <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ] ) ) <EOL> self . assertAlmostEqual ( list_result [ <NUM_LIT:0> ] , <NUM_LIT:3> ) <EOL> assert_series_almost_equal ( list_result , array_result ) <EOL> list_result = alpha_diversity ( '<STR_LIT>' , [ <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ] , <EOL> tree = self . tree1 , otu_ids = self . oids1 ) <EOL> array_result = alpha_diversity ( '<STR_LIT>' , np . array ( [ <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ] ) , <EOL> tree = self . tree1 , otu_ids = self . oids1 ) <EOL> self . assertAlmostEqual ( list_result [ <NUM_LIT:0> ] , <NUM_LIT> ) <EOL> assert_series_almost_equal ( list_result , array_result ) <EOL> def test_observed_otus ( self ) : <EOL> expected = pd . Series ( [ <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:3> ] , index = self . sids1 ) <EOL> actual = alpha_diversity ( '<STR_LIT>' , self . table1 , self . sids1 ) <EOL> assert_series_almost_equal ( actual , expected ) <EOL> actual = alpha_diversity ( observed_otus , self . table1 , self . sids1 ) <EOL> assert_series_almost_equal ( actual , expected ) <EOL> expected = pd . Series ( [ <NUM_LIT:2> , <NUM_LIT:1> , <NUM_LIT:0> ] , index = self . sids2 ) <EOL> actual = alpha_diversity ( '<STR_LIT>' , self . table2 , self . sids2 ) <EOL> assert_series_almost_equal ( actual , expected ) <EOL> def test_faith_pd ( self ) : <EOL> expected = [ ] <EOL> for e in self . table1 : <EOL> expected . append ( faith_pd ( e , tree = self . tree1 , otu_ids = self . oids1 ) ) <EOL> expected = pd . Series ( expected ) <EOL> actual = alpha_diversity ( '<STR_LIT>' , self . table1 , tree = self . tree1 , <EOL> otu_ids = self . oids1 ) <EOL> assert_series_almost_equal ( actual , expected ) <EOL> expected = [ ] <EOL> for e in self . table2 : <EOL> expected . append ( faith_pd ( e , tree = self . tree2 , otu_ids = self . oids2 ) ) <EOL> expected = pd . Series ( expected ) <EOL> actual = alpha_diversity ( '<STR_LIT>' , self . table2 , tree = self . tree2 , <EOL> otu_ids = self . oids2 ) <EOL> assert_series_almost_equal ( actual , expected ) <EOL> def test_no_ids ( self ) : <EOL> expected = pd . Series ( [ <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:3> ] ) <EOL> actual = alpha_diversity ( '<STR_LIT>' , self . table1 ) <EOL> assert_series_almost_equal ( actual , expected ) <EOL> def test_optimized ( self ) : <EOL> optimized = alpha_diversity ( '<STR_LIT>' , self . table1 , tree = self . tree1 , <EOL> otu_ids = self . oids1 ) <EOL> unoptimized = alpha_diversity ( faith_pd , self . table1 , tree = self . tree1 , <EOL> otu_ids = self . oids1 ) <EOL> assert_series_almost_equal ( optimized , unoptimized ) <EOL> class BetaDiversityTests ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . table1 = [ [ <NUM_LIT:1> , <NUM_LIT:5> ] , <EOL> [ <NUM_LIT:2> , <NUM_LIT:3> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:1> ] ] <EOL> self . sids1 = list ( '<STR_LIT>' ) <EOL> self . tree1 = TreeNode . read ( io . StringIO ( <EOL> '<STR_LIT>' ) ) <EOL> self . oids1 = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . table2 = [ [ <NUM_LIT> , <NUM_LIT:64> , <NUM_LIT> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:3> , <NUM_LIT:1> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:3> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0> , <NUM_LIT:12> , <NUM_LIT:1> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:5> , <NUM_LIT:5> , <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT:9> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:8> , <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:1> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT:0> ] ] <EOL> self . sids2 = list ( '<STR_LIT>' ) <EOL> def test_invalid_input ( self ) : <EOL> error_msg = ( "<STR_LIT>" ) <EOL> with self . assertRaisesRegex ( ValueError , error_msg ) : <EOL> beta_diversity ( self . table1 , list ( '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> error_msg = "<STR_LIT>" <EOL> with self . assertRaisesRegex ( ValueError , error_msg ) : <EOL> beta_diversity ( '<STR_LIT>' , self . table1 ) <EOL> error_msg = ( "<STR_LIT>" ) <EOL> with self . assertRaisesRegex ( ValueError , error_msg ) : <EOL> beta_diversity ( '<STR_LIT>' , [ [ [ <NUM_LIT> ] ] ] ) <EOL> error_msg = "<STR_LIT>" <EOL> with self . assertRaisesRegex ( ValueError , error_msg ) : <EOL> beta_diversity ( '<STR_LIT>' , [ [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:4> ] , [ <NUM_LIT:0> , <NUM_LIT:3> , - <NUM_LIT:12> , <NUM_LIT> ] ] ) <EOL> with self . assertRaisesRegex ( ValueError , error_msg ) : <EOL> beta_diversity ( '<STR_LIT>' , [ [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:3> , - <NUM_LIT:4> ] , [ <NUM_LIT:0> , <NUM_LIT:3> , <NUM_LIT:12> , <NUM_LIT> ] ] ) <EOL> error_msg = ( "<STR_LIT>" ) <EOL> with self . assertRaisesRegex ( TypeError , error_msg ) : <EOL> beta_diversity ( '<STR_LIT>' , [ [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:3> ] , [ <NUM_LIT:0> , <NUM_LIT:3> , <NUM_LIT:12> ] ] , <EOL> not_a_real_kwarg = <NUM_LIT> ) <EOL> with self . assertRaisesRegex ( TypeError , error_msg ) : <EOL> beta_diversity ( '<STR_LIT>' , [ [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:3> ] , [ <NUM_LIT:0> , <NUM_LIT:3> , <NUM_LIT:12> ] ] , <EOL> not_a_real_kwarg = <NUM_LIT> , tree = self . tree1 , <EOL> otu_ids = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> with self . assertRaisesRegex ( TypeError , error_msg ) : <EOL> beta_diversity ( '<STR_LIT>' , [ [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:3> ] , [ <NUM_LIT:0> , <NUM_LIT:3> , <NUM_LIT:12> ] ] , <EOL> not_a_real_kwarg = <NUM_LIT> , tree = self . tree1 , <EOL> otu_ids = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> with self . assertRaisesRegex ( TypeError , error_msg ) : <EOL> beta_diversity ( weighted_unifrac , [ [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:3> ] , [ <NUM_LIT:0> , <NUM_LIT:3> , <NUM_LIT:12> ] ] , <EOL> not_a_real_kwarg = <NUM_LIT> , tree = self . tree1 , <EOL> otu_ids = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def test_invalid_input_phylogenetic ( self ) : <EOL> self . assertRaises ( ValueError , beta_diversity , '<STR_LIT>' , <EOL> self . table1 , list ( '<STR_LIT>' ) , tree = self . tree1 ) <EOL> self . assertRaises ( ValueError , beta_diversity , '<STR_LIT>' , <EOL> self . table1 , list ( '<STR_LIT>' ) , tree = self . tree1 ) <EOL> self . assertRaises ( ValueError , beta_diversity , '<STR_LIT>' , <EOL> self . table1 , list ( '<STR_LIT>' ) , otu_ids = self . oids1 ) <EOL> self . assertRaises ( ValueError , beta_diversity , '<STR_LIT>' , <EOL> self . table1 , list ( '<STR_LIT>' ) , otu_ids = self . oids1 ) <EOL> t = TreeNode . read ( <EOL> io . StringIO ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> counts = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] <EOL> otu_ids = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . assertRaises ( DuplicateNodeError , beta_diversity , <EOL> '<STR_LIT>' , counts , otu_ids = otu_ids , tree = t ) <EOL> self . assertRaises ( DuplicateNodeError , beta_diversity , <EOL> '<STR_LIT>' , counts , otu_ids = otu_ids , <EOL> tree = t ) <EOL> t = TreeNode . read ( io . StringIO ( '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> counts = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] <EOL> otu_ids = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . assertRaises ( ValueError , beta_diversity , <EOL> '<STR_LIT>' , counts , otu_ids = otu_ids , tree = t ) <EOL> self . assertRaises ( ValueError , beta_diversity , <EOL> '<STR_LIT>' , counts , otu_ids = otu_ids , <EOL> tree = t ) <EOL> t = TreeNode . read ( <EOL> io . StringIO ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> counts = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] <EOL> otu_ids = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . assertRaises ( ValueError , beta_diversity , <EOL> '<STR_LIT>' , counts , otu_ids = otu_ids , tree = t ) <EOL> self . assertRaises ( ValueError , beta_diversity , <EOL> '<STR_LIT>' , counts , otu_ids = otu_ids , <EOL> tree = t ) <EOL> t = TreeNode . read ( <EOL> io . StringIO ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> counts = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] <EOL> otu_ids = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . assertRaises ( ValueError , beta_diversity , <EOL> '<STR_LIT>' , counts , otu_ids = otu_ids , tree = t ) <EOL> self . assertRaises ( ValueError , beta_diversity , <EOL> '<STR_LIT>' , counts , otu_ids = otu_ids , <EOL> tree = t ) <EOL> t = TreeNode . read ( <EOL> io . StringIO ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> counts = [ <NUM_LIT:1> , <NUM_LIT:2> ] <EOL> otu_ids = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . assertRaises ( ValueError , beta_diversity , <EOL> '<STR_LIT>' , counts , otu_ids = otu_ids , tree = t ) <EOL> self . assertRaises ( ValueError , beta_diversity , <EOL> '<STR_LIT>' , counts , otu_ids = otu_ids , <EOL> tree = t ) <EOL> t = TreeNode . read ( <EOL> io . StringIO ( '<STR_LIT>' ) ) <EOL> counts = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] <EOL> otu_ids = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . assertRaises ( ValueError , beta_diversity , <EOL> '<STR_LIT>' , counts , otu_ids = otu_ids , tree = t ) <EOL> self . assertRaises ( ValueError , beta_diversity , <EOL> '<STR_LIT>' , counts , otu_ids = otu_ids , <EOL> tree = t ) <EOL> t = TreeNode . read ( <EOL> io . StringIO ( '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> counts = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] <EOL> otu_ids = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . assertRaises ( ValueError , beta_diversity , <EOL> '<STR_LIT>' , counts , otu_ids = otu_ids , tree = t ) <EOL> self . assertRaises ( ValueError , beta_diversity , <EOL> '<STR_LIT>' , counts , otu_ids = otu_ids , <EOL> tree = t ) <EOL> t = TreeNode . read ( <EOL> io . StringIO ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> counts = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] <EOL> otu_ids = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . assertRaises ( MissingNodeError , beta_diversity , <EOL> '<STR_LIT>' , counts , otu_ids = otu_ids , tree = t ) <EOL> self . assertRaises ( MissingNodeError , beta_diversity , <EOL> '<STR_LIT>' , counts , otu_ids = otu_ids , <EOL> tree = t ) <EOL> def test_empty ( self ) : <EOL> actual = beta_diversity ( '<STR_LIT>' , <EOL> np . array ( [ [ ] , [ ] ] , dtype = np . int64 ) , <EOL> ids = [ '<STR_LIT:a>' , '<STR_LIT:b>' ] ) <EOL> expected_dm = DistanceMatrix ( [ [ <NUM_LIT:0.0> , <NUM_LIT:0.0> ] , [ <NUM_LIT:0.0> , <NUM_LIT:0.0> ] ] , [ '<STR_LIT:a>' , '<STR_LIT:b>' ] ) <EOL> npt . assert_array_equal ( actual , expected_dm ) <EOL> actual = beta_diversity ( '<STR_LIT>' , <EOL> np . array ( [ [ ] , [ ] ] , dtype = np . int64 ) , <EOL> ids = [ '<STR_LIT:a>' , '<STR_LIT:b>' ] , tree = self . tree1 , otu_ids = [ ] ) <EOL> expected_dm = DistanceMatrix ( [ [ <NUM_LIT:0.0> , <NUM_LIT:0.0> ] , [ <NUM_LIT:0.0> , <NUM_LIT:0.0> ] ] , [ '<STR_LIT:a>' , '<STR_LIT:b>' ] ) <EOL> self . assertEqual ( actual , expected_dm ) <EOL> def test_input_types ( self ) : <EOL> actual_array = beta_diversity ( '<STR_LIT>' , <EOL> np . array ( [ [ <NUM_LIT:1> , <NUM_LIT:5> ] , [ <NUM_LIT:2> , <NUM_LIT:3> ] ] ) , <EOL> ids = [ '<STR_LIT:a>' , '<STR_LIT:b>' ] ) <EOL> actual_list = beta_diversity ( '<STR_LIT>' , <EOL> [ [ <NUM_LIT:1> , <NUM_LIT:5> ] , [ <NUM_LIT:2> , <NUM_LIT:3> ] ] , ids = [ '<STR_LIT:a>' , '<STR_LIT:b>' ] ) <EOL> self . assertEqual ( actual_array , actual_list ) <EOL> def test_euclidean ( self ) : <EOL> actual_dm = beta_diversity ( '<STR_LIT>' , self . table1 , self . sids1 ) <EOL> self . assertEqual ( actual_dm . shape , ( <NUM_LIT:3> , <NUM_LIT:3> ) ) <EOL> npt . assert_almost_equal ( actual_dm [ '<STR_LIT:A>' , '<STR_LIT:A>' ] , <NUM_LIT:0.0> ) <EOL> npt . assert_almost_equal ( actual_dm [ '<STR_LIT:B>' , '<STR_LIT:B>' ] , <NUM_LIT:0.0> ) <EOL> npt . assert_almost_equal ( actual_dm [ '<STR_LIT:C>' , '<STR_LIT:C>' ] , <NUM_LIT:0.0> ) <EOL> npt . assert_almost_equal ( actual_dm [ '<STR_LIT:A>' , '<STR_LIT:B>' ] , <NUM_LIT> ) <EOL> npt . assert_almost_equal ( actual_dm [ '<STR_LIT:B>' , '<STR_LIT:A>' ] , <NUM_LIT> ) <EOL> npt . assert_almost_equal ( actual_dm [ '<STR_LIT:A>' , '<STR_LIT:C>' ] , <NUM_LIT> ) <EOL> npt . assert_almost_equal ( actual_dm [ '<STR_LIT:C>' , '<STR_LIT:A>' ] , <NUM_LIT> ) <EOL> npt . assert_almost_equal ( actual_dm [ '<STR_LIT:B>' , '<STR_LIT:C>' ] , <NUM_LIT> ) <EOL> npt . assert_almost_equal ( actual_dm [ '<STR_LIT:C>' , '<STR_LIT:B>' ] , <NUM_LIT> ) <EOL> actual_dm = beta_diversity ( '<STR_LIT>' , self . table2 , self . sids2 ) <EOL> expected_data = [ <EOL> [ <NUM_LIT:0.> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT:0.> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0.> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0.> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0.> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0.> ] ] <EOL> expected_dm = DistanceMatrix ( expected_data , self . sids2 ) <EOL> for id1 in self . sids2 : <EOL> for id2 in self . sids2 : <EOL> npt . assert_almost_equal ( actual_dm [ id1 , id2 ] , <EOL> expected_dm [ id1 , id2 ] , <NUM_LIT:6> ) <EOL> def test_braycurtis ( self ) : <EOL> actual_dm = beta_diversity ( '<STR_LIT>' , self . table1 , self . sids1 ) <EOL> self . assertEqual ( actual_dm . shape , ( <NUM_LIT:3> , <NUM_LIT:3> ) ) <EOL> npt . assert_almost_equal ( actual_dm [ '<STR_LIT:A>' , '<STR_LIT:A>' ] , <NUM_LIT:0.0> ) <EOL> npt . assert_almost_equal ( actual_dm [ '<STR_LIT:B>' , '<STR_LIT:B>' ] , <NUM_LIT:0.0> ) <EOL> npt . assert_almost_equal ( actual_dm [ '<STR_LIT:C>' , '<STR_LIT:C>' ] , <NUM_LIT:0.0> ) <EOL> npt . assert_almost_equal ( actual_dm [ '<STR_LIT:A>' , '<STR_LIT:B>' ] , <NUM_LIT> ) <EOL> npt . assert_almost_equal ( actual_dm [ '<STR_LIT:B>' , '<STR_LIT:A>' ] , <NUM_LIT> ) <EOL> npt . assert_almost_equal ( actual_dm [ '<STR_LIT:A>' , '<STR_LIT:C>' ] , <NUM_LIT> ) <EOL> npt . assert_almost_equal ( actual_dm [ '<STR_LIT:C>' , '<STR_LIT:A>' ] , <NUM_LIT> ) <EOL> npt . assert_almost_equal ( actual_dm [ '<STR_LIT:B>' , '<STR_LIT:C>' ] , <NUM_LIT> ) <EOL> npt . assert_almost_equal ( actual_dm [ '<STR_LIT:C>' , '<STR_LIT:B>' ] , <NUM_LIT> ) <EOL> actual_dm = beta_diversity ( '<STR_LIT>' , self . table2 , self . sids2 ) <EOL> expected_data = [ <EOL> [ <NUM_LIT:0.> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT:0.> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0.> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0.> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0.> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0.> ] ] <EOL> expected_dm = DistanceMatrix ( expected_data , self . sids2 ) <EOL> for id1 in self . sids2 : <EOL> for id2 in self . sids2 : <EOL> npt . assert_almost_equal ( actual_dm [ id1 , id2 ] , <EOL> expected_dm [ id1 , id2 ] , <NUM_LIT:6> ) <EOL> def test_unweighted_unifrac ( self ) : <EOL> dm1 = beta_diversity ( '<STR_LIT>' , self . table1 , self . sids1 , <EOL> otu_ids = self . oids1 , tree = self . tree1 ) <EOL> dm2 = beta_diversity ( unweighted_unifrac , self . table1 , self . sids1 , <EOL> otu_ids = self . oids1 , tree = self . tree1 ) <EOL> self . assertEqual ( dm1 . shape , ( <NUM_LIT:3> , <NUM_LIT:3> ) ) <EOL> self . assertEqual ( dm1 , dm2 ) <EOL> expected_data = [ [ <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT> ] , <EOL> [ <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0.0> ] ] <EOL> expected_dm = DistanceMatrix ( expected_data , ids = self . sids1 ) <EOL> for id1 in self . sids1 : <EOL> for id2 in self . sids1 : <EOL> npt . assert_almost_equal ( dm1 [ id1 , id2 ] , <EOL> expected_dm [ id1 , id2 ] , <NUM_LIT:6> ) <EOL> def test_weighted_unifrac ( self ) : <EOL> dm1 = beta_diversity ( '<STR_LIT>' , self . table1 , self . sids1 , <EOL> otu_ids = self . oids1 , tree = self . tree1 ) <EOL> dm2 = beta_diversity ( weighted_unifrac , self . table1 , self . sids1 , <EOL> otu_ids = self . oids1 , tree = self . tree1 ) <EOL> self . assertEqual ( dm1 . shape , ( <NUM_LIT:3> , <NUM_LIT:3> ) ) <EOL> self . assertEqual ( dm1 , dm2 ) <EOL> expected_data = [ <EOL> [ <NUM_LIT:0.0> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT:0.0> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0.0> ] ] <EOL> expected_dm = DistanceMatrix ( expected_data , ids = self . sids1 ) <EOL> for id1 in self . sids1 : <EOL> for id2 in self . sids1 : <EOL> npt . assert_almost_equal ( dm1 [ id1 , id2 ] , <EOL> expected_dm [ id1 , id2 ] , <NUM_LIT:6> ) <EOL> def test_weighted_unifrac_normalized ( self ) : <EOL> dm1 = beta_diversity ( '<STR_LIT>' , self . table1 , self . sids1 , <EOL> otu_ids = self . oids1 , tree = self . tree1 , <EOL> normalized = True ) <EOL> dm2 = beta_diversity ( weighted_unifrac , self . table1 , self . sids1 , <EOL> otu_ids = self . oids1 , tree = self . tree1 , <EOL> normalized = True ) <EOL> self . assertEqual ( dm1 . shape , ( <NUM_LIT:3> , <NUM_LIT:3> ) ) <EOL> self . assertEqual ( dm1 , dm2 ) <EOL> expected_data = [ <EOL> [ <NUM_LIT:0.0> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT:0.0> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0.0> ] ] <EOL> expected_dm = DistanceMatrix ( expected_data , ids = self . sids1 ) <EOL> for id1 in self . sids1 : <EOL> for id2 in self . sids1 : <EOL> npt . assert_almost_equal ( dm1 [ id1 , id2 ] , <EOL> expected_dm [ id1 , id2 ] , <NUM_LIT:6> ) <EOL> def test_scipy_kwargs ( self ) : <EOL> dm1 = beta_diversity ( '<STR_LIT>' , self . table1 , self . sids1 ) <EOL> dm2 = beta_diversity ( '<STR_LIT>' , self . table1 , self . sids1 , p = <NUM_LIT> ) <EOL> for id1 in self . sids1 : <EOL> for id2 in self . sids1 : <EOL> if id1 != id2 : <EOL> self . assertNotEqual ( dm1 [ id1 , id2 ] , dm2 [ id1 , id2 ] ) <EOL> def test_alt_pairwise_func ( self ) : <EOL> def not_a_real_pdist ( counts , metric ) : <EOL> return [ [ <NUM_LIT:0.0> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT:0.0> ] ] <EOL> dm1 = beta_diversity ( '<STR_LIT>' , self . table1 , <EOL> otu_ids = self . oids1 , tree = self . tree1 , <EOL> pairwise_func = not_a_real_pdist ) <EOL> expected = DistanceMatrix ( [ [ <NUM_LIT:0.0> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT:0.0> ] ] ) <EOL> self . assertEqual ( dm1 , expected ) <EOL> dm1 = beta_diversity ( '<STR_LIT>' , self . table1 , <EOL> otu_ids = self . oids1 , tree = self . tree1 , <EOL> pairwise_func = not_a_real_pdist ) <EOL> expected = DistanceMatrix ( [ [ <NUM_LIT:0.0> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT:0.0> ] ] ) <EOL> self . assertEqual ( dm1 , expected ) <EOL> dm1 = beta_diversity ( unweighted_unifrac , self . table1 , <EOL> otu_ids = self . oids1 , tree = self . tree1 , <EOL> pairwise_func = not_a_real_pdist ) <EOL> expected = DistanceMatrix ( [ [ <NUM_LIT:0.0> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT:0.0> ] ] ) <EOL> self . assertEqual ( dm1 , expected ) <EOL> dm1 = beta_diversity ( "<STR_LIT>" , self . table1 , <EOL> pairwise_func = not_a_real_pdist ) <EOL> expected = DistanceMatrix ( [ [ <NUM_LIT:0.0> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT:0.0> ] ] ) <EOL> self . assertEqual ( dm1 , expected ) <EOL> class MetricGetters ( TestCase ) : <EOL> def test_get_alpha_diversity_metrics ( self ) : <EOL> m = get_alpha_diversity_metrics ( ) <EOL> self . assertTrue ( '<STR_LIT>' in m ) <EOL> self . assertTrue ( '<STR_LIT>' in m ) <EOL> def test_get_alpha_diversity_metrics_sorted ( self ) : <EOL> m = get_alpha_diversity_metrics ( ) <EOL> n = sorted ( list ( m ) ) <EOL> self . assertEqual ( m , n ) <EOL> def test_get_beta_diversity_metrics ( self ) : <EOL> m = get_beta_diversity_metrics ( ) <EOL> self . assertTrue ( '<STR_LIT>' in m ) <EOL> self . assertTrue ( '<STR_LIT>' in m ) <EOL> def test_get_beta_diversity_metrics_sorted ( self ) : <EOL> m = get_beta_diversity_metrics ( ) <EOL> n = sorted ( list ( m ) ) <EOL> self . assertEqual ( m , n ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
