o
    i?                     @   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	}| jddg|d	}tjd }|j	|| fd
dd}t
|| jsOJ t|j  t|j| |j	|| fddd}t
|| jsoJ 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 ||sJ ||rJ 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sFJ t|jd!dgksRJ t|jddgks^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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   indexc                          S N r   columnsr   ]/home/kim/smarthome/.venv/lib/python3.10/site-packages/sklearn/utils/tests/test_set_output.py<lambda>!       z%test_pandas_adapter.<locals>.<lambda>r   c                      r   r   r   r   r   r   r!   r"   '   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inplacer   TN)pytestimportorskipnpasarrayobject	DataFrameZSeriesr   adapterscreate_container
isinstancer   r    r   is_supported_containerarrayrename_columnshstackZtestingassert_frame_equallist)pdX_npr   	X_df_origZ
X_ser_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   s\   

rP   c                     sN  t d} tg dg dg}g d | j| dd}tjd }|j|| fddd	}t|| js5J t	|j
  tjg d
td}|j|||d	}t	|j
| ||sYJ ||r`J 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sJ t|j
ddgks%J d!S )"z+Check Polars adapter has expected behavior.polarsr   r   )r   r   Zf3row)ZschemaZorientc                      r   r   r   r   r   r   r!   r"   e   r#   z%test_polars_adapter.<locals>.<lambda>r   )r&   r.   r'   r   )r&   r'   gr(   r*   r-   r+   r)   r'   r0   r1   r2   r   )rA   r   r   r   r&   r.   dFr3   TN)r4   r5   r6   r>   r9   r   r:   r;   r<   r   r    r7   r8   r=   r?   r@   Zpolars.testingrA   rB   )plrD   rE   rF   rG   rH   rJ   rK   rL   rM   rN   rA   rI   rO   r   r   r!   test_polars_adapter]   sD   

 
  rV   csr_containerc              	   C   s   t g dg dg}| |}d}tdd0 tjt|d td||t  W d   n1 s1w   Y  W d   dS W d   dS 1 sIw   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)r6   r7   r   r4   raises
ValueErrorr   r   )rW   XZX_csrr[   r   r   r!    test__container_error_validation   s   "r`   c                   @      e Zd ZdS ),EstimatorWithoutSetOutputAndWithoutTransformN__name__
__module____qualname__r   r   r   r!   rb          rb   c                   @      e Zd ZdddZdS )!EstimatorNoSetOutputWithTransformNc                 C      |S r   r   selfr_   yr   r   r!   r\         z+EstimatorNoSetOutputWithTransform.transformr   rd   re   rf   r\   r   r   r   r!   ri          ri   c                   @   *   e Zd ZdddZdddZdddZdS )	EstimatorWithSetOutputNc                 C      |j d | _| S Nr   shapen_features_in_rk   r   r   r!   fit      zEstimatorWithSetOutput.fitc                 C   rj   r   r   rk   r   r   r!   r\      rn   z EstimatorWithSetOutput.transformc                 C      t jdd t| jD tdS )Nc                 S      g | ]}d | qS r_   r   .0ir   r   r!   
<listcomp>       z@EstimatorWithSetOutput.get_feature_names_out.<locals>.<listcomp>r   r6   r7   rangerw   r8   rl   Zinput_featuresr   r   r!   get_feature_names_out      z,EstimatorWithSetOutput.get_feature_names_outr   rd   re   rf   rx   r\   r   r   r   r   r!   rr      s    

rr   c                  C   s   t  } t| dd t } tjtdd t| dd W d   n1 s$w   Y  t t	g dg} t| dd t
d| }|d dksHJ t| d	d t
d| }|d d	ks[J t| dd t
d| }|d d	ksnJ dS )
z)Check _safe_set_output works as expected.r   r\   zUnable to configure outputrZ   Nr   r   r   r\   densedefault)rb   r   ri   r4   r]   r^   rr   rx   r6   r7   r
   )estconfigr   r   r!   test__safe_set_output   s    


