a
    Sic8                     @   s   d Z ddlZddlmZ ddlmZmZmZ dddZ	G d	d
 d
Z
G dd de
ZG dd de
ZG dd de
Zdd ZdS )z4
    High-level access to HDF5 dataspace selections
    N   )product   )h5sh5r	_selectorc                 C   s   t |ts|f}t|dkr|d }t |trD|j| kr@td|S t |tjrx|jj	dkrx|j| krntdt
|S t |tjr|du rtdt||j}| |jkrtdt| |d	S |dur|j}nt| }t|}||S )
a1   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.

    dataset
        A h5py.Dataset 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, MultiBlockSlices only
        Returns a SimpleSelection instance

    Indices, slices, ellipses, lists or boolean index arrays
        Returns a FancySelection instance.
    r   r   zMismatched selection shapebz-Boolean indexing array has incompatible shapeNz1Cannot apply a region reference without a datasetz,Reference shape does not match dataset shape)spaceid)
isinstancetuplelen	Selectionshape	TypeErrornpndarraydtypekindPointSelection	from_maskr   RegionReference
get_regionidr   r   create_simpleSelectormake_selection)r   argsdatasetargsidselectorspace r"   O/var/www/html/django/DPS/env/lib/python3.9/site-packages/h5py/_hl/selections.pyselect   s0    







r$   c                   @   sn   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	edd Z
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ur|| _ |j| _n2t|}|| _t|tjft| | _ | j   dS )z= Create a selection.  Shape may be None if spaceid is given. N)	_idr   _shaper   r   r   	UNLIMITEDr   
select_all)selfr   r	   r"   r"   r#   __init__p   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 )r&   r+   r"   r"   r#   r      s    zSelection.shapec                 C   s
   | j  S )z' Number of elements currently selected )r%   get_select_npointsr+   r"   r"   r#   nselect   s    zSelection.nselectc                 C   s   | j fS )z0 Shape of selection (always 1-D for this class) )r-   r+   r"   r"   r#   mshape   s    zSelection.mshapec                 C   s   | j S )z8Shape of array to read/write (always 1-D for this class))r.   r+   r"   r"   r#   array_shape   s    zSelection.array_shapec                 C   s   t || jkrtd|S )N7Broadcasting is not supported for point-wise selections)r   r-   r   r)   source_shaper"   r"   r#   expand_shape   s    zSelection.expand_shapec                 c   s"   t || jkrtd| jV  dS )z" Get an iterable for broadcasting r0   N)r   r-   r   r%   r1   r"   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#   __getitem__   s    zSelection.__getitem__)N)__name__
__module____qualname____doc__r*   propertyr   r   r-   r.   r/   r3   r4   r6   r"   r"   r"   r#   r   U   s   





r   c                       sP   e Zd ZdZd fdd	Zdd ZedddZd	d
 Zdd Z	dd Z
  ZS )r   z
        Represents a point-wise selection.  You can supply sequences of
        points to the three methods append(), prepend() and set(), or
        instantiate it with a single boolean array using from_mask().
    Nc                    s(   t  || |d ur$| |tj d S N)superr*   _perform_selectionr   
SELECT_SET)r)   r   r	   points	__class__r"   r#   r*      s    zPointSelection.__init__c                 C   sn   t j|ddd}t|jdkr.d|jd f|_| j tjkrDtj}t|dkr\| j	  n| j
|| dS )z7 Internal method which actually performs the selection Cu8)orderr   r   r   N)r   asarrayr   r   r%   get_select_typer   
SEL_POINTSr?   select_noneselect_elements)r)   r@   opr"   r"   r#   r>      s    z!PointSelection._perform_selectionc                 C   s>   t |tjr|jjdks tdt| }| |j||dS )z9Create a point-wise selection from a NumPy boolean array r   z4PointSelection.from_mask only works with bool arrays)r@   )	r
   r   r   r   r   r   	transposenonzeror   )clsmaskr	   r@   r"   r"   r#   r      s    zPointSelection.from_maskc                 C   s   |  |tj dS )z@ Add the sequence of points to the end of the current selection N)r>   r   SELECT_APPENDr)   r@   r"   r"   r#   append   s    zPointSelection.appendc                 C   s   |  |tj dS )zF Add the sequence of points to the beginning of the current selection N)r>   r   SELECT_PREPENDrQ   r"   r"   r#   prepend   s    zPointSelection.prependc                 C   s   |  |tj dS )z@ Replace the current selection with the given sequence of pointsN)r>   r   r?   rQ   r"   r"   r#   set   s    zPointSelection.set)NN)N)r7   r8   r9   r:   r*   r>   classmethodr   rR   rT   rU   __classcell__r"   r"   rA   r#   r      s   r   c                       sJ   e Zd ZdZedd Zedd Zd fdd	Zd	d
 Zdd Z	  Z
S )SimpleSelectionz A single "rectangular" (regular) selection composed of only slices
        and integer arguments.  Can participate in broadcasting.
    c                 C   s
   | j d S )z Shape of current selection r   )_selr+   r"   r"   r#   r.      s    zSimpleSelection.mshapec                 C   s$   | j d }tdd t| j|D S )N   c                 s   s   | ]\}}|s|V  qd S r<   r"   ).0xsr"   r"   r#   	<genexpr>       z.SimpleSelection.array_shape.<locals>.<genexpr>)rY   r   zipr.   )r)   scalarr"   r"   r#   r/      s    
