a
    h=                     @   s  d dl Z d dlmZ d dlZd dlZd dlmZ d dlm	Z	m
Z
 d dlmZ d dlmZmZmZmZmZmZmZmZ d dlmZ dd	 Zd
d Zejdedd ZG dd dZG 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#ejdddgd d! Z$d"d# Z%ejd$ddgd%d& Z&G d'd( d(edd)Z'd*d+ Z(d,d- Z)G d.d/ d/Z*d0d1 Z+d2d3 Z,G d4d5 d5eZ-d6d7 Z.G d8d9 d9eZ/d:d; Z0G d<d= d=eZ1ejdddgd>d? Z2ejd@e3ej4dAdB Z5dCdD Z6dEdF Z7dS )G    N)
namedtuple)assert_array_equal)config_context
get_config)StandardScaler)ADAPTERS_MANAGERContainerAdapterProtocol_get_adapter_from_container_get_output_config_safe_set_output_SetOutputMixin_wrap_data_with_containercheck_library_installed)CSR_CONTAINERSc                     s  t d} tg dg dg}tjg dtd tddg}| jddgdd	gg|d
}tjd }|j|| fddd}t	|| jsJ t
|j  t
|j| tjddgtd}| jddgdd	ggddgd
}|j|||d}t
|j| t
|j|j ||sJ ||rJ tjddgtd}|||}t
|j| | jg dg dgg dd}	| jdgdggdgd}
||	|
g}| jg dg dgg dd}| j|| | jddgdd	ggddgd}tjddgtd}|||}t
|j| | jddgdd	gg|d
}|j||ddgdd }||us>J t|jddgksVJ t|jddgksnJ | jddgdd	gg|d
}|j||ddgd!d }||u sJ t|jddgksJ t|jddgksJ d"S )#z+Check pandas adapter has expected behavior.pandas   r      r   r   r   )f0f1f2Zdtyper   r      r   indexc                      s    S N r   columnsr   _/var/www/html/assistant/venv/lib/python3.9/site-packages/sklearn/utils/tests/test_set_output.py<lambda>        z%test_pandas_adapter.<locals>.<lambda>r   r   r   
      acr   r      r         r%   ber*   r(   r   r   r(   r*   r   r*   r+   r(   r%   r-   r.   r&   Zx__aZy__ar-   Fr   ZinplaceTN)pytestimportorskipnpasarrayobject	DataFramer   adapterscreate_container
isinstancer   r   r   is_supported_containerarrayrename_columnshstackZtestingassert_frame_equallist)pdX_npr   	X_df_origadapterX_containernew_columnsX_dfnew_dfX_df_1X_df_2	X_stackedexpected_dfX_outputr   r   r    test_pandas_adapter   sR    

rO   c                     sT  t d} tg dg dg}g d | j| dd}tjd }|j|| fddd	}t|| jsjJ t	|j
  tjg d
td}|j|||d	}t	|j
| ||sJ ||rJ tjg dtd}|||}t	|j
| | jg dg dgg ddd}| jdgdggdgdd}|||g}	| jg dg dgg ddd}
ddlm} ||	|
 | jddgddggddgdd}|j||ddgdd}||usJ t|j
ddgksJ t|j
ddgksJ | jddgddggddgdd}|j||ddgd d}||u s J t|j
ddgks8J t|j
ddgksPJ d!S )"z+Check Polars adapter has expected behavior.polarsr   r   )r   r   Zf3row)ZschemaZorientc                      s    S r   r   r   r   r   r    r!   ^   r"   z%test_polars_adapter.<locals>.<lambda>r   )r%   r-   r&   r   )r%   r&   gr'   r)   r,   r*   r(   r&   r/   r0   r1   r   )r@   r   r   r   r%   r-   dFr2   TN)r3   r4   r5   r=   r8   r   r9   r:   r;   r   r   r6   r7   r<   r>   r?   Zpolars.testingr@   rA   )plrC   rD   rE   rF   rG   rI   rJ   rK   rL   rM   r@   rH   rN   r   r   r    test_polars_adapterV   sD    

 
  rU   csr_containerc              	   C   s   t g dg dg}| |}d}tddN tjt|d  td||t  W d   n1 sb0    Y  W d   n1 s0    Y  dS )	z*Check errors in _wrap_data_with_container.r   r   z.The transformer outputs a scipy sparse matrix.r   transform_outputmatch	transformN)r5   r6   r   r3   raises
