o
    ?Hh>8                     @   s  d Z ddlZddlZddlZddlZddlmZ ddlmZ ddl	m
Z
 ddlZddlmZ ddlZddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZmZ ddlmZ ddlmZmZ ddlm Z m!Z!m"Z"m#Z# ddl$m%Z% ddl&m'Z'm(Z(m)Z) ddl*m+Z+m,Z, ddl-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z: ddl;m<Z< dd Z=d[ddZ>G dd deZ?ej@Adee>dddfe>dfee>ddfedd d!fedd"d#d$d%d&fe? d'fgd(d) ZBe:eCe) e(d*d+d, ZDd-d. ZEej@jFe<d/d0ej@Gd1d2d3 ZHd4d5 ZId6d7 ZJd8d9 ZKd:d; ZLeCe
e) eedd geL ZMej@jAd<eMe'd=d>d? ZNd@dAgZOdBdC ZPdDdE e)dFD ZQej@jAdFeQe'd=dGdH ZRdIdE e) D ZSej@jAd<eSe'd=dJdK ZTej@jAd<eCe) e'd=dLdM ZUeCe
e)dFee# e! e"dNdOe dPdQgZVej@jAd<eVe'd=dRdS ZWej@jAd<eVe'd=ej@AdTe6e1e7e2gdUdV ZXej@jAd<e) e'd=dWdX ZYdYdZ ZZdS )\z.
General tests for all estimators in sklearn.
    N)partial)isgenerator)chain)LinAlgWarning)BaseEstimator)ColumnTransformer)make_classification)ConvergenceWarning)enable_halving_search_cvenable_iterative_imputer)LogisticRegression)FeatureUnionmake_pipeline)FunctionTransformerMinMaxScalerOneHotEncoderStandardScaler)all_estimators)_get_check_estimator_ids_get_expected_failed_checks_tested_estimators)SkipTestignore_warnings)(check_dataframe_column_names_consistencycheck_estimator!check_get_feature_names_out_error$check_global_output_transform_pandas(check_global_set_output_transform_polarscheck_inplace_ensure_writeablecheck_param_validationcheck_set_output_transform!check_set_output_transform_pandas!check_set_output_transform_polars'check_transformer_get_feature_names_out.check_transformer_get_feature_names_out_pandasparametrize_with_checks)_IS_WASMc                  C   s4   t  D ]\} }d| }|  drJ |qd S )NzDBase estimators such as {0} should not be included in all_estimatorsbase)r   formatlower
startswith)name	Estimatormsg r.   Y/home/air/sanwanet/gpt-api/venv/lib/python3.10/site-packages/sklearn/tests/test_common.py test_all_estimator_no_base_classB   s   r0      c                 C      d S Nr.   )xyr.   r.   r/   _sample_funcK      r6   c                   @   s   e Zd ZdZdd ZdS )CallableEstimatorzqDummy development stub for an estimator.

    This is to make sure a callable estimator passes common tests.
    c                 C   r2   r3   r.   )selfr.   r.   r/   __call__U   r7   zCallableEstimator.__call__N)__name__
__module____qualname____doc__r:   r.   r.   r.   r/   r8   O   s    r8   zval, expected)r5   z_sample_func(y=1)worldg       @)CzLogisticRegression(C=2.0)z	newton-cgbalancedT)random_statesolverclass_weight
warm_startz]LogisticRegression(class_weight='balanced',random_state=1,solver='newton-cg',warm_start=True)zCallableEstimator()c                 C   s   t | |ksJ d S r3   )r   )valexpectedr.   r.   r/   test_get_check_estimator_idsY   s   rH   )expected_failed_checksc                 C   s@   t ttttfd ||  W d    d S 1 sw   Y  d S Ncategory)r   FutureWarningr	   UserWarningr   )	estimatorcheckrequestr.   r.   r/   test_estimatorss   s
   

"rR   c                  C   sL   t jtdd tt dd} W d   n1 sw   Y  t| s$J dS )zTCheck that check_estimator with generate_only=True raises a deprecation
    warning.z$`generate_only` is deprecated in 1.6matchT)generate_onlyN)pytestwarnsrM   r   r   r   )all_instance_gen_checksr.   r.   r/   .test_check_estimator_generate_only_deprecation   s   rY   z,importlib not supported for Pyodide packages)reasonzZignore:Since version 1.0, it is not needed to import enable_hist_gradient_boosting anymorec                  C   s   t jtjg} tj| ddd d}dd |D }|dg D ](}d|v r%qd	|v r*qt|d
