B
    T\                 @   s  d dl mZmZmZ d dl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mZ d dlZd dlmZmZmZ ddlmZ dd	lmZ dd
lm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% ddlm&Z&m'Z' ddl(m)Z) ddl*m+Z+m,Z, ddlm-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9 ddl:m;Z; e
ej<dddZ<e
ej=dd Z=e
ej>dd Z>e
ej?dd Z?e
ej@dd Z@e
ejAdd d!ZAe
ejBdd"d#ZBe
ejCdd$d%ZCe
ejDd&d' ZDe
ejEdd(d)ZEd*d+ ZFe
ejGd,d- ZGe
ejHd.d/ ZHd0ZIeIJ ZKd1d2 ZLe
ejMdd3d4ZMe
ejNd5d6 ZNe
ejOd7d8 ZOe&ejPd9d: ZPe
ejQd;d< ZQd=d> ZRe
ejSd?d@ ZSe
ejTdAdB ZTe
ejUddCdDZUe
ejVddFdGZVe
ejWddHdIZWdJdK ZXe
ejYdLdM ZYe
ejZddNdOZZe
ej[ddPdQZ[ddRdSZ\e
ej]ddTdUZ]e
ej^ddVdWZ^e
ej_ddXdYZ_ddZd[Z`e
ejadd\d]Zadd^d_Zbe&eced`dddad`Zde
ejeddbdcZee
ejfddde Zfe
ejgddfdgZge
ejhddhdiZhe
ejidjdk Zie
ejjddldmZjdndo Zke
ejlddpdqZldrds Zmdtdu Zndvdw Zoe
ejpddzd{Zpe
ejqdd|d}Zqd~d Zre
ejsdd Zsdd ZtejuetevgdZtdd Zwe
ejxdd Zxe
ejydddZye
ejzdddZze
ej{dd Z{e
ej|dd Z|dd Z}dd Z~e
ejdddZe
ejdd Ze
e jdddZdddZe
ejdd ZdddZe
ejdddZdS )    )divisionprint_functionabsolute_importN)LooseVersion)wrapspartial)RealIntegral)concatsliding_window
interleave   )Iterable)flatten)tokenize)HighLevelGraph)funcname   )chunk)arangediagemptyindices)
safe_wrapsvalidate_axis)ones)multiplysqrt)Array
map_blockselemwise
from_arrayasarray
asanyarrayconcatenatestack	blockwisebroadcast_shapesis_scalar_for_elemwisebroadcast_totensordot_lookup)einsumc             C   sH   x&|d k	r&| j |k r&| d d d f } qW |d k	rD| j|krD| |} | S )N)ndimdtypeastype)xr-   ndmin r1   2lib/python3.7/site-packages/dask/array/routines.pyarray   s
    
r3   c              G   s   dd | D } t j|  S )Nc             S   s   g | ]}t |r|n|jqS r1   )r(   r-   ).0ar1   r1   r2   
<listcomp>*   s    zresult_type.<locals>.<listcomp>)npresult_type)argsr1   r1   r2   r8   (   s    r8   c              G   s   g }xp| D ]h}t |}|jdkr*|d }n>|jdkrH|d d d d f }n |jdkrh|d d d d d f }|| q
W t|dkr|d S |S d S )Nr   )NNNr   r   )r#   r,   appendlen)arysnew_arysr/   r1   r1   r2   
atleast_3d.   s    




r>   c              G   sn   g }xL| D ]D}t |}|jdkr*|d }n|jdkrD|d d d f }|| q
W t|dkrf|d S |S d S )Nr   )NNr   )r#   r,   r:   r;   )r<   r=   r/   r1   r1   r2   
atleast_2dB   s    



r?   c              G   sR   g }x0| D ](}t |}|jdkr(|d  }|| q
W t|dkrJ|d S |S d S )Nr   r   )r#   r,   r:   r;   )r<   r=   r/   r1   r1   r2   
atleast_1dT   s    

r@   Fc             C   s    t dd | D } t| d|dS )Nc             s   s   | ]}t |V  qd S )N)r?   )r4   r/   r1   r1   r2   	<genexpr>f   s    zvstack.<locals>.<genexpr>r   )axisallow_unknown_chunksizes)tupler$   )tuprC   r1   r1   r2   vstackd   s    rF   c             C   s2   t dd | D r t| d|dS t| d|dS d S )Nc             s   s   | ]}|j d kV  qdS )r   N)r,   )r4   r/   r1   r1   r2   rA   l   s    zhstack.<locals>.<genexpr>r   )rB   rC   r   )allr$   )rE   rC   r1   r1   r2   hstackj   s    rH   c             C   s    t dd | D } t| d|dS )Nc             s   s   | ]}t |V  qd S )N)r>   )r4   r/   r1   r1   r2   rA   t   s    zdstack.<locals>.<genexpr>r   )rB   rC   )rD   r$   )rE   rC   r1   r1   r2   dstackr   s    rI   c          	   C   sr   ||kr| S |dk r|| j  }|dk r0|| j  }tt| j }t|}|| ||< ||< ttj|| |||| jdS )Nr   )axis1axis2r-   )r,   listranger&   r7   swapaxesr-   )r5   rJ   rK   indoutr1   r1   r2   rN   x   s    

rN   c                sl   |rt | jkr4tdntt jd d d }t fdd|D }ttj| tt j j|dS )Nzaxes don't match arrayc             3   s$   | ]}|d k r| j  n|V  qdS )r   N)r,   )r4   d)r5   r1   r2   rA      s    ztranspose.<locals>.<genexpr>)r-   axes)	r;   r,   
ValueErrorrD   rM   r&   r7   	transposer-   )r5   rS   r1   )r5   r2   rU      s    
rU   c             C   sl   t | } | jtdg }ytddd||< W n. tk
rZ   tdt|t| jf Y nX t|}| | S )z
    Reverse element order along axis.

    Parameters
    ----------
    axis : int
        Axis to reverse element order of.

    Returns
    -------
    reversed array : ndarray
    NrQ   z#`axis` of %s invalid for %s-D array)r#   r,   slice
IndexErrorrT   strrD   )mrB   slr1   r1   r2   flip   s    r[   c             C   s
   t | dS )Nr   )r[   )rY   r1   r1   r2   flipud   s    r\   c             C   s
   t | dS )Nr   )r[   )rY   r1   r1   r2   fliplr   s    r]   Zabcdefghijklmnopqrstuvwxyzc                s  t  gdd d}tt|}t fddd D }tfddd D }t|dkr||k r| dkrt	t
fd	dt jD fd
dtjD  }n| d}td d g|j }x td D ] | d  qW |t
| }|S )Nc             S   s   | j S )N)Z__array_priority__)r/   r1   r1   r2   <lambda>   s    z_tensordot.<locals>.<lambda>)keyc                s   g | ]} j | qS r1   )shape)r4   i)r5   r1   r2   r6      s    z_tensordot.<locals>.<listcomp>r   c                s   g | ]} j | qS r1   )r`   )r4   ra   )br1   r2   r6      s    r   c                s    g | ]\}}| d  kr|qS )r   r1   )r4   ra   s)rS   r1   r2   r6      s    c                s    g | ]\}}| d  kr|qS )r   r1   )r4   ra   rc   )rS   r1   r2   r6      s    )rS   )maxr*   dispatchtyper7   r3   r;   rG   minzerosrD   	enumerater`   rV   r,   sortedinsert)r5   rb   rS   r/   	tensordotZa_dimsZb_dimsrO   r1   )r5   rS   rb   r2   
_tensordot   s    $ rm   c          
   C   s"  t |tr|\}}n.tt| jd | j| d d}ttd|}t |trR|f}t |trb|f}t |trtt|}t |trt|}t| j	|j	}tt| j}tt| j| j|j }|| }x0t
||D ]"\}	}
|||
  ||	 ||
< qW tt|| ||||||fd}|j|d}|S )Nr   rQ   r   )r-   rS   )rB   )
isinstancer   rD   rM   r,   r	   rL   r7   Zpromote_typesr-   zipremover&   rm   sum)ZlhsZrhsrS   Z	left_axesZ
right_axesdtZ
left_indexZright_indexZ	out_indexlrZintermediateresultr1   r1   r2   rl      s2    

 



rl   c             C   s"   t | || jd f|jd ffdS )Nr   r   )rS   )rl   r,   )r5   rb   r1   r1   r2   dot   s    rv   c             C   s   t |   | S )N)rv   conjravel)r5   rb   r1   r1   r2   vdot   s    ry   c             C   sP  t | } t |}| jdks$|jdkr,tdd}| jdkrPd}| tjd d f } d}|jdkrtd}|d d tjf }| j|jk r| |j| j tjf  } n$| j|jkr|| j|j tjf  }ttjttd| jd | ttd| jd | jd df |ttd| jd d| jf t	| |dd}|r>|ddd d f }|rL|d }|S )	Nr   z"`matmul` does not support scalars.Fr   T)r-   r$   .).r   )
r#   r,   rT   r7   newaxisr&   matmulrD   rM   r8   )r5   rb   Za_is_1dZb_is_1drP   r1   r1   r2   r{      s6    

"r{   c          	   C   sB   |   } |  }t| j |j j}ttjd| d|d|dS )NZijra   j)r-   )r   r7   outerr-   rf   r&   )r5   rb   r-   r1   r1   r2   r}   &  s    r}   c             C   s   t j||| f||S )N)r7   apply_along_axis)arrfunc1dfunc1d_axisfunc1d_argsfunc1d_kwargsr1   r1   r2   _inner_apply_along_axis0  s    r   c             O   s  t |}|j|  t|jd | }tjd|jd}t| |f||}ttjtdk rt|jdkj	t
ddkrtd||jd | |j||d  f |j|d d   }|jtt| d |j|jd | |j |j|d d   |tt|||j d| |||d
}|S )N)r   )r-   z1.13.0r   zdNo more than one non-trivial dimension allowed in result. Need NumPy 1.13.0+ for this functionality.z-along-axis)	namer-   chunksZ	drop_axisnew_axisr   r   r   r   )r"   r`   r;   r7   r   r-   r3   r   __version__rq   intrT   rechunkr   r   r   r   rL   rM   r,   )r   rB   r   r9   kwargsZ	test_dataZtest_resultru   r1   r1   r2   r~   :  s.    
6
$r~   c             C   s   t |}yt|}W n tk
r.   |f}Y nX |jtd f }|}xT|D ]L}t| ||d}|j|jd kr||d | d  }qJ|j|jkrJtdqJW |S )Nr   r   )NzJfunc must either preserve dimensionality of the input or reduce it by one.)r"   rD   	TypeErrorr,   rV   r~   rT   )funcr5   rS   rZ   ru   ra   r1   r1   r2   apply_over_axesd  s    

