
    UgW                       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 	 d dl1m2Z2m3Z3 erd dl5m6Z6 dgZ7 ejp                  e9      Z:eddddd       Z;	 	 	 	 ddZ<d Z=d Z>d Z?d Z@ddZAd ZBd ZCd ZDd ZEy# e4$ r d\  Z2Z3Y [w xY w)    )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                  t        t        |             }t        t        t        fd|D                          }|D cg c]  }|j	                  ||       }	}|r5t        ||      \  }
}rddl}|}nt        } ||      }t        ||      }nd}
d}d}t        ||	      D cg c]  \  }}t        |||||
       }}}t        t        t        d|                  }t        d |D              }t        t        ||z               }t        t        d|            }t        t        d|            }t        ||	      }t        |||||      \  }}t        ||      }t        ||	|||      }t!        || |      }|D cg c],  }|j"                  t$        j&                  k7  s!|j"                  . }}||||||||fS c c}w c c}}w c c}w )	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  B   K   | ]  }|j                          y wN)_build_bases).0rr.   r/   s     3lib/python3.12/site-packages/datashader/compiler.py	<genexpr>z%compile_components.<locals>.<genexpr>i   s     O$Qq~~dK@$   r   NF   c              3  2   K   | ]  }|d    	|d      yw   N )r4   cs     r6   r7   z%compile_components.<locals>.<genexpr>   s     B111A!A$s   

      )listtraverse_aggregationr   r   
out_dshapemake_antialias_stage_2cupynp make_antialias_stage_2_functionszip_get_call_tuplesr   make_createmake_append	make_infomake_combinemake_finalizecolumnr   RowIndex)aggschemaglyphr-   r.   r/   redsbasesbdshapesself_intersectantialias_stage_2rE   array_moduleantialias_stage_2_funcsdcallscolsnan_check_colstempscombine_tempscreateappendany_uses_cuda_mutexinfocombinefinalizer>   column_namess       ``                       r6   r,   r,   $   s   D $S)*D O$OOPQEKPQ5aq||FIt[A5GQ ,B4,O))LL-l;"BCTV[]aCN#P !"&
 ug.0.!Q aFD)^[Y. 
 0 uQ'(DBBBNtn,-.D q%!Eq%)M.F"-eT5%"SFT4!45D5'5-DR]^GUC{CH&*Qdahh-:P:P.PAHHdLQ4(4EG^ W R.0$ Rs   G+G
"G-Gc                z   |r|dk(  rf| t         j                  t         j                  fv r|rt        S t        S | t         j
                  t         j                  fv r|rt        S t        S t        | t         j                  k(  r|rt        S t        S | t         j
                  k(  r|rt        S t        S | t         j                  k(  r|rt        S t        S | t         j                  k(  r|rt         S t"        S t        |dk(  rV| t         j                  t         j                  fv rt$        S | t         j
                  t         j                  fv rt&        S t        | t         j                  k(  rt(        S | t         j
                  k(  rt*        S | t         j                  k(  rt,        S | t         j                  k(  rt.        S t0        S )N)r   MAXLASTr(   r'   MINFIRSTr%   r$   NotImplementedErrorr   r   r   r   r    r   r"   r!   r&   r#   r   r   r   r   r   )combinationzeron_reductioncategoricals       r6   #_get_antialias_stage_2_combine_funcrt      s   2:3779M9R9RSS0;,VAVV!5!9!9;O;U;U VV0;,VAVV))2666/:+T@TT 4 8 88/:+T@TT 4 : ::1<-XBXX 4 9 990;,VAVV))
 2:3779M9R9RSS''!5!9!9;O;U;U VV''))2666&& 4 8 88&& 4 : ::(( 4 9 99''&&    c                   | \  }}}}t        ||||      D 	
cg c]  \  }}	}
}t        ||	|
|       }}
}	}}|D cg c]  }|j                          }}|dd  dgz   }i }t        |d<   t	        |      D ]  }|||j
                  <    d t               D        }g d}t        t        |||            D ]  \  }\  }}}|rf||   }t        |t              r|j                  }|j                  ||||         }t        |      }|||<   |j                  d| d| d|dz
   d	       r|ru|j                  d|j
                   d| d
