U
    3dS                     @   s  d dl mZ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 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 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 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 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$ e#egegegeg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) Z+ej()d*eed d+d,d-eeddd.d-gd/d0 Z,ej()d1ed2d3d.d4e-d5fed+d6d7d4e-d8fedd d,d4e.d9fed dd:d4e.d;fed+d<d.d4e.d=fgd>d? Z/d@dA Z0dBdC Z1ej()dDe2dEfedEfedFfej3dGfgdHdI Z4dJdK Z5ej()dLeedd d,d-eed dd,d-eeddd7d-edMdNdOhe e edPe e g	dQdR Z6ej()dSeeddTd:d-eedUdVd.d-feeddTd:d-eedUdVd7d-feeddTd:d-eedWdVd.d-feeddTd:d-eedVdd,d-feeddTd:d-eedWdd7d-feedTdd,d-eedUdVd.d-feedTdd,d-eedUdVd7d-feedTdd,d-eed+d3d.d-feedTdd,d-eeddUd,d-feedTdd,d-eeddXd7d-feedUdVd.d-eedd+d:d-feedUdVd.d-eed+dd,d-feedUdVd.d-eedYdXd7d-feedUdVd.d-eedYdXd:d-feedUdVd7d-eedZd[d7d-feedUdVd7d-eedZd[d,d-feed3dd,d-eed d+d.d-feed+dd,d-eed d+d.d-fgd\d] Z7ej()d^e gee8ge ge ge ge ge geeddd.d-geed dd,d-eedd d7d-gg	d_d` Z9ej()dLe e ee8e e e e e e edMdNdOheed+d3dTheeddd7d-eed d[d7d-eed dd7d-eedd d7d-eed d+d7d-eed dd.d-eedd d:d-edPe e gdadb Z:ej()dceed d+d.d-ddfeed dd7d-defedMdNdOhdNfee;ej<ej=hej=fe>dfdg fdhdid+d3gdTdWggfdie?d+d3gdTdWggfdjed+d3gdTdWggfdkdlej@Ad fdme&e& fe2d+fednfdodpdqdrdsdteBdufdtejCfdvedPe'd dwfdxgdydz ZDej()d{eed d+d.d-efed|d}hefeeddd~hefdiefdjefdlefdefe>efe2efdefdefdtefedPefdefgdd ZEdd ZFdd ZGdd ZHdd ZIdd ZJdd ZKdd ZLdd ZMdd ZNdd ZOdd ZPdd ZQdd ZRdd ZSdd ZTdd ZUdS )    )IntegralRealN)
csr_matrix)BaseEstimator)LeaveOneOut
deprecated)Hidden)Interval)Options)
StrOptions)_ArrayLikes)	_Booleans)
_Callables)
_CVObjects)_InstancesOf)_MissingValues)_PandasNAConstraint)_IterablesNotString)_NoneConstraint)_RandomStates)_SparseMatrices)_VerboseHelper)
HasMethods)make_constraint)generate_invalid_param_val)generate_valid_param)validate_params)InvalidParameterError)abcd)r"   c                O   s   dS )z/A function to test the validation of functions.N )r   r    r!   r"   argskwargsr#   r#   M/tmp/pip-unpacked-wheel-zrfo1fqw/sklearn/utils/tests/test_param_validation.py_func#   s    r'   c                   @   sB   e Zd ZdZedegidd Ze edegidd ZdS )_ClasszJA class to test the _InstancesOf constraint and the validation of methods.r   c                 C   s   dS )zA validated methodNr#   selfr   r#   r#   r&   _method+   s    z_Class._methodc                 C   s   dS )zA deprecated validated methodNr#   r)   r#   r#   r&   _deprecated_method/   s    z_Class._deprecated_methodN)	__name__
__module____qualname____doc__r   r   r+   r   r,   r#   r#   r#   r&   r(   (   s   
r(   c                   @   s6   e Zd ZU dZdegiZeed< dd Z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__:   s    z_Estimator.__init__Nc                 C   s   |    d S r3   )Z_validate_paramsr*   Xyr#   r#   r&   fit=   s    z_Estimator.fit)NN)
r-   r.   r/   r0   r   r2   dict__annotations__r5   r9   r#   r#   r#   r&   r1   5   s   
r1   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)r<   intervalr#   r#   r&   test_interval_rangeA   s    rG   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   NrB   r@   r?   rD   )r
   r   npinfrE   rF   r#   r#   r&   test_interval_inf_in_boundsQ   s    rK   rF      r?   r@   rC   c                 C   s   t j| kstdS )z)Check that np.nan is not in any interval.N)rH   nanrE   rJ   r#   r#   r&   test_nan_not_in_intervala   s    rN   zparams, error, matchg      ?r>   )typer?   rB   rA   z=Expecting left to be an int for an interval over the integersg       @rD   z>Expecting right to be an int for an interval over the integersz&left can't be None when closed == leftrB   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errorrR   r#   r#   r&   test_interval_errorsj   s     rW   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_byrE   stroptionsr#   r#   r&   test_stroptions   s
    r\   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   rH   rI   rX   rE   rY   rZ   r#   r#   r&   test_options   s
    r_   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   rY   rE   )rO   Zexpected_type_name