r   c             C   s   | j |d| j|d S )N)rB   )rd   rg   )r5   rB   r1   r1   r2   ptp  s    r   rQ   c             C   s   t | } t|}t|}| jtd g }| jtd g }tdd ||< td d||< t|}t|}| }x t|D ]}|| ||  }qrW |S )Nr   rQ   )r"   r   r,   rV   rD   rM   )r5   nrB   Zsl_1Zsl_2rt   ra   r1   r1   r2   diff  s    r   c             C   sn   t | } |  }|dd  |d d  }|g}|d k	rHt | g| }|d k	rb|t | g }t|}|S )Nr   rQ   )r"   r   r$   )ZaryZto_endZto_beginZaryfrt   r1   r1   r2   ediff1d  s    r   c             C   sH   || }|dk	r,||d | |d |  }t j| |fd|i|}|S )aU  
    x: nd-array
        array of one block
    coord: 1d-array or scalar
        coordinate along which the gradient is computed.
    axis: int
        axis along which the gradient is computed
    array_locs:
        actual location along axis. None if coordinate is scalar
    grad_kwargs:
        keyword to be passed to np.gradient
    Nr   r   rB   )r7   gradient)r/   Zblock_idcoordrB   
array_locsgrad_kwargsZ	block_locZgradr1   r1   r2   _gradient_kernel  s
    r   c                sZ  t t|dd|d< |d dkr2tdd}|dd }|d krZttj}nt	|t
