U
    ½mœdu  ã                   @   s  d dl mZ d dl mZ d dlm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Zd dlZd dlZd dlmZ d d	lmZ d
dlmZ G dd„ deeƒZdd„ Zdd„ Zdd„ Zdd„ ZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZ G dd„ deƒZ!G d d!„ d!eƒZ"G d"d#„ d#e"ƒZ#G d$d%„ d%eƒZ$G d&d'„ d'eƒZ%G d(d)„ d)eƒZ&G d*d+„ d+eƒZ'G d,d-„ d-eƒZ(G d.d/„ d/eƒZ)G d0d1„ d1eƒZ*G d2d3„ d3eƒZ+G d4d5„ d5eƒZ,G d6d7„ d7eƒZ-G d8d9„ d9eƒZ.G d:d;„ d;ƒZ/dBd<d=„Z0d>d?„ Z1d@dA„ Z2dS )Cé    )ÚABC)Úabstractmethod)ÚIterableN)Ú	signature)ÚIntegral)ÚReal©Úissparse)Ú
csr_matrixé   ©Ú_is_arraylike_not_scalarc                   @   s   e Zd ZdZdS )ÚInvalidParameterErrorzyCustom exception to be raised when the parameter of a class/method/function
    does not have a valid type or value.
    N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   úX/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/sklearn/utils/_param_validation.pyr      s   r   c                 C   sÊ   |  ¡ D ]¼\}}|| krq| | }|dkr,qdd„ |D ƒ}|D ]}| |¡r> qq>dd„ |D ƒ}t|ƒdkrx|d › }n*d dd„ |d	d
… D ƒ¡› d|d
 › �}td|›d|› d|› d|›d�	ƒ‚qd	S )a‚  Validate types and values of given parameters.

    Parameters
    ----------
    parameter_constraints : dict or {"no_validation"}
        If "no_validation", validation is skipped for this parameter.

        If a dict, it must be a dictionary `param_name: list of constraints`.
        A parameter is valid if it satisfies one of the constraints from the list.
        Constraints can be:
        - an Interval object, representing a continuous or discrete range of numbers
        - the string "array-like"
        - the string "sparse matrix"
        - the string "random_state"
        - callable
        - None, meaning that None is a valid value for the parameter
        - any type, meaning that any instance of this type is valid
        - an Options object, representing a set of elements of a given type
        - a StrOptions object, representing a set of strings
        - the string "boolean"
        - the string "verbose"
        - the string "cv_object"
        - the string "missing_values"
        - a HasMethods object, representing method(s) an object must have
        - a Hidden object, representing a constraint not meant to be exposed to the user

    params : dict
        A dictionary `param_name: param_value`. The parameters to validate against the
        constraints.

    caller_name : str
        The name of the estimator or function or method that called this function.
    Zno_validationc                 S   s   g | ]}t |ƒ‘qS r   )Úmake_constraint©Ú.0Ú
constraintr   r   r   Ú
<listcomp>J   s     z2validate_parameter_constraints.<locals>.<listcomp>c                 S   s   g | ]}|j s|‘qS r   ©Úhiddenr   r   r   r   r   U   s     r   r   ú, c                 S   s   g | ]}t |ƒ‘qS r   ©Ústr©r   Úcr   r   r   r   ]   s     Néÿÿÿÿú or zThe z parameter of z	 must be z. Got ú	 instead.)ÚitemsÚis_satisfied_byÚlenÚjoinr   )Úparameter_constraintsÚparamsÚcaller_nameÚ
param_nameZ	param_valÚconstraintsr   Zconstraints_strr   r   r   Úvalidate_parameter_constraints   s(    "
ÿ(ÿÿr-   c                 C   s  t | tƒr| dkrtƒ S t | tƒr0| dkr0tƒ S t | tƒrH| dkrHtƒ S | tkrVtƒ S | dkrdtƒ S t | tƒrvt	| ƒS t | t
tttfƒrŒ| S t | tƒr¤| dkr¤tƒ S t | tƒr¼| dkr¼tƒ S t | tƒrÔ| dkrÔtƒ S t | tƒrì| dkrìtƒ S t | tƒ�rt| jƒ} d	| _| S td
| › �ƒ‚dS )a  Convert the constraint into the appropriate Constraint object.

    Parameters
    ----------
    constraint : object
        The constraint to convert.

    Returns
    -------
    constraint : instance of _Constraint
        The converted constraint.
    z
