
    h$,fZ*                     @   d dl mZ d dlZd dlZd dlmZ d dl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mZmZmZmZ d dlmZmZ ej4                  j7                  d      Zej4                  j;                  d	 ej<                  g d
      g d
g      d        Zd Z ed        Z!ed        Z" G d de      Z#d Z$ej4                  j;                  dddg      d        Z%d Z&ej4                  j;                  d e             ej4                  j;                  dddg dddfg dddfg d dd!fg d dd"fg      d#               Z'eej4                  j;                  d$g d%      ej4                  j;                  d&d'd(ejP                  ged'fd'd)ejP                   ged)fejR                  ejR                  geejR                  fg d
ejP                  ejP                  ejP                  gg d*g eed +      g d,fg d
ejP                  ejP                  ejP                  gg d*g eed'+      d-ejP                  d.gfd'd(ejP                  ged(fd'd(ejP                  ged(fejR                   ejR                   geejR                   fg d
ejP                  ejP                  ejP                  gg d*g eed +      g d/fg d
ejP                  ejP                  ejP                  gg d*g eed'+      d"ejP                  d0gfg
      d1                      Z*eej4                  j;                  d$g d2      d3               Z+d4 Z, G d5 d6e	      Z-eej4                  j;                  d7d8d9 fdd: fd;d< fg      d=               Z.ed>        Z/d? Z0ej4                  j;                  d@eeg      dA        Z1y)B    )partialN)assert_allclose)config_context)BaseEstimator)_ArrayAPIWrapper_asarray_with_order_atol_for_type_convert_to_numpy _estimator_with_converted_arrays_nanmax_nanmin_NumPyAPIWrapper_weighted_sumget_namespacesupported_float_dtypes)yield_namespace_device_dtype_combinations)_array_api_for_tests'skip_if_array_api_compat_not_configuredz0ignore:The numpy.array_api submodule:UserWarningX         c                 L    t        |       \  }}t        |t              sJ |rJ y)z.Check that get_namespace returns NumPy wrapperN)r   
isinstancer   )r   xp_outis_array_api_compliants      Blib/python3.12/site-packages/sklearn/utils/tests/test_array_api.py"test_get_namespace_ndarray_defaultr   !   s1     &31%5"F"f.///%%%%    c                     t        j                  g d      } t        |       \  }}|j                  ddd      }t	        |dgdz         t        j                  t        d      5  |j                  dd	       d
d
d
       y
# 1 sw Y   y
xY w)z;Check expected behavior with device and creation functions.r   
          @cpu)
fill_valuedevicezUnsupported devicematchcudar&   N)	numpyasarrayr   fullr   pytestraises
ValueErrorzeros)r   r   _
full_arrays       r   *test_get_namespace_ndarray_creation_devicer4   )   sv    i Aa IFARC>JJ
+	z)=	> (R'( ( (s   %BBc                      t        j                  d      } t        j                  g dg      }t	        d      5  t        |      \  }}|sJ || j                  u sJ 	 ddd       y# 1 sw Y   yxY w)z%Test get_namespace on NumPy ndarrays.array_api_compatr   Tarray_api_dispatchN)r.   importorskipr+   r,   r   r   )r6   X_npr   r   s       r   (test_get_namespace_ndarray_with_dispatchr;   5   sq     **+=>==)%D	4	0 0)6t)<&&%%%)/////0 0 0s   #A''A0c                     t        j                  d      } t        j                  g dg      }| j                  |      }t	        d      5  t        |      \  }}|sJ t        |t              sJ t        j                  t              5  t        ||      \  }}ddd       ddd       y# 1 sw Y   xY w# 1 sw Y   yxY w)z'Test get_namespace for ArrayAPI arrays.numpy.array_apir   Tr7   N)
r.   r9   r+   r,   r   r   r   r   r/   	TypeError)xpr:   X_xpr   r   s        r   test_get_namespace_array_apirA   B   s     
		.	/B==)%D::dD	4	0 G)6t)<&&%%%&"2333]]9% 	G-:4-F*F*	GG G
	G 	GG Gs$   >B6	B*B6*B3	/B66B?c                   "     e Zd ZdZ fdZ xZS )_AdjustableNameAPITestWrapperz:API wrapper that has an adjustable name. Used for testing.c                 4    t         |   |       || _        y )N)array_namespace)super__init____name__)selfrE   name	__class__s      r   rG   z&_AdjustableNameAPITestWrapper.__init__U   s    9r    )rH   