ValueErrorr   r   )rV   XZX_csrrZ   r   r   r     test__container_error_validation   s    r_   c                   @   s   e Zd ZdS ),EstimatorWithoutSetOutputAndWithoutTransformN__name__
__module____qualname__r   r   r   r    r`      s   r`   c                   @   s   e Zd ZdddZdS )!EstimatorNoSetOutputWithTransformNc                 C   s   |S r   r   selfr^   yr   r   r    r[      s    z+EstimatorNoSetOutputWithTransform.transform)Nrb   rc   rd   r[   r   r   r   r    re      s   re   c                   @   s*   e Zd ZdddZd	ddZd
ddZdS )EstimatorWithSetOutputNc                 C   s   |j d | _| S Nr   shapen_features_in_rf   r   r   r    fit   s    zEstimatorWithSetOutput.fitc                 C   s   |S r   r   rf   r   r   r    r[      s    z EstimatorWithSetOutput.transformc                 C   s   t jdd t| jD tdS )Nc                 S   s   g | ]}d | qS r^   r   .0ir   r   r    
<listcomp>   r"   z@EstimatorWithSetOutput.get_feature_names_out.<locals>.<listcomp>r   r5   r6   rangern   r7   rg   Zinput_featuresr   r   r    get_feature_names_out   s    z,EstimatorWithSetOutput.get_feature_names_out)N)N)Nrb   rc   rd   ro   r[   rx   r   r   r   r    rj      s   

rj   c                  C   s   t  } t| dd t } tjtdd t| dd W d   n1 sH0    Y  t t	g dg} t| dd t
d| }|d dksJ t| d	d t
d| }|d d	ksJ t| dd t
d| }|d d	ksJ dS )
z)Check _safe_set_output works as expected.r   r[   zUnable to configure outputrY   Nr   r   r   r[   densedefault)r`   r   re   r3   r\   r]   rj   ro   r5   r6   r
   )estconfigr   r   r    test__safe_set_output   s    *


r   c                   @   s   e Zd ZdddZdS )2EstimatorNoSetOutputWithTransformNoFeatureNamesOutNc                 C   s   |S r   r   rf   r   r   r    r[      s    z<EstimatorNoSetOutputWithTransformNoFeatureNamesOut.transform)Nri   r   r   r   r    r      s   r   c                  C   s   t  } t| drJ dS )zEEstimator without get_feature_names_out does not define `set_output`.
set_outputN)r   hasattr)r~   r   r   r    test_set_output_mixin   s    r   c                  C   sh   t g dg dg} t }t|dd d}tjt|d ||  W d   n1 sZ0    Y  dS )z$Check transform with invalid config.r   r   badrz   output config must be inrY   N)r5   r6   rj   r   r3   r\   r]   r[   r^   r~   msgr   r   r    test__safe_set_output_error   s    r   dataframe_libr   rP   c                 C   s   t | }tg dg dg}t |}|jdd}||u sDJ ||}t|tj	s^J |j| d ||}t||j
sJ dS )z%Check that the output is a dataframe.r   r   Nrz   )r3   r4   r5   r6   rj   ro   r   r[   r;   Zndarrayr8   )r   libr^   r~   Zest2Z
X_trans_npZ
X_trans_pdr   r   r    test_set_output_method   s    


r   c                  C   sn   t g dg dg} t | }|jdd d}tjt|d ||  W d   n1 s`0    Y  dS )z-Check transform fails with invalid transform.r   r   r   rz   r   rY   N)	r5   r6   rj   ro   r   r3   r\   r]   r[   r   r   r   r    test_set_output_method_error   s    r   rX   c                 C   s   t  d }td}|d |ks"J t| d td}|d | ksFJ t }td|}|d | ksfJ t }td|}|d | ksJ |jdd td|}|d dksJ W d   n1 s0    Y  |j| d td|}|d | ksJ dS )z+Check _get_output_config works as expected.rX   r[   r|   rW   r}   rz   N)r   r
   r   re   rj   r   )rX   Zglobal_configr   r~   r   r   r    test__get_output_config   s$    