array-likezsparse matrixZrandom_stateNÚbooleanÚverboseZmissing_valuesZ	cv_objectTúUnknown constraint type: )Ú
isinstancer   Ú_ArrayLikesÚ_SparseMatricesÚ_RandomStatesÚcallableÚ
_CallablesÚ_NoneConstraintÚtypeÚ_InstancesOfÚIntervalÚ
StrOptionsÚOptionsÚ
HasMethodsÚ	_BooleansÚ_VerboseHelperÚ_MissingValuesÚ
_CVObjectsÚHiddenr   r   r   Ú
ValueError©r   r   r   r   r   g   s6    

r   c                    s   ‡ fdd„}|S )a  Decorator to validate types and values of functions and methods.

    Parameters
    ----------
    parameter_constraints : dict
        A dictionary `param_name: list of constraints`. See the docstring of
        `validate_parameter_constraints` for a description of the accepted constraints.

        Note that the *args and **kwargs parameters are not validated and must not be
        present in the parameter_constraints dictionary.

    Returns
    -------
    decorated_function : function or method
        The decorated function.
    c                    s(   t ˆ dˆƒ t ˆ ¡‡ ‡fdd„ƒ}|S )NZ_skl_parameter_constraintsc               
      s¾   t ˆƒ}|j| |Ž}| ¡  dd„ |j ¡ D ƒ‰ ˆ ddg7 ‰ ‡ fdd„|j ¡ D ƒ}tˆ|ˆjd� zˆ| |ŽW S  t	k
r¸ } z*t
 dd	ˆj› d
�t|ƒ¡}t	|ƒ|‚W 5 d }~X Y nX d S )Nc                 S   s$   g | ]}|j |j|jfkr|j‘qS r   )ÚkindÚVAR_POSITIONALÚVAR_KEYWORDÚname)r   Úpr   r   r   r   ³   s   þzGvalidate_params.<locals>.decorator.<locals>.wrapper.<locals>.<listcomp>ÚselfÚclsc                    s   i | ]\}}|ˆ kr||“qS r   r   )r   ÚkÚv©Z	to_ignorer   r   Ú
<dictcomp>¹   s       zGvalidate_params.<locals>.decorator.<locals>.wrapper.<locals>.<dictcomp>)r*   zparameter of \w+ must bezparameter of z must be)r   ÚbindÚapply_defaultsÚ
parametersÚvaluesÚ	argumentsr$   r-   r   r   ÚreÚsubr   )ÚargsÚkwargsZfunc_sigr)   ÚeÚmsg)Úfuncr(   rN   r   Úwrapper©   s,    þ  ÿýz3validate_params.<locals>.decorator.<locals>.wrapper)ÚsetattrÚ	functoolsÚwraps)r[   r\   ©r(   )r[   r   Ú	decorator£   s    #z"validate_params.<locals>.decoratorr   )r(   ra   r   r`   r   Úvalidate_params‘   s    ,rb   c                 C   s>   | j }| j}|dkr|S | tkr$dS | tkr0dS |› d|› �S )z(Convert type into human readable string.ÚbuiltinsÚfloatÚintÚ.)r   r   r   r   )ÚtÚmoduleÚqualnamer   r   r   Ú
_type_nameÒ   s    rj   c                   @   s0   e Zd ZdZdd„ Zedd„ ƒZedd„ ƒZdS )	Ú_Constraintz&Base class for the constraint objects.c                 C   s
   d| _ d S ©NFr   ©rJ   r   r   r   Ú__init__â   s    z_Constraint.__init__c                 C   s   dS )a  Whether or not a value satisfies the constraint.

        Parameters
        ----------
        val : object
            The value to check.

        Returns
        -------
        is_satisfied : bool
            Whether or not the constraint is satisfied by this value.
        Nr   ©rJ   Úvalr   r   r   r%   å   s    z_Constraint.is_satisfied_byc                 C   s   dS )z;A human readable representational string of the constraint.Nr   rm   r   r   r   Ú__str__ô   s    z_Constraint.__str__N)r   r   r   r   rn   r   r%   rq   r   r   r   r   rk   ß   s   
