U
    -e \                     @   sP  d dl mZmZ d dlZd dl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mZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z) e)egegegegd	d
ddd dddZ*G dd dZ+G dd deZ,ej-.deegdd Z/dd Z0ej-.deed dddeeddddgdd Z1ej-.ded d!dd"e2d#fedd$d%d"e2d&fedd dd"e3d'fed dd(d"e3d)fedd*dd"e3d+fgd,d- Z4d.d/ Z5d0d1 Z6ej-.d2e7d3fed3fed4fej8d5fgd6d7 Z9d8d9 Z:ej-.d:eedd ddeed dddeeddd%ded;d<d=he ed
d>e% ed?e  e g
d@dA Z;ej-.dBeeddCd(deedDdEddfeeddCd(deedDdEd%dfeeddCd(deedFdEddfeeddCd(deedEdddfeeddCd(deedFdd%dfeedCdddeedDdEddfeedCdddeedDdEd%dfeedCdddeedd!ddfeedCdddeeddDddfeedCdddeeddGd%dfeedDdEddeeddd(dfeedDdEddeeddddfeedDdEddeedHdGd%dfeedDdEddeedHdGd(dfeedDdEd%deedIdJd%dfeedDdEd%deedIdJddfeed!dddeed dddfeeddddeed dddfgdKdL Z<ej-.d:e ee=e e! e# e$ e eeddd%dgdMdN Z>ej-.d:e e ee=e! e# e$ e e% e ed
d>ed;d<d=heedd!dCheeddd%deed dJd%deed dd%deedd d%deed dd%deed dddeedd d(ded?e  e gdOdP Z?ej-.dQeed ddddRfeed dd%ddSfed;d<d=hd<fee@ejAejBhejBfeCdTdU fdVdWdd!gdCdFggfdWeDdd!gdCdFggfdXedd!gdCdFggfdYdZejEFd fd[e+e+ fe7dfed\fd]d^e d*fe d_fe dfe eGd`fe ejHfe dafed?e,d dbfdcgddde ZIej-.dfeed dddefedgdhhefeedRdihefdWefdXe$fdZe#fde!feCefe7efdjefdke%fed
d>efed?efdlefgdmdn ZJdodp ZKdqdr ZLdsdt ZMdudv ZNdwdx ZOdydz ZPd{d| ZQd}d~ ZRdd ZSdd ZTdd ZUdd ZVdd ZWdd ZXdd ZYdd ZZdd Z[dd Z\dd Z]ej-.dd
dgdd Z^ej-.dddddgdd Z_dS )    )IntegralRealN)
csr_matrix)config_context
get_config)BaseEstimator_fit_context)LeaveOneOut
deprecated)
HasMethodsHiddenIntervalInvalidParameterErrorMissingValuesOptions
RealNotInt
StrOptions_ArrayLikes	_Booleans
_Callables
_CVObjects_InstancesOf_IterablesNotString_NoneConstraint_PandasNAConstraint_RandomStates_SparseMatrices_VerboseHelpergenerate_invalid_param_valgenerate_valid_parammake_constraintvalidate_params)abcdTprefer_skip_nested_validation)r&   c                O   s   dS )z/A function to test the validation of functions.N )r#   r$   r%   r&   argskwargsr)   r)   j/var/www/html/Darija-Ai-Train/env/lib/python3.8/site-packages/sklearn/utils/tests/test_param_validation.py_func'   s    r-   c                   @   sJ   e Zd ZdZedegidddd Ze edegidddd Zd	S )
_ClasszJA class to test the _InstancesOf constraint and the validation of methods.r#   Tr'   c                 C   s   dS )zA validated methodNr)   selfr#   r)   r)   r,   _method2   s    z_Class._methodc                 C   s   dS )zA deprecated validated methodNr)   r/   r)   r)   r,   _deprecated_method6   s    z_Class._deprecated_methodN)	__name__
__module____qualname____doc__r"   r   r1   r   r2   r)   r)   r)   r,   r.   /   s   
r.   c                   @   s@   e Zd ZU dZdegiZeed< dd Ze	dddd	d
Z
dS )
_Estimatorz<An estimator to test the validation of estimator parameters.r#   _parameter_constraintsc                 C   s
   || _ d S Nr#   r/   r)   r)   r,   __init__A   s    z_Estimator.__init__Tr'   Nc                 C   s   d S r9   r)   r0   Xyr)   r)   r,   fitD   s    z_Estimator.fit)NN)r3   r4   r5   r6   r   r8   dict__annotations__r;   r   r?   r)   r)   r)   r,   r7   <   s
   
r7   interval_typec                 C   s   t | dddd}d|kr d|ks$tt | dddd}d|krDd|ksHtt | dddd}d|krhd|ksltt | dddd}d|krd|kstdS )	z.Check the range of values depending on closed.   leftclosedrightbothneitherN)r   AssertionError)rB   intervalr)   r)   r,   test_interval_rangeI   s    rM   c                  C   sp   t tdddd} tj| kstt tdddd} tj | ks>tt tdddd} tj| ks\ttj | ksltdS )zjCheck that inf is included iff a bound is closed and set to None.

    Only valid for real intervals.
    r   NrH   rF   rE   rJ   )r   r   npinfrK   rL   r)   r)   r,   test_interval_inf_in_boundsY   s    rQ   rL      rE   rF   rI   c                 C   s   t j| kstdS )z)Check that np.nan is not in any interval.N)rN   nanrK   rP   r)   r)   r,   test_nan_not_in_intervali   s    rT   zparams, error, match      ?rD   )typerE   rH   rG   z=Expecting left to be an int for an interval over the integersg       @rJ   z>Expecting right to be an int for an interval over the integersz&left can't be None when closed == leftrH   z(right can't be None when closed == rightzright can't be less than leftc              	   C   s(   t j||d tf |  W 5 Q R X dS )zNCheck that informative errors are raised for invalid combination of parametersmatchN)pytestraisesr   )paramserrorrY   r)   r)   r,   test_interval_errorsr   s     r^   c                  C   sR   t dddhdhd} | ds"t| ds0t| dr>tdt| ksNtdS )z*Sanity check for the StrOptions constraintr#   r$   r%   r
   r&   z'c' (deprecated)N)r   is_satisfied_byrK   stroptionsr)   r)   r,   test_stroptions   s
    rc   c                  C   sX   t tddtjhdhd} | ds&t| tjs6t| drDtdt| ksTtdS )z'Sanity check for the Options constraintg            ?r
   Gz?z-0.5 (deprecated)N)r   r   rN   rO   r_   rK   r`   ra   r)   r)   r,   test_options   s
    rf   ztype, expected_type_nameintfloatznumpy.ndarrayc                 C   s$   t | }t|d| dks tdS )z?Check the string representation of the _InstancesOf constraint.zan instance of ''N)r   r`   rK   )rV   Zexpected_type_name
constraintr)   r)   r,   %test_instances_of_type_human_readable   s    rk   c                  C   s\   t ddg} G dd d}G dd d}| | s8t| | rHtt| dksXtdS )	z Check the HasMethods constraint.r#   r$   c                   @   s   e Zd Zdd Zdd ZdS )ztest_hasmethods.<locals>._Goodc                 S   s   d S r9   r)   r0   r)   r)   r,   r#      s    z test_hasmethods.<locals>._Good.ac                 S   s   d S r9   r)   rl   r)   r)   r,   r$      s    z test_hasmethods.<locals>._Good.bN)r3   r4   r5   r#   r$   r)   r)   r)   r,   _Good   s   rm   c                   @   s   e Zd Zdd ZdS )ztest_hasmethods.<locals>._Badc                 S   s   d S r9   r)   rl   r)   r)   r,   r#      s    ztest_hasmethods.<locals>._Bad.aN)r3   r4   r5   r#   r)   r)   r)   r,   _Bad   s   rn   z"an object implementing 'a' and 'b'N)r   r_   rK   r`   )rj   rm   rn   r)   r)   r,   test_hasmethods   s    ro   rj   r#   r$   r%   )Znumeric_onlyr?   c                 C   s   t | }| |rtdS )z>Check that the value generated does not satisfy the constraintNr   r_   rK   )rj   	bad_valuer)   r)   r,   test_generate_invalid_param_val   s    rr   zinteger_interval, real_interval         i   
   c                 C   sP   t |d}||rt| |r&tt | d}||r>t| |rLtdS )ztCheck that the value generated for an interval constraint does not satisfy any of
    the interval constraints.
    rj   Nrp   )Zinteger_intervalZreal_intervalrq   r)   r)   r,   +test_generate_invalid_param_val_2_intervals   s    Q

