o
    &ѹg6                     @   s  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Zd dl	Z	d dl
Z
d dl
mZ d dlmZ d dlmZmZ d dlmZm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mZm Z m!Z! d dl"m#Z# d dl$m%Z%m&Z& e' # e(de) ej*+e
j,gZ-e.dd ede-dD Z/W d   n1 sw   Y  g dZ0g dZ1dd Z2dd Z3dd Z4dd Z5e	j67de	j68de d d! Z9d"d# Z:e!d$d% Z;e!d&d' Z<dS )(    N)	signature)walk_packages)metrics)make_classification)StackingClassifierStackingRegressor)enable_halving_search_cvenable_iterative_imputerLogisticRegression)FunctionTransformer)all_estimators)_construct_instances)_get_func_nameassert_docstring_consistencycheck_docstring_parametersignore_warningsskip_if_no_numpydoc)_is_deprecated)_enforce_estimator_tags_X_enforce_estimator_tags_yignorec                 C   s,   g | ]}d |d v sd|d v s|d qS )z._   z.tests. ).0Zpckgr   r   k/Users/admin/.pyenv/versions/3.10.0/lib/python3.10/site-packages/sklearn/tests/test_docstring_parameters.py
<listcomp>0   s    

r   zsklearn.)prefixpath)z%sklearn.utils.deprecation.load_mlcompzsklearn.pipeline.make_pipelinezsklearn.pipeline.make_unionz%sklearn.utils.extmath.safe_sparse_dotzsklearn.utils._joblibZHalfBinomialLoss)fitZscoreZfit_predictZfit_transformZpartial_fitZpredictc               
      sP  t jddd ddlm}  g }tD ]  drq dkrqtjdd	 t	 }W d    n1 s5w   Y  t
|t
j}d
d |D }|D ]\}}g }|tv sY|drZqJt
|r`qJtjdd	}| |}W d    n1 svw   Y  t|rtd| |d f t|jrqJ|t|j|7 }|jD ]0}	t||	}
t|
rqd }|	tv rt|
}d|jv r|jd jd u rdg}t|
|d}||7 }q||7 }qJt
|t
j} fdd|D }|D ]/\}}|drq|dkr drqt|tfddtD st|s|t|7 }qqd |}t|dkr&t!d| d S )Nnumpydocz+numpydoc is required to test the docstrings)reasonr   	docscrapez	.conftestzsklearn.utils.fixesT)recordc                 S   s    g | ]}|d  j dr|qS )r   sklearn)
__module__
startswith)r   clsr   r   r   r   d   s     z-test_docstring_parameters.<locals>.<listcomp>_z"Error for __init__ of %s in %s:
%sy)r   c                    s   g | ]}|d  j  kr|qS )r   )r&   )r   fnnamer   r   r      s    configurationsetupc                 3   s    | ]}| v V  qd S )Nr   )r   d)name_r   r   	<genexpr>   s    z,test_docstring_parameters.<locals>.<genexpr>
zDocstring Error:
)"pytestimportorskipr    r#   PUBLIC_MODULESendswithwarningscatch_warnings	importlibimport_moduleinspect
getmembersisclass_DOCSTRING_IGNORESr'   
isabstractClassDoclenRuntimeErrorr   __new__r   __init__methodsgetattr_METHODS_IGNORE_NONE_Yr   
parametersdefault
isfunctionr   anyjoinAssertionError)r#   Z	incorrectmoduleclassescnamer(   Zthis_incorrectwZcdocmethod_namemethodZparam_ignoresigresultZ	functionsfnamefuncmsgr   )r-   r1   r   test_docstring_parametersM   sz   









rZ   c                 C   s   | t  dddgiS )NCg?r   r
   )ZSearchCVr   r   r   _construct_searchcv_instance   s   r\   c                 C   s\   | j dkr| ddddgfgdS | j dkr| dt fgd	S | j d
kr,| dt fgdS d S )NColumnTransformerZtransformerpassthroughr   r   )ZtransformersPipelineZclf)ZstepsFeatureUnion)Ztransformer_list)__name__r   r   )	Estimatorr   r   r   $_construct_compose_pipeline_instance   s   


rc   c                 C   s8   t jg dg dg dg dg dgt jd}| |dS )N)r   r   r   )rd      )r   r   r   )r   r   r   )r   re   r   )Zdtype)
dictionary)nparrayZfloat64)rb   rf   r   r   r   _construct_sparse_coder   s
    
ri   z-ignore::sklearn.exceptions.ConvergenceWarningzname, Estimatorc              	   C   s  t d ddlm} ||}|d }|jdv rt|}n2|jdv r(t|}n(|jdkr2t|}n|jdkrJt	d	d
dd\}}|t
 ||}ntt|}|jdkr\|jdd n5|jdkrh|jdd n)|jdkss|jdrz|jdd n|jdv r|jdd n|jdkr|jdd d| v r|jdd |jdkr|jdd d| v r|jdd i }|jdr|jdv rg d }n|jd!krddd"d#dd$g}d }nt	d	d#dddd%\}}t||}t||}| jjr|| n+| jjr|tj||f  n| jjr||tjd&f | n||| |D ]:}	|	j|v r0q&d'|	j  }
d(|
v r?q&t!t"d) t#||	jsNJ W d    q&1 sZw   Y  q&t$|}d*d+ |D }t%|&|}t%|&|}|rt'd,|j d-| d S ).Nr    r   r"   Z
Attributes)ZHalvingRandomSearchCVZRandomizedSearchCVZHalvingGridSearchCVZGridSearchCV)r]   r_   r`   ZSparseCoderZFrozenEstimator      )	n_samples
n_featuresrandom_stateZSelectKBestre   )kZDummyClassifierZ
stratified)ZstrategyZCCAZPLSr   )Zn_components)ZGaussianRandomProjectionZSparseRandomProjectionZTSNE)Z
perplexitymax_iter)rp      rn   )rn   Z
Vectorizer)ZCountVectorizerZHashingVectorizerZTfidfVectorizer)zThis is the first document.z%This document is the second document.zAnd this is the third one.zIs this the first document?ZDictVectorizer)foobar   )rr   Zbaz)rl   rm   Zn_redundantZ	n_classesrn   . zonly categoryc                 S   s   g | ]}|j qS r   r,   )r   attrr   r   r   r   %  s    z1test_fit_docstring_attributes.<locals>.<listcomp>zUndocumented attributes for z: )(r4   r5   r    r#   rA   ra   r\   rc   ri   r   r   r   nextr   Z
set_paramsr'   
get_paramsr7   r   r   Z__sklearn_tags__Ztarget_tagsZone_d_labelsZtwo_d_labelsrg   Zc_Z
input_tagsZthree_d_arrayZnewaxisr-   rM   desclowerr   FutureWarninghasattr_get_all_fitted_attributesset
differencerN   )r-   rb   r#   doc
attributesZestXr*   Zskipped_attributesrx   r{   fit_attrZfit_attr_namesZundocumented_attrsr   r   r   test_fit_docstring_attributes   s   




















$r   c                 C   s   t | j }t ; tjdtd t| jD ]&}t	| j|}t
|ts&qzt	| | W n ttfy8   Y qw || qW d   n1 sIw   Y  dd |D S )zBGet all the fitted attributes of an estimator including propertieserrorrv   Nc                 S   s$   g | ]}| d r|d s|qS )r)   )r7   r'   )r   ro   r   r   r   r   D  s   $ z._get_all_fitted_attributes.<locals>.<listcomp>)list__dict__keysr8   r9   filterwarningsr}   dir	__class__rG   
isinstancepropertyAttributeErrorappend)Z	estimatorr   r-   objr   r   r   r   .  s    

