U
    _»{j2&  ã                   @   sŒ   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	œd
d„Z
ddd„Zdd„ Zdd„ Zdd„ ZG dd„ dƒZddœdd„ZdS )é    ©Úwraps)Úissparseé   )Ú
get_configé   )Úcheck_pandas_support)Úavailable_ifN)Úindexc                C   st   t | ƒrtdƒ‚t|ƒr<z
|ƒ }W n tk
r:   d}Y nX tdƒ}t| |jƒrb|dk	r^|| _| S |j| ||dd�S )aA  Create a Pandas DataFrame.

    If `data_to_wrap` is a DataFrame, then the `columns` and `index` will be changed
    inplace. If `data_to_wrap` is a ndarray, then a new DataFrame is created with
    `columns` and `index`.

    Parameters
    ----------
    data_to_wrap : {ndarray, dataframe}
        Data to be wrapped as pandas dataframe.

    columns : callable, ndarray, or None
        The column names or a callable that returns the column names. The
        callable is useful if the column names require some computation.
        If `columns` is a callable that raises an error, `columns` will have
        the same semantics as `None`. If `None` and `data_to_wrap` is already a
        dataframe, then the column names are not changed. If `None` and
        `data_to_wrap` is **not** a dataframe, then columns are
        `range(n_features)`.

    index : array-like, default=None
        Index for data. `index` is ignored if `data_to_wrap` is already a DataFrame.

    Returns
    -------
    dataframe : DataFrame
        Container with column names or unchanged `output`.
    z+Pandas output does not support sparse data.Nz$Setting output container to 'pandas'F)r
   ÚcolumnsÚcopy)r   Ú
ValueErrorÚcallableÚ	Exceptionr   Ú
isinstanceÚ	DataFramer   )Údata_to_wrapr   r
   Úpd© r   úX/var/www/html/bwave/python/venv/lib/python3.8/site-packages/sklearn/utils/_set_output.pyÚ_wrap_in_pandas_container
   s    "

r   c                 C   sL   t |di ƒ}| |kr||  }ntƒ | › d� }|dkrDtd|› �ƒ‚d|iS )a  Get output config based on estimator and global configuration.

    Parameters
    ----------
    method : {"transform"}
        Estimator's method for which the output container is looked up.

    estimator : estimator instance or None
        Estimator to get the output configuration from. If `None`, check global
        configuration is used.

    Returns
    -------
    config : dict
        Dictionary with keys:

        - "dense": specifies the dense container for `method`. This can be
          `"default"` or `"pandas"`.
    Ú_sklearn_output_configÚ_output>   ÚdefaultÚpandasz0output config must be 'default' or 'pandas' got Údense)Úgetattrr   r   )ÚmethodÚ	estimatorZest_sklearn_output_configZdense_configr   r   r   Ú_get_output_config?   s    
ÿr   c                 C   sL   t | |ƒ}|d dkst|ƒs"|S dd„ }||ƒr8|jnd}t|||jd�S )aÖ  Wrap output with container based on an estimator's or global config.

    Parameters
    ----------
    method : {"transform"}
        Estimator's method to get container output for.

    data_to_wrap : {ndarray, dataframe}
        Data to wrap with container.

    original_input : {ndarray, dataframe}
        Original input of function.

    estimator : estimator instance
        Estimator with to get the output configuration from.

    Returns
    -------
    output : {ndarray, dataframe}
        If the output config is "default" or the estimator is not configured
        for wrapping return `data_to_wrap` unchanged.
        If the output config is "pandas", return `data_to_wrap` as a pandas
        DataFrame.
    r   r   c                 S   sR   ddl }t| dƒrNt| dƒrNz|jd }W n tk
r@   Y dS X t| |jƒS dS )zvReturn True if the X is a pandas dataframe.

        This is is backport from 1.4 in 1.3.1 for compatibility.
        r   Nr   Zilocr   F)ÚsysÚhasattrÚmodulesÚKeyErrorr   r   )ÚXr    r   r   r   r   Ú_is_pandas_df   s    z0_wrap_data_with_container.<locals>._is_pandas_dfN)r   r
   r   )r   Ú_auto_wrap_is_configuredr
   r   Úget_feature_names_out)r   r   Zoriginal_inputr   Zoutput_configr%   r
   r   r   r   Ú_wrap_data_with_containera   s    