r{   c              	   C   s"   t t t|  W 5 Q R X dS )zmCheck that the function raises NotImplementedError when there's no invalid value
    for the constraint.
    N)rZ   r[   NotImplementedErrorr   rz   r)   r)   r,   )test_generate_invalid_param_val_all_valid<  s    r}   c                 C   s   t | }| |stdS )z;Check that the value generated does satisfy the constraint.N)r    r_   rK   )rj   valuer)   r)   r,   test_generate_valid_paramQ  s    r   zconstraint_declaration, valuegzG?*   c                 C   s   | d S )NrR   r)   )xr)   r)   r,   <lambda>y      r   )NNz
array-likezsparse matrix)random_stater   r   )r   Nrd   )booleanF)verboserR   g      rS   missingr:   )	cv_objectru   c                 C   s   t | }||stdS )z+Sanity check for the is_satisfied_by methodN)r!   r_   rK   )constraint_declarationr~   rj   r)   r)   r,   test_is_satisfied_byr  s     r   z1constraint_declaration, expected_constraint_classZoption1Zoption2re   r   r   r   c                 C   s   t | }|j|kstdS )zHCheck that make_constraint dispaches to the appropriate constraint classN)r!   	__class__rK   )r   Zexpected_constraint_classrj   r)   r)   r,   test_make_constraint  s    r   c                	   C   s&   t jtdd td W 5 Q R X dS )zNCheck that an informative error is raised when an unknown constraint is passedzUnknown constraintrX   znot a valid constraintN)rZ   r[   
ValueErrorr!   r)   r)   r)   r,   test_make_constraint_unknown  s    r   c                	   C   s  t jtdd tddd W 5 Q R X t jtdd tddgddi W 5 Q R X t jtdd tdddi W 5 Q R X t jtd	d tdddd
 W 5 Q R X t jtdd& tddddgddiddi W 5 Q R X t jtdd& tddddgddiddi W 5 Q R X dS )zGCheck that validate_params works no matter how the arguments are passedz"The 'a' parameter of _func must berX   wrongrR   r%   z"The 'b' parameter of _func must ber%   "The 'c' parameter of _func must bez"The 'd' parameter of _func must be)r%   r&   r   rD   rs   rv   eru   fourN)rR   )r   )r   )rZ   r[   r   r-   r)   r)   r)   r,   test_validate_params  s<         * r   c                  C   s(   t dtgidddd } | dd dS )	zTCheck that no error is raised when there are parameters without
    constraints
    r#   Tr'   c                 S   s   d S r9   r)   )r#   r$   r)   r)   r,   func  s    z1test_validate_params_missing_params.<locals>.funcrR   rD   N)r"   rg   )r   r)   r)   r,   #test_validate_params_missing_params  s    