.
r   c                   @   s   e Zd ZdddZdS ) EstimatorWithSetOutputNoAutoWrapNc                 C   s   |S r   r   rf   r   r   r    r[   !  s    z*EstimatorWithSetOutputNoAutoWrap.transform)Nri   r   r   r   r    r      s   r   Zauto_wrap_output_keysc                  C   s@   t  } t| drJ tg dg dg}|| |u s<J dS )z4Check that auto_wrap_output_keys=None does not wrap.r   r   r   N)r   r   r5   r6   r[   )r~   r^   r   r   r    test_get_output_auto_wrap_false%  s    r   c                  C   sJ   d} t jt| d$ G dd dtdd}W d    n1 s<0    Y  d S )Nz6auto_wrap_output_keys must be None or a tuple of keys.rY   c                   @   s   e Zd ZdS )zLtest_auto_wrap_output_keys_errors_with_incorrect_input.<locals>.BadEstimatorNra   r   r   r   r    BadEstimator2  s   r   Zbad_parameterr   )r3   r\   r]   r   )r   r   r   r   r    6test_auto_wrap_output_keys_errors_with_incorrect_input.  s    r   c                       s   e Zd Z fddZ  ZS )AnotherMixinc                    s   t  jf i | || _d S r   )super__init_subclass__custom_parameter)clsr   kwargs	__class__r   r    r   7  s    zAnotherMixin.__init_subclass__)rb   rc   rd   r   __classcell__r   r   r   r    r   6  s   r   c                  C   s<   G dd dt tdd} |  }|jdks*J t|ds8J dS )z9Check that multiple init_subclasses passes parameters up.c                   @   s    e Zd ZdddZdddZdS )z>test_set_output_mixin_custom_mixin.<locals>.BothMixinEstimatorNc                 S   s   |S r   r   rf   r   r   r    r[   @  s    zHtest_set_output_mixin_custom_mixin.<locals>.BothMixinEstimator.transformc                 S   s   |S r   r   rw   r   r   r    rx   C  s    zTtest_set_output_mixin_custom_mixin.<locals>.BothMixinEstimator.get_feature_names_out)N)N)rb   rc   rd   r[   rx   r   r   r   r    BothMixinEstimator?  s   
r   {   )r   r   N)r   r   r   r   )r   r~   r   r   r    "test_set_output_mixin_custom_mixin<  s    r   c                  C   sZ   G dd dt } G dd d| }G dd d| }G dd d||}| d	dksVJ d	S )
zjCheck that multi-inheritance resolves to the correct class method.

    Non-regression test gh-25293.
    c                   @   s   e Zd Zdd ZdS )z!test_set_output_mro.<locals>.Basec                 S   s   dS )NBaser   rg   r^   r   r   r    r[   R  s    z+test_set_output_mro.<locals>.Base.transformNri   r   r   r   r    r   Q  s   r   c                   @   s   e Zd ZdS )ztest_set_output_mro.<locals>.ANra   r   r   r   r    AU  s   r   c                   @   s   e Zd Zdd ZdS )ztest_set_output_mro.<locals>.Bc                 S   s   dS )NBr   r   r   r   r    r[   Y  s    z(test_set_output_mro.<locals>.B.transformNri   r   r   r   r    r   X  s   r   c                   @   s   e Zd ZdS )ztest_set_output_mro.<locals>.CNra   r   r   r   r    C\  s   r   N)r   r[   )r   r   r   r   r   r   r    test_set_output_mroK  s
    r   c                   @   s*   e Zd ZdddZd	ddZd
ddZdS )EstimatorWithSetOutputIndexNc                 C   s   |j d | _| S rk   rl   rf   r   r   r    ro   c  s    zEstimatorWithSetOutputIndex.fitc                 C   s.   dd l }|j| dd t|jd D dS )Nr   c                 S   s   g | ]}d | qS )sr   rq   r   r   r    rt   k  r"   z9EstimatorWithSetOutputIndex.transform.<locals>.<listcomp>r   )r   r8   to_numpyrv   rm   )rg   r^   rh   rB   r   r   r    r[   g  s    z%EstimatorWithSetOutputIndex.transformc                 C   s   t jdd t| jD tdS )Nc                 S   s   g | ]}d | qS rp   r   rq   r   r   r    rt   n  r"   zEEstimatorWithSetOutputIndex.get_feature_names_out.<locals>.<listcomp>r   ru   rw   r   r   r    rx   m  s    z1EstimatorWithSetOutputIndex.get_feature_names_out)N)N)Nry   r   r   r   r    r   b  s   

