ó
¦–Õ\c           @` sÂ  d  d l  m Z m Z m Z d  d l m Z d  d l Z d  d l m Z m	 Z	 d  d l
 m Z m Z d  d l Z d  d l Z d  d l m Z m Z m Z m Z d d l m Z d d	 l m Z d d
 l m Z m Z e d d d ƒ Z d „  Z d „  Z  d „  Z! d „  Z" d „  Z# d „  Z$ d „  Z% d „  Z& d „  Z' d „  Z( d  d „ Z) d „  Z* e d „  ƒ Z+ d „  Z, d „  Z- d „  Z. d „  Z/ d „  Z0 d „  Z1 d „  Z2 d „  Z3 d  „  Z4 d! „  Z5 d S("   i    (   t   absolute_importt   divisiont   print_function(   t   productN(   t   Integralt   Number(   t   getitemt
   itemgetter(   t   memoizet   merget   pluckt   concati   (   t   core(   t   HighLevelGraph(   t   tokenizet   is_dask_collectionc         C` sm   t  |  t ƒ rA t |  ƒ } | |  k r: t d |  ƒ ‚ qi | Sn( |  d k rQ d St d t |  ƒ |  ƒ ‚ d S(   s   Sanitize a one-element index.s$   Bad index.  Must be integer-like: %ss   Invalid index typeN(   t
   isinstanceR   t   intt
   IndexErrort   Nonet	   TypeErrort   type(   t   indt   ind2(    (    s1   lib/python2.7/site-packages/dask/array/slicing.pyt   _sanitize_index_element   s    c         C` s‡  |  d k r d St |  t ƒ rJ t t |  j ƒ t |  j ƒ t |  j ƒ ƒ St |  t ƒ rc t |  ƒ St |  ƒ rs |  St	 j
 |  ƒ } | j t k rÌ t	 j | ƒ } t | ƒ d k r¿ | d } n  t	 j
 | ƒ St	 j | j t	 j ƒ rè | St	 j | j t	 j ƒ rk| j t	 j ƒ } t	 j | | ƒ r(| St	 j | | ƒ } | j ƒ  t	 j | ƒ d } t d | ƒ ‚ n t d t |  ƒ |  ƒ ‚ d S(   s   Sanitize the elements for indexing along one axis

    >>> sanitize_index([2, 3, 5])
    array([2, 3, 5])
    >>> sanitize_index([True, False, True, False])
    array([0, 2])
    >>> sanitize_index(np.array([1, 2, 3]))
    array([1, 2, 3])
    >>> sanitize_index(np.array([False, True, True]))
    array([1, 2])
    >>> type(sanitize_index(np.int32(0)))
    <class 'int'>
    >>> sanitize_index(1.0)
    1
    >>> sanitize_index(0.5)
    Traceback (most recent call last):
    ...
    IndexError: Bad index.  Must be integer-like: 0.5
    i   i    s$   Bad index.  Must be integer-like: %ss   Invalid index typeN(   R   R   t   sliceR   t   startt   stopt   stepR   R   t   npt
   asanyarrayt   dtypet   boolt   nonzerot   lent
   issubdtypet   integert   floatingt   astypet   intpt   allcloset   iscloset   ravelt   flatnonzeroR   R   R   (   R   t   index_arrayR!   t	   int_indext	   check_intt	   first_err(    (    s1   lib/python2.7/site-packages/dask/array/slicing.pyt   sanitize_index!   s8    
	c         ` sü   t  t t  | ƒ ƒ } t d „  | Dƒ ƒ r t g  | D] } t t | ƒ ƒ ^ q5 Œ  } t ‡  ‡ f d †  | Dƒ ƒ } | | f St d „  | Dƒ ƒ } t | ƒ | } | t d d d ƒ f | 7} t
 ˆ ˆ  | | ƒ \ }	 }
 t  t t  |
 ƒ ƒ }
 |	 |
 f S(   s–  
    Master function for array slicing

    This function makes a new dask that slices blocks along every
    dimension and aggregates (via cartesian product) each dimension's
    slices so that the resulting block slices give the same results
    as the original slice on the original structure

    Index must be a tuple.  It may contain the following types

        int, slice, list (at most one list), None

    Parameters
    ----------
    in_name - string
      This is the dask variable name that will be used as input
    out_name - string
      This is the dask variable output name
    blockshape - iterable of integers
    index - iterable of integers, slices, lists, or None

    Returns
    -------
    Dict where the keys are tuples of

        (out_name, dim_index[, dim_index[, ...]])

    and the values are

        (function, (in_name, dim_index, dim_index, ...),
                   (slice(...), [slice()[,...]])

    Also new blockdims with shapes of each block

        ((10, 10, 10, 10), (20, 20))

    Examples
    --------
    >>> dsk, blockdims = slice_array('y', 'x', [(20, 20, 20, 20, 20)],
    ...                              (slice(10, 35),))  #  doctest: +SKIP
    >>> dsk  # doctest: +SKIP
    {('y', 0): (getitem, ('x', 0), (slice(10, 20),)),
     ('y', 1): (getitem, ('x', 1), (slice(0, 15),))}
    >>> blockdims  # doctest: +SKIP
    ((10, 15),)

    See Also
    --------
    This function works by successively unwrapping cases and passing down
    through a sequence of functions.

    slice_with_newaxis - handle None/newaxis case
    slice_wrap_lists - handle fancy indexing with lists
    slice_slices_and_integers - handle everything else
    c         s` s6   |  ], } t  | t ƒ o- | t d  d  d  ƒ k Vq d  S(   N(   R   R   R   (   t   .0t   index(    (    s1   lib/python2.7/site-packages/dask/array/slicing.pys	   <genexpr>‘   s   c         3` s)   |  ] } ˆ f | ˆ  f | f Vq d  S(   N(    (   R1   t   s(   t   in_namet   out_name(    s1   lib/python2.7/site-packages/dask/array/slicing.pys	   <genexpr>”   s   c         s` s   |  ] } | d  k	 Vq d  S(   N(   R   (   R1   t   i(    (    s1   lib/python2.7/site-packages/dask/array/slicing.pys	   <genexpr>™   s    N(   t   tuplet   mapt   allR   t   rangeR"   t   dictt   sumR   R   t   slice_with_newaxes(   R5   R4   t	   blockdimsR2   t   bdt   suffixest   dskt   not_none_countt   missingt   dsk_outt   bd_out(    (   R4   R5   s1   lib/python2.7/site-packages/dask/array/slicing.pyt   slice_arrayV   s    8	+
c         ` s_  t  g  | D] } | d k	 r
 | ^ q
 ƒ } g  t | ƒ D] \ } } | d k r8 | ^ q8 } t | ƒ } xM t | ƒ D]? \ } }	 t d „  | |	  Dƒ ƒ }
 |
 ru | | c |
 8<qu qu Wt ˆ | | | ƒ \ } } | rQt | ƒ ‰  t | ƒ ‰ ‡  ‡ ‡ f d †  | j ƒ  Dƒ } t | ‡ f d †  | j ƒ  Dƒ ƒ } ˆ  | d ƒ } | | f S| | f Sd S(   s]   
    Handle indexing with Nones

    Strips out Nones then hands off to slice_wrap_lists
    c         s` s   |  ] } t  | t ƒ Vq d  S(   N(   R   R   (   R1   R   (    (    s1   lib/python2.7/site-packages/dask/array/slicing.pys	   <genexpr>¯   s    c         ` s[   i  |  ]Q \ } } | d  ˆ k r | d  ˆ | d d ƒ f ˆ f ˆ  | d d  ƒ “ q S(   i    i   i   N(   R   (   R1   t   kt   v(   t   expandt   expand_origR5   (    s1   lib/python2.7/site-packages/dask/array/slicing.pys
   <dictcomp>»   s   		c         ` s/   i  |  ]% \ } } | d  ˆ  k r | | “ q S(   i    (    (   R1   RG   RH   (   R5   (    s1   lib/python2.7/site-packages/dask/array/slicing.pys
   <dictcomp>Á   s   	 i   N(   i   (	   R7   R   t	   enumeratet   listR<   t   slice_wrap_listst   expandert   itemsR	   (   R5   R4   R>   R2   R   t   index2R6   t
   where_nonet   where_none_origt   xt   nRA   t
   blockdims2t   dsk2t   dsk3t
   blockdims3(    (   RI   RJ   R5   s1   lib/python2.7/site-packages/dask/array/slicing.pyR=   ¤   s"    +1%
c         ` s  t  d „  | Dƒ ƒ s t ‚ t | ƒ t | ƒ k sC t d ƒ ‚ n  g  t | ƒ D]3 \ } } t | t j ƒ rP | j d k rP | ^ qP } t | ƒ d k rª t	 d ƒ ‚ n  | rù | | d j
 rù t | ƒ } t d d d ƒ | | j ƒ  <t | ƒ } n  | st |  | | | ƒ St d „  | Dƒ ƒ } t  d „  | Dƒ ƒ rt| d ‰  t |  | | | | d d ˆ  ƒ\ } }	 n› d	 t |  | | | f ƒ }
 t |
 | | | ƒ \ } } | d ‰  ˆ  t ‡  f d
 †  t | ƒ Dƒ ƒ } t |  |
 | | ˆ  d | ƒ\ } } t | | ƒ }	 |	 | f S(   s  
    Fancy indexing along blocked array dasks

    Handles index of type list.  Calls slice_slices_and_integers for the rest

    See Also
    --------

    take - handle slicing with lists ("fancy" indexing)
    slice_slices_and_integers - handle slicing with slices and integers
    c         s` s-   |  ]# } t  | t t t t j f ƒ Vq d  S(   N(   R   R   RL   R   R   t   ndarray(   R1   R6   (    (    s1   lib/python2.7/site-packages/dask/array/slicing.pys	   <genexpr>Ø   s   s   Too many indices for arrayi    i   s#   Don't yet support nd fancy indexingc         s` s9   |  ]/ } t  | t j ƒ r- t d  d  d  ƒ n | Vq d  S(   N(   R   R   RY   R   R   (   R1   R6   (    (    s1   lib/python2.7/site-packages/dask/array/slicing.pys	   <genexpr>î   s   c         s` s9   |  ]/ } t  | t j ƒ p0 | t d  d  d  ƒ k Vq d  S(   N(   R   R   RY   R   R   (   R1   R6   (    (    s1   lib/python2.7/site-packages/dask/array/slicing.pys	   <genexpr>ó   s   t   axiss   slice-c         3` s6   |  ], \ } } | ˆ  k  r t  | t ƒ r d  Vq d S(   i   N(   R   R   (   R1   R6   R   (   RZ   (    s1   lib/python2.7/site-packages/dask/array/slicing.pys	   <genexpr>   s    	(   R9   t   AssertionErrorR"   R   RK   R   R   RY   t   ndimt   NotImplementedErrort   sizeRL   R   t   popR7   t   slice_slices_and_integerst   takeR   R<   R	   (   R5   R4   R>   R2   R6   R   t
   where_listt   index_without_listRU   RW   t   tmpRA   t   axis2RV   (    (   RZ   s1   lib/python2.7/site-packages/dask/array/slicing.pyRM   Ì   s:    	-		
 
&c      	   C` sE  t  t t | ƒ ƒ } xV t | | ƒ D]E \ } } t j | ƒ r% | t d d d ƒ k r% t d | ƒ ‚ q% q% Wt	 d „  | Dƒ ƒ sŠ t
 ‚ t | ƒ t | ƒ k s¨ t
 ‚ t t t | | | ƒ ƒ } g  | D] } t | j ƒ  ƒ ^ qÊ }	 t t | g g  |	 D] }
 t d |
 ƒ ^ qû Œ ƒ } t t |  g g  t | | ƒ D]d \ } } t | t ƒ s8| j rŠ| j d k  rŠt t | ƒ ƒ d d d … n t t | ƒ ƒ ^ q8Œ ƒ } t t g  |	 D] }
 t d |
 ƒ ^ qµŒ  ƒ } d „  t | | | ƒ Dƒ } g  t | | | ƒ D]0 \ } } } t | t ƒ st | | | ƒ ^ q} | | f S(   s`   
    Dask array indexing with slices and integers

    See Also
    --------

    _slice_1d
    s"   Arrays chunk sizes are unknown: %sc         s` s$   |  ] } t  | t t f ƒ Vq d  S(   N(   R   R   R   (   R1   R   (    (    s1   lib/python2.7/site-packages/dask/array/slicing.pys	   <genexpr>  s    i    Niÿÿÿÿi   c         S` s+   i  |  ]! \ } } } t  | | f | “ q S(    (   R   (   R1   R5   R4   t   slices(    (    s1   lib/python2.7/site-packages/dask/array/slicing.pys
   <dictcomp>,  s   	(   R7   R8   R<   t   zipR   t   isnanR   R   t
   ValueErrorR9   R[   R"   RL   t	   _slice_1dt   sortedRO   R   R
   R   R   R   R:   t   new_blockdim(   R5   R4   R>   R2   t   shapet   dimR   t   block_slicesR6   t   sorted_block_slicesR3   t   in_namest   dt	   out_namest
   all_slicesRD   t   dbt   new_blockdims(    (    s1   lib/python2.7/site-packages/dask/array/slicing.pyR`     s(    	'%4d.'c         C` s:  t  j | d t  j ƒ} t | t ƒ rz | j | d d ƒ} | d k r] | | | d } n | } i t | ƒ t | ƒ 6St | t ƒ s t ‚ | t	 k rµ d „  t
 t | ƒ ƒ Dƒ S| j pÁ d } | d k r | j pÜ d } | j d	 k	 r÷ | j n |  } na | j d	 k	 r| j n |  d } | |  k r8|  d n | } | j d	 k rX|  d n | j } | d k  rz| |  7} n  | d k  r“| |  7} n  t ƒ  }	 | d k r¡| j | d d ƒ}
 | j | d d ƒ} t | d t | ƒ ƒ } |
 d k r| | |
 d } | | |
 d } n  x¤t
 |
 | ƒ D]l } | | } | | k  r†| d k r†t | t | | ƒ | ƒ |	 | <| | | } n
 | | } | | 8} q.Wn$| } | j | d d ƒ}
 | j | d d ƒ} t |
 d t | ƒ d ƒ }
 t | d d ƒ } xÁ t
 |
 | d ƒ D]­ } | | } | d k r9d } n | | d } | | k o^| k  n r| | k rt | | t | | d | | ƒ | ƒ |	 | <| | d | } | | d } qqWxO |	 j ƒ  D]A \ } } | t d | | d ƒ k rÒt d	 d	 d	 ƒ |	 | <qÒqÒW|	 s6t d d d ƒ |	 d <n  |	 S(
   s 	  Returns a dict of {blocknum: slice}

    This function figures out where each slice should start in each
    block for a single dimension. If the slice won't return any elements
    in the block, that block will not be in the output.

    Parameters
    ----------

    dim_shape - the number of elements in this dimension.
      This should be a positive, non-zero integer
    blocksize - the number of elements per block in this dimension
      This should be a positive, non-zero integer
    index - a description of the elements in this dimension that we want
      This might be an integer, a slice(), or an Ellipsis

    Returns
    -------

    dictionary where the keys are the integer index of the blocks that
      should be sliced and the values are the slices

    Examples
    --------

    Trivial slicing

    >>> _slice_1d(100, [60, 40], slice(None, None, None))
    {0: slice(None, None, None), 1: slice(None, None, None)}

    100 length array cut into length 20 pieces, slice 0:35

    >>> _slice_1d(100, [20, 20, 20, 20, 20], slice(0, 35))
    {0: slice(None, None, None), 1: slice(0, 15, 1)}

    Support irregular blocks and various slices

    >>> _slice_1d(100, [20, 10, 10, 10, 25, 25], slice(10, 35))
    {0: slice(10, 20, 1), 1: slice(None, None, None), 2: slice(0, 5, 1)}

    Support step sizes

    >>> _slice_1d(100, [15, 14, 13], slice(10, 41, 3))
    {0: slice(10, 15, 3), 1: slice(1, 14, 3), 2: slice(2, 12, 3)}

    >>> _slice_1d(100, [20, 20, 20, 20, 20], slice(0, 100, 40))  # step > blocksize
    {0: slice(0, 20, 40), 2: slice(0, 20, 40), 4: slice(0, 20, 40)}

    Also support indexing single elements

    >>> _slice_1d(100, [20, 20, 20, 20, 20], 25)
    {1: 5}

    And negative slicing

    >>> _slice_1d(100, [20, 20, 20, 20, 20], slice(100, 0, -3)) # doctest: +NORMALIZE_WHITESPACE
    {4: slice(-1, -21, -3),
     3: slice(-2, -21, -3),
     2: slice(-3, -21, -3),
     1: slice(-1, -21, -3),
     0: slice(-2, -20, -3)}

    >>> _slice_1d(100, [20, 20, 20, 20, 20], slice(100, 12, -3)) # doctest: +NORMALIZE_WHITESPACE
    {4: slice(-1, -21, -3),
     3: slice(-2, -21, -3),
     2: slice(-3, -21, -3),
     1: slice(-1, -21, -3),
     0: slice(-2, -8, -3)}

    >>> _slice_1d(100, [20, 20, 20, 20, 20], slice(100, -12, -3))
    {4: slice(-1, -12, -3)}
    R   t   sidet   righti    i   c         S` s   i  |  ] } t  | “ q S(    (   t   colon(   R1   RG   (    (    s1   lib/python2.7/site-packages/dask/array/slicing.pys
   <dictcomp>  s   	 t   leftiÿÿÿÿN(   R   t   cumsumt   int64R   R   t   searchsortedR   R   R[   Ry   R:   R"   R   R   R   R   R;   t   mint   maxRO   (   t	   dim_shapet   lengthsR2   t   chunk_boundariesR6   R   R   R   R   Rr   t   istartt   istopt   lengtht   rstartt
   chunk_stopt   chunk_startt   offsetRG   RH   (    (    s1   lib/python2.7/site-packages/dask/array/slicing.pyRj   7  sv    I!"#	


	(

c         C` sÏ   t  j | ƒ } t  j t |  ƒ d d t ƒ} d | d <t  j |  d | d ƒ} t  j t |  ƒ d d t ƒ} d | d <t  j | | ƒ | d )g  t | d  | d | ƒ D]  \ } } } | | | !| ^ q« S(   sy   

    >>> partition_by_size([10, 20, 10], [1, 5, 9, 12, 29, 35])
    [array([1, 5, 9]), array([ 2, 19]), array([5])]
    i   R   i    t   outiÿÿÿÿ(   R   R   t   emptyR"   R   R{   R}   Rg   (   t   sizest   seqRz   Rx   t	   locationst   jRG   t   l(    (    s1   lib/python2.7/site-packages/dask/array/slicing.pyt   partition_by_sizeÝ  s    

c         C` s1   t  |  ƒ d k r t St j |  d  |  d k ƒ S(   se    Is sequence sorted?

    >>> issorted([1, 2, 3])
    True
    >>> issorted([3, 1, 2])
    False
    i    iÿÿÿÿi   (   R"   t   TrueR   R9   (   R   (    (    s1   lib/python2.7/site-packages/dask/array/slicing.pyt   issortedï  s    c   	      C` s   t  j | ƒ } t  j |  ƒ } t  j | | d d ƒ} t  j t  j | ƒ ƒ d d } t  j d g | t | ƒ g g ƒ } g  } xy t t | ƒ d ƒ D]a } | | | | | d !} | | | } | d k rå | | | d } n  | j	 | | f ƒ q— W| S(   s‚   Construct a plan to slice chunks with the given index

    Parameters
    ----------
    chunks : Tuple[int]
        One dimensions worth of chunking information
    index : np.ndarray[int]
        The index passed to slice on that dimension

    Returns
    -------
    out : List[Tuple[int, np.ndarray]]
        A list of chunk/sub-index pairs corresponding to each output chunk
    Rw   Rx   i    i   (
   R   R   R{   R}   t   wheret   difft   concatenateR"   R:   t   append(	   t   chunksR2   t
   cum_chunkst   chunk_locationsR”   RŠ   R6   t	   sub_indext   chunk(    (    s1   lib/python2.7/site-packages/dask/array/slicing.pyt   slicing_planü  s     $c         C` s  t  | | | ƒ } t | ƒ t | | ƒ d k rƒ t j t | ƒ t | | ƒ ƒ } d d l m } t j d | | d d ƒn  g  | D] \ } }	 |	 ^ qŠ }
 g  | D] \ } } | ^ q© } g  | D] } t t | ƒ ƒ ^ qÈ } t	 | ƒ } t	 t t | ƒ ƒ ƒ | | <t	 t
 |  g | Œ ƒ } t	 | ƒ } | | | <g  | D] } t g t | ƒ ^ qC} |
 | | <t	 t
 | Œ  ƒ } t	 t
 | g | Œ ƒ } g  t | | ƒ D] \ } } t | | f ^ q¦} t	 | ƒ } t t t |
 ƒ ƒ | | <t t | | ƒ ƒ } t | ƒ | f S(   s   Index array with an iterable of index

    Handles a single index by a single list

    Mimics ``np.take``

    >>> chunks, dsk = take('y', 'x', [(20, 20, 20, 20)], [5, 1, 47, 3], axis=0)
    >>> chunks
    ((2, 1, 1),)
    >>> dsk  # doctest: +SKIP
    {('y', 0): (getitem, (np.concatenate, [(getitem, ('x', 0), ([1, 3, 5],)),
                                           (getitem, ('x', 2), ([7],))],
                                          0),
                         (2, 0, 4, 1))}

    When list is sorted we retain original block structure

    >>> chunks, dsk = take('y', 'x', [(20, 20, 20, 20)], [1, 3, 5, 47], axis=0)
    >>> chunks
    ((3, 1),)
    >>> dsk  # doctest: +SKIP
    {('y', 0): (getitem, ('x', 0), ([1, 3, 5],)),
     ('y', 2): (getitem, ('x', 2), ([7],))}
    i
   i   (   t   PerformanceWarningsE   Slicing with an out-of-order index is generating %d times more chunkst
   stackleveli   (   R   R"   t   matht   ceilR   Rž   t   warningst   warnR:   RL   R   Ry   Rg   R   R7   R8   R;   (   t   outnamet   innameR˜   R2   RZ   t   plant   factorRž   t   _t   idxt   index_listsR6   t   where_indexR?   t   dimst   indimst   keyst   outdimsRf   t   inkeyst   inkeyt   slct   valuest   chunks2RA   (    (    s1   lib/python2.7/site-packages/dask/array/slicing.pyRa     s0     #	

%
&
1c         C` s·   t  | t ƒ r% t t t |  | ƒ ƒ St  | t ƒ r_ | d k  rX t j |  ƒ rX | |  S| Sn  t  | t j t	 f ƒ r³ t j |  ƒ r³ t j
 | ƒ } t j | d k  | |  | ƒ S| S(   sQ   Flip negative indices around to positive ones

    >>> posify_index(10, 3)
    3
    >>> posify_index(10, -3)
    7
    >>> posify_index(10, [3, -3])
    array([3, 7])

    >>> posify_index((10, 20), (3, -3))
    (3, 17)
    >>> posify_index((10, 20), (3, [3, 4, -3]))  # doctest: +NORMALIZE_WHITESPACE
    (3, array([ 3,  4, 17]))
    i    (   R   R7   R8   t   posify_indexR   R    Rh   R   RY   RL   R   R”   (   Rm   R   (    (    s1   lib/python2.7/site-packages/dask/array/slicing.pyRµ   U  s    (c         B` sÐ   |  s d „  } | Sd } g  } d } xU e  e |  ƒ d ƒ D]= } | |  k r^ | j d ƒ q< | j d | ƒ | d 7} q< Wd | } d j | ƒ } | j e ƒ    } i  } e | d	 d
 ƒ | | U| d Sd  S(   Nc         S` s   |  S(   N(    (   R   t   val(    (    s1   lib/python2.7/site-packages/dask/array/slicing.pyRI   t  s    sK   def expand(seq, val):
            return ({left}) + tuple({right})
        i    i   s   val, s	   seq[%d], s   seq[%d:]t    s	   <dynamic>t   execRI   (   R:   R   R—   t   joint   formatt   localst   compile(   R”   RI   t   declRz   R   R6   Rx   t   ns(    (    s1   lib/python2.7/site-packages/dask/array/slicing.pyt	   _expanderq  s"    	
c         C` s   t  t |  ƒ ƒ S(   s“   Create a function to insert value at many locations in sequence.

    >>> expander([0, 2])(['a', 'b', 'c'], 'z')
    ('z', 'a', 'z', 'b', 'c')
    (   R¿   R7   (   R”   (    (    s1   lib/python2.7/site-packages/dask/array/slicing.pyRN   ‹  s    c         C` s=  | t  d d d ƒ k r | St | t ƒ r8 t | ƒ g St | t ƒ sN t ‚ t t |  | | ƒ j	 ƒ  d t
 d ƒ ƒ} g  | D]@ \ } } | t  d d d ƒ k r¹ t  d | | d ƒ n | ^ q } t | t  ƒ r| j r| j d k  r| d d d … } n  g  | D]0 } t t j d | j | j | j ƒ ƒ ^ q	S(   s  

    >>> new_blockdim(100, [20, 10, 20, 10, 40], slice(0, 90, 2))
    [10, 5, 10, 5, 15]

    >>> new_blockdim(100, [20, 10, 20, 10, 40], [5, 1, 30, 22])
    [4]

    >>> new_blockdim(100, [20, 10, 20, 10, 40], slice(90, 10, -2))
    [16, 5, 10, 5, 4]
    t   keyi    i   Niÿÿÿÿg      ð?(   R   R   R   RL   R"   R   R[   Rk   Rj   RO   R   R   R   R    R¡   R   R   (   R€   R   R2   t   pairsR6   R²   Rf   (    (    s1   lib/python2.7/site-packages/dask/array/slicing.pyRl   ”  s    J'c         C` s˜   g  t  | ƒ D] \ } } | t k r | ^ q } | s; | S| d } |  t | ƒ t d „  | Dƒ ƒ d } | |  t d d d ƒ f | | | d S(   sþ    Replace ... with slices, :, : ,:

    >>> replace_ellipsis(4, (3, Ellipsis, 2))
    (3, slice(None, None, None), slice(None, None, None), 2)

    >>> replace_ellipsis(2, (Ellipsis, None))
    (slice(None, None, None), slice(None, None, None), None)
    i    c         s` s   |  ] } | d  k Vq d  S(   N(   R   (   R1   R6   (    (    s1   lib/python2.7/site-packages/dask/array/slicing.pys	   <genexpr>½  s    i   N(   RK   t   EllipsisR"   R<   R   R   (   RT   R2   R6   R   t
   isellipsist   loct   extra_dimensions(    (    s1   lib/python2.7/site-packages/dask/array/slicing.pyt   replace_ellipsis®  s    
1
(c         C` s  t  |  t ƒ rt j | ƒ r" |  S|  j | ƒ \ } } } | d k rµ | d k r[ d } n  | | k rp d } n  | d k r… d } n  | d k	 rò | d k	 rò | | k  rò | } qò n= | d k  rò | | d k rÚ d } n  | d k  rò d } qò n  t | | | ƒ S|  S(   sã    Normalize slices to canonical form

    Parameters
    ----------
    idx: slice or other index
    dim: dimension length

    Examples
    --------
    >>> normalize_slice(slice(0, 10, 1), 10)
    slice(None, None, None)
    i    i   N(   R   R   R    Rh   t   indicesR   (   R©   Rn   R   R   R   (    (    s1   lib/python2.7/site-packages/dask/array/slicing.pyt   normalize_sliceÂ  s(    			$	c         C` sÈ  t  |  t ƒ s |  f }  n  t t | ƒ |  ƒ }  d } xX |  D]P } t | d ƒ rq | j d k rq | | j 7} q= | d k rƒ q= q= | d 7} q= W|  t d ƒ f t | ƒ | }  t g  |  D] } | d k	 r¼ | ^ q¼ ƒ t | ƒ k rø t d ƒ ‚ n  g  } d } xE |  D]= } | d k	 r;| j	 | | ƒ | d 7} q| j	 d ƒ qWx9 t
 |  | ƒ D]( \ } } | d k	 r\t | | ƒ q\q\Wt t t |  ƒ ƒ }  t t t |  | ƒ ƒ }  t | |  ƒ }  |  S(   s`   Normalize slicing indexes

    1.  Replaces ellipses with many full slices
    2.  Adds full slices to end of index
    3.  Checks bounding conditions
    4.  Replaces numpy arrays with lists
    5.  Posify's integers and lists
    6.  Normalizes slices to canonical form

    Examples
    --------
    >>> normalize_index(1, (10,))
    (1,)
    >>> normalize_index(-1, (10,))
    (9,)
    >>> normalize_index([-1], (10,))
    (array([9]),)
    >>> normalize_index(slice(-3, 10, 1), (10,))
    (slice(7, None, None),)
    >>> normalize_index((Ellipsis, None), (10,))
    (slice(None, None, None), None)
    i    R\   i   s   Too many indices for arrayN(   R   R7   RÆ   R"   t   hasattrR\   R   R   R   R—   Rg   t   check_indexR8   R0   RÈ   Rµ   (   R©   Rm   t   n_sliced_dimsR6   t
   none_shapeR   Rr   (    (    s1   lib/python2.7/site-packages/dask/array/slicing.pyt   normalize_indexæ  s6    !7c         C` s:  t  j | ƒ r d St |  t t  j f ƒ r² t  j |  ƒ } | j t k rw | j | k r¯ t	 d | j | f ƒ ‚ q¯ q6| | k j
 ƒ  sœ | | k  j
 ƒ  r6t	 d | ƒ ‚ q6n„ t |  t ƒ rÅ d St |  ƒ rÕ d S|  d k rå d S|  | k r
t	 d |  | f ƒ ‚ n, |  | k  r6d } t	 | |  | f ƒ ‚ n  d S(   s/   Check validity of index for a given dimension

    Examples
    --------
    >>> check_index(3, 5)
    >>> check_index(5, 5)
    Traceback (most recent call last):
    ...
    IndexError: Index is not smaller than dimension 5 >= 5

    >>> check_index(6, 5)
    Traceback (most recent call last):
    ...
    IndexError: Index is not smaller than dimension 6 >= 5

    >>> check_index(-1, 5)
    >>> check_index(-6, 5)
    Traceback (most recent call last):
    ...
    IndexError: Negative index is not greater than negative dimension -6 <= -5

    >>> check_index([1, 2], 5)
    >>> check_index([6, 3], 5)
    Traceback (most recent call last):
    ...
    IndexError: Index out of bounds 5

    >>> check_index(slice(0, 3), 5)

    >>> check_index([True], 1)
    >>> check_index([True, True], 3)
    Traceback (most recent call last):
    ...
    IndexError: Boolean array length 2 doesn't equal dimension 3
    >>> check_index([True, True, True], 1)
    Traceback (most recent call last):
    ...
    IndexError: Boolean array length 3 doesn't equal dimension 1
    Ns2   Boolean array length %s doesn't equal dimension %ss   Index out of bounds %ss,   Index is not smaller than dimension %d >= %ds?   Negative index is not greater than negative dimension %d <= -%d(   R   Rh   R   RL   RY   R   R   R    R^   R   t   anyR   R   R   (   R   t	   dimensionRS   t   msg(    (    s1   lib/python2.7/site-packages/dask/array/slicing.pyRÊ     s.    )%c         ` s¶  d d l  m } t | ƒ |  j k s+ t ‚ g  | D]B } t | t t f ƒ pq t | t j	 | f ƒ oq | j d k ^ q2 } t
 | ƒ d k r› t d ƒ ‚ n  g  } d } xü t | ƒ D]î \ } } | | ‰  t | | ƒ r•| j j d k r•| j d k rO| t j } t |  | ˆ  ƒ }  |  t ‡  f d †  t |  j ƒ Dƒ ƒ }  | d 7} q¢| j d k r†t |  | ˆ  ƒ }  | j t d ƒ ƒ q¢t d ƒ ‚ q´ | j | ƒ q´ W|  t | ƒ f S(	   s+   Slice x with at most one 1D dask arrays of ints.

    This is a helper function of :meth:`Array.__getitem__`.

    Parameters
    ----------
    x: Array
    index: tuple with as many elements as x.ndim, among which there are
           one or more Array's with dtype=int

    Returns
    -------
    tuple of (sliced x, new index)

    where the new index is the same as the input, but with slice(None)
    replaced to the original slicer where a 1D filter has been applied and
    one less element where a zero-dimensional filter has been applied.
    i   (   t   Arrayi    s$   Don't yet support nd fancy indexing)t   iuc         3` s-   |  ]# } | ˆ  k r d  n	 t  d ƒ Vq d S(   i    N(   R   R   (   R1   R6   (   t   out_axis(    s1   lib/python2.7/site-packages/dask/array/slicing.pys	   <genexpr>‰  s   sZ   Slicing with dask.array of ints only permitted when the indexer has zero or one dimensionsN(   R   RÑ   R"   R\   R[   R   R7   RL   R   RY   R<   R]   RK   R   t   kindt   newaxist!   slice_with_int_dask_array_on_axisR:   R—   R   R   (   RS   R2   RÑ   R©   t   fancy_indexest	   out_indext   dropped_axis_cntt   in_axis(    (   RÓ   s1   lib/python2.7/site-packages/dask/array/slicing.pyt   slice_with_int_dask_arrayb  s2    L
!c         C` sÈ  d d l  m } m } m } d d l m } d | k oF |  j k  n sQ t ‚ t j	 |  j
 | ƒ j ƒ  r| t d ƒ ‚ n  t j t j |  j
 | ƒ d ƒ } d | d <| | d d ƒ} | | j | j |  j
 | f | j ƒ } t t |  j ƒ ƒ } |  j f }	 | f }
 | | d  |	 | | d } | |  |	 | | d } | | j | |  | | |	 | |
 d |  j | d | d	 |  j ƒ} | | j | | |	 | | d
 t d |  j
 | d | d	 |  j ƒ} | S(   s˜    Slice a ND dask array with a 1D dask arrays of ints along the given
    axis.

    This is a helper function of :func:`slice_with_int_dask_array`.
    i   (   RÑ   t	   blockwiset
   from_array(   Rœ   i    sO   Slicing an array with unknown chunks with a dask.array of ints is not supportedR˜   t   x_sizeRZ   R   R–   t   x_chunks(   R   RÑ   RÜ   RÝ   R·   Rœ   R\   R[   R   Rh   R˜   RÎ   R]   t   rollR{   t   daskt   nameR   R7   R:   RÛ   Rm   t#   slice_with_int_dask_array_aggregateR’   (   RS   R©   RZ   RÑ   RÜ   RÝ   Rœ   R‰   t   x_axest   idx_axest   offset_axest   p_axest   y_axest   pt   y(    (    s1   lib/python2.7/site-packages/dask/array/slicing.pyRÖ   ™  s,    %"
(			c         ` s  d d l  m ‰  m } m } g  | D]6 } t | ˆ  ƒ rS | j t k rS t d
 ƒ n | ^ q# } t	 | ƒ d k r-| d j
 |  j
 k r-| t |  d |  j | Œ} d t |  | ƒ ‰ ‡ f d †  t t  j | j ƒ  ƒ ƒ Dƒ } t j f | j f } t j ˆ | d | g ƒ}	 ˆ  |	 ˆ | |  j ƒ | f St ‡  f d †  | Dƒ ƒ rXt d	 ƒ ‚ n  g  | D]6 } t | ˆ  ƒ r‰| j t k r‰| n	 t d
 ƒ ^ q_}
 g  } d } x‡ |
 D] } t | ˆ  ƒ r| j t k r| t t | | | j
 ƒ ƒ f } | |  j
 7} n t d
 ƒ d
 f } | d 7} | j | ƒ q®Wt t | ƒ ƒ } | t t t |  j
 ƒ ƒ |  t t |  j
 ƒ ƒ d |  j | Œ} g  } xk t | | j ƒ D]W \ } } t | ˆ  ƒ rå| j t k rå| j t j f t	 | ƒ ƒ q›| j | ƒ q›Wt | ƒ | _ | t | ƒ f S(   s   Slice x with one or more dask arrays of bools

    This is a helper function of `Array.__getitem__`.

    Parameters
    ----------
    x: Array
    index: tuple with as many elements as x.ndim, among which there are
           one or more Array's with dtype=bool

    Returns
    -------
    tuple of (sliced x, new index)

    where the new index is the same as the input, but with slice(None)
    replaced to the original slicer when a filter has been applied.

    Note: The sliced x will have nan chunks on the sliced axes.
    i   (   RÑ   RÜ   t   elemwisei    R   s   getitem-c         ` s%   i  |  ] \ } } | ˆ  | f “ q S(    (    (   R1   R6   RG   (   Râ   (    s1   lib/python2.7/site-packages/dask/array/slicing.pys
   <dictcomp>ã  s   	 t   dependenciesc         3` s<   |  ]2 } t  | ˆ  ƒ o3 | j t k o3 | j d  k Vq d S(   i   N(   R   R   R    R\   (   R1   R   (   RÑ   (    s1   lib/python2.7/site-packages/dask/array/slicing.pys	   <genexpr>è  s   s   Slicing with dask.array of bools only permitted when the indexer has only one dimension or when it has the same dimension as the sliced arrayN(    R   RÑ   RÜ   Rë   R   R   R    R   R   R"   R\   R   R   RK   t   flattent   __dask_keys__R   t   nant   npartitionsR   t   from_collectionsRÎ   R]   R7   R:   R—   RL   R   t   getitem_variadicRg   R˜   t   _chunks(   RS   R2   RÜ   Rë   R   RØ   Rê   RA   R˜   t   grapht   indexest   argindsR6   t   newRŠ   Rœ   (    (   RÑ   Râ   s1   lib/python2.7/site-packages/dask/array/slicing.pyt   slice_with_bool_dask_arrayÅ  sD    @(+@"
0 c         G` s   |  | S(   N(    (   RS   R2   (    (    s1   lib/python2.7/site-packages/dask/array/slicing.pyRò     s    (6   t
   __future__R    R   R   t	   itertoolsR   R    t   numbersR   R   t   operatorR   R   R¢   t   numpyR   t   toolzR   R	   R
   R   R·   R   t   highlevelgraphR   t   baseR   R   R   R   Ry   R   R0   RF   R=   RM   R`   Rj   R‘   R“   R   Ra   Rµ   R¿   RN   Rl   RÆ   RÈ   RÍ   RÊ   RÛ   RÖ   Rø   Rò   (    (    (    s1   lib/python2.7/site-packages/dask/array/slicing.pyt   <module>   sD   "		5	N	(	?	,	¦			!8						$	8	D	7	,	H