o
    ôj.  ã                   @   sÚ   d dl mZ d dlmZ d dlmZ d dlZddlm	Z	m
Z
mZmZmZ ddlmZ ddd	œd
d„Zd"dd„ZG dd„ deƒZdd„ ZG dd„ deƒZdd„ Zdd„ Zddœdd„Zd#dd„ZG dd„ deƒZd d!„ ZdS )$é    )ÚCounter)Úsuppress)Ú
NamedTupleNé   )Ú_isinÚ_searchsortedÚdeviceÚget_namespaceÚxpx©Úis_scalar_nanF©Úreturn_inverseÚreturn_countsc                C   s&   | j tkrt| ||d�S t| ||d�S )a�  Helper function to find unique values with support for python objects.

    Uses pure python method for object dtype, and numpy method for
    all other dtypes.

    Parameters
    ----------
    values : ndarray
        Values to check for unknowns.

    return_inverse : bool, default=False
        If True, also return the indices of the unique values.

    return_counts : bool, default=False
        If True, also return the number of times each unique item appears in
        values.

    Returns
    -------
    unique : ndarray
        The sorted unique values.

    unique_inverse : ndarray
        The indices to reconstruct the original array from the unique array.
        Only provided if `return_inverse` is True.

    unique_counts : ndarray
        The number of times each of the unique values comes up in the original
        array. Only provided if `return_counts` is True.
    r   )ÚdtypeÚobjectÚ_unique_pythonÚ
_unique_np)Úvaluesr   r   © r   úh/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/sklearn/utils/_encode.pyÚ_unique   s   
ÿÿr   c           
      C   s  t | ƒ\}}d\}}|r|r| | ¡\}}}}n|r"| | ¡\}}n|r,| | ¡\}}n| | ¡}|jrgt|d ƒrgt||j|d�}|d|d … }|rR||||k< |rg| 	||d… ¡||< |d|d … }|f}	|rq|	|f7 }	|rx|	|f7 }	t
|	ƒdkr‚|	d S |	S )z…Helper function to find unique values for numpy arrays that correctly
    accounts for nans. See `_unique` documentation for details.)NNéÿÿÿÿ©ÚxpNr   r   )r	   Ú
unique_allÚunique_inverseÚunique_countsÚunique_valuesÚsizer   r   ÚnanÚsumÚlen)
r   r   r   r   Ú_ÚinverseÚcountsÚuniquesÚnan_idxÚretr   r   r   r   =   s.   


r   c                   @   s*   e Zd ZU dZeed< eed< dd„ ZdS )ÚMissingValuesz'Data class for missing data informationr    Únonec                 C   s*   g }| j r
| d¡ | jr| tj¡ |S )z3Convert tuple to a list where None is always first.N)r*   Úappendr    Únp)ÚselfÚoutputr   r   r   Úto_listj   s   
zMissingValues.to_listN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚboolÚ__annotations__r/   r   r   r   r   r)   d   s
   
 r)   c                 C   sn   dd„ | D ƒ}|s| t ddd�fS d|v r)t|ƒdkr"t ddd�}nt ddd�}nt ddd�}| | }||fS )a.  Extract missing values from `values`.

    Parameters
    ----------
    values: set
        Set of values to extract missing from.

    Returns
    -------
    output: set
        Set with missing values extracted.

    missing_values: MissingValues
        Object with missing value information.
    c                 S   s    h | ]}|d u st |ƒr|’qS ©Nr   ©Ú.0Úvaluer   r   r   Ú	<setcomp>„   s    z#_extract_missing.<locals>.<setcomp>F)r    r*   Nr   T)r)   r"   )r   Zmissing_values_setZoutput_missing_valuesr.   r   r   r   Ú_extract_missingt   s   ÿr;   c                       s(   e Zd ZdZ‡ fdd„Zdd„ Z‡  ZS )Ú_nandictz!Dictionary with support for nans.c                    s6   t ƒ  |¡ | ¡ D ]\}}t|ƒr|| _ d S q
