
    Pi?                     d   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 Zd	 Zej                            d
e          d             Z G d d          Z G d d          Z G d de          Zd Z  G d de          Z!d Z"d Z#ej                            dddg          d             Z$d Z%ej                            dddg          d             Z& G d ded           Z'd! Z(d" Z) G d# d$          Z*d% Z+d& Z, G d' d(e          Z-d) Z. G d* d+e          Z/d, Z0 G d- d.e          Z1ej                            dddg          d/             Z2ej                            d0 e3ej4                            d1             Z5d2 Z6d3 Z7dS )4    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                  z   t          j        d          } t          j        g dg dg          }t          j        g dt                    t          j        ddg          }|                     ddgddgg|	          }|                     ddg|	          }t          j        d         }|	                    ||fd
          }t          || j                  sJ t          |j                   t          |j        |           |	                    ||fd          }t          || j                  sJ t          |j                   t          |j        |           t          j        ddgt                    }|                     ddgddggddg	          }|	                    |||          }	t          |	j        |           t          |	j        |j                   |                    |          sJ |                    |          rJ t          j        ddgt                    }|                    ||          }	t          |	j        |           |                     g dg dgg d          }
|                     dgdggdg          }|                    |
|g          }|                     g dg dgg d          }| j                            ||           |                     ddgddggddg          }t          j        ddgt                    }|                    ||          }	t          |	j        |           |                     ddgddgg|	          }|	                    ||ddgd          }||usJ t)          |j                  d dgk    sJ t)          |j                  ddgk    sJ |                     ddgddgg|	          }|	                    ||ddgd!          }||u sJ t)          |j                  ddgk    sJ t)          |j                  ddgk    sJ d"S )#z+Check pandas adapter has expected behavior.pandas   r      r   r   r   )f0f1f2dtyper      r   indexc                       S N columnss   w/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/sklearn/utils/tests/test_set_output.py<lambda>z%test_pandas_adapter.<locals>.<lambda>!       G     r"   c                       S r    r!   r"   s   r$   r%   z%test_pandas_adapter.<locals>.<lambda>'   s    W r'   r   r   
      acr   r      r         r+   ber0   r.   r   r   r.   r0   r   r0   r1   r.   r+   r3   r4   r,   x__ay__ar3   Fr#   inplacer   TN)pytestimportorskipnpasarrayobject	DataFrameSeriesr   adapterscreate_container
isinstancer   r#   r   is_supported_containerarrayrename_columnshstacktestingassert_frame_equallist)pdX_npr   	X_df_orig
X_ser_origadapterX_containernew_columnsX_dfnew_dfX_df_1X_df_2	X_stackedexpected_dfX_outputr#   s                  @r$   test_pandas_adapterr[      s   		X	&	&B:yyy))),--Dj+++6:::GJ1vEq!fq!f-U;;IAq6//J'1G**4OOOO*TTKk2<00000{*G444{(%000 **4____*UUKk2<00000{*G444{(%000 *dD\888K<<!Q!Q(R<99D%%dI{%KKFv~{333v|TZ000))$/////--d33333 (C:V444K##D+66Fv~{333 \\999iii0///\JJF\\A3*se\44F/00I,,	|||$.B.B.B   K J!!)[999
 <<!Q!Q(3*<==D(FF+6:::K##D+66Fv~{333 <<!Q!Q(<66D''dS#JPU'VVH4!Q'''' !!c3Z//// <<!Q!Q(<66D''dS#JPT'UUHt#s++++ !!c3Z//////r'   c                     t          j        d          } t          j        g dg dg          }g d|                     |d          }t
          j        d         }|                    ||fd          }t          || j                  sJ t          |j
                   t          j        g d	t          
          }|                    |||          }t          |j
        |           |                    |          sJ |                    |          rJ t          j        g dt          
          }|                    ||          }t          |j
        |           |                     g dg dgg dd          }|                     dgdggdgd          }|                    ||g          }	|                     g dg dgg dd          }
