U
    /e'                  
   @   s  d dl Z d dlZd dlmZ d dlmZ eejZ	e	edkZ
e	edkZe	edkZe	edkZe	edkZze  n eejd	d
ddejd	d
dedreejd
ddddreed	d
d	sedejZejjZW 5 Q R X W n< ek
r*   d$ddZejjeejj d d
ZY nX G dd dZeedd Zd%ddZe
rfejjjZnd&dddddZd'd d!Ze
rd d"l m!Z!m"Z" nd d#l#m$Z$ e$ Z!Z"dS )(    N)parse)derived_fromz1.20.0z1.21.0z1.22.0z1.23.0z1.24.0g?   Zunsafe)casting)r   dtypeg      ?i8)r      zIDivide not working with dtype: https://github.com/numpy/numpy/issues/3484c                 C   s$   t | ||}|dk	r ||}|S )zImplementation of numpy.divide that works with dtype kwarg.

        Temporary compatibility fix for a bug in numpy's version. See
        https://github.com/numpy/numpy/issues/3484 for the relevant issue.N)npdivideZastype)x1Zx2outr   x r   ;/tmp/pip-unpacked-wheel-dbjnr7gq/dask/array/numpy_compat.pyr
   %   s    
r
   c                   @   s>   e Zd ZdZdd Zdd dd dd fdd	ZdddZdS )	_Recurserz;
    Utility class for recursing over nested iterables
    c                 C   s
   || _ d S N
recurse_if)selfr   r   r   r   __init__=   s    z_Recurser.__init__c                 K   s   | S r   r   r   kwargsr   r   r   <lambda>C       z_Recurser.<lambda>c                 K   s   | S r   r   r   r   r   r   r   D   r   c                  K   s   | S r   r   )r   r   r   r   r   E   r   c                    s     fdd  |f|S )a{  
        Iterate over the nested list, applying:
        * ``f_map`` (T -> U) to items
        * ``f_reduce`` (Iterable[U] -> U) to mapped items

        For instance, ``map_reduce([[1, 2], 3, 4])`` is::

            f_reduce([
              f_reduce([
                f_map(1),
                f_map(2)
              ]),
              f_map(3),
              f_map(4)
            ]])


        State can be passed down through the calls with `f_kwargs`,
        to iterables of mapped items. When kwargs are passed, as in
        ``map_reduce([[1, 2], 3, 4], **kw)``, this becomes::

            kw1 = f_kwargs(**kw)
            kw2 = f_kwargs(**kw1)
            f_reduce([
              f_reduce([
                f_map(1), **kw2)
                f_map(2,  **kw2)
              ],      **kw1),
              f_map(3, **kw1),
              f_map(4, **kw1)
            ]],     **kw)
        c                    s@    | s| f|S f |  fdd| D f|S d S )Nc                 3   s   | ]} |fV  qd S r   r   ).0xi)fnext_kwargsr   r   	<genexpr>n   s     z2_Recurser.map_reduce.<locals>.f.<locals>.<genexpr>r   r   r   f_kwargsf_mapf_reducer   )r   r   r   i   s    

z_Recurser.map_reduce.<locals>.fr   )r   r   r!   r"   r    r   r   r   r   
map_reduce@   s    )z_Recurser.map_reducer   c                 c   sL   |  |}|||fV  |sdS t|D ] \}}| |||f E dH  q&dS )aB  
        Iterate over x, yielding (index, value, entering), where

        * ``index``: a tuple of indices up to this point
        * ``value``: equal to ``x[index[0]][...][index[-1]]``. On the first iteration, is
                     ``x`` itself
        * ``entering``: bool. The result of ``recurse_if(value)``
        N)r   	enumeratewalk)r   r   indexZ
