U
    Ovf%                  	   @   s  d dl Z d dlZd dlmZ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 d dlmZ ejdZejde d	d
dgd	d
dggdd Zdd Zedd Zedd ZG dd de
Zdd Zdd Zdd Zejdddgdd  Zd!d" Z eejd#d$d%d&gd'd( Z!d)d* Z"G d+d, d,eZ#eejd-d%d.d/ fdd0d/ fd&d1d/ fgd2d3 Z$ed4d5 Z%d6d7 Z&ejd8e
egd9d: Z'dS );    N)assert_allcloseassert_array_equal)config_context)BaseEstimator)_ArrayAPIWrapper_asarray_with_order_convert_to_numpy _estimator_with_converted_arrays_NumPyAPIWrapperget_namespace)'skip_if_array_api_compat_not_configuredz0ignore:The numpy.array_api submodule:UserWarningX         c                 C   s&   t | \}}t|tst|r"tdS )z.Check that get_namespace returns NumPy wrapperN)r   
isinstancer
   AssertionError)r   xp_outis_array_api_compliant r   F/tmp/pip-unpacked-wheel-qu3nn_q2/sklearn/utils/tests/test_array_api.py"test_get_namespace_ndarray_default   s    r   c               	   C   sh   t dddg} t| \}}|jdddd}t|dgd  tjtdd	 |jdd
d W 5 Q R X dS )z;Check expected behavior with device and creation functions.r   r   r   
          @cpu)Z
fill_valuedevicezUnsupported devicematchcudar   N)	numpyasarrayr   fullr   pytestraises
ValueErrorzeros)r   r   _Z
full_arrayr   r   r   *test_get_namespace_ndarray_creation_device   s    r(   c               	   C   sX   t d} tdddgg}tdd( t|\}}|s<t|| jksJtW 5 Q R X dS )z%Test get_namespace on NumPy ndarrays.array_api_compatr   r   r   TZarray_api_dispatchN)r#   importorskipr    r!   r   r   r   )r)   X_npr   r   r   r   r   (test_get_namespace_ndarray_with_dispatch*   s    
r-   c               
   C   s   t d} tdddgg}| |}tddL t|\}}|sFtt|tsTtt 	t
 t||\}}W 5 Q R X W 5 Q R X dS )z'Test get_namespace for ArrayAPI arrays.numpy.array_apir   r   r   Tr*   N)r#   r+   r    r!   r   r   r   r   r   r$   	TypeError)xpr,   ZX_xpr   r   r   r   r   test_get_namespace_array_api7   s    

r1   c                       s    e Zd ZdZ fddZ  ZS )_AdjustableNameAPITestWrapperz:API wrapper that has an adjustable name. Used for testing.c                    s   t  j|d || _d S )N)array_namespace)super__init____name__)selfr3   name	__class__r   r   r5   J   s    z&_AdjustableNameAPITestWrapper.__init__)r6   
__module____qualname____doc__r5   __classcell__r   r   r9   r   r2   G   s   r2   c                  C   s~   t d} t| d}t|}|jdddgdddgg|jd}|||j}|j|jksZt	|j||jd}|j|jkszt	d	S )
z6Test _ArrayAPIWrapper for ArrayAPIs that is not NumPy.r.   wrapped_numpy.array_apir   r   r         dtypeN)
r#   r+   r2   r   r!   float64Zastypefloat32rC   r   )numpy_array_apixp_r0   r   ZX_convertedr   r   r   test_array_api_wrapper_astypeO   s    

 rH   c                  C   s~   t d} t| d}t|}|jdddgdddgg|jd}|j||dgdd	}t|d
sbtt	|t
j|dgdd	 dS )z2Test that fast path is called for numpy.array_api.r.   r   r   r   r@   rA   rB   r   Zaxis__array_namespace__N)r#   r+   r2   r   r!   rD   takehasattrr   r   r    )rF   rG   r0   r   X_taker   r   r   )test_array_api_wrapper_take_for_numpy_api]   s    

 rN   c               	   C   s  t d} t| d}t|}|jdddg|jd}|j||dgdd}t|d	sXtt	|t
j|dgdd |jdddgdd
dgg|jd}|j||dgdd}t|d	stt	|t
j|dgdd |j||ddgdd}t|d	stt	|t
j|ddgdd t jtdd |j||dgdd W 5 Q R X t jtdd* |j|dggg|dgdd W 5 Q R X dS )z#Test _ArrayAPIWrapper API for take.r.   r?   r   r   r   rB   r   rI   rJ   r@   rA   z"Only axis in \(0, 1\) is supportedr   z$Only X.ndim in \(1, 2\) is supportedN)r#   r+   r2   r   r!   rD   rK   rL   r   r   r    r$   r%   )rF   rG   r0   ZX_1drM   r   r   r   r   test_array_api_wrapper_takej   s$    

 "rO   	array_apir    r.   c                 C   sD   t | }|dddg}t|d|d}t|}|jd s@tdS )z=Test _asarray_with_order passes along order for NumPy arrays.333333?333333@ffffff@Forderr0   F_CONTIGUOUSN)r#   r+   r!   r   r    flagsr   )rP   r0   r   X_newX_new_npr   r   r   test_asarray_with_order   s
    

