
    0Ph=                     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                     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|
          }t          j        d         }|                    ||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      r   indexc                       S N columnss   c/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/sklearn/utils/tests/test_set_output.py<lambda>z%test_pandas_adapter.<locals>.<lambda>        G     r"   r   r   
      acr   r      r         r*   ber/   r-   r   r   r-   r/   r   r/   r0   r-   r*   r2   r3   r+   x__ay__ar2   Fr#   inplaceTN)pytestimportorskipnpasarrayobject	DataFramer   adapterscreate_container
isinstancer   r#   r   is_supported_containerarrayrename_columnshstack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#   s                 @r$   test_pandas_adapterrX      s   		X	&	&B:yyy))),--Dj+++6:::GJ1vEq!fq!f-U;;I'1G**4OOOO*TTK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>^   r&   r'   r"   )r*   r2   r+   r   )r*   r+   gr,   r.   r1   r/   r-   r+   r4   r5   r6   r   )rI   r   r   r   r*   r2   dFr9   TN)r;   r<   r=   rE   r@   r   rA   rB   rC   r   r#   r>   r?   rD   rF   rG   polars.testingrI   rJ   )plrL   rM   rN   rO   rP   rR   rS   rT   rU   rV   rI   rQ   rW   r#   s                 @r$   test_polars_adapterrd   V   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   )re   XX_csrrj   s       r$    test__container_error_validationrp      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$   rr   rr      s        Dr'   rr   c                       e Zd ZddZdS )!EstimatorNoSetOutputWithTransformNc                     |S r    r!   selfrn   ys      r$   rk   z+EstimatorNoSetOutputWithTransform.transform       r'   r    rt   ru   rv   rk   r!   r'   r$   rx   rx      (             r'   rx   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_rz   s      r$   fitzEstimatorWithSetOutput.fit       gajr'   c                     |S r    r!   rz   s      r$   rk   z EstimatorWithSetOutput.transform   r}   r'   c                 p    t          j        d t          | j                  D             t                    S )Nc                     g | ]}d | S rn   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    rt   ru   rv   r   rk   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   rk   zUnable to configure outputri   Nr   r   r   rk   densedefault)rr   r   rx   r;   rl   rm   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!   rz   s      r$   rk   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 inri   N)r=   r>   r   r   r;   rl   rm   rk   rn   r   msgs      r$   test__safe_set_output_errorr      s    

IIIyyy)**A
 
"
"CSE****
$C	z	-	-	-  a                 s   A::A>A>dataframe_libr   rZ   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   rk   rC   ndarrayr@   )r   librn   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   ri   N)	r=   r>   r   r   r   r;   rl   rm   rk   r   s      r$   test_set_output_method_errorr      s     	
IIIyyy)**A
 
"
"
&
&q
)
)CNNUN###
$C	z	-	-	-  a                 s   /BBBrh   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.rh   rk   r   rg   r   r   N)r   r   r   rx   r   r   )rh   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!   rz   s      r$   rk   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>   rk   )r   rn   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.ri   c                       e Zd ZdS )Ltest_auto_wrap_output_keys_errors_with_incorrect_input.<locals>.BadEstimatorNrs   r!   r'   r$   BadEstimatorr   2  s        Dr'   r   bad_parameterr   )r;   rl   rm   r   )r   r   s     r$   6test_auto_wrap_output_keys_errors_with_incorrect_inputr   .  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__7  s.    !!++F+++/r'   )rt   ru   rv   r   __classcell__)r   s   @r$   r   r   6  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!   rz   s      r$   rk   zHtest_set_output_mixin_custom_mixin.<locals>.BothMixinEstimator.transform@  s    Hr'   c                     |S r    r!   r   s     r$   r   zTtest_set_output_mixin_custom_mixin.<locals>.BothMixinEstimator.get_feature_names_outC  s    !!r'   r    )rt   ru   rv   rk   r   r!   r'   r$   BothMixinEstimatorr   ?  s<        	 	 	 		" 	" 	" 	" 	" 	"r'   r   {   )r   r   N)r   r   r   r   )r   r   s     r$   "test_set_output_mixin_custom_mixinr   <  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{   rn   s     r$   rk   z+test_set_output_mro.<locals>.Base.transformR  s    6r'   Nr~   r!   r'   r$   r   r   Q  s#        	 	 	 	 	r'   r   c                       e Zd ZdS )test_set_output_mro.<locals>.ANrs   r!   r'   r$   Ar   U          r'   r   c                       e Zd Zd ZdS )test_set_output_mro.<locals>.Bc                     dS )NBr!   r   s     r$   rk   z(test_set_output_mro.<locals>.B.transformY  s    3r'   Nr~   r!   r'   r$   r   r   X  s#        	 	 	 	 	r'   r   c                       e Zd ZdS )test_set_output_mro.<locals>.CNrs   r!   r'   r$   Cr   \  r   r'   r   N)r   rk   )r   r   r   r   s       r$   test_set_output_mror   K  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   rz   s      r$   r   zEstimatorWithSetOutputIndex.fitc  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>k  s    0T0T0TQQ0T0T0Tr'   r   )r   r@   to_numpyr   r   )r{   rn   r|   rK   s       r$   rk   z%EstimatorWithSetOutputIndex.transformg  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>n  r   r'   r   r   r   s     r$   r   z1EstimatorWithSetOutputIndex.get_feature_names_outm  r   r'   r    r   r!   r'   r$   r   r   b  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   )r/   r-   r0   r   r   r   r   s0s1N)	r;   r<   r@   r   r   r   rk   r   r   )rK   rn   r   X_transs       r$   !test_set_output_pandas_keep_indexr   q  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   rz   s      r$   rk   zEstimatorReturnTuple.transform  s    1q5)))r'   r    )rt   ru   rv   r   rk   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   rk   rC   r   rn   Y)r  rn   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   )rC   rJ   lenr   rz   s      r$   r   zEstimatorWithListInput.fit  s/    !T"""""!!A$iir'   c                     |S r    r!   rz   s      r$   rk   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   )r/   r-   r0      r   )X0X1X2X3N)
r;   r<   r  r   r   rk   rC   r@   r   r#   )r   r   rn   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)rC   r   rA   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' requiresri   r   N)	importlibr  setattrr;   rl   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*   r2   zAThe container does not have a registered adapter in scikit-learn.ri   N)r;   r<   r@   r
   container_librl   rm   r   )rK   rn   rN   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   rX   rd   markparametrizerp   rr   rx   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  sortedrA   r  r!  r'  r!   r'   r$   <module>r2     so       " " " " " "      , , , , , , 6 6 6 6 6 6 6 6 0 0 0 0 0 0	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 / . . . . .<0 <0 <0~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'   