rk   c                       s0   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Z‡  ZS )r9   z~Constraint representing instances of a given type.

    Parameters
    ----------
    type : type
        The valid type.
    c                    s   t ƒ  ¡  || _d S ©N)Úsuperrn   r8   )rJ   r8   ©Ú	__class__r   r   rn     s    
z_InstancesOf.__init__c                 C   s   t || jƒS rr   )r1   r8   ro   r   r   r   r%     s    z_InstancesOf.is_satisfied_byc                 C   s   dt | jƒ›�S )Nzan instance of )rj   r8   rm   r   r   r   rq   	  s    z_InstancesOf.__str__©r   r   r   r   rn   r%   rq   Ú__classcell__r   r   rt   r   r9   ù   s   r9   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )r7   z+Constraint representing the None singleton.c                 C   s   |d kS rr   r   ro   r   r   r   r%     s    z_NoneConstraint.is_satisfied_byc                 C   s   dS )NÚNoner   rm   r   r   r   rq     s    z_NoneConstraint.__str__N©r   r   r   r   r%   rq   r   r   r   r   r7     s   r7   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )Ú_NanConstraintz/Constraint representing the indicator `np.nan`.c                 C   s   t |tƒot |¡S rr   )r1   r   ÚmathÚisnanro   r   r   r   r%     s    z_NanConstraint.is_satisfied_byc                 C   s   dS )Nz	numpy.nanr   rm   r   r   r   rq     s    z_NanConstraint.__str__Nry   r   r   r   r   rz     s   rz   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )Ú_PandasNAConstraintz.Constraint representing the indicator `pd.NA`.c                 C   s@   z$dd l }t|t|jƒƒo"| |¡W S  tk
r:   Y dS X d S )Nr   F)Zpandasr1   r8   ZNAZisnaÚImportError)rJ   rp   Úpdr   r   r   r%   $  s
    z#_PandasNAConstraint.is_satisfied_byc                 C   s   dS )Nz	pandas.NAr   rm   r   r   r   rq   ,  s    z_PandasNAConstraint.__str__Nry   r   r   r   r   r}   !  s   r}   c                       s>   e Zd ZdZddœ‡ fdd„
Zdd„ Zdd	„ Zd
d„ Z‡  ZS )r<   aG  Constraint representing a finite set of instances of a given type.

    Parameters
    ----------
    type : type

    options : set
        The set of valid scalars.

    deprecated : set or None, default=None
        A subset of the `options` to mark as deprecated in the string
        representation of the constraint.
    N©Ú
deprecatedc                   s:   t ƒ  ¡  || _|| _|ptƒ | _| j| j r6tdƒ‚d S )Nz7The deprecated options must be a subset of the options.)rs   rn   r8   ÚoptionsÚsetr�   rC   )rJ   r8   r‚   r�   rt   r   r   rn   ?  s    
zOptions.__init__c                 C   s   t || jƒo|| jkS rr   )r1   r8   r‚   ro   r   r   r   r%   H  s    zOptions.is_satisfied_byc                 C   s   |›}|| j kr|› d�}|S )z-Add a deprecated mark to an option if needed.z (deprecated)r€   )rJ   ÚoptionZ
option_strr   r   r   Ú_mark_if_deprecatedK  s    

zOptions._mark_if_deprecatedc                    s4   d  ‡ fdd„ˆ jD ƒ¡› }dtˆ jƒ› d|› d�S )Nr   c                    s   g | ]}ˆ   |¡‘qS r   )r…   )r   Úorm   r   r   r   T  s     z#Options.__str__.<locals>.<listcomp>za z among {Ú})r'   r‚   rj   r8   )rJ   Úoptions_strr   rm   r   rq   R  s    ÿzOptions.__str__)	r   r   r   r   rn   r%   r…   rq   rw   r   r   rt   r   r<   0  s
   	r<   c                       s&   e Zd ZdZddœ‡ fdd„
