<s> import unittest <EOL> import warnings <EOL> import numpy as np <EOL> import numpy . testing as npt <EOL> from skbio . stats import subsample_counts , isubsample <EOL> def setup ( ) : <EOL> """<STR_LIT>""" <EOL> warnings . simplefilter ( "<STR_LIT:ignore>" ) <EOL> def teardown ( ) : <EOL> """<STR_LIT>""" <EOL> warnings . resetwarnings ( ) <EOL> class SubsampleCountsTests ( unittest . TestCase ) : <EOL> def test_subsample_counts_nonrandom ( self ) : <EOL> a = np . array ( [ <NUM_LIT:0> , <NUM_LIT:5> , <NUM_LIT:0> ] ) <EOL> npt . assert_equal ( subsample_counts ( a , <NUM_LIT:5> ) , a ) <EOL> exp = np . array ( [ <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:0> ] ) <EOL> npt . assert_equal ( subsample_counts ( a , <NUM_LIT:2> ) , exp ) <EOL> npt . assert_equal ( <EOL> subsample_counts ( a , <NUM_LIT:2> , replace = True ) , exp ) <EOL> a = [ <NUM_LIT:3> , <NUM_LIT:0> , <NUM_LIT:1> ] <EOL> exp = np . array ( [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] ) <EOL> npt . assert_equal ( subsample_counts ( a , <NUM_LIT:0> ) , exp ) <EOL> npt . assert_equal ( subsample_counts ( a , <NUM_LIT:0> , replace = True ) , exp ) <EOL> def test_subsample_counts_without_replacement ( self ) : <EOL> a = np . array ( [ <NUM_LIT:2> , <NUM_LIT:0> , <NUM_LIT:1> ] ) <EOL> actual = set ( ) <EOL> for i in range ( <NUM_LIT:1000> ) : <EOL> obs = subsample_counts ( a , <NUM_LIT:2> ) <EOL> actual . add ( tuple ( obs ) ) <EOL> self . assertEqual ( actual , { ( <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> ) , ( <NUM_LIT:2> , <NUM_LIT:0> , <NUM_LIT:0> ) } ) <EOL> obs = subsample_counts ( a , <NUM_LIT:2> ) <EOL> self . assertTrue ( np . array_equal ( obs , np . array ( [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> ] ) ) or <EOL> np . array_equal ( obs , np . array ( [ <NUM_LIT:2> , <NUM_LIT:0> , <NUM_LIT:0> ] ) ) ) <EOL> def test_subsample_counts_with_replacement ( self ) : <EOL> a = np . array ( [ <NUM_LIT:2> , <NUM_LIT:0> , <NUM_LIT:1> ] ) <EOL> actual = set ( ) <EOL> for i in range ( <NUM_LIT:1000> ) : <EOL> obs = subsample_counts ( a , <NUM_LIT:2> , replace = True ) <EOL> actual . add ( tuple ( obs ) ) <EOL> self . assertEqual ( actual , { ( <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> ) , ( <NUM_LIT:2> , <NUM_LIT:0> , <NUM_LIT:0> ) , ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:2> ) } ) <EOL> a = np . array ( [ <NUM_LIT:2> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:1> , <NUM_LIT:8> , <NUM_LIT:6> , <NUM_LIT:0> , <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:5> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:5> ] ) <EOL> actual = set ( ) <EOL> for i in range ( <NUM_LIT:1000> ) : <EOL> obs = subsample_counts ( a , <NUM_LIT> , replace = True ) <EOL> self . assertEqual ( obs . sum ( ) , <NUM_LIT> ) <EOL> actual . add ( tuple ( obs ) ) <EOL> self . assertTrue ( len ( actual ) > <NUM_LIT:10> ) <EOL> def test_subsample_counts_with_replacement_equal_n ( self ) : <EOL> a = np . array ( [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:2> , <NUM_LIT:1> ] ) <EOL> actual = set ( ) <EOL> for i in range ( <NUM_LIT:1000> ) : <EOL> obs = subsample_counts ( a , <NUM_LIT:10> , replace = True ) <EOL> self . assertEqual ( obs . sum ( ) , <NUM_LIT:10> ) <EOL> actual . add ( tuple ( obs ) ) <EOL> self . assertTrue ( len ( actual ) > <NUM_LIT:1> ) <EOL> def test_subsample_counts_invalid_input ( self ) : <EOL> with self . assertRaises ( ValueError ) : <EOL> subsample_counts ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , - <NUM_LIT:1> ) <EOL> with self . assertRaises ( TypeError ) : <EOL> subsample_counts ( [ <NUM_LIT:1> , <NUM_LIT> , <NUM_LIT:3> ] , <NUM_LIT:2> ) <EOL> with self . assertRaises ( ValueError ) : <EOL> subsample_counts ( [ [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , [ <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ] ] , <NUM_LIT:2> ) <EOL> with self . assertRaises ( ValueError ) : <EOL> subsample_counts ( [ <NUM_LIT:0> , <NUM_LIT:5> , <NUM_LIT:0> ] , <NUM_LIT:6> ) <EOL> class ISubsampleTests ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> np . random . seed ( <NUM_LIT> ) <EOL> self . sequences = [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> ] <EOL> def mock_sequence_iter ( self , items ) : <EOL> return ( { '<STR_LIT>' : sid , '<STR_LIT>' : seq } for sid , seq in items ) <EOL> def test_isubsample_simple ( self ) : <EOL> maximum = <NUM_LIT:10> <EOL> def bin_f ( x ) : <EOL> return x [ '<STR_LIT>' ] . rsplit ( '<STR_LIT:_>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> exp = sorted ( [ ( '<STR_LIT:a>' , { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) , <EOL> ( '<STR_LIT:a>' , { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) , <EOL> ( '<STR_LIT:a>' , { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) , <EOL> ( '<STR_LIT:a>' , { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) , <EOL> ( '<STR_LIT:a>' , { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) , <EOL> ( '<STR_LIT:b>' , { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) , <EOL> ( '<STR_LIT:b>' , { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) , <EOL> ( '<STR_LIT:c>' , { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) , <EOL> ( '<STR_LIT:c>' , { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) , <EOL> ( '<STR_LIT:c>' , { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) ] , <EOL> key = lambda x : x [ <NUM_LIT:0> ] ) <EOL> obs = isubsample ( self . mock_sequence_iter ( self . sequences ) , maximum , <EOL> bin_f = bin_f ) <EOL> self . assertEqual ( sorted ( obs , key = lambda x : x [ <NUM_LIT:0> ] ) , exp ) <EOL> def test_per_sample_sequences_min_seqs ( self ) : <EOL> maximum = <NUM_LIT:10> <EOL> minimum = <NUM_LIT:3> <EOL> def bin_f ( x ) : <EOL> return x [ '<STR_LIT>' ] . rsplit ( '<STR_LIT:_>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> exp = sorted ( [ ( '<STR_LIT:a>' , { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) , <EOL> ( '<STR_LIT:a>' , { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) , <EOL> ( '<STR_LIT:a>' , { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) , <EOL> ( '<STR_LIT:a>' , { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) , <EOL> ( '<STR_LIT:a>' , { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) , <EOL> ( '<STR_LIT:c>' , { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) , <EOL> ( '<STR_LIT:c>' , { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) , <EOL> ( '<STR_LIT:c>' , { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) ] , <EOL> key = lambda x : x [ <NUM_LIT:0> ] ) <EOL> obs = isubsample ( self . mock_sequence_iter ( self . sequences ) , maximum , <EOL> minimum , bin_f = bin_f ) <EOL> self . assertEqual ( sorted ( obs , key = lambda x : x [ <NUM_LIT:0> ] ) , exp ) <EOL> def test_per_sample_sequences_complex ( self ) : <EOL> maximum = <NUM_LIT:2> <EOL> def bin_f ( x ) : <EOL> return x [ '<STR_LIT>' ] . rsplit ( '<STR_LIT:_>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> exp = sorted ( [ ( '<STR_LIT:a>' , { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) , <EOL> ( '<STR_LIT:a>' , { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) , <EOL> ( '<STR_LIT:b>' , { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) , <EOL> ( '<STR_LIT:b>' , { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) , <EOL> ( '<STR_LIT:c>' , { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) , <EOL> ( '<STR_LIT:c>' , { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) ] , <EOL> key = lambda x : x [ <NUM_LIT:0> ] ) <EOL> obs = isubsample ( self . mock_sequence_iter ( self . sequences ) , maximum , <EOL> bin_f = bin_f , buf_size = <NUM_LIT:1> ) <EOL> self . assertEqual ( sorted ( obs , key = lambda x : x [ <NUM_LIT:0> ] ) , exp ) <EOL> def test_min_gt_max ( self ) : <EOL> gen = isubsample ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , maximum = <NUM_LIT:2> , minimum = <NUM_LIT:10> ) <EOL> with self . assertRaises ( ValueError ) : <EOL> next ( gen ) <EOL> def test_min_lt_zero ( self ) : <EOL> gen = isubsample ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , maximum = <NUM_LIT:0> , minimum = - <NUM_LIT:10> ) <EOL> with self . assertRaises ( ValueError ) : <EOL> next ( gen ) <EOL> def test_max_lt_zero ( self ) : <EOL> gen = isubsample ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , maximum = - <NUM_LIT:10> ) <EOL> with self . assertRaises ( ValueError ) : <EOL> next ( gen ) <EOL> def test_binf_is_none ( self ) : <EOL> maximum = <NUM_LIT:2> <EOL> items = [ <NUM_LIT:1> , <NUM_LIT:2> ] <EOL> exp = [ ( True , <NUM_LIT:1> ) , ( True , <NUM_LIT:2> ) ] <EOL> obs = isubsample ( items , maximum ) <EOL> self . assertEqual ( list ( obs ) , exp ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import nose <EOL> nose . runmodule ( ) </s>
