3
½ìœ](L  ã               @   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d„ ZG d	d
„ d
eƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )z4
    High-level access to HDF5 dataspace selections
é    )Úabsolute_importN)Úxrangeé   )Úh5sÚh5rc             C   s,  t |tƒs|f}t|ƒdkr¦|d }t |tƒrD|j| kr@tdƒ‚|S t |tjƒrp|jj	dkrpt
| ƒ}||  |S t |tjƒr¦tj||ƒ}| |jkrštdƒ‚t| |d�S xp|D ]h}t |tƒ oÂ|tk	r¬y(t|ƒ t |tjƒrê|jdkrêtƒ ‚W q¬ tk
�r   t| ƒ}||  |S X q¬W t| ƒ}||  |S )a"   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.
    é   r   zMismatched selection shapeÚbz,Reference shape does not match dataset shape)Úspaceid)r   )Ú
isinstanceÚtupleÚlenÚ	SelectionÚshapeÚ	TypeErrorÚnpÚndarrayÚdtypeÚkindÚPointSelectionr   ZRegionReferenceZ
get_regionÚsliceÚEllipsisÚintÚ	ExceptionÚFancySelectionÚSimpleSelection)r   ÚargsÚdsidÚargÚselÚsidÚa© r!   ú3/tmp/pip-build-pcqn41bp/h5py/h5py/_hl/selections.pyÚselect   s<    






r#   c               @   s    e Zd ZdZdd„ Zdd„ ZdS )Ú_RegionProxyzû
        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 )z& Supply a h5py.h5d.DatasetID instance N)Úid)Úselfr   r!   r!   r"   Ú__init__m   s    z_RegionProxy.__init__c             C   s(   t | jj|| jƒ}tj| jdtj|jƒS )zˆ Takes arbitrary selection terms and produces a RegionReference
        object.  Selection must be compatible with the dataset.
        Ú.)r#   r%   r   r   ÚcreateZDATASET_REGION)r&   r   Z	selectionr!   r!   r"   Ú__getitem__q   s    z_RegionProxy.__getitem__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r'   r*   r!   r!   r!   r"   r$   a   s   
r$   c               @   sZ   e Zd ZdZddd„Zedd„ ƒZedd„ ƒZed	d
„ ƒZedd„ ƒZ	dd„ Z
dd„ ZdS )r   a€  
        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).
    Nc             C   sN   |dk	r|| _ |j| _n2t|ƒ}|| _tj|tjft|ƒ ƒ| _ | j jƒ  dS )z= Create a selection.  Shape may be None if spaceid is given. N)	Ú_idr   Ú_shaper   r   Zcreate_simpleZ	UNLIMITEDr   Ú
select_all)r&   r   r	   r!   r!   r"   r'   “   s    
zSelection.__init__c             C   s   | j S )z SpaceID instance )r/   )r&   r!   r!   r"   r%   ž   s    zSelection.idc             C   s   | j S )z Shape of whole dataspace )r0   )r&   r!   r!   r"   r   £   s    zSelection.shapec             C   s
   | j jƒ S )z' Number of elements currently selected )r/   Úget_select_npoints)r&   r!   r!   r"   Únselect¨   s    zSelection.nselectc             C   s   | j fS )z0 Shape of selection (always 1-D for this class) )r3   )r&   r!   r!   r"   Úmshape­   s    zSelection.mshapec             c   s$   t j|ƒ| jkrtdƒ‚| jV  dS )z" Get an iterable for broadcasting z7Broadcasting is not supported for point-wise selectionsN)r   Úproductr3   r   r/   )r&   Útarget_shaper!   r!   r"   Ú	broadcast²   s    zSelection.broadcastc             C   s   t dƒ‚d S )Nz$This class does not support indexing)ÚNotImplementedError)r&   r   r!   r!   r"   r*   ¸   s    zSelection.__getitem__)N)r+   r,   r-   r.   r'   Úpropertyr%   r   r3   r4   r7   r*   r!   r!   r!   r"   r   x   s   
r   c               @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )r   zÂ
        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   sn   t j|ddd�}t|jƒdkr.d|jd f|_| jjƒ tjkrDtj}t|ƒdkr\| jj	ƒ  n| jj
