
    h$,f;                        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 d dlmZ d Zd	 Zej2                  j5                  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j2                  j5                  dddg      d        Z#d Z$ej2                  j5                  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, Z/ G d- d.e      Z0ej2                  j5                  dddg      d/        Z1ej2                  j5                  d0 e2ejf                              d1        Z4d2 Z5y)3    N)
namedtuple)assert_array_equal)config_context
get_config)StandardScaler)ADAPTERS_MANAGERContainerAdapterProtocol_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      }| j                  ddgdd	gg|
      }t        j                  d   }|j                  ||fd      }t        || j
                        sJ t        |j                         t        |j                  |       t        j                  ddgt              }| j                  ddgdd	ggddg
      }|j                  |||      }t        |j                  |       t        |j                  |j                         |j                  |      sJ |j                  |      rJ t        j                  ddgt              }|j                  ||      }t        |j                  |       | j                  g dg dgg d      }	| j                  dgdggdg      }
|j!                  |	|
g      }| j                  g dg dgg d      }| j"                  j%                  ||       | j                  ddgdd	ggddg      }t        j                  ddgt              }|j                  ||      }t        |j                  |       | j                  ddgdd	gg|
      }|j                  ||ddgd      }||usJ t'        |j                        ddgk(  sJ t'        |j                        ddgk(  sJ | j                  ddgdd	gg|
      }|j                  ||ddgd       }||u sJ t'        |j                        ddgk(  sJ t'        |j                        ddgk(  sJ y!)"z+Check pandas adapter has expected behavior.pandas   r      r   r   r   )f0f1f2dtyper   r      r   indexc                       S N columnss   Clib/python3.12/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.   r/   r,   r)   r1   r2   r*   x__ay__ar1   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_adapterrW      sq   			X	&B::y),-Djj+6:GJJ1vEq!fq!f-U;I''1G**4O*TKk2<<000{**G4{((%0 **dD\8K<<!Q!Q(R<9D%%dI{%KFv~~{3v||TZZ0))$///--d333 ((C:V4K##D+6Fv~~{3 \\9i0/\JF\\A3*se\4F/0I,,	|$.B  K JJ!!)[9
 <<!Q!Q(3*<=D((FF+6:K##D+6Fv~~{3 <<!Q!Q(<6D''dS#JPU'VH4!Q'''  !c3Z/// <<!Q!Q(<6D''dS#JPT'UHt#s+++  !c3Z///r&   c                  T   t        j                  d      } t        j                  g dg dg      }g d| j	                  |d      }t
        j                  d   }|j                  ||fd      }t        || j                        sJ t        |j                         t        j                  g d	t        
      }|j                  |||      }t        |j                  |       |j                  |      sJ |j                  |      rJ t        j                  g dt        
      }|j                  ||      }t        |j                  |       | j	                  g dg dgg dd      }| j	                  dgdggdgd      }|j                  ||g      }	| j	                  g dg dgg dd      }
ddlm}  ||	|
       | j	                  ddgddggddgd      }|j                  ||ddgd      }||usJ t%        |j                        ddgk(  sJ t%        |j                        ddgk(  sJ | j	                  ddgddggddgd      }|j                  ||ddgd      }||u sJ t%        |j                        ddgk(  sJ t%        |j                        ddgk(  sJ y )!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)   r1   r*   r   )r)   r*   gr+   r-   r0   r.   r,   r*   r3   r4   r5   r   )rH   r   r   r   r)   r1   dFr8   TN)r:   r;   r<   rD   r?   r   r@   rA   rB   r   r"   r=   r>   rC   rE   rF   polars.testingrH   rI   )plrK   rL   rM   rN   rO   rQ   rR   rS   rT   rU   rH   rP   rV   r"   s                 @r#   test_polars_adapterrc   U   s   			X	&B88Y	*+D GT'%@I''1G**4O*TKk2<<000{**G4 **_F;K%%iK%PFv~~{3)))444--d333 ((?&9K##I{;Fv~~{3 \\9i0QV\WF\\A3*cU5\AF/0I,,	|$-A%  K 2y+. <<!Q!Q(#sE<JD''dS#JPU'VH4#s+++  !c3Z/// <<!Q!Q(#sE<JD''dS#JPT'UHt#s+++  !c3Z///r&   csr_containerc           	         t        j                  g dg dg      } | |      }d}t        d      5  t        j                  t
        |      5  t        d||t                      ddd       ddd       y# 1 sw Y   xY w# 1 sw Y   yxY w)	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   )rd   XX_csrri   s       r#    test__container_error_validationro      s     	