r   c                   @   rh   )2EstimatorNoSetOutputWithTransformNoFeatureNamesOutNc                 C   rj   r   r   rk   r   r   r!   r\      rn   z<EstimatorNoSetOutputWithTransformNoFeatureNamesOut.transformr   ro   r   r   r   r!   r      rp   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   sj   t g dg dg} t }t|dd d}tjt|d ||  W d   dS 1 s.w   Y  dS )z$Check transform with invalid config.r   r   badr   output config must be inrZ   N)r6   r7   rr   r   r4   r]   r^   r\   r_   r   msgr   r   r!   test__safe_set_output_error   s   "r   dataframe_libr   rQ   c                 C   s   t | }tg dg dg}t |}|jdd}||u s"J ||}t|tj	s/J |j| d ||}t||j
sBJ dS )z%Check that the output is a dataframe.r   r   Nr   )r4   r5   r6   r7   rr   rx   r   r\   r<   Zndarrayr9   )r   libr_   r   Zest2Z
X_trans_npZ
X_trans_pdr   r   r!   test_set_output_method   s   


r   c                  C   sp   t g dg dg} t | }|jdd d}tjt|d ||  W d   dS 1 s1w   Y  dS )z-Check transform fails with invalid transform.r   r   r   r   r   rZ   N)	r6   r7   rr   rx   r   r4   r]   r^   r\   r   r   r   r!   test_set_output_method_error   s   "r   rY   c                 C   s   t  d }td}|d |ksJ t| dG td}|d | ks#J t }td|}|d | ks3J t }td|}|d | ksCJ |jdd td|}|d dksVJ W d   n1 s`w   Y  |j| d td|}|d | ksxJ dS )z+Check _get_output_config works as expected.rY   r\   r   rX   r   r   N)r   r
   r   ri   rr   r   )rY   Zglobal_configr   r   r   r   r!   test__get_output_config  s&   




