a
    DfX                     @  s  d dl mZ d dlmZ d dlZd dlmZ d dlmZm	Z	m
Z
mZ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mZmZmZmZ dd
lmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0 zd dl1m2Z2m3Z3 W n e4y   d\Z2Z3Y n0 er,d dl5m6Z6 dgZ7e8e9Z:eddddddZ;dddddddZ<dd Z=dd Z>dd Z?dd Z@dd d!d"ZAd#d$ ZBd%d& ZCd'd( ZDd)d* ZEdS )+    )annotations)countN)TYPE_CHECKING)uniqueconcatpluckgetmemoizeliteral_unroll   )AntialiasCombination)SpecialColumnUsesCudaMutexbycategory_codessummary)isnullngjitnanmax_in_placenanmin_in_placenansum_in_placenanfirst_in_placenanlast_in_placenanmax_n_in_place_3dnanmax_n_in_place_4dnanmin_n_in_place_3dnanmin_n_in_place_4dnanfirst_n_in_place_3dnanfirst_n_in_place_4dnanlast_n_in_place_3dnanlast_n_in_place_4drow_min_in_placerow_min_n_in_place_3drow_min_n_in_place_4drow_max_in_placerow_max_n_in_place_3drow_max_n_in_place_4d)cuda_mutex_lockcuda_mutex_unlock)NN)UnzippedAntialiasStage2compile_componentsF)	antialiascudapartitionedc                  sr  t t| }t ttfdd|D } fdd|D } rt||\}	rhddl}
|
}nt}|	|}	t|	|}ndd}	d} fddt||D }t tt	d	|}t d
d |D }t t|| }t t	d|}t t	d|}t
||}t|||| \}}t||}t|||| }t|| }dd |D }||||||	||fS )a]  Given an ``Aggregation`` object and a schema, return 5 sub-functions
    and information on how to perform the second stage aggregation if
    antialiasing is requested,

    Parameters
    ----------
    agg : Aggregation
        The expression describing the aggregation(s) to be computed.

    schema : DataShape
        Columns and dtypes in the source dataset.

    glyph : Glyph
        The glyph to render.

    antialias : bool
        Whether to render using antialiasing.

    cuda : bool
        Whether to render using CUDA (on the GPU) or CPU.

    partitioned : bool
        Whether the source dataset is partitioned using dask.

    Returns
    -------
    A tuple of the following:

    ``create(shape)``
        Function that takes the aggregate shape, and returns a tuple of
        initialized numpy arrays.

    ``info(df, canvas_shape)``
        Function that takes a dataframe, and returns preprocessed 1D numpy
        arrays of the needed columns.

    ``append(i, x, y, *aggs_and_cols)``
        Function that appends the ``i``th row of the table to the ``(x, y)``
        bin, given the base arrays and columns in ``aggs_and_cols``. This does
        the bulk of the work.

    ``combine(base_tuples)``
        Function that combines a list of base tuples into a single base tuple.
        This forms the reducing step in a reduction tree.

    ``finalize(aggs, cuda)``
        Function that is given a tuple of base numpy arrays and returns the
        finalized ``DataArray`` or ``Dataset``.

    ``antialias_stage_2``
        If using antialiased lines this is a tuple of the ``AntialiasCombination``
        values corresponding to the aggs. If not using antialiased lines then
        this is ``False``.

    ``antialias_stage_2_funcs``
        If using antialiased lines which require a second stage combine, this
        is a tuple of the three combine functions which are the accumulate,
        clear and copy_back functions. If not using antialiased lines then this
        is ``None``.

    ``column_names``
        Names of DataFrame columns or DataArray variables that are used by the
        agg.
    c                 3  s   | ]}|  V  qd S N)_build_bases).0r)r-   r.    `/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/datashader/compiler.py	<genexpr>i       z%compile_components.<locals>.<genexpr>c                   s   g | ]}|  qS r3   )Z
out_dshaper1   b)r,   r-   r.   schemar3   r4   
<listcomp>j   r6   z&compile_components.<locals>.<listcomp>r   NFc              
     s$   g | ]\}}t || qS r3   )_get_call_tuplesr1   r8   dr,   r-   r.   r9   self_intersectr3   r4   r:      s      c                 s  s"   | ]}|d  dur|d  V  qdS    Nr3   r1   cr3   r3   r4   r5      r6         c                 S  s   g | ]}|j tjkr|j qS r3   )columnr   ZRowIndexrC   r3   r3   r4   r:      r6   )listtraverse_aggregationr   r   make_antialias_stage_2cupynp make_antialias_stage_2_functionszipr   make_createmake_append	make_infomake_combinemake_finalize)aggr9   glyphr,   r-   r.   redsbasesdshapesantialias_stage_2rK   array_moduleZantialias_stage_2_funcscallscolsZnan_check_colstempscombine_tempscreateappendany_uses_cuda_mutexinfocombinefinalizeZcolumn_namesr3   r>   r4   r+   $   sB    B r   floatboolZcombinationzeroZn_reductioncategoricalc                 C  s$  |r|dkrJ| t jt jfv r(|r$tS tS | t jt jfv rD|r@tS tS t	n\| t jkr`|r\t
