a
    h>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-   U/var/www/html/assistant/venv/lib/python3.9/site-packages/sklearn/tests/test_common.py test_all_estimator_no_base_classB   s    r/      c                 C   s   d S Nr-   )xyr-   r-   r.   _sample_funcK   s    r4   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   s   d S r1   r-   )selfr-   r-   r.   __call__U   s    zCallableEstimator.__call__N)__name__
__module____qualname____doc__r7   r-   r-   r-   r.   r5   O   s   r5   zval, expected)r3   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 r1   )r   )valexpectedr-   r-   r.   test_get_check_estimator_idsY   s    r@   )Zexpected_failed_checksc                 C   s>   t ttttfd ||  W d    n1 s00    Y  d S Ncategory)r   FutureWarningr	   UserWarningr   )	estimatorcheckrequestr-   r-   r.   test_estimatorss   s    
rI   c                  C   sL   t jtdd tt dd} W d   n1 s20    Y  t| sHJ 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warnsrD   r   r   r   )Zall_instance_gen_checksr-   r-   r.   .test_check_estimator_generate_only_deprecation   s
    $rN   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 ]P}d|v rJq<d	|v rTq<t|d
d}t|ddD ]}t	||slJ d
||qlq<d S )Nsklearn.c                 S   s   d S r1   r-   _r-   r-   r.   <lambda>       z-test_import_all_consistency.<locals>.<lambda>)pathprefixonerrorc                 S   s   g | ]\}}}|qS r-   r-   ).0rR   modnamer-   r-   r.   
<listcomp>   rT   z/test_import_all_consistency.<locals>.<listcomp>sklearnz.tests.zsklearn._build_utilsdummy)fromlist__all__r-   z#Module '{0}' has no attribute '{1}')osrU   dirnamer[   __file__pkgutilwalk_packages
__import__getattrhasattrr(   )sklearn_pathpkgsZsubmodsrY   packager+   r-   r-   r.   test_import_all_consistency   s    
rj   c                  C   s`   t jtjg} d}tj| dd dD ]4\}}}d|v s&|ds&||v rLq&|tjv s&J q&d S )N)utilsZtestsr'   Zconftestc                 S   s   d S r1   r-   rQ   r-   r-   r.   rS      rT   z3test_root_import_all_completeness.<locals>.<lambda>)rU   rW   .rR   )	r_   rU   r`   r[   ra   rb   rc   r*   r^   )rg   Z
EXCEPTIONSrR   rY   r-   r-   r.   !test_root_import_all_completeness   s    rm   c                     sn   t d h dtjtjg} dd tj| ddD  fdd	 D }|g ksjJ d	
|d S )
Nz(?x)
                                      \.externals(\.|$)|
                                      \.tests(\.|$)|
                                      \._
                                      >   zsklearn.datasets.datazsklearn.datasets.imageszsklearn.datasets.descrc                 S   s   i | ]\}}}||qS r-   r-   )rX   rR   r+   ispkgr-   r-   r.   
<dictcomp>   s   z1test_all_tests_are_importable.<locals>.<dictcomp>rP   )rV   c                    s6   g | ].\}}|r|vr  |s|d  vr|qS )z.tests)search)rX   r+   rn   ZHAS_TESTS_EXCEPTIONSlookupZresource_modulesr-   r.   rZ      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compiler_   rU   r`   r[   ra   rb   rc   itemsr(   )rg   Zmissing_testsr-   rq   r.   test_all_tests_are_importable   s     
rv   c                  C   sv   d} t jt| d tt W d    n1 s00    Y  t jt| d ttg W d    n1 sh0    Y  d S )Nz8Passing a class was deprecated.* isn't supported anymorerJ   )rL   Zraises	TypeErrorr   r   r%   r,   r-   r-   r.   test_class_support_removed   s
    &ry   c                  c   s   t  D ]} t|  }d|v r0| jdddV  qd|v rv| jddd}|jjdv rntj|tjj	dd	d
V  q|V  qd|v r| jddV  qd S )N	oob_scoreT)rz   Z	bootstrapearly_stoppingr0   )r{   n_iter_no_change>   ZMLPRegressorZMLPClassifierzMLP still validates in fitrx   )Zmarksr|   )r|   )
r   set
get_params
set_params	__class__r8   rL   parammarkxfail)rF   Z
est_paramsestr-   r-   r.   _estimators_that_predict_in_fit   s    

r   rF   )Zidsc              	   C   s   t | trtd dt| v r*td ttdf tjdd}t	| j
j|  W d    n1 sf0    Y  |D ]}dt|jvstJ qtW d    n1 s0    Y  d S )Nz$ColumnTransformer is not tested herer   zCEstimator does not support check_dataframe_column_names_consistencyrB   T)recordz was fitted without feature names)
isinstancer   rL   skipr   r   rD   warningscatch_warningsr   r   r8   strmessage)rF   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_outTrl   r0   )rf   r9   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   rX   r   r-   r-   r.   rZ   "  s   rZ   r   c                 C   sJ   t td, t| jj|  t| jj|  W d    n1 s<0    Y  d S rA   )r   rD   r#   r   r8   r$   )r   r-   r-   r.   'test_transformers_get_feature_names_out)  s    r   c                 C   s   g | ]}t |d r|qS )r   )rf   r   r-   r-   r.   rZ   7  s   c                 C   s   | j j}t||  d S r1   )r   r8   r   )rF   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   rL   r   r   r8   r   rF   r+   r-   r-   r.   test_check_param_validationD  s    

r   F)Zsparse_outputz
one-to-one)Zfeature_names_outc                 C   s`   | j j}t| ds$td| d ttd t| j j|  W d    n1 sR0    Y  d S )N
set_outputz(Skipping check_set_output_transform for z!: Does not support set_output APIrB   )r   r8   rf   rL   r   r   rD   r    r   r-   r-   r.   test_set_output_transformZ  s    

r   
check_funcc                 C   sh   | j j}t| ds,td|j d| d ttd || j j|  W d    n1 sZ0    Y  d S )Nr   z	Skipping z for z%: Does not support set_output API yetrB   )r   r8   rf   rL   r   r   rD   )rF   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r8| jdd nt| d|dkr\| jdd	d
 |dkrp| jdd |dkr| 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)Zkernel)r   r8   rf   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 sR0    Y  t|dkslJ t|d j	d	sJ t|d
 j	dsJ t|d j	dsJ 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)r6   Xr3   r-   r-   r.   fit  s    
zAtest_transition_public_api_deprecations.<locals>.OldEstimator.fitc                 S   s   |S r1   r-   )r6   r   r-   r-   r.   	transform  s    zGtest_transition_public_api_deprecations.<locals>.OldEstimator.transform)N)r8   r9   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   rL   rM   rD   r   lenr   r   r*   )r   r   r3   Zold_estimatorZwarning_listr-   r-   r.   'test_transition_public_api_deprecations  s    
(r   )r0   )[r;   r_   rb   rs   r   	functoolsr   inspectr   	itertoolsr   rL   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/   r4   r5   r   Zparametrizer@   listrI   rN   r   filterwarningsrj   rm   rv   ry   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   <	




	
&	




	
