U
    3dj+                     @   sz   d dl ZddlmZmZ ddlmZmZmZ ddlm	Z	 G dd dZ
G d	d
 d
ZG dd deeZG dd deZdS )    N   )BaseEstimatorClassifierMixin   )_check_sample_weight_num_samplescheck_array)check_is_fittedc                   @   s    e Zd ZdZdd Zdd ZdS )ArraySlicingWrapper-
    Parameters
    ----------
    array
    c                 C   s
   || _ d S Narrayselfr    r   :/tmp/pip-unpacked-wheel-zrfo1fqw/sklearn/utils/_mocking.py__init__   s    zArraySlicingWrapper.__init__c                 C   s   t | j| S r   MockDataFramer   )r   Zaslicer   r   r   __getitem__   s    zArraySlicingWrapper.__getitem__N)__name__
__module____qualname____doc__r   r   r   r   r   r   r
      s   r
   c                   @   sD   e Zd ZdZdd Zdd ZdddZd	d
 Zdd ZdddZ	dS )r   r   c                 C   s*   || _ || _|j| _|j| _t|| _d S r   )r   valuesshapendimr
   Zilocr   r   r   r   r      s
    zMockDataFrame.__init__c                 C   s
   t | jS r   )lenr   r   r   r   r   __len__'   s    zMockDataFrame.__len__Nc                 C   s   | j S r   r   )r   dtyper   r   r   	__array__*   s    zMockDataFrame.__array__c                 C   s   t | j|jkS r   r   r   otherr   r   r   __eq__0   s    zMockDataFrame.__eq__c                 C   s
   | |k S r   r   r#   r   r   r   __ne__3   s    zMockDataFrame.__ne__r   c                 C   s   t | jj||dS )N)axis)r   r   take)r   indicesr'   r   r   r   r(   6   s    zMockDataFrame.take)N)r   )
r   r   r   r   r   r    r"   r%   r&   r(   r   r   r   r   r      s   
r   c                	   @   sj   e Zd ZdZdddddddddddZdd	d
ZdddZdd Zdd Zdd Z	dddZ
dd ZdS )CheckingClassifiera  Dummy classifier to test pipelining and meta-estimators.

    Checks some property of `X` and `y`in fit / predict.
    This allows testing whether pipelines / cross-validation or metaestimators
    changed the input.

    Can also be used to check if `fit_params` are passed correctly, and
    to force a certain score to be returned.

    Parameters
    ----------
    check_y, check_X : callable, default=None
        The callable used to validate `X` and `y`. These callable should return
        a bool where `False` will trigger an `AssertionError`.

    check_y_params, check_X_params : dict, default=None
        The optional parameters to pass to `check_X` and `check_y`.

    methods_to_check : "all" or list of str, default="all"
        The methods in which the checks should be applied. By default,
        all checks will be done on all methods (`fit`, `predict`,
        `predict_proba`, `decision_function` and `score`).

    foo_param : int, default=0
        A `foo` param. When `foo > 1`, the output of :meth:`score` will be 1
        otherwise it is 0.

    expected_sample_weight : bool, default=False
        Whether to check if a valid `sample_weight` was passed to `fit`.

    expected_fit_params : list of str, default=None
        A list of the expected parameters given when calling `fit`.

    Attributes
    ----------
    classes_ : int
        The classes seen during `fit`.

    n_features_in_ : int
        The number of features seen during `fit`.

    Examples
    --------
    >>> from sklearn.utils._mocking import CheckingClassifier

    This helper allow to assert to specificities regarding `X` or `y`. In this
    case we expect `check_X` or `check_y` to return a boolean.

    >>> from sklearn.datasets import load_iris
    >>> X, y = load_iris(return_X_y=True)
    >>> clf = CheckingClassifier(check_X=lambda x: x.shape == (150, 4))
    >>> clf.fit(X, y)
    CheckingClassifier(...)

    We can also provide a check which might raise an error. In this case, we
    expect `check_X` to return `X` and `check_y` to return `y`.

    >>> from sklearn.utils import check_array
    >>> clf = CheckingClassifier(check_X=check_array)
    >>> clf.fit(X, y)
    CheckingClassifier(...)
    Nallr   check_ycheck_y_paramscheck_Xcheck_X_paramsmethods_to_check	foo_paramexpected_sample_weightexpected_fit_paramsc          	      C   s4   || _ || _|| _|| _|| _|| _|| _|| _d S r   r,   )	r   r-   r.   r/   r0   r1   r2   r3   r4   r   r   r   r   z   s    zCheckingClassifier.__init__Tc                 C   s   |rt |  | jdk	rV| jdkr$i n| j}| j|f|}t|ttjfrR|sVtn|}|dk	r| jdk	r| j	dkrvi n| j	}| j|f|}t|ttjfr|stn|}||fS )a  Validate X and y and make extra check.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)
            The data set.
        y : array-like of shape (n_samples), default=None
            The corresponding target, by default None.
        should_be_fitted : bool, default=True
            Whether or not the classifier should be already fitted.
            By default True.

        Returns
        -------
        X, y
        N)