rnd}|f}t|j}t|tt|krtdtfd	d
|D }|dkrd}t|dkrt|| }t|t|krtdtjjtjt
fr
tn(tjjtr2jjdk r2tg }xt|D ] \} xBj  D ]4}t||d d k rXtdt| qXW t|| rd }nbt	|| trtdtj  }	t |	d }
|
|	 d }|
d  d8  < d|d< ||
f}|!j"t#j fddtjD d||  ||d qBW |rV|d }|S )NZ
edge_orderr   r   z+edge_order must be less than or equal to 2.FrB   Tzduplicate axes not allowedc             3   s   | ]}| j  V  qd S )N)r,   )r4   ax)fr1   r2   rA     s    zgradient.<locals>.<genexpr>r1   )r   zGSpacing must either be a single scalar, or a scalar / 1d-array per axis   zcChunk size must be larger than edge_order + 1. Minimum chunk for aixs {} is {}. Rechunk to proceed.z(dask array coordinated is not supported.rQ   r   c                s   i | ]}| krd nd|qS )r   r   r1   )r4   r|   )r   r1   r2   
<dictcomp>   s    zgradient.<locals>.<dictcomp>Znone)r-   Zdepthboundaryr   rB   r   r   )$r"   mathZceilgetrT   poprD   rM   r,   rn   r	   r   r;   setr   
issubclassr-   rf   r7   Zbool8r.   floatr   itemsizeri   r   rg   formatisscalarr   NotImplementedErrorr3   cumsumr:   Zmap_overlapr   )r   Zvarargsr   Zdrop_result_listrB   Zresultsra   cr   r   Zarray_loc_stopZarray_loc_startr1   )r   r   r2   r     sp    