r   c               
   C   sx   t  t} tjtdd | dddd W 5 Q R X tjtdd. tjtdd | dddd W 5 Q R X W 5 Q R X d	S )
z5Check that validate_params functions can be decoratedzFunction _func is deprecatedrX   rR   rD   rs   r   r   r   N)r   r-   rZ   warnsFutureWarningr[   r   )Zdecorated_functionr)   r)   r,    test_decorate_validated_function  s    
 r   c                
   C   sj   t jtdd t d W 5 Q R X t jtdd, t jtdd t d W 5 Q R X W 5 Q R X dS )z-Check that validate_params works with methodsz+The 'a' parameter of _Class._method must berX   r   z)Function _deprecated_method is deprecatedz6The 'a' parameter of _Class._deprecated_method must beN)rZ   r[   r   r.   r1   r   r   r2   r)   r)   r)   r,   test_validate_params_method  s     r   c               	   C   s.   t d} tjtdd |   W 5 Q R X dS )z9Check that validate_params works with Estimator instancesr   z'The 'a' parameter of _Estimator must berX   N)r7   rZ   r[   r   r?   )Zestr)   r)   r,   test_validate_params_estimator  s     r   c                	   C   s4   t jtdd tdddhddhd W 5 Q R X dS )	z@Check that the deprecated parameter must be a subset of options.z#deprecated options must be a subsetrX   r#   r$   r%   r&   r
   N)rZ   r[   r   r   r)   r)   r)   r,   !test_stroptions_deprecated_subset  s    r   c               	   C   s   t dtttgidddd } | dddd	 | dddg tjtd
