U
    /eH<                     @   s  d dl Z d dlZd dlmZ 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 ed dd	 Zd
d Zdd Zdd dd dd dd dd dd dd dd dd dd dd dd dd dd dd dd dd d d d!d d"d d#d d$d d%d d&d d'd d(d d)d gZejd*ed+d, Zd-d. Zejd*eejd/d0d1 Zejd*eejd/d2d3 Zd4d5 Zd6d7 Zd8d9 Z d:d; Z!ejd<d=ejd>d?d@dAdBdCdDdEdFdGg	dHdI Z"ejd<d=ejd>d?d@dAdBdCdDdEdFdGg	dJdK Z#ejd>dLdMgdNdO Z$dPdQ Z%dRdS Z&dTdU Z'dVdW Z(ejdXdYdZgd[d\ Z)d]d^ Z*d_d` Z+ejdadbdcddgdedf Z,dgdh Z-didj Z.dS )k    N)deepcopy)product)
_numpy_123)	assert_eq)tokenizezdask.array.mac                  C   s   t jjdddgdddgdd} t jjdddgdddgdd}t jjdddgddd}t| t| kshtt|t|ks|tt|t|kstt| t|kstt| t|kstd S )	N         TF
   mask
fill_valuer   )npmamasked_arrayr   AssertionError)mm2Zm3 r   @/tmp/pip-unpacked-wheel-dbjnr7gq/dask/array/tests/test_masked.pytest_tokenize_masked_array   s    r   c                  C   s<   t jjdddgdddgdd} tj| ddd	}t||  d S )
Nr   r   r	   TFr
   r   )r   chunksasarray)r   r   r   da
from_arrayr   )r   dmr   r   r   test_from_array_masked_array   s    r   c                  C   s   t jjddgddgd} tj| | jdd}| }i }t||d}tjddgddg}t	|| t	||  t
| t jjstt	||  t
| t jjstd S )	Nr   r   r   r   Fr   )memoT)r   r   r   r   r   shapecopyr   masked_wherer   
isinstancecomputer   )txyr   y2xxr   r   r   test_copy_deepcopy"   s    


r*   c                 C   s   | S Nr   r&   r   r   r   <lambda>4       r-   c                 C   s
   t | S r+   )r   expm1r,   r   r   r   r-   5   r.   c                 C   s   d|  S Nr   r   r,   r   r   r   r-   6   r.   c                 C   s   | d S r0   r   r,   r   r   r   r-   7   r.   c                 C   s   | d S r0   r   r,   r   r   r   r-   8   r.   c                 C   s   | |  S r+   r   r,   r   r   r   r-   9   r.   c                 C   s   | |  S r+   r   r,   r   r   r   r-   :   r.   c                 C   s   | d S )Nr   r   r,   r   r   r   r-   ;   r.   c                 C   s   | d d df S Nr   r   r,   r   r   r   r-   <   r.   c                 C   s   | d dd ddf S )Nr   r	   r   r,   r   r   r   r-   =   r.   c                 C   s   | j S r+   )Tr,   r   r   r   r-   >   r.   c                 C   s   t | dS )N)r   r   r   )r   Z	transposer,   r   r   r   r-   ?   r.   c                 C   s   |   S r+   sumr,   r   r   r   r-   @   r.   c                 C   s   |  t| jd S N)dotr   aranger    r,   r   r   r   r-   A   r.   c                 C   s   |  t| jd S r5   )r7   r   Zeyer    r,   r   r   r   r-   B   r.   c                 C   s$   t j| t| jd d ddgdS )Nr   )r   r   Zaxes)r   	tensordotr   Zonesr    r,   r   r   r   r-   C   r.   c                 C   s   | j ddS Nr   axisr3   r,   r   r   r   r-   D   r.   c                 C   s   | j ddS r;   )maxr,   r   r   r   r-   E   r.   c                 C   s   | j ddS )Nr   r   r<   r3   r,   r   r   r   r-   F   r.   c                 C   s   |  tjS r+   )astyper   Z