r   c                s
   d krt djdkstd k	r6jjks6tt }d| d k	r fddt D }tjdgdgdj	}n* fddt D }tg j	}d| tj
t|d	f|d	f<  ff}tj|d krgngd
}t|||S )Nz.Must specify minlength argument in da.bincountr   z	bincount-c                s0   i | ](\}}t jj|fj|f f|fqS r1   )r7   bincountr   )r4   ra   _)	minlengthr   weightsr/   r1   r2   r     s   zbincount.<locals>.<dictcomp>)r   c                s*   i | ]"\}}t jj|fd  f|fqS )N)r7   r   r   )r4   ra   r   )r   r   r/   r1   r2   r     s   zbincount-sum-r   )dependencies)r   r,   AssertionErrorr   r   ri   __dask_keys__r7   r   r-   rq   rL   r   from_collectionsr   )r/   r   r   tokendskr-   r   graphr1   )r   r   r   r/   r2   r     s&    "r   c             C   s2   t |}t jdg|ddj}| jt j|||dS )Nr   F)right)r-   binsr   )r7   r"   digitizer-   r   )r5   r   r   r-   r1   r1   r2   r   -  s    
r   c                s  t  s"dks dkr"td|dk	r>|j| jkr>tdt  s }\}}||krl|d8 }|d7 }t j|| d dd n }t| ||||}	ttt| 	 }
d|
 t d ff}d	|	 d fd
d	|dkrfddt
t| 	 D }t g d j}n>t| 	 }t|	 }fddt
t||D }|j}tj||dkrn| gn| |gd}t|||d}|jdd}|dk	r|rtt  t|jd}|| |   fS | fS n<|rtt  t|jd}|||    fS | fS dS )aq  
    Blocked variant of :func:`numpy.histogram`.

    Follows the signature of :func:`numpy.histogram` exactly with the following
    exceptions:

    - Either an iterable specifying the ``bins`` or the number of ``bins``
      and a ``range`` argument is required as computing ``min`` and ``max``
      over blocked arrays is an expensive operation that must be performed
      explicitly.

    - ``weights`` must be a dask.array.Array with the same block structure
      as ``a``.

    Examples
    --------
    Using number of bins and range:

    >>> import dask.array as da
    >>> import numpy as np
    >>> x = da.from_array(np.arange(10000), chunks=10)
    >>> h, bins = da.histogram(x, bins=10, range=[0, 10000])
    >>> bins
    array([    0.,  1000.,  2000.,  3000.,  4000.,  5000.,  6000.,  7000.,
            8000.,  9000., 10000.])
    >>> h.compute()
    array([1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000])

    Explicitly specifying the bins:

    >>> h, bins = da.histogram(x, bins=np.array([0, 5000, 10000]))
    >>> bins
    array([    0,  5000, 10000])
    >>> h.compute()
    array([5000, 5000])
    Nzudask.array.histogram requires either specifying bins as an iterable or specifying both a range and the number of binsz<Input array and weights must have the same chunked structureg      ?r   T)Zendpoint)r   zhistogram-sum-c                s   t j|  ||dd t j S )N)rM   r   r   )r7   	histogramrz   )r/   rM   r   )r   r1   r2   
block_histt  s    zhistogram.<locals>.block_histc                s"   i | ]\}} |f|d fqS )r   r1   )r4   ra   k)r   r   rM   r1   r2   r   x  s   zhistogram.<locals>.<dictcomp>r   c                s(   i | ] \}\}} ||f|d fqS )r   r1   )r4   ra   r   w)r   r   rM   r1   r2   r   ~  s   )r   )r-   )rB   )r   )NN)r7   iterablerT   r   Zlinspacer   r;   rL   r   r   ri   r   r-   ro   r   r   r   rq   r!   r   r.   r   )r5   r   rM   Znormedr   ZdensityZ	bin_tokenZmnZmxr   Znchunksr   r   r-   Za_keysZw_keysr   mappedr   Zdbr1   )r   r   r   rM   r2   r   4  sL    %

$

r   c       
      C   sH  |d k	r|t |krtdt| } |d kr<t| tj}nt|}t| |tj}t| d|d}|jd dkrtd}|r|jd }d}n|jd }d}|d kr|dkrd}nd}t|| }	|	dkrt	