| d        dj                  |      }t        j!                  |       t#        ||       t%        |d         }t'        j(                  t'        j*                  |            rt&        j,                  |d<   dg}t        |      D ]  \  }}|j                  d| d| d        dj                  |      }t        j!                  |       t#        ||       t%        |d         }t$        d        }|||fS c c}}
}	}w c c}w )Nr   Fr   c              3  &   K   | ]	  }d |   yw)rf   Nr=   r4   is     r6   r7   z3make_antialias_stage_2_functions.<locals>.<genexpr>   s     ,Gqwqc]G   )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                @    t        |       D ]  }|d   d d  |d   d d   y )Nr   r   r   )aggs_and_copiesagg_and_copys     r6   aa_stage_2_copy_backz>make_antialias_stage_2_functions.<locals>.aa_stage_2_copy_back  s*     +?;L!-a!3LOA <ru   )rH   rt   is_wherer   set__name__r   	enumerate
isinstancer   	reduction_combine_callbacknextrc   joinloggerdebugexecr   rF   anyisnanr}   )rY   rU   r.   r/   aa_combinations	aa_zeroesaa_n_reductionsaa_categoricalcombrq   n_redcatfuncsrV   base_is_wherenext_base_is_where	namespacefuncnameslinesry   r   next_is_wherewhere_reductionrf   namecoder|   aa_zeror   r   s                                  r6   rG   rG      s   BS?OY OYPRP NdTSWY^`c0tUCHP 
 R ,115aQZZ\5M1&qr*eW4I"0IE
#'	$--   -EG,EE /8E=<N9P /Q**D(M#AhO/2."1";";%77k>Z[K\]G;D%IdOLL4& 1!4MaPQcURZ[]LL4==/):1#=STUSVV[\^#/Q& 99UD
LLy!),C"DE 
vvbhhy!"66	%56E	*
7+A3j	CD +99UD
LLyY'9:; 4 4
 !"24HHHER 2s
   I
Ic              #     K   t        | t              r%| j                  D ]  }t        |      E d{     y|  y7 w)z/Yield a left->right traversal of an aggregationN)r   r   valuesrB   )rQ   as     r6   rB   rB     s8     #wA+A...  	 /s   .?=?c           	         | j                  |||||      | f| j                  | j                  | j                  |      | j	                  ||      |r| j                         nt        j                  | j                         fS r2   )	_build_appendinputsnan_check_column_build_temps_build_combine_tempsuses_cuda_mutexr   Nois_categorical)basedshaperR   r.   r-   rX   r/   s          r6   rI   rI     sy     	664NK	$!!$4"&M,<,<	 	ru   c                    t        | |      D cg c]  \  }}|j                  |       c}}|rdd l}|nt        fdS c c}}w )Nr   c                0     t         fdD              S )Nc              3  0   K   | ]  } |        y wr2   r=   )r4   r>   rZ   shapes     r6   r7   z0make_create.<locals>.<lambda>.<locals>.<genexpr>-  s     Hx!q5xs   )tuple)r   rZ   creatorss   `r6   <lambda>zmake_create.<locals>.<lambda>-  s    HxHHru   )rH   _build_createrE   rF   )rU   rW   r.   rV   r\   rE   rZ   r   s         @@r6   rJ   rJ   &  sI    14UG1DE1Dv1"1DEHHH Fs   Ac                      fd}|S )Nc                    t         fdD              }	rrdd l}dd l}ddlm}  ||j
                         |d      k\  r"|j                  |t        j                        }n!|j                  dt        j                        }||fz  }|S )Nc              3  B   K   | ]  }|j                          y wr2   )apply)r4   r>   r.   dfs     r6   r7   z*make_info.<locals>.info.<locals>.<genexpr>2  s     4t!AGGB%tr8   r   )Versionz0.57)dtype)r   )	r   rE   numbapackaging.versionr   __version__zerosrF   uint32)