complex128r,   r   r   r   r-   G   r.   c                 C   s   |  dd S )Nc                 S   s   | d S r0   r   r,   r   r   r   r-   H   r.   z<lambda>.<locals>.<lambda>)
map_blocksr,   r   r   r   r-   H   r.   c                 C   s
   |  dS r1   )roundr,   r   r   r   r-   I   r.   c                 C   s$   |  | jd | jd  | jd fS )Nr   r   r   )reshaper    r,   r   r   r   r-   J   r.   c                 C   s   t | S r+   )absr,   r   r   r   r-   K   r.   c                 C   s   | dkS )N      ?r   r,   r   r   r   r-   L   r.   c                 C   s
   |  dS )N)   rF   rF   Zrechunkr,   r   r   r   r-   M   r.   c                 C   s
   |  dS )Nr   r   r   rG   r,   r   r   r   r-   N   r.   funcc                 C   sr   t jjddd}d||dk < t j|d}| |}| |}t|t j|d |jrn| }t|t	jj
sntd S )Nr   r	   rF   r   r   r   r   r   皙?)r   randomr   masked_equalr   filledr    r$   r#   r   r   r   )rI   r&   r'   r)   yyzzr   r   r   
test_basicR   s    rS   c               	   C   s   t jjddd} d| | dk < t jjddd}d||dk < t j| d}t j|d}tt j| |dd	t jt j||dd	d tt j| |d
d	t jt j||d
d	d tt j| |dd	t jt j||dd	d d S )NrJ   rK   rL   r   rM   )rF   r	   r   rH   )r   r   r9   r   r   )r?   )r   r   )r   rN   r   rO   r   r:   rP   )r&   r'   r)   rQ   r   r   r   test_tensordotc   s$    rU   zignore::numpy.ComplexWarningc                 C   s   t jjddd}t jjddd}d||dk < t j|d}t j||gdd}t j||gdd}| |}| |}t||ddd d S )	NrJ   rK   rL   r   rM   r<   FZ
check_metaZ
check_type)r   rN   r   rO   concatenater   )rI   r&   r'   rQ   dsddssr   r   r   test_mixed_concatenatez   s    r\   c                 C   sR   t jjddd}d||dk < dd }||}| |}| |}t||ddd	 d S )
N)rF   r	   rF   rK   rL   r   rM   c                 S   s   t   dk rtj| dS | S )NrE   r   )rN   r   r   rO   r,   r   r   r   r-      r.   z#test_mixed_random.<locals>.<lambda>FrV   )r   rN   rA   r   )rI   rX   fnrY   rZ   r[   r   r   r   test_mixed_random   s    
r^   c                  C   sv   t jjddd} d| | dk < t j| d} t jddd}t j|| gdd	}|jd
ksXt| }t	|t
jjsrtd S )Nr
   r
      ra   rL   r   rM   )r
   r   )ra   r   r   r<   )r
      )r   rN   r   rO   zerosrW   r    r   r$   r#   r   r   )r'   r&   zrR   r   r   r   test_mixed_output_type   s    re   c            	   	   C   s  t dddddgd d} t dddddgd }tj| dd	}tj|d
d	}t j| |}t|| g||gD ]\}}ttj||| qrttj|dt j| d ttj	|dt j	| d ttj
|dt j
| d ttj|dt j| d ttj|dt j| d ttj|dt j| d ttjd|t jd|  ttj|dk|t j| dk|  tt$ tj|dkd d df | W 5 Q R X ttj|ddt j| dd ttj|ddt j| dd ttj|dt j| d tt tj|| W 5 Q R X tt tj|| W 5 Q R X | d}t j |d< |d< tj|dd	}ttj|t j| t j|d}tj|d}ttjj|ddt jj|dd d S )Nr6   r   r   r      r_   ra   rL   rF   Ff8r   r   )   ra   r   )r   arrayrC   r   r   r   masked_greaterr   r   Zmasked_greater_equalZmasked_lessZmasked_less_equalrO   Zmasked_not_equalr"   pytestraises
IndexErrorZmasked_insideZmasked_outsideZmasked_values
ValueErrorr@   nanZmasked_invalidZfix_invalid)	r&   r'   dxZdysolabZmyZdmyr   r   r   test_creation_functions   s>    &(""
rw   c                  C   s   t dddddgd d} tj| dd	}t j| d}tj|d}ttj|t j| ttj|d
t j|d
 d S )Nrf   r6   r   r   r   rg   r_   ra   rL   )	r   rl   rC   r   r   r   rO   r   rP   )r&   rs   mxmdxr   r   r   test_filled   s    r{   c                 C   s|   |   }|tjjkr"||ksxtnVt|t|ks6tt|drjtj|j	|j	 t