dt d}	|d k	rt|d|d}t||f|}||jd| dd	 }|s,t|j| |	  S t||j |	  S d S )
Nzddof must be integerr   )r0   r-   r   r   z!Degrees of freedom <= 0 for sliceg        T)rB   keepdims)r   rT   r"   r7   r8   Zfloat64r3   r`   r   warningswarnRuntimeWarningr$   meanrv   Trw   squeeze)
rY   yrowvarZbiasZddofr-   XNrB   Zfactr1   r1   r2   cov  s>    

r   c             C   sP   t | ||}|jdkr|| S t|}||jd df}t|}|| |j S )Nr1   r   r   )r   r`   r   reshaper   r   )r/   r   r   r   rR   Zsqr_dr1   r1   r2   corrcoef  s    
r   c             C   s   | j tj|| jdS )N)decimalsr-   )r   r7   roundr-   )r5   r   r1   r1   r2   r     s    r   c             C   s  |dk	}|dk	}t | }d|jfg}|r:|dt jf |rN|dt jf |rb|dt jf t j|j|d}||d< |rt jt|t jd|d< |s|rxlt	|d D ]\\}	}
| |
k}|r|| j
d|d |	|	d  d	 |r|| jd|d |	|	d  d	 qW |S )
a  
    Helper/wrapper function for :func:`numpy.unique`.

    Uses :func:`numpy.unique` to find the unique values for the array chunk.
    Given this chunk may not represent the whole array, also take the
    ``indices`` and ``counts`` that are in 1-to-1 correspondence to ``ar``
    and reduce them in the same fashion as ``ar`` is reduced. Namely sum
    any counts that correspond to the same value and take the smallest
    index that corresponds to the same value.

    To handle the inverse mapping from the unique values to the original
    array, simply return a NumPy array created with ``arange`` with enough
    values to correspond 1-to-1 to the unique values. While there is more
    work needed to be done to create the full inverse mapping for the
    original array, this provides enough information to generate the
    inverse mapping in Dask.

    Given Dask likes to have one array returned from functions like
    ``blockwise``, some formatting is done to stuff all of the resulting arrays
    into one big NumPy structured array. Dask is then able to handle this
    object and can split it apart into the separate results on the Dask side,
    which then can be passed back to this function in concatenated chunks for
    further reduction or can be return to the user to perform other forms of
    analysis.

    By handling the problem in this way, it does not matter where a chunk
    is in a larger array or how big it is. The chunk can still be computed
    on the same way. Also it does not matter if the chunk is the result of
    other chunks being run through this function multiple times. The end
    result will still be just as accurate using this strategy.
    Nvaluesr   inversecounts)r-   Tr   )r   rP   )r7   uniquer-   r:   intpr   r`   r   r;   ri   rg   rq   )arr   r   return_inversereturn_indexreturn_countsurr   rt   ra   vrY   r1   r1   r2   _unique_internal  s,    !

"&r   c             C   s~  |   } | dg}d| jfg}|rX|t| jd tj| jd ddg |dtjf n|d d g |r|t	| jd ftj| jd ddg |dtjf n|d d g t
tdf||dd}td	d
 |jD |_|d g}|r||d  n
|d  |r||d  n
|d  d|j }|dftftdd
 |D  |f i}	d| jfg}|rz|dtjf |r|dtjf |r|dtjf dd |D }
tj||	|
d}tjff}t||||}|d g}|r||d  |rF| d d d f |d d d d f ktj}|||d  jdd |rZ||d  t|dkrr|d }nt|}|S )Nra   r   r   )r-   r   r   r   F)r-   r   c             s   s   | ]}t jft| V  qd S )N)r7   nanr;   )r4   r   r1   r1   r2   rA   5  s    zunique.<locals>.<genexpr>zunique-aggregate-c             s   s*   | ]"}t |d rtj| fn|V  qdS )r   N)hasattrr7   r$   r   )r4   or1   r1   r2   rA   O  s   r   c             S   s   g | ]}t |d r|qS )r   )r   )r4   r   r1   r1   r2   r6   ^  s    zunique.<locals>.<listcomp>)r   r   )rB   )rx   r-   extendr   r`   r7   r   r   r:   r   r&   r   rD   Z_chunksr   r   r   r   r   r.   rq   r;   )r   r   r   r   r9   Z	out_dtyperP   Z	out_partsr   r   r   r   r   ru   Zmtchesr1   r1   r2   r     sn    