do_recurseir   r   r   r   r%   r   s    	
z_Recurser.walkN)r   )__name__
__module____qualname____doc__r   r#   r%   r   r   r   r   r   4   s   
2r   c                    s   t jj | jd t jj|| jd}t t|kr@td fddt| jD }tt	| D ]\}}|
|| qf| |}|S )NsourcedestinationzJ`source` and `destination` arguments must have the same number of elementsc                    s   g | ]}| kr|qS r   r   )r   nr,   r   r   
<listcomp>   s      zmoveaxis.<locals>.<listcomp>)r	   corenumericnormalize_axis_tuplendimlen
ValueErrorrangesortedzipinsert	transpose)ar,   r-   orderdestsrcresultr   r/   r   moveaxis   s      
rA   c                 C   s   | j }tjj||}|dk r&||7 }d}d|  krB|d k s`n t|d| d|d |f ||k rp|d8 }||kr| d S ttd|}|| |	|| | 
|S )Nr   z5'%s' arg requires %d <= %s < %d, but %d was passed inr   start.)r4   r	   r1   r2   Znormalize_axis_indexr6   listr7   remover:   r;   )r<   axisrB   r.   msgZaxesr   r   r   rollaxis   s    
rG   F)subok	writeablec                   s`  ddl m} t|rt|n|f}tj d|d t|}t|dk rTtd|dkrtt j	}t
|t
|krtdt
| d j	 d	n>|| j	d
d}t
|t
|krtdt
| dt
| d jt fdd|D  }t j}t||D ]4\}	}
||	 |
k r$td||	  |
d 8  < qt|| }tjjj ||||dS )ac  
        Create a sliding window view into the array with the given window shape.
        Also known as rolling or moving window, the window slides across all
        dimensions of the array and extracts subsets of the array at all window
        positions.

        .. versionadded:: 1.20.0
        Parameters
        ----------
        x : array_like
            Array to create the sliding window view from.
        window_shape : int or tuple of int
            Size of window over each axis that takes part in the sliding window.
            If `axis` is not present, must have same length as the number of input
            array dimensions. Single integers `i` are treated as if they were the
            tuple `(i,)`.
        axis : int or tuple of int, optional
            Axis or axes along which the sliding window is applied.
            By default, the sliding window is applied to all axes and
            `window_shape[i]` will refer to axis `i` of `x`.
            If `axis` is given as a `tuple of int`, `window_shape[i]` will refer to
            the axis `axis[i]` of `x`.
            Single integers `i` are treated as if they were the tuple `(i,)`.
        subok : bool, optional
            If True, sub-classes will be passed-through, otherwise the returned
            array will be forced to be a base-class array (default).
        writeable : bool, optional
            When true, allow writing to the returned view. The default is false,
            as this should be used with caution: the returned view contains the
            same memory location multiple times, so writing to one location will
            cause others to change.
        Returns
        -------
        view : ndarray
            Sliding window view of the array. The sliding window dimensions are
            inserted at the end, and the original dimensions are trimmed as
            required by the size of the sliding window.
            That is, ``view.shape = x_shape_trimmed + window_shape``, where
            ``x_shape_trimmed`` is ``x.shape`` with every entry reduced by one less
            than the corresponding window size.
        r   )r3   F)copyrH   z-`window_shape` cannot contain negative valuesNzOSince axis is `None`, must provide window_shape for all dimensions of `x`; got z' window_shape elements and `x.ndim` is .T)Zallow_duplicatez8Must provide matching length window_shape and axis; got z window_shape elements and z axes elements.c                 3   s   | ]} j | V  qd S r   )strides)r   axr   r   r   r      s     z&sliding_window_view.<locals>.<genexpr>z4window shape cannot be larger than input array shaper   )rL   shaperH   rI   )Znumpy.core.numericr3   r	   iterabletuplearrayanyr6   r7   r4   r5   rL   rC   rO   r9   libstride_tricksZ
as_strided)r   Zwindow_shaperE   rH   rI   r3   Zwindow_shape_arrayZout_stridesZx_shape_trimmedrM   ZdimZ	out_shaper   rN   r   sliding_window_view   s@    ,

    rV   linearc                 C   s(   t rtj| ||dS tj| ||dS d S )N)method)interpolation)
_numpy_122r	   
percentile)r<   qrX   r   r   r   r[     s    r[   )	ArrayLike	DTypeLike)Any)NN)r   )N)rW   )%warningsZnumpyr	   Zpackaging.versionr   parse_versionZ
dask.utilsr   __version__Z_np_versionZ
_numpy_120Z
_numpy_121rZ   Z
_numpy_123Z
_numpy_124catch_warningsZallcloser
   float	TypeErrormaZ	ma_divider1   Z_DomainedBinaryOperationZ_DomainSafeDivider   rA   rG   rT   rU   rV   r[   Znumpy.typingr]   r^   typingr_   r   r   r   r   <module>   sb   



 
  
S

  Y