Z‡  ZS )r;   a2  Constraint representing a finite set of strings.

    Parameters
    ----------
    options : set of str
        The set of valid strings.

    deprecated : set of str or None, default=None
        A subset of the `options` to mark as deprecated in the string
        representation of the constraint.
    Nr€   c                   s   t ƒ jt||d� d S )N)r8   r‚   r�   )rs   rn   r   )rJ   r‚   r�   rt   r   r   rn   f  s    zStrOptions.__init__)r   r   r   r   rn   rw   r   r   rt   r   r;   Y  s   r;   c                       sH   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	‡  Z
S )r:   uU  Constraint representing a typed interval.

    Parameters
    ----------
    type : {numbers.Integral, numbers.Real, "real_not_int"}
        The set of numbers in which to set the interval.

        If "real_not_int", only reals that don't have the integer type
        are allowed. For example 1.0 is allowed but 1 is not.

    left : float or int or None
        The left bound of the interval. None means left bound is -âˆž.

    right : float, int or None
        The right bound of the interval. None means right bound is +âˆž.

    closed : {"left", "right", "both", "neither"}
        Whether the interval is open or closed. Possible choices are:

        - `"left"`: the interval is closed on the left and open on the right.
          It is equivalent to the interval `[ left, right )`.
        - `"right"`: the interval is closed on the right and open on the left.
          It is equivalent to the interval `( left, right ]`.
        - `"both"`: the interval is closed.
          It is equivalent to the interval `[ left, right ]`.
        - `"neither"`: the interval is open.
          It is equivalent to the interval `( left, right )`.

    Notes
    -----
    Setting a bound to `None` and setting the interval closed is valid. For instance,
    strictly speaking, `Interval(Real, 0, None, closed="both")` corresponds to
    `[0, +âˆž) U {+âˆž}`.
    c                   s.   t ƒ  ¡  || _|| _|| _|| _|  ¡  d S rr   )rs   rn   r8   ÚleftÚrightÚclosedÚ_check_params)rJ   r8   r‰   rŠ   r‹   rt   r   r   rn   Ž  s    
zInterval.__init__c                 C   sp  | j ttdfkr"td| j › d�ƒ‚| jdkr>td| j› d�ƒ‚| j tkrêd}| jd k	rpt| jtƒsptd|› �ƒ‚| jd k	r”t| jtƒs”td|› �ƒ‚| jd kr¾| jd	kr¾td
| j› d|› �ƒ‚| jd krè| jdkrètd| j› d|› �ƒ‚nD| jd k	�rt| jtƒ�stdƒ‚| jd k	�r.t| jtƒ�s.tdƒ‚| jd k	�rl| jd k	�rl| j| jk�rltd| j› d| j› �ƒ‚d S )NÚreal_not_intzJtype must be either numbers.Integral, numbers.Real or 'real_not_int'. Got r#   )r‰   rŠ   ÚbothÚneitherz@closed must be either 'left', 'right', 'both' or 'neither'. Got z"for an interval over the integers.zExpecting left to be an int zExpecting right to be an int ©r‰   rŽ   z"left can't be None when closed == ú ©rŠ   rŽ   z#right can't be None when closed == z#Expecting left to be a real number.z$Expecting right to be a real number.z(right can't be less than left. Got left=z and right=)	r8   r   r   rC   r‹   r‰   r1   Ú	TypeErrorrŠ   )rJ   Úsuffixr   r   r   rŒ   —  s<    ÿ
ÿ
ÿÿ&ÿzInterval._check_paramsc                 C   sˆ   t  |¡rdS | jdkrtjntj}| jdkr4tjntj}| jd krLt j	 n| j}| j
d krbt j	n| j
}|||ƒrvdS |||ƒr„dS dS )NFr�   r’   T)Únpr|   r‹   ÚoperatorÚltÚleÚgtÚger‰   ÚinfrŠ   )rJ   rp   Zleft_cmpZ	right_cmpr‰   rŠ   r   r   r   Ú__contains__¾  s    


