<s> import warnings <EOL> import numpy as np <EOL> import scipy . sparse as sp <EOL> from scipy . linalg import pinv2 <EOL> from gplearn . skutils . testing import ( assert_equal , assert_raises , assert_true , <EOL> assert_almost_equal , assert_array_equal , <EOL> SkipTest ) <EOL> from gplearn . skutils import check_random_state <EOL> from gplearn . skutils import deprecated <EOL> from gplearn . skutils import resample <EOL> from gplearn . skutils import safe_mask <EOL> from gplearn . skutils import column_or_1d <EOL> from gplearn . skutils import safe_indexing <EOL> from gplearn . skutils import shuffle <EOL> from sklearn . utils . extmath import pinvh <EOL> from gplearn . skutils . mocking import MockDataFrame <EOL> def test_make_rng ( ) : <EOL> assert_true ( check_random_state ( None ) is np . random . mtrand . _rand ) <EOL> assert_true ( check_random_state ( np . random ) is np . random . mtrand . _rand ) <EOL> rng_42 = np . random . RandomState ( <NUM_LIT> ) <EOL> assert_true ( check_random_state ( <NUM_LIT> ) . randint ( <NUM_LIT:100> ) == rng_42 . randint ( <NUM_LIT:100> ) ) <EOL> rng_42 = np . random . RandomState ( <NUM_LIT> ) <EOL> assert_true ( check_random_state ( rng_42 ) is rng_42 ) <EOL> rng_42 = np . random . RandomState ( <NUM_LIT> ) <EOL> assert_true ( check_random_state ( <NUM_LIT> ) . randint ( <NUM_LIT:100> ) != rng_42 . randint ( <NUM_LIT:100> ) ) <EOL> assert_raises ( ValueError , check_random_state , "<STR_LIT>" ) <EOL> def test_resample_noarg ( ) : <EOL> assert_true ( resample ( ) is None ) <EOL> def test_deprecated ( ) : <EOL> with warnings . catch_warnings ( record = True ) as w : <EOL> warnings . simplefilter ( "<STR_LIT>" ) <EOL> @ deprecated ( ) <EOL> def ham ( ) : <EOL> return "<STR_LIT>" <EOL> spam = ham ( ) <EOL> assert_equal ( spam , "<STR_LIT>" ) <EOL> assert_equal ( len ( w ) , <NUM_LIT:1> ) <EOL> assert_true ( issubclass ( w [ <NUM_LIT:0> ] . category , DeprecationWarning ) ) <EOL> assert_true ( "<STR_LIT>" in str ( w [ <NUM_LIT:0> ] . message ) . lower ( ) ) <EOL> with warnings . catch_warnings ( record = True ) as w : <EOL> warnings . simplefilter ( "<STR_LIT>" ) <EOL> @ deprecated ( "<STR_LIT>" ) <EOL> class Ham ( object ) : <EOL> SPAM = <NUM_LIT:1> <EOL> ham = Ham ( ) <EOL> assert_true ( hasattr ( ham , "<STR_LIT>" ) ) <EOL> assert_equal ( len ( w ) , <NUM_LIT:1> ) <EOL> assert_true ( issubclass ( w [ <NUM_LIT:0> ] . category , DeprecationWarning ) ) <EOL> assert_true ( "<STR_LIT>" in str ( w [ <NUM_LIT:0> ] . message ) . lower ( ) ) <EOL> def test_resample_value_errors ( ) : <EOL> assert_raises ( ValueError , resample , [ <NUM_LIT:0> ] , [ <NUM_LIT:0> , <NUM_LIT:1> ] ) <EOL> assert_raises ( ValueError , resample , [ <NUM_LIT:0> , <NUM_LIT:1> ] , [ <NUM_LIT:0> , <NUM_LIT:1> ] , n_samples = <NUM_LIT:3> ) <EOL> assert_raises ( ValueError , resample , [ <NUM_LIT:0> , <NUM_LIT:1> ] , [ <NUM_LIT:0> , <NUM_LIT:1> ] , meaning_of_life = <NUM_LIT> ) <EOL> def test_safe_mask ( ) : <EOL> random_state = check_random_state ( <NUM_LIT:0> ) <EOL> X = random_state . rand ( <NUM_LIT:5> , <NUM_LIT:4> ) <EOL> X_csr = sp . csr_matrix ( X ) <EOL> mask = [ False , False , True , True , True ] <EOL> mask = safe_mask ( X , mask ) <EOL> assert_equal ( X [ mask ] . shape [ <NUM_LIT:0> ] , <NUM_LIT:3> ) <EOL> mask = safe_mask ( X_csr , mask ) <EOL> assert_equal ( X_csr [ mask ] . shape [ <NUM_LIT:0> ] , <NUM_LIT:3> ) <EOL> def test_pinvh_simple_real ( ) : <EOL> a = np . array ( [ [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , [ <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ] , [ <NUM_LIT:7> , <NUM_LIT:8> , <NUM_LIT:10> ] ] , dtype = np . float64 ) <EOL> a = np . dot ( a , a . T ) <EOL> a_pinv = pinvh ( a ) <EOL> assert_almost_equal ( np . dot ( a , a_pinv ) , np . eye ( <NUM_LIT:3> ) ) <EOL> def test_pinvh_nonpositive ( ) : <EOL> a = np . array ( [ [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , [ <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ] , [ <NUM_LIT:7> , <NUM_LIT:8> , <NUM_LIT:9> ] ] , dtype = np . float64 ) <EOL> a = np . dot ( a , a . T ) <EOL> u , s , vt = np . linalg . svd ( a ) <EOL> s [ <NUM_LIT:0> ] *= - <NUM_LIT:1> <EOL> a = np . dot ( u * s , vt ) <EOL> a_pinv = pinv2 ( a ) <EOL> a_pinvh = pinvh ( a ) <EOL> assert_almost_equal ( a_pinv , a_pinvh ) <EOL> def test_pinvh_simple_complex ( ) : <EOL> a = ( np . array ( [ [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , [ <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ] , [ <NUM_LIT:7> , <NUM_LIT:8> , <NUM_LIT:10> ] ] ) <EOL> + <NUM_LIT> * np . array ( [ [ <NUM_LIT:10> , <NUM_LIT:8> , <NUM_LIT:7> ] , [ <NUM_LIT:6> , <NUM_LIT:5> , <NUM_LIT:4> ] , [ <NUM_LIT:3> , <NUM_LIT:2> , <NUM_LIT:1> ] ] ) ) <EOL> a = np . dot ( a , a . conj ( ) . T ) <EOL> a_pinv = pinvh ( a ) <EOL> assert_almost_equal ( np . dot ( a , a_pinv ) , np . eye ( <NUM_LIT:3> ) ) <EOL> def test_column_or_1d ( ) : <EOL> EXAMPLES = [ <EOL> ( "<STR_LIT>" , [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ) , <EOL> ( "<STR_LIT>" , [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> ] ) , <EOL> ( "<STR_LIT>" , np . arange ( <NUM_LIT:10> ) / <NUM_LIT> ) , <EOL> ( "<STR_LIT>" , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) , <EOL> ( "<STR_LIT>" , [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:0> ] ) , <EOL> ( "<STR_LIT>" , [ [ <NUM_LIT:1> ] , [ <NUM_LIT:2> ] , [ <NUM_LIT:3> ] ] ) , <EOL> ( "<STR_LIT>" , [ [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ] , [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ] ] ) , <EOL> ( "<STR_LIT>" , [ [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ] ) , <EOL> ( "<STR_LIT>" , [ [ <NUM_LIT:1> , <NUM_LIT:1> ] , [ <NUM_LIT:2> , <NUM_LIT:2> ] , [ <NUM_LIT:3> , <NUM_LIT:1> ] ] ) , <EOL> ( "<STR_LIT>" , [ [ <NUM_LIT:5> , <NUM_LIT:1> ] , [ <NUM_LIT:4> , <NUM_LIT:2> ] , [ <NUM_LIT:3> , <NUM_LIT:1> ] ] ) , <EOL> ( "<STR_LIT>" , [ [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ] ) , <EOL> ( "<STR_LIT>" , np . arange ( <NUM_LIT:30> ) . reshape ( ( - <NUM_LIT:1> , <NUM_LIT:3> ) ) ) , <EOL> ] <EOL> for y_type , y in EXAMPLES : <EOL> if y_type in [ "<STR_LIT>" , '<STR_LIT>' , "<STR_LIT>" ] : <EOL> assert_array_equal ( column_or_1d ( y ) , np . ravel ( y ) ) <EOL> else : <EOL> assert_raises ( ValueError , column_or_1d , y ) <EOL> def test_safe_indexing ( ) : <EOL> X = [ [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , [ <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ] , [ <NUM_LIT:7> , <NUM_LIT:8> , <NUM_LIT:9> ] ] <EOL> inds = np . array ( [ <NUM_LIT:1> , <NUM_LIT:2> ] ) <EOL> X_inds = safe_indexing ( X , inds ) <EOL> X_arrays = safe_indexing ( np . array ( X ) , inds ) <EOL> assert_array_equal ( np . array ( X_inds ) , X_arrays ) <EOL> assert_array_equal ( np . array ( X_inds ) , np . array ( X ) [ inds ] ) <EOL> def test_safe_indexing_pandas ( ) : <EOL> try : <EOL> import pandas as pd <EOL> except ImportError : <EOL> raise SkipTest ( "<STR_LIT>" ) <EOL> X = np . array ( [ [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , [ <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ] , [ <NUM_LIT:7> , <NUM_LIT:8> , <NUM_LIT:9> ] ] ) <EOL> X_df = pd . DataFrame ( X ) <EOL> inds = np . array ( [ <NUM_LIT:1> , <NUM_LIT:2> ] ) <EOL> X_df_indexed = safe_indexing ( X_df , inds ) <EOL> X_indexed = safe_indexing ( X_df , inds ) <EOL> assert_array_equal ( np . array ( X_df_indexed ) , X_indexed ) <EOL> def test_safe_indexing_mock_pandas ( ) : <EOL> X = np . array ( [ [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , [ <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ] , [ <NUM_LIT:7> , <NUM_LIT:8> , <NUM_LIT:9> ] ] ) <EOL> X_df = MockDataFrame ( X ) <EOL> inds = np . array ( [ <NUM_LIT:1> , <NUM_LIT:2> ] ) <EOL> X_df_indexed = safe_indexing ( X_df , inds ) <EOL> X_indexed = safe_indexing ( X_df , inds ) <EOL> assert_array_equal ( np . array ( X_df_indexed ) , X_indexed ) <EOL> def test_shuffle_on_ndim_equals_three ( ) : <EOL> def to_tuple ( A ) : <EOL> return tuple ( tuple ( tuple ( C ) for C in B ) for B in A ) <EOL> A = np . array ( [ [ [ <NUM_LIT:1> , <NUM_LIT:2> ] , [ <NUM_LIT:3> , <NUM_LIT:4> ] ] , [ [ <NUM_LIT:5> , <NUM_LIT:6> ] , [ <NUM_LIT:7> , <NUM_LIT:8> ] ] ] ) <EOL> S = set ( to_tuple ( A ) ) <EOL> shuffle ( A ) <EOL> assert_equal ( set ( to_tuple ( A ) ) , S ) </s>