r	   r/   r0   
isinstanceboolnpZbool_AssertionErrorr-   r.   )r   Xyshould_be_fittedparamsZ	checked_XZ	checked_yr   r   r   
_check_X_y   s    


zCheckingClassifier._check_X_yc              	   K   s   t |t |kst| jdks(d| jkr<| j||dd\}}t|d | _tt|ddd| _	| j
rt| j
t| }|rtdt| d	| D ]<\}}t |t |krtd
| dt | dt | dq| jr|dkrtdt|| | S )a   Fit classifier.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)
            Training vector, where `n_samples` is the number of samples and
            `n_features` is the number of features.

        y : array-like of shape (n_samples, n_outputs) or (n_samples,),                 default=None
            Target relative to X for classification or regression;
            None for unsupervised learning.

        sample_weight : array-like of shape (n_samples,), default=None
            Sample weights. If None, then samples are equally weighted.

        **fit_params : dict of string -> object
            Parameters passed to the ``fit`` method of the estimator

        Returns
        -------
        self
        r+   fitF)r;   r   T)Z	ensure_2dZallow_ndzExpected fit parameter(s) z
 not seen.zFit parameter z has length z; expected .Nz#Expected sample_weight to be passed)r   r8   r1   r=   r7   r   Zn_features_in_uniquer   classes_r4   setlistitemsr3   r   )r   r9   r:   Zsample_weightZ
fit_paramsmissingkeyvaluer   r   r   r>      s*    
zCheckingClassifier.fitc                 C   s:   | j dksd| j kr"| |\}}| jtjt|td S )a>  Predict the first class seen in `classes_`.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)
            The input data.

        Returns
        -------
        preds : ndarray of shape (n_samples,)
            Predictions of the first class seens in `classes_`.
        r+   predict)r!   )r1   r=   rA   r7   zerosr   intr   r9   r:   r   r   r   rH      s    zCheckingClassifier.predictc                 C   sN   | j dksd| j kr"| |\}}tt|t| jf}d|dddf< |S )a  Predict probabilities for each class.

        Here, the dummy classifier will provide a probability of 1 for the
        first class of `classes_` and 0 otherwise.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)
            The input data.

        Returns
        -------
        proba : ndarray of shape (n_samples, n_classes)
            The probabilities for each sample and class.
        r+   predict_probar   Nr   )r1   r=   r7   rI   r   r   rA   )r   r9   r:   Zprobar   r   r   rL      s
    z CheckingClassifier.predict_probac                 C   sn   | j dksd| j kr"| |\}}t| jdkr>tt|S tt|t| jf}d|dddf< |S dS )aB  Confidence score.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)
            The input data.

        Returns
        -------
        decision : ndarray of shape (n_samples,) if n_classes == 2                else (n_samples, n_classes)
            Confidence score.
        r+   decision_functionr   r   Nr   )r1   r=   r   rA   r7   rI   r   )r   r9   r:   Zdecisionr   r   r   rM   	  s    z$CheckingClassifier.decision_functionc                 C   s8   | j dksd| j kr | || | jdkr0d}nd}|S )aQ  Fake score.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)
            Input data, where `n_samples` is the number of samples and
            `n_features` is the number of features.

        Y : array-like of shape (n_samples, n_output) or (n_samples,)
            Target relative to X for classification or regression;
            None for unsupervised learning.

        Returns
        -------
        score : float
            Either 0 or 1 depending of `foo_param` (i.e. `foo_param > 1 =>
            score=1` otherwise `score=0`).
        r+   scorer   g      ?g        )r1   r=   r2   )r   r9   YrN   r   r   r   rN   %  s    
zCheckingClassifier.scorec                 C   s   ddgdS )NTZ1dlabel)
_skip_testZX_typesr   r   r   r   r   
_more_tags@  s    zCheckingClassifier._more_tags)NT)N)NN)r   r   r   r   r   r=   r>   rH   rL   rM   rN   rQ   r   r   r   r   r*   :   s"   B
#
0
r*   c                   @   s:   e Zd ZdZdddZdd Zdd Zd	d
 Zdd ZdS )NoSampleWeightWrapperzWrap estimator which will not expose `sample_weight`.

    Parameters
    ----------
    est : estimator, default=None
        The estimator to wrap.
    Nc                 C   s
   || _ d S r   )est)r   rS   r   r   r   r   M  s    zNoSampleWeightWrapper.__init__c                 C   s   | j ||S r   )rS   r>   rK   r   r   r   r>   P  s    zNoSampleWeightWrapper.fitc                 C   s   | j |S r   )rS   rH   r   r9   r   r   r   rH   S  s    zNoSampleWeightWrapper.predictc                 C   s   | j |S r   )rS   rL   rT   r   r   r   rL   V  s    z#NoSampleWeightWrapper.predict_probac                 C   s   ddiS )NrP   Tr   r   r   r   r   rQ   Y  s    z NoSampleWeightWrapper._more_tags)N)	r   r   r   r   r   r>   rH   rL   rQ   r   r   r   r   rR   D  s   
rR   )Znumpyr7   baser   r   Z
validationr   r   r   r	   r
   r   r*   rR   r   r   r   r   <module>   s   $  