||ƒ dS )z7 Internal method which actually performs the selection ÚCÚu8)Úorderr   r   r   N)r   Zasarrayr   r   r/   Úget_select_typer   Ú
SEL_POINTSÚ
SELECT_SETÚselect_noneZselect_elements)r&   ÚpointsÚopr!   r!   r"   Ú_perform_selectionÃ   s    z!PointSelection._perform_selectionc             C   sP   t |tjƒo|jjdks tdƒ‚|j| jks4tdƒ‚tj|jƒ ƒ}| j	|ƒ | S )z9 Perform point-wise selection from a NumPy boolean array r   z6PointSelection __getitem__ only works with bool arraysz-Boolean indexing array has incompatible shape)
r
   r   r   r   r   r   r   Z	transposeÚnonzeroÚset)r&   r   rA   r!   r!   r"   r*   Ñ   s    
zPointSelection.__getitem__c             C   s   | j |tjƒ dS )z@ Add the sequence of points to the end of the current selection N)rC   r   ZSELECT_APPEND)r&   rA   r!   r!   r"   ÚappendÜ   s    zPointSelection.appendc             C   s   | j |tjƒ dS )zF Add the sequence of points to the beginning of the current selection N)rC   r   ZSELECT_PREPEND)r&   rA   r!   r!   r"   Úprependà   s    zPointSelection.prependc             C   s   | j |tjƒ dS )z@ Replace the current selection with the given sequence of pointsN)rC   r   r?   )r&   rA   r!   r!   r"   rE   ä   s    zPointSelection.setN)	r+   r,   r-   r.   rC   r*   rF   rG   rE   r!   r!   r!   r"   r   »   s   r   c               @   s4   e Zd ZdZedd„ ƒZdd„ Zdd„ Zdd	„ Zd
S )r   z‰ A single "rectangular" (regular) selection composed of only slices
        and integer arguments.  Can participate in broadcasting.
    c             C   s   | j S )z Shape of current selection )Ú_mshape)r&   r!   r!   r"   r4   ï   s    zSimpleSelection.mshapec             O   sF   t j| |f|ž|Ž t| jƒ}d| | jd| d| f| _| j| _d S )Nr   r   F)r   )r   )F)r   r'   r   r   Ú_selrH   )r&   r   r   ÚkwdsÚrankr!   r!   r"   r'   ô   s    
zSimpleSelection.__init__c             C   sœ   t |tƒs|f}| jf krLt|ƒdkr>|d tf fkr>tdƒ‚| jjƒ  | S t| j|ƒ\}}}}| jj	|||ƒ ||||f| _
tdd„ t||ƒD ƒƒ| _| S )Nr   z7Invalid index for scalar dataset (only ..., () allowed)c             s   s   | ]\}}|s|V  qd S )Nr!   )Ú.0ÚxÚyr!   r!   r"   ú	<genexpr>  s    z.SimpleSelection.__getitem__.<locals>.<genexpr>)r
   r   r   r   r   r   r/   r1   Ú_handle_simpleÚselect_hyperslabrI   ÚziprH   )r&   r   ÚstartÚcountÚstepÚscalarr!   r!   r"   r*   ú   s    


zSimpleSelection.__getitem__c             c   s¨  | j f kr:tj|ƒdkr$td| ƒ‚| jjƒ  | jV  dS | j\}}}}t|ƒ}t|ƒ}g }xtt	d|d ƒD ]b}	t|ƒdks†||	  r’|j
dƒ ql|jƒ }
|
dks°||	  |
kr¼|j
|
ƒ qltd|| jf ƒ‚qlW tdd„ |D ƒƒrötd|| jf ƒ‚|jƒ  t|ƒ}tdd	„ t||ƒD ƒƒ}ttj|ƒƒ}|dk�r@| jV  nd| jjƒ }|jd| ||ƒ xFt	|ƒD ]:}	td
d	„ ttj|	|ƒ|||ƒD ƒƒ}|j|ƒ |V  �qfW dS )z· Return an iterator over target dataspaces for broadcasting.

        Follows the standard NumPy broadcasting rules against the current
        selection shape (self.mshape).
        r   zCan't broadcast %s to scalarNr   zCan't broadcast %s -> %sc             S   s   g | ]}|d k‘qS )r   r!   )rL   Únr!   r!   r"   ú