r   c                   @   rh   ) EstimatorWithSetOutputNoAutoWrapNc                 C   rj   r   r   rk   r   r   r!   r\   (  rn   z*EstimatorWithSetOutputNoAutoWrap.transformr   ro   r   r   r   r!   r   '  rp   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   r6   r7   r\   )r   r_   r   r   r!   test_get_output_auto_wrap_false,  s   r   c                  C   sL   d} t jt| d G dd dtdd}W d    d S 1 sw   Y  d S )Nz6auto_wrap_output_keys must be None or a tuple of keys.rZ   c                   @   ra   )zLtest_auto_wrap_output_keys_errors_with_incorrect_input.<locals>.BadEstimatorNrc   r   r   r   r!   BadEstimator9  rg   r   Zbad_parameterr   )r4   r]   r^   r   )r   r   r   r   r!   6test_auto_wrap_output_keys_errors_with_incorrect_input5  s   "r   c                       s   e Zd Z fddZ  ZS )AnotherMixinc                    s   t  jdi | || _d S )Nr   )super__init_subclass__custom_parameter)clsr   kwargs	__class__r   r!   r   >  s   
zAnotherMixin.__init_subclass__)rd   re   rf   r   __classcell__r   r   r   r!   r   =  s    r   c                  C   s<   G dd dt tdd} |  }|jdksJ t|dsJ 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   rj   r   r   rk   r   r   r!   r\   G  rn   zHtest_set_output_mixin_custom_mixin.<locals>.BothMixinEstimator.transformc                 S   rj   r   r   r   r   r   r!   r   J  rn   zTtest_set_output_mixin_custom_mixin.<locals>.BothMixinEstimator.get_feature_names_outr   )rd   re   rf   r\   r   r   r   r   r!   BothMixinEstimatorF  s    
r   {   )r   r   N)r   r   r   r   )r   r   r   r   r!   "test_set_output_mixin_custom_mixinC  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s+J d	S )
zjCheck that multi-inheritance resolves to the correct class method.

    Non-regression test gh-25293.
    c                   @      e Zd Zdd ZdS )z!test_set_output_mro.<locals>.Basec                 S      dS )NBaser   rl   r_   r   r   r!   r\   Y  rn   z+test_set_output_mro.<locals>.Base.transformNro   r   r   r   r!   r   X      r   c                   @   ra   )ztest_set_output_mro.<locals>.ANrc   r   r   r   r!   A\  rg   r   c                   @   r   )ztest_set_output_mro.<locals>.Bc                 S   r   )NBr   r   r   r   r!   r\   `  rn   z(test_set_output_mro.<locals>.B.transformNro   r   r   r   r!   r   _  r   r   c                   @   ra   )ztest_set_output_mro.<locals>.CNrc   r   r   r   r!   Cc  rg   r   N)r   r\   )r   r   r   r   r   r   r!   test_set_output_mroR  s
   r   c                   @   rq   )	EstimatorWithSetOutputIndexNc                 C   rs   rt   ru   rk   r   r   r!   rx   j  ry   zEstimatorWithSetOutputIndex.fitc                 C   s.   dd l }|j| dd t|jd D dS )Nr   c                 S   r{   )sr   r}   r   r   r!   r   r  r   z9EstimatorWithSetOutputIndex.transform.<locals>.<listcomp>r   )r   r9   to_numpyr   rv   )rl   r_   rm   rC   r   r   r!   r\   n  s   &z%EstimatorWithSetOutputIndex.transformc                 C   rz   )Nc                 S   r{   r|   r   r}   r   r   r!   r   u  r   zEEstimatorWithSetOutputIndex.get_feature_names_out.<locals>.<listcomp>r   r   r   r   r   r!   r   t  r   z1EstimatorWithSetOutputIndex.get_feature_names_outr   r   r   r   r   r!   r   i  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   r   s0s1N)	r4   r5   r9   r   r   rx   r\   r   r   )rC   r_   r   X_transr   r   r!   !test_set_output_pandas_keep_indexx  s   


r   c                   @   s   e Zd Zdd ZdddZdS )EstimatorReturnTuplec                 C   s
   || _ d S r   OutputTuple)rl   r   r   r   r!   __init__  s   
zEstimatorReturnTuple.__init__Nc                 C   s   |  |d| S )Nr   r   rk   r   r   r!   r\     s   zEstimatorReturnTuple.transformr   )rd   re   rf   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   r6   r7   r   r\   r<   r   r_   Y)r   r_   r   r   r   r   r!   test_set_output_named_tuple_out  s   


r   c                   @   rq   )	EstimatorWithListInputNc                 C   s    t |tsJ t|d | _| S )Nr   )r<   rB   lenrw   rk   r   r   r!   rx     s   zEstimatorWithListInput.fitc                 C   rj   r   r   rk   r   r   r!   r\     rn   z EstimatorWithListInput.transformc                 C   rz   )Nc                 S   r{   r|   r   r}   r   r   r!   r     r   z@EstimatorWithListInput.get_feature_names_out.<locals>.<listcomp>r   r   r   r   r   r!   r     r   z,EstimatorWithListInput.get_feature_names_outr   r   r   r   r   r!   r     s    

r   c                 C   s`   t | }g dg dg}t }|j| d |||}t||js&J t|j	g d dS )zJCheck set_output for list input.

    Non-regression test for #27037.
    )r   r   r   r   )r+   r)   r,      r   )ZX0ZX1ZX2ZX3N)
r4   r5   r   r   rx   r\   r<   r9   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   r:   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   dS 1 s)w   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' requiresrZ   r   N)	importlibr   setattrr4   r]   r   r   )Zmonkeypatchr   r   r   r   r!   test_check_library_installed  s   
"r   c                  C   s|   t d} | g dg dd}t|}|jdksJ d}t jt|d t|  W d   dS 1 s7w   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.rZ   N)r4   r5   r9   r	   Zcontainer_libr]   r^   r   )rC   r_   rF   err_msgr   r   r!   test_get_adapter_from_container  s   
"r   )8r   collectionsr   numpyr6   r4   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   rP   rV   markZparametrizer`   rb   ri   rr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   sortedr:   r   r   r   r   r   r   r!   <module>   sT    (
F6



 	

