U
    OvfN\                     @   sb  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)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 Z0dd Z1ej./deed dddeeddddgdd Z2ej./ded d!dd"e3d#fedd$d%d"e3d&fedd dd"e4d'fed dd(d"e4d)fedd*dd"e4d+fgd,d- Z5d.d/ Z6d0d1 Z7ej./d2e8d3fed3fed4fej9d5fgd6d7 Z:d8d9 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eAejBejChejCfeDdTdU fdVdWdd!gdCdFggfdWeEdd!gdCdFggfdXedd!gdCdFggfdYdZejFGd fd[e,e, fe8dfed\fd]d^d_ejHfe d*fe d`fe dfe eId_fe ejHfe dafed?e-d dbfdcgddde ZJe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eDefe8efdjefdke&fed
d>efed?efdlefd_e!fgdmdn ZKdodp ZLdqdr ZMdsdt ZNdudv ZOdwdx ZPdydz ZQd{d| ZRd}d~ ZSdd ZTdd ZUdd ZVdd ZWdd ZXdd ZYdd ZZdd Z[dd 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_NanConstraint_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*   M/tmp/pip-unpacked-wheel-qu3nn_q2/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-   _method3   s    z_Class._methodc                 C   s   dS )zA deprecated validated methodNr*   r0   r*   r*   r-   _deprecated_method7   s    z_Class._deprecated_methodN)	__name__
__module____qualname____doc__r#   r   r2   r   r3   r*   r*   r*   r-   r/   0   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$   r0   r*   r*   r-   __init__B   s    z_Estimator.__init__Tr(   Nc                 C   s   d S r:   r*   r1   Xyr*   r*   r-   fitE   s    z_Estimator.fit)NN)r4   r5   r6   r7   r   r9   dict__annotations__r<   r   r@   r*   r*   r*   r-   r8   =   s
   
r8   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)rC   intervalr*   r*   r-   test_interval_rangeJ   s    rN   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   NrI   rG   rF   rK   )r   r   npinfrL   rM   r*   r*   r-   test_interval_inf_in_boundsZ   s    rR   rM      rF   rG   rJ   c                 C   s   t j| kstdS )z)Check that np.nan is not in any interval.N)rO   nanrL   rQ   r*   r*   r-   test_nan_not_in_intervalj   s    rU   zparams, error, match      ?rE   )typerF   rI   rH   z=Expecting left to be an int for an interval over the integersg       @rK   z>Expecting right to be an int for an interval over the integersz&left can't be None when closed == leftrI   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errorrZ   r*   r*   r-   test_interval_errorss   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_byrL   stroptionsr*   r*   r-   test_stroptions   s
    rd   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   rO   rP   r`   rL   ra   rb   r*   r*   r-   test_options   s
    rg   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   ra   rL   )rW   Zexpected_type_name
constraintr*   r*   r-   %test_instances_of_type_human_readable   s    rl   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 r:   r*   r1   r*   r*   r-   r$      s    z test_hasmethods.<locals>._Good.ac                 S   s   d S r:   r*   rm   r*   r*   r-   r%      s    z test_hasmethods.<locals>._Good.bN)r4   r5   r6   r$   r%   r*   r*   r*   r-   _Good   s   rn   c                   @   s   e Zd Zdd ZdS )ztest_hasmethods.<locals>._Badc                 S   s   d S r:   r*   rm   r*   r*   r-   r$      s    ztest_hasmethods.<locals>._Bad.aN)r4   r5   r6   r$   r*   r*   r*   r-   _Bad   s   ro   z"an object implementing 'a' and 'b'N)r   r`   rL   ra   )rk   rn   ro   r*   r*   r-   test_hasmethods   s    rp   rk   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`   rL   )rk   	bad_valuer*   r*   r-   test_generate_invalid_param_val   s    rs   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.
    rk   Nrq   )Zinteger_intervalZreal_intervalrr   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)r[   r\   NotImplementedErrorr    r{   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`   rL   )rk   valuer*   r*   r-   test_generate_valid_paramR  s    r   zconstraint_declaration, valuegzG?*   c                 C   s   | d S )NrS   r*   )xr*   r*   r-   <lambda>z      r   )NNz