<listcomp>-  s    z-SimpleSelection.broadcast.<locals>.<listcomp>c             s   s   | ]\}}|| V  qd S )Nr!   )rL   rM   rN   r!   r!   r"   rO   5  s    z,SimpleSelection.broadcast.<locals>.<genexpr>c             s   s&   | ]\}}}}|| | | V  qd S )Nr!   )rL   rM   rN   ÚzÚsr!   r!   r"   rO   >  s    )r   )r   r   r5   r   r/   r1   rI   r   Úlistr   rF   Úpopr4   ÚanyÚreverser   rR   r   ÚcopyrQ   Zunravel_indexZoffset_simple)r&   r6   rS   rT   rU   rV   rK   ÚtargetZtshapeÚidxÚtÚchunksZnchunksr   Úoffsetr!   r!   r"   r7     s>    




$
zSimpleSelection.broadcastN)	r+   r,   r-   r.   r9   r4   r'   r*   r7   r!   r!   r!   r"   r   é   s
   r   c               @   s4   e Zd ZdZedd„ ƒZdd„ Zdd„ Zdd	„ Zd
S )r   a(  
        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)rH   )r&   r!   r!   r"   r4   O  s    zFancySelection.mshapec             O   s    t j| |f|ž|Ž | j| _d S )N)r   r'   r   rH   )r&   r   r   rJ   r!   r!   r"   r'   S  s    zFancySelection.__init__c                s‚  t |tƒs|f}t|t| jƒƒ}i }x¼t|ƒD ]°\}}t |tƒs.t|dƒr||jt	jdƒkr|t|jƒdkrpt