j| } tj|}t| |dd d S )Nr   T)Z	equal_nan)r$   r   r   maskedr   typehasattrZtestingZassert_equalr   r   rP   r   )ru   rv   resr   r   r   assert_eq_ma   s    
r   dtype)i8rh   	reductionr4   prodZmeanvarZstdminr>   anyallc                 C   s   t jdddd | }tj|dd}t j|d}tj|d}t	t|}t	t |}t
|||| t
||dd||dd t
||d	d
d||d	d t
||ddd||dd t
||dd	dd||dd	d t
||ddd||dd t
||dd	dd||dd	d d S )N*   rb   r
   rF   rF   rL   ra   r   r<   TrF   )keepdimssplit_everyr   r   )r=   r   )r=   r   r   )r=   r   r   )r   rN   RandomStaterandr@   r   r   r   rm   getattrr   )r   r   r&   rs   ry   rz   dfuncrI   r   r   r   test_reductions   s&    

r   c                 C   sL   t jjddgdd}tj|dd}tt|}tt |}t|||| d S )Nr   r   Tr   F)r   )r   r   r   r   r   r   r   )r   r   r&   rs   r   rI   r   r   r   test_reductions_allmasked  s
    

r   ZargminZargmaxc                 C   s   t jd}tj|dd}t j|d}tj|d}tt| }tt | }t|||| t||d||d t||d||d t||d||d d S )N)r
   r
   r
   )r	   rF   ra   rL   rM   r   r   r   )r   rN   r   r   r   rm   r   r   )r   r&   rs   ry   dmxr   rI   r   r   r   test_arg_reductions  s    