zInterval.__contains__c                 C   s,   | j dkr t|tƒot|tƒ S t|| j ƒS )Nr�   )r8   r1   r   r   ro   r   r   r   Ú_has_valid_typeÎ  s    
zInterval._has_valid_typec                 C   s   |   |¡sdS || kS rl   )r�   ro   r   r   r   r%   Ó  s    
zInterval.is_satisfied_byc                 C   sz   | j tkrdnd}| jdkr dnd}| jd kr2dn| j}| jd krFdn| j}| jdkrZd	nd
}|› d|› |› d|› |› �S )Nzan intza floatr�   ú[ú(z-infr›   r’   ú]ú)z in the range r   )r8   r   r‹   r‰   rŠ   )rJ   Ztype_strZleft_bracketZ
left_boundZright_boundZright_bracketr   r   r   rq   Ù  s    ÿzInterval.__str__)r   r   r   r   rn   rŒ   rœ   r�   r%   rq   rw   r   r   rt   r   r:   j  s   #	'r:   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )r2   z#Constraint representing array-likesc                 C   s   t |ƒS rr   r   ro   r   r   r   r%   è  s    z_ArrayLikes.is_satisfied_byc                 C   s   dS )Nzan array-liker   rm   r   r   r   rq   ë  s    z_ArrayLikes.__str__Nry   r   r   r   r   r2   å  s   r2   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )r3   z(Constraint representing sparse matrices.c                 C   s   t |ƒS rr   r   ro   r   r   r   r%   ò  s    z_SparseMatrices.is_satisfied_byc                 C   s   dS )Nza sparse matrixr   rm   r   r   r   rq   õ  s    z_SparseMatrices.__str__Nry   r   r   r   r   r3   ï  s   r3   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )r6   z"Constraint representing callables.c                 C   s   t |ƒS rr   )r5   ro   r   r   r   r%   ü  s    z_Callables.is_satisfied_byc                 C   s   dS )Nz
a callabler   rm   r   r   r   rq   ÿ  s    z_Callables.__str__Nry   r   r   r   r   r6   ù  s   r6   c                       s0   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Z‡  ZS )r4   z™Constraint representing random states.

    Convenience class for
    [Interval(Integral, 0, 2**32 - 1, closed="both"), np.random.RandomState, None]
    c                    s0   t ƒ  ¡  ttdddd�ttjjƒtƒ g| _	d S )Nr   l   ÿÿ rŽ   ©r‹   )
rs   rn   r:   r   r9   r•   ÚrandomÚRandomStater7   Ú_constraintsrm   rt   r   r   rn   
  s
    

ýz_RandomStates.__init__c                    s   t ‡ fdd„| jD ƒƒS )Nc                 3   s   | ]}|  ˆ ¡V  qd S rr   ©r%   r   ©rp   r   r   Ú	<genexpr>  s     z0_RandomStates.is_satisfied_by.<locals>.<genexpr>©Úanyr¥   ro   r   r§   r   r%     s    z_RandomStates.is_satisfied_byc                 C   s.   d  dd„ | jd d… D ƒ¡› d| jd › �S )Nr   c                 S   s   g | ]}t |ƒ‘qS r   r   r   r   r   r   r     s     z)_RandomStates.__str__.<locals>.<listcomp>r!   r"   ©r'   r¥   rm   r   r   r   rq     s    ,ÿz_RandomStates.__str__rv   r   r   rt   r   r4     s   r4   c                       s0   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Z‡  ZS )r>   zrConstraint representing boolean likes.

    Convenience class for
    [bool, np.bool_, Integral (deprecated)]
    c                    s(   t ƒ  ¡  ttƒttjƒttƒg| _d S rr   )rs   rn   r9   Úboolr•   Úbool_r   r¥   rm   rt   r   r   rn   #  s
    
