a
    yn„f<,  ã                   @  s®   d dl mZ d dlZd dlmZ d dlmZ d dlm	Z	m
Z
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
Ze ¡ Ze ¡ Zeƒ dddddœdd„ƒZdd„ ZdS )é    )ÚannotationsN)ÚNumpyBackend)Úhigh_level_function)ÚHighLevelContextÚensure_same_backendÚmaybe_posaxis)ÚNumpyMetadata)Úunknown_length)Úis_unknown_scalar)Úis_integer_likeÚregularize_axis)Ú	unflattenT)Ú	highlevelÚbehaviorÚattrsc                c  s   | fV  t | |||||ƒS )aÑ
  
    Args:
        array: Array-like data (anything #ak.to_layout recognizes).
        counts (int or array): Number of elements the new level should have.
            If an integer, the new level will be regularly sized; otherwise,
            it will consist of variable-length lists with the given lengths.
        axis (int): The dimension at which this operation is applied. The
            outermost dimension is `0`, followed by `1`, etc., and negative
            values count backward from the innermost: `-1` is the innermost
            dimension, `-2` is the next level up, etc.
        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.

    Returns an array with an additional level of nesting. This is roughly the
    inverse of #ak.flatten, where `counts` were obtained by #ak.num (both with
    `axis=1`).

    For example,

        >>> original = ak.Array([[0, 1, 2], [], [3, 4], [5], [6, 7, 8, 9]])
        >>> counts = ak.num(original)
        >>> array = ak.flatten(original)
        >>> counts
        <Array [3, 0, 2, 1, 4] type='5 * int64'>
        >>> array
        <Array [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] type='10 * int64'>
        >>> ak.unflatten(array, counts)
        <Array [[0, 1, 2], [], [3, ...], [5], [6, 7, 8, 9]] type='5 * var * int64'>

    An inner dimension can be unflattened by setting the `axis` parameter, but
    operations like this constrain the `counts` more tightly.

    For example, we can subdivide an already divided list:

        >>> original = ak.Array([[1, 2, 3, 4], [], [5, 6, 7], [8, 9]])
        >>> ak.unflatten(original, [2, 2, 1, 2, 1, 1], axis=1).show()
        [[[1, 2], [3, 4]],
         [],
         [[5], [6, 7]],
         [[8], [9]]]

    But the counts have to add up to the lengths of those lists. We can't mix
    values from the first `[1, 2, 3, 4]` with values from the next `[5, 6, 7]`.

        >>> ak.unflatten(original, [2, 1, 2, 2, 1, 1], axis=1).show()
        ValueError: while calling
            ak.unflatten(
                array = <Array [[1, 2, 3, 4], [], ..., [8, 9]] type='4 * var * int64'>
                counts = [2, 1, 2, 2, 1, 1]
                axis = 1
                highlevel = True
                behavior = None
            )
        Error details: structure imposed by 'counts' does not fit in the array or partition at axis=1

    Also note that new lists created by this function cannot cross partitions
    (which is only possible at `axis=0`, anyway).

    See also #ak.num and #ak.flatten.
    )Ú_impl)ÚarrayÚcountsÚaxisr   r   r   © r   úb/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/awkward/operations/ak_unflatten.pyr      s    Cr   c           
   
     s  t ˆƒ‰t||d��:}t|j| ddd�|jˆddddd�ƒ\}}W d   ƒ n1 sT0    Y  t|ƒrŠt|ƒsv|tu r|t‰ntˆƒ‰d ‰nÜ|jrž|j	sž| 
