a
    yn„f>B  ã                   @  sÆ  d dl mZ d dlZd dlZd dlZd dlmZ 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mZmZ d dlmZ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  d dl!m"Z" d dl#m$Z$m%Z%m&Z& d dl'm(Z( d dl)m*Z* e(de+ƒZ,i Z-dddœdd„Z.ddœdd„Z/d<ddddœdd „Z0d!d"„ Z1dd#œd$d%„Z2ddd&œd'd(„Z3ddd&œd)d*„Z4d+d,œd-d.„Z5d/ddd0œd1d2„Z6d/ddd0œd3d4„Z7d/dd5œd6d7„Z8d8d9„ Z9d=d:d;„Z:dS )>é    )ÚannotationsN)ÚIterable)Úchain)Úattrs_of)ÚBackend)Ú
backend_ofÚbackend_of_objÚcommon_backend)Úbehavior_ofÚfind_custom_castÚ
find_ufuncÚfind_ufunc_generic©Úas_hashable©Úwrap_layout)Ú	to_nplike)Úparameters_intersect)Úis_non_string_like_iterable)ÚAnyÚIteratorÚMapping)ÚSentinel©Ú
NumpyArrayÚUNSUPPORTEDr   zIterator[Backend])ÚargsÚreturnc                 c  sP   t  | ¡}|rL| ¡ }t|dd�}|dur2|V  q
t|ttfƒr
| |¡ q
dS )z¦
    Args:
        args: iterable of objects to visit

    Yields the encountered backends of layout / array-like arguments encountered
    in the argument list.
    N©Údefault)ÚcollectionsÚdequeÚpopleftr   Ú
isinstanceÚtupleÚlistÚextend)r   ÚstackÚargÚbackend© r*   úY/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/awkward/_connect/numpy.pyÚ_find_backends&   s    
r,   r   ©r)   c                   sÀ   t | d d�}|d urb|j}| | ¡r:t| | ¡ˆ j|d�S tj| ddddd�}| ˆ ¡jdd�S nZt	| t
ƒr‚t
‡ fdd	„| D ƒƒS t	| tƒrž‡ fd
d„| D ƒS t| ƒr¸td| ›d�ƒ‚n| S d S )Nr   )Zfrom_nplikeFÚerror)Úallow_recordÚallow_unknownÚprimitive_policyÚstring_policyT©Zallow_missingc                 3  s   | ]}t |ˆ ƒV  qd S ©N©Ú_to_rectilinear©Ú.0Úxr-   r*   r+   Ú	<genexpr>P   ó    z"_to_rectilinear.<locals>.<genexpr>c                   s   g | ]}t |ˆ ƒ‘qS r*   r5   r7   r-   r*   r+   Ú
<listcomp>R   r;   z#_to_rectilinear.<locals>.<listcomp>z*encountered an unsupported iterable value zu whilst converting arguments to NumPy-friendly types. If this argument should be supported, please file a bug report.)r   ÚnplikeZis_own_arrayr   ÚasarrayÚakÚ	to_layoutÚ
to_backendZto_backend_arrayr#   r$   r%   r   Ú	TypeError)r(   r)   Zarg_backendZ
arg_nplikeÚlayoutr*   r-   r+   r6   :   s0    
ÿû


ÿr6   zdict[str, Any]zMapping | NonezMapping[str, Any] | None)ÚkwargsÚbehaviorÚattrsc              
     s®   t  | ¡}|d ur ||i |¤ŽS t|| ¡ ƒ}tt|ƒƒ}t|ƒ‰ t‡ fdd„|D ƒƒ}	‡ fdd„| ¡ D ƒ}
