a
    yn„fÉ'  ã                	   @  s.  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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 d dlmZ d dlmZ dZe  ¡ Z!e  ¡ Z"e  ¡ Z#e  ¡ Z$eƒ ddddddddœdd„ƒZ%dd„ Z&dd„ Z'dd„ Z(dd „ Z)d!d"œd#d$„Z*dS )%é    )Úannotations)ÚIterable)ÚdateÚdatetimeÚtime)ÚNumber)ÚAny)Ú_extN)ÚNumpyBackend)ÚTypeTracerBackend)Úhigh_level_function)ÚCupy)ÚJax)ÚNumpy)ÚNumpyMetadata)Ú
TypeTracer)Ú	to_layoutTFÚerrorÚpromoteúas-characters)Úallow_recordÚallow_unknownÚnone_policyÚuse_from_iterÚprimitive_policyÚstring_policyÚregulararrayc             	   c  s   | fV  t | |||||||ƒS )a„  
    Args:
        array: Array-like data. May be a high level #ak.Array, #ak.Record (if `allow_record`),
            #ak.ArrayBuilder, or low-level #ak.contents.Content, #ak.record.Record (if `allow_record`),
            or a supported backend array (NumPy `ndarray`, CuPy `ndarray`,
            JAX DeviceArray), data-less TypeTracer, Arrow object, or an arbitrary Python
            iterable (for #ak.from_iter to convert).
        allow_record (bool): If True, allow #ak.record.Record as an output;
            otherwise, if the output would be a scalar record, raise an error.
        allow_unknown (bool): If True, allow non-Awkward outputs; otherwise,
            raise an error.
        use_from_iter (bool): If True, allow conversion of iterable inputs to
            arrays using #ak.from_iter; otherwise, throw an Exception.
        none_policy (bool): ("error", "pass-through", "promote"): If "error", throw an Exception
            for None inputs; if "pass-through", return None; otherwise,
            for "promote" return a length-one array containing the None with unknown type.
        primitive_policy ("error", "pass-through", "promote"): If "error", throw an Exception
            for scalar inputs; if "pass-through", return the scalar input; otherwise,
            for "promote" return a length-one array containing the scalar.
        string_policy ("error", "pass-through", "as-characters", "promote"): If "error", throw an Exception
            for scalar inputs; if "pass-through", return the scalar input;
            ir "as-characters", return an array of characters; otherwise,
            for "promote" return a length-one array containing the string.
        regulararray (bool): Prefer to create #ak.contents.RegularArray nodes for
            regular array objects.

    Converts `array` (many types supported, including all Awkward Arrays and
    Records) into a #ak.contents.Content and maybe #ak.record.Record or
    other types.

    This function is usually used to sanitize inputs for other functions; it
    would rarely be used in a data analysis because #ak.contents.Content and
    #ak.record.Record are lower-level than #ak.Array.
    )Ú_impl)Úarrayr   r   r   r   r   r   r   © r   úb/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/awkward/operations/ak_to_layout.pyr      s    /ør   c                 C  s4   |d u r| S | d u r|S | |u r$| S i | ¥|¥S d S )Nr   )ÚprimaryZ	secondaryr   r   r    Úmaybe_merge_mappings\   s    r"   c                C  sX   |dv sJ ‚|dkr| S |dkr$|S |dkrDt dt| ƒj› d�ƒ‚ntd|›d�ƒ‚d S )	N©úpass-throughr   r   r$   r   r   zEncountered a scalar (z.), but scalar conversion/promotion is disabledz.Encountered an invalid primitive policy value úB. The permitted values are "pass-through", "promote", and "error".)Ú	TypeErrorÚtypeÚ__name__Ú