S tS | t jkrv|rrtS tS | t jkr|rtS tS | t jkr|rtS tS t	nx|dkr| t jt jfv rtS | t jt jfv rtS t	nB| t jkrtS | t jk rtS | t jkrtS | t jkrtS tS d S )N)r   MAXZLASTr'   r&   ZMINZFIRSTr$   r#   NotImplementedErrorr   r   r   r   r   r   r!   r    r%   r"   r   r   r   r   r   rg   r3   r3   r4   #_get_antialias_stage_2_combine_func   s>    




rm   c              
   C  s  | \}}}}dd t ||||D }dd |D }	|	dd  dg }
i }t|d< t|D ]}|||j< qXdd t D }g d	}tt ||	|
D ]\}\}}}|r|| }t|tr|j}|	|||| }t
|}|||< |d
| d| d|d  d q|r
q|d
|j d| d| d qd|}t| t|| t|d }tt|rrtj|d< dg}t|D ]"\}}|d| d| d qd|}t| t|| t|d }tdd }|||fS )Nc                 S  s"   g | ]\}}}}t ||||qS r3   )rm   )r1   combrh   Zn_redcatr3   r3   r4   r:      r6   z4make_antialias_stage_2_functions.<locals>.<listcomp>c                 S  s   g | ]}|  qS r3   is_wherer7   r3   r3   r4   r:      r6   r   Fr   c                 s  s   | ]}d | V  qdS )rc   Nr3   r1   ir3   r3   r4   r5      r6   z3make_antialias_stage_2_functions.<locals>.<genexpr>)z7def aa_stage_2_accumulate(aggs_and_copies, first_pass):z    if first_pass:z1        for a in literal_unroll(aggs_and_copies):z            a[1][:] = a[0][:]z	    else:z        z(aggs_and_copies[z][::-1], aggs_and_copies[z][::-1])z][1], aggs_and_copies[z][0])
aa_stage_2_accumulatenanz&def aa_stage_2_clear(aggs_and_copies):z    aggs_and_copies[z
][0].fill()aa_stage_2_clearc                 S  s.   t | D ] }|d d d  |d d d < qd S )Nr   r   r
   )Zaggs_and_copiesZagg_and_copyr3   r3   r4   aa_stage_2_copy_back  s    z>make_antialias_stage_2_functions.<locals>.aa_stage_2_copy_back)rN   r   set__name__r   	enumerate