array-likezsparse matrix)random_stater   r   )r   Nre   )booleanF)verboserS   rT   g      missingr;   )	cv_objectrv   c                 C   s   t | }||stdS )z+Sanity check for the is_satisfied_by methodN)r"   r`   rL   )constraint_declarationr   rk   r*   r*   r-   test_is_satisfied_bys  s    !r   z1constraint_declaration, expected_constraint_classZoption1Zoption2rf   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__rL   )r   Zexpected_constraint_classrk   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 constraintrY   znot a valid constraintN)r[   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 berY   wrongrS   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   rE   rt   rw   erv   ZfourN)rS   )r   )r   )r[   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 r:   r*   )r$   r%   r*   r*   r-   func  s    z1test_validate_params_missing_params.<locals>.funcrS   rE   N)r#   rh   )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 deprecatedrY   rS   rE   rt   r   r   r   N)r   r.   r[   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 berY   r   z)Function _deprecated_method is deprecatedz6The 'a' parameter of _Class._deprecated_method must beN)r[   r\   r   r/   r2   r   r   r3   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 berY   N)r8   r[   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 subsetrY   r$   r%   r&   r'   r
   N)r[   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 r:   r*   r   r*   r*   r-   f  s    z!test_hidden_constraint.<locals>.frS   rE   rt   )r$   r%   r&   The 'param' parameterrY   badr   zan instance of 'dict'zan instance of 'list'N)
r#   r   listrA   r[   r\   r   ra   r   rL   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 r:   r*   r   r*   r*   r-   r   1  s    z!test_hidden_stroptions.<locals>.fr   rY   r   r   N)	r#   r   r   r[   r\   r   ra   r   rL   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.   rL   r/   r2   r*   r*   r*   r-   4test_validate_params_set_param_constraints_attributeG  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 r:   r*   r   r*   r*   r-   r   T  s    z1test_boolean_constraint_deprecated_int.<locals>.fFz4Passing an int for a boolean parameter is deprecatedrY   rS   N)r#   rO   Zbool_r[   r   r   r   r*   r*   r-   &test_boolean_constraint_deprecated_intO  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 r:   r*   r   r*   r*   r-   r   f  s    ztest_no_validation.<locals>.fzThe 'param1' parameterrY   r   )r   c                   @   s   e Zd ZdS )z$test_no_validation.<locals>.SomeTypeN)r4   r5   r6   r*   r*   r*   r-   SomeTyper  s   r   )r   )NNr#   rh   r[   r\   r   )r   r   r*   r*   r-   test_no_validationc  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pandasrS   rE   rt   N)r[   Zimportorskipr   r`   ZNArL   rO   array)pdZna_constraintr*   r*   r-   $test_pandas_na_constraint_with_pd_nay  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.rS   rE   rt   rz   zsome stringN)r   r`   rL   ranger{   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.rv   rS   rE   rt   rw   Nznot a CV object)r   r`   rL   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   r;   )r%   superr<   )r1   r%   r   r*   r-   r<     s    z@test_third_party_estimator.<locals>.ThirdPartyEstimator.__init__Nc                    s   t  || d S r:   )r   r@   r=   r   r*   r-   r@     s    z;test_third_party_estimator.<locals>.ThirdPartyEstimator.fit)NN)r4   r5   r6   r<   r@   __classcell__r*   r*   r   r-   ThirdPartyEstimator  s   r   r   r%   N)r8   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   rS   rJ   rG   rV   N)r   r   r`   rL   r{   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.rV   rS   N)
isinstancer   rL   rO   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 r:   r*   r;   r*   r*   r-   r     s    z%test_skip_param_validation.<locals>.fThe 'a' parameterrY   1r;   skip_parameter_validationN)r#   rh   r[   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 r:   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' parameterrY   r   r   rS   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   rS   N)r#   rh   r   rL   )r   r)   Zexpected_skippedr   Zactual_skippedr*   r*   r-   .test_skip_nested_validation_and_config_context  s     
r   )r   )aZnumbersr   r   ZnumpyrO   r[   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.   r/   r8   markZparametrizerN   rR   rU   	TypeErrorr   r_   rd   rg   rh   Zndarrayrl   rp   rs   r|   r   r~   r   rW   Zfloat32r   callabler   randomZRandomStaterT   ri   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  h




	

M







"	
	