ValueError©ÚobjÚlayoutr   r   r   r    Ú_handle_as_primitiveg   s    ÿ
ÿr-   c                C  sJ   |dv sJ ‚|dkr| S |dkr$|S |dkr6t dƒ‚ntd|›d�ƒ‚d S )Nr#   r$   r   r   zCEncountered a None value, but None conversion/promotion is disabledz)Encountered an invalid none-policy value r%   )r&   r)   )r+   r,   r   r   r   r    Ú_handle_as_noney   s    ÿ
ÿr.   c                C  s,   |dv sJ ‚| j dkr$t| ||d�S |S d S )Nr#   r   ©r   )Úndimr-   r*   r   r   r    Ú_handle_array_like‹   s    
r1   r   )Úreturnc                 C  s:  t | tjjƒr| S t | tjjƒr6|s.tdƒ‚n| S �n t | tjjƒrJ| j	S t | tjjƒrp|sftdƒ‚n| j	S �nÆt | tjj
ƒrˆ|  ¡ j	S t | tj
ƒrœ|  ¡ S t | ¡rÊtjj| |dd|d�}t| ||d�S t | ¡� røtjj| |d|d�}t| ||d�S t | ¡�r&tjj| |d|d�}t| ||d�S t | ¡�rŠt ¡ }	| jdk�rT|	j | d¡} | jjd	v �rjtd
ƒ‚tjj| d |	d�}t| ||d�S tj  !| d¡�rªtjj"| dd�S t#| dƒ�rÔt#| dƒ�rÔtjj$| d|d�S t | t%j&ƒ�rtjjt '| ¡|dd|d�}t| ||d�S t | t(t)fƒ�r�tjj*| gdd�}
|dk�r<| S |dk�rN|
d S |dk�r\|
S |dk�r~tdt+| ƒj,› d�ƒ‚nt-d|›d�ƒ‚n¦t | t.t/t0t1t2fƒ�rÂt3| tjj*| gdd�|d�S | d u �rèt4| tjj*| gdd�|d�S t | t5ƒ�r|�rtjj*| d|d�S tdƒ‚n |�r | S tdt+| ƒj,› d�ƒ‚d S )Nz2ak.Record objects are not allowed in this functionTF)r   ZrecordarrayÚ	highlevelr   r/   )r   r3   r   r   )é   >   ÚUÚSz9strings are currently not supported for typetracer arrays)Ú
parametersÚbackendZpyarrow)r3   Z
__dlpack__Z__dlpack_device__)r3   r   r$   r   r   r   zEncountered a z-, but string conversion/promotion is disabledz+Encountered an invalid string policy value zS. The permitted values are "pass-through", "as-characters", "promote", and "error".)r   )r3   r   zBEncountered an iterable object, but coercing iterables is disabledzEncountered unknown type z , and `allow_unknown` is `False`)6Ú
isinstanceÚakÚcontentsZContentÚrecordZRecordr&   r3   ZArrayr,   ZArrayBuilderZsnapshotr	   ÚnumpyZis_own_arrayÚ
operationsZ
from_numpyr1   r   Z	from_cupyr   Zfrom_jaxr   r   Úinstancer0   ZnplikeZreshapeZdtypeÚkindÚNotImplementedErrorZ
NumpyArrayZ_utilZ	in_moduleZ
from_arrowÚhasattrZfrom_dlpackÚnpZgenericZasarrayÚstrÚbytesZ	from_iterr'   r(   r)   r   r   r   r   Úboolr-   r.   r   )r+   r   r   r   r   r   r   r   Zpromoted_layoutr8   r,   r   r   r    r   “   sè    




ûÿüÿüÿÿÿÿûÿ



ÿ
ÿý
ýÿÿÿr   )+Ú
__future__r   Úcollections.abcr   r   r   r   Únumbersr   Útypingr   Zawkward_cpp.libr	   Zawkwardr:   Zawkward._backends.numpyr
   Zawkward._backends.typetracerr   Zawkward._dispatchr   Zawkward._nplikes.cupyr   Zawkward._nplikes.jaxr   Zawkward._nplikes.numpyr   Zawkward._nplikes.numpy_liker   Zawkward._nplikes.typetracerr   Ú__all__r?   rC   r=   Znumpy_backendZtypetracer_backendr   r"   r-   r.   r1   r   r   r   r   r    Ú<module>   sB   ÷=