a
    yn„f°@  ã                	   @  sþ   d dl mZ d dlZd dlZd dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ d dlmZmZ d d	lmZ d d
lmZmZ d dlmZ d dlmZmZ dZe ¡ Ze ¡ Zeƒ d"dddddddœdd„ƒZ dd„ Z!ddddddœdd„Z"d d!„ Z#dS )#é    )ÚannotationsN)Úregularize_backend)Úhigh_level_function)Úwrap_layout)Ú	ArrayLike)ÚNumpy)Ú	NumpyLikeÚNumpyMetadata)ÚPlaceholderArray)Ú	ShapeItemÚunknown_length)Ú
is_integer)Úindex_to_dtypeÚregularize_buffer_key)Úfrom_buffersú{form_key}-{attribute}Úcpuú<FT)ÚbackendÚ	byteorderÚallow_noncanonical_formÚ	highlevelÚbehaviorÚattrsc          
      C  s   t | ||||||||	|ƒ
S )u½  
    Args:
        form (#ak.forms.Form or str/dict equivalent): The form of the Awkward
            Array to reconstitute from named buffers.
        length (int): Length of the array. (The output of this function is always
            single-partition.)
        container (Mapping, such as dict): The str â†’ Python buffers that
            represent the decomposed Awkward Array. This `container` is only
            assumed to have a `__getitem__` method that accepts strings as keys.
        buffer_key (str or callable): Python format string containing
            `"{form_key}"` and/or `"{attribute}"` or a function that takes these
            as keyword arguments and returns a string to use as a key for a buffer
            in the `container`.
        backend (str): Library to use to generate values that are
            put into the new array. The default, cpu, makes NumPy
            arrays, which are in main memory (e.g. not GPU). If all the values in
            `container` have the same `backend` as this, they won't be copied.
        byteorder (`"<"`, `">"`): Endianness of buffers read from `container`.
            If the byteorder does not match the current system byteorder, the
            arrays will be copied.
        allow_noncanonical_form (bool): If True, non-canonical forms will be
            simplified to produce arrays with canonical layouts; otherwise,
            an exception will be thrown for such forms.
        highlevel (bool): If True, return an #ak.Array; otherwise, return
            a low-level #ak.contents.Content subclass.
        behavior (None or dict): Custom #ak.behavior for the output array, if
            high-level.
        attrs (None or dict): Custom attributes for the output array, if
            high-level.

    Reconstitutes an Awkward Array from a Form, length, and a collection of memory
    buffers, so that data can be losslessly read from file formats and storage
    devices that only map names to binary blobs (such as a filesystem directory).

    The first three arguments of this function are the return values of
    #ak.to_buffers, so a full round-trip is

        >>> reconstituted = ak.from_buffers(*ak.to_buffers(original))

    The `container` argument lets you specify your own Mapping, which might be
    an interface to some storage format or device (e.g. h5py). It's okay if
    the `container` dropped NumPy's `dtype` and `shape` information, leaving
    raw bytes, since `dtype` and `shape` can be reconstituted from the
    #ak.forms.NumpyForm.
    If the values of `container` are recognised as arrays by the given backend,
    a view over their existing data will be used, where possible.

    The `buffer_key` should be the same as the one used in #ak.to_buffers.

    When `allow_noncanonical_form` is set to True, this function readily accepts
    non-simplified forms, i.e. forms which will be simplified by Awkward Array
    into "canonical" representations, e.g. `option[option[...]]` â†’ `option[...]`.
    Such forms can be produced by the low-level ArrayBuilder `snapshot()` method.
    Given that Awkward Arrays must have canonical layouts, it follows that
    invoking this function with `allow_noncanonical_form` may produce arrays
    whose forms differ to the input form.

    In order for a non-simplified form to be considered valid, it should be one
    that the #ak.contents.Content layout classes could produce iff. the
    simplification rules were removed.


    See #ak.to_buffers for examples.
    )Ú_impl)
ÚformÚlengthÚ	containerÚ
buffer_keyr   r   r   r   r   r   © r   úe/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/awkward/operations/ak_from_buffers.pyr      s    Nör   c
                 C  s¬   t |ƒ}t| tƒr<tjj | ¡r.tj | ¡} qRtj 	| ¡} nt| t