constraintr#   r#   r&   %test_instances_of_type_human_readable   s    rd   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 r3   r#   r*   r#   r#   r&   r      s    z test_hasmethods.<locals>._Good.ac                 S   s   d S r3   r#   re   r#   r#   r&   r       s    z test_hasmethods.<locals>._Good.bN)r-   r.   r/   r   r    r#   r#   r#   r&   _Good   s   rf   c                   @   s   e Zd Zdd ZdS )ztest_hasmethods.<locals>._Badc                 S   s   d S r3   r#   re   r#   r#   r&   r      s    ztest_hasmethods.<locals>._Bad.aN)r-   r.   r/   r   r#   r#   r#   r&   _Bad   s   rg   z"an object implementing 'a' and 'b'N)r   rX   rE   rY   )rc   rf   rg   r#   r#   r&   test_hasmethods   s    rh   rc   r   r    r!   r9   c                 C   s   t | }| |rtdS )z>Check that the value generated does not satisfy the constraintNr   rX   rE   )rc   	bad_valuer#   r#   r&   test_generate_invalid_param_val   s    rk   zinteger_interval, real_interval         i   
   c                 C   s\   t ||| gd}||rt| |r,tt | || gd}||rJt| |rXtdS )ztCheck that the value generated for an interval constraint does not satisfy any of
    the interval constraints.
    constraintsNri   )Zinteger_intervalZreal_intervalrj   r#   r#   r&   +test_generate_invalid_param_val_2_intervals   s    Q  ru   rt   c              	   C   s*   t t t| d | d W 5 Q R X dS )zmCheck that the function raises NotImplementedError when there's no invalid value
    for the constraint.
    r   rs   N)rS   rT   NotImplementedErrorr   rs   r#   r#   r&   )test_generate_invalid_param_val_all_valid7  s    rw   c                 C   s   t | }| |stdS )z;Check that the value generated does satisfy the constraint.N)r   rX   rE   )rc   valuer#   r#   r&   test_generate_valid_paramP  s    ry   zconstraint_declaration, valuegzG?*   c                 C   s   | d S )NrL   r#   )xr#   r#   r&   <lambda>w      r|   )NNz
