o
    &ѹg>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)nameZ	Estimatormsg r-   ]/Users/admin/.pyenv/versions/3.10.0/lib/python3.10/site-packages/sklearn/tests/test_common.py test_all_estimator_no_base_classB   s   r/      c                 C      d S Nr-   )xyr-   r-   r.   _sample_funcK      r5   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   r1   r2   r-   )selfr-   r-   r.   __call__U   r6   zCallableEstimator.__call__N)__name__
__module____qualname____doc__r9   r-   r-   r-   r.   r7   O   s    r7   zval, expected)r4   z_sample_func(y=1)Zworldg       @)CzLogisticRegression(C=2.0)z	newton-cgZbalancedT)random_stateZsolverZclass_weightZ
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 r2   )r   )valexpectedr-   r-   r.   test_get_check_estimator_idsY   s   rB   )Z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
   

"rK   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)Zgenerate_onlyN)pytestwarnsrF   r   r   r   )Zall_instance_gen_checksr-   r-   r.   .test_check_estimator_generate_only_deprecation   s   rP   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   r1   r2   r-   _r-   r-   r.   <lambda>       z-test_import_all_consistency.<locals>.<lambda>)pathprefixonerrorc                 S   s   g | ]\}}}|qS r-   r-   ).0rT   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}')osrW   dirnamer]   __file__pkgutilwalk_packages
__import__getattrhasattrr(   )sklearn_pathpkgsZsubmodsr[   packager+   r-   r-   r.   test_import_all_consistency   s"   
rl   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'   Zconftestc                 S   r1   r2   r-   rS   r-   r-   r.   rU      rV   z3test_root_import_all_completeness.<locals>.<lambda>)rW   rY   .rT   )	ra   rW   rb   r]   rc   rd   re   r*   r`   )ri   Z
EXCEPTIONSrT   r[   r-   r-   r.   !test_root_import_all_completeness   s   rp   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(\.|$)|
                                      \._
                                      >   zsklearn.datasets.imageszsklearn.datasets.datazsklearn.datasets.descrc                 S   s   i | ]\}}}||qS r-   r-   )rZ   rT   r+   ispkgr-   r-   r.   
<dictcomp>   s    z1test_all_tests_are_importable.<locals>.<dictcomp>rR   )rX   c                    s6   g | ]\}}|r|vr  |s|d  vr|qS )z.tests)search)rZ   r+   rq   ZHAS_TESTS_EXCEPTIONSlookupZresource_modulesr-   r.   r\      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compilera   rW   rb   r]   rc   rd   re   itemsr(   )ri   Zmissing_testsr-   rt   r.   test_all_tests_are_importable   s   
ry   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 anymorerL   )rN   Z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}   Z	bootstrapearly_stoppingr0   )r~   n_iter_no_change>   ZMLPRegressorZMLPClassifierzMLP still validates in fitr{   )Zmarksr   )r   )
r   set
get_params
set_params	__class__r:   rN   parammarkxfail)rH   Z
est_paramsestr-   r-   r.   _estimators_that_predict_in_fit   s    

r   rH   )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_consistencyrD   T)recordz was fitted without feature names)
isinstancer   rN   skipr   r   rF   warningscatch_warningsr   r   r:   strmessage)rH   r   warningr-   r-   r.   #test_pandas_column_name_consistency   s$   

"r   ZensembleZkernel_approximationc                 C   s&   t | drdS | jdd }|tvS )Nget_feature_names_outTro   r0   )rh   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   rZ   r   r-   r-   r.   r\   "  s    r\   r   c                 C   sL   t td t| jj|  t| jj|  W d    d S 1 sw   Y  d S rC   )r   rF   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   )rh   r   r-   r-   r.   r\   7  s
    
c                 C   s   | j j}t||  d S r2   )r   r:   r   )rH   Z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   rN   r   r   r:   r   rH   r+   r-   r-   r.   test_check_param_validationD  s   

r   F)Zsparse_outputz
one-to-one)Z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 APIrD   )r   r:   rh   rN   r   r   rF   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 yetrD   )r   r:   rh   rN   r   r   rF   )rH   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.ZHDBSCANZprecomputedZbrute)Zmetric	algorithmZPCAfull)Z
svd_solverZ	KernelPCA)kernel)r   r:   rh   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)Z_validate_dataZ_check_n_featuresZ_check_feature_names)r8   Xr4   r-   r-   r.   fit  s   
zAtest_transition_public_api_deprecations.<locals>.OldEstimator.fitc                 S   s   |S r2   r-   )r8   r   r-   r-   r.   	transform  r6   zGtest_transition_public_api_deprecations.<locals>.OldEstimator.transformr2   )r:   r;   r<   r   r   r-   r-   r-   r.   OldEstimator  s    
r   
      r   )Z	n_samplesZ
n_featuresr?   N   z,`BaseEstimator._validate_data` is deprecatedr0   z/`BaseEstimator._check_n_features` is deprecated   z2`BaseEstimator._check_feature_names` is deprecated)
r   r   rN   rO   rF   r   lenr   r   r*   )r   r   r4   Zold_estimatorZwarning_listr-   r-   r.   'test_transition_public_api_deprecations  s    
r   )r0   )[r=   ra   rd   rv   r   	functoolsr   inspectr   	itertoolsr   rN   Zscipy.linalgr   r]   Zsklearn.baser   Zsklearn.composer   Zsklearn.datasetsr   Zsklearn.exceptionsr	   Zsklearn.experimentalr
   r   Zsklearn.linear_modelr   Zsklearn.pipeliner   r   Zsklearn.preprocessingr   r   r   r   Zsklearn.utilsr   Z-sklearn.utils._test_common.instance_generatorr   r   r   Zsklearn.utils._testingr   r   Zsklearn.utils.estimator_checksr   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   Zsklearn.utils.fixesr&   r/   r5   r7   r   ZparametrizerB   listrK   rP   r   filterwarningsrl   rp   ry   r|   r   Zcolumn_name_estimatorsr   r   r   ZGET_FEATURES_OUT_ESTIMATORSr   Z%ESTIMATORS_WITH_GET_FEATURE_NAMES_OUTr   r   ZSET_OUTPUT_ESTIMATORSr   r   r   r   r-   r-   r-   r.   <module>   s    <
	



	
&	




	