Iy)*A!E<E		2 O]]:U3 	O%k5!^=MN	OO O	O 	OO Os#   BA6%B6A?	;BBc                       e Zd Zy),EstimatorWithoutSetOutputAndWithoutTransformN__name__
__module____qualname__r    r&   r#   rq   rq      s    r&   rq   c                       e Zd ZddZy)!EstimatorNoSetOutputWithTransformNc                     |S r   r    selfrm   ys      r#   rj   z+EstimatorNoSetOutputWithTransform.transform       r&   r   rs   rt   ru   rj   r    r&   r#   rw   rw          r&   rw   c                   $    e Zd ZddZddZddZy)EstimatorWithSetOutputNc                 .    |j                   d   | _        | S Nr   shapen_features_in_ry   s      r#   fitzEstimatorWithSetOutput.fit       ggajr&   c                     |S r   r    ry   s      r#   rj   z EstimatorWithSetOutput.transform   r|   r&   c                     t        j                  t        | j                        D cg c]  }d| 	 c}t              S c c}w Nrm   r   r<   r=   ranger   r>   rz   input_featuresis      r#   get_feature_names_outz,EstimatorWithSetOutput.get_feature_names_out   2    zzE$2E2E,FGqQqc7GvVVG   A r   rs   rt   ru   r   rj   r   r    r&   r#   r   r      s    Wr&   r   c                     t               } t        | d       t               } t        j                  t
        d      5  t        | d       ddd       t               j                  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 y# 1 sw Y   xY w)
z)Check _safe_set_output works as expected.r   rj   zUnable to configure outputrh   Nr   r   r   rj   densedefault)rq   r   rw   r:   rk   rl   r   r   r<   r=   r
   )estconfigs     r#   test__safe_set_outputr      s     7
8CSH- ,
-C	z)E	F 212 !
"
&
&rzz9+'>
?CSH-S1F'?h&&&SI.S1F'?i''' SD)S1F'?i'''2 2s   C,,C5c                       e Zd ZddZy)2EstimatorNoSetOutputWithTransformNoFeatureNamesOutNc                     |S r   r    ry   s      r#   rj   z<EstimatorNoSetOutputWithTransformNoFeatureNamesOut.transform   r|   r&   r   r}   r    r&   r#   r   r      r~   r&   r   c                  4    t               } t        | d      rJ y)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  |j                  |        ddd       y# 1 sw Y   yxY w)z$Check transform with invalid config.r   r   badr   output config must be inrh   N)r<   r=   r   r   r:   rk   rl   rj   rm   r   msgs      r#   test__safe_set_output_errorr      s[    


Iy)*A
 
"CSE*
$C	z	- a  s   A++A4dataframe_libr   rY   c                    t        j                  |       }t        j                  g dg dg      }t	               j                  |      }|j                  d      }||u sJ |j                  |      }t        |t        j                        sJ |j                  |        |j                  |      }t        ||j                        sJ y)z%Check that the output is a dataframe.r   r   Nr   )r:   r;   r<   r=   r   r   r   rj   rB   ndarrayr?   )r   librm   r   est2
X_trans_np
X_trans_pds          r#   test_set_output_methodr      s     

m
,C


Iy)*A
 
"
&
&q
)C >>D>)D3;;"Jj"**---NN]N+q!Jj#--000r&   c                     t        j                  g dg dg      } t               j                  |       }|j	                  d       d}t        j                  t        |      5  |j                  |        ddd       y# 1 sw Y   yxY w)z-Check transform fails with invalid transform.r   r   r   r   r   rh   N)	r<   r=   r   r   r   r:   rk   rl   rj   r   s      r#   test_set_output_method_errorr      sl     	

Iy)*A
 