,
r   c             C   s*   t j|  ||d}|| jd|j  S )N)assume_unique)r   )r7   Zin1drx   r   r`   r,   )elementtest_elementsr   r   r1   r1   r2   _isin_kernel|  s    r   isinc                s|   t   t |}tt j}t fddt|jD }tt||  |||dd |D t|d	}|j|d}|rx| }|S )Nc             3   s   | ]}| j  V  qd S )N)r,   )r4   ra   )r   r1   r2   rA     s    zisin.<locals>.<genexpr>c             S   s   i | ]}d d |qS )c             S   s   dS )Nr   r1   )r   r1   r1   r2   r^     s    z!isin.<locals>.<dictcomp>.<lambda>r1   )r4   rB   r1   r1   r2   r     s    zisin.<locals>.<dictcomp>)Zadjust_chunksr-   r   )rB   )r"   rD   rM   r,   r&   r   boolany)r   r   r   invertZelement_axesZ	test_axesr   ru   r1   )r   r2   r     s$    c             C   s:  | }|d kr2t |}t|ts&td|f}d}nPyt| W n tk
rX   |f}Y nX yt| W n tk
r   |f}Y nX t|t|krtdxt||D ]\}}| }||j| ; }|jt	d g }|jt	d g }t	|d ||< t	d |||< t
|}t
|}t|| || g|d}qW || j}|S )NzAExpect `shift` to be an instance of Integral when `axis` is None.)r   z,Must have the same number of shifts as axes.)rB   )rx   rn   r	   r   r;   rT   ro   r`   r,   rV   rD   r$   r   )r3   shiftrB   ru   ra   rc   Zsl1Zsl2r1   r1   r2   roll  s<    
r   c             C   s
   |  dS )N)rQ   )r   )r3   r1   r1   r2   rx     s    rx   c                s   d kr"t dd t jD ntt s2ft fddD rPtdt jt fddt jD } | S )Nc             s   s   | ]\}}|d kr|V  qdS )r   Nr1   )r4   ra   rR   r1   r1   r2   rA     s    zsqueeze.<locals>.<genexpr>c             3   s   | ]} j | d kV  qdS )r   N)r`   )r4   ra   )r5   r1   r2   rA     s    z,cannot squeeze axis with size other than onec             3   s&   | ]\}}| krd nt dV  qdS )r   N)rV   )r4   ra   rc   )rB   r1   r2   rA     s    )rD   ri   r`   rn   r   rT   r   r,   )r5   rB   rZ   r1   )r5   rB   r2   r     s    
r   c                s   t tt |}jdkr(td d kr<| }d t |j |t fddt|jD  }|t fddt|jD  }|S )Nr   z!Condition must be one dimensionalr   c             3   s,   | ]$}| krt d tnt d V  qd S )N)rV   r;   )r4   ra   )rB   	conditionr1   r2   rA     s   zcompress.<locals>.<genexpr>c             3   s"   | ]}| krnt d V  qd S )N)rV   )r4   ra   )rB   r   r1   r2   rA     s   )	r"   r.   r   r,   rT   rx   r   rD   rM   )r   r5   rB   r1   )rB   r   r2   compress  s    
r   c             C   s(   t | t} t |}t|  | S )N)r"   r.   r   r   rx   )r   r   r1   r1   r2   extract  s    r   c             C   sJ   t || j}t| tjr.t|tr.t| ||S | td f| |f  S d S )N)r   r,   rn   r7   ndarrayr   _take_dask_array_from_numpyrV   )r5   r   rB   r1   r1   r2   take  s    r   c                s<   t  tjstt |tst|j fdd|j jdS )Nc                s   t  | S )N)r7   r   )block)r5   rB   r1   r2   r^     s    z-_take_dask_array_from_numpy.<locals>.<lambda>)r   r-   )rn   r7   r   r   r   r   r   r-   )r5   r   rB   r1   )r5   rB   r2   r     s
    r   c             C   s   t ttj|d| | jdS )N)r   )r-   )r   r   r7   aroundr-   )r/   r   r1   r1   r2   r     s    r   c             C   s   dd l }t|| S )Nr   )pandasr7   r"   isnull)r   pdr1   r1   r2   _asarray_isnull  s    r   c             C   s   ddl }tt| ddS )z pandas.isnull for dask arrays r   Nr   )r-   )r   r    r   )r   r   r1   r1   r2   r     s    r   c             C   s
   t |  S )z  pandas.notnull for dask arrays )r   )r   r1   r1   r2   notnull%  s    r   h㈵>:0yE>c             C   s"   t tj|||d}t|| |ddS )N)rtolatol	equal_nanr   )r-   )r   r7   iscloser    )arr1arr2r   r   r   r   r1   r1   r2   r   *  s    r   c             C   s   t | ||||d S )N)r   r   r   )r   rG   )r   r   r   r   r   r1   r1   r2   allclose0  s    r   c             G   s   t | |S )N)r7   choose)r5   choicesr1   r1   r2   variadic_choose5  s    r   c             C   s   t t| f| S )N)r    r   )r5   r   r1   r1   r2   r   9  s    r   c             C   s   t t| S )N)r   r7   count_nonzero)r   r1   r1   r2   _isnonzero_vec>  s    r  )Zotypesc             C   sJ   yt jt | jdt W n tk