r   canvas_shaperetrE   r   r   mutex_arrayr^   r.   r   s
   `      r6   re   zmake_info.<locals>.info1  ss    4t441u(()WV_<"jjRYYjG"jjRYYj?K>!C
ru   r=   )r^   r.   r   re   s   ``` r6   rL   rL   0  s     Kru   c                ,  012 d t               D        1t        |       t        |      z   }i }t        d |D              }|r	t        |d<   t        d |D              }t        d |D              }	|	r|dgz  }t        |d<   t
        |d<   |D 
cg c]  }
t        1       }}
t        t        ||            0i }g }g }|j                  }|5d	j                  t        |      D cg c]  }d
t        |      z    c}      2nd 2d}i }i }t        j                  t        j                         rqt        j#                  d|        t        j#                  d|	        0j%                         D ].  \  }}t        j#                  d| d| dt'        |dd               0 d20fd}t)        |      D ]  \  }\  }} }}}}}}| xr |t*        j,                  k(  }|j/                  t        |1fd|D                     t        1      }t        j#                  d| d|        |||<   | D 
cg c]  }
0|
   	 } }
|r2t1        |d   t2              r| j5                  02fd|dd  D               nV|| j5                  0fd|D               n9|r| j5                  02fd|D               n| j5                  02fd|D               |r0|d      }!|j7                  |!d       }"|"Gt1        |d   t2              rdnd}#dt        1       }"|"||!<   |j9                  |" d|! d2 d|# d        ||   }$|j9                  |$ d!|$ d"|" d       | j5                  |D 
cg c]  }
||
   	 c}
       |r| d#d$gz  } |r|r|j;                          t=        |       dk(  xr | d   j?                         }%|%r9| d   }&t1        |&t@              r|&jB                  }&tE        |&jF                        }'|j7                  |'d       }(|(d u })|)rt        1      }(|(||'<   | j9                  |(       |j;                         }*|r|s|j9                   |d%             |)r|j9                  |( d!|*        n|j9                  |*       |d}+n/0|    d2 d},|d&   }*d'|, d(|d&<   |j9                  d)|*        d)}+|j9                  |+ d*|( d+       |j9                  d)|+ | d,d	j                  |        d        n|r|s|j9                   |d%             |rH0|    d2 d},|j9                  d'|, d(       |j9                  d)| d,d	j                  |        d        n&|j9                  | d,d	j                  |        d        |r|j9                   |d             |} ||j%                         D -.cg c]  \  }-}.d-jI                  |.0|-          c}.}-z   |z   }|r |d%      g|z    |d      gz   }|rd#d$g|z   }|1d.jI                  d	j                  |      d/j                  |            }/n1d0jI                  2d	j                  |      d/j                  |            }/t        j#                  |/       tK        |/|       tM        |d1         |	fS c c}