"
&
&q
)CNNUN#
$C	z	- a  s   $A??Brg   c                    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 |j                  d       t        d|      }|d   dk(  sJ 	 ddd       j                  |        t        d|      }|d   | k(  sJ y# 1 sw Y   2xY w)z+Check _get_output_config works as expected.rg   rj   r   rf   r   r   N)r   r
   r   rw   r   r   )rg   global_configr   r   s       r#   test__get_output_configr      s   
 L!34M,F'?m+++	)9	: ,#K0g"2222/1#K5g"2222$&#K5g"2222 	+#K5g)+++#,& NN-N.S1F'?....+, ,s   A>CC(c                       e Zd ZddZy) EstimatorWithSetOutputNoAutoWrapNc                     |S r   r    ry   s      r#   rj   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      }|| j	                  |      u sJ y)z4Check that auto_wrap_output_keys=None does not wrap.r   r   r   N)r   r   r<   r=   rj   )r   rm   s     r#   test_get_output_auto_wrap_falser   $  sD    
*
,CsL)))


Iy)*Aa    r&   c                      d} t        j                  t        |       5   G d dt        d      }d d d        y # 1 sw Y   y xY w)Nz6auto_wrap_output_keys must be None or a tuple of keys.rh   c                       e Zd Zy)Ltest_auto_wrap_output_keys_errors_with_incorrect_input.<locals>.BadEstimatorNrr   r    r&   r#   BadEstimatorr   1  s    r&   r   bad_parameterr   )r:   rk   rl   r   )r   r   s     r#   6test_auto_wrap_output_keys_errors_with_incorrect_inputr   -  s8    
BC	z	- 	?/ 	  s	   9Ac                        e Zd Z fdZ xZS )AnotherMixinc                 2    t        |   di | || _        y )Nr    )super__init_subclass__custom_parameter)clsr   kwargs	__class__s      r#   r   zAnotherMixin.__init_subclass__6  s    !+F+/r&   )rs   rt   ru   r   __classcell__)r   s   @r#   r   r   5  s    0 0r&   r   c                  |     G d dt         t        d      }  |        }|j                  dk(  sJ t        |d      sJ y)z9Check that multiple init_subclasses passes parameters up.c                       e Zd ZddZddZy)>test_set_output_mixin_custom_mixin.<locals>.BothMixinEstimatorNc                     |S r   r    ry   s      r#   rj   zHtest_set_output_mixin_custom_mixin.<locals>.BothMixinEstimator.transform?  s    Hr&   c                     |S r   r    )rz   r   s     r#   r   zTtest_set_output_mixin_custom_mixin.<locals>.BothMixinEstimator.get_feature_names_outB  s    !!r&   r   )rs   rt   ru   rj   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   ;  s@    "_lS " 
C3&&&3%%%r&   c                       G d dt               }  G d d|       } G d d|       } G d d||      } |       j                  d	      dk(  sJ y	)
zjCheck that multi-inheritance resolves to the correct class method.

    Non-regression test gh-25293.
    c                       e Zd Zd Zy)!test_set_output_mro.<locals>.Basec                      y)NBaser    rz   rm   s     r#   rj   z+test_set_output_mro.<locals>.Base.transformQ  s    r&   Nr}   r    r&   r#   r   r   P  s    	r&   r   c                       e Zd Zy)test_set_output_mro.<locals>.ANrr   r    r&   r#   Ar   T      r&   r   c                       e Zd Zd Zy)test_set_output_mro.<locals>.Bc                      y)NBr    r   s     r#   rj   z(test_set_output_mro.<locals>.B.transformX  s    r&   Nr}   r    r&   r#   r   r   W  s    	r&   r   c                       e Zd Zy)test_set_output_mro.<locals>.CNrr   r    r&   r#   Cr   [  r   r&   r   N)r   rj   )r   r   r   r   s       r#   test_set_output_mror   J  sO     D D Aq  3==#%%%r&   c                   $    e Zd ZddZddZddZy)EstimatorWithSetOutputIndexNc                 .    |j                   d   | _        | S r   r   ry   s      r#   r   zEstimatorWithSetOutputIndex.fitb  r   r&   c                     dd l }|j                  |j                         t        |j                  d         D cg c]  }d| 	 c}      S c c}w )Nr   sr   )r   r?   to_numpyr   r   )rz   rm   r{   rJ   r   s        r#   rj   z%EstimatorWithSetOutputIndex.transformf  sB     ||AJJL%PQ