array-likezsparse matrix)random_stater   r~   )r~   Nr]   )booleanF)verboserL   )missing_valuesrP   )r   g      )r   Nr   rM   )r   missingr4   )	cv_objectrn   c                 C   s   t | }||stdS )z+Sanity check for the is_satisfied_by methodN)r   rX   rE   )constraint_declarationrx   rc   r#   r#   r&   test_is_satisfied_byp  s     r   z1constraint_declaration, expected_constraint_classZoption1Zoption2r^   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__rE   )r   Zexpected_constraint_classrc   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 constraintrQ   znot a valid constraintN)rS   rT   
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 berQ   wrongrL   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   r>   rl   ro   ern   ZfourN)rL   )r   )r   )rS   rT   r   r'   r#   r#   r#   r&   test_validate_params  s<         * r   c                  C   s$   t dtgidd } | dd dS )zTCheck that no error is raised when there are parameters without
    constraints
    r   c                 S   s   d S r3   r#   )r   r    r#   r#   r&   func  s    z1test_validate_params_missing_params.<locals>.funcrL   r>   N)r   r`   )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 deprecatedrQ   rL   r>   rl   r   r   r   N)r   r'   rS   warnsFutureWarningrT   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 berQ   r   z)Function _deprecated_method is deprecatedz6The 'a' parameter of _Class._deprecated_method must beN)rS   rT   r   r(   r+   r   r   r,   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 berQ   N)r1   rS   rT   r   r9   )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 subsetrQ   r   r    r!   r"   r   N)rS   rT   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g tjtdd	}| d
d W 5 Q R X t|j}d|kstt	d|kst	dS )zECheck that internal constraints are not exposed in the error message.paramc                 S   s   d S r3   r#   r   r#   r#   r&   f  s    z!test_hidden_constraint.<locals>.frL   r>   rl   )r   r    r!   The 'param' parameterrQ   badr   zan instance of 'dict'zan instance of 'list'N)
r   r	   listr:   rS   rT   r   rY   rx   rE   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 tjtdd}| dd	 W 5 Q R X t|j}d|ksrtd|ks~td
S )zBCheck that we can have 2 StrOptions constraints, one being hidden.r   autowarnc                 S   s   d S r3   r#   r   r#   r#   r&   r   *  s    z!test_hidden_stroptions.<locals>.fr   rQ   r   r   N)	r   r   r	   rS   rT   r   rY   rx   rE   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'   rE   r(   r+   r#   r#   r#   r&   4test_validate_params_set_param_constraints_attribute=  s    r   c               	   C   sR   t ddgi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   c                 S   s   d S r3   r#   r   r#   r#   r&   r   J  s    z1test_boolean_constraint_deprecated_int.<locals>.fTFz4Passing an int for a boolean parameter is deprecatedrQ   rL   N)r   rH   Zbool_rS   r   r   )r   r#   r#   r&   &test_boolean_constraint_deprecated_intE  s    
 r   c               	   C   sh   t tdg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param2c                 S   s   d S r3   r#   r   r#   r#   r&   r   \  s    ztest_no_validation.<locals>.fzThe 'param1' parameterrQ   r   )r   c                   @   s   e Zd ZdS )z$test_no_validation.<locals>.SomeTypeN)r-   r.   r/   r#   r#   r#   r&   SomeTypee  s   r   )r   )NN)r   r`   rS   rT   r   )r   r   r#   r#   r&   test_no_validationY  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pandasrL   r>   rl   N)rS   Zimportorskipr   rX   ZNArE   rH   array)pdZna_constraintr#   r#   r&   $test_pandas_na_constraint_with_pd_nal  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.rL   r>   rl   rr   zsome stringN)r   rX   rE   rangerc   r#   r#   r&   test_iterable_not_stringu  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.rn   rL   r>   rl   ro   Nznot a CV object)r   rX   rE   r   r   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   r4   )r    superr5   )r*   r    r   r#   r&   r5     s    z@test_third_party_estimator.<locals>.ThirdPartyEstimator.__init__Nc                    s   t  || d S r3   )r   r9   r6   r   r#   r&   r9     s    z;test_third_party_estimator.<locals>.ThirdPartyEstimator.fit)NN)r-   r.   r/   r5   r9   __classcell__r#   r#   r   r&   ThirdPartyEstimator  s   r   r   )r    N)r1   r9   )r   r#   r#   r&   test_third_party_estimator  s    
r   )r   )VZnumbersr   r   ZnumpyrH   Zscipy.sparser   rS   Zsklearn.baser   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(   r1   markZparametrizerG   rK   rN   	TypeErrorr   rW   r\   r_   r`   Zndarrayrd   rh   rk   ru   r   rw   ry   rO   Zfloat32Zfloat64callabler   randomZRandomStatera   rM   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r#   r#   r#   r&   <module>   s  




	

M







"	