r   c                  C   sL   t jt jt jt jt jg} t| dddgd d}t| dgd| d dS )	z>Check docstrings parameters of related metrics are consistent.TZaverageZzero_division)include_paramsZexclude_paramsa  This parameter is required for multiclass/multilabel targets\.
        If ``None``, the metrics for each class are returned\. Otherwise, this
        determines the type of averaging performed on the data:
        ``'binary'``:
            Only report results for the class specified by ``pos_label``\.
            This is applicable only if targets \(``y_\{true,pred\}``\) are binary\.
        ``'micro'``:
            Calculate metrics globally by counting the total true positives,
            false negatives and false positives\.
        ``'macro'``:
            Calculate metrics for each label, and find their unweighted
            mean\.  This does not take label imbalance into account\.
        ``'weighted'``:
            Calculate metrics for each label, and find their average weighted
            by support \(the number of true instances for each label\)\. This
            alters 'macro' to account for label imbalance; it can result in an
            F-score that is not between precision and recall\.[\s\w]*\.*
        ``'samples'``:
            Calculate metrics for each instance, and find their average \(only
            meaningful for multilabel classification where this differs from
            :func:`accuracy_score`\)\.ru   )r   Zdescr_regex_patternN)	r   Zprecision_recall_fscore_supportZf1_scoreZfbeta_scoreZprecision_scoreZrecall_scorer   rM   split)Zmetrics_to_checkZdescription_regexr   r   r   3test_precision_recall_f_score_docstring_consistencyG  s$   
r   c                   C   s   t ttgg dddgd dS )z?Check docstrings parameters stacking estimators are consistent.)cvZn_jobsr^   verboseTZfinal_estimator_)r   Zinclude_attrsexclude_attrsN)r   r   r   r   r   r   r   8test_stacking_classifier_regressor_docstring_consistencyx  s   
r   )=r:   r<   osr8   r   pkgutilr   Znumpyrg   r4   r%   r   Zsklearn.datasetsr   Zsklearn.ensembler   r   Zsklearn.experimentalr   r	   Zsklearn.linear_modelr   Zsklearn.preprocessingr   Zsklearn.utilsr   Z-sklearn.utils._test_common.instance_generatorr   Zsklearn.utils._testingr   r   r   r   r   Zsklearn.utils.deprecationr   Zsklearn.utils.estimator_checksr   r   r9   simplefilterr}   r   dirname__file__Zsklearn_pathr   r6   r?   rH   rZ   r\   rc   ri   markr   Zparametrizer   r   r   r   r   r   r   r   <module>   sV   



O

	y
0