BS0TQ1QC0T|UU0Ts   A
c                     t        j                  t        | j                        D cg c]  }d| 	 c}t              S c c}w r   r   r   s      r#   r   z1EstimatorWithSetOutputIndex.get_feature_names_outl  r   r   r   r   r    r&   r#   r   r   a  s    VWr&   r   c                     t        j                  d      } | j                  g dg dgddg      }t               j	                  d      }|j                  |       |j                  |      }t        |j                  dd	g       y
)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)	r:   r;   r?   r   r   r   rj   r   r   )rJ   rm   r   X_transs       r#   !test_set_output_pandas_keep_indexr   p  sp    
 
		X	&B
i+Aq6:A
%
'
2
2X
2
FCGGAJmmAGw}}tTl3r&   c                       e Zd Zd ZddZy)EstimatorReturnTuplec                     || _         y r   OutputTuple)rz   r   s     r#   __init__zEstimatorReturnTuple.__init__  s
    &r&   Nc                 ,    | j                  |d|z        S )Nr   r   ry   s      r#   rj   zEstimatorReturnTuple.transform  s    1q5))r&   r   )rs   rt   ru   r   rj   r    r&   r#   r   r     s    '*r&   r   c                      t        dd      } t        j                  g dg      }t        |       }|j	                  |      }t        ||       sJ t        |j                  |       t        |j                  d|z         y)z+Check that namedtuples are kept by default.OutputzX, Yr   r   r   N)	r   r<   r=   r   rj   rB   r   rm   Y)r   rm   r   r   s       r#   test_set_output_named_tuple_outr     sf    &)F


I;A
6
2CmmAGgv&&&wyy!$wyy!a%(r&   c                   $    e Zd ZddZddZddZy)EstimatorWithListInputNc                 P    t        |t              sJ t        |d         | _        | S )Nr   )rB   rI   lenr   ry   s      r#   r   zEstimatorWithListInput.fit  s'    !T"""!!A$ir&   c                     |S r   r    ry   s      r#   rj   z EstimatorWithListInput.transform  r|   r&   c                     t        j                  t        | j                        D cg c]  }d| 	 c}t              S c c}w r   r   r   s      r#   r   z,EstimatorWithListInput.get_feature_names_out  r   r   r   r   r    r&   r#   r   r     s    
Wr&   r   c                    t        j                  |       }g dg dg}t               }|j                  |        |j	                  |      j                  |      }t        ||j                        sJ t        |j                  g d       y)zJCheck set_output for list input.

    Non-regression test for #27037.
    )r   r   r   r   )r.   r,   r/      r   )X0X1X2X3N)
r:   r;   r   r   r   rj   rB   r?   r   r"   )r   r   rm   r   X_outs        r#   test_set_output_list_inputr    so     

m
,C	|$A
 
"CNN]N+GGAJ  #EeS]]+++u}}&>?r&   namec                 J    t        t        j                  |    t              sJ y)z*Check adapters have the correct interface.N)rB   r   r@   r	   )r  s    r#    test_adapter_class_has_interfacer    s!     &//57OPPPr&   c                     t         j                  fd}| j                  t         d|       d}t        j                  t
        |      5  t        d       ddd       y# 1 sw Y   yxY w)zCheck import error changed.c                 8    | dk(  r
t                | d        y )Nr   )package)ImportError)r  orig_import_modules    r#   patched_import_modulez;test_check_library_installed.<locals>.patched_import_module  s    8-4.r&   import_modulez-Setting output container to 'pandas' requiresrh   r   N)	importlibr  setattrr:   rk   r  r   )monkeypatchr  r   r  s      @r#   test_check_library_installedr    sY    "00/
 	?4IJ
9C	{#	. *)* * *s   A  A))6r  collectionsr   numpyr<   r:   numpy.testingr   sklearn._configr   r   sklearn.preprocessingr   sklearn.utils._set_outputr   r	   r
   r   r   r   r   sklearn.utils.fixesr   rW   rc   markparametrizero   rq   rw   r   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#   <module>r$     s    "   , 6 0   /<0~30l .9O :O	 	 
	W_ 	W(6 
*	 8X*>?1 @1(	 +h-AB/ C/@d 
!0 0&&.W/ W4*? *	)
W_ 
W 8X*>?@ @@  (8(A(A!BCQ DQ
*r&   