d S r6   )ÚsuperÚ__init__Úitemsr   Ú	nan_value)r-   ÚmappingÚkeyr9   ©Ú	__class__r   r   r>   �   s   þÿz_nandict.__init__c                 C   ó    t | dƒrt|ƒr| jS t|ƒ‚)Nr@   )Úhasattrr   r@   ÚKeyError©r-   rB   r   r   r   Ú__missing__¤   ó   z_nandict.__missing__)r0   r1   r2   r3   r>   rI   Ú__classcell__r   r   rC   r   r<   š   s    r<   c                    sD   t | |ƒ\}}tdd„ t|ƒD ƒƒ‰ |j‡ fdd„| D ƒt| ƒd�S )z,Map values based on its position in uniques.c                 S   s   i | ]\}}||“qS r   r   )r8   ÚiÚvalr   r   r   Ú
<dictcomp>­   s    z#_map_to_integer.<locals>.<dictcomp>c                    s   g | ]}ˆ | ‘qS r   r   ©r8   Úv©Útabler   r   Ú
<listcomp>®   ó    z#_map_to_integer.<locals>.<listcomp>)r   )r	   r<   Ú	enumerateÚasarrayr   )r   r&   r   r#   r   rQ   r   Ú_map_to_integerª   s    rW   c                C   sÂ   zt | ƒ}t|ƒ\}}t|ƒ}| | ¡ ¡ tj|| jd�}W n ty=   tdd„ t dd„ | D ƒƒD ƒƒ}td|› �ƒ‚w |f}|rK|t	| |ƒf7 }|rU|t
| |ƒf7 }t|ƒdkr_|d S |S )N©r   c                 s   s   � | ]}|j V  qd S r6   )r2   )r8   Útr   r   r   Ú	<genexpr>»   s   € z!_unique_python.<locals>.<genexpr>c                 s   s   � | ]}t |ƒV  qd S r6   )ÚtyperO   r   r   r   rZ   »   s   € zPEncoders require their input argument must be uniformly strings or numbers. Got r   r   )Úsetr;   ÚsortedÚextendr/   r,   Úarrayr   Ú	TypeErrorrW   Ú_get_countsr"   )r   r   r   Úuniques_setÚmissing_valuesr&   Útypesr(   r   r   r   r   ±   s(    ÿÿþr   T)Úcheck_unknownc             
   C   s~   t | |ƒ\}}| | jd¡s(zt| |ƒW S  ty' } ztd|› �ƒ‚d}~ww |r8t| |ƒ}|r8td|› �ƒ‚t|| |d�S )aØ  Helper function to encode values into [0, n_uniques - 1].

    Uses pure python method for object dtype, and numpy method for
    all other dtypes.
    The numpy method has the limitation that the `uniques` need to
    be sorted. Importantly, this is not checked but assumed to already be
    the case. The calling method needs to ensure this for all non-object
    values.

    Parameters
    ----------
    values : ndarray
        Values to encode.
    uniques : ndarray
        The unique values in `values`. If the dtype is not object, then
        `uniques` needs to be sorted.
    check_unknown : bool, default=True
        If True, check for values in `values` that are not in `unique`
        and raise an error. This is ignored for object dtype, and treated as
        True in this case. This parameter is useful for
        _BaseEncoder._transform() to avoid calling _check_unknown()
        twice.

    Returns
    -------
    encoded : ndarray
        Encoded values
    Únumericz%y contains previously unseen labels: Nr   )r	   Úisdtyper   rW   rG   Ú