r:   | jttdS X | tS d S )N)r-   )	r7   rh   rD   r-   r.   r   rT   r   r  )r5   r1   r1   r2   	isnonzeroE  s
    r  c                sf   t | } t|  }t| jtj| jd  jdkrTt	 fddt
t D dd t| dd  S )N)r-   r   r   c                s   g | ]} |   qS r1   )rx   )r4   ra   )rO   r1   r2   r6   \  s    zargwhere.<locals>.<listcomp>)rB   r   )r"   r  r   r   r`   r7   r   r   r,   r%   rM   r;   r   )r5   Znzr1   )rO   r2   argwhereT  s    
"r  c             C   s   |d k|d kkrt d|d kr0|d kr0t| S t| r~t||}t|}t|}t|j|j}| rj|n|}t||	|S t
tj| ||S d S )Nz1either both or neither of x and y should be given)rT   nonzeror7   r   r8   r"   r'   r`   r)   r.   r    where)r   r/   r   r-   r`   rP   r1   r1   r2   r  b  s    

r  c             C   s   t t| tjj|dS )N)rB   )r  r"   r.   r7   r   rq   )r5   rB   r1   r1   r2   r   v  s    r   c             C   s   t t|  d d df S )Nr   )r  r"   rx   )r5   r1   r1   r2   flatnonzero{  s    r  c                s<   t |   jdkr2t fddt jd D S  fS d S )Nr   c             3   s   | ]} d d |f V  qd S )Nr1   )r4   ra   )rO   r1   r2   rA     s    znonzero.<locals>.<genexpr>)r  r,   rD   rM   r`   )r5   r1   )rO   r2   r    s    
 r  c             O   s&   t j| t||d f|d |d S )Nfunclist	func_argsfunc_kw)r7   	piecewiserL   )r/   condlistr   r1   r1   r2   _int_piecewise  s    r  c             C   s   t t j| f|S )N)r7   r%   unravel_index)r   func_kwargsr1   r1   r2   _unravel_index_kernel  s    r  Cc          	   C   sP   |r:| j r:t| jttjt|ff| j d||dd}ntdd |D }|S )Nr   )dimsorder)r-   r   r   r  c             s   s   | ]}t d tjddV  qdS ))r   r   )r-   r   N)r   r7   r   )r4   ra   r1   r1   r2   rA     s    z unravel_index.<locals>.<genexpr>)sizerD   r   r  r7   r   r;   r   )r   r  r  Zunraveled_indicesr1   r1   r2   r    s    
r  c             O   s    t t| f|| jd|||dS )Nr
  )r-   r   r  r  r	  )r   r  r-   )r/   r  r  r9   kwr1   r1   r2   r
    s    r
  c       	         s   s*t fdd  D s*d}t|dtkrDttjdt   fddt	
 D }t fddtjD }tjd	j jd
j}tj|gd}t|||d
S )Nc             3   s.   | ]&\}} j | D ]}|| d kV  qqdS )r   N)r   )r4   ra   Zdivbd)r/   r1   r2   rA     s    zcoarsen.<locals>.<genexpr>z6Coarsening factor does not align with block dimensionsZdaskzcoarsen-c                s,   i | ]$}t j| ff|d d  qS )r   N)r   coarsen)r4   r_   )rS   r   	reductiontrim_excessr1   r2   r     s   zcoarsen.<locals>.<dictcomp>c             3   s*   | ]"\ }t  fd d|D V  qdS )c             3   s"   | ]}t | d  V  qdS )r   N)r   r   )r4   r  )rS   ra   r1   r2   rA     s    z$coarsen.<locals>.<genexpr>.<genexpr>N)rD   )r4   Zbds)rS   )ra   r2   rA     s   )r   )r-   )r   )rG   itemsrT   inspectZgetfilegetattrr7   __name__r   r   r   rD   ri   r   r   r,   r-   r   r   r   )	r  r/   rS   r  msgr   r   rr   r   r1   )rS   r   r  r  r/   r2   r    s    r  c                sL   t dg|dgg}dd td|D }tdf|  fdd|D }|S )z Split an array into a list of arrays (using slices) at the given breaks

    >>> split_at_breaks(np.arange(6), [3, 5])
    [array([0, 1, 2]), array([3, 4]), array([5])]
    Nc             S   s   g | ]\}}t ||qS r1   )rV   )r4   ra   r|   r1   r1   r2   r6     s    z#split_at_breaks.<locals>.<listcomp>r   c                s   g | ]} |f  qS r1   r1   )r4   rc   )r3   preslicer1   r2   r6     s    )r
   r   rV   )r3   ZbreaksrB   Zpadded_breaksZslicesZsplit_arrayr1   )r3   r  r2   split_at_breaks  s
    r  c                s  t  | j ttr,tj| j   tjdk}|rNt	t
dk | j   tdk  rtdt| t }t|dddkrdt| }t|dt|}t|f|i|d|d}t fdd	t| jD }t||}n|r |td f  d
  }t fdd	tt| j|jD }	||	}td d }
t|
|
dk }t|| }tt||g}dd |D }t| dS )Nr   z9da.insert only implemented for monotonic ``obj`` argumentr,   zvalues-r-   r1   )r   r-   c             3   s&   | ]\}} |krt n|V  qd S )N)r;   )r4   r   rc   )rB   objr1   r2   rA     s   zinsert.<locals>.<genexpr>)Nc             3   s&   | ]\}\}} |kr|n|V  qd S )Nr1   )r4   r   Zarr_bdZ	values_bd)rB   r1   r2   rA     s   rQ   c             S   s   g | ]}|j r|qS r1   )nbytes)r4   ra   r1   r1   r2   r6     s    zinsert.<locals>.<listcomp>)rB   )r   r,   rn   rV   r7   r   r   r`   r"   r@   r  r   r   r   r  r   r  r   rf   r   rD   ri   r)   ro   r   r   r   r   rL   r   r$   )r   r   r   rB   Z
scalar_objZ	split_arrr   r-   Zvalues_shapeZvalues_chunksr   Zvalues_breaksZsplit_valuesZinterleavedr1   )rB   r   r2   rk     s>    