__module____qualname____doc__rG   __classcell__)rK   s   @r   rC   rC   R   s    D r    rC   c                     t        j                  d      } t        | d      }t        |      }|j	                  g dg dg|j
                        }|j                  ||j                        }|j                  |j                  k(  sJ |j	                  ||j                        }|j                  |j                  k(  sJ y)z6Test _ArrayAPIWrapper for ArrayAPIs that is not NumPy.r=   zwrapped_numpy.array_apir   r         dtypeN)	r.   r9   rC   r   r,   float64astypefloat32rU   )numpy_array_apixp_r?   r   X_converteds        r   test_array_api_wrapper_astyper\   Z   s    ))*;<O
'9R
SC	#	B


Y	*2::
>A))Arzz*K

*****Qbjj*1K

***r    	array_apir+   r=   c                     t        j                  |       }|j                  g d      }t        |d|      }t	        j                  |      }|j
                  d   sJ y)z=Test _asarray_with_order passes along order for NumPy arrays.333333?333333@gffffff@Forderr?   F_CONTIGUOUSN)r.   r9   r,   r   r+   flags)r]   r?   r   X_newX_new_nps        r   test_asarray_with_orderri   h   sP     
		Y	'B


?#A4E}}U#H>>.)))r    c                  ,   t        j                  d      } t        | d      }t        j                  g dg dgd      }|j	                  |      }t        |d|      }t        j                  |      }|j                  d	   sJ |j                  d
   rJ y)z<Test _asarray_with_order ignores order for Generic ArrayAPI.r=   zwrapped.array_apir_   )ra         @r`   C)rd   rb   rc   C_CONTIGUOUSre   N)r.   r9   rC   r+   r,   r   rf   )r?   rZ   r   rg   rh   s        r   test_asarray_with_order_ignoredrn   t   s    			.	/B
',?
@C8DAAA5E}}U#H>>.)))~~n----r    z#array_namespace, device, dtype_namez"sample_weight, normalize, expected)NFg      $@)NTg      @)皙?ro   g      ?gffffff?Frk   Tg      @r   r   r   rR   g      >@      @c                 z   t        | |      }t        j                  g d|      }|j                  ||      }|*t        j                  ||      }|j                  ||      }t        d      5  t	        |||      }d d d        t        t              sJ t        ||t        |             y # 1 sw Y   3xY w)Nrp   rT   r*   Tr7   )atol)	r   r+   r,   r   r   r   floatr   r	   )	rE   r&   
dtype_namesample_weight	normalizeexpectedr?   sample_scoreresults	            r   test_weighted_sumr{      s    " 
ov	6B==Z@L::l6::L m:F

=
@	4	0 G|]IFG fe$$$FH>*+EF	G Gs   1B11B:library)r+   r=   cupycupy.array_apitorchzX,reduction,expectedr   r   )rR   rS         @)axis      ?r#   rq   r         @)r   g      @r   r   c                     t        j                  |       }t        |t              r|j	                  |      }t        d      5   ||j	                  |            }ddd       t        |       y# 1 sw Y   xY w)z-Check NaN reductions like _nanmin and _nanmaxTr7   N)r.   r9   r   listr,   r   r   )r|   r   	reductionrx   r?   rz   s         r   test_nan_reductionsr      sh    J 
		W	%B(D!::h'	4	0 *2::a=)* FH%* *s   A00A9)r}   r   r~   c                 \   t        j                  |       }| dk(  rO|j                  j                  j	                         st        j
                  d       |j                  g dd      }n|j                  g d      }t        ||      }t        j                  g d      }t        ||       y)z0Check convert_to_numpy for GPU backed libraries.r   ztest requires cudar   r)   r*   r?   N)
r.   r9   backendsr)   is_builtskipr,   r
   r+   r   )r|   r?   X_gpuX_cpuexpected_outputs        r   test_convert_to_numpy_gpur      s     
		W	%B'{{((*KK,-

?6
:

?+e+EmmO4OE?+r    c                      t        j                  d      } | j                  g dd      }t        ||       }t	        j                  g d      }t        ||       y)z.Check convert_to_numpy for PyTorch CPU arrays.r   r   r$   r*   r   N)r.   r9   r,   r
   r+   r   )r   X_torchr   r   s       r   test_convert_to_numpy_cpur      sH    (EmmOEm:Gg%0EmmO4OE?+r    c                       e Zd ZddZy)SimpleEstimatorNc                 <    || _         |j                  d   | _        | S )Nr   )X_shapen_features_)rI   r   ys      r   fitzSimpleEstimator.fit   s    771:r    N)rH   rL   rM   r    r    r   r   r      s    r    r   zarray_namespace, converterr   c                 >    | j                         j                         S r   )r$   r+   arrays    r   <lambda>r      s    		 1 1 3 r    c                 ,    t        j                  |       S r   )r+   r,   r   s    r   r   r      s    %--*> r    r~   c                 6    | j                   j                         S r   )_arraygetr   s    r   r   r      s    )9)9); r    c                     t        j                  |       }|j                  ddgg      }t               j	                  |      }t        ||      }t        |j                  t        j                        sJ y)z(Convert estimator attributes to ndarray.?      @N)
r.   r9   r,   r   r   r   r   r   r+   ndarray)rE   	converterr?   r   estnew_ests         r   !test_convert_estimator_to_ndarrayr      s_     
		_	-B


S#J< A




"C.sI>Ggjj%--000r    c                      t        j                  d      t        j                  ddgg      } t	               j                  |       }t        |fd      }t        |j                  d      sJ y)z0Convert estimator attributes to ArrayAPI arrays.r=   r   r   c                 &    j                  |       S r   )r,   )r   r?   s    r   r   z5test_convert_estimator_to_array_api.<locals>.<lambda>  s    "**UBS r    __array_namespace__N)	r.   r9   r+   r,   r   r   r   hasattrr   )r:   r   r   r?   s      @r   #test_convert_estimator_to_array_apir   	  s_     
		.	/B==3*&D




%C.s4STG7::4555r    c                  n   t               } | j                  g dg dg      }| j                  |dd      }|j                  |u sJ | j                  |dd      }|j                  |j                  usJ t	        j
                  t        d	      5  | j                  |d
       ddd       y# 1 sw Y   yxY w)zDCheck reshape behavior with copy and is strict with non-tuple shape.r   rQ   )F)copy)   r   Tzshape must be a tupler'   r   N)r   r,   reshapebaser.   r/   r>   )r?   r   	X_no_copyX_copys       r   test_reshape_behaviorr     s    		B


Iy)*A

1e%
0I>>QZZ6Z-F;;aff$$$	y(?	@ 


1b  s   B++B4wrapperc                 t   | t         k(  r!t        j                  d      }t        |      }n
t               }|j	                  |j
                  |j
                        sJ |j	                  |j
                  d      sJ |j	                  |j                  d      sJ |j	                  |j                  d      rJ t        |      D ]  }|j	                  |d      rJ  |j	                  |j                  d      sJ |j	                  |j
                  d      rJ |j	                  |j                  d      sJ |j	                  |j                  d      rJ |j	                  |j                  d      sJ |j	                  |j                  d      rJ |j	                  |j                  d      sJ |j	                  |j
                  d      sJ |j	                  |j                  d      sJ |j	                  |j
                  d      rJ | t        k(  rZ|j	                  |j                  d      rJ |j	                  |j                  d      sJ |j	                  |j                   d      sJ t        j"                  t$        d	      5  |j	                  |j                  d
      sJ 	 ddd       y# 1 sw Y   yxY w)zGTest isdtype implementation from _ArrayAPIWrapper and _NumPyAPIWrapper.r=   zreal floatingboolzsigned integerzunsigned integernumericzcomplex floatingzUnrecognized data typer'   unknownN)r   r.   r9   r   isdtyperX   rV   int32r   r   int16uint32uint16int64int8	complex64
complex128r/   r0   )r   rZ   r?   rU   s       r   $test_get_namespace_array_api_isdtyper   $  sC    ""!!"34c"::bjj"**---::bjj/222::bjj/222zz"((O444'+ 2zz%1112 ::bggv&&&zz"**f---::bhh 0111zz"))%5666::bii!3444zz"(($6777::bhh	***::bjj),,,::bii+++zz"**&8999""::bgg'9:::zz",,(:;;;zz"--);<<<	z)A	B /zz"((I.../ / /s   J..J7)2	functoolsr   r+   r.   numpy.testingr   sklearn._configr   sklearn.baser   sklearn.utils._array_apir   r   r	   r
   r   r   r   r   r   r   r   r   sklearn.utils._testingr   r   markfilterwarnings
pytestmarkparametrizer,   r   r4   r;   rA   rC   r\   ri   rn   r{   naninfr   r   r   r   r   r   r   r   r   r    r   <module>r      s      ) * &   
 [[''6

 }u}}Y7CD& E&	( )	0 )	0 )G )G$4 + w0A&BC* D*. )+T+V (	uc*	tT*	ud#	tS!
G
G" )N 
Q			GQ'
R%))	gr*
))UYY	%))4EIIuyy9;GG!$	
 EIIuyy9;GG!$%))S!	

 Q			GQ'
Q			GQ'99*uyyj	!7UYYJ7EIIuyy9;GG!$	
 EIIuyy9;GG!$%))S!	
->
&? )F
& )$GH, I ), ,m  ) 	34	>?	;<1 )1 )6 )6 %57G$HI&/ J&/r    