ValueErrorÚ_check_unknownr   )r   r&   re   r   r#   ÚeÚdiffr   r   r   Ú_encodeË   s   €ÿ
rl   c                    sŠ  t | |ƒ\}}d}| | jd¡spt| ƒ}t|ƒ\}}t|ƒ‰tˆƒ\‰‰|ˆ }|jo.ˆj }	|jo5ˆj }
‡‡fdd„‰ |r\|sE|	sE|
rR| ‡ fdd„| D ƒ¡}n
|jt	| ƒ|j
d�}t|ƒ}|
rg| d¡ |	ro| tj¡ nM| | ¡}tj||d|d	�}|r”|jrŠt| ||ƒ}n
|jt	| ƒ|j
d�}| | |¡¡r¹| |¡}| |¡r¹|jr´|r´| | ¡}d
||< ||  }t|ƒ}|rÃ||fS |S )a‰  
    Helper function to check for unknowns in values to be encoded.

    Uses pure python method for object dtype, and numpy method for
    all other dtypes.

    Parameters
    ----------
    values : array
        Values to check for unknowns.
    known_values : array
        Known values. Must be unique.
    return_mask : bool, default=False
        If True, return a mask of the same shape as `values` indicating
        the valid values.

    Returns
    -------
    diff : list
        The unique values present in `values` and not in `know_values`.
    valid_mask : boolean array
        Additionally returned if ``return_mask=True``.

    Nrf   c                    s$   | ˆv pˆ j o
| d u pˆ jot| ƒS r6   )r*   r    r   )r9   )Úmissing_in_uniquesrb   r   r   Úis_valid  s
   ÿýz _check_unknown.<locals>.is_validc                    s   g | ]}ˆ |ƒ‘qS r   r   r7   )rn   r   r   rS   &  rT   z"_check_unknown.<locals>.<listcomp>rX   T)Úassume_uniquer   r   )r	   rg   r   r\   r;   r    r*   r_   Úonesr"   r4   Úlistr+   r,   r   r
   Ú	setdiff1dr   r   ÚanyÚisnan)r   Zknown_valuesÚreturn_maskr   r#   Ú
valid_maskZ
values_setZmissing_in_valuesrk   Znan_in_diffZnone_in_diffr   Zdiff_is_nanÚis_nanr   )rn   rm   rb   r   ri   ö   sL   
€





ri   c                       s0   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Z‡  ZS )Ú_NaNCounterz$Counter with support for nan values.c                    s   t ƒ  |  |¡¡ d S r6   )r=   r>   Ú_generate_items)r-   r?   rC   r   r   r>   M  s   z_NaNCounter.__init__c                 c   s>   � |D ]}t |ƒs|V  qt| dƒsd| _|  jd7  _qdS )z>Generate items without nans. Stores the nan counts separately.Ú	nan_countr   r   N)r   rF   rz   )r-   r?   Úitemr   r   r   ry   P  s   €
úz_NaNCounter._generate_itemsc                 C   rE   )Nrz   )rF   r   rz   rG   rH   r   r   r   rI   Z  rJ   z_NaNCounter.__missing__)r0   r1   r2   r3   r>   ry   rI   rK   r   r   rC   r   rx   J  s
    
rx   c           
   	   C   sæ   | j jdv r9t| ƒ}tjt|ƒtjd�}t|ƒD ]\}}tt	ƒ� || ||< W d  ƒ n1 s1w   Y  q|S t
| dd�\}}tj||dd�}t |d ¡r[t |d ¡r[d|d< t ||| ¡}	tj|tjd�}||	 ||< |S )zÌGet the count of each of the `uniques` in `values`.

    The counts will use the order passed in by `uniques`. For non-object dtypes,
    `uniques` is assumed to be sorted and `np.nan` is at the end.
    ZOUrX   NT)r   )ro   r   )r   Úkindrx   r,   Úzerosr"   Úint64rU   r   rG   r   Úisinrt   ÚsearchsortedÚ
zeros_like)
r   r&   Úcounterr.   rL   r{   r   r%   Zuniques_in_valuesZunique_valid_indicesr   r   r   ra   `  s"   
ÿ€ra   )FF)F)Úcollectionsr   Ú
contextlibr   Útypingr   Únumpyr,   Ú
_array_apir   r   r   r	   r
   Ú_missingr   r   r   r)   r;   Údictr<   rW   r   rl   ri   rx   ra   r   r   r   r   Ú<module>   s"   
)'&
+T