| |	i |
¤Ž}t	j
jj|dddddddd�}t||d|d�S d S )	Nc                 3  s   | ]}t |ˆ ƒV  qd S r4   r5   r7   r-   r*   r+   r:   m   r;   z!array_function.<locals>.<genexpr>c                   s   i | ]\}}|t |ˆ ƒ“qS r*   r5   )r8   ÚkÚvr-   r*   r+   Ú
<dictcomp>n   r;   z"array_function.<locals>.<dictcomp>Túpass-through)r/   r0   Znone_policyZregulararrayZuse_from_iterr1   r2   )rE   Úallow_otherrF   )ÚimplementedÚgetr   ÚvaluesÚ	frozensetr,   r	   r$   Úitemsr?   Ú
operationsZak_to_layoutÚ_implr   )ÚfuncÚtypesr   rD   rE   rF   ÚfunctionZall_argumentsZunique_backendsZrectilinear_argsZrectilinear_kwargsÚresultÚoutr*   r-   r+   Úarray_function\   s(    
ø
rX   c                   s   ‡ fdd„}|S )Nc                   sJ   t  ˆ ¡‰dd„ ˆj ¡ D ƒ‰t ˆ ¡‡ ‡‡fdd„ƒ}|tttˆƒ< ˆ S )Nc                 S  s   h | ]}|j tu r|j’qS r*   )r   r   Úname)r8   Úpr*   r*   r+   Ú	<setcomp>�   s   z0implements.<locals>.decorator.<locals>.<setcomp>c                    sH   ˆj | i |¤Ž}|j ¡ ˆ@ }|r:d |¡}td|› �ƒ‚ˆ | i |¤ŽS )Nú, zCAwkward NEP-18 overload was provided with unsupported argument(s): )ÚbindÚ	argumentsÚkeysÚjoinrB   )r   rD   Ú
parametersZprovided_invalid_namesÚnames©rU   Ú	signatureZunsupported_namesr*   r+   Úensure_valid_args…   s    
ÿz8implements.<locals>.decorator.<locals>.ensure_valid_args)	Úinspectrd   ra   rN   Ú	functoolsÚwrapsrL   ÚgetattrÚnumpy)rU   re   ©Únumpy_functionrc   r+   Ú	decorator   s    
ÿ
zimplements.<locals>.decoratorr*   )rl   rm   r*   rk   r+   Ú
implements~   s    rn   ©rE   c                   s|   ‡ fdd„| D ƒ}g }|D ]\}t |ˆ ƒ}tjj|d u r:|n||ƒdddd�}t|tjjtjjfƒrl| 	|¡}| 
|¡ q|S )Nc                   s0   g | ](}t |tjjtjjfƒr(t|ˆ ƒn|‘qS r*   ©r#   r?   ÚcontentsÚContentÚrecordÚRecordr   r7   ro   r*   r+   r<   —   s   þÿz,_array_ufunc_custom_cast.<locals>.<listcomp>TrJ   )r0   r1   r2   )r   r?   rQ   r@   r#   rq   rr   rs   rt   rA   Úappend)ÚinputsrE   r)   r   Ú
nextinputsr9   Zcast_fcnZmaybe_layoutr*   ro   r+   Ú_array_ufunc_custom_cast–   s     
ü
ü
rx   )rD   rE   c                   sB   ‡ fdd„|D ƒ}| |i |¤Ž}t |tƒs0|f}tdd„ |D ƒƒS )Nc                   s0   g | ](}t |tjjtjjfƒr(t|ˆ ƒn|‘qS r*   rp   r7   ro   r*   r+   r<   ±   s   þÿz'_array_ufunc_adjust.<locals>.<listcomp>c                 s  s.   | ]&}t |tjjtjjfƒr"|jn|V  qd S r4   ©r#   r?   Ú	highlevelZArrayrt   rC   r7   r*   r*   r+   r:   »   s   ÿz&_array_ufunc_adjust.<locals>.<genexpr>)r#   r$   )Úcustomrv   rD   rE   r   rW   r*   ro   r+   Ú_array_ufunc_adjust®   s    
ü
þr|   c                   sR   ‡ fdd„|D ƒ}| ||||ƒ}|t u r,d S t|tƒs<|f}tdd„ |D ƒƒS d S )Nc                   s   g | ]}t |ˆ d d�‘qS )T)rK   r   r7   ro   r*   r+   r<   Ä   r;   z-_array_ufunc_adjust_apply.<locals>.<listcomp>c                 s  s.   | ]&}t |tjjtjjfƒr"|jn|V  qd S r4   ry   r7   r*   r*   r+   r:   Ì   s   ÿz,_array_ufunc_adjust_apply.<locals>.<genexpr>)ÚNotImplementedr#   r$   )Úapply_ufuncÚufuncÚmethodrv   rD   rE   rw   rW   r*   ro   r+   Ú_array_ufunc_adjust_applyÁ   s    
þr�   ztuple[Any, ...] | None)r   c                 C  s²   g }d}|D ]Œ}t |tjjƒrŠ| d¡| d¡ }}|d urL| |¡ d}q˜|d urd| |¡ d}q˜t |tƒr~| |jj¡ q˜| d ¡ q| t|ƒ¡ q|rª| g|¢R S d S d S )NFÚ
__record__Ú__list__T)	r#   r?   rq   rr   Ú	parameterru   r   ÚdtypeÚtype)r   rv   rd   Zhas_seen_nominal_typer9   Zrecord_nameZ	list_namer*   r*   r+   Ú_array_ufunc_signatureÒ   s$    