rk   c       
      C   sV  t | } |d kr0| |}|j| j|j }nt |}t| jjtjtjfr`t	| j|jd}nt	| j|j}| j
|j
kr|d krtd|jdkrtd|j
d | j
| krtdt|| jd d |j
 }|d|}|rdd	lm}	 ||	|   }|j||d
}t| ||d|| }|rN|j
|j
krFt||j
 }||fS |S d S )NZf8z;Axis must be specified when shapes of a and weights differ.r   z81D weights expected when shapes of a and weights differ.r   z5Length of weights not compatible with specified axis.)r   rQ   )getmaskarray)rB   r-   )r-   )r#   r   r-   rf   r  r   r7   ZintegerZbool_r8   r`   r   r,   rT   r)   rN   Zmar"  rq   r   copy)
r5   rB   r   returned	is_maskedZavgZsclZwgtZresult_dtyper"  r1   r1   r2   _average  s<    

r&  c             C   s   t | |||ddS )NF)r%  )r&  )r5   rB   r   r$  r1   r1   r2   average3  s    r'  )NN)F)F)F)N)r   )N)r   rQ   )NN)NN)F)NNFNN)Nr   r   N)Nr   )r   )F)FFF)F)FF)N)N)N)r   )r   )r   r   F)r   r   F)NN)N)r  )F)r   )NNFF)NNF)Z
__future__r   r   r   r  r   r   Zdistutils.versionr   	functoolsr   r   Znumbersr   r	   Znumpyr7   Ztoolzr
   r   r   Zcompatibilityr   Zcorer   baser   Zhighlevelgraphr   Zutilsr    r   Zcreationr   r   r   r   r   r   Zwrapr   Zufuncr   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   Zeinsumfuncsr+   r3   r8   r>   r?   r@   rF   rH   rI   rN   rU   r[   r\   r]   ZalphabetupperZALPHABETrm   rl   rv   ry   r{   r}   r   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r   r   rx   r   r   r   r   r   r   r   r   r   r   r   r   r   r  Z	vectorizer   r  r  r  r   r  r  r  r  r  r
  r  r  rk   r&  r'  r1   r1   r1   r2   <module>   s   <#'

*N
c0

=f
/			
/
0