¡ }|j	rÚ|jjs´|jjrÚ|jdd�‰tjj|d	d
dd� ¡ ‰n"|jsæ|jrô| ¡ ‰d‰ntdƒ‚ˆjdk�rtdƒ‚t ˆjtj¡�s*tdƒ‚|jjjˆjd tjd�‰d	ˆd	< |jjjˆˆdd … d� ‡‡‡‡fdd„‰ˆd	k�s”t|ˆdƒd	k�ržˆ|ƒ}	n(dd„ ‰‡ ‡‡‡fdd„‰ tj  !|ˆ ¡}	ˆd u�rˆjtu�rˆjdk�röˆd	 d	k�stdˆ› �ƒ‚|j"|	|d�S )N)r   r   FÚerror)Úallow_recordÚprimitive_policyTzpass-through)r   Zallow_unknownr   Zstring_policy©Z
valid_whenr   éÿÿÿÿ)r   r   z@counts must be an integer or a one-dimensional array of integersé   zcounts must be one-dimensionalzcounts must be integers©Údtype)Z	maybe_outc                   sH  | j j}tˆtƒsˆtu r\ˆturL| jturLdˆ  krB| jksLn tdƒ‚tj 	| ˆ¡}nè|j
ˆ| | | j¡g¡dd�d d }ˆjturÆ| jturÆt|ƒsÆ|ˆjks¸ˆ| | jkrÆtdˆ › �ƒ‚ˆd |d … }ˆ|d … | | j¡ ‰tj tj |¡| ¡}tˆttjfƒ�sDtjj|jˆtjd�|d�}tjj||d	d
�}|S )Nr   z-too large counts for array or negative countsÚright©Zsider   úMstructure imposed by 'counts' does not fit in the array or partition at axis=r   )ZnplikeFr   )ÚbackendÚindex_nplikeÚ
isinstanceÚintr	   ÚlengthÚ
ValueErrorÚakÚcontentsÚRegularArrayÚsearchsortedZasarrayZshape_item_as_indexÚsizer
   ÚListOffsetArrayÚindexÚIndex64ÚboolÚnpZbool_ZIndex8Zint8ÚByteMaskedArray)Úlayoutr#   ÚoutÚpositionÚoffsetsr.   )r   r   Úcurrent_offsetsÚmaskr   r   Úunflatten_this_layout˜   sj    ÿþýýÿûúùÿÿþýûú	ÿÿÿ
þþz$_impl.<locals>.unflatten_this_layoutc                   s~   |j s|jr6|j|j ˆ |j r&ˆd nˆˆˆˆ¡d�S |jsB|jrf|j‡ ‡‡‡‡fdd„|jD ƒd�S |jsr|j	rv|S t
‚d S )Nr   )Úcontentc              	     s   g | ]}|  ˆ ˆˆˆˆ¡‘qS r   )Ú_recursively_apply)Ú.0Úc©ÚactionÚdepthÚdepth_contextÚlateral_contextÚoptionsr   r   Ú
<listcomp>â   s   ùûz?_impl.<locals>.recursively_apply_to_content.<locals>.<listcomp>)r)   )Úis_listÚ	is_optionÚcopyr:   r;   Zis_unionZ	is_recordr)   Úis_numpyÚ
is_unknownÚAssertionError)r?   r3   r@   rA   rB   rC   Úkwargsr   r>   r   Úrecursively_apply_to_contentÓ   s&    ûÿ	øÿz+_impl.<locals>.recursively_apply_to_contentc           
        sþ   |   d¡} t| ˆ|ƒ}||kræ| jræ|  d¡}|j}ˆ|jd |d … ƒ}t|tjj	ƒrb|jj}n"t|tjj
ƒr~| d¡j}n|j}|jj|j|jdd�d }	|jjrÈ|j |j|	 |¡sÈtdˆ› �ƒ‚d|	d< tj tj |	¡|¡S ˆˆ | |fi |¤ŽS d S )	NFTr   r   r    r   r!   r   )Z	to_packedr   rE   Zto_ListOffsetArray64r6   r:   r$   r(   r)   r2   r*   r#   r+   ÚdataZ
known_dataZarray_equalr'   r-   r.   r/   )
r3   r@   r"   rK   ZposaxisZlistoffsetarrayZouteroffsetsr:   ZinneroffsetsZ	positions)Úapplyr   rL   r9   r   r   rN   ò   sB    

ÿ

ÿýÿÿ
ÿþÿÿz_impl.<locals>.applyr!   )r   )#r   r   r   Úunwrapr   r
   r	   r%   Z
is_indexedrF   Úprojectr:   rH   rI   Zmask_as_boolr(   Ú
operationsZ	fill_noneZto_backend_arrayr'   Úndimr1   Z
issubdtyper   Úintegerr"   r#   Úemptyr,   Zint64Zcumsumr   Z_doZrecursively_applyÚwrap)
r   r   r   r   r   r   Úctxr3   Zmaybe_counts_layoutr4   r   )rN   r   r   r7   r8   rL   r9   r   r   ]   sˆ    ûþ&ÿþÿþÿÿÿÿ6
)ÿþý
ýÿÿr   )r   )Ú
__future__r   Zawkwardr(   Zawkward._backends.numpyr   Zawkward._dispatchr   Zawkward._layoutr   r   r   Zawkward._nplikes.numpy_liker   Zawkward._nplikes.shaper	   Zawkward._nplikes.typetracerr
   Zawkward._regularizer   r   Ú__all__Úinstancer1   Znumpy_backendr   r   r   r   r   r   Ú<module>   s   H