<s> """<STR_LIT>""" <EOL> import os <EOL> import inspect <EOL> import pkgutil <EOL> import warnings <EOL> import sys <EOL> import re <EOL> import platform <EOL> import struct <EOL> import scipy as sp <EOL> import scipy . io <EOL> from functools import wraps <EOL> from operator import itemgetter <EOL> try : <EOL> from urllib2 import urlopen <EOL> from urllib2 import HTTPError <EOL> except ImportError : <EOL> from urllib . request import urlopen <EOL> from urllib . error import HTTPError <EOL> import tempfile <EOL> import shutil <EOL> import os . path as op <EOL> import atexit <EOL> try : <EOL> WindowsError <EOL> except NameError : <EOL> WindowsError = None <EOL> import sklearn <EOL> from sklearn . base import BaseEstimator <EOL> from sklearn . externals import joblib <EOL> from nose . tools import assert_equal <EOL> from nose . tools import assert_not_equal <EOL> from nose . tools import assert_true <EOL> from nose . tools import assert_false <EOL> from nose . tools import assert_raises <EOL> from nose . tools import raises <EOL> from nose import SkipTest <EOL> from nose import with_setup <EOL> from numpy . testing import assert_almost_equal <EOL> from numpy . testing import assert_array_equal <EOL> from numpy . testing import assert_array_almost_equal <EOL> from numpy . testing import assert_array_less <EOL> from numpy . testing import assert_approx_equal <EOL> import numpy as np <EOL> from sklearn . base import ( ClassifierMixin , RegressorMixin , TransformerMixin , <EOL> ClusterMixin ) <EOL> from sklearn . cluster import DBSCAN <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" ] <EOL> try : <EOL> from nose . tools import assert_in , assert_not_in <EOL> except ImportError : <EOL> def assert_in ( x , container ) : <EOL> assert_true ( x in container , msg = "<STR_LIT>" % ( x , container ) ) <EOL> def assert_not_in ( x , container ) : <EOL> assert_false ( x in container , msg = "<STR_LIT>" % ( x , container ) ) <EOL> try : <EOL> from nose . tools import assert_raises_regex <EOL> except ImportError : <EOL> def assert_raises_regex ( expected_exception , expected_regexp , <EOL> callable_obj = None , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> not_raised = False <EOL> try : <EOL> callable_obj ( * args , ** kwargs ) <EOL> not_raised = True <EOL> except expected_exception as e : <EOL> error_message = str ( e ) <EOL> if not re . compile ( expected_regexp ) . search ( error_message ) : <EOL> raise AssertionError ( "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( expected_regexp , error_message ) ) <EOL> if not_raised : <EOL> raise AssertionError ( "<STR_LIT>" % <EOL> ( expected_exception . __name__ , <EOL> callable_obj . __name__ ) ) <EOL> assert_raises_regexp = assert_raises_regex <EOL> def _assert_less ( a , b , msg = None ) : <EOL> message = "<STR_LIT>" % ( a , b ) <EOL> if msg is not None : <EOL> message += "<STR_LIT>" + msg <EOL> assert a < b , message <EOL> def _assert_greater ( a , b , msg = None ) : <EOL> message = "<STR_LIT>" % ( a , b ) <EOL> if msg is not None : <EOL> message += "<STR_LIT>" + msg <EOL> assert a > b , message <EOL> def assert_less_equal ( a , b , msg = None ) : <EOL> message = "<STR_LIT>" % ( a , b ) <EOL> if msg is not None : <EOL> message += "<STR_LIT>" + msg <EOL> assert a <= b , message <EOL> def assert_greater_equal ( a , b , msg = None ) : <EOL> message = "<STR_LIT>" % ( a , b ) <EOL> if msg is not None : <EOL> message += "<STR_LIT>" + msg <EOL> assert a >= b , message <EOL> def assert_warns ( warning_class , func , * args , ** kw ) : <EOL> """<STR_LIT>""" <EOL> clean_warning_registry ( ) <EOL> with warnings . catch_warnings ( record = True ) as w : <EOL> warnings . simplefilter ( "<STR_LIT>" ) <EOL> result = func ( * args , ** kw ) <EOL> if hasattr ( np , '<STR_LIT>' ) : <EOL> w = [ e for e in w <EOL> if e . category is not np . VisibleDeprecationWarning ] <EOL> if not len ( w ) > <NUM_LIT:0> : <EOL> raise AssertionError ( "<STR_LIT>" <EOL> % func . __name__ ) <EOL> found = any ( warning . category is warning_class for warning in w ) <EOL> if not found : <EOL> raise AssertionError ( "<STR_LIT>" <EOL> % ( func . __name__ , warning_class , w ) ) <EOL> return result <EOL> def assert_warns_message ( warning_class , message , func , * args , ** kw ) : <EOL> """<STR_LIT>""" <EOL> clean_warning_registry ( ) <EOL> with warnings . catch_warnings ( record = True ) as w : <EOL> warnings . simplefilter ( "<STR_LIT>" ) <EOL> if hasattr ( np , '<STR_LIT>' ) : <EOL> warnings . simplefilter ( '<STR_LIT:ignore>' , np . VisibleDeprecationWarning ) <EOL> result = func ( * args , ** kw ) <EOL> if not len ( w ) > <NUM_LIT:0> : <EOL> raise AssertionError ( "<STR_LIT>" <EOL> % func . __name__ ) <EOL> found = [ issubclass ( warning . category , warning_class ) for warning in w ] <EOL> if not any ( found ) : <EOL> raise AssertionError ( "<STR_LIT>" <EOL> "<STR_LIT:%s>" <EOL> % ( func . __name__ , warning_class ) ) <EOL> message_found = False <EOL> for index in [ i for i , x in enumerate ( found ) if x ] : <EOL> msg = w [ index ] . message <EOL> msg = str ( msg . args [ <NUM_LIT:0> ] if hasattr ( msg , '<STR_LIT:args>' ) else msg ) <EOL> if callable ( message ) : <EOL> check_in_message = message <EOL> else : <EOL> check_in_message = lambda msg : message in msg <EOL> if check_in_message ( msg ) : <EOL> message_found = True <EOL> break <EOL> if not message_found : <EOL> raise AssertionError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( message , func . __name__ , msg ) ) <EOL> return result <EOL> def assert_no_warnings ( func , * args , ** kw ) : <EOL> clean_warning_registry ( ) <EOL> with warnings . catch_warnings ( record = True ) as w : <EOL> warnings . simplefilter ( '<STR_LIT>' ) <EOL> result = func ( * args , ** kw ) <EOL> if hasattr ( np , '<STR_LIT>' ) : <EOL> w = [ e for e in w <EOL> if e . category is not np . VisibleDeprecationWarning ] <EOL> if len ( w ) > <NUM_LIT:0> : <EOL> raise AssertionError ( "<STR_LIT>" <EOL> % ( func . __name__ , w ) ) <EOL> return result <EOL> def ignore_warnings ( obj = None , category = Warning ) : <EOL> """<STR_LIT>""" <EOL> if callable ( obj ) : <EOL> return _IgnoreWarnings ( category = category ) ( obj ) <EOL> else : <EOL> return _IgnoreWarnings ( category = category ) <EOL> class _IgnoreWarnings ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , category ) : <EOL> self . _record = True <EOL> self . _module = sys . modules [ '<STR_LIT>' ] <EOL> self . _entered = False <EOL> self . log = [ ] <EOL> self . category = category <EOL> def __call__ ( self , fn ) : <EOL> """<STR_LIT>""" <EOL> @ wraps ( fn ) <EOL> def wrapper ( * args , ** kwargs ) : <EOL> clean_warning_registry ( ) <EOL> with warnings . catch_warnings ( ) : <EOL> warnings . simplefilter ( "<STR_LIT:ignore>" , self . category ) <EOL> return fn ( * args , ** kwargs ) <EOL> return wrapper <EOL> def __repr__ ( self ) : <EOL> args = [ ] <EOL> if self . _record : <EOL> args . append ( "<STR_LIT>" ) <EOL> if self . _module is not sys . modules [ '<STR_LIT>' ] : <EOL> args . append ( "<STR_LIT>" % self . _module ) <EOL> name = type ( self ) . __name__ <EOL> return "<STR_LIT>" % ( name , "<STR_LIT:U+002CU+0020>" . join ( args ) ) <EOL> def __enter__ ( self ) : <EOL> clean_warning_registry ( ) <EOL> warnings . simplefilter ( "<STR_LIT:ignore>" , self . category ) <EOL> if self . _entered : <EOL> raise RuntimeError ( "<STR_LIT>" % self ) <EOL> self . _entered = True <EOL> self . _filters = self . _module . filters <EOL> self . _module . filters = self . _filters [ : ] <EOL> self . _showwarning = self . _module . showwarning <EOL> def __exit__ ( self , * exc_info ) : <EOL> if not self . _entered : <EOL> raise RuntimeError ( "<STR_LIT>" % self ) <EOL> self . _module . filters = self . _filters <EOL> self . _module . showwarning = self . _showwarning <EOL> self . log [ : ] = [ ] <EOL> clean_warning_registry ( ) <EOL> try : <EOL> from nose . tools import assert_less <EOL> except ImportError : <EOL> assert_less = _assert_less <EOL> try : <EOL> from nose . tools import assert_greater <EOL> except ImportError : <EOL> assert_greater = _assert_greater <EOL> def _assert_allclose ( actual , desired , rtol = <NUM_LIT> , atol = <NUM_LIT:0> , <EOL> err_msg = '<STR_LIT>' , verbose = True ) : <EOL> actual , desired = np . asanyarray ( actual ) , np . asanyarray ( desired ) <EOL> if np . allclose ( actual , desired , rtol = rtol , atol = atol ) : <EOL> return <EOL> msg = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % ( rtol , atol , actual , desired ) <EOL> raise AssertionError ( msg ) <EOL> if hasattr ( np . testing , '<STR_LIT>' ) : <EOL> assert_allclose = np . testing . assert_allclose <EOL> else : <EOL> assert_allclose = _assert_allclose <EOL> def assert_raise_message ( exceptions , message , function , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> function ( * args , ** kwargs ) <EOL> except exceptions as e : <EOL> error_message = str ( e ) <EOL> if message not in error_message : <EOL> raise AssertionError ( "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( message , error_message ) ) <EOL> else : <EOL> if isinstance ( exceptions , tuple ) : <EOL> names = "<STR_LIT>" . join ( e . __name__ for e in exceptions ) <EOL> else : <EOL> names = exceptions . __name__ <EOL> raise AssertionError ( "<STR_LIT>" % <EOL> ( names , function . __name__ ) ) <EOL> def fake_mldata ( columns_dict , dataname , matfile , ordering = None ) : <EOL> """<STR_LIT>""" <EOL> datasets = dict ( columns_dict ) <EOL> for name in datasets : <EOL> datasets [ name ] = datasets [ name ] . T <EOL> if ordering is None : <EOL> ordering = sorted ( list ( datasets . keys ( ) ) ) <EOL> datasets [ '<STR_LIT>' ] = sp . empty ( ( <NUM_LIT:1> , len ( ordering ) ) , <EOL> dtype = '<STR_LIT:object>' ) <EOL> for i , name in enumerate ( ordering ) : <EOL> datasets [ '<STR_LIT>' ] [ <NUM_LIT:0> , i ] = name <EOL> scipy . io . savemat ( matfile , datasets , oned_as = '<STR_LIT>' ) <EOL> class mock_mldata_urlopen ( object ) : <EOL> def __init__ ( self , mock_datasets ) : <EOL> """<STR_LIT>""" <EOL> self . mock_datasets = mock_datasets <EOL> def __call__ ( self , urlname ) : <EOL> dataset_name = urlname . split ( '<STR_LIT:/>' ) [ - <NUM_LIT:1> ] <EOL> if dataset_name in self . mock_datasets : <EOL> resource_name = '<STR_LIT:_>' + dataset_name <EOL> from io import BytesIO <EOL> matfile = BytesIO ( ) <EOL> dataset = self . mock_datasets [ dataset_name ] <EOL> ordering = None <EOL> if isinstance ( dataset , tuple ) : <EOL> dataset , ordering = dataset <EOL> fake_mldata ( dataset , resource_name , matfile , ordering ) <EOL> matfile . seek ( <NUM_LIT:0> ) <EOL> return matfile <EOL> else : <EOL> raise HTTPError ( urlname , <NUM_LIT> , dataset_name + "<STR_LIT>" , <EOL> [ ] , None ) <EOL> def install_mldata_mock ( mock_datasets ) : <EOL> from sklearn import datasets <EOL> datasets . mldata . urlopen = mock_mldata_urlopen ( mock_datasets ) <EOL> def uninstall_mldata_mock ( ) : <EOL> from sklearn import datasets <EOL> datasets . mldata . urlopen = urlopen <EOL> META_ESTIMATORS = [ "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> OTHER = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" ] <EOL> DONT_TEST = [ '<STR_LIT>' , '<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>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<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> def all_estimators ( include_meta_estimators = False , <EOL> include_other = False , type_filter = None , <EOL> include_dont_test = False ) : <EOL> """<STR_LIT>""" <EOL> def is_abstract ( c ) : <EOL> if not ( hasattr ( c , '<STR_LIT>' ) ) : <EOL> return False <EOL> if not len ( c . __abstractmethods__ ) : <EOL> return False <EOL> return True <EOL> all_classes = [ ] <EOL> path = sklearn . __path__ <EOL> for importer , modname , ispkg in pkgutil . walk_packages ( <EOL> path = path , prefix = '<STR_LIT>' , onerror = lambda x : None ) : <EOL> if ( "<STR_LIT>" in modname ) : <EOL> continue <EOL> module = __import__ ( modname , fromlist = "<STR_LIT>" ) <EOL> classes = inspect . getmembers ( module , inspect . isclass ) <EOL> all_classes . extend ( classes ) <EOL> all_classes = set ( all_classes ) <EOL> estimators = [ c for c in all_classes <EOL> if ( issubclass ( c [ <NUM_LIT:1> ] , BaseEstimator ) and <EOL> c [ <NUM_LIT:0> ] != '<STR_LIT>' ) ] <EOL> estimators = [ c for c in estimators if not is_abstract ( c [ <NUM_LIT:1> ] ) ] <EOL> if not include_dont_test : <EOL> estimators = [ c for c in estimators if not c [ <NUM_LIT:0> ] in DONT_TEST ] <EOL> if not include_other : <EOL> estimators = [ c for c in estimators if not c [ <NUM_LIT:0> ] in OTHER ] <EOL> if not include_meta_estimators : <EOL> estimators = [ c for c in estimators if not c [ <NUM_LIT:0> ] in META_ESTIMATORS ] <EOL> if type_filter is not None : <EOL> if not isinstance ( type_filter , list ) : <EOL> type_filter = [ type_filter ] <EOL> else : <EOL> type_filter = list ( type_filter ) <EOL> filtered_estimators = [ ] <EOL> filters = { '<STR_LIT>' : ClassifierMixin , <EOL> '<STR_LIT>' : RegressorMixin , <EOL> '<STR_LIT>' : TransformerMixin , <EOL> '<STR_LIT>' : ClusterMixin } <EOL> for name , mixin in filters . items ( ) : <EOL> if name in type_filter : <EOL> type_filter . remove ( name ) <EOL> filtered_estimators . extend ( [ est for est in estimators <EOL> if issubclass ( est [ <NUM_LIT:1> ] , mixin ) ] ) <EOL> estimators = filtered_estimators <EOL> if type_filter : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % repr ( type_filter ) ) <EOL> return sorted ( set ( estimators ) , key = itemgetter ( <NUM_LIT:0> ) ) <EOL> def set_random_state ( estimator , random_state = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( estimator , DBSCAN ) : <EOL> return <EOL> if "<STR_LIT>" in estimator . get_params ( ) : <EOL> estimator . set_params ( random_state = random_state ) <EOL> def if_matplotlib ( func ) : <EOL> """<STR_LIT>""" <EOL> @ wraps ( func ) <EOL> def run_test ( * args , ** kwargs ) : <EOL> try : <EOL> import matplotlib <EOL> matplotlib . use ( '<STR_LIT>' , warn = False ) <EOL> import matplotlib . pyplot as plt <EOL> plt . figure ( ) <EOL> except ImportError : <EOL> raise SkipTest ( '<STR_LIT>' ) <EOL> else : <EOL> return func ( * args , ** kwargs ) <EOL> return run_test <EOL> def skip_if_32bit ( func ) : <EOL> """<STR_LIT>""" <EOL> @ wraps ( func ) <EOL> def run_test ( * args , ** kwargs ) : <EOL> bits = <NUM_LIT:8> * struct . calcsize ( "<STR_LIT:P>" ) <EOL> if bits == <NUM_LIT:32> : <EOL> raise SkipTest ( '<STR_LIT>' ) <EOL> else : <EOL> return func ( * args , ** kwargs ) <EOL> return run_test <EOL> def if_not_mac_os ( versions = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> message = '<STR_LIT>' <EOL> '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> warnings . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> DeprecationWarning ) <EOL> mac_version , _ , _ = platform . mac_ver ( ) <EOL> skip = '<STR_LIT:.>' . join ( mac_version . split ( '<STR_LIT:.>' ) [ : <NUM_LIT:2> ] ) in versions <EOL> def decorator ( func ) : <EOL> if skip : <EOL> @ wraps ( func ) <EOL> def func ( * args , ** kwargs ) : <EOL> raise SkipTest ( message ) <EOL> return func <EOL> return decorator <EOL> def if_safe_multiprocessing_with_blas ( func ) : <EOL> """<STR_LIT>""" <EOL> @ wraps ( func ) <EOL> def run_test ( * args , ** kwargs ) : <EOL> if sys . platform == '<STR_LIT>' : <EOL> raise SkipTest ( <EOL> "<STR_LIT>" ) <EOL> return func ( * args , ** kwargs ) <EOL> return run_test <EOL> def clean_warning_registry ( ) : <EOL> """<STR_LIT>""" <EOL> warnings . resetwarnings ( ) <EOL> reg = "<STR_LIT>" <EOL> for mod_name , mod in list ( sys . modules . items ( ) ) : <EOL> if '<STR_LIT>' in mod_name : <EOL> continue <EOL> if hasattr ( mod , reg ) : <EOL> getattr ( mod , reg ) . clear ( ) <EOL> def check_skip_network ( ) : <EOL> if int ( os . environ . get ( '<STR_LIT>' , <NUM_LIT:0> ) ) : <EOL> raise SkipTest ( "<STR_LIT>" ) <EOL> def check_skip_travis ( ) : <EOL> """<STR_LIT>""" <EOL> if os . environ . get ( '<STR_LIT>' ) == "<STR_LIT:true>" : <EOL> raise SkipTest ( "<STR_LIT>" ) <EOL> def _delete_folder ( folder_path , warn = False ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if os . path . exists ( folder_path ) : <EOL> shutil . rmtree ( folder_path ) <EOL> except WindowsError : <EOL> if warn : <EOL> warnings . warn ( "<STR_LIT>" % folder_path ) <EOL> class TempMemmap ( object ) : <EOL> def __init__ ( self , data , mmap_mode = '<STR_LIT:r>' ) : <EOL> self . temp_folder = tempfile . mkdtemp ( prefix = '<STR_LIT>' ) <EOL> self . mmap_mode = mmap_mode <EOL> self . data = data <EOL> def __enter__ ( self ) : <EOL> fpath = op . join ( self . temp_folder , '<STR_LIT>' ) <EOL> joblib . dump ( self . data , fpath ) <EOL> data_read_only = joblib . load ( fpath , mmap_mode = self . mmap_mode ) <EOL> atexit . register ( lambda : _delete_folder ( self . temp_folder , warn = True ) ) <EOL> return data_read_only <EOL> def __exit__ ( self , exc_type , exc_val , exc_tb ) : <EOL> _delete_folder ( self . temp_folder ) <EOL> with_network = with_setup ( check_skip_network ) <EOL> with_travis = with_setup ( check_skip_travis ) </s>
