U
    %¼|e²"  ã                   @   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é   )Úcheck_pandas_supporté   )Ú
get_config)Úavailable_ifN)Úindexc                C   sr   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�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')r
   Úcolumns)r   Ú
ValueErrorÚcallableÚ	Exceptionr   Ú
isinstanceZ	DataFramer   )Údata_to_wrapr   r
   Úpd© r   úV/var/www/website-v5/atlas_env/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>   ÚpandasÚdefaultz0output 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   s:   t | |ƒ}|d dkst|ƒs"|S t|t|ddƒ|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   r
   N)r   r
   r   )r   Ú_auto_wrap_is_configuredr   r   Úget_feature_names_out)r   r   Zoriginal_inputr   Zoutput_configr   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 … ˜S tˆ||| ƒS )Nr   r   )r   Útupler    )ÚselfÚXÚargsÚkwargsr   ©Úfr   r   r   ÚwrappedŠ   s    
ÿ
þz$_wrap_method_output.<locals>.wrappedr   )r'   r   r(   r   r&   r   Ú_wrap_method_output‡   s    r)   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   ÚsetÚhasattr)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.
    ©r+   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.r+   )r+   Úfit_transform)ÚsuperÚ__init_subclass__r   r!   r   r,   r*   Úitemsr-   ÚaddÚ__dict__r)   r   Úsetattr)Úclsr.   r%   Zmethod_to_keyr   ÚkeyÚwrapped_method©Ú	__class__r   r   r3   °   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   r+   )r-   r   )r"   r+   r   r   r   Ú
set_outputÐ   s    

z_SetOutputMixin.set_output)r0   )	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r3   r	   r   r=   Ú__classcell__r   r   r;   r   r/   ¦   s   	 r/   r0   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.
    r+   r1   Nr=   zUnable to configure output for z' because `set_output` is not available.r0   )r-   r   r=   )r   r+   Zset_output_for_transformr   r   r   Ú_safe_set_outputï   s    

ý

ÿrC   )N)Ú	functoolsr   Úscipy.sparser   Ú r   Ú_configr   Z_available_ifr	   r   r   r    r)   r   r/   rC   r   r   r   r   Ú<module>   s   ü5
"&I