ddlm}  ||	|
           |                     ddgddggddgd          }|                    ||ddgd          }||usJ t%          |j
                  ddgk    sJ t%          |j
                  ddgk    sJ |                     ddgddggddgd          }|                    ||ddg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   f3row)schemaorientc                       S r    r!   r"   s   r$   r%   z%test_polars_adapter.<locals>.<lambda>e   r&   r'   r"   )r+   r3   r,   r   )r+   r,   gr-   r/   r2   r0   r.   r,   r5   r6   r7   r   )rK   r   r   r   r+   r3   dFr:   TN)r<   r=   r>   rG   rA   r   rC   rD   rE   r   r#   r?   r@   rF   rH   rI   polars.testingrK   rL   )plrN   rO   rQ   rR   rS   rU   rV   rW   rX   rY   rK   rT   rZ   r#   s                 @r$   test_polars_adapterrg   ]   s\   		X	&	&B8YYY			*++D   GT'%@@I'1G**4OOOO*TTKk2<00000{*G444 *___F;;;K%%iK%PPFv~{333)))44444--d33333 (???&999K##I{;;Fv~{333 \\999iii0QV\WWF\\A3*cU5\AAF/00I,,	|||$-A-A-A%   K 211111y+... <<!Q!Q(#sE<JJD''dS#JPU'VVH4#s++++ !!c3Z//// <<!Q!Q(#sE<JJD''dS#JPT'UUHt#s++++ !!c3Z//////r'   csr_containerc           	      H   t          j        g dg dg          } | |          }d}t          d          5  t          j        t
          |          5  t          d||t                                 ddd           n# 1 swxY w Y   ddd           dS # 1 swxY w 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)r>   r?   r   r<   raises
ValueErrorr   r   )rh   XX_csrrm   s       r$    test__container_error_validationrs      sR    	
IIIyyy)**AM!E<E		2	2	2 O O]:U333 	O 	O%k5!^=M=MNNN	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	OO O O O O O O O O O O O O O O O O Os5   BA?3B?B	BB	BBBc                       e Zd ZdS ),EstimatorWithoutSetOutputAndWithoutTransformN__name__
__module____qualname__r!   r'   r$   ru   ru      s        Dr'   ru   c                       e Zd ZddZdS )!EstimatorNoSetOutputWithTransformNc                     |S r    r!   selfrq   ys      r$   rn   z+EstimatorNoSetOutputWithTransform.transform       r'   r    rw   rx   ry   rn   r!   r'   r$   r{   r{      (             r'   r{   c                   &    e Zd ZddZddZddZdS )EstimatorWithSetOutputNc                 *    |j         d         | _        | S Nr   shapen_features_in_r}   s      r$   fitzEstimatorWithSetOutput.fit       gajr'   c                     |S r    r!   r}   s      r$   rn   z EstimatorWithSetOutput.transform   r   r'   c                 p    t          j        d t          | j                  D             t                    S )Nc                     g | ]}d | S rq   r!   .0is     r$   
<listcomp>z@EstimatorWithSetOutput.get_feature_names_out.<locals>.<listcomp>       GGGq7q77GGGr'   r   r>   r?   ranger   r@   r~   input_featuress     r$   get_feature_names_outz,EstimatorWithSetOutput.get_feature_names_out   1    zGGE$2E,F,FGGGvVVVVr'   r    rw   rx   ry   r   rn   r   r!   r'   r$   r   r      sV              W W W W W Wr'   r   c                  n   t                      } t          | d           t                      } t          j        t
          d          5  t          | d           ddd           n# 1 swxY w Y   t                                          t          j	        g dg                    } t          | d           t          d|           }|d         dk    sJ t          | d	           t          d|           }|d         d	k    sJ t          | d           t          d|           }|d         d	k    sJ dS )