ƒrRtj | ¡} t|ƒrb|dksjtdƒ‚t| tjjƒs€tdƒ‚t|ƒ}
t| |||
|||	ƒ}t||||d�S )Nr   z0'length' argument must be a non-negative integerzL'form' argument must be a Form or its Python dict/JSON string representation)r   r   r   )r   Ú
isinstanceÚstrÚakÚtypesÚ	numpytypeZis_primitiveÚformsÚ	NumpyFormÚ	from_jsonÚdictÚ	from_dictr   Ú	TypeErrorZFormr   Ú_reconstituter   )r   r   r   r   r   r   r   r   r   ÚsimplifyÚgetkeyÚoutr   r   r    r   u   s     

ÿr   r   znp.dtyper   r"   r   )ÚnplikeÚdtypeÚcountr   Úreturnc                 C  sÀ   |t u rt| t f|ƒS t|tƒr8|jt u r8t| |f|ƒS t|tƒsL|  |¡r�| j| |¡ddd�}|j|k r„td|j› d|› d�ƒ‚|d |… S | j|||d�}|t	j
jkr¸|jdd�S |S d S )	N)éÿÿÿÿF)ÚshapeÚcopyzsize of array (z) is less than size of form (ú))r1   r2   )Zinplace)r   r
   r!   ÚsizeZis_own_arrayÚreshapeÚviewr+   Z
frombufferr#   Z_utilZnative_byteorderÚbyteswap)r0   Úbufferr1   r2   r   Úarrayr   r   r    Ú_from_bufferš   s    
ÿr>   c                   s  t | tjjƒr.ˆdkr$tdˆ› �ƒ‚tj ¡ S t | tjjƒr´tjj	 
| j¡}ˆˆ| dƒ }ˆt | j¡ }	tˆ j|||	ˆd�}
| jdkr ˆ j |
ˆg| j¢R ¡}
tjj|
| jˆ d�S t | tjjƒ�r t| jˆˆˆˆ ˆˆƒ}ˆrêtjjj}ntjj}||| jd�S t | tjjƒ�r¬ˆˆ| dƒ }ˆtu �r.t}ntt ˆd	 ¡ƒ}tˆ j|t| j |ˆd�}t| jˆˆˆˆ ˆˆƒ}ˆ�r‚tjj j}ntjj }|tj! "|¡|| j#ˆ| j$| jd�S t | tjj%ƒ�r0ˆˆ| dƒ }tˆ j|t| j ˆˆd�}t| jˆˆˆˆ ˆˆƒ}ˆ�rtjj&j}ntjj&}|tj! "|¡|| j#| jd�S t | tjj'ƒ�rêˆˆ| d
ƒ }tˆ j|t| j! ˆˆd�}t |t(ƒ�rzt}n(t)|ƒdk�rŒdnt*dˆ j *|¡d ƒ}t| j|ˆˆˆ ˆˆƒ}ˆ�rÊtjj+j}ntjj+}|tj! "|¡|| jd�S t | tjj,ƒ�r¦ˆˆ| d
ƒ }tˆ j|t| j! ˆˆd�}t |t(ƒ�r4t}n*t)|ƒdk�rFdnˆ j -ˆ j *|¡d ¡}t| j|ˆˆˆ ˆˆƒ}ˆ�r†tjj.j}ntjj.}|tj! "|¡|| jd�S t | tjj/ƒ�r~ˆˆ| dƒ }ˆˆ| dƒ }tˆ j|t| j0 ˆˆd�}tˆ j|t| j1 ˆˆd�}t |t(ƒ�rt}n*|||k }t)|ƒdk�r6dn
ˆ j *|¡}t| j|ˆˆˆ ˆˆƒ}tjj2tj! "|¡tj! "|¡|| jd�S t | tjj3ƒ�rˆˆ| dƒ }tˆ j|t| j4 ˆd ˆd�}t |t(ƒ�rÌt}nt)|ƒdk�rÞdn|d }t| j|ˆˆˆ ˆˆƒ}tjj5tj! "|¡|| jd�S t | tjj6ƒ�r`ˆ| j7 }t| j|ˆˆˆ ˆˆƒ}tjj8|| j7ˆ| jd�S t | tjj9ƒ�r²‡ ‡‡‡‡‡fdd„| jD ƒ}tjj:|| j;�r¢d n| j<ˆ| jd�S t | tjj=ƒ�rðˆˆ| dƒ }ˆˆ| d
ƒ }tˆ j|t| j> ˆˆd�}tˆ j|t| j! ˆˆd�}t |t(ƒ�s*t |t(ƒ�r<tgt)| jƒ ‰nVg ‰t?t)| jƒƒD ]B}|||k }t)|ƒdk�rxˆ @d¡ nˆ @ˆ j *|¡d ¡ �qN‡ ‡‡‡‡‡fdd„tA| jƒD ƒ}ˆ�rÆtjjBj}ntjjB}|tj! "|¡tj! "|¡|| jd�S tCdtDtE| ƒƒ ƒ‚d S )Nr   z+EmptyForm node, but the expected length is Údata)r1   r2   r   r   )Ú
parametersr   )r@   Úmaskg       @Úindexé   ÚstartsÚstopsÚoffsetsr4   c              
     s    g | ]}t |ˆˆˆˆ ˆˆƒ‘qS r   ©r,   )Ú.0Úcontent)r   r   r   r.   r   r-   r   r    Ú
