U
    -e%                  	   @   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   c/var/www/html/Darija-Ai-Train/env/lib/python3.8/site-packages/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
ValueErrorZ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    

r0   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__)selfr2   name	__class__r   r   r4   J   s    z&_AdjustableNameAPITestWrapper.__init__)r5   
__module____qualname____doc__r4   __classcell__r   r   r8   r   r1   G   s   r1   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*   r1   r   r!   float64Zastypefloat32rB   r   )numpy_array_apixp_r/   r   ZX_convertedr   r   r   test_array_api_wrapper_astypeO   s    

 rG   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?   r@   rA   r   Zaxis__array_namespace__N)r#   r*   r1   r   r!   rC   takehasattrr   r   r    )rE   rF   r/   r   X_taker   r   r   )test_array_api_wrapper_take_for_numpy_api]   s    

 rM   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   rA   r   rH   rI   r?   r@   z"Only axis in \(0, 1\) is supportedr   z$Only X.ndim in \(1, 2\) is supportedN)r#   r*   r1   r   r!   rC   rJ   rK   r   r   r    r$   r%   )rE   rF   r/   ZX_1drL   r   r   r   r   test_array_api_wrapper_takej   s$    

 "rN   	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orderr/   F_CONTIGUOUSN)r#   r*   r!   r   r    flagsr   )rO   r/   r   X_newX_new_npr   r   r   test_asarray_with_order   s
    

rZ   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_apirP   rQ   rR   g      @C)rU   rS   rT   ZC_CONTIGUOUSrV   N)r#   r*   r1   r    r!   r   rW   r   )r/   rF   r   rX   rY   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   r/   N)r#   r*   Zhas_cudaskipr!   r   r    r   )r]   r/   ZX_gpuX_cpuexpected_outputr   r   r   test_convert_to_numpy_gpu   s    

re   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   r`   r   r   ra   N)r#   r*   r!   r   r    r   )r^   ZX_torchrc   rd   r   r   r   test_convert_to_numpy_cpu   s
    
rf   c                   @   s   e Zd ZdddZdS )SimpleEstimatorNc                 C   s   || _ |jd | _| S )Nr   )X_shapeZn_features_)r6   r   yr   r   r   fit   s    zSimpleEstimator.fit)N)r5   r:   r;   rk   r   r   r   r   rg      s   rg   zarray_namespace, converterc                 C   s   |    S N)r   r    arrayr   r   r   <lambda>       ro   c                 C   s
   t | S rl   )r    r!   rm   r   r   r   ro      rp   c                 C   s
   | j  S rl   )Z_arraygetrm   r   r   r   ro      rp   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!   rg   rk   r	   r   rh   r    Zndarrayr   )r2   	converterr/   r   estnew_estr   r   r   !test_convert_estimator_to_ndarray   s
    

rw   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-   rr   rs   c                    s
     | S rl   )r!   rm   ra   r   r   ro      rp   z5test_convert_estimator_to_array_api.<locals>.<lambda>rI   N)
r#   r*   r    r!   rg   rk   r	   rK   rh   r   )r+   ru   rv   r   ra   r   #test_convert_estimator_to_array_api   s
    
rx   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?   r@   )F)copy)   r   Tzshape must be a tupler   ry   N)r
   r!   Zreshapebaser   r#   r$   r.   )r/   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isdtyperD   r   rC   Zint32r   Zint16Zuint32Zuint16Zint64Zint8Z	complex64Z
complex128r$   r%   )r~   rF   r/   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,   r0   r1   rG   rM   rN   rZ   r\   re   rf   rg   rw   rx   r}   r   r   r   r   r   <module>   sT    "