r   c                  C   s   t jdddd} tj| dd}t j| d}tj|d}dD ]8}t|j	|d	|j	|d	 t|j
|d	|j
|d	 qDd S )
Nr   rg         )   ra   rF   rL   rM   )r   r   r   r<   )r   rN   r   r   r   r   r   rm   r   ZcumsumZcumprod)r&   rs   ry   r   r=   r   r   r   test_cumulative+  s    r   c                  C   s   t jd} tj| dd}t j| d}tj|d}ttj|t j| ttj|t j|  ttj|t j| ttj|t j|  d S )Nr_   r	   rF   rL   rM   )	r   rN   r   r   r   rm   r   getmaskarrayZgetdatar&   rs   ry   r   r   r   r   test_accessors6  s    r   c               	   C   sf  t jdd} tj| dd}tjt ddd}ddd|fg}t| |g|D ]\}\}}ttjj	||d	t jj	| |d	 ttjj	||d
k|dt jj	| | d
k|d ttjj	||d
k|dt jj	| | d
k|d ttjj	||ddt jj	| |dd qPt
t tjj	||d	 W 5 Q R X t
t jj& tjj	||d dd df d W 5 Q R X d S )Nr_   Zf4r   rL   r   r   )NN)rE   rE   rk   rM   r   rh   )r   r   r	   r   )r   rN   r@   r   r   rl   r   r   r   r   rn   ro   rq   Z	MaskError)r&   rs   f1Zfill_valuesdataZdffr   r   r   test_masked_arrayB  s2     r   c               	   C   s   t jddd} tj| dd}t j| d}tj|d}tj|d t j|d t|| tj|d t j| d t||  t	
t tj|d W 5 Q R X t	
t tj|| W 5 Q R X d S )	Nr   r
   r_   r   rL   r	   ig@xD)r   rN   randintr   r   r   rm   Zset_fill_valuer   rn   ro   	TypeErrorrq   r   r   r   r   test_set_fill_value`  s    

r   r   FTc                 C   s   t ddgddgddgg}t dd}t jj||d}tjj||dd}t dd	g}tj|dd
}tjj||d| d}t	rt
|t jj||d| d n| st
|t jj||dd d S )NTFr   r	   r   r   r   r   r   r   g      ?g      ?rL   r   )weightsr=   r   )r   r=   )r   rl   r8   rC   r   r   r   r   Zaverager   r   )r   r   r   ru   d_ar   Z	d_weightsZda_avgr   r   r   &test_average_weights_with_masked_arrayu  s    r   c                  C   s~   t ddgddgddgg} t dd}t jj|| d}tj|dd}|| }|| }t|| t|	 t jj
sztd S )NTFr   r   r   )r   r   rL   )r   rl   r8   rC   r   r   r   r   r#   r$   r   r   )r   r&   r|   rs   r   rt   r   r   r   !test_arithmetic_results_in_masked  s    
r   c                  C   s6  t dd} | d dk| d dkB }t j|| }tj|dd}dD ].}tjj||d	}t jj||d	}t|| qHtjj|d
d}t jj|d
d}t|| t j	d}tj|dd}ttj|t j| t dd}tj| dd}dD ]8}tjj||d	}t jj||d	}t||t
jdkd qd S )Nx   )   r
   r	   r   rF   )r   r	   rL   )Nr   r   r<   Tr   win32)Zcheck_dtype)r   r8   rC   r   r"   r   r   countr   Z
masked_allsysplatform)r   r   r&   rs   r=   r   rt   r   r   r   
test_count  s(    
r   funcnameZ	ones_likeZ
zeros_likeZ
empty_likec           	      C   s   t ddgddgddgg}t dd}t jj||d}tjj||dd}ttj| }tt jj| }||}||}d| krt	tj
|t j
| n
t	|| d S )	NTFr   r   r   r   r   empty)r   rl   r8   rC   r   r   r   r   corer   r   )	r   r   r   ru   r   Zda_funcZnp_funcr   rt   r   r   r   test_like_funcs  s    r   c            	      C   s   t dd} t dddgdddgdddgg}t jj| |d}tjj| |dd}|dk|dkf||f|d	k|d	kf|d
k|d
kffD ]j\}}t j|}tj|}t|t	|st
t|t|kst
tt|D ]}t|| ||  qqd S )N	   )r	   r	   TFr   r   r   rF   rf   r   )r   r8   rC   rl   r   r   r   Znonzeror#   r}   r   lenranger   )	r   r   ru   r   c1c2rt   r   ir   r   r   test_nonzero  s    "r   c               
   C   sv  t jjddd} t jjddgdddgd}d | d	< | d
< tjj| |dd}t jj| |d} t jjdddt j	}tj
|dd}t j| }tj|}tt|D ]}t|| ||  q|dk| dkf|| fdd	dt jt jfddt jt jff	D ]|\}}	d|d d d f  |d d d f  f|d d |d d ffD ]4\}
}tj|||
}t j|	| |}t|| q:qd S )Nr
   )      )sizeFTrE   )ru   r   pr   r`   r   )rF   ra   )r   r   r   r   )rF   rL   ra   rT   )TTri   )FFr   )r   rN   r   choicer   r   r   rl   r@   Zuint8r   wherer   r   r   ZTrue_ZFalse_)r&   r   rX   r'   ert   r   r   r   r   Zb1Zb2Zw1Zw2r   r   r   
test_where  s2    

Fr   )/rN   r   r!   r   	itertoolsr   Znumpyr   rn   Z
dask.arrayrl   r   Zdask.array.numpy_compatr   Zdask.array.utilsr   Z	dask.baser   Zimportorskipr   r   r*   Z	functionsmarkZparametrizerS   rU   filterwarningsr\   r^   re   rw   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s   



2  



