<s> """<STR_LIT>""" <EOL> import warnings <EOL> from tempfile import NamedTemporaryFile <EOL> from itertools import product <EOL> import numpy as np <EOL> from numpy . testing import assert_array_equal , assert_warns <EOL> import scipy . sparse as sp <EOL> from nose . tools import assert_raises , assert_true , assert_false , assert_equal <EOL> from gplearn . skutils . testing import assert_raises_regexp <EOL> from gplearn . skutils import as_float_array , check_array , check_symmetric <EOL> from gplearn . skutils import check_X_y <EOL> from gplearn . skutils . estimator_checks import NotAnArray <EOL> from sklearn . random_projection import sparse_random_matrix <EOL> from sklearn . linear_model import ARDRegression <EOL> from sklearn . neighbors import KNeighborsClassifier <EOL> from sklearn . ensemble import RandomForestRegressor <EOL> from sklearn . svm import SVR <EOL> from sklearn . datasets import make_blobs <EOL> from gplearn . skutils . validation import ( <EOL> NotFittedError , <EOL> has_fit_parameter , <EOL> check_is_fitted , <EOL> check_consistent_length ) <EOL> from gplearn . skutils . testing import assert_raise_message <EOL> def test_as_float_array ( ) : <EOL> X = np . ones ( ( <NUM_LIT:3> , <NUM_LIT:10> ) , dtype = np . int32 ) <EOL> X = X + np . arange ( <NUM_LIT:10> , dtype = np . int32 ) <EOL> X2 = as_float_array ( X , copy = False ) <EOL> np . testing . assert_equal ( X2 . dtype , np . float32 ) <EOL> X = X . astype ( np . int64 ) <EOL> X2 = as_float_array ( X , copy = True ) <EOL> assert_true ( as_float_array ( X , False ) is not X ) <EOL> np . testing . assert_equal ( X2 . dtype , np . float64 ) <EOL> X = np . ones ( ( <NUM_LIT:3> , <NUM_LIT:2> ) , dtype = np . float32 ) <EOL> assert_true ( as_float_array ( X , copy = False ) is X ) <EOL> X = np . asfortranarray ( X ) <EOL> assert_true ( np . isfortran ( as_float_array ( X , copy = True ) ) ) <EOL> matrices = [ <EOL> np . matrix ( np . arange ( <NUM_LIT:5> ) ) , <EOL> sp . csc_matrix ( np . arange ( <NUM_LIT:5> ) ) . toarray ( ) , <EOL> sparse_random_matrix ( <NUM_LIT:10> , <NUM_LIT:10> , density = <NUM_LIT> ) . toarray ( ) <EOL> ] <EOL> for M in matrices : <EOL> N = as_float_array ( M , copy = True ) <EOL> N [ <NUM_LIT:0> , <NUM_LIT:0> ] = np . nan <EOL> assert_false ( np . isnan ( M ) . any ( ) ) <EOL> def test_np_matrix ( ) : <EOL> X = np . arange ( <NUM_LIT:12> ) . reshape ( <NUM_LIT:3> , <NUM_LIT:4> ) <EOL> assert_false ( isinstance ( as_float_array ( X ) , np . matrix ) ) <EOL> assert_false ( isinstance ( as_float_array ( np . matrix ( X ) ) , np . matrix ) ) <EOL> assert_false ( isinstance ( as_float_array ( sp . csc_matrix ( X ) ) , np . matrix ) ) <EOL> def test_memmap ( ) : <EOL> asflt = lambda x : as_float_array ( x , copy = False ) <EOL> with NamedTemporaryFile ( prefix = '<STR_LIT>' ) as tmp : <EOL> M = np . memmap ( tmp , shape = <NUM_LIT:100> , dtype = np . float32 ) <EOL> M [ : ] = <NUM_LIT:0> <EOL> for f in ( check_array , np . asarray , asflt ) : <EOL> X = f ( M ) <EOL> X [ : ] = <NUM_LIT:1> <EOL> assert_array_equal ( X . ravel ( ) , M ) <EOL> X [ : ] = <NUM_LIT:0> <EOL> def test_ordering ( ) : <EOL> X = np . ones ( ( <NUM_LIT:10> , <NUM_LIT:5> ) ) <EOL> for A in X , X . T : <EOL> for copy in ( True , False ) : <EOL> B = check_array ( A , order = '<STR_LIT:C>' , copy = copy ) <EOL> assert_true ( B . flags [ '<STR_LIT>' ] ) <EOL> B = check_array ( A , order = '<STR_LIT:F>' , copy = copy ) <EOL> assert_true ( B . flags [ '<STR_LIT>' ] ) <EOL> if copy : <EOL> assert_false ( A is B ) <EOL> X = sp . csr_matrix ( X ) <EOL> X . data = X . data [ : : - <NUM_LIT:1> ] <EOL> assert_false ( X . data . flags [ '<STR_LIT>' ] ) <EOL> for copy in ( True , False ) : <EOL> Y = check_array ( X , accept_sparse = '<STR_LIT>' , copy = copy , order = '<STR_LIT:C>' ) <EOL> assert_true ( Y . data . flags [ '<STR_LIT>' ] ) <EOL> def test_check_array ( ) : <EOL> X = [ [ <NUM_LIT:1> , <NUM_LIT:2> ] , [ <NUM_LIT:3> , <NUM_LIT:4> ] ] <EOL> X_csr = sp . csr_matrix ( X ) <EOL> assert_raises ( TypeError , check_array , X_csr ) <EOL> X_array = check_array ( [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ] ) <EOL> assert_equal ( X_array . ndim , <NUM_LIT:2> ) <EOL> X_array = check_array ( [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ] , ensure_2d = False ) <EOL> assert_equal ( X_array . ndim , <NUM_LIT:1> ) <EOL> X_ndim = np . arange ( <NUM_LIT:8> ) . reshape ( <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> ) <EOL> assert_raises ( ValueError , check_array , X_ndim ) <EOL> check_array ( X_ndim , allow_nd = True ) <EOL> X_inf = np . arange ( <NUM_LIT:4> ) . reshape ( <NUM_LIT:2> , <NUM_LIT:2> ) . astype ( np . float ) <EOL> X_inf [ <NUM_LIT:0> , <NUM_LIT:0> ] = np . inf <EOL> assert_raises ( ValueError , check_array , X_inf ) <EOL> check_array ( X_inf , force_all_finite = False ) <EOL> X_nan = np . arange ( <NUM_LIT:4> ) . reshape ( <NUM_LIT:2> , <NUM_LIT:2> ) . astype ( np . float ) <EOL> X_nan [ <NUM_LIT:0> , <NUM_LIT:0> ] = np . nan <EOL> assert_raises ( ValueError , check_array , X_nan ) <EOL> check_array ( X_inf , force_all_finite = False ) <EOL> X_C = np . arange ( <NUM_LIT:4> ) . reshape ( <NUM_LIT:2> , <NUM_LIT:2> ) . copy ( "<STR_LIT:C>" ) <EOL> X_F = X_C . copy ( "<STR_LIT:F>" ) <EOL> X_int = X_C . astype ( np . int ) <EOL> X_float = X_C . astype ( np . float ) <EOL> Xs = [ X_C , X_F , X_int , X_float ] <EOL> dtypes = [ np . int32 , np . int , np . float , np . float32 , None , np . bool , object ] <EOL> orders = [ '<STR_LIT:C>' , '<STR_LIT:F>' , None ] <EOL> copys = [ True , False ] <EOL> for X , dtype , order , copy in product ( Xs , dtypes , orders , copys ) : <EOL> X_checked = check_array ( X , dtype = dtype , order = order , copy = copy ) <EOL> if dtype is not None : <EOL> assert_equal ( X_checked . dtype , dtype ) <EOL> else : <EOL> assert_equal ( X_checked . dtype , X . dtype ) <EOL> if order == '<STR_LIT:C>' : <EOL> assert_true ( X_checked . flags [ '<STR_LIT>' ] ) <EOL> assert_false ( X_checked . flags [ '<STR_LIT>' ] ) <EOL> elif order == '<STR_LIT:F>' : <EOL> assert_true ( X_checked . flags [ '<STR_LIT>' ] ) <EOL> assert_false ( X_checked . flags [ '<STR_LIT>' ] ) <EOL> if copy : <EOL> assert_false ( X is X_checked ) <EOL> else : <EOL> if ( X . dtype == X_checked . dtype and <EOL> X_checked . flags [ '<STR_LIT>' ] == X . flags [ '<STR_LIT>' ] <EOL> and X_checked . flags [ '<STR_LIT>' ] == X . flags [ '<STR_LIT>' ] ) : <EOL> assert_true ( X is X_checked ) <EOL> X_csc = sp . csc_matrix ( X_C ) <EOL> X_coo = X_csc . tocoo ( ) <EOL> X_dok = X_csc . todok ( ) <EOL> X_int = X_csc . astype ( np . int ) <EOL> X_float = X_csc . astype ( np . float ) <EOL> Xs = [ X_csc , X_coo , X_dok , X_int , X_float ] <EOL> accept_sparses = [ [ '<STR_LIT>' , '<STR_LIT>' ] , [ '<STR_LIT>' , '<STR_LIT>' ] ] <EOL> for X , dtype , accept_sparse , copy in product ( Xs , dtypes , accept_sparses , <EOL> copys ) : <EOL> with warnings . catch_warnings ( record = True ) as w : <EOL> X_checked = check_array ( X , dtype = dtype , <EOL> accept_sparse = accept_sparse , copy = copy ) <EOL> if ( dtype is object or sp . isspmatrix_dok ( X ) ) and len ( w ) : <EOL> message = str ( w [ <NUM_LIT:0> ] . message ) <EOL> messages = [ "<STR_LIT>" , <EOL> "<STR_LIT>" ] <EOL> assert_true ( message in messages ) <EOL> else : <EOL> assert_equal ( len ( w ) , <NUM_LIT:0> ) <EOL> if dtype is not None : <EOL> assert_equal ( X_checked . dtype , dtype ) <EOL> else : <EOL> assert_equal ( X_checked . dtype , X . dtype ) <EOL> if X . format in accept_sparse : <EOL> assert_equal ( X . format , X_checked . format ) <EOL> else : <EOL> assert_equal ( X_checked . format , accept_sparse [ <NUM_LIT:0> ] ) <EOL> if copy : <EOL> assert_false ( X is X_checked ) <EOL> else : <EOL> if ( X . dtype == X_checked . dtype and X . format == X_checked . format ) : <EOL> assert_true ( X is X_checked ) <EOL> X_dense = check_array ( [ [ <NUM_LIT:1> , <NUM_LIT:2> ] , [ <NUM_LIT:3> , <NUM_LIT:4> ] ] ) <EOL> assert_true ( isinstance ( X_dense , np . ndarray ) ) <EOL> assert_raises ( ValueError , check_array , X_ndim . tolist ( ) ) <EOL> check_array ( X_ndim . tolist ( ) , allow_nd = True ) <EOL> X_no_array = NotAnArray ( X_dense ) <EOL> result = check_array ( X_no_array ) <EOL> assert_true ( isinstance ( result , np . ndarray ) ) <EOL> def test_check_array_min_samples_and_features_messages ( ) : <EOL> msg = "<STR_LIT>" <EOL> assert_raise_message ( ValueError , msg , check_array , [ ] ) <EOL> msg = "<STR_LIT>" <EOL> assert_raise_message ( ValueError , msg , check_array , [ ] , ensure_2d = False ) <EOL> msg = "<STR_LIT>" <EOL> assert_raise_message ( TypeError , msg , check_array , <NUM_LIT> , ensure_2d = False ) <EOL> X_checked = check_array ( <NUM_LIT> , ensure_2d = True ) <EOL> assert_array_equal ( np . array ( [ [ <NUM_LIT> ] ] ) , X_checked ) <EOL> X = np . ones ( ( <NUM_LIT:1> , <NUM_LIT:10> ) ) <EOL> y = np . ones ( <NUM_LIT:1> ) <EOL> msg = "<STR_LIT>" <EOL> assert_raise_message ( ValueError , msg , check_X_y , X , y , <EOL> ensure_min_samples = <NUM_LIT:2> ) <EOL> assert_raise_message ( ValueError , msg , check_X_y , X , y , <EOL> ensure_min_samples = <NUM_LIT:2> , ensure_2d = False ) <EOL> X = np . ones ( ( <NUM_LIT:10> , <NUM_LIT:2> ) ) <EOL> y = np . ones ( <NUM_LIT:2> ) <EOL> msg = "<STR_LIT>" <EOL> assert_raise_message ( ValueError , msg , check_X_y , X , y , <EOL> ensure_min_features = <NUM_LIT:3> ) <EOL> assert_raise_message ( ValueError , msg , check_X_y , X , y , <EOL> ensure_min_features = <NUM_LIT:3> , allow_nd = True ) <EOL> X = np . empty ( <NUM_LIT:0> ) . reshape ( <NUM_LIT:10> , <NUM_LIT:0> ) <EOL> y = np . ones ( <NUM_LIT:10> ) <EOL> msg = "<STR_LIT>" <EOL> assert_raise_message ( ValueError , msg , check_X_y , X , y ) <EOL> X = np . ones ( ( <NUM_LIT:10> , <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT> ) ) <EOL> y = np . ones ( <NUM_LIT:10> ) <EOL> X_checked , y_checked = check_X_y ( X , y , allow_nd = True ) <EOL> assert_array_equal ( X , X_checked ) <EOL> assert_array_equal ( y , y_checked ) <EOL> def test_has_fit_parameter ( ) : <EOL> assert_false ( has_fit_parameter ( KNeighborsClassifier , "<STR_LIT>" ) ) <EOL> assert_true ( has_fit_parameter ( RandomForestRegressor , "<STR_LIT>" ) ) <EOL> assert_true ( has_fit_parameter ( SVR , "<STR_LIT>" ) ) <EOL> assert_true ( has_fit_parameter ( SVR ( ) , "<STR_LIT>" ) ) <EOL> def test_check_symmetric ( ) : <EOL> arr_sym = np . array ( [ [ <NUM_LIT:0> , <NUM_LIT:1> ] , [ <NUM_LIT:1> , <NUM_LIT:2> ] ] ) <EOL> arr_bad = np . ones ( <NUM_LIT:2> ) <EOL> arr_asym = np . array ( [ [ <NUM_LIT:0> , <NUM_LIT:2> ] , [ <NUM_LIT:0> , <NUM_LIT:2> ] ] ) <EOL> test_arrays = { '<STR_LIT>' : arr_asym , <EOL> '<STR_LIT>' : sp . dok_matrix ( arr_asym ) , <EOL> '<STR_LIT>' : sp . csr_matrix ( arr_asym ) , <EOL> '<STR_LIT>' : sp . csc_matrix ( arr_asym ) , <EOL> '<STR_LIT>' : sp . coo_matrix ( arr_asym ) , <EOL> '<STR_LIT>' : sp . lil_matrix ( arr_asym ) , <EOL> '<STR_LIT>' : sp . bsr_matrix ( arr_asym ) } <EOL> assert_raises ( ValueError , check_symmetric , arr_bad ) <EOL> for arr_format , arr in test_arrays . items ( ) : <EOL> assert_warns ( UserWarning , check_symmetric , arr ) <EOL> assert_raises ( ValueError , check_symmetric , arr , raise_exception = True ) <EOL> output = check_symmetric ( arr , raise_warning = False ) <EOL> if sp . issparse ( output ) : <EOL> assert_equal ( output . format , arr_format ) <EOL> assert_array_equal ( output . toarray ( ) , arr_sym ) <EOL> else : <EOL> assert_array_equal ( output , arr_sym ) <EOL> def test_check_is_fitted ( ) : <EOL> assert_raises ( ValueError , check_is_fitted , ARDRegression , "<STR_LIT>" ) <EOL> assert_raises ( TypeError , check_is_fitted , "<STR_LIT>" , "<STR_LIT>" ) <EOL> ard = ARDRegression ( ) <EOL> svr = SVR ( ) <EOL> try : <EOL> assert_raises ( NotFittedError , check_is_fitted , ard , "<STR_LIT>" ) <EOL> assert_raises ( NotFittedError , check_is_fitted , svr , "<STR_LIT>" ) <EOL> except ValueError : <EOL> assert False , "<STR_LIT>" <EOL> try : <EOL> check_is_fitted ( ard , "<STR_LIT>" , "<STR_LIT>" ) <EOL> except ValueError as e : <EOL> assert_equal ( str ( e ) , "<STR_LIT>" ) <EOL> try : <EOL> check_is_fitted ( svr , "<STR_LIT>" , "<STR_LIT>" ) <EOL> except AttributeError as e : <EOL> assert_equal ( str ( e ) , "<STR_LIT>" ) <EOL> ard . fit ( * make_blobs ( ) ) <EOL> svr . fit ( * make_blobs ( ) ) <EOL> assert_equal ( None , check_is_fitted ( ard , "<STR_LIT>" ) ) <EOL> assert_equal ( None , check_is_fitted ( svr , "<STR_LIT>" ) ) <EOL> def test_check_consistent_length ( ) : <EOL> check_consistent_length ( [ <NUM_LIT:1> ] , [ <NUM_LIT:2> ] , [ <NUM_LIT:3> ] , [ <NUM_LIT:4> ] , [ <NUM_LIT:5> ] ) <EOL> check_consistent_length ( [ [ <NUM_LIT:1> , <NUM_LIT:2> ] , [ [ <NUM_LIT:1> , <NUM_LIT:2> ] ] ] , [ <NUM_LIT:1> , <NUM_LIT:2> ] , [ '<STR_LIT:a>' , '<STR_LIT:b>' ] ) <EOL> check_consistent_length ( [ <NUM_LIT:1> ] , ( <NUM_LIT:2> , ) , np . array ( [ <NUM_LIT:3> ] ) , sp . csr_matrix ( ( <NUM_LIT:1> , <NUM_LIT:2> ) ) ) <EOL> assert_raises_regexp ( ValueError , '<STR_LIT>' , <EOL> check_consistent_length , [ <NUM_LIT:1> , <NUM_LIT:2> ] , [ <NUM_LIT:1> ] ) <EOL> assert_raises_regexp ( TypeError , '<STR_LIT>' , <EOL> check_consistent_length , [ <NUM_LIT:1> , <NUM_LIT:2> ] , <NUM_LIT:1> ) <EOL> assert_raises_regexp ( TypeError , '<STR_LIT>' , <EOL> check_consistent_length , [ <NUM_LIT:1> , <NUM_LIT:2> ] , object ( ) ) <EOL> assert_raises ( TypeError , check_consistent_length , [ <NUM_LIT:1> , <NUM_LIT:2> ] , np . array ( <NUM_LIT:1> ) ) <EOL> assert_raises_regexp ( TypeError , '<STR_LIT>' , check_consistent_length , <EOL> [ <NUM_LIT:1> , <NUM_LIT:2> ] , RandomForestRegressor ( ) ) </s>