ýz_Booleans.__init__c                    s8   t ˆ tƒr t ˆ tƒs t dt¡ t‡ fdd„| jD ƒƒS )NzrPassing an int for a boolean parameter is deprecated in version 1.2 and won't be supported anymore in version 1.4.c                 3   s   | ]}|  ˆ ¡V  qd S rr   r¦   r   r§   r   r   r¨   4  s     z,_Booleans.is_satisfied_by.<locals>.<genexpr>)r1   r   r¬   ÚwarningsÚwarnÚFutureWarningrª   r¥   ro   r   r§   r   r%   +  s    ýz_Booleans.is_satisfied_byc                 C   s.   d  dd„ | jd d… D ƒ¡› d| jd › �S )Nr   c                 S   s   g | ]}t |ƒ‘qS r   r   r   r   r   r   r   8  s     z%_Booleans.__str__.<locals>.<listcomp>r!   r"   r«   rm   r   r   r   rq   6  s    ,ÿz_Booleans.__str__rv   r   r   rt   r   r>     s   r>   c                       s0   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Z‡  ZS )r?   z�Helper constraint for the verbose parameter.

    Convenience class for
    [Interval(Integral, 0, None, closed="left"), bool, numpy.bool_]
    c                    s0   t ƒ  ¡  ttdd dd�ttƒttjƒg| _d S )Nr   r‰   r¢   )	rs   rn   r:   r   r9   r¬   r•   r­   r¥   rm   rt   r   r   rn   D  s
    
ýz_VerboseHelper.__init__c                    s   t ‡ fdd„| jD ƒƒS )Nc                 3   s   | ]}|  ˆ ¡V  qd S rr   r¦   r   r§   r   r   r¨   M  s     z1_VerboseHelper.is_satisfied_by.<locals>.<genexpr>r©   ro   r   r§   r   r%   L  s    z_VerboseHelper.is_satisfied_byc                 C   s.   d  dd„ | jd d… D ƒ¡› d| jd › �S )Nr   c                 S   s   g | ]}t |ƒ‘qS r   r   r   r   r   r   r   Q  s     z*_VerboseHelper.__str__.<locals>.<listcomp>r!   r"   r«   rm   r   r   r   rq   O  s    ,ÿz_VerboseHelper.__str__rv   r   r   rt   r   r?   =  s   r?   c                       s0   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Z‡  ZS )r@   zõHelper constraint for the `missing_values` parameters.

    Convenience for
    [
        Integral,
        Interval(Real, None, None, closed="both"),
        str,
        None,
        _NanConstraint(),
        _PandasNAConstraint(),
    ]
    c                    s:   t ƒ  ¡  ttƒttd d dd�ttƒtƒ tƒ t	ƒ g| _
d S )NrŽ   r¢   )rs   rn   r9   r   r:   r   r   r7   rz   r}   r¥   rm   rt   r   r   rn   d  s    
ùz_MissingValues.__init__c                    s   t ‡ fdd„| jD ƒƒS )Nc                 3   s   | ]}|  ˆ ¡V  qd S rr   r¦   r   r§   r   r   r¨   q  s     z1_MissingValues.is_satisfied_by.<locals>.<genexpr>r©   ro   r   r§   r   r%   p  s    z_MissingValues.is_satisfied_byc                 C   s.   d  dd„ | jd d… D ƒ¡› d| jd › �S )Nr   c                 S   s   g | ]}t |ƒ‘qS r   r   r   r   r   r   r   u  s     z*_MissingValues.__str__.<locals>.<listcomp>r!   r"   r«   rm   r   r   r   rq   s  s    ,ÿz_MissingValues.__str__rv   r   r   rt   r   r@   V  s   r@   c                       s@   e Zd ZdZedeegiƒ‡ fdd„ƒZdd„ Zdd„ Z	‡  Z
S )	r=   aI  Constraint representing objects that expose specific methods.

    It is useful for parameters following a protocol and where we don't want to impose
    an affiliation to a specific module or class.

    Parameters
    ----------
    methods : str or list of str
        The method(s) that the object is expected to expose.
    Úmethodsc                    s$   t ƒ  ¡  t|tƒr|g}|| _d S rr   )rs   rn   r1   r   r±   ©rJ   r±   rt   r   r   rn   †  s    