d}| dd W 5 Q R X t|j}d|ksxt	d|kst	dS )zECheck that internal constraints are not exposed in the error message.paramTr'   c                 S   s   d S r9   r)   r   r)   r)   r,   f  s    z!test_hidden_constraint.<locals>.frR   rD   rs   )r#   r$   r%   The 'param' parameterrX   badr   zan instance of 'dict'zan instance of 'list'N)
r"   r   listr@   rZ   r[   r   r`   r~   rK   r   exc_infoerr_msgr)   r)   r,   test_hidden_constraint  s      
 
r   c               	   C   s   t dtdhttdhgidddd } | d | d tjtdd	}| d
d W 5 Q R X t|j}d|ksvtd|kstdS )zBCheck that we can have 2 StrOptions constraints, one being hidden.r   autowarnTr'   c                 S   s   d S r9   r)   r   r)   r)   r,   r   .  s    z!test_hidden_stroptions.<locals>.fr   rX   r   r   N)	r"   r   r   rZ   r[   r   r`   r~   rK   r   r)   r)   r,   test_hidden_stroptions+  s     
 
r   c                   C   s$   t tdstt t jds tdS )zCheck that the validate_params decorator properly sets the parameter constraints
    as attribute of the decorated function/method.
    Z_skl_parameter_constraintsN)hasattrr-   rK   r.   r1   r)   r)   r)   r,   4test_validate_params_set_param_constraints_attributeD  s    r   c               	   C   sV   t ddgidddd } | d | td tjtdd	 | d
 W 5 Q R X dS )zCheck that validate_params raise a deprecation message but still passes
    validation when using an int for a parameter accepting a boolean.
    r   r   Tr'   c                 S   s   d S r9   r)   r   r)   r)   r,   r   Q  s    z1test_boolean_constraint_deprecated_int.<locals>.fFz4Passing an int for a boolean parameter is deprecatedrX   rR   N)r"   rN   Zbool_rZ   r   r   r   r)   r)   r,   &test_boolean_constraint_deprecated_intL  s    
 r   c               	   C   sl   t tdgddddddd} tjtdd	 | d
d W 5 Q R X G dd d}| |d | | d dS )z5Check that validation can be skipped for a parameter.NZno_validationparam1param2Tr'   c                 S   s   d S r9   r)   r   r)   r)   r,   r   c  s    ztest_no_validation.<locals>.fzThe 'param1' parameterrX   r   )r   c                   @   s   e Zd ZdS )z$test_no_validation.<locals>.SomeTypeN)r3   r4   r5   r)   r)   r)   r,   SomeTypeo  s   r   )r   )NNr"   rg   rZ   r[   r   )r   r   r)   r)   r,   test_no_validation`  s    
r   c                  C   s>   t d} t }|| js t|tdddgr:tdS )z9Add a specific test for checking support for `pandas.NA`.ZpandasrR   rD   rs   N)rZ   Zimportorskipr   r_   ZNArK   rN   array)pdZna_constraintr)   r)   r,   $test_pandas_na_constraint_with_pd_nav  s    
r   c                  C   s>   t  } | dddgst| tds,t| dr:tdS )zGCheck that a string does not satisfy the _IterableNotString constraint.rR   rD   rs   ry   zsome stringN)r   r_   rK   rangerz   r)   r)   r,   test_iterable_not_string  s    r   c                  C   sn   t  } | dst| t s$t| ddgddgfddgddgfgsNt| ds\t| drjtdS )zUCheck that the _CVObjects constraint accepts all current ways
    to pass cv objects.ru   rR   rD   rs   rv   Nznot a CV object)r   r_   rK   r	   rz   r)   r)   r,   test_cv_objects  s    *r   c                  C   s"   G dd dt } | dd  dS )zCheck that the validation from a scikit-learn estimator inherited by a third
    party estimator does not impose a match between the dict of constraints and the
    parameters of the estimator.
    c                       s*   e Zd Z fddZd fdd	Z  ZS )z7test_third_party_estimator.<locals>.ThirdPartyEstimatorc                    s   || _ t jdd d S )Nr   r:   )r$   superr;   )r0   r$   r   r)   r,   r;     s    z@test_third_party_estimator.<locals>.ThirdPartyEstimator.__init__Nc                    s   t  || d S r9   )r   r?   r<   r   r)   r,   r?     s    z;test_third_party_estimator.<locals>.ThirdPartyEstimator.fit)NN)r3   r4   r5   r;   r?   __classcell__r)   r)   r   r,   ThirdPartyEstimator  s   r   r   r$   N)r7   r?   )r   r)   r)   r,   test_third_party_estimator  s    