w c c}w c c}
w c c}
w c c}.}-w )3Nc              3  >   K   | ]  }d j                  |        yw)z_{0}Nformatrx   s     r6   r7   zmake_append.<locals>.<genexpr>B  s     /w!V]]1ws   c              3  &   K   | ]	  }|d      ywr;   r=   r4   calls     r6   r7   zmake_append.<locals>.<genexpr>E  s     0%$d1g%rz   r   c              3  H   K   | ]  }|d    t         j                  k(    yw   N)r   Globalr   s     r6   r7   zmake_append.<locals>.<genexpr>H  s      NDG}';';;    "c              3  H   K   | ]  }|d    t         j                  k7    ywr   )r   r   r   s     r6   r7   zmake_append.<locals>.<genexpr>I  s      Ledd1g)9)99er   _cuda_mutexr)   r*   z, ry   Fzglobal_cuda_mutex zany_uses_cuda_mutex zarg_lk  rO   c                &    | rdnd}| dd    dS )Nr)   r*   (r   z	, (y, x))r=   )lockr   arg_lks     r6   get_cuda_mutex_callz(make_append.<locals>.get_cuda_mutex_calld  s'    $( .Aq./y99ru   c              3  4   K   | ]  }t                y wr2   )r   )r4   ry   r   s     r6   r7   zmake_append.<locals>.<genexpr>k  s     #?ADKs   zfunc r   c              3  H   K   | ]  }d j                  |           ywz{0}[{1}]Nr   )r4   colr   	subscripts     r6   r7   zmake_append.<locals>.<genexpr>q  s#     VXc
))&+yAX   "r   c              3  F   K   | ]  }d j                  |           yw)z{0}Nr   )r4   ry   r   s     r6   r7   zmake_append.<locals>.<genexpr>s  s     >AVAY/s   !c              3  H   K   | ]  }d j                  |           yw)z{0}[{1}][1]Nr   r4   ry   r   r   s     r6   r7   zmake_append.<locals>.<genexpr>u  s*      '!%A &,,VAY	B!%r   c              3  H   K   | ]  }d j                  |           ywr   r   r   s     r6   r7   zmake_append.<locals>.<genexpr>x  s*      '!%A #))&)Y?!%r    z[0]r   z = int([]r~   z = z[:, :, 	aa_factorprev_aa_factorTrj   zif not isnull(z):z    zif z >= 0:z(x, y, z{0} = {1}[y, x]zdef append(x, y, {0}):
    {1}z
    z#def append({0}, x, y, {1}):
    {2}rc   )r   boolreturnstr)'r   rA   r   r   r)   r*   r   dictrH   ndimsr   ranger   r   isEnabledForloggingDEBUGr   itemsgetattrr   r   Localupdater   r   extendr	   rc   poplenr   r   r   hashselectorr   r   r   )3rU   r^   r]   rS   r-   r   r   need_isnullglobal_cuda_mutexrd   ry   	signaturelocal_lkheadbodyr   nprev_local_cuda_mutexcategorical_argswhere_selectorskvr   indexr   r   r`   _r   rs   local_cuda_mutex	func_nameargscategorical_argcat_name	col_indexargr   r   selector_hashupdate_index_arg_namenew_selector	prev_body