r‡   Ústr)r€   rD   rE   c                 C  s0  | t ju �r t|ƒdk�r t|d tjjƒ�r |d j�r |d  d¡dk�r t|d tjjƒ�r |d j�r |d  d¡dk�r |dksŽJ ‚|\}}t  	|j
j¡}t  	|j
j¡}t|j|ƒ}	t|j|ƒ}
t|	ƒt|
ƒkrôtjj|	|
kd d�rô|}n–tj |	¡}tj |
¡}dd	„ |D ƒ}d
d	„ |D ƒ}dd„ t|ƒD ƒ}t jt|ƒd t jd�}t|ƒD ]\}}| |t|ƒ¡||< �qZd|d< || }||k}tj |¡fS g }|D ]J}t|tjjƒ�rÜ|j�rÜ| t| ¡ |d�¡ n| t||dd�¡ �q¨t| |ƒ|i |¤Ž}t|tƒ�s|f}tdd„ |D ƒƒS d S )Né   r   Ú	__array__Úcategoricalé   Ú__call__)Úaxisc                 S  s   g | ]}t |ƒ‘qS r*   r   r7   r*   r*   r+   r<     r;   z,_array_ufunc_categorical.<locals>.<listcomp>c                 S  s   g | ]}t |ƒ‘qS r*   r   r7   r*   r*   r+   r<   	  r;   c                 S  s   i | ]\}}||“qS r*   r*   )r8   Úir9   r*   r*   r+   rI   
  r;   z,_array_ufunc_categorical.<locals>.<dictcomp>©r…   éÿÿÿÿro   T)rE   rK   c                 s  s   | ]}t j|d d�V  qdS )T)r0   N)r?   r@   r7   r*   r*   r+   r:      r;   z+_array_ufunc_categorical.<locals>.<genexpr>)rj   ÚequalÚlenr#   r?   rq   rr   Ú
is_indexedr„   r>   ÚindexÚdatar   ÚcontentrQ   ÚallZto_listÚ	enumerateÚemptyZint64rM   r   ru   Úprojectri   r$   )r   r€   rv   rD   rE   ÚoneÚtwoZ	one_indexZ	two_indexZone_contentZtwo_contentZ
one_mappedZone_listZtwo_listZone_hashableZtwo_hashableZ
two_lookupZ
one_to_twor�   r9   rW   rw   r*   r*   r+   Ú_array_ufunc_categoricalë   s^    ÿ
þýüûúùø
ÿrž   c                 C  sÜ  |dksJ ‚| t jt jfvs(t|ƒdkr,d S |\}}t|tjjƒr`| d¡dv r`tj	|dd�}n$t|t
tfƒr€tj	|gdd�}nd S t|tjjƒr°| d¡dv r°tj	|dd�}n$t|t
tfƒrÐtj	|gdd�}nd S tj||ddd�\}}|jj}tjj|tj ¡ d	dd
�}|j�sJ ‚tjj|tj ¡ d	dd
�}	|	j�s>J ‚| |j¡}
| |	j¡}|
|k}| ||
¡}|
| }t|ƒdk�r¸|| }|| }tjjt|ƒt|ƒkd	dd�}|j||< | t ju �rÎ| |¡}tj |¡fS )Nr�   r‰   rŠ   ©ÚstringÚ
bytestringF)rz   rŒ   )rz   Zdepth_limitr‘   )rŽ   Úmaskr   )rŽ   rz   )rj   r’   Ú	not_equalr“   r#   r?   rq   rr   r„   Zwithout_parametersrˆ   ÚbytesZbroadcast_arraysr)   r=   Z_doÚreduceZ	_reducersZCountZis_numpyr>   r–   Úlogical_andrQ   r˜   r   Zlogical_notr   )r   r€   rv   rD   rE   ÚleftÚrightr=   Zleft_counts_layoutZright_counts_layoutZcounts1Zcounts2rW   ÚpossibleZpossible_countsZonepossibleZtwopossibleZreducedr*   r*   r+   Ú_array_ufunc_string_likes#  sN    ÿý

rª   )r€   rD   c                   s®   ˆdkst |ƒdksdˆv r tS t|Ž ‰t|Ž ‰ t|ddiŽ‰t|ˆˆƒ}‡‡‡‡‡fdd„}tjj||dˆj	d	�}t |ƒd
kr’t
|d ˆˆ d�S t‡ ‡fdd„|D ƒƒS d S )Nr�   r   rW   Zcoerce_to_commonTc                   sh  dd„ | D ƒ}t |ƒdksJ ‚tˆ| ƒ}|d urPtˆ|ƒ}|d urPt|| ˆˆƒS tdd„ |D ƒƒr~tˆˆ| ˆˆƒ}|d ur~|S tdd„ |D ƒƒrÚtˆˆ| ˆˆƒ}|d ur¬|S tdd„ |D ƒƒrÚtt	ˆƒj
› dˆj› d	�ƒ‚ˆtju rìtd
ƒ‚|D ]:}tˆ|ˆƒ}|d urðt|ˆˆ| ˆˆƒ}|d urð|  S qðtdd„ | D ƒƒ�rªt tdd„ |D ƒ¡‰ dd„ | D ƒ}ˆj ˆˆ|ˆ¡}	t|	tƒ�ršt‡‡ fdd„|	D ƒƒS t|	ˆˆ d�fS tdd„ |D ƒƒ�rdg }
| D ]|}t|tjjƒ�r0| d¡d u�rü|
 | d¡¡ n2| d¡d u�r|
 | d¡¡ n|
 t	|ƒj¡ n|
 t	|ƒj¡ �qÆtd t	ˆƒj