d}t|ddD ]}t	||sEJ d
||q6qd S )Nsklearn.c                 S   r2   r3   r.   _r.   r.   r/   <lambda>       z-test_import_all_consistency.<locals>.<lambda>)pathprefixonerrorc                 S   s   g | ]\}}}|qS r.   r.   ).0r]   modnamer.   r.   r/   
<listcomp>   s    z/test_import_all_consistency.<locals>.<listcomp>sklearnz.tests.zsklearn._build_utilsdummy)fromlist__all__r.   z#Module '{0}' has no attribute '{1}')osr`   dirnamerf   __file__pkgutilwalk_packages
__import__getattrhasattrr(   )sklearn_pathpkgssubmodsrd   packager+   r.   r.   r/   test_import_all_consistency   s"   
rv   c                  C   s`   t jtjg} d}tj| dd dD ]\}}}d|v s%|ds%||v r&q|tjv s-J qd S )N)utilstestsr'   conftestc                 S   r2   r3   r.   r\   r.   r.   r/   r^      r_   z3test_root_import_all_completeness.<locals>.<lambda>)r`   rb   .r]   )	rj   r`   rk   rf   rl   rm   rn   r*   ri   )rr   
EXCEPTIONSr]   rd   r.   r.   r/   !test_root_import_all_completeness   s   r|   c                     sn   t d h dtjtjg} dd tj| ddD  fdd	 D }|g ks5J d	
|d S )
Nz(?x)
                                      \.externals(\.|$)|
                                      \.tests(\.|$)|
                                      \._
                                      >   sklearn.datasets.datasklearn.datasets.descrsklearn.datasets.imagesc                 S   s   i | ]\}}}||qS r.   r.   )rc   r]   r+   ispkgr.   r.   r/   
<dictcomp>   s    z1test_all_tests_are_importable.<locals>.<dictcomp>r[   )ra   c                    s6   g | ]\}}|r|vr  |s|d  vr|qS )z.tests)search)rc   r+   r   HAS_TESTS_EXCEPTIONSlookupresource_modulesr.   r/   re      s    z1test_all_tests_are_importable.<locals>.<listcomp>zn{0} do not have `tests` subpackages. Perhaps they require __init__.py or a meson.build in the parent directory)recompilerj   r`   rk   rf   rl   rm   rn   itemsr(   )rr   missing_testsr.   r   r/   test_all_tests_are_importable   s   
r   c                  C   sx   d} t jt| d tt W d    n1 sw   Y  t jt| d ttg W d    d S 1 s5w   Y  d S )Nz8Passing a class was deprecated.* isn't supported anymorerS   )rV   raises	TypeErrorr   r   r%   r-   r.   r.   r/   test_class_support_removed   s   
"r   c                  c   s    t  D ]C} t|  }d|v r| jdddV  qd|v r<| jddd}|jjdv r8tj|tjj	dd	d
V  q|V  qd|v rG| jddV  qd S )N	oob_scoreT)r   	bootstrapearly_stoppingr1   )r   n_iter_no_change>   MLPRegressorMLPClassifierzMLP still validates in fitr   )marksr   )r   )
r   set
get_params
set_params	__class__r;   rV   parammarkxfail)rO   
est_paramsestr.   r.   r/   _estimators_that_predict_in_fit   s    

r   rO   )idsc              	   C   s   t | tr
td dt| v rtd ttd4 tjdd}t	| j
j|  W d    n1 s3w   Y  |D ]}dt|jvsEJ q:W d    d S 1 sQw   Y  d S )Nz$ColumnTransformer is not tested herer   zCEstimator does not support check_dataframe_column_names_consistencyrK   T)recordz was fitted without feature names)
isinstancer   rV   skipr   r   rM   warningscatch_warningsr   r   r;   strmessage)rO   r   warningr.   r.   r/   #test_pandas_column_name_consistency   s$   

"r   ensemblekernel_approximationc                 C   s&   t | drdS | jdd }|tvS )Nget_feature_names_outTrz   r1   )rq   r<   split"GET_FEATURES_OUT_MODULES_TO_IGNORE)transformermoduler.   r.   r/   '_include_in_get_feature_names_out_check  s   
r   c                 C   s   g | ]}t |r|qS r.   )r   rc   r   r.   r.   r/   re   "  s    re   r   c                 C   sL   t td t| jj|  t| jj|  W d    d S 1 sw   Y  d S rJ   )r   rM   r#   r   r;   r$   )r   r.   r.   r/   'test_transformers_get_feature_names_out)  s   "r   c                 C   s   g | ]	}t |d r|qS )r   )rq   r   r.   r.   r/   re   7  s
    