r[   c                  C   st   t d} t| d}tjdddgdddggdd}||}t|d	|d
}t|}|jd sbt|jd rptdS )z<Test _asarray_with_order ignores order for Generic ArrayAPI.r.   zwrapped.array_apirQ   rR   rS   g      @C)rV   rT   rU   ZC_CONTIGUOUSrW   N)r#   r+   r2   r    r!   r   rX   r   )r0   rG   r   rY   rZ   r   r   r   test_asarray_with_order_ignored   s    



r]   libraryZcupytorchzcupy.array_apic                 C   sr   t | }| dkr8|js"t d |jdddgdd}n|dddg}t||d}tdddg}t|| d	S )
z0Check convert_to_numpy for GPU backed libraries.r_   ztest requires cuda      ?r         @r   r   r0   N)r#   r+   Zhas_cudaskipr!   r   r    r   )r^   r0   ZX_gpuX_cpuexpected_outputr   r   r   test_convert_to_numpy_gpu   s    

rf   c                  C   sH   t d} | jdddgdd}t|| d}tdddg}t|| dS )	z.Check convert_to_numpy for PyTorch CPU arrays.r_   r`   r   ra   r   r   rb   N)r#   r+   r!   r   r    r   )r_   ZX_torchrd   re   r   r   r   test_convert_to_numpy_cpu   s
    
rg   c                   @   s   e Zd ZdddZdS )SimpleEstimatorNc                 C   s   || _ |jd | _| S )Nr   )X_shapeZn_features_)r7   r   yr   r   r   fit   s    zSimpleEstimator.fit)N)r6   r;   r<   rl   r   r   r   r   rh      s   rh   zarray_namespace, converterc                 C   s   |    S N)r   r    arrayr   r   r   <lambda>       rp   c                 C   s
   t | S rm   )r    r!   rn   r   r   r   rp      rq   c                 C   s
   | j  S rm   )Z_arraygetrn   r   r   r   rp      rq   c                 C   sF   t | }|ddgg}t |}t||}t|jtj	sBt
dS )z(Convert estimator attributes to ndarray.?      @N)r#   r+   r!   rh   rl   r	   r   ri   r    Zndarrayr   )r3   	converterr0   r   estnew_estr   r   r   !test_convert_estimator_to_ndarray   s
    

rx   c                     sL   t d tddgg} t | }t| fdd}t|jdsHt	dS )z0Convert estimator attributes to ArrayAPI arrays.r.   rs   rt   c                    s
     | S rm   )r!   rn   rb   r   r   rp      rq   z5test_convert_estimator_to_array_api.<locals>.<lambda>rJ   N)
r#   r+   r    r!   rh   rl   r	   rL   ri   r   )r,   rv   rw   r   rb   r   #test_convert_estimator_to_array_api   s
    
ry   c               	   C   s   t  } | dddgdddgg}| j|ddd}|j|ks>t| j|d	d
d}|j|jk	s^ttjtdd | |d W 5 Q R X dS )zDCheck reshape behavior with copy and is strict with non-tuple shape.r   r   r   r@   rA   )F)copy)   r   Tzshape must be a tupler   rz   N)r
   r!   Zreshapebaser   r#   r$   r/   )r0   r   Z	X_no_copyZX_copyr   r   r   test_reshape_behavior   s    r~   wrapperc              	   C   s  | t krtd}t |}nt }||j|js6t||jdsHt||jdsZt||jdrlt||j	ds~t||jdrt||j
dst||jdrt||jdst||jdrt||jdst||jdst||jdst||jdr$t| tkrj||jdrBt||jdsVt||jdsjttjtdd	 ||j
d
stW 5 Q R X dS )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#   r+   r
   ZisdtyperE   r   rD   Zint32r   Zint16Zuint32Zuint16Zint64Zint8Z	complex64Z
complex128r$   r%   )r   rG   r0   r   r   r   $test_get_namespace_array_api_isdtype   s0    


r   )(r    r#   Znumpy.testingr   r   Zsklearn._configr   Zsklearn.baser   Zsklearn.utils._array_apir   r   r   r	   r
   r   Zsklearn.utils._testingr   markfilterwarningsZ
pytestmarkZparametrizer!   r   r(   r-   r1   r2   rH   rN   rO   r[   r]   rf   rg   rh   rx   ry   r~   r   r   r   r   r   <module>   sT    "








