a
    �ù»j)<  ã                   @   s  d dl Z d dlmZ d dlmZmZ d dlZd dlm	Z	 ddl
mZ ddlmZ dd	lmZ d
d„ Zdd„ ZeG dd„ deƒƒZG dd„ dƒZG dd„ dƒZG dd„ dƒZeƒ Ze eƒ ¡ e eƒ ¡ dd„ Z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 ))é    N©Úwraps)ÚProtocolÚruntime_checkable)Úissparseé   )Ú
get_configé   )Úavailable_if)Ú2_create_pandas_dataframe_from_non_pandas_containerc              
   C   sN   zt  | ¡W S  tyH } z$td| › d| › d�ƒ|‚W Y d}~n
d}~0 0 dS )zCheck library is installed.zSetting output container to 'z' requires z to be installedN)Ú	importlibÚimport_moduleÚImportError)ZlibraryÚexc© r   ú?E:\soccer\ml_env\Lib\site-packages\sklearn/utils/_set_output.pyÚcheck_library_installed   s    ÿýr   c                 C   s*   t | ƒr&z| ƒ W S  ty$   Y d S 0 | S ©N)ÚcallableÚ	Exception©Úcolumnsr   r   r   Úget_columns   s    r   c                   @   s8   e Zd ZU eed< ddd„Zdd„ Zdd„ Zd	d
„ ZdS )ÚContainerAdapterProtocolÚcontainer_libFc                 C   s   dS )aò  Create container from `X_output` with additional metadata.

        Parameters
        ----------
        X_output : {ndarray, dataframe}
            Data to wrap.

        X_original : {ndarray, dataframe}
            Original input dataframe. This is used to extract the metadata that should
            be passed to `X_output`, e.g. pandas row index.

        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 `None`,
            then no columns are passed to the container's constructor.

        inplace : bool, default=False
            Whether or not we intend to modify `X_output` in-place. However, it does
            not guarantee that we return the same object if the in-place operation
            is not possible.

        Returns
        -------
        wrapped_output : container_type
            `X_output` wrapped into the container type.
        Nr   )ÚselfÚX_outputÚ
X_originalr   Úinplacer   r   r   Úcreate_container(   s    z)ContainerAdapterProtocol.create_containerc                 C   s   dS )a  Return True if X is a supported container.

        Parameters
        ----------
        Xs: container
            Containers to be checked.

        Returns
        -------
        is_supported_container : bool
            True if X is a supported container.
        Nr   )r   ÚXr   r   r   Úis_supported_containerD   s    z/ContainerAdapterProtocol.is_supported_containerc                 C   s   dS )aQ  Rename columns in `X`.

        Parameters
        ----------
        X : container
            Container which columns is updated.

        columns : ndarray of str
            Columns to update the `X`'s columns with.

        Returns
        -------
        updated_container : container
            Container with new names.
        Nr   ©r   r    r   r   r   r   Úrename_columnsR   s    z'ContainerAdapterProtocol.rename_columnsc                 C   s   dS )a  Stack containers horizontally (column-wise).

        Parameters
        ----------
        Xs : list of containers
            List of containers to stack.

        Returns
        -------
        stacked_Xs : container
            Stacked containers.
        Nr   )r   ÚXsr   r   r   Úhstackc   s    zContainerAdapterProtocol.hstackN)F)	Ú__name__Ú
__module__Ú__qualname__ÚstrÚ__annotations__r   r!   r#   r%   r   r   r   r   r   $   s
   

r   c                   @   s2   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ ZdS )ÚPandasAdapterÚpandasTc                 C   st   t dƒ}t|ƒ}|r t||jƒs\t||jƒr4|j}nt||jƒrH|j}nd }t||| d�}|d urp|  ||¡S |S )Nr,   )r    ÚindexÚcopy)r   r   Ú
isinstanceÚ	DataFramer-   r   r#   )r   r   r   r   r   Úpdr-   r   r   r   r   u   s    ÿzPandasAdapter.create_containerc                 C   s   t dƒ}t||jƒS )Nr,   ©r   r/   r0   )r   r    r1   r   r   r   r!   �   s    z$PandasAdapter.is_supported_containerc                 C   s
   ||_ |S r   r   r"   r   r   r   r#   “   s    zPandasAdapter.rename_columnsc                 C   s   t dƒ}|j|dd�S )Nr,   r	   )Zaxis©r   Úconcat)r   r$   r1   r   r   r   r%   ™   s    zPandasAdapter.hstackN)T©r&   r'   r(   r   r   r!   r#   r%   r   r   r   r   r+   r   s
   