r   c                  C   s0   t tdddd} | dst| dr,tdS )z9Check for the type RealNotInt in the Interval constraint.r   rR   rI   rF   rU   N)r   r   r_   rK   rz   r)   r)   r,   test_interval_real_not_int  s    r   c                   C   sH   t dtstt dtrtt tdts0tt tdtrDtdS )zCheck for the RealNotInt type.rU   rR   N)
isinstancer   rK   rN   float64Zint64r)   r)   r)   r,   test_real_not_int  s    r   c               	   C   sb   t dtgidddd } tjtdd | dd	 W 5 Q R X tdd
 | dd	 W 5 Q R X dS )z@Check that param validation can be skipped using config_context.r#   Tr'   c                 S   s   d S r9   r)   r:   r)   r)   r,   r     s    z%test_skip_param_validation.<locals>.fThe 'a' parameterrX   1r:   skip_parameter_validationN)r"   rg   rZ   r[   r   r   r   r)   r)   r,   test_skip_param_validation  s    
r   r(   Fc              	      s   t dtgidddd  t dtgi| d fdd}tjtd	d
 |dd W 5 Q R X | rl|dd n$tjtdd
 |dd W 5 Q R X dS )z,Check that nested validation can be skipped.r#   Tr'   c                 S   s   d S r9   r)   r:   r)   r)   r,   r     s    z&test_skip_nested_validation.<locals>.fr$   c                    s
    ddS )Ninvalid_param_valuer:   r)   r   r   r)   r,   g  s    z&test_skip_nested_validation.<locals>.gzThe 'b' parameterrX   r   r   rR   r   Nr   )r(   r   r)   r   r,   test_skip_nested_validation  s    
r   zJskip_parameter_validation, prefer_skip_nested_validation, expected_skipped)TTT)TFT)FTT)FFFc              	   C   sH   t dtgi|ddd }t| d |d}W 5 Q R X ||ksDtdS )z5Check interaction between global skip and local skip.r#   r'   c                 S   s
   t  d S )Nr   )r   r:   r)   r)   r,   r     s    z9test_skip_nested_validation_and_config_context.<locals>.gr   rR   N)r"   rg   r   rK   )r   r(   Zexpected_skippedr   Zactual_skippedr)   r)   r,   .test_skip_nested_validation_and_config_context  s     
r   )r   )`numbersr   r   numpyrN   rZ   Zscipy.sparser   Zsklearn._configr   r   Zsklearn.baser   r   Zsklearn.model_selectionr	   Zsklearn.utilsr   Zsklearn.utils._param_validationr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r-   r.   r7   markZparametrizerM   rQ   rT   	TypeErrorr   r^   rc   rf   rg   Zndarrayrk   ro   rr   r{   r   r}   r   rV   Zfloat32r   callabler   randomZRandomStaterh   rS   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r)   r)   r)   r,   <module>   s  d




	

M







"	
	