whitespacevarrQ   r   r   r   r   r   s3                                                   @@@r6   rK   rK   A  s   /uw/E%[4:%FI0%00K$	(NNNLeLL 	=/!'6	#$):	%&&,-fefI-#fi()FHDDKKEIIU5\B\sSV|\BC		!O7==))*;)<=>+,?+@ABLLNDAqLL71#Qqc71h+E*FGH #:
  	]\eT#3UAP[00[_H[H[5[E#?#?@AK	uYKq/0#	)#()5aq	5):d1g~>KKVTRSRTXVV]KK>>>KK '!%' ' KK '!%' ' $T!WoO'++OTBH",T!Wn"EB5	 e.4< 1xj/@)Ai[XYZ[E"CKK3%s3%wxj:;%0%QXa[%01["233D 5HHJu:?:uQx'8'8':#AhO/2."1";"; !9!9:M$3$7$7t$L!0D8L(,U%1F.KK-. 
I(=/5645SDEI&
  '
 012!I;a@ H	+C53Rd9+./#
KK:,c*?)@GHKK$zl9+WTYYt_<MQOP(=/56 012!I;a@nSE45d9+WTYYt_4EQGHyk40ACDKK+E23 0u  x $,NN$46$4ysD %++D&+>$46 68<=D #D)*T15H5O4PP "23i?	}!6$))I"6d8KL 	&DIIi$8(--:MN 	 LLy8$%':::O . C0 *2 1z6s   Y<"Z'ZZ
Zc                   t        d t        |       D              }|j                  t        d t        |       D                     | D cg c]  }|j                          c}dd  dgz   }	t	        | ||||	      D 
cg c]:  \  }}
}}}|rd n|j                  |
|||      |f|z   |z   D cg c]  }||   	 c}f< c}}}}}
}fd}|S c c}w c c}w c c}}}}}
}w )Nc              3  *   K   | ]  \  }}||f  y wr2   r=   r4   r
  r	  s      r6   r7   zmake_combine.<locals>.<genexpr>       8'7Va1a&'7   c              3  `   K   | ]&  \  }}t        |t              s|j                  |f ( y wr2   )r   r   r   r  s      r6   r7   zmake_combine.<locals>.<genexpr>  s+     \6FFQ*UVXZJ[Q'6Fs   ..r   Fc                    t        d t        |  D              }g }t              D ]E  \  }\  }}| |t        ||       }|r|j                  |d d d          5|j	                  |       G t        |      S )Nc              3  F   K   | ]  }t        j                  |        y wr2   )rF   stack)r4   bss     r6   r7   z0make_combine.<locals>.combine.<locals>.<genexpr>  s     ?->rbhhrl->s   !rj   )r   rH   r	   r   rc   )	base_tuplesrU   r   r   r   indsr   r   r]   s	          r6   rf   zmake_combine.<locals>.combine  s    ?S+->??&)-&?"HltT|T5)*D 

4":&

4  '@ Szru   )r   r   r   r   rH   _build_combine)rU   rW   r`   ra   r-   r.   r/   r   rV   r   r\   tctr  ry   rf   r   r]   s                   @@r6   rM   rM     s    8y'788F
MM$\i6F\\] ,115aQZZ\51M&qr*eW4 &)Oa%bd d%b!!Q2q dQ--aD+N#$$(R-0-Qvay-02%bdE N+ 2 1ds   C-C2C>CCc                   t        d t        |       D              }t        t              r{g t	        j
                  j                        D ]M  \  }}t        | ||||      }	 |j                  ||      } | D 	cg c]  }	||	   	 }
}	j                  |||
f       O dfd	}|S j                  |      S # t        $ r Y Mw xY wc c}	w )Nc              3  *   K   | ]  \  }}||f  y wr2   r=   r  s      r6   r7   z make_finalize.<locals>.<genexpr>  r  r  c                    D ci c]  \  }}}| |t        ||       |fi | }}}}
j                  d   }dD ci c]  }|||   j                  |    }	}t        j                  ||	      S c c}}}w c c}w )Nr   )x_rangey_range)attrs)r	   keysr.  xrDataset)rU   r.   kwargskey	finalizerr%  datar   attrr.  rQ   r]   s             r6   rg   zmake_finalize.<locals>.finalize  s    27927.i 3tU#3TDVDD27  9
 88A;D>TU>TdT4:++D11>TEU::d%009 Vs
    A2A9)F)r   r   r   r   rH   r/  r   rN   r3   AttributeErrorrc   _build_finalize)rU   rQ   rR   r.   r/   r   r3  valfrV   r%  rg   r]   s    `          @r6   rN   rN     s    8y'788F#wCHHcjj1HCeS&$DA(({; (--u!F1IuD-LL#q$( 2		1 ""6**# " -s   &B:<C	:	CCc                V    d| D ]  }|j                         sd n dfd}|fS )NTFc                N     t        t        t         fdD                     S )Nc              3  B   K   | ]  }|j                          y wr2   )_antialias_stage_2)r4   rV   rZ   rX   s     r6   r7   zDmake_antialias_stage_2.<locals>.antialias_stage_2.<locals>.<genexpr>#  s&      !1*/Q "#!5!5nl!S*/r8   )r   rH   r   )rZ   rU   rX   s   `r6   rY   z1make_antialias_stage_2.<locals>.antialias_stage_2"  s+    S& !1*/!1 1 2 3 	3ru   )r   r+   )_antialias_requires_2_stages)rT   rU   redrY   rX   s    `  @r6   rD   rD     s;    
 N++-"N 
3 ,,,ru   )rp   r   rq   floatrr   r   rs   r   )r   r   )F
__future__r   	itertoolsr   r   typingr   toolzr   r   r   r	   r
   r   r   numpyrF   xarrayr0  r-   r   
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(   )datashader.transfer_functions._cuda_utilsr)   r*   ImportErrordatashader.antialiasr+   __all__	getLoggerr   r   r,   rt   rG   rB   rI   rJ   rL   rK   rM   rN   rD   r=   ru   r6   <module>rO     s    "     5 5     + Q Q     4\ <  
  
		8	$ 	8=EW\ q 	qh*'59*'HL*'ZFIRI"V;r@+:-A  4)3&O&4s   6C 
CC