zHasMethods.__init__c                    s   t ‡ fdd„| jD ƒƒS )Nc                 3   s   | ]}t tˆ |d ƒƒV  qd S rr   )r5   Úgetattr)r   Úmethodr§   r   r   r¨   Ž  s     z-HasMethods.is_satisfied_by.<locals>.<genexpr>)Úallr±   ro   r   r§   r   r%   �  s    zHasMethods.is_satisfied_byc                 C   sT   t | jƒdkr| jd ›}n.d dd„ | jd d… D ƒ¡› d| jd ›�}d|› �S )	Nr   r   r   c                 S   s   g | ]}t |ƒ‘qS r   )Úrepr©r   Úmr   r   r   r   •  s     z&HasMethods.__str__.<locals>.<listcomp>r!   z and zan object implementing )r&   r±   r'   r²   r   r   r   rq   �  s
    ,ÿzHasMethods.__str__)r   r   r   r   rb   r   Úlistrn   r%   rq   rw   r   r   rt   r   r=   z  s
   r=   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )Ú_IterablesNotStringz7Constraint representing iterables that are not strings.c                 C   s   t |tƒot |tƒ S rr   )r1   r   r   ro   r   r   r   r%   ž  s    z#_IterablesNotString.is_satisfied_byc                 C   s   dS )Nzan iterabler   rm   r   r   r   rq   ¡  s    z_IterablesNotString.__str__Nry   r   r   r   r   rº   ›  s   rº   c                       s0   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Z‡  ZS )rA   zÞConstraint representing cv objects.

    Convenient class for
    [
        Interval(Integral, 2, None, closed="left"),
        HasMethods(["split", "get_n_splits"]),
        _IterablesNotString(),
        None,
    ]
    c                    s4   t ƒ  ¡  ttdd dd�tddgƒtƒ tƒ g| _d S )Né   r‰   r¢   ÚsplitZget_n_splits)rs   rn   r:   r   r=   rº   r7   r¥   rm   rt   r   r   rn   ±  s    

üz_CVObjects.__init__c                    s   t ‡ fdd„| jD ƒƒS )Nc                 3   s   | ]}|  ˆ ¡V  qd S rr   r¦   r   r§   r   r   r¨   »  s     z-_CVObjects.is_satisfied_by.<locals>.<genexpr>r©   ro   r   r§   r   r%   º  s    z_CVObjects.is_satisfied_byc                 C   s.   d  dd„ | jd d… D ƒ¡› d| jd › �S )Nr   c                 S   s   g | ]}t |ƒ‘qS r   r   r   r   r   r   r   ¿  s     z&_CVObjects.__str__.<locals>.<listcomp>r!   r"   r«   rm   r   r   r   rq   ½  s    ,ÿz_CVObjects.__str__rv   r   r   rt   r   rA   ¥  s   	rA   c                   @   s   e Zd ZdZdd„ ZdS )rB   zÄClass encapsulating a constraint not meant to be exposed to the user.

    Parameters
    ----------
    constraint : str or _Constraint instance
        The constraint to be used internally.
    c                 C   s
   || _ d S rr   rD   )rJ   r   r   r   r   rn   Í  s    zHidden.__init__N)r   r   r   r   rn   r   r   r   r   rB   Ä  s   rB   c                 C   s¢   t | tƒrdd | j¡› �S t | tƒr6t dddg¡S t | tƒrDdS t | tƒr\t	ddi ƒƒ S t | t
ƒrjd	S t | tƒrxd
S t | tƒs†t‚|dkr”| gn|}t| |ƒS )aS  Return a value that does not satisfy the constraint.

    Raises a NotImplementedError if there exists no invalid value for this constraint.

    This is only useful for testing purpose.

    Parameters
    ----------
    constraint : _Constraint instance
        The constraint to generate a value for.

    constraints : list of _Constraint instances or None, default=None
        The list of all constraints for this parameter. If None, the list only
        containing `constraint` is used.

    Returns
    -------
    val : object
        A value that does not satisfy the constraint.
    znot r"   r   r»   é   r!   ZHasNotMethodsr   za stringznot a cv objectN)r1   r;   r'   r‚   r@   r•   Úarrayr?   r=   r8   rº   rA   r:   ÚNotImplementedErrorÚ$_generate_invalid_param_val_interval)r   r,   r   r   r   Úgenerate_invalid_param_valÑ  s     






