ó
¾÷Xc           @@  sü   d  Z  d d l m Z d d l Z d d l m Z d d l Z d d l m	 Z	 m
 Z
 d „  Z d e f d	 „  ƒ  YZ d
 e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d „  Z d „  Z d „  Z d „  Z d „  Z d S(   s4   
    High-level access to HDF5 dataspace selections
i    (   t   absolute_importN(   t   xrangei   (   t   h5st   h5rc         C@  s“  t  | t ƒ s | f } n  t | ƒ d k rç | d } t  | t ƒ rh | j |  k rd t d ƒ ‚ n  | St  | t j ƒ r’ t |  ƒ } | | | St  | t	 j
 ƒ rç t	 j | | ƒ } |  | j k rÔ t d ƒ ‚ n  t |  d | ƒSn  x‘ | D]‰ } t  | t ƒ rî | t k	 rî y; t | ƒ t  | t j ƒ rJ| j d k rJt ƒ  ‚ n  Wqwt k
 rst |  ƒ } | | | SXqî qî Wt |  ƒ } | | | S(   s&   High-level routine to generate a selection from arbitrary arguments
    to __getitem__.  The arguments should be the following:

    shape
        Shape of the "source" dataspace.

    args
        Either a single argument or a tuple of arguments.  See below for
        supported classes of argument.
    
    dsid
        A h5py.h5d.DatasetID instance representing the source dataset.

    Argument classes:

    Single Selection instance
        Returns the argument.

    numpy.ndarray
        Must be a boolean mask.  Returns a PointSelection instance.

    RegionReference
        Returns a Selection instance.

    Indices, slices, ellipses only
        Returns a SimpleSelection instance

    Indices, slices, ellipses, lists or boolean index arrays
        Returns a FancySelection instance.
    i   i    s   Mismatched selection shapes,   Reference shape does not match dataset shapet   spaceid(   i   (   t
   isinstancet   tuplet   lent	   Selectiont   shapet	   TypeErrort   npt   ndarrayt   PointSelectionR   t   RegionReferencet
   get_regiont   slicet   Ellipsist   intt	   Exceptiont   FancySelectiont   SimpleSelection(   R	   t   argst   dsidt   argt   selt   sidt   a(    (    s1   /tmp/pip-build-isqEY4/h5py/h5py/_hl/selections.pyt   select   s<    

!t   _RegionProxyc           B@  s    e  Z d  Z d „  Z d „  Z RS(   sû   
        Thin proxy object which takes __getitem__-style index arguments and
        produces RegionReference objects.  Example:

        >>> dset = myfile['dataset']
        >>> myref = dset.regionref[0:100,20:30]
        >>> data = dset[myref]

    c         C@  s   | |  _  d S(   s&    Supply a h5py.h5d.DatasetID instance N(   t   id(   t   selfR   (    (    s1   /tmp/pip-build-isqEY4/h5py/h5py/_hl/selections.pyt   __init__m   s    c         C@  s:   t  |  j j | |  j ƒ } t j |  j d t j | j ƒ S(   sˆ    Takes arbitrary selection terms and produces a RegionReference
        object.  Selection must be compatible with the dataset.
        t   .(   R   R   R	   R   t   createt   DATASET_REGION(   R   R   t	   selection(    (    s1   /tmp/pip-build-isqEY4/h5py/h5py/_hl/selections.pyt   __getitem__q   s    (   t   __name__t
   __module__t   __doc__R    R%   (    (    (    s1   /tmp/pip-build-isqEY4/h5py/h5py/_hl/selections.pyR   a   s   
	R   c           B@  sh   e  Z d  Z d d „ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z	 d „  Z
 d „  Z RS(	   s‰  
        Base class for HDF5 dataspace selections.  Subclasses support the
        "selection protocol", which means they have at least the following
        members:
        
        __init__(shape)   => Create a new selection on "shape"-tuple
        __getitem__(args) => Perform a selection with the range specified.
                             What args are allowed depends on the
                             particular subclass in use.

        id (read-only) =>      h5py.h5s.SpaceID instance
        shape (read-only) =>   The shape of the dataspace.
        mshape  (read-only) => The shape of the selection region. 
                               Not guaranteed to fit within "shape", although
                               the total number of points is less than
                               product(shape).
        nselect (read-only) => Number of selected points.  Always equal to
                               product(mshape).

        broadcast(target_shape) => Return an iterable which yields dataspaces
                                   for read, based on target_shape.

        The base class represents "unshaped" selections (1-D).
    c         C@  so   | d k	 r$ | |  _ | j |  _ nG t | ƒ } | |  _ t j | t j f t | ƒ ƒ |  _ |  j j	 ƒ  d S(   s=    Create a selection.  Shape may be None if spaceid is given. N(
   t   Nonet   _idR	   t   _shapeR   R   t   create_simplet	   UNLIMITEDR   t
   select_all(   R   R	   R   (    (    s1   /tmp/pip-build-isqEY4/h5py/h5py/_hl/selections.pyR    “   s    		%c         C@  s   |  j  S(   s    SpaceID instance (   R*   (   R   (    (    s1   /tmp/pip-build-isqEY4/h5py/h5py/_hl/selections.pyR   ž   s    c         C@  s   |  j  S(   s    Shape of whole dataspace (   R+   (   R   (    (    s1   /tmp/pip-build-isqEY4/h5py/h5py/_hl/selections.pyR	   £   s    c         C@  s   |  j  j ƒ  S(   s'    Number of elements currently selected (   R*   t   get_select_npoints(   R   (    (    s1   /tmp/pip-build-isqEY4/h5py/h5py/_hl/selections.pyt   nselect¨   s    c         C@  s
   |  j  f S(   s0    Shape of selection (always 1-D for this class) (   R0   (   R   (    (    s1   /tmp/pip-build-isqEY4/h5py/h5py/_hl/selections.pyt   mshape­   s    c         c@  s3   t  j | ƒ |  j k r' t d ƒ ‚ n  |  j Vd S(   s"    Get an iterable for broadcasting s7   Broadcasting is not supported for point-wise selectionsN(   R   t   productR0   R
   R*   (   R   t   target_shape(    (    s1   /tmp/pip-build-isqEY4/h5py/h5py/_hl/selections.pyt	   broadcast²   s    c         C@  s   t  d ƒ ‚ d  S(   Ns$   This class does not support indexing(   t   NotImplementedError(   R   R   (    (    s1   /tmp/pip-build-isqEY4/h5py/h5py/_hl/selections.pyR%   ¸   s    N(   R&   R'   R(   R)   R    t   propertyR   R	   R0   R1   R4   R%   (    (    (    s1   /tmp/pip-build-isqEY4/h5py/h5py/_hl/selections.pyR   x   s   	R   c           B@  s;   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   sÂ   
        Represents a point-wise selection.  You can supply sequences of
        points to the three methods append(), prepend() and set(), or a
        single boolean array to __getitem__.
    c         C@  s¦   t  j | d d d d ƒ} t | j ƒ d k rI d | j d f | _ n  |  j j ƒ  t j k rm t j } n  t | ƒ d k r |  j j	 ƒ  n |  j j
 | | ƒ d S(   s7    Internal method which actually performs the selection t   ordert   Ct   dtypet   u8i   i    N(   R   t   asarrayR   R	   R*   t   get_select_typeR   t
   SEL_POINTSt
   SELECT_SETt   select_nonet   select_elements(   R   t   pointst   op(    (    s1   /tmp/pip-build-isqEY4/h5py/h5py/_hl/selections.pyt   _perform_selectionÃ   s    c         C@  sz   t  | t j ƒ o! | j j d k s3 t d ƒ ‚ n  | j |  j k sT t d ƒ ‚ n  t j | j ƒ  ƒ } |  j	 | ƒ |  S(   s9    Perform point-wise selection from a NumPy boolean array t   bs6   PointSelection __getitem__ only works with bool arrayss-   Boolean indexing array has incompatible shape(
   R   R   R   R9   t   kindR
   R	   t	   transposet   nonzerot   set(   R   R   RA   (    (    s1   /tmp/pip-build-isqEY4/h5py/h5py/_hl/selections.pyR%   Ñ   s    $c         C@  s   |  j  | t j ƒ d S(   s@    Add the sequence of points to the end of the current selection N(   RC   R   t   SELECT_APPEND(   R   RA   (    (    s1   /tmp/pip-build-isqEY4/h5py/h5py/_hl/selections.pyt   appendÜ   s    c         C@  s   |  j  | t j ƒ d S(   sF    Add the sequence of points to the beginning of the current selection N(   RC   R   t   SELECT_PREPEND(   R   RA   (    (    s1   /tmp/pip-build-isqEY4/h5py/h5py/_hl/selections.pyt   prependà   s    c         C@  s   |  j  | t j ƒ d S(   s@    Replace the current selection with the given sequence of pointsN(   RC   R   R>   (   R   RA   (    (    s1   /tmp/pip-build-isqEY4/h5py/h5py/_hl/selections.pyRH   ä   s    (   R&   R'   R(   RC   R%   RJ   RL   RH   (    (    (    s1   /tmp/pip-build-isqEY4/h5py/h5py/_hl/selections.pyR   »   s   				R   c           B@  s8   e  Z d  Z e d „  ƒ Z d „  Z d „  Z d „  Z RS(   s‰    A single "rectangular" (regular) selection composed of only slices
        and integer arguments.  Can participate in broadcasting.
    c         C@  s   |  j  S(   s    Shape of current selection (   t   _mshape(   R   (    (    s1   /tmp/pip-build-isqEY4/h5py/h5py/_hl/selections.pyR1   ï   s    c         O@  s\   t  j |  | | | Ž t |  j ƒ } d | |  j d | t f | f |  _ |  j |  _ d  S(   Ni    i   (   i    (   i   (   R   R    R   R	   t   Falset   _selRM   (   R   R	   R   t   kwdst   rank(    (    s1   /tmp/pip-build-isqEY4/h5py/h5py/_hl/selections.pyR    ô   s    'c         C@  sá   t  | t ƒ s | f } n  |  j d k rr t | ƒ d k ra | d t d f k ra t d ƒ ‚ n  |  j j ƒ  |  St |  j | ƒ \ } } } } |  j j	 | | | ƒ | | | | f |  _
 t d „  t | | ƒ Dƒ ƒ |  _ |  S(   Ni    s7   Invalid index for scalar dataset (only ..., () allowed)c         s@  s!   |  ] \ } } | s | Vq d  S(   N(    (   t   .0t   xt   y(    (    s1   /tmp/pip-build-isqEY4/h5py/h5py/_hl/selections.pys	   <genexpr>  s    (    (    (   R   R   R	   R   R   R
   R*   R.   t   _handle_simplet   select_hyperslabRO   t   zipRM   (   R   R   t   startt   countt   stept   scalar(    (    s1   /tmp/pip-build-isqEY4/h5py/h5py/_hl/selections.pyR%   ú   s    ("c         c@  s  |  j  d k rP t j | ƒ d k r7 t d | ƒ ‚ n  |  j j ƒ  |  j Vd S|  j \ } } } } t | ƒ } t | ƒ } g  } x— t	 d | d ƒ D]‚ }	 t | ƒ d k sº | |	 rÊ | j
 d ƒ q— | j ƒ  }
 |
 d k só | |	 |
 k r| j
 |
 ƒ q— t d | | f ƒ ‚ q— W| j ƒ  t | ƒ } t d „  t | | ƒ Dƒ ƒ } t t j | ƒ ƒ } | d k r~|  j Vn€ |  j j ƒ  } | j d	 | | | ƒ xW t	 | ƒ D]I }	 t d „  t t j |	 | ƒ | | | ƒ Dƒ ƒ } | j | ƒ | Vq±Wd S(
   s·    Return an iterator over target dataspaces for broadcasting.

        Follows the standard NumPy broadcasting rules against the current
        selection shape (self.mshape).
        i   s   Can't broadcast %s to scalarNi    s   Can't broadcast %s -> %sc         s@  s   |  ] \ } } | | Vq d  S(   N(    (   RR   RS   RT   (    (    s1   /tmp/pip-build-isqEY4/h5py/h5py/_hl/selections.pys	   <genexpr>/  s    c         s@  s-   |  ]# \ } } } } | | | | Vq d  S(   N(    (   RR   RS   RT   t   zt   s(    (    s1   /tmp/pip-build-isqEY4/h5py/h5py/_hl/selections.pys	   <genexpr>8  s    (    (   i    (   R	   R   R2   R
   R*   R.   RO   R   t   listR   RJ   t   popt   reverseR   RW   R   t   copyRV   t   unravel_indext   offset_simple(   R   R3   RX   RY   RZ   R[   RQ   t   targett   tshapet   idxt   tt   chunkst   nchunksR   t   offset(    (    s1   /tmp/pip-build-isqEY4/h5py/h5py/_hl/selections.pyR4     s:    
1(   R&   R'   R(   R6   R1   R    R%   R4   (    (    (    s1   /tmp/pip-build-isqEY4/h5py/h5py/_hl/selections.pyR   é   s
   		R   c           B@  s8   e  Z d  Z e d „  ƒ Z d „  Z d „  Z d „  Z RS(   s(  
        Implements advanced NumPy-style selection operations in addition to
        the standard slice-and-int behavior.

        Indexing arguments may be ints, slices, lists of indicies, or
        per-axis (1D) boolean arrays.

        Broadcasting is not supported for these selections.
    c         C@  s   |  j  S(   N(   RM   (   R   (    (    s1   /tmp/pip-build-isqEY4/h5py/h5py/_hl/selections.pyR1   I  s    c         O@  s&   t  j |  | | | Ž |  j |  _ d  S(   N(   R   R    R	   RM   (   R   R	   R   RP   (    (    s1   /tmp/pip-build-isqEY4/h5py/h5py/_hl/selections.pyR    M  s    c         @  s  t  | t ƒ s | f } n  t | t |  j ƒ ƒ } i  } xú t | ƒ D]ì \ } } t  | t ƒ sF t | d ƒ r¿ | j t	 j d ƒ k r¿ t | j ƒ d k r¬ t
 d ƒ ‚ n  | j ƒ  d } n  y t | ƒ | | <Wn t
 k
 ræ q2Xt | ƒ } t | d  | d ƒ } t d „  | Dƒ ƒ r2t
 d ƒ ‚ q2qF qF Wt | ƒ d k rWt
 d	 ƒ ‚ n  t | ƒ d k rxt
 d
 ƒ ‚ n  t t | j ƒ  ƒ d ƒ ‰  t ‡  f d †  | j ƒ  Dƒ ƒ sÉt
 d | ƒ ‚ n  g  } x[ t ˆ  ƒ D]M } t | ƒ } x+ t j | ƒ D] \ }	 }
 |
 | | |	 <qþW| j | ƒ qÜW|  j j ƒ  xW t | ƒ D]I \ } } t |  j | ƒ \ } } } } |  j j | | | d t j ƒqGWt | ƒ } xT t t | ƒ ƒ D]@ } | | k rÜt | | ƒ | | <q³| | r³d | | <q³q³Wt d „  | Dƒ ƒ |  _ d  S(   NR9   t   booli   s#   Boolean indexing arrays must be 1-Di    iÿÿÿÿc         s@  s!   |  ] \ } } | | k Vq d  S(   N(    (   RR   t   fstt   snd(    (    s1   /tmp/pip-build-isqEY4/h5py/h5py/_hl/selections.pys	   <genexpr>h  s    s-   Indexing elements must be in increasing ordersM   Only one indexing vector or array is currently allowed for advanced selections    Advanced selection inappropriatec         3@  s!   |  ] } t  | ƒ ˆ  k Vq d  S(   N(   R   (   RR   RS   (   t   vectorlength(    s1   /tmp/pip-build-isqEY4/h5py/h5py/_hl/selections.pys	   <genexpr>q  s    s3   All sequence arguments must have the same length %sRB   c         s@  s!   |  ] } | d  k r | Vq d S(   i    N(    (   RR   RS   (    (    s1   /tmp/pip-build-isqEY4/h5py/h5py/_hl/selections.pys	   <genexpr>  s    (   R   R   t   _expand_ellipsisR   R	   t	   enumerateR   t   hasattrR9   R   R
   RG   R^   RW   t   anyt   valuest   allR   t   sixt	   iteritemsRJ   R*   R?   RU   RV   R   t	   SELECT_ORRM   (   R   R   t   sequenceargsRf   R   t   list_argt   adjacentt	   argvectort   entryt   positiont   seqt   vectorRX   RY   RZ   R[   R1   (    (   Rn   s1   /tmp/pip-build-isqEY4/h5py/h5py/_hl/selections.pyR%   Q  sT    '"#
c         c@  s*   | |  j  k s t d ƒ ‚ n  |  j Vd  S(   Ns4   Broadcasting is not supported for complex selections(   R1   R
   R*   (   R   R3   (    (    s1   /tmp/pip-build-isqEY4/h5py/h5py/_hl/selections.pyR4   ‘  s    (   R&   R'   R(   R6   R1   R    R%   R4   (    (    (    s1   /tmp/pip-build-isqEY4/h5py/h5py/_hl/selections.pyR   =  s
   
		@c         C@  së   t  d „  |  Dƒ ƒ } | d k r1 t d ƒ ‚ n. | d k r_ t |  ƒ | k r_ |  t f }  n  g  } t |  ƒ } xR |  D]J } | t k rµ | j t d d d ƒ f | | d ƒ qx | j | ƒ qx Wt | ƒ | k rç t d ƒ ‚ n  | S(   s7    Expand ellipsis objects and fill in missing axes.
    c         s@  s!   |  ] } | t  k r d  Vq d S(   i   N(   R   (   RR   R   (    (    s1   /tmp/pip-build-isqEY4/h5py/h5py/_hl/selections.pys	   <genexpr>™  s    i   s   Only one ellipsis may be used.i    s   Argument sequence too longN(	   t   sumt
   ValueErrorR   R   t   extendR   R)   RJ   R
   (   R   RQ   t   n_elt
   final_argst   n_argsR   (    (    s1   /tmp/pip-build-isqEY4/h5py/h5py/_hl/selections.pyRo   –  s    +c         C@  s%  t  | t |  ƒ ƒ } g  } g  } g  } g  } xÍ t | |  ƒ D]¼ \ } } t | t ƒ ry t | | ƒ \ } }	 }
 t } nL y( t t | ƒ | ƒ \ } }	 }
 t	 } Wn! t
 k
 rÄ t
 d | ƒ ‚ n X| j | ƒ | j |	 ƒ | j |
 ƒ | j | ƒ q= Wt | ƒ t | ƒ t | ƒ t | ƒ f S(   sU   Process a "simple" selection tuple, containing only slices and
        integer objects.  Return is a 4-tuple with tuples for start,
        count, step, and a flag which tells if the axis is a "scalar"
        selection (indexed by an integer).

        If "args" is shorter than "shape", the remaining axes are fully
        selected.
    s.   Illegal index "%s" (must be a slice or number)(   Ro   R   RW   R   R   t   _translate_sliceRN   t   _translate_intR   t   TrueR
   RJ   R   (   R	   R   RX   RY   RZ   R[   R   t   lengthRS   RT   R\   R]   (    (    s1   /tmp/pip-build-isqEY4/h5py/h5py/_hl/selections.pyRU   ­  s&    		
c         C@  s_   |  d k  r | |  }  n  d |  k o0 | k  n sR t  d |  | d f ƒ ‚ n  |  d d f S(   sk    Given an integer index, return a 3-tuple
        (start, count, step)
        for hyperslab selection
    i    s   Index (%s) out of range (0-%s)i   (   R   (   t   expR‰   (    (    s1   /tmp/pip-build-isqEY4/h5py/h5py/_hl/selections.pyR‡   Î  s
    c         C@  su   |  j  | ƒ \ } } } | d k  r7 t d | ƒ ‚ n  | | k  rR t d ƒ ‚ n  d | | d | } | | | f S(   s    Given a slice object, return a 3-tuple
        (start, count, step)
        for use with the hyperslab selection routines
    i   s   Step must be >= 1 (got %d)s(   Reverse-order selections are not allowed(   t   indicesR   (   RŠ   R‰   RX   t   stopRZ   RY   (    (    s1   /tmp/pip-build-isqEY4/h5py/h5py/_hl/selections.pyR†   Û  s    c         @  s¬  ˆ j  ƒ  } ˆ j ƒ  } | t j k r+ d S| t j k rf | t j k rM d S| t j k rˆ t ƒ  Sn" | t j	 k rˆ t
 d | ƒ ‚ n  ˆ j ƒ  ‰  t ˆ j ƒ } | t j k rº d | S| t j k rÐ ˆ j S| t j k ræ ˆ  f S| t j k rt
 d | ƒ ‚ n  ˆ  d k rd	 | Sd „  ˆ j ƒ  Dƒ \ ‰ } | ˆ t j | f ƒ ‰ ‡  ‡ ‡ f d †  ‰ t ‡ ‡ f d †  t | ƒ Dƒ ƒ } t j | ƒ ˆ  k r¨ˆ  f S| S(
   s1   Given a dataspace, try to deduce the shape of the selection.

    Returns one of:
        * A tuple with the selection shape, same length as the dataspace 
        * A 1D selection shape for point-based and multiple-hyperslab selections
        * None, for unselected scalars and for NULL dataspaces
    s   Unrecognized dataspace class %si    s    Unrecognized selection method %sc         s@  s   |  ] } t  j | ƒ Vq d  S(   N(   R   t   array(   RR   RS   (    (    s1   /tmp/pip-build-isqEY4/h5py/h5py/_hl/selections.pys	   <genexpr>  s    c         @  s‘   ˆ | d k r d Sˆ j  ƒ  } | | c d 7<ˆ j  ƒ  } | | c d 8<|  j  ƒ  } | j t | ƒ t | ƒ d t j ƒ| j ƒ  } ˆ  | S(   sä   Determine the number of elements selected along a particular axis.

        To do this, we "mask off" the axis by making a hyperslab selection
        which leaves only the first point along the axis.  For a 2D dataset
        with selection box shape (X, Y), for axis 1, this would leave a
        selection of shape (X, 1).  We count the number of points N_leftover
        remaining in the selection and compute the axis selection length by
        N_axis = N/N_leftover.
        i   RB   (   Ra   RV   R   R   t   SELECT_NOTBR/   (   R   t   axisRX   RY   t
   masked_sidt
   N_leftover(   t   Nt   bottomcornert   boxshape(    s1   /tmp/pip-build-isqEY4/h5py/h5py/_hl/selections.pyt
   get_n_axis#  s    %c         3@  s   |  ] } ˆ  ˆ | ƒ Vq d  S(   N(    (   RR   RS   (   R•   R   (    s1   /tmp/pip-build-isqEY4/h5py/h5py/_hl/selections.pys	   <genexpr>?  s    N(   i    (   i    (   t   get_simple_extent_typeR<   R   t   NULLR)   t   SCALARt   SEL_NONEt   SEL_ALLR   t   SIMPLER
   R/   R   R	   R=   t   SEL_HYPERSLABSt   get_select_boundsR   t   onesR   R2   (   R   t	   sel_classt   sel_typeRQ   t	   topcornerR	   (    (   R’   R“   R”   R•   R   s1   /tmp/pip-build-isqEY4/h5py/h5py/_hl/selections.pyt   guess_shapeî  s<    	  
%(   R(   t
   __future__R    Ru   t	   six.movesR   t   numpyR   t    R   R   R   t   objectR   R   R   R   R   Ro   RU   R‡   R†   R¢   (    (    (    s1   /tmp/pip-build-isqEY4/h5py/h5py/_hl/selections.pyt   <module>   s    	FC.TY		!		