c                 C   s   | j j}t||  d S r3   )r   r;   r   )rO   estimator_namer.   r.   r/   +test_estimators_get_feature_names_out_error<  s   r   c                 C   s*   t | tr
td | jj}t||  d S )NzFeatureUnion is not tested here)r   r   rV   r   r   r;   r   rO   r+   r.   r.   r/   test_check_param_validationD  s   

r   F)sparse_outputz
one-to-one)feature_names_outc                 C   sb   | j j}t| dstd| d ttd t| j j|  W d    d S 1 s*w   Y  d S )N
set_outputz(Skipping check_set_output_transform for z!: Does not support set_output APIrK   )r   r;   rq   rV   r   r   rM   r    r   r.   r.   r/   test_set_output_transformZ  s   

"r   
check_funcc                 C   sj   | j j}t| dstd|j d| d ttd || j j|  W d    d S 1 s.w   Y  d S )Nr   z	Skipping z for z%: Does not support set_output API yetrK   )r   r;   rq   rV   r   r   rM   )rO   r   r+   r.   r.   r/   $test_set_output_transform_configuredh  s   
"r   c                 C   s   | j j}t| dr| jdd nt| dr| jdd nt| d|dkr.| jdd	d
 |dkr8| jdd |dkrB| jdd t||  d S )NcopyF)r   copy_X)r   z! doesn't require writeable input.HDBSCANprecomputedbrute)metric	algorithmPCAfull)
svd_solver	KernelPCA)kernel)r   r;   rq   r   r   r   r   r.   r.   r/   #test_check_inplace_ensure_writeable  s   

r   c                  C   s   G dd dt } tdddd\}}|  }tt}|| W d   n1 s)w   Y  t|dks6J t|d j	d	sBJ t|d
 j	dsNJ t|d j	dsZJ dS )zThis test checks that we raised deprecation warning explaining how to transition
    to the new developer public API from 1.5 to 1.6.
    c                   @   s   e Zd ZdddZdd ZdS )z=test_transition_public_api_deprecations.<locals>.OldEstimatorNc                 S   s*   |  |}| j|dd | j|dd | S )NT)reset)_validate_data_check_n_features_check_feature_names)r9   Xr5   r.   r.   r/   fit  s   
zAtest_transition_public_api_deprecations.<locals>.OldEstimator.fitc                 S   s   |S r3   r.   )r9   r   r.   r.   r/   	transform  r7   zGtest_transition_public_api_deprecations.<locals>.OldEstimator.transformr3   )r;   r<   r=   r   r   r.   r.   r.   r/   OldEstimator  s    
r   
      r   )	n_samples
n_featuresrB   N   z,`BaseEstimator._validate_data` is deprecatedr1   z/`BaseEstimator._check_n_features` is deprecated   z2`BaseEstimator._check_feature_names` is deprecated)
r   r   rV   rW   rM   r   lenr   r   r*   )r   r   r5   old_estimatorwarning_listr.   r.   r/   'test_transition_public_api_deprecations  s    
r   )r1   )[r>   rj   rm   r   r   	functoolsr   inspectr   	itertoolsr   rV   scipy.linalgr   rf   sklearn.baser   sklearn.composer   sklearn.datasetsr   sklearn.exceptionsr	   sklearn.experimentalr
   r   sklearn.linear_modelr   sklearn.pipeliner   r   sklearn.preprocessingr   r   r   r   sklearn.utilsr   -sklearn.utils._test_common.instance_generatorr   r   r   sklearn.utils._testingr   r   sklearn.utils.estimator_checksr   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   sklearn.utils.fixesr&   r0   r6   r8   r   parametrizerH   listrR   rY   r   filterwarningsrv   r|   r   r   r   column_name_estimatorsr   r   r   GET_FEATURES_OUT_ESTIMATORSr   %ESTIMATORS_WITH_GET_FEATURE_NAMES_OUTr   r   SET_OUTPUT_ESTIMATORSr   r   r   r   r.   r.   r.   r/   <module>   s    <
	



	
&	




	