<listcomp>“  s   ýÿz!_reconstitute.<locals>.<listcomp>Útagsc              
     s(   g | ] \}}t |ˆ| ˆˆˆ ˆˆƒ‘qS r   rG   )rH   ÚirI   )r   r   r   r.   Úlengthsr-   r   r    rJ   »  s   ýÿzunexpected form node type: )Fr!   r#   r&   Z	EmptyFormÚ
ValueErrorÚcontentsZ
EmptyArrayr'   r$   r%   Zprimitive_to_dtypeZ	primitiveÚmathÚprodZinner_shaper>   r0   r9   Z
NumpyArrayÚ_parametersZUnmaskedFormr,   rI   ZUnmaskedArrayZ
simplifiedZBitMaskedFormr   ÚintÚceilZindex_npliker   rA   ZBitMaskedArrayrB   ÚIndexZ
valid_whenZ	lsb_orderZByteMaskedFormZByteMaskedArrayZIndexedOptionFormr
   ÚlenÚmaxZIndexedOptionArrayZIndexedFormZindex_as_shape_itemZIndexedArrayZListFormrD   rE   Z	ListArrayZListOffsetFormrF   ZListOffsetArrayZRegularFormr8   ZRegularArrayZ
RecordFormZRecordArrayÚis_tupleÚfieldsZ	UnionFormrK   ÚrangeÚappendÚ	enumerateZ
UnionArrayÚAssertionErrorr"   Útype)r   r   r   r.   r   r   r-   r1   Z	raw_arrayZreal_lengthr?   rI   ÚmakeZnext_lengthrA   rB   Z
raw_array1Z
raw_array2rD   rE   Zreduced_stopsrF   rO   rK   ÚtagZselected_indexr   )r   r   r   r.   r   rM   r-   r    r,   »   sÚ   
û
ÿÿ
ûÿ
ú	ûÿ
üû&ÿÿ
ýû
ÿÿýÿ
ýûûÿÿ

üûÿ
ý
ÿüüüûûü

ür,   )r   )$Ú
__future__r   rP   Zawkwardr#   Zawkward._backends.dispatchr   Zawkward._dispatchr   Zawkward._layoutr   Zawkward._nplikes.array_liker   Zawkward._nplikes.numpyr   Zawkward._nplikes.numpy_liker   r	   Zawkward._nplikes.placeholderr
   Zawkward._nplikes.shaper   r   Zawkward._regularizer   Zawkward.forms.formr   r   Ú__all__ÚinstanceÚnpÚnumpyr   r   r>   r,   r   r   r   r    Ú<module>   s8    üõ[%!