zSimpleSelection.array_shapeNc                    sH   t  || |d ur|| _n&t| j}d| | jd| d| f| _d S )Nr   r   )F)r=   r*   rY   r   r   )r)   r   r	   Z	hyperslabrankrA   r"   r#   r*      s
    
zSimpleSelection.__init__c                 C   s   | j \}}}}t|}t|}g }td|d D ]b}	t|dksJ||	  rV|d q0| }
|
dkst||	  |
kr||
 q0td|| jf q0tdd |D rtd|| jf t	|ddd S )a  Match the dimensions of an array to be broadcast to the selection

        The returned shape describes an array of the same size as the input
        shape, but its dimensions

        E.g. with a dataset shape (10, 5, 4, 2), writing like this::

            ds[..., 0] = np.ones((5, 4))

        The source shape (5, 4) will expand to (1, 5, 4, 1).
        Then the broadcast method below repeats that chunk 10
        times to write to an effective shape of (10, 5, 4, 1).
        r   r   zCan't broadcast %s -> %sc                 S   s   g | ]}|d kqS rc   r"   )r[   nr"   r"   r#   
<listcomp>
  r_   z0SimpleSelection.expand_shape.<locals>.<listcomp>N)
rY   r   listrangerR   popr   r/   anyr   )r)   r2   startcountstepra   rd   Zremaining_src_dimsZeshapeidxtr"   r"   r#   r3      s    zSimpleSelection.expand_shapec                 c   s   | j dkr8t|dkr"td| | j  | jV  dS | j\}}}}t|}| |}tdd t	||D }t|}	|	dkr| jV  n^| j
 }
|
d| || t|	D ]8}tdd t	t|||||D }|
| |
V  qdS )	z Return an iterator over target dataspaces for broadcasting.

        Follows the standard NumPy broadcasting rules against the current
        selection shape (self.mshape).
        r"   r   zCan't broadcast %s to scalarNc                 s   s   | ]\}}|| V  qd S r<   r"   )r[   r\   yr"   r"   r#   r^   %  r_   z,SimpleSelection.broadcast.<locals>.<genexpr>rb   c                 s   s&   | ]\}}}}|| | | V  qd S r<   r"   )r[   r\   rq   zr]   r"   r"   r#   r^   .  r_   )r   r   r   r%   r(   rY   r   r3   r   r`   copyselect_hyperslabri   r   unravel_indexoffset_simple)r)   r2   rl   rm   rn   ra   rd   tshapechunksZnchunksr   ro   offsetr"   r"   r#   r4     s&    




$
zSimpleSelection.broadcast)NNr7   r8   r9   r:   r;   r.   r/   r*   r3   r4   rW   r"   r"   rA   r#   rX      s   

	'rX   c                       sJ   e Zd ZdZedd Zedd Zd fdd	Zd	d
 Zdd Z	  Z
S )FancySelectiona'  
        Implements advanced NumPy-style selection operations in addition to
        the standard slice-and-int behavior.

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

        Broadcasting is not supported for these selections.
    c                 C   s   | j S r<   )_mshaper+   r"   r"   r#   r.   ?  s    zFancySelection.mshapec                 C   s   | j S r<   )_array_shaper+   r"   r"   r#   r/   C  s    zFancySelection.array_shapeNc                    s8   t  || |d u r| j}|d u r(|}|| _|| _d S r<   )r=   r*   r   r|   r}   )r)   r   r	   r.   r/   rA   r"   r#   r*   G  s    zFancySelection.__init__c                 C   s   || j kstd|S Nz4Broadcasting is not supported for complex selections)r/   r   r1   r"   r"   r#   r3   P  s    
zFancySelection.expand_shapec                 c   s   || j kstd| jV  d S r~   )r/   r   r%   r1   r"   r"   r#   r4   U  s    
zFancySelection.broadcast)NNNrz   r"   r"   rA   r#   r{   3  s   


	r{   c                    s8    } }|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| 
  tj}|tjkrd| S |tjkrjS |tjkr fS |tjkrt	d|  dkrd| S dd  D \}| t|f  fdd	tfd
dt|D }t| 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 %srb   z Unrecognized selection method %sr   c                 s   s   | ]}t |V  qd S r<   )r   arrayr[   r\   r"   r"   r#   r^     r_   zguess_shape.<locals>.<genexpr>c                    sr   | dkrdS   }||  d7  <   }||  d8  < |   }|jt|t|tjd | } | 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   )rK   )rs   rt   r   r   SELECT_NOTBr,   )r   axisrl   rm   Z
masked_sidZ
N_leftover)Nbottomcornerboxshaper"   r#   
get_n_axis  s    zguess_shape.<locals>.get_n_axisc                 3   s   | ]} |V  qd S r<   r"   r   )r   r   r"   r#   r^     r_   )get_simple_extent_typerG   r   NULLSCALARSEL_NONESEL_ALLr   SIMPLEr   r,   r   r   rH   SEL_HYPERSLABSget_select_boundsr   onesri   r   )r   Z	sel_classZsel_typerd   Z	topcornerr   r"   )r   r   r   r   r   r#   guess_shape[  s8    	







r   )N)r:   numpyr   baser    r   r   r   r$   r   r   rX   r{   r   r"   r"   r"   r#   <module>
   s   
BN0`(