r   c                  C   s^   t d} | jg dg dgddgd}t jdd}|| ||}t|jdd	g d
S )zZCheck that set_output does not override index.

    Non-regression test for gh-25730.
    r   r{   )r*   r(   r+   r   r   r   rz   s0s1N)	r3   r4   r8   r   r   ro   r[   r   r   )rB   r^   r~   X_transr   r   r    !test_set_output_pandas_keep_indexq  s    


r   c                   @   s   e Zd Zdd ZdddZdS )EstimatorReturnTuplec                 C   s
   || _ d S r   OutputTuple)rg   r   r   r   r    __init__  s    zEstimatorReturnTuple.__init__Nc                 C   s   |  |d| S )Nr   r   rf   r   r   r    r[     s    zEstimatorReturnTuple.transform)N)rb   rc   rd   r   r[   r   r   r   r    r     s   r   c                  C   s\   t dd} tg dg}t| d}||}t|| s<J t|j| t|jd|  dS )z+Check that namedtuples are kept by default.OutputzX, Yr{   r   r   N)	r   r5   r6   r   r[   r;   r   r^   Y)r   r^   r~   r   r   r   r    test_set_output_named_tuple_out  s    


r   c                   @   s*   e Zd ZdddZd	ddZd
ddZdS )EstimatorWithListInputNc                 C   s    t |tsJ t|d | _| S )Nr   )r;   rA   lenrn   rf   r   r   r    ro     s    zEstimatorWithListInput.fitc                 C   s   |S r   r   rf   r   r   r    r[     s    z EstimatorWithListInput.transformc                 C   s   t jdd t| jD tdS )Nc                 S   s   g | ]}d | qS rp   r   rq   r   r   r    rt     r"   z@EstimatorWithListInput.get_feature_names_out.<locals>.<listcomp>r   ru   rw   r   r   r    rx     s    z,EstimatorWithListInput.get_feature_names_out)N)N)Nry   r   r   r   r    r     s   

r   c                 C   s`   t | }g dg dg}t }|j| d |||}t||jsLJ t|j	g d dS )zJCheck set_output for list input.

    Non-regression test for #27037.
    )r   r   r   r   )r*   r(   r+      rz   )ZX0ZX1ZX2ZX3N)
r3   r4   r   r   ro   r[   r;   r8   r   r   )r   r   r^   r~   ZX_outr   r   r    test_set_output_list_input  s    
r   namec                 C   s   t tj|  tsJ dS )z*Check adapters have the correct interface.N)r;   r   r9   r   r   r   r   r     test_adapter_class_has_interface  s    r   c                    s^   t j  fdd}| t d| d}tjt|d td W d   n1 sP0    Y  dS )zCheck import error changed.c                    s   | dkrt   | d d d S )Nr   )package)ImportErrorr   Zorig_import_moduler   r    patched_import_module  s    z;test_check_library_installed.<locals>.patched_import_moduleimport_modulez-Setting output container to 'pandas' requiresrY   r   N)	importlibr   setattrr3   r\   r   r   )Zmonkeypatchr   r   r   r   r    test_check_library_installed  s    r   c                  C   sz   t d} | g dg dd}t|}|jdks8J d}t jt|d t|  W d   n1 sl0    Y  dS )z4Check the behavior fo `_get_adapter_from_container`.r   r{   )r#      d   )r%   r-   zAThe container does not have a registered adapter in scikit-learn.rY   N)r3   r4   r8   r	   Zcontainer_libr\   r]   r   )rB   r^   rE   err_msgr   r   r    test_get_adapter_from_container  s    
r   )8r   collectionsr   numpyr5   r3   Znumpy.testingr   Zsklearn._configr   r   Zsklearn.preprocessingr   Zsklearn.utils._set_outputr   r   r	   r
   r   r   r   r   Zsklearn.utils.fixesr   rO   rU   markZparametrizer_   r`   re   rj   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   sortedr9   r   r   r   r   r   r   r    <module>   sR   (
?6



 	