isinstancer   	reductionZ_combine_callbacknextr`   joinloggerdebugexecr   rL   anyisnanrv   )rY   rW   r-   r.   Zaa_combinationsZ	aa_zeroesZaa_n_reductionsZaa_categoricalfuncsbase_is_wherenext_base_is_where	namespacefuncnameslinesrs   rq   Znext_is_wherewhere_reductionrc   namecoderu   Zaa_zerorx   ry   r3   r3   r4   rM      s\    








rM   c                 c  s6   t | tr,| jD ]}t|D ]
}|V  qqn| V  dS )z/Yield a left->right traversal of an aggregationN)r}   r   valuesrI   )rT   aZa2r3   r3   r4   rI     s
    

rI   c              	   C  sH   |  |||||| f| j| j| || |||r:|  ntj|  fS r/   )	Z_build_appendinputsnan_check_columnZ_build_tempsZ_build_combine_tempsuses_cuda_mutexr   NoZis_categorical)baseZdshaper9   r-   r,   r?   r.   r3   r3   r4   r;     s    
r;   c                   s8   dd t | |D |r&dd l}| nt  fddS )Nc                 S  s   g | ]\}}| |qS r3   )Z_build_creater<   r3   r3   r4   r:   (  r6   zmake_create.<locals>.<listcomp>r   c                   s   t  fddD S )Nc                 3  s   | ]}| V  qd S r/   r3   rC   )rZ   shaper3   r4   r5   .  r6   z0make_create.<locals>.<lambda>.<locals>.<genexpr>)tupler   rZ   Zcreatorsr   r4   <lambda>.  r6   zmake_create.<locals>.<lambda>)rN   rK   rL   )rW   rX   r-   rK   r3   r   r4   rO   '  s    rO   )r   c                   s    fdd}|S )Nc                   sz   t  fddD }rvdd l}dd l}ddlm} ||j|dkr\|j|tjd}n|jdtjd}||f7 }|S )Nc                 3  s   | ]}|  V  qd S r/   )applyrC   )r-   dfr3   r4   r5   3  r6   z*make_info.<locals>.info.<locals>.<genexpr>r   )Versionz0.57)Zdtype)r   )	r   rK   numbaZpackaging.versionr   __version__zerosrL   Zuint32)r   Zcanvas_shaperetrK   r   r   Zmutex_arrayr\   r-   r   )r   r4   rb   2  s    
zmake_info.<locals>.infor3   )r\   r-   r   rb   r3   r   r4   rQ   1  s    rQ   c           +        s  dd t  D t| t| }i }tdd |D }|rBt|d< tdd |D }tdd |D }	|	r|dg7 }t|d< t|d	< fd
d|D }
tt||
 i g }g }|j}|d urd	dd t
|D nd d}i }i }ttjrNtd|  td|	    D ].\}}td| d| dt|dd   qddd fdd}t|D ]\}\}} }}}}}}| o|tjk}t|fdd|D  t}td| d|  |||<  fdd| D }|r&t|d tr&| fdd|dd  D  n`|d u rJ| fdd|D  n<|rl| fd d|D  n| fd!d|D  |r" |d  }||d }|d u rt|d trd"nd#} d$t }|||< || d%| d& d'|  d( |
| }!||! d)|! d*| d' |fd+d|D  |rL|d,d-g7 }|r`|r`|  t| dkox| d  }"|"r| d }#t|#tr|#j }#t!|#j"}$||$d }%|%d u }&|&rt}%|%||$< ||% | }'|r|s||d. |&r||% d)|'  n
||' |d u r0d"}(n@ |  d& d'})|d/ }'d0|) d1|d/< |d2|'  d2}(||( d3|% d4 |d2|( | d5d	| d( n|r|s||d. |r |  d& d'})|d0|) d1 |d2| d5d	| d( n|| d5d	| d( |r@||d |}qj| fd6d D  | }|r|d.g| |dg }|rd,d-g|
 }
|d u rd7#d	|
d8	|}*nd9#d	|
d8	|}*t|* t$|*| t%|d: |	fS );Nc                 s  s   | ]}d  |V  qdS )z_{0}Nformatrr   r3   r3   r4   r5   C  r6   zmake_append.<locals>.<genexpr>c                 s  s   | ]}|d  V  qdS rA   r3   r1   callr3   r3   r4   r5   F  r6   r   c                 s  s   | ]}|d  t jkV  qdS    N)r   Globalr   r3   r3   r4   r5   I  r6   c                 s  s   | ]}|d  t jkV  qdS r   )r   r   r   r3   r3   r4   r5   J  r6   _cuda_mutexr(   r)   c                   s   g | ]}t  qS r3   r   rr   r   r3   r4   r:   Q  r6   zmake_append.<locals>.<listcomp>z, c                 S  s   g | ]}d t | qS )rs   )str)r1   nr3   r3   r4   r:   X  r6   Fzglobal_cuda_mutex zany_uses_cuda_mutex zarg_lk  rG   rf   r   )lockreturnc                   s    | rdnd}| d d  dS )Nr(   r)   (r   z	, (y, x))r3   )r   r   arg_lkr3   r4   get_cuda_mutex_calle  s    z(make_append.<locals>.get_cuda_mutex_callc                 3  s   | ]}t  V  qd S r/   r   rr   r   r3   r4   r5   l  r6   zfunc c                   s   g | ]} | qS r3   r3   rr   r   r3   r4   r:   p  r6   r   c                 3  s   | ]}d   | V  qdS z{0}[{1}]Nr   )r1   colr   	subscriptr3   r4   r5   r  r6   r   c                 3  s   | ]}d   | V  qdS )z{0}Nr   rr   r   r3   r4   r5   t  r6   c                 3  s   | ]}d   | V  qdS )z{0}[{1}][1]Nr   rr   r   r3   r4   r5   v  s   c                 3  s   | ]}d   | V  qdS r   r   rr   r   r3   r4   r5   y  s    z[0]ro   z = int([]rw   z = z[:, :, c                   s   g | ]} | qS r3   r3   rr   )local_lkr3   r4   r:     r6   Z	aa_factorZprev_aa_factorTrj   zif not isnull(z):z    zif z >= 0:z(x, y, c                   s    g | ]\}}d  | | qS )z{0} = {1}[y, x]r   )r1   rT   r   r   r3   r4   r:     s   zdef append(x, y, {0}):
    {1}z
    z#def append({0}, x, y, {1}):
    {2}r`   )&r   rH   r   r   r(   r)   dictrN   ndimsr   ranger   isEnabledForloggingDEBUGr   itemsgetattrr|   r   ZLocalupdater   r}   r   extendr   r`   poplenrq   r   r~   hashselectorr   r   r   )+rW   r\   r[   rU   r,   r   r   Zneed_isnullZglobal_cuda_mutexra   	signatureheadbodyr   Zprev_local_cuda_mutexZcategorical_argsZwhere_selectorskvr   indexr   r   r]   _r   ri   Zlocal_cuda_mutex	func_nameargsZcategorical_argZcat_nameZ	col_indexargrq   r   Zselector_hashZupdate_index_arg_nameZnew_selectorZ	prev_body