ˆjd |
¡¡ƒ‚d S )Nc                 S  s   g | ]}t |tjjƒr|‘qS r*   )r#   r?   rq   rr   r7   r*   r*   r+   r<   q  r;   z/array_ufunc.<locals>.action.<locals>.<listcomp>rŒ   c                 s  s"   | ]}|j o| d ¡dkV  qdS )rŠ   r‹   N)r”   r„   r7   r*   r*   r+   r:   }  s   z.array_ufunc.<locals>.action.<locals>.<genexpr>c                 s  s"   | ]}|j o| d ¡dv V  qdS ©rŠ   rŸ   N©Zis_listr„   r7   r*   r*   r+   r:   …  s   ÿc                 s  s"   | ]}|j o| d ¡dv V  qdS r«   r¬   r7   r*   r*   r+   r:   Ž  s   ÿÚ.zƒ is not implemented for string types. To register an implementation, add a name to these string(s) and register a behavior overloadzTmatrix multiplication (`@` or `np.matmul`) is not yet implemented for Awkward Arraysc                 s  s(   | ] }t |tƒpt |tjjƒ V  qd S r4   )r#   r   r?   rq   rr   r7   r*   r*   r+   r:   ¦  s   ÿc                 s  s   | ]}|j V  qd S r4   )Ú_parameters)r8   Úcr*   r*   r+   r:   ¬  r;   c                 S  s    g | ]}t |tƒr|jn|‘qS r*   )r#   r   r–   r7   r*   r*   r+   r<   ¯  r;   c                 3  s   | ]}t |ˆ ˆd �V  qdS )©r)   ra   Nr   r7   r°   r*   r+   r:   ³  s   ÿr°   c                 s  s*   | ]"}|  d ¡dup |  d¡duV  qdS )rƒ   Nr‚   )r„   r7   r*   r*   r+   r:   »  s   ÿrƒ   r‚   z'no {}.{} overloads for custom types: {}r\   ) r“   r‡   r   r|   Úanyrž   rª   r˜   rB   r†   Ú
__module__Ú__name__rj   ÚmatmulÚNotImplementedErrorr   r�   rg   r¥   r   r=   r~   r#   r$   r   r?   rq   rr   r„   ru   Úformatr`   )rv   Úignorerq   rd   r{   rW   r9   r~   Z
input_argsrV   Úerror_message)r)   rE   rD   r€   r   )ra   r+   Úactionp  sˆ    

ÿþþÿ
ÿÿ
þ
ÿþþ
ÿÿzarray_ufunc.<locals>.actionF)Zallow_recordsZfunction_namerŒ   ©rE   rF   c                 3  s   | ]}t |ˆˆ d �V  qdS )rº   Nr   )r8   Úo)rF   rE   r*   r+   r:   Ù  r;   zarray_ufunc.<locals>.<genexpr>)r“   r}   r
   r   r   rx   r?   Z_broadcastingZbroadcast_and_applyr³   r   r$   )r   r€   rv   rD   r¹   rW   r*   )rF   r)   rE   rD   r€   r   r+   Úarray_ufuncf  s    b
ÿr¼   c                 C  s   t ‚d S r4   )rµ   )rv   r*   r*   r+   Úaction_for_matmulÜ  s    r½   c                 C  s.   t jj| dd�}|d u r|S tj||d�S d S )NFr3   r�   )r?   rQ   Zto_numpyrj   Úarray)rC   r…   rW   r*   r*   r+   Úconvert_to_arrayà  s    r¿   )N)N);Ú
__future__r   r    rg   rf   Úcollections.abcr   Ú	itertoolsr   rj   Zawkwardr?   Zawkward._attrsr   Zawkward._backends.backendr   Zawkward._backends.dispatchr   r   r	   Zawkward._behaviorr
   r   r   r   Zawkward._categoricalr   Zawkward._layoutr   Zawkward._nplikesr   Zawkward._parametersr   Zawkward._regularizer   Zawkward._typingr   r   r   Zawkward._utilr   Zawkward.contents.numpyarrayr   r³   r   rL   r,   r6   rX   rn   rx   r|   r�   r‡   rž   rª   r¼   r½   r¿   r*   r*   r*   r+   Ú<module>   sF   
( ú"8Cv