rÁ   c                 C   s  | j tkrr| jdkrD| jdkrD| jdkr.tjS | jdkr@tj S t‚| jdk	r^t | j¡d S t 	| j¡d S �n¤| jdkrŠ| jdkrŠt‚dd„ |D ƒ}|r¤|d nd}|dkrÎ| jdk	rÄ| jd S | jd S | j}|dk	rî| jdkrî|d }| j}|dk	�r| jdk�r|d }|j}|jdk	�rHt
t 	|j¡ƒ}|jdk�rH|d }|j}|jdk	�r~t
t |j¡ƒ}|jdk�r~|d }|dk	�r |dk	�r t||ƒd S |dk	�rÂ|dk	�rÂt||ƒd S |dk	�rò|dk	�rì|| d	k�rì|d S t‚n$|dk	�r|| d	k�r|d S t‚dS )
aÈ  Return a value that does not satisfy an interval constraint.

    Generating an invalid value for an integer interval depends on the other constraints
    since an int is a real, meaning that it can be valid for a real interval.
    Assumes that there can be at most 2 interval constraints: one integer interval
    and/or one real interval.

    This is only useful for testing purpose.

    Parameters
    ----------
    interval : Interval instance
        The interval to generate a value for.

    constraints : list of _Constraint instances
        The list of all constraints for this parameter.

    Returns
    -------
    val : object
        A value that does not satisfy the interval constraint.
    N)r‰   r�   )rŠ   r�   g      à?c                 S   s$   g | ]}t |tƒr|jtkr|‘qS r   )r1   r:   r8   r   )r   Úir   r   r   r   -  s    
 
 z8_generate_invalid_param_val_interval.<locals>.<listcomp>r   r   r»   )r8   r   r‰   rŠ   r‹   r•   r›   r¿   ÚfloorÚceilre   ÚminÚmax)Úintervalr,   Zreal_intervalsZreal_intervalZint_leftZ	int_rightZ	real_leftZ
real_rightr   r   r   rÀ      s^    



ÿ



rÀ   c                 C   s   t | tƒrt dddg¡S t | tƒr8tddgddggƒS t | tƒrNtj d¡S t | t	ƒr`dd„ S t | t
ƒrndS t | tƒr€|  ¡ S t | tƒrŽd	S t | tƒrœdS t | tƒr¬tjS t | tƒrÐtd
ddd„ | jD ƒƒƒ S t | tƒrädddgS t | tƒròdS t | tƒ�r| jD ]
}|  S t | tƒ�rŽ| }|jdk�r<|jdk�r<dS |jdk�rR|jd S |jdk�rh|jd S |jtk�r„|j|j d S |jd S td| › �ƒ‚dS )a1  Return a value that does satisfy a constraint.

    This is only useful for testing purpose.

    Parameters
    ----------
    constraint : Constraint instance
        The constraint to generate a value for.

    Returns
    -------
    val : object
        A value that does satisfy the constraint.
    r   r»   r½   r   é*   c                 S   s   | S rr   r   )Úxr   r   r   Ú<lambda>�  ó    z&generate_valid_param.<locals>.<lambda>NTZValidHasMethodsr   c                 S   s   i | ]}|d d„ “qS )c                 S   s   d S rr   r   rm   r   r   r   rÊ   ”  rË   z1generate_valid_param.<locals>.<dictcomp>.<lambda>r   r·   r   r   r   rO   ”  s      z(generate_valid_param.<locals>.<dictcomp>é   r0   )r1   r2   r•   r¾   r3   r
   r4   r£   r¤   r6   r7   r9   r8   r>   r?   r@   Únanr=   r±   rº   rA   r<   r‚   r:   r‰   rŠ   r   rC   )r   r„   rÇ   r   r   r   Úgenerate_valid_paramh  sV    









  ÿ






rÎ   )N)3Úabcr   r   Úcollections.abcr   r^   r{   Úinspectr   Únumbersr   r   r–   rU   r®   Únumpyr•   Zscipy.sparser	   r
   Z
validationr   rC   r“   r   r-   r   rb   rj   rk   r9   r7   rz   r}   r<   r;   r:   r2   r3   r6   r4   r>   r?   r@   r=   rº   rA   rB   rÁ   rÀ   rÎ   r   r   r   r   Ú<module>   sR   K*A

){


!$!

/h