dƒ‚|jƒ d }yt|ƒ||< W n t
k
r    Y q.X t|ƒ}t|d d… |dd … ƒ}tdd„ |D ƒƒr.t
dƒ‚q.W t|ƒdkröt
d	ƒ‚t|ƒdk�rt
d
ƒ‚tt|jƒ ƒd ƒ‰ t‡ fdd„|jƒ D ƒƒ�sHt
d| ƒ‚ˆ dk�r¤g }xxtˆ ƒD ]>}t|ƒ}x$tj|ƒD ]\}	}
|
| ||	< �qxW |j|ƒ �q`W n,t|ƒ}x|D ]}	tddƒ||	< �q²W |g}| jjƒ  x@t|ƒD ]4\}}t| j|ƒ\}}}}| jj|||tjd� �qäW t|ƒ}xDtt|ƒƒD ]4}||k�rRt|| ƒ||< n|| �r2d||< �q2W tdd„ |D ƒƒ| _d S )Nr   Úboolr   z#Boolean indexing arrays must be 1-Dr   c             s   s   | ]\}}||kV  qd S )Nr!   )rL   ZfstZsndr!   r!   r"   rO   n  s    z-FancySelection.__getitem__.<locals>.<genexpr>z-Indexing elements must be in increasing orderzMOnly one indexing vector or array is currently allowed for advanced selectionz Advanced selection inappropriatec             3   s   | ]}t |ƒˆ kV  qd S )N)r   )rL   rM   )Úvectorlengthr!   r"   rO   w  s    z3All sequence arguments must have the same length %s)rB   c             s   s   | ]}|d kr|V  qdS )r   Nr!   )rL   rM   r!   r!   r"   rO   ž  s    éÿÿÿÿrg   )r
   r   Ú_expand_ellipsisr   r   Ú	enumerater   Úhasattrr   r   r   rD   r[   rR   r]   ÚvaluesÚallr   ÚsixÚ	iteritemsrF   r/   r@   rP   rQ   r   Z	SELECT_ORrH   )r&   r   Zsequenceargsra   r   Zlist_argÚadjacentZ	argvectorÚentryÚpositionÚseqZvectorrS   rT   rU   rV   r4   r!   )rf   r"   r*   W  s^    






zFancySelection.__getitem__c             c   s   || j kstdƒ‚| jV  d S )Nz4Broadcasting is not supported for complex selections)r4   r   r/   )r&   r6   r!   r!   r"   r7      s    
zFancySelection.broadcastN)	r+   r,   r-   r.   r9   r4   r'   r*   r7   r!   r!   r!   r"   r   C  s
   
Ir   c             C   s¨   t dd„ | D ƒƒ}|dkr$tdƒ‚n|dkrBt| ƒ|krB| tf } g }t| ƒ}x@| D ]8}|tkr‚|jtdddƒf|| d  ƒ qT|j|ƒ qTW t|ƒ|kr¤tdƒ‚|S )z7 Expand ellipsis objects and fill in missing axes.
    c             s   s   | ]}|t krd V  qdS )r   N)r   )rL   r   r!   r!   r"   rO   ¨  s    z#_expand_ellipsis.<locals>.<genexpr>r   zOnly one ellipsis may be used.r   NzArgument sequence too long)ÚsumÚ
ValueErrorr   r   Úextendr   rF   r   )r   rK   Zn_elZ
final_argsZn_argsr   r!   r!   r"   rh   ¥  s    


"rh   c             C   sØ   t |t| ƒƒ}g }g }g }g }xœt|| ƒD ]Ž\}}t|tƒrRt||ƒ\}}	}
d}n>ytt|ƒ|ƒ\}}	}
d}W n  tk
rŽ   td| ƒ‚Y nX |j	|ƒ |j	|	ƒ |j	|
ƒ |j	|ƒ q*W t
|ƒt
|ƒt
|ƒt
|ƒfS )aU   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.
    FTz.Illegal index "%s" (must be a slice or number))rh   r   rR   r
   r   Ú_translate_sliceÚ_translate_intr   r   rF   r   )r   r   rS   rT   rU   rV   r   ÚlengthrM   rN   rY   rZ   r!   r!   r"   rP   ¼  s&    	



rP   c             C   sF   | dk r||  } d|   ko"|k n  s<t d| |d f ƒ‚| ddfS )zk Given an integer index, return a 3-tuple
        (start, count, step)
        for hyperslab selection
    r   zIndex (%s) out of range (0-%s)r   )rt   )Úexprx   r!   r!   r"   rw   Ý  s
    rw   c             C   sN   | j |ƒ\}}}|dk r$td| ƒ‚||k r0dS d|| d |  }|||fS )z Given a slice object, return a 3-tuple
        (start, count, step)
        for use with the hyperslab selection routines
    r   zStep must be >= 1 (got %d)r   )r   r   r   )Úindicesrt   )ry   rx   rS   ÚstoprU   rT   r!   r!   r"   rv   ê  s    rv   c                s8  ˆj ƒ }ˆjƒ }|tjkrdS |tjkrH|tjkr6dS |tjkr^tƒ S n|tjkr^t	d| ƒ‚ˆ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| ƒ‚ˆ dkrÈd| S dd„ ˆjƒ D ƒ\‰}|ˆ tj|fƒ ‰‡ ‡‡fdd„‰t‡‡fd	d„t|ƒD ƒƒ}tj|ƒˆ k�r4ˆ fS |S )a0   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
    NzUnrecognized dataspace class %sr   z Unrecognized selection method %sc             s   s   | ]}t j|ƒV  qd S )N)r   Úarray)rL   rM   r!   r!   r"   rO   .  s    zguess_shape.<locals>.<genexpr>c                sr   ˆ| dkrdS ˆj ƒ }||  d7  < ˆj ƒ }||  d8  < | j ƒ }|jt|ƒt|ƒtjd� |jƒ }ˆ | S )aä   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.
        r   )rB   )r_   rQ   r   r   ZSELECT_NOTBr2   )r   ZaxisrS   rT   Z
masked_sidZ
N_leftover)ÚNÚbottomcornerÚboxshaper!   r"   Ú
get_n_axis3  s    zguess_shape.<locals>.get_n_axisc             3   s   | ]}ˆ ˆ|ƒV  qd S )Nr!   )rL   rM   )r€   r   r!   r"   rO   O  s    )r   )r   )Zget_simple_extent_typer=   r   ZNULLZSCALARZSEL_NONEZSEL_ALLr   ZSIMPLEr   r2   r   r   r>   ZSEL_HYPERSLABSZget_select_boundsr   Zonesr   r5   )r   Z	sel_classZsel_typerK   Z	topcornerr   r!   )r}   r~   r   r€   r   r"   Úguess_shapeþ  s<    	


 
 





r�   )r.   Ú
__future__r   rm   Z	six.movesr   Znumpyr   Ú r   r   r#   Úobjectr$   r   r   r   r   rh   rP   rw   rv   r�   r!   r!   r!   r"   Ú<module>   s    FC.Zb!