r+   c                   @   s2   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ ZdS )ÚPolarsAdapterÚpolarsTc                 C   s`   t dƒ}t|ƒ}t|tjƒr$| ¡ n|}|r8t||jƒsH|j||dd�S |d ur\|  ||¡S |S )Nr7   Úrow)ZschemaZorient)r   r   r/   ÚnpZndarrayÚtolistr0   r#   )r   r   r   r   r   Úplr   r   r   r   ¡   s    zPolarsAdapter.create_containerc                 C   s   t dƒ}t||jƒS )Nr7   r2   )r   r    r;   r   r   r   r!   ®   s    z$PolarsAdapter.is_supported_containerc                 C   s
   ||_ |S r   r   r"   r   r   r   r#   ²   s    zPolarsAdapter.rename_columnsc                 C   s   t dƒ}|j|dd�S )Nr7   Ú
horizontal)Úhowr3   )r   r$   r;   r   r   r   r%   ¸   s    zPolarsAdapter.hstackN)Tr5   r   r   r   r   r6   ž   s
   
r6   c                   @   s(   e Zd Zdd„ Zedd„ ƒZdd„ ZdS )ÚContainerAdaptersManagerc                 C   s
   i | _ d S r   )Úadapters©r   r   r   r   Ú__init__¾   s    z!ContainerAdaptersManager.__init__c                 C   s   dht | jƒB S )NÚdefault)Úsetr?   r@   r   r   r   Úsupported_outputsÁ   s    z*ContainerAdaptersManager.supported_outputsc                 C   s   || j |j< d S r   )r?   r   )r   Úadapterr   r   r   ÚregisterÅ   s    z!ContainerAdaptersManager.registerN)r&   r'   r(   rA   ÚpropertyrD   rF   r   r   r   r   r>   ½   s   
r>   c              
   C   sn   | j j d¡d }ztj| W S  tyh } z2ttj ¡ ƒ}td|› d| ›d�ƒ|‚W Y d}~n
d}~0 0 dS )z˜Get the adapter that knows how to handle such container.

    See :class:`sklearn.utils._set_output.ContainerAdapterProtocol` for more
    details.
    Ú.r   zZThe container does not have a registered adapter in scikit-learn. Available adapters are: z" while the container provided is: N)	Ú	__class__r'   ÚsplitÚADAPTERS_MANAGERr?   ÚKeyErrorÚlistÚkeysÚ
ValueError)Ú	containerÚmodule_namer   Zavailable_adaptersr   r   r   Ú_get_adapter_from_containerÎ   s    ÿþÿürR   c                 C   s4   t | |ƒd }ztj| W S  ty.   Y dS 0 dS )zGet container adapter.ÚdenseN)Ú_get_output_configrK   r?   rL   )ÚmethodÚ	estimatorÚdense_configr   r   r   Ú_get_container_adapterà   s
    rX   c                 C   s\   t |di ƒ}| |v r||  }ntƒ | › d� }tj}||vrTtdt|ƒ› 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Ú_outputzoutput config must be in z, got rS   )Úgetattrr   rK   rD   rO   Úsorted)rU   rV   Zest_sklearn_output_configrW   rD   r   r   r   rT   é   s    
ÿrT   c                 C   sb   t | |ƒ}|d dkst|ƒs"|S |d }t|ƒrFtd| ¡ › d�ƒ‚tj| }|j|||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.
    rS   rB   zmThe transformer outputs a scipy sparse matrix. Try to set the transformer output to a dense array or disable z- output with set_output(transform='default').r   )	rT   Ú_auto_wrap_is_configuredr   rO   Ú
capitalizerK   r?   r   Úget_feature_names_out)rU   Údata_to_wrapZoriginal_inputrV   Zoutput_configrW   rE   r   r   r   Ú_wrap_data_with_container  s     
þÿ
ýra   c                    s   t ˆ ƒ‡ ‡fdd„ƒ}|S )z@Wrapper used by `_SetOutputMixin` to automatically wrap methods.c                    sr   ˆ | |g|¢R i |¤Ž}t |tƒrdtˆ|d || ƒg|dd … ¢R }tt|ƒdƒr`t|ƒ |¡S |S tˆ||| ƒS )Nr   r	   Ú_make)r/   Útuplera   ÚhasattrÚtyperb   )r   r    ÚargsÚkwargsr`   Zreturn_tuple©ÚfrU   r   r   Úwrapped=  s    
ÿ
þz$_wrap_method_output.<locals>.wrappedr   )ri   rU   rj   r   rh   r   Ú_wrap_method_output:  s    rk   c                 C   s    t | dtƒ ƒ}t| dƒod|v 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[   rC   rd   )rV   Úauto_wrap_output_keysr   r   r   r]   Q  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.
    ©rm   c                    s°   t ƒ jf i |¤Ž t|tƒs,|d u s,tdƒ‚|d u r@tƒ | _d S dddœ}tƒ | _| ¡ D ]P\}}t| |ƒrZ||vrvqZ| j 	|¡ || j
vrŽqZtt| |ƒ|ƒ}t| ||ƒ qZd S )Nz6auto_wrap_output_keys must be None or a tuple of keys.rm   )rm   Úfit_transform)ÚsuperÚ__init_subclass__r/   rc   rO   rC   rl   Úitemsrd   ÚaddÚ__dict__rk   r[   Úsetattr)Úclsrn   rg   Zmethod_to_keyrU   ÚkeyZwrapped_method©rI   r   r   rs   h  s*    ÿÿþ
z!_SetOutputMixin.__init_subclass__Nc                C   s*   |du r| S t | dƒsi | _|| jd< | S )aÆ  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", "polars"}, default=None
            Configure output of `transform` and `fit_transform`.

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

            .. versionadded:: 1.4
                `"polars"` option was added.

        Returns
        -------
        self : estimator instance
            Estimator instance.
        NrY   rm   )rd   rY   )r   rm   r   r   r   Ú
set_outputˆ  s    

z_SetOutputMixin.set_output)rp   )	r&   r'   r(   Ú__doc__rs   r
   r]   r{   Ú__classcell__r   r   rz   r   ro   ^  s   	 ro   rp   c                C   sJ   t | dƒpt | dƒo|du}|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", "polars"}, 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.
    rm   rq   Nr{   zUnable to configure output for z' because `set_output` is not available.rp   )rd   rO   r{   )rV   rm   Zset_output_for_transformr   r   r   Ú_safe_set_output«  s    

ý

ÿr~   )N)N) r   Ú	functoolsr   Útypingr   r   Únumpyr9   Zscipy.sparser   Ú_configr   Z_available_ifr
   Úfixesr   r   r   r   r+   r6   r>   rK   rF   rR   rX   rT   ra   rk   r]   ro   r~   r   r   r   r   Ú<module>   s2   	M,
	
#.M