ýr(   c                    s   t ˆ ƒ‡ ‡fdd„ƒ}|S )z@Wrapper used by `_SetOutputMixin` to automatically wrap methods.c                    sj   ˆ | |f|ž|Ž}t |tƒr\tˆ|d || ƒf|dd … ˜}tt|ƒdƒrXt|ƒ |¡S |S tˆ||| ƒS )Nr   r   Ú_make)r   Útupler(   r!   Útyper)   )Úselfr$   ÚargsÚkwargsr   Zreturn_tuple©Úfr   r   r   Úwrapped›   s    
ÿ
þz$_wrap_method_output.<locals>.wrappedr   )r0   r   r1   r   r/   r   Ú_wrap_method_output˜   s    r2   c                 C   s    t | dtƒ ƒ}t| dƒod|kS )zÆReturn True if estimator is configured for auto-wrapping the transform method.

    `_SetOutputMixin` sets `_sklearn_auto_wrap_output_keys` to `set()` if auto wrapping
    is manually disabled.
    Ú_sklearn_auto_wrap_output_keysr'   Ú	transform)r   Úsetr!   )r   Úauto_wrap_output_keysr   r   r   r&   ¯   s    
þr&   c                       s8   e Zd ZdZd‡ fdd„	Zeeƒddœdd„ƒZ‡  ZS )	Ú_SetOutputMixina\  Mixin that dynamically wraps methods to return container based on config.

    Currently `_SetOutputMixin` wraps `transform` and `fit_transform` and configures
    it based on `set_output` of the global configuration.

    `set_output` is only defined if `get_feature_names_out` is defined and
    `auto_wrap_output_keys` is the default value.
    ©r4   c                    s¬   t ƒ jf |Ž t|tƒs(|d ks(tdƒ‚|d kr<tƒ | _d S dddœ}tƒ | _| ¡ D ]P\}}t| |ƒrV||krrqV| j 	|¡ || j
krŠqVtt| |ƒ|ƒ}t| ||ƒ qVd S )Nz6auto_wrap_output_keys must be None or a tuple of keys.r4   )r4   Úfit_transform)ÚsuperÚ__init_subclass__r   r*   r   r5   r3   Úitemsr!   ÚaddÚ__dict__r2   r   Úsetattr)Úclsr6   r.   Zmethod_to_keyr   ÚkeyZwrapped_method©Ú	__class__r   r   r;   Æ   s*    ÿÿþ
z!_SetOutputMixin.__init_subclass__Nc                C   s*   |dkr| S t | dƒsi | _|| jd< | S )aD  Set output container.

        See :ref:`sphx_glr_auto_examples_miscellaneous_plot_set_output.py`
        for an example on how to use the API.

        Parameters
        ----------
        transform : {"default", "pandas"}, default=None
            Configure output of `transform` and `fit_transform`.

            - `"default"`: Default output format of a transformer
            - `"pandas"`: DataFrame output
            - `None`: Transform configuration is unchanged

        Returns
        -------
        self : estimator instance
            Estimator instance.
        Nr   r4   )r!   r   )r,   r4   r   r   r   Ú
set_outputæ   s    

z_SetOutputMixin.set_output)r8   )	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r;   r	   r&   rD   Ú__classcell__r   r   rB   r   r7   ¼   s   	 r7   r8   c                C   sJ   t | dƒpt | dƒo|dk	}|s$dS t | dƒs>td| › d�ƒ‚| j|d�S )a  Safely call estimator.set_output and error if it not available.

    This is used by meta-estimators to set the output for child estimators.

    Parameters
    ----------
    estimator : estimator instance
        Estimator instance.

    transform : {"default", "pandas"}, default=None
        Configure output of the following estimator's methods:

        - `"transform"`
        - `"fit_transform"`

        If `None`, this operation is a no-op.

    Returns
    -------
    estimator : estimator instance
        Estimator instance.
    r4   r9   NrD   zUnable to configure output for z' because `set_output` is not available.r8   )r!   r   rD   )r   r4   Zset_output_for_transformr   r   r   Ú_safe_set_output  s    

ý

ÿrJ   )N)Ú	functoolsr   Zscipy.sparser   Ú_configr   Ú r   Z_available_ifr	   r   r   r(   r2   r&   r7   rJ   r   r   r   r   Ú<module>   s   ü5
"7I