z)Check _safe_set_output works as expected.r   rn   zUnable to configure outputrl   Nr   r   r   rn   densedefault)ru   r   r{   r<   ro   rp   r   r   r>   r?   r   )estconfigs     r$   test__safe_set_outputr      s    7
8
8CSH---- ,
-
-C	z)E	F	F	F 2 211112 2 2 2 2 2 2 2 2 2 2 2 2 2 2 !
"
"
&
&rz999+'>'>
?
?CSH----S11F'?h&&&&SI....S11F'?i'''' SD))))S11F'?i''''''s   	A''A+.A+c                       e Zd ZddZdS )2EstimatorNoSetOutputWithTransformNoFeatureNamesOutNc                     |S r    r!   r}   s      r$   rn   z<EstimatorNoSetOutputWithTransformNoFeatureNamesOut.transform   r   r'   r    r   r!   r'   r$   r   r      r   r'   r   c                  F    t                      } t          | d          rJ dS )zEEstimator without get_feature_names_out does not define `set_output`.
set_outputN)r   hasattr)r   s    r$   test_set_output_mixinr      s)    
<
>
>CsL)))))))r'   c                     t          j        g dg dg          } t                      }t          |d           d}t	          j        t          |          5  |                    |            ddd           dS # 1 swxY w Y   dS )z$Check transform with invalid config.r   r   badr   output config must be inrl   N)r>   r?   r   r   r<   ro   rp   rn   rq   r   msgs      r$   test__safe_set_output_errorr      s    

IIIyyy)**A
 
"
"CSE****
$C	z	-	-	-  a                 s   A::A>A>dataframe_libr   r]   c                    t          j        |           }t          j        g dg dg          }t	                                          |          }|                    d          }||u sJ |                    |          }t          |t          j	                  sJ |                    |            |                    |          }t          ||j
                  sJ dS )z%Check that the output is a dataframe.r   r   Nr   )r<   r=   r>   r?   r   r   r   rn   rE   ndarrayrA   )r   librq   r   est2
X_trans_np
X_trans_pds          r$   test_set_output_methodr      s     
m
,
,C

IIIyyy)**A
 
"
"
&
&q
)
)C >>D>))D3;;;;""Jj"*-----NN]N+++q!!Jj#-0000000r'   c                  >   t          j        g dg dg          } t                                          |           }|                    d           d}t          j        t          |          5  |                    |            ddd           dS # 1 swxY w Y   dS )z-Check transform fails with invalid transform.r   r   r   r   r   rl   N)	r>   r?   r   r   r   r<   ro   rp   rn   r   s      r$   test_set_output_method_errorr      s     	
IIIyyy)**A
 
"
"
&
&q
)
)CNNUN###
$C	z	-	-	-  a                 s   /BBBrk   c                 t   t                      d         }t          d          }|d         |k    sJ t          |           5  t          d          }|d         | k    sJ t                      }t          d|          }|d         | k    sJ t	                      }t          d|          }|d         | k    sJ |                    d           t          d|          }|d         dk    sJ 	 ddd           n# 1 swxY w Y   |                    |            t          d|          }|d         | k    sJ dS )z+Check _get_output_config works as expected.rk   rn   r   rj   r   r   N)r   r   r   r{   r   r   )rk   global_configr   r   s       r$   test__get_output_configr     s   
 LL!34M,,F'?m++++	)9	:	:	: , ,#K00g"22222/11#K55g"22222$&&#K55g"22222 	+++#K55g)+++++#, , , , , , , , , , , , , , ,& NN-N...S11F'?.......s   B*C99C= C=c                       e Zd ZddZdS ) EstimatorWithSetOutputNoAutoWrapNc                     |S r    r!   r}   s      r$   rn   z*EstimatorWithSetOutputNoAutoWrap.transform(  r   r'   r    r   r!   r'   r$   r   r   '  r   r'   r   auto_wrap_output_keysc                      t                      } t          | d          rJ t          j        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   r>   r?   rn   )r   rq   s     r$   test_get_output_auto_wrap_falser   ,  sb    
*
,
,CsL)))))

IIIyyy)**Aa        r'   c                      d} t          j        t          |           5   G d dt          d          }d d d            d S # 1 swxY w Y   d S )Nz6auto_wrap_output_keys must be None or a tuple of keys.rl   c                       e Zd ZdS )Ltest_auto_wrap_output_keys_errors_with_incorrect_input.<locals>.BadEstimatorNrv   r!   r'   r$   BadEstimatorr   9  s        Dr'   r   bad_parameterr   )r<   ro   rp   r   )r   r   s     r$   6test_auto_wrap_output_keys_errors_with_incorrect_inputr   5  s    
BC	z	-	-	-  	 	 	 	 	?/ 	 	 	 	                 s   AAAc                        e Zd Z fdZ xZS )AnotherMixinc                 H     t                      j        di | || _        d S )Nr!   )super__init_subclass__custom_parameter)clsr   kwargs	__class__s      r$   r   zAnotherMixin.__init_subclass__>  s.    !!++F+++/r'   )rw   rx   ry   r   __classcell__)r   s   @r$   r   r   =  s8        0 0 0 0 0 0 0 0 0r'   r   c                       G d dt           t          d          }  |             }|j        dk    sJ t          |d          sJ dS )z9Check that multiple init_subclasses passes parameters up.c                       e Zd ZddZddZdS )>test_set_output_mixin_custom_mixin.<locals>.BothMixinEstimatorNc                     |S r    r!   r}   s      r$   rn   zHtest_set_output_mixin_custom_mixin.<locals>.BothMixinEstimator.transformG  s    Hr'   c                     |S r    r!   r   s     r$   r   zTtest_set_output_mixin_custom_mixin.<locals>.BothMixinEstimator.get_feature_names_outJ  s    !!r'   r    )rw   rx   ry   rn   r   r!   r'   r$   BothMixinEstimatorr   F  s<        	 	 	 		" 	" 	" 	" 	" 	"r'   r   {   )r   r   N)r   r   r   r   )r   r   s     r$   "test_set_output_mixin_custom_mixinr   C  su    " " " " "_lS " " " " 


C3&&&&3%%%%%%%r'   c                       G d dt                     }  G d d|           } G d d|           } G 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 ZdS )!test_set_output_mro.<locals>.Basec                     dS )NBaser!   r~   rq   s     r$   rn   z+test_set_output_mro.<locals>.Base.transformY  s    6r'   Nr   r!   r'   r$   r   r   X  s#        	 	 	 	 	r'   r   c                       e Zd ZdS )test_set_output_mro.<locals>.ANrv   r!   r'   r$   Ar   \          r'   r   c                       e Zd Zd ZdS )test_set_output_mro.<locals>.Bc                     dS )NBr!   r   s     r$   rn   z(test_set_output_mro.<locals>.B.transform`  s    3r'   Nr   r!   r'   r$   r   r   _  s#        	 	 	 	 	r'   r   c                       e Zd ZdS )test_set_output_mro.<locals>.CNrv   r!   r'   r$   Cr   c  r   r'   r   N)r   rn   )r   r   r   r   s       r$   test_set_output_mror   R  s               D       D       Aq    133==#%%%%%%r'   c                   &    e Zd ZddZddZddZdS )EstimatorWithSetOutputIndexNc                 *    |j         d         | _        | S r   r   r}   s      r$   r   zEstimatorWithSetOutputIndex.fitj  r   r'   c                     dd l }|                    |                                d t          |j        d                   D                       S )Nr   c                     g | ]}d | S )sr!   r   s     r$   r   z9EstimatorWithSetOutputIndex.transform.<locals>.<listcomp>r  s    0T0T0TQQ0T0T0Tr'   r   )r   rA   to_numpyr   r   )r~   rq   r   rM   s       r$   rn   z%EstimatorWithSetOutputIndex.transformn  sL     ||AJJLL0T0T%PQ
BSBS0T0T0T|UUUr'   c                 p    t          j        d t          | j                  D             t                    S )Nc                     g | ]}d | S r   r!   r   s     r$   r   zEEstimatorWithSetOutputIndex.get_feature_names_out.<locals>.<listcomp>u  r   r'   r   r   r   s     r$   r   z1EstimatorWithSetOutputIndex.get_feature_names_outt  r   r'   r    r   r!   r'   r$   r   r   i  sZ           V V V VW W W W W Wr'   r   c                  2   t          j        d          } |                     g dg dgddg          }t                                          d          }|                    |           |                    |          }t          |j        dd	g           d
S )zZCheck that set_output does not override index.

    Non-regression test for gh-25730.
    r   r   )r0   r.   r1   r   r   r   r   s0s1N)	r<   r=   rA   r   r   r   rn   r   r   )rM   rq   r   X_transs       r$   !test_set_output_pandas_keep_indexr   x  s    
 
	X	&	&B
iii+Aq6::A
%
'
'
2
2X
2
F
FCGGAJJJmmAGw}tTl33333r'   c                       e Zd Zd ZddZdS )EstimatorReturnTuplec                     || _         d S r    OutputTuple)r~   r  s     r$   __init__zEstimatorReturnTuple.__init__  s    &r'   Nc                 4    |                      |d|z            S )Nr   r   r}   s      r$   rn   zEstimatorReturnTuple.transform  s    1q5)))r'   r    )rw   rx   ry   r  rn   r!   r'   r$   r   r     s7        ' ' '* * * * * *r'   r   c                     t          dd          } t          j        g dg          }t          |           }|                    |          }t          ||           sJ t          |j        |           t          |j        d|z             dS )z+Check that namedtuples are kept by default.OutputzX, Yr   r   r   N)	r   r>   r?   r   rn   rE   r   rq   Y)r  rq   r   r   s       r$   test_set_output_named_tuple_outr    s    &))F

III;A
6
2
2
2CmmAGgv&&&&&wy!$$$wy!a%(((((r'   c                   &    e Zd ZddZddZddZdS )EstimatorWithListInputNc                 h    t          |t                    sJ t          |d                   | _        | S )Nr   )rE   rL   lenr   r}   s      r$   r   zEstimatorWithListInput.fit  s/    !T"""""!!A$iir'   c                     |S r    r!   r}   s      r$   rn   z EstimatorWithListInput.transform  r   r'   c                 p    t          j        d t          | j                  D             t                    S )Nc                     g | ]}d | S r   r!   r   s     r$   r   z@EstimatorWithListInput.get_feature_names_out.<locals>.<listcomp>  r   r'   r   r   r   s     r$   r   z,EstimatorWithListInput.get_feature_names_out  r   r'   r    r   r!   r'   r$   r	  r	    sV           
   W W W W W Wr'   r	  c                 2   t          j        |           }g dg dg}t                      }|                    |            |                    |                              |          }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   )r0   r.   r1      r   )X0X1X2X3N)
r<   r=   r	  r   r   rn   rE   rA   r   r#   )r   r   rq   r   X_outs        r$   test_set_output_list_inputr    s     
m
,
,C	|||$A
 
"
"CNN]N+++GGAJJ  ##EeS]+++++u}&>&>&>?????r'   namec                 T    t          t          j        |          t                    sJ dS )z*Check adapters have the correct interface.N)rE   r   rC   r	   )r  s    r$    test_adapter_class_has_interfacer    s*     &/57OPPPPPPPr'   c                     t           j        fd}|                     t           d|           d}t          j        t
          |          5  t          d           ddd           dS # 1 swxY w Y   dS )zCheck import error changed.c                 J    | dk    rt                       | d            d S )Nr   )package)ImportError)r  orig_import_modules    r$   patched_import_modulez;test_check_library_installed.<locals>.patched_import_module  s4    8--4......r'   import_modulez-Setting output container to 'pandas' requiresrl   r   N)	importlibr   setattrr<   ro   r  r   )monkeypatchr  r   r  s      @r$   test_check_library_installedr$    s    "0/ / / / /
 	?4IJJJ
9C	{#	.	.	. * *)))* * * * * * * * * * * * * * * * * *s   A))A-0A-c                  L   t          j        d          } |                     g dg dd          }t          |          }|j        dk    sJ d}t          j        t          |          5  t          |                                           ddd           dS # 1 swxY w Y   dS )z4Check the behavior fo `_get_adapter_from_container`.r   r   )r)      d   )r+   r3   zAThe container does not have a registered adapter in scikit-learn.rl   N)r<   r=   rA   r
   container_libro   rp   r   )rM   rq   rQ   err_msgs       r$   test_get_adapter_from_containerr*    s    		X	&	&B
999===99::A)!,,G H,,,,QG	z	1	1	1 2 2#AJJLL1112 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2s   *"BB B)8r!  collectionsr   numpyr>   r<   numpy.testingr   sklearn._configr   r   sklearn.preprocessingr   sklearn.utils._set_outputr   r	   r
   r   r   r   r   r   sklearn.utils.fixesr   r[   rg   markparametrizers   ru   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r	  r  sortedrC   r  r$  r*  r!   r'   r$   <module>r5     sr       " " " " " "      , , , , , , 6 6 6 6 6 6 6 6 0 0 0 0 0 0	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 / . . . . .C0 C0 C0L30 30 30l .99O O :9O	 	 	 	 	 	 	 	       
	W 	W 	W 	W 	W_ 	W 	W 	W( ( (6       
* * *	 	 	 8X*>??1 1 @?1(	 	 	 +h-ABB/ / CB/@    d    
! ! !  0 0 0 0 0 0 0 0& & && & &.W W W W W/ W W W4 4 4* * * * *? * * *	) 	) 	)
W 
W 
W 
W 
W_ 
W 
W 
W 8X*>??@ @ @?@  (8(A!B!BCCQ Q DCQ
* * * 2 2 2 2 2r'   