whitespacevarr   r3   )r   r   r   r   r4   rP   B  s    
($


"



$ 


rP   c           	        s   t dd t| D t dd t| D  dd | D dd  dg } fddt| ||||D fd	d
}|S )Nc                 s  s   | ]\}}||fV  qd S r/   r3   r1   r   r   r3   r3   r4   r5     r6   zmake_combine.<locals>.<genexpr>c                 s  s&   | ]\}}t |tr|j|fV  qd S r/   )r}   r   r~   r   r3   r3   r4   r5     r6   c                 S  s   g | ]}|  qS r3   rp   r7   r3   r3   r4   r:     r6   z make_combine.<locals>.<listcomp>r   Fc                   sJ   g | ]B\}}}}}|rd n| | fdd|f| | D fqS )Nc                   s   g | ]} | qS r3   r3   rr   r   r3   r4   r:     r6   z+make_combine.<locals>.<listcomp>.<listcomp>)Z_build_combine)r1   r8   r=   tctr   )r,   r   r-   r.   r3   r4   r:     s   c                   sv   t dd t|  D }g }t D ]H\}\}}|d u r:q$|t|| }|rb||d d d  q$|| q$t |S )Nc                 s  s   | ]}t |V  qd S r/   )rL   stack)r1   bsr3   r3   r4   r5     r6   z0make_combine.<locals>.combine.<locals>.<genexpr>rj   )r   rN   r   r   r`   )Zbase_tuplesrW   r   rq   r   indsr   )r   r[   r3   r4   rc     s    zmake_combine.<locals>.combine)r   r|   r   rN   )	rW   rX   r]   r^   r,   r-   r.   r   rc   r3   )r,   r   r   r[   r-   r.   r4   rR     s    rR   c           
   	     s   t dd t| D t trg t j jD ]^\}}t| ||||}z|||} W n t	yl   Y n0 fdd| D }
|||f q2d fdd	}	|	S  |S d S )	Nc                 s  s   | ]\}}||fV  qd S r/   r3   r   r3   r3   r4   r5     r6   z make_finalize.<locals>.<genexpr>c                   s   g | ]} | qS r3   r3   r7   r   r3   r4   r:     r6   z!make_finalize.<locals>.<listcomp>Fc                   sB    fddD j d fdddD }tj|dS )Nc                   s,   i | ]$\}}}||t | fi qS r3   )r   )r1   key	finalizerr   )rW   r-   kwargsr3   r4   
<dictcomp>
  s   z3make_finalize.<locals>.finalize.<locals>.<dictcomp>r   c                   s   i | ]}|  j | qS r3   attrs)r1   attr)datar   r3   r4   r     r6   )Zx_rangeZy_ranger   )keysxrZDataset)rW   r-   r   r   )rT   r[   )rW   r-   r   r   r   r4   rd   	  s    
zmake_finalize.<locals>.finalize)F)r   r|   r}   r   rN   r   r   rS   r0   AttributeErrorr`   Z_build_finalize)
rW   rT   r9   r-   r.   r   valfr   rd   r3   )rT   r   r[   r4   rS     s    

rS   c                   s:   d| D ]}|  rd qqdd fdd}|fS )NTFr*   )r   c                   s    t tt fddD  S )Nc                 3  s   | ]}|  V  qd S r/   )Z_antialias_stage_2r7   )rZ   r?   r3   r4   r5   $  s   zDmake_antialias_stage_2.<locals>.antialias_stage_2.<locals>.<genexpr>)r   rN   r   rZ   rW   r?   r   r4   rY   #  s    z1make_antialias_stage_2.<locals>.antialias_stage_2)Z_antialias_requires_2_stages)rV   rW   ZredrY   r3   r   r4   rJ     s    rJ   )F
__future__r   	itertoolsr   r   typingr   Ztoolzr   r   r   r   r	   r   r   numpyrL   Zxarrayr   r,   r   Z
reductionsr   r   r   r   r   utilsr   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   Z)datashader.transfer_functions._cuda_utilsr(   r)   ImportErrorZdatashader.antialiasr*   __all__	getLoggerr{   r   r+   rm   rM   rI   r;   rO   rQ   rP   rR   rS   rJ   r3   r3   r3   r4   <module>   s>   \
t-I

  