
    |LeU                      d dl mZ d dlZd dlmZ d dlmZ d dlZd dl	m
Z
mZmZmZ d dl	mZ d dlmZmZ d dlZd dlmZmZ d d	lmZmZ d d
l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+ n# e,$ r d\  ZZ Z!Z"Z#Z$Z%Z&Z'Z(Z)Z*Z+Y nw xY w	 d dl-Z-d dl.Z/n# e0$ r dxZ-Z/Y nw xY wddl1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@  G d de          ZA G d de          ZB G d de2          ZC G d deC          ZD G d deC          ZE G d deE          ZF G d deF          ZG G d deG          ZH G d d eE          ZI G d! d"e2          ZJ G d# d$eJ          ZK G d% d&eK          ZL G d' d(eL          ZM G d) d*eJ          ZN G d+ d,eK          ZO G d- d.eJ          ZP G d/ d0eJ          ZQ G d1 d2eQ          ZR G d3 d4eQ          ZS G d5 d6eS          ZT G d7 d8eQ          ZU G d9 d:eQ          ZV G d; d<eQ          ZW G d= d>eN          ZX G d? d@eJ          ZY G dA dBeJ          ZZ G dC dDeJ          Z[ G dE dFeJ          Z\ G dG dHe\          Z] G dI dJe\          Z^ G dK dLeK          Z_ G dM dNe_          Z` G dO dPe`          Za G dQ dRe`          Zb G dS dTe_          Zc G dU dVe_          Zd G dW dXeJ          Ze G dY dZeQ          Zf G d[ d\e2          Zg G d] d^eK          Zh G d_ d`eh          Zi G da dbeh          Zj G dc dde_          Zk G de dfek          Zl G dg dhek          Zm en eodi  ep            q                                D                                 ddgz   ZrdS )j    )annotationsN)Enum)Version)dshape	isnumericRecordOption)	coretypes)concatunique)AntialiasCombinationAntialiasStage2)isminus1isnull)cuda)cuda_atomic_nanmincuda_atomic_nanmax	cuda_argscuda_row_min_in_placecuda_nanmax_n_in_place_4dcuda_nanmax_n_in_place_3dcuda_nanmin_n_in_place_4dcuda_nanmin_n_in_place_3dcuda_row_max_n_in_place_4dcuda_row_max_n_in_place_3dcuda_row_min_n_in_place_4dcuda_row_min_n_in_place_3dcuda_shift_and_insert)NNNNNNNNNNNNN   )Exprngjitnansum_missingnanmax_in_placenansum_in_placerow_min_in_placenanmax_n_in_place_4dnanmax_n_in_place_3dnanmin_n_in_place_4dnanmin_n_in_place_3drow_max_n_in_place_4drow_max_n_in_place_3drow_min_n_in_place_4drow_min_n_in_place_3dshift_and_insertc                      e Zd ZdZdZdS )SpecialColumna  
    Internally datashader identifies the columns required by the user's
    Reductions and extracts them from the supplied source (e.g. DataFrame) to
    pass through the dynamically-generated append function in compiler.py and
    end up as arguments to the Reduction._append* functions. Each column is
    a string name or a SpecialColumn. A column of None is used in Reduction
    classes to denote that no column is required.
    r   N)__name__
__module____qualname____doc__RowIndex     5lib/python3.11/site-packages/datashader/reductions.pyr0   r0   -   s          HHHr7   r0   c                      e Zd ZdZdZdZdZdS )UsesCudaMutexaY  
    Enum that encapsulates the need for a Reduction to use a CUDA mutex to
    operate correctly on a GPU. Possible values:

    No: the Reduction append_cuda function is atomic and no mutex is required.
    Local: Reduction append_cuda needs wrapping in a mutex.
    Global: the overall compiled append function needs wrapping in a mutex.
    r   r      N)r1   r2   r3   r4   NoLocalGlobalr6   r7   r8   r:   r:   9   s)          
BEFFFr7   r:   c                  F    e Zd ZdZddZed             Zed             ZdS )	
Preprocessz#Base clase for preprocessing steps.columnstr | SpecialColumn | Nonec                    || _         d S NrA   selfrA   s     r8   __init__zPreprocess.__init__I   s    r7   c                    | j         fS rD   rE   rG   s    r8   inputszPreprocess.inputsL   s    ~r7   c                    d S rD   r6   rJ   s    r8   nan_check_columnzPreprocess.nan_check_columnP   s    tr7   NrA   rB   )r1   r2   r3   r4   rH   propertyrK   rM   r6   r7   r8   r@   r@   G   sd        --      X   X  r7   r@   c                      e Zd ZdZd ZdS )extractz=Extract a column from a dataframe as a numpy array of values.c                   | j         t          j        u rjd}t          |t          j                  r|j        |         }|j        d         }n3t          |dd           }t          |p||d          }t          |          }t          rt          |t          j
                  r| j         t          j        u r$t          j        |||z   t          j                  S || j                  j        j        dk    rt          j        }nd}t%          t          j                  t%          d          k    r!|| j                                      |          S t          j        || j                                      |	                    S | j         t          j        u rJ|r$t          j        |||z   t          j                  S t          j        |||z   t          j                  S t          |t          j                  r]|rIt          || j                  j        t          j                  st          j        || j                            S || j                  j        S || j                  j        S )
N_datashader_row_offset_datashader_row_lengthattrsr   dtypef22.02na_valuefillna)rA   r0   r5   
isinstancexrDatasetrU   getattrlencudf	DataFramecparangenpint64rW   kindnanr   __version__to_cupyarrayto_gpu_arraydatandarrayasarrayvalues)rG   dfr   	attr_name
row_offset
row_lengthrU   nullvals           r8   applyzextract.applyW   s   ;-0000I"bj)) %Xi0
X&>?

GT22$U[b)Q??
 WW
 	*Jr4>22 	*{m444yZ*-DBHUUUU$+$)S00&t'((GG,<,<<<$+...@@@8Bt{O888HHIII[M222 VyZ*-DBHUUUUyZ*-DBHUUUUBJ'' 	* ,Jr$+';RZHH ,z"T[/222$+++dk?))r7   N)r1   r2   r3   r4   rx   r6   r7   r8   rQ   rQ   U   s)        GG!* !* !* !* !*r7   rQ   c                  :    e Zd ZdZed             Zd Zd Zd ZdS )CategoryPreprocessz*Base class for categorizing preprocessors.c                    | j         S z"Returns name of categorized columnrE   rJ   s    r8   
cat_columnzCategoryPreprocess.cat_column}   s     {r7   c                     t          d          )z7Returns list of categories corresponding to input shapezcategories not implementedNotImplementedErrorrG   input_dshapes     r8   
categorieszCategoryPreprocess.categories   s    !">???r7   c                     t          d          )zValidates input shapezvalidate not implementedr   rG   	in_dshapes     r8   validatezCategoryPreprocess.validate   s    !"<===r7   c                     t          d          )z3Applies preprocessor to DataFrame and returns arrayzapply not implementedr   rG   rs   r   s      r8   rx   zCategoryPreprocess.apply   s    !"9:::r7   N)	r1   r2   r3   r4   rO   r}   r   r   rx   r6   r7   r8   rz   rz   {   sc        44  X@ @ @> > >; ; ; ; ;r7   rz   c                  $    e Zd ZdZd Zd Zd ZdS )category_codesal  
    Extract just the category codes from a categorical column.

    To create a new type of categorizer, derive a subclass from this
    class or one of its subclasses, implementing ``__init__``,
    ``_hashable_inputs``, ``categories``, ``validate``, and ``apply``.

    See the implementation of ``category_modulo`` in ``reductions.py``
    for an example.
    c                0    |j         | j                 j        S rD   )measurerA   r   r   s     r8   r   zcategory_codes.categories   s    #DK0;;r7   c                    | j         |j        vrt          d          t          |j        | j                  t
          j                  st          d          d S )Nspecified column not foundzinput must be categorical)rA   dict
ValueErrorr^   r   ctCategoricalr   s     r8   r   zcategory_codes.validate   sW    ;in,,9:::)+DK8".II 	:8999	: 	:r7   c                t   t           rt          |t           j                  r|t          t           j                  t          d          k    r)|| j                 j        j                                        S || j                 j        j        	                                S || j                 j        j        j
        S NrY   )rc   r^   rd   r   rk   rA   catcodesrl   rn   rr   r   s      r8   rx   zcategory_codes.apply   s     	4Jr4>22 	4t'((GG,<,<<<$+*088:::dk?&,99;;;dk?&,33r7   N)r1   r2   r3   r4   r   r   rx   r6   r7   r8   r   r      sK        	 	< < <: : :4 4 4 4 4r7   r   c            	           e Zd ZdZej        ej        ej        ej        ej	        ej
        ej        ej        ej        h	Zd fd	Z fdZd Zd Zd Z xZS )	category_moduloz
    A variation on category_codes that assigns categories using an integer column, modulo a base.
    Category is computed as (column_value - offset)%modulo.
    r   c                f    t                                          |           || _        || _        d S rD   )superrH   offsetmodulo)rG   rA   r   r   	__class__s       r8   rH   zcategory_modulo.__init__   s-       r7   c                b    t                                                      | j        | j        fz   S rD   )r   _hashable_inputsr   r   rG   r   s    r8   r   z category_modulo._hashable_inputs   s'    ww''))T[$+,FFFr7   c                D    t          t          | j                            S rD   )listranger   r   s     r8   r   zcategory_modulo.categories   s    E$+&&'''r7   c                    | j         |j        vrt          d          |j        | j                  | j        vrt          d          d S )Nr   zinput must be an integer column)rA   r   r   r   IntegerTypesr   s     r8   r   zcategory_modulo.validate   sP    ;in,,9:::T[)1BBB>??? CBr7   c                0   || j                  | j        z
  | j        z  }t          rlt	          |t          j                  rRt          t          j                  t          d          k    r|                                S |	                                S |j
        S r   )rA   r   r   rc   r^   Seriesr   rk   rl   rn   rr   )rG   rs   r   results       r8   rx   zcategory_modulo.apply   s~    T[/DK/4;> 	!Jr4;// 	!t'((GG,<,<<<~~'''&&(((= r7   )r   )r1   r2   r3   r4   r   bool_uint8uint16uint32uint64int8int16int32rh   r   rH   r   r   r   rx   __classcell__r   s   @r8   r   r      s          Hbh	29biRTRZHbh(L     
G G G G G( ( (@ @ @! ! ! ! ! ! !r7   r   c                  :     e Zd ZdZd fd	Z fdZd Zd Z xZS )category_binninga  
    A variation on category_codes that assigns categories by binning a continuous-valued column.
    The number of categories returned is always nbins+1.
    The last category (nbin) is for NaNs in the data column, as well as for values under/over the
    binned interval (when include_under or include_over is False).

    Parameters
    ----------
    column:   column to use
    lower:    lower bound of first bin
    upper:    upper bound of last bin
    nbins:     number of bins
    include_under: if True, values below bin 0 are assigned to category 0
    include_over:  if True, values above the last bin (nbins-1) are assigned to category nbin-1
    Tc                    t                                          ||dz              || _        ||z
  t          |          z  | _        || _        |rdn|| _        |r|dz
  n|| _        d S Nr   r   )r   rH   bin0floatbinsizenbins	bin_underbin_over)rG   rA   lowerupperr   include_underinclude_overr   s          r8   rH   zcategory_binning.__init__   sm    +++	u5
+6$0;qer7   c                z    t                                                      | j        | j        | j        | j        fz   S rD   )r   r   r   r   r   r   r   s    r8   r   z!category_binning._hashable_inputs   s1    ww''))TYdnVZVc,dddr7   c                @    | j         |j        vrt          d          d S )Nr   )rA   r   r   r   s     r8   r   zcategory_binning.validate   s(    ;in,,9::: -,r7   c                   t           rt          |t           j                  rt          t           j                  t          d          k    r,|| j                                     t          j                  }n3t          j	        || j                 
                    d                    }t          j        |          }n3|| j                                                 }t          j        |          }|| j        z
  | j        z  }d||<   |                    t"                    }| j        ||dk     <   | j        ||| j        k    <   | j        ||<   |S )NrY   rZ   Tr\   r   )rc   r^   rd   r   rk   rA   rl   re   rj   rm   rn   isnanto_numpyrg   r   r   astypeintr   r   r   )rG   rs   r   rr   
nan_valuesindex_floatindexs          r8   rx   zcategory_binning.apply   s    	*Jr4>22 	*t'((GG,<,<<<DK00"&0AA"T[/">">d">"K"KLL&))JJ_--//F&))J	)T\9 #$J""3''>eai%)]etz!" Jjr7   )TT)	r1   r2   r3   r4   rH   r   r   rx   r   r   s   @r8   r   r      s          < < < < < <e e e e e; ; ;      r7   r   c                  `     e Zd ZdZ fdZed             Zed             Zd Zd Z	d Z
 xZS )category_valueszVExtract a category and a value column from a dataframe as (2,N) numpy array of values.c                X    t                                          |           || _        d S rD   )r   rH   categorizer)rG   r   value_columnr   s      r8   rH   zcategory_values.__init__  s)    &&&&r7   c                (    | j         j        | j        fS rD   r   rA   rJ   s    r8   rK   zcategory_values.inputs  s     '55r7   c                    | j         j        S r|   r   rJ   s    r8   r}   zcategory_values.cat_column  s     &&r7   c                    | j         j        S rD   )r   r   r   s     r8   r   zcategory_values.categories  s    **r7   c                6    | j                             |          S rD   )r   r   r   s     r8   r   zcategory_values.validate  s    ((333r7   c                   | j                             ||          }t          rQt          |t          j                  r6dd l}| j        t          j        k    rd}n*|| j                 j	        j
        dk    rt          j        }nd}|                    |          }| j        t          j        k    r.t          t          j                                      ||          }nt          t          j                  t          d          k    r"|| j                                     |          }n3|                    || j                                     |                    }|                    ||fd          S | j        t          j        k    r.t          t          j                                      ||          }n|| j                 j        }t          j        ||fd          S )Nr   rX   rY   rZ   axis)r   rx   rc   r^   rd   cupyrA   r0   r5   rW   ri   rg   rj   rq   rQ   r   rk   rl   r]   stackrr   )rG   rs   r   ar   rw   bs          r8   rx   zcategory_values.apply  s   ""2t,, 	-Jr4>22 	-KKK{m444DK&+s22&QA{m444M23399"dCC)**gg.>.>>>t{O++W+==LLDK!7!7!@!@AA::q!f2:...{m444M23399"dCCt{O*8QF,,,,r7   )r1   r2   r3   r4   rH   rO   rK   r}   r   r   rx   r   r   s   @r8   r   r     s        ``' ' ' ' ' 6 6 X6 ' ' X'+ + +4 4 4- - - - - - -r7   r   c                  >   e Zd ZdZd!d"dZed             Zd#d	Zd
 Zd Z	ed             Z
d Zd Zd Zd$dZd Zd Zd%dZd Zd Zd%dZd Zed             Zed             Zed             Zed             Zed             Zed             Zed              ZdS )&	Reductionz"Base class for per-bin reductions.NrA   rB   c                "    || _         d | _        d S rD   )rA   _nan_check_columnrF   s     r8   rH   zReduction.__init__7  s    !%r7   c                <    | j         t          | j                   S d S rD   )r   rQ   rJ   s    r8   rM   zReduction.nan_check_column;  s!    !-412224r7   returnr:   c                    t           j        S )a1  Return ``True`` if this Reduction needs to use a CUDA mutex to
        ensure that it is threadsafe across CUDA threads.

        If the CUDA append functions are all atomic (i.e. using functions from
        the numba.cuda.atomic module) then this is ``False``, otherwise it is
        ``True``.
        )r:   r<   rJ   s    r8   uses_cuda_mutexzReduction.uses_cuda_mutexB  s     r7   c                    dS )a  Return ``True`` if this Reduction uses a row index virtual column.

        For some reductions the order of the rows of supplied data is
        important. These include ``first`` and ``last`` reductions as well as
        ``where`` reductions that return a row index. In some situations the
        order is intrinsic such as ``first`` reductions that are processed
        sequentially (i.e. on a CPU without using Dask) and no extra column is
        required. But in situations of parallel processing (using a GPU or
        Dask) extra information is needed that is provided by a row index
        virtual column.

        Returning ``True`` from this function will cause a row index column to
        be created and passed to the ``append`` functions in the usual manner.
        Fr6   rG   r   partitioneds      r8   uses_row_indexzReduction.uses_row_indexL  s	     ur7   c                    | j         t          j        k    rd S | j         |j        vrt	          d          t          |j        | j                            st	          d          d S )Nr   zinput must be numeric)rA   r0   r5   r   r   r   r   r   s     r8   r   zReduction.validate]  sh    ;-000F;in,,9:::*4;788 	64555	6 	6r7   c                ,    t          | j                  fS rD   rQ   rA   rJ   s    r8   rK   zReduction.inputse      $$&&r7   c                    dS )z?Return ``True`` if this is or contains a categorical reduction.Fr6   rJ   s    r8   is_categoricalzReduction.is_categoricali  s    ur7   c                    dS )z]Return ``True`` if this is a ``where`` reduction or directly wraps
        a where reduction.Fr6   rJ   s    r8   is_wherezReduction.is_wherem  s	     ur7   c                    dS )NFr6   rJ   s    r8   _antialias_requires_2_stagesz&Reduction._antialias_requires_2_stagesr  s	     ur7   tuple[AntialiasStage2]c                @    t          t          |            d          )Nz"._antialias_stage_2 is not defined)r   typerG   self_intersectarray_modules      r8   _antialias_stage_2zReduction._antialias_stage_2x  s!     "T$ZZ"S"S"STTTr7   c                    | fS rD   r6   r   s      r8   _build_baseszReduction._build_bases~  s	    wr7   c                    dS Nr6   r6   r   s      r8   _build_combine_tempszReduction._build_combine_temps  s	    
 rr7   Fc                    dS r   r6   rG   r   s     r8   _build_tempszReduction._build_temps  s	     rr7   c                   t          |j        dd           }|Dt          |j        j                  dk    r'|j        j        d         }t	          |d                   }t          |t                    rt	          |j                  }|t	          t          j	                  k    r| j
        S |t	          t          j                  k    r| j        S |t	          t          j                  k    r| j        S |t	          t          j                  k    r| j        S |t	          t          j                  k    r| j        S t'          dt                     )Nfieldsr   r   zUnexpected dshape )ra   r   rb   r  r   r^   r	   tyr   r   _create_boolfloat32_create_float32_nanfloat64_create_float64_nanrh   _create_int64r   _create_uint32r   )rG   required_dshaper  first_fields       r8   _build_createzReduction._build_create  s$   0(DAA#o&=&D"E"E"I"I)18;K$[^44Oov.. 	9$_%788OfRX....$$rz 2 222++rz 2 222++rx 0 000%%ry 1 111&&%&C6&C&CDDDr7   c                    |r.|r| j         | j        S |r| j        S | j         | j        S | j        S |r| j         | j        S |r| j        S | j         | j        S | j        S rD   )	rA   _append_no_field_antialias_cuda_append_antialias_cuda_append_no_field_cuda_append_cuda_append_no_field_antialias_append_antialias_append_no_field_appendrG   r   schemar   	antialiasr   s         r8   _build_appendzReduction._build_append  s     	$ )T[0;; )22$11(( $T[066 $--$,,|#r7   c                    | j         S rD   )_combinerG   r   r  r   r   categoricals         r8   _build_combinezReduction._build_combine  s
    }r7   c                    | j         S rD   )	_finalize)rG   r   s     r8   _build_finalizezReduction._build_finalize  s
    ~r7   c                0    |                     | d          S )NboolrV   zerosshaper   s     r8   r  zReduction._create_bool  s    !!%v!666r7   c                <    |                     | |j        d          S )Nf4rV   fullrj   r)  s     r8   r  zReduction._create_float32_nan         (8 EEEr7   c                <    |                     | |j        d          S Nf8rV   r-  r)  s     r8   r
  zReduction._create_float64_nan  r/  r7   c                0    |                     | d          S r1  )emptyr)  s     r8   _create_float64_emptyzReduction._create_float64_empty      !!%t!444r7   c                0    |                     | d          S r1  r'  r)  s     r8   _create_float64_zerozReduction._create_float64_zero  r6  r7   c                2    |                     | dd          S )Nr   i8rV   )r.  r)  s     r8   r  zReduction._create_int64  s      $ 777r7   c                0    |                     | d          S )Nu4rV   r'  r)  s     r8   r  zReduction._create_uint32  r6  r7   rD   rN   r   r:   r   r   F)r1   r2   r3   r4   rH   rO   rM   r   r   r   rK   r   r   r   r   r   r   r  r  r  r!  r$  staticmethodr  r  r
  r5  r8  r  r  r6   r7   r8   r   r   5  s       ,,& & & & &   X         "6 6 6 ' ' X'    
  U U U U       
E E E.$ $ $(      7 7 \7 F F \F F F \F 5 5 \5 5 5 \5 8 8 \8 5 5 \5 5 5r7   r   c                  \     e Zd ZdZd fd	Zed             Z fdZed	d            Z	 xZ
S )
OptionalFieldReductionzOBase class for things like ``count`` or ``any`` for which the field is optionalNc                J    t                                          |           d S rD   )r   rH   rG   rA   r   s     r8   rH   zOptionalFieldReduction.__init__  s!         r7   c                >    | j         t          | j                   fndS r   )rA   rQ   rJ   s    r8   rK   zOptionalFieldReduction.inputs  s!    *.+*A$$&&rIr7   c                \    | j         #t                                          |           d S d S rD   )rA   r   r   rG   r   r   s     r8   r   zOptionalFieldReduction.validate  s1    ;"GGY''''' #"r7   Fc                2    t          j        | d         fi |S Nr   r_   	DataArraybasesr   kwargss      r8   r#  z OptionalFieldReduction._finalize      |E!H/////r7   rD   r?  )r1   r2   r3   r4   rH   rO   rK   r   r@  r#  r   r   s   @r8   rB  rB    s        YY! ! ! ! ! ! J J XJ( ( ( ( ( 0 0 0 \0 0 0 0 0r7   rB  c                  >     e Zd ZdZd fd	Zd Z fdZ fdZ xZS )	&SelfIntersectingOptionalFieldReductionz
    Base class for optional field reductions for which self-intersecting
    geometry may or may not be desirable.
    Ignored if not using antialiasing.
    NTc                X    t                                          |           || _        d S rD   r   rH   r   rG   rA   r   r   s      r8   rH   z/SelfIntersectingOptionalFieldReduction.__init__  )       ,r7   c                    | j          S rD   r   rJ   s    r8   r   zCSelfIntersectingOptionalFieldReduction._antialias_requires_2_stages      &&&r7   c                    |r.|s,|r| j         | j        S | j        S | j         | j        S | j        S t                                          |||||          S rD   )rA   2_append_no_field_antialias_cuda_not_self_intersect)_append_antialias_cuda_not_self_intersect-_append_no_field_antialias_not_self_intersect$_append_antialias_not_self_intersectr   r  rG   r   r  r   r  r   r   s         r8   r  z4SelfIntersectingOptionalFieldReduction._build_append  ss     	E^ 	E 	E;&RRII;&MMDD ww$$VVT9nUUUr7   c                V    t                                                      | j        fz   S rD   r   r   r   r   s    r8   r   z7SelfIntersectingOptionalFieldReduction._hashable_inputs  &     ww''))T-@,BBBr7   NT	r1   r2   r3   r4   rH   r   r  r   r   r   s   @r8   rQ  rQ    s         
- - - - - -' ' 'V V V V V"C C C C C C C C Cr7   rQ  c                     e Zd ZdZd ZddZeed                         Zeed                         Z	eed                         Z
eed	                         Zeed
                         Zeed                         Ze ej        d          d                         Ze ej        d          d                         Ze ej        d          d                         Ze ej        d          d                         Ze ej        d          d                         ZddZed             Zed             ZdS )counta  Count elements in each bin, returning the result as a uint32, or a
    float32 if using antialiasing.

    Parameters
    ----------
    column : str, optional
        If provided, only counts elements in ``column`` that are not ``NaN``.
        Otherwise, counts every element.
    c                j    |rt          t          j                  nt          t          j                  S rD   )r   r   r  r   rG   r   r  r   r   s        r8   
out_dshapezcount.out_dshape  s&    %.Evbj!!!F294E4EEr7   r   r   c                    |r t          t          j        |j                  fS t          t          j        |j                  fS rD   r   r   SUM_1AGGrj   SUM_2AGGr   s      r8   r   zcount._antialias_stage_2  ?     	W#$8$A<CSTTVV#$8$A<CSTTVVr7   c                L    t          |          s||| fxx         dz  cc<   dS dS Nr   r   r   r   xyaggfields       r8   r  zcount._append%  s5     e}} 	1IIINIII1rr7   c                    t          |          s3t          ||| f                   r|||| f<   n||| fxx         |z  cc<   dS dS Nr   r   rp  rr  rs  rt  ru  	aa_factors        r8   r  zcount._append_antialias-  s]     e}} 	c!Q$i   '%AqD		AqD			Y&			1rr7   c                    t          |          s.t          ||| f                   s|||| f         k    r	|||| f<   dS dS rw  rp  rx  s        r8   r]  z*count._append_antialias_not_self_intersect8  S     e}} 	c!Q$i   IAqD	$9$9%AqD	qrr7   c                *    ||| fxx         dz  cc<   dS r   r6   rr  rs  rt  s      r8   r  zcount._append_no_fieldA  s"     	AqD			Q			qr7   c                h    t          ||| f                   r|||| f<   n||| fxx         |z  cc<   dS rI  rp  rr  rs  rt  ry  s       r8   r  z count._append_no_field_antialiasG  sJ     #ad) 	#!C1II1III"IIIqr7   c                b    t          ||| f                   s|||| f         k    r	|||| f<   dS dS rw  rp  r  s       r8   r\  z3count._append_no_field_antialias_not_self_intersectP  E     #ad) 		C1I 5 5!C1I1rr7   Tdevicec                    ||z  }t          |          s*t          ||| f|          }t          |          s||k     rdS dS rw  r   r   rr  rs  rt  ru  ry  valueolds          r8   r  zcount._append_antialias_cudaY  P     ie}} 	$S1a&%88Cc{{ cEkkqrr7   c                x    t          |          s*t          ||| f|          }t          |          s||k     rdS dS rw  r  rr  rs  rt  ry  r  s        r8   rZ  z8count._append_no_field_antialias_cuda_not_self_intersectc  I     i   	$S1a&)<<Cc{{ cIooqrr7   c                n    t          |          s%t          j                            ||| fd           dS dS ro  r   nb_cudaatomicaddrq  s       r8   r  zcount._append_cudal  s:     e}} 	NsQFA...1rr7   c                x    t          |          s*t          ||| f|          }t          |          s||k     rdS dS rw  r  r  s        r8   r  z%count._append_no_field_antialias_cudat  r  r7   c                L    t           j                            ||| fd           dS r   )r  r  r  r}  s      r8   r  zcount._append_no_field_cuda}  s'     	3A***qr7   Fc                "    |r| j         S | j        S rD   _combine_antialiasr  r  s         r8   r!  zcount._build_combine       	!**= r7   c                0    |                      dd          S )Nr   r<  r   rW   sumaggss    r8   r  zcount._combine      xxQdx+++r7   c                    | d         }t          dt          |                     D ]}t          || |                    |S Nr   r   )r   rb   r$   r  retis      r8   r  zcount._combine_antialias  F    1gq#d))$$ 	* 	*ACa))))
r7   Nr>  r?  )r1   r2   r3   r4   rh  r   r@  r!   r  r  r]  r  r  r\  r  jitr  rZ  r  r  r  r!  r  r  r6   r7   r8   re  re    sy        F F FW W W W 
  U \ 
  U \ 
  U \ 
  U \ 
  U \ 
  U \ W[   \ W[   \ W[   \ W[   \ W[   \! ! ! ! , , \,   \  r7   re  c                       e Zd ZdZ e            f fd	Zd ZddZed             Z	ed             Z
d Zd	 Zed
             Zd Zd Zed             ZddZd Zd ZddZd Zd Zd ZddZd Zd Z xZS )byaz  Apply the provided reduction separately per category.

    Parameters
    ----------
    cats: str or CategoryPreprocess instance
        Name of column to aggregate over, or a categorizer object that returns categories.
        Resulting aggregate has an outer dimension axis along the categories present.
    reduction : Reduction
        Per-category reduction function.
    c                J   t                                                       t          |t                    r|| _        n9t          |t
                    rt          |          | _        nt          d          | j        j        | _        | j        j        f| _	        t          |dd           x}| xj	        |d d d         z  c_	        n | xj	        t          |dd           fz  c_	        || _        | j        !t          | j        | j                  | _        d S | j        | _        d S )NzEfirst argument must be a column name or a CategoryPreprocess instancecolumnsr   rA   )r   rH   r^   rz   r   strr   	TypeErrorrA   r  ra   	reduction
val_columnr   
preprocess)rG   r}   r  r  r   s       r8   rH   zby.__init__  s    j"455 	e)D
C(( 	e-j99Dcddd&-(/1y)T:::GG LLGDDbDM)LLLLLWY$??AALL"?&-d.>PPDOOO".DOOOr7   c                    t          t          |           |                                 | j                                        | j        f          S rD   )hashr   r   r   r  rJ   s    r8   __hash__zby.__hash__  sE    T$ZZ!6!6!8!8$:J:[:[:]:]^% & & 	&r7   Fc                j     t           fd j                            |          D                       S )Nc              3  B   K   | ]}t          j        |          V  d S rD   r  r   ).0tmprG   s     r8   	<genexpr>z"by._build_temps.<locals>.<genexpr>  s0      \\3R(#..\\\\\\r7   )tupler  r  r  s   ` r8   r  zby._build_temps  s7    \\\\$.:U:UVZ:[:[\\\\\\r7   c                    | j         d         S rI  r  rJ   s    r8   r}   zby.cat_column      |Ar7   c                    | j         d         S Nr   r  rJ   s    r8   r  zby.val_column  r  r7   c                n    | j                             |           | j                            |           d S rD   )r  r   r  r   s     r8   r   zby.validate  s4      +++	*****r7   c                    | j                             |          }| j                            ||||          t	          t          fd|D                                 S )Nc                    g | ]}|fS r6   r6   )r  c	red_shapes     r8   
<listcomp>z!by.out_dshape.<locals>.<listcomp>  s    ;;;q)n;;;r7   )r   r   r  rh  r   r   )rG   r   r  r   r   catsr  s         @r8   rh  zby.out_dshape  s_    **<88N--lIt[YY	f;;;;d;;;<<===r7   c                    | j         fS rD   )r  rJ   s    r8   rK   z	by.inputs  s    !!r7   c                    dS rb  r6   rJ   s    r8   r   zby.is_categorical      tr7   c                4    | j                                         S rD   )r  r   rJ   s    r8   r   zby.is_where  s    ~&&(((r7   c                    | j         j        S rD   )r  rM   rJ   s    r8   rM   zby.nan_check_column  s    ~..r7   r   r:   c                4    | j                                         S rD   )r  r   rJ   s    r8   r   zby.uses_cuda_mutex  s    ~--///r7   c                8    | j                             ||          S rD   )r  r   r   s      r8   r   zby.uses_row_index  s    ~,,T;???r7   c                4    | j                                         S rD   )r  r   rJ   s    r8   r   zby._antialias_requires_2_stages  s    ~::<<<r7   r   c                    | j                             ||          }t          |d         j        |d         j        |d         j        d          fS )Nr   T)combinationzeron_reductionr   )r  r   r   r  r  r  rG   r   r   r  s       r8   r   zby._antialias_stage_2  sT    n//MMCF,>%(V[,/F,>,02 2 2 4 	4r7   c                H     t          j        j                   fdS )Nc                T     j                                       | fz   |          S rD   )r  r  )r*  r   n_catsr  rG   s     r8   <lambda>z"by._build_create.<locals>.<lambda>  s7     ,4>+G+G, ,"fY.,> ,> r7   )rb   r   r  )rG   r  r  s   ``@r8   r  zby._build_create  s;    _,344> > > > > > 	>r7   c                      j                             ||          }t          |          dk    r|d          u r|S t           fd|D                       S )Nr   r   c              3  B   K   | ]}t          j        |          V  d S rD   r  )r  baserG   s     r8   r  z"by._build_bases.<locals>.<genexpr>  s0      BBDR($//BBBBBBr7   )r  r   rb   r  )rG   r   r   rM  s   `   r8   r   zby._build_bases  s_    ++D+>>u::??uQx4//LBBBBEBBBBBBr7   c                >    | j                             |||||          S rD   )r  r  r  s         r8   r  zby._build_append  s     ~++FFD)^\\\r7   c                >    | j                             ||||d          S rb  )r  r!  r  s         r8   r!  zby._build_combine   s!    ~,,VYkSWXXXr7   c                8    | j                             ||          S rD   )r  r   r   s      r8   r   zby._build_combine_temps  s    ~224EEEr7   c                j     t           j                                                d fd	}|S )NFc                    t          j        |          }|dxx         j        gz  cc<   |d         j        <    j                                      | fd|i|S )Ndimscoordsr   )copydeepcopyr}   r  r$  )rM  r   rN  r  r   rG   s      r8   finalizez$by._build_finalize.<locals>.finalize	  sm     ]6**F6NNNt//NNN04F8T_-94>11&99%UUdUfUUUr7   r?  )r   r   r   )rG   r   r  r  s   `` @r8   r$  zby._build_finalize  sZ    D$//7788	V 	V 	V 	V 	V 	V 	V 	V r7   r?  r=  r>  )r1   r2   r3   r4   re  rH   r  r  rO   r}   r  r   rh  rK   r   r   rM   r   r   r   r   r  r   r  r!  r   r$  r   r   s   @r8   r  r    s       	 	 .3UWW / / / / / /8& & &] ] ] ]   X   X+ + +> > >
 " " X"  ) ) ) / / X/0 0 0 0@ @ @= = =4 4 4 4> > >
C C C] ] ]Y Y Y YF F F      r7   r  c                      e Zd ZdZd ZddZeed                         Zeed                         Z	eed                         Z
eed	                         ZeZe
ZddZed             Zed             ZdS )anyzWhether any elements in ``column`` map to each bin.

    Parameters
    ----------
    column : str, optional
        If provided, any elements in ``column`` that are ``NaN`` are skipped.
    c                j    |rt          t          j                  nt          t          j                  S rD   )r   r   r  r   rg  s        r8   rh  zany.out_dshape  s&    %.Dvbj!!!F284D4DDr7   r   r   c                B    t          t          j        |j                  fS rD   r   r   MAXrj   r   s      r8   r   zany._antialias_stage_2       4 8,:JKKMMr7   c                6    t          |          s	d||| f<   dS dS )NTr   r   rp  rq  s       r8   r  zany._append"  s)     e}} 	C1I1rr7   c                    t          |          s.t          ||| f                   s|||| f         k    r	|||| f<   dS dS rw  rp  rx  s        r8   r  zany._append_antialias*  r{  r7   c                    d||| f<   dS )NTr   r6   r}  s      r8   r  zany._append_no_field3  s     AqD	qr7   c                b    t          ||| f                   s|||| f         k    r	|||| f<   dS dS rw  rp  r  s       r8   r  zany._append_no_field_antialias9  r  r7   Fc                "    |r| j         S | j        S rD   r  r  s         r8   r!  zany._build_combineE  r  r7   c                0    |                      dd          S )Nr   r&  r  r  r  s    r8   r  zany._combineK  s    xxQfx---r7   c                    | d         }t          dt          |                     D ]}t          || |                    |S r  )r   rb   r#   r  s      r8   r  zany._combine_antialiasO  r  r7   Nr>  r?  )r1   r2   r3   r4   rh  r   r@  r!   r  r  r  r  r  r  r!  r  r  r6   r7   r8   r  r    s3        E E EN N N N 
  U \ 
  U \ 
  U \ 
  U \ L,! ! ! ! . . \.   \  r7   r  c                      e Zd ZdZd Zedd            Zed             Zd Z	ee
d                         Ze ej        d	          d
                         Zed             ZdS )	_upsamplez+"Special internal class used for upsamplingc                N    t          t          t          j                            S rD   r   r	   r   r	  rg  s        r8   rh  z_upsample.out_dshapeY      fRZ(()))r7   Fc                2    t          j        | d         fi |S rI  rJ  rL  s      r8   r#  z_upsample._finalize\  rO  r7   c                ,    t          | j                  fS rD   r   rJ   s    r8   rK   z_upsample.inputs`  r   r7   c                    | j         S rD   )r5  rG   r  s     r8   r  z_upsample._build_created  s     ))r7   c                    d S rD   r6   rq  s       r8   r  z_upsample._appendi  	     	r7   Tr  c                    d S rD   r6   rq  s       r8   r  z_upsample._append_cudao  r  r7   c                .    t          j        | d          S Nr   r   rg   nanmaxr  s    r8   r  z_upsample._combineu      yA&&&&r7   Nr?  )r1   r2   r3   r4   rh  r@  r#  rO   rK   r  r!   r  r  r  r  r  r6   r7   r8   r  r  W  s        55* * * 0 0 0 \0 ' ' X'* * *
 
  U \ W[   \ ' ' \' ' 'r7   r  c                  0    e Zd ZdZd Zedd            ZdS )FloatingReductionzBBase classes for reductions that always have floating-point dtype.c                N    t          t          t          j                            S rD   r  rg  s        r8   rh  zFloatingReduction.out_dshape|  r  r7   Fc                2    t          j        | d         fi |S rI  rJ  rL  s      r8   r#  zFloatingReduction._finalize  rO  r7   Nr?  )r1   r2   r3   r4   rh  r@  r#  r6   r7   r8   r  r  z  sI        LL* * * 0 0 0 \0 0 0r7   r  c                      e Zd ZdZddZd Zeed                         Zeed                         Z	eed                         Z
e ej        d	
          d                         Zed             ZdS )	_sum_zerozSum of all elements in ``column``.

    Parameters
    ----------
    column : str
        Name of the column to aggregate over. Column data type must be numeric.
    r   r   c                r    |rt          t          j        d          fS t          t          j        d          fS rI  )r   r   rk  rl  r   s      r8   r   z_sum_zero._antialias_stage_2  s9     	H#$8$A1EEGG#$8$A1EEGGr7   c                    | j         S rD   r8  r  s     r8   r  z_sum_zero._build_create      ((r7   c                L    t          |          s||| fxx         |z  cc<   dS dS rw  rp  rq  s       r8   r  z_sum_zero._append  s6     e}} 	1IIIIII1rr7   c                V    ||z  }t          |          s||| fxx         |z  cc<   dS dS rw  rp  rr  rs  rt  ru  ry  r  s         r8   r  z_sum_zero._append_antialias  s?     ie}} 	1IIIIII1rr7   c                \    ||z  }t          |          s|||| f         k    r	|||| f<   dS dS rw  rp  r  s         r8   r]  z._sum_zero._append_antialias_not_self_intersect  sD     ie}} 	QT!2!2C1I1rr7   Tr  c                n    t          |          s%t          j                            ||| f|           dS dS rw  r  rq  s       r8   r  z_sum_zero._append_cuda  s:     e}} 	NsQFE2221rr7   c                0    |                      dd          S )Nr   r2  r  r  r  s    r8   r  z_sum_zero._combine  r  r7   Nr>  )r1   r2   r3   r4   r   r  r@  r!   r  r  r]  r  r  r  r  r6   r7   r8   r  r    s        H H H H) ) ) 
  U \ 
  U \ 
  U \ W[   \ , , \, , ,r7   r  c                  >     e Zd ZdZd fd	Zd Z fdZ fdZ xZS )	!SelfIntersectingFloatingReductionz
    Base class for floating reductions for which self-intersecting geometry
    may or may not be desirable.
    Ignored if not using antialiasing.
    NTc                X    t                                          |           || _        d S rD   rS  rT  s      r8   rH   z*SelfIntersectingFloatingReduction.__init__  rU  r7   c                    | j          S rD   rW  rJ   s    r8   r   z>SelfIntersectingFloatingReduction._antialias_requires_2_stages  rX  r7   c                    |r(|s&|rt          d          | j        | j        S | j        S t	                                          |||||          S )NrQ  )r   rA   r\  r]  r   r  r^  s         r8   r  z/SelfIntersectingFloatingReduction._build_append  sf     	E^ 	E E)*RSSS;&MMDDww$$VVT9nUUUr7   c                V    t                                                      | j        fz   S rD   r`  r   s    r8   r   z2SelfIntersectingFloatingReduction._hashable_inputs  ra  r7   rb  rc  r   s   @r8   r  r    s         
- - - - - -' ' '
V 
V 
V 
V 
VC C C C C C C C Cr7   r  c                      e Zd ZdZddZd Zeed                         Zeed                         Z	eed                         Z
ed	             Zedd            ZdS )r  a  Sum of all elements in ``column``.

    Elements of resulting aggregate are nan if they are not updated.

    Parameters
    ----------
    column : str
        Name of the column to aggregate over. Column data type must be numeric.
        ``NaN`` values in the column are skipped.
    r   r   c                    |r t          t          j        |j                  fS t          t          j        |j                  fS rD   rj  r   s      r8   r   zsum._antialias_stage_2  rm  r7   c                \    |r(t          | j                  t          | j                  fS | fS rD   )r  rA   r  r   s      r8   r   zsum._build_bases  s0     	dk**C,<,<==7Nr7   c                    t          |          s3t          ||| f                   r|||| f<   n||| fxx         |z  cc<   dS dS rw  rp  rq  s       r8   r  zsum._append  s]     e}} 	c!Q$i   #!AqD		AqD			U"			1rr7   c                    ||z  }t          |          s3t          ||| f                   r|||| f<   n||| fxx         |z  cc<   dS dS rw  rp  r  s         r8   r  zsum._append_antialias  sf     ie}} 	c!Q$i   #!AqD		AqD			U"			1rr7   c                    ||z  }t          |          s.t          ||| f                   s|||| f         k    r	|||| f<   dS dS rw  rp  r  s         r8   r]  z(sum._append_antialias_not_self_intersect  s\     ie}} 	c!Q$i   EC1I$5$5!AqD	qrr7   c                $    t          | d          S r  )r"   r  s    r8   r  zsum._combine  s    d++++r7   Fc                    |r7| \  }}t          j        ||t           j                  }t          j        |fi |S t          j        | d         fi |S rI  )rg   whererj   r_   rK  )rM  r   rN  sumsanysrr  s         r8   r#  zsum._finalize  s\     	4JD$tRV,,A<,,V,,,<a33F333r7   Nr>  r?  )r1   r2   r3   r4   r   r   r@  r!   r  r  r]  r  r#  r6   r7   r8   r  r    s        	 	W W W W   
  U \ 
  U \ 
  U \ , , \, 4 4 4 \4 4 4r7   r  c                       e Zd ZdZddZ fdZd ZddZee	d	                         Z
e ej        d
          d                         Zed             Z xZS )m2a`  Sum of square differences from the mean of all elements in ``column``.

    Intermediate value for computing ``var`` and ``std``, not intended to be
    used on its own.

    Parameters
    ----------
    column : str
        Name of the column to aggregate over. Column data type must be numeric.
        ``NaN`` values in the column are skipped.
    r   r:   c                    t           j        S rD   )r:   r>   rJ   s    r8   r   zm2.uses_cuda_mutex3  s    ##r7   c                \    t          t          |                               |||||          S rD   )r   r$  r  r^  s         r8   r  zm2._build_append6  s'    R,,VVT9n]]]r7   c                    | j         S rD   r
  r  s     r8   r  zm2._build_create9  r  r7   Fc                R    t          | j                  t          | j                  fS rD   r  rA   re  r  s     r8   r  zm2._build_temps<  !    $+&&dk(:(:;;r7   c                    t          |          sW|dk    rQt          j        |          |z  }t          j        ||z             |dz   z  }||| fxx         ||z
  ||z
  z  z  cc<   dS dS Nr   r   r   r   rg   r	  rr  rs  r$  ru  r  re  u1us           r8   r  z
m2._append@  z    
 e}} 	qyyZ__u,JsU{++uqy91a4URZEAI66qrr7   Tr  c                    t          |          sW|dk    rQt          j        |          |z  }t          j        ||z             |dz   z  }||| fxx         ||z
  ||z
  z  z  cc<   dS dS r,  r-  r.  s           r8   r  zm2._append_cudaN  r1  r7   c                   t          j        dd          5  t          j        |d          |                    d          z  }t          j        | |||z  |z
  dz  z  z   d          cd d d            S # 1 swxY w Y   d S )Nignoredivideinvalidr   r   r;   )rg   errstatenansumr  )Msr!  nsmus       r8   r  zm2._combine[  s    [(;;; 	@ 	@4a(((266q6>>9B9R"d2glQ%6"66Q???	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@s   AA66A:=A:r=  r?  )r1   r2   r3   r4   r   r  r  r  r@  r!   r  r  r  r  r  r   r   s   @r8   r$  r$  '  s        
 
$ $ $ $^ ^ ^ ^ ^) ) )< < < < 
	 	 U \	 W[	 	  \	 @ @ \@ @ @ @ @r7   r$  c                      e Zd ZdZd ZddZeed                         Zeed                         Z	e e
j        d	          d
                         Zed             ZdS )minzMinimum value of all elements in ``column``.

    Parameters
    ----------
    column : str
        Name of the column to aggregate over. Column data type must be numeric.
        ``NaN`` values in the column are skipped.
    c                    dS rb  r6   rJ   s    r8   r   z min._antialias_requires_2_stagesk  r  r7   r   r   c                B    t          t          j        |j                  fS rD   r   r   MINrj   r   s      r8   r   zmin._antialias_stage_2n  r  r7   c                    t          |          s.t          ||| f                   s||| f         |k    r	|||| f<   dS dS rw  rp  rq  s       r8   r  zmin._appendr  R     e}} 	&QT"3"3 	s1a4y57H7HC1I1rr7   c                    ||z  }t          |          s.t          ||| f                   s|||| f         k    r	|||| f<   dS dS rw  rp  r  s         r8   r  zmin._append_antialiasz  [     ie}} 	&QT"3"3 	us1a4y7H7HC1I1rr7   Tr  c                x    t          |          s*t          ||| f|          }t          |          s||k    rdS dS rw  )r   r   rr  rs  rt  ru  r  s        r8   r  zmin._append_cuda  G     e}} 	$S1a&%88Cc{{ cEkkqrr7   c                .    t          j        | d          S r  )rg   nanminr  s    r8   r  zmin._combine  r  r7   Nr>  )r1   r2   r3   r4   r   r   r@  r!   r  r  r  r  r  r  r6   r7   r8   r>  r>  b  s           N N N N 
  U \ 
  U \ W[   \ ' ' \' ' 'r7   r>  c                     e Zd ZdZddZeed                         Zeed                         Ze e	j
        d          d	                         Ze e	j
        d          d
                         Zed             ZdS )maxzMaximum value of all elements in ``column``.

    Parameters
    ----------
    column : str
        Name of the column to aggregate over. Column data type must be numeric.
        ``NaN`` values in the column are skipped.
    r   r   c                B    t          t          j        |j                  fS rD   r  r   s      r8   r   zmax._antialias_stage_2  r  r7   c                    t          |          s.t          ||| f                   s||| f         |k     r	|||| f<   dS dS rw  rp  rq  s       r8   r  zmax._append  rD  r7   c                    ||z  }t          |          s.t          ||| f                   s|||| f         k    r	|||| f<   dS dS rw  rp  r  s         r8   r  zmax._append_antialias  rF  r7   Tr  c                    ||z  }t          |          s*t          ||| f|          }t          |          s||k     rdS dS rw  r  r  s          r8   r  zmax._append_antialias_cuda  r  r7   c                x    t          |          s*t          ||| f|          }t          |          s||k     rdS dS rw  r  rH  s        r8   r  zmax._append_cuda  rI  r7   c                .    t          j        | d          S r  r  r  s    r8   r  zmax._combine  r  r7   Nr>  )r1   r2   r3   r4   r   r@  r!   r  r  r  r  r  r  r  r6   r7   r8   rM  rM    s        N N N N 
  U \ 
  U \ W[   \ W[   \ ' ' \' ' 'r7   rM  c                  "     e Zd ZdZ fdZ xZS )	count_cata]  Count of all elements in ``column``, grouped by category.
    Alias for `by(...,count())`, for backwards compatibility.

    Parameters
    ----------
    column : str
        Name of the column to aggregate over. Column data type must be
        categorical. Resulting aggregate has a outer dimension axis along the
        categories present.
    c                r    t          t          |                               |t                                 d S rD   )r   rU  rH   re  rD  s     r8   rH   zcount_cat.__init__  s-    i''88888r7   )r1   r2   r3   r4   rH   r   r   s   @r8   rU  rU    sB        	 	9 9 9 9 9 9 9 9 9r7   rU  c                  0    e Zd ZdZd Zedd            ZdS )meanzMean of all elements in ``column``.

    Parameters
    ----------
    column : str
        Name of the column to aggregate over. Column data type must be numeric.
        ``NaN`` values in the column are skipped.
    c                R    t          | j                  t          | j                  fS rD   r)  r   s      r8   r   zmean._build_bases  r*  r7   Fc                    | \  }}t          j        dd          5  t          j        |dk    ||z  t           j                  }d d d            n# 1 swxY w Y   t	          j        |fi |S Nr4  r5  r   rg   r8  r   rj   r_   rK  )rM  r   rN  r!  countsrr  s         r8   r#  zmean._finalize  s    f[(;;; 	: 	:!T&["&99A	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	:|A(((((s   (AAANr?  r1   r2   r3   r4   r   r@  r#  r6   r7   r8   rX  rX    sM         < < < ) ) ) \) ) )r7   rX  c                  0    e Zd ZdZd Zedd            ZdS )varzVariance of all elements in ``column``.

    Parameters
    ----------
    column : str
        Name of the column to aggregate over. Column data type must be numeric.
        ``NaN`` values in the column are skipped.
    c                x    t          | j                  t          | j                  t          | j                  fS rD   r  rA   re  r$  r   s      r8   r   zvar._build_bases  +    $+&&dk(:(:Bt{OOLLr7   Fc                    | \  }}}t          j        dd          5  t          j        |dk    ||z  t           j                  }d d d            n# 1 swxY w Y   t	          j        |fi |S r[  r\  rM  r   rN  r!  r]  m2srr  s          r8   r#  zvar._finalize  s    !fc[(;;; 	; 	;!S6\26::A	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	;|A(((((s   (AAANr?  r^  r6   r7   r8   r`  r`    P         M M M ) ) ) \) ) )r7   r`  c                  0    e Zd ZdZd Zedd            ZdS )stdzStandard Deviation of all elements in ``column``.

    Parameters
    ----------
    column : str
        Name of the column to aggregate over. Column data type must be numeric.
        ``NaN`` values in the column are skipped.
    c                x    t          | j                  t          | j                  t          | j                  fS rD   rb  r   s      r8   r   zstd._build_bases	  rc  r7   Fc                    | \  }}}t          j        dd          5  t          j        |dk    t          j        ||z            t           j                  }d d d            n# 1 swxY w Y   t          j        |fi |S r[  )rg   r8  r   sqrtrj   r_   rK  re  s          r8   r#  zstd._finalize  s    !fc[(;;; 	D 	D!RWS6\%:%:BFCCA	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D|A(((((s   :A##A'*A'Nr?  r^  r6   r7   r8   ri  ri     rg  r7   ri  c                  h     e Zd ZdZd Zd Zd Z fdZed             Z	d Z
ed
d	            Z xZS )_first_or_lastz6Abstract base class of first and last reductions.
    c                4    t          t          j                  S rD   r   r   r	  rg  s        r8   rh  z_first_or_last.out_dshape      bj!!!r7   c                
    |p|S rD   r6   r   s      r8   r   z_first_or_last.uses_row_index      "{"r7   c                    dS rb  r6   rJ   s    r8   r   z+_first_or_last._antialias_requires_2_stages  r  r7   c                   |                      ||          rP|                                 }t          || j                  }| j        |_        |                    ||          |fz   S t                                          ||          S N)selectorlookup_columnr   _create_row_index_selectorr   rA   r   r   r   rG   r   r   row_index_selectorwrapperr   s        r8   r   z_first_or_last._build_bases       t[11 	;!%!@!@!B!B%7t{SSSG(,G%%224EE
RR77''k:::r7   c                V    t          |           dk    rt          d          | d         S Nr   z6_combine should never be called with more than one aggr   rb   RuntimeErrorr  s    r8   r  z_first_or_last._combine*  +     t99q==WXXXAwr7   c                    d S rD   r6   rJ   s    r8   rz  z)_first_or_last._create_row_index_selector2      r7   Fc                2    t          j        | d         fi |S Nr   rJ  rL  s      r8   r#  z_first_or_last._finalize5       
 |E"I00000r7   r?  )r1   r2   r3   r4   rh  r   r   r   r@  r  rz  r#  r   r   s   @r8   rn  rn    s         " " "# # #  ; ; ; ; ;   \   1 1 1 \1 1 1 1 1r7   rn  c                  l    e Zd ZdZd	dZeed                         Zeed                         Zd Z	dS )
firsta  First value encountered in ``column``.

    Useful for categorical data where an actual value must always be returned,
    not an average or other numerical calculation.

    Currently only supported for rasters, externally to this class.

    Parameters
    ----------
    column : str
        Name of the column to aggregate over. If the data type is floating point,
        ``NaN`` values in the column are skipped.
    r   r   c                B    t          t          j        |j                  fS rD   r   r   FIRSTrj   r   s      r8   r   zfirst._antialias_stage_2K  s     4 :L<LMMOOr7   c                d    t          |          s t          ||| f                   r	|||| f<   dS dS rw  rp  rq  s       r8   r  zfirst._appendN  s@     e}} 	AqD	!2!2 	C1I1rr7   c                    ||z  }t          |          s.t          ||| f                   s|||| f         k    r	|||| f<   dS dS rw  rp  r  s         r8   r  zfirst._append_antialiasV  rF  r7   c                    t                      S rD   )_min_row_indexrJ   s    r8   rz  z first._create_row_index_selector_      r7   Nr>  
r1   r2   r3   r4   r   r@  r!   r  r  rz  r6   r7   r8   r  r  =  s         P P P P 
  U \ 
  U \         r7   r  c                  l    e Zd ZdZd	dZeed                         Zeed                         Zd Z	dS )
lasta  Last value encountered in ``column``.

    Useful for categorical data where an actual value must always be returned,
    not an average or other numerical calculation.

    Currently only supported for rasters, externally to this class.

    Parameters
    ----------
    column : str
        Name of the column to aggregate over. If the data type is floating point,
        ``NaN`` values in the column are skipped.
    r   r   c                B    t          t          j        |j                  fS rD   r   r   LASTrj   r   s      r8   r   zlast._antialias_stage_2q  s     4 9<;KLLNNr7   c                6    t          |          s	|||| f<   dS dS rw  rp  rq  s       r8   r  zlast._appendt  s)     e}} 	C1I1rr7   c                    ||z  }t          |          s.t          ||| f                   s|||| f         k    r	|||| f<   dS dS rw  rp  r  s         r8   r  zlast._append_antialias|  rF  r7   c                    t                      S rD   )_max_row_indexrJ   s    r8   rz  zlast._create_row_index_selector  r  r7   Nr>  r  r6   r7   r8   r  r  c  s         O O O O 
  U \ 
  U \         r7   r  c                  F     e Zd Zd	 fd	Zd Zd Z fdZd Z fdZ xZ	S )
FloatingNReductionNr   c                h    t                                          |           |dk    r|nd| _        d S r  )r   rH   n)rG   rA   r  r   s      r8   rH   zFloatingNReduction.__init__  s2       1ff!r7   c                4    t          t          j                  S rD   rp  rg  s        r8   rh  zFloatingNReduction.out_dshape  rq  r7   c                    d}t          j        | j                  }t          j        |          }|dxx         |gz  cc<   ||d         |<   |S )Nr  r  r  )rg   rf   r  r  r  )rG   rN  n_namen_valuess       r8   _add_finalize_kwargsz'FloatingNReduction._add_finalize_kwargs  sU    9TV$$ v&&v6("#+x r7   c                      fdS )Nc                z     t          t                                                  | j        fz   |          S rD   )r   r  r  r  r*  r   r   r  rG   s     r8   r  z2FloatingNReduction._build_create.<locals>.<lambda>  sB     ,51CT+J+J+X+X, ,"dfY.,> ,> r7   r6   rG   r  r   s   ``r8   r  z FloatingNReduction._build_create  s(    > > > > > > 	>r7   c                     d fd	}|S )NFc                >     j         di |} j        | fd|i|S )Nr   r6   )r  r#  )rM  r   rN  rG   s      r8   r  z4FloatingNReduction._build_finalize.<locals>.finalize  s:    .T.8888F!4>%==d=f===r7   r?  r6   )rG   r   r  s   `  r8   r$  z"FloatingNReduction._build_finalize  s(    	> 	> 	> 	> 	> 	> r7   c                V    t                                                      | j        fz   S rD   )r   r   r  r   s    r8   r   z#FloatingNReduction._hashable_inputs  s"    ww''))TVI55r7   r  )
r1   r2   r3   rH   rh  r  r  r$  r   r   r   s   @r8   r  r    s        $ $ $ $ $ $" " "
 
 
> > > > >  6 6 6 6 6 6 6 6 6r7   r  c                  b     e Zd ZdZd Zd Z fdZed             Zd Z	ed	d            Z
 xZS )
_first_n_or_last_nz:Abstract base class of first_n and last_n reductions.
    c                
    |p|S rD   r6   r   s      r8   r   z!_first_n_or_last_n.uses_row_index  rs  r7   c                    dS rb  r6   rJ   s    r8   r   z/_first_n_or_last_n._antialias_requires_2_stages  r  r7   c                   |                      ||          rP|                                 }t          || j                  }| j        |_        |                    ||          |fz   S t                                          ||          S rv  ry  r{  s        r8   r   z_first_n_or_last_n._build_bases  r~  r7   c                V    t          |           dk    rt          d          | d         S r  r  r  s    r8   r  z_first_n_or_last_n._combine  r  r7   c                    d S rD   r6   rJ   s    r8   rz  z-_first_n_or_last_n._create_row_index_selector  r  r7   Fc                2    t          j        | d         fi |S r  rJ  rL  s      r8   r#  z_first_n_or_last_n._finalize  r  r7   r?  )r1   r2   r3   r4   r   r   r   r@  r  rz  r#  r   r   s   @r8   r  r    s         # # #  ; ; ; ; ;   \   1 1 1 \1 1 1 1 1r7   r  c                  h    e Zd ZddZeed                         Zeed                         Zd ZdS )	first_nr   r   c                F    t          t          j        |j        d          fS NTr  r  r   s      r8   r   zfirst_n._antialias_stage_2  s"     4 :L<LZ^___aar7   c                    t          |          s`|j        d         }t          ||| |dz
  f                   sdS t          |          D ]&}t          ||| |f                   r|||| |f<   |c S 'dS Nr;   r   r   r   r*  r   rr  rs  rt  ru  r  r  s         r8   r  zfirst_n._append  s     e}} 		!A#aAaCi.)) r 1XX  #aAg,'' #(C1aLHHH rr7   c                    ||z  }t          |          s`|j        d         }t          ||| |dz
  f                   sdS t          |          D ]&}t          ||| |f                   r|||| |f<   |c S 'dS r  r  rr  rs  rt  ru  ry  r  r  r  s           r8   r  zfirst_n._append_antialias  s     ie}} 		!A#aAaCi.)) r 1XX  #aAg,'' #(C1aLHHH rr7   c                ,    t          | j                  S N)r  )_min_n_row_indexr  rJ   s    r8   rz  z"first_n._create_row_index_selector      $&))))r7   Nr>  	r1   r2   r3   r   r@  r!   r  r  rz  r6   r7   r8   r  r    s        b b b b 
  U \  
  U \"* * * * *r7   r  c                  h    e Zd ZddZeed                         Zeed                         Zd ZdS )	last_nr   r   c                F    t          t          j        |j        d          fS r  r  r   s      r8   r   zlast_n._antialias_stage_2  s"     4 9<;KY]^^^``r7   c                Z    t          |          st          ||| f         |d           dS dS rw  r   r.   rq  s       r8   r  zlast_n._append  s6     e}} 	SAYq1111rr7   c                d    ||z  }t          |          st          ||| f         |d           dS dS rw  r  r  s         r8   r  zlast_n._append_antialias  s?     ie}} 	SAYq1111rr7   c                ,    t          | j                  S r  )_max_n_row_indexr  rJ   s    r8   rz  z!last_n._create_row_index_selector  r  r7   Nr>  r  r6   r7   r8   r  r     s        a a a a 
  U \ 
  U \* * * * *r7   r  c                      e Zd ZddZddZeed                         Zeed                         Ze e	j
        d	          d
                         ZddZed             Zed             ZdS )max_nr   r:   c                    t           j        S rD   r:   r=   rJ   s    r8   r   zmax_n.uses_cuda_mutex      ""r7   r   c                F    t          t          j        |j        d          fS r  r  r   s      r8   r   zmax_n._antialias_stage_2   "     4 8,:JX\]]]__r7   c                    t          |          sc|j        d         }t          |          D ]F}t          ||| |f                   s|||| |f         k    rt          ||| f         ||           |c S GdS Nr;   r   r   r*  r   r.   r  s         r8   r  zmax_n._append$       e}} 	 	!A1XX  #aAg,'' 53q!Qw<+?+?$SAYq999HHH ,@ rr7   c                    ||z  }t          |          sc|j        d         }t          |          D ]F}t          ||| |f                   s|||| |f         k    rt          ||| f         ||           |c S GdS r  r  r  s           r8   r  zmax_n._append_antialias1       ie}} 	 	!A1XX  #aAg,'' 53q!Qw<+?+?$SAYq999HHH ,@ rr7   Tr  c                    t          |          sc|j        d         }t          |          D ]F}t          ||| |f                   s|||| |f         k    rt          ||| f         ||           |c S GdS r  r   r*  r   r   r  s         r8   r  zmax_n._append_cuda@       e}} 	 	!A1XX  #aAg,'' 53q!Qw<+?+?)#ad)UA>>>HHH ,@ rr7   Fc                "    |r| j         S | j        S rD   _combine_cudar  r  s         r8   r!  zmax_n._build_combineM       	!%%= r7   c                    | d         }t          dt          |                     D ]F}|j        dk    rt          | d         | |                    *t	          | d         | |                    G|S Nr   r      )r   rb   ndimr'   r&   r  s      r8   r  zmax_n._combineS  p    1gq#d))$$ 	7 	7Ax1}}$T!Wd1g6666$T!Wd1g6666
r7   c                .   | d         }t          |j        d d                   }t          dt          |                     D ]R}|j        dk    r#t          |         | d         | |                    0t          |         | d         | |                    S|S Nr   r   r   r  )r   r*  r   rb   r  r   r   r  r  kernel_argsr  s       r8   r  zmax_n._combine_cuda]      1g	#2#//q#d))$$ 	I 	IAx1}})+6tAwQHHHH)+6tAwQHHHH
r7   Nr=  r>  r?  )r1   r2   r3   r   r   r@  r!   r  r  r  r  r  r!  r  r  r6   r7   r8   r  r    s	       # # # #` ` ` ` 
	 	 U \	 

 
 U \
 W[	 	  \	! ! ! !   \   \  r7   r  c                      e Zd ZddZd ZddZeed                         Zeed                         Z	e e
j        d	
          d                         ZddZed             Zed             ZdS )min_nr   r:   c                    t           j        S rD   r  rJ   s    r8   r   zmin_n.uses_cuda_mutexj  r  r7   c                    dS rb  r6   rJ   s    r8   r   z"min_n._antialias_requires_2_stagesm  r  r7   r   c                F    t          t          j        |j        d          fS r  rA  r   s      r8   r   zmin_n._antialias_stage_2p  r  r7   c                    t          |          sc|j        d         }t          |          D ]F}t          ||| |f                   s|||| |f         k     rt          ||| f         ||           |c S GdS r  r  r  s         r8   r  zmin_n._appendt  r  r7   c                    ||z  }t          |          sc|j        d         }t          |          D ]F}t          ||| |f                   s|||| |f         k     rt          ||| f         ||           |c S GdS r  r  r  s           r8   r  zmin_n._append_antialias  r  r7   Tr  c                    t          |          sc|j        d         }t          |          D ]F}t          ||| |f                   s|||| |f         k     rt          ||| f         ||           |c S GdS r  r  r  s         r8   r  zmin_n._append_cuda  r  r7   Fc                "    |r| j         S | j        S rD   r  r  s         r8   r!  zmin_n._build_combine  r  r7   c                    | d         }t          dt          |                     D ]F}|j        dk    rt          | d         | |                    *t	          | d         | |                    G|S r  )r   rb   r  r)   r(   r  s      r8   r  zmin_n._combine  r  r7   c                .   | d         }t          |j        d d                   }t          dt          |                     D ]R}|j        dk    r#t          |         | d         | |                    0t          |         | d         | |                    S|S r  )r   r*  r   rb   r  r   r   r  s       r8   r  zmin_n._combine_cuda  r  r7   Nr=  r>  r?  )r1   r2   r3   r   r   r   r@  r!   r  r  r  r  r  r!  r  r  r6   r7   r8   r  r  i  s       # # # #  ` ` ` ` 
	 	 U \	 

 
 U \
 W[	 	  \	! ! ! !   \   \  r7   r  c                  Z    e Zd ZdZd Zed             Zed             Zed             ZdS )modeat  Mode (most common value) of all the values encountered in ``column``.

    Useful for categorical data where an actual value must always be returned,
    not an average or other numerical calculation.

    Currently only supported for rasters, externally to this class.
    Implementing it for other glyph types would be difficult due to potentially
    unbounded data storage requirements to store indefinite point or line
    data per pixel.

    Parameters
    ----------
    column : str
        Name of the column to aggregate over. If the data type is floating point,
        ``NaN`` values in the column are skipped.
    c                N    t          t          t          j                            S rD   r  rg  s        r8   rh  zmode.out_dshape  r  r7   c                     t          d          Nz.mode is currently implemented only for rastersr   r}  s      r8   r  zmode._append      !"RSSSr7   c                     t          d          r  r   r  s    r8   r  zmode._combine  r  r7   c                     t          d          r  r   )rM  rN  s     r8   r#  zmode._finalize  r  r7   N)	r1   r2   r3   r4   rh  r@  r  r  r#  r6   r7   r8   r  r    s          * * * T T \T T T \T T T \T T Tr7   r  c                  `    e Zd ZdZd d! fdZd Zd	 Zd
 Zd"dZd Z	 fdZ
d#dZeed                         Zeed                         Ze ej        d          d                         Ze ej        d          d                         Zd Z fdZd Zd$dZd Z fdZd Z xZS )%r   a  
    Returns values from a ``lookup_column`` corresponding to a ``selector``
    reduction that is applied to some other column.

    If ``lookup_column`` is ``None`` then it uses the index of the row in the
    DataFrame instead of a named column. This is returned as an int64
    aggregation with -1 used to denote no value.

    Examples
    --------
    >>> canvas.line(df, 'x', 'y', agg=ds.where(ds.max("value"), "other"))  # doctest: +SKIP

    This returns the values of the "other" column that correspond to the
    maximum of the "value" column in each bin.

    Parameters
    ----------
    selector: Reduction
        Reduction used to select the values of the ``lookup_column`` which are
        returned by this ``where`` reduction.

    lookup_column : str | None
        Column containing values that are returned from this ``where``
        reduction, or ``None`` to return row indexes instead.
    Nrw  r   rx  
str | Nonec                F   t          |t          t          t          t          t
          t          t          t          t          t          f
          st          d          |t          j        }t                                          |           || _        |j        |f| _        d S )NzWselector can only be a first, first_n, last, last_n, max, max_n, min or min_n reduction)r^   r  r  r  r  rM  r  r>  r  _max_or_min_row_index_max_n_or_min_n_row_indexr  r0   r5   r   rH   rw  rA   r  )rG   rw  rx  r   s      r8   rH   zwhere.__init__  s    (UGT63sTY%:<U%W X X 	656 6 6  )2M'''  7r7   c                n    t          t          |           |                                 | j        f          S rD   )r  r   r   rw  rJ   s    r8   r  zwhere.__hash__  s+    T$ZZ!6!6!8!8$-HIIIr7   c                    dS rb  r6   rJ   s    r8   r   zwhere.is_where  r  r7   c                    | j         t          j        k    rt          t          j                  S t          t          j                  S rD   )rA   r0   r5   r   r   rh   r	  )rG   r   r  r   r   s        r8   rh  zwhere.out_dshape  s2    ;-000"(###"*%%%r7   r   r:   c                    t           j        S rD   r  rJ   s    r8   r   zwhere.uses_cuda_mutex  r  r7   c                b    | j         t          j        k    p| j                            ||          S rD   )rA   r0   r5   rw  r   r   s      r8   r   zwhere.uses_row_index  s0    }55 @,,T;??	Ar7   c                   | j         t          j        k    r!t                                          |           | j                            |           | j         t          j        k    r$| j         | j        j         k    rt          d          d S d S )Nz<where and its contained reduction cannot use the same column)rA   r0   r5   r   r   rw  r   rG  s     r8   r   zwhere.validate  s    ;-000GGY'''y)));-000T[DMDX5X5X[\\\ 105X5Xr7   r   c                    | j                             ||          }| j        t          j        k    r)t          |d         j        d|d         j                  f}|S )Nr   r   )r  r  r  )rw  r   rA   r0   r5   r   r  r  r  s       r8   r   zwhere._antialias_stage_2  sc    m..~|LL;-000"s1v/A(*/21v/AC C C EC 
r7   c                ^    |j         dk    rt          ||| f         ||           n|||| f<   |S Nr;   r  r.   rr  rs  rt  ru  update_indexs        r8   r  zwhere._append)  s?     8a<<SAY|<<<<C1Ir7   c                `    |j         dk    rt          ||| f         ||           d S |||| f<   d S r  r  rr  rs  rt  ru  ry  r  s         r8   r  zwhere._append_antialias2  s@     8a<<SAY|<<<<<C1IIIr7   Tr  c                ^    |j         dk    rt          ||| f         ||           n|||| f<   |S r  r  r   r  s         r8   r  zwhere._append_antialias_cuda;  s?     8a<<!#ad)ULAAAAC1Ir7   c                ^    |j         dk    rt          ||| f         ||           n|||| f<   |S r  r  r  s        r8   r  zwhere._append_cudaE  s?     8a<<!#ad)ULAAAAC1Ir7   c                F    |r|r| j         S | j        S |r| j        S | j        S rD   )r  r  r  r  r  s         r8   r  zwhere._build_appendN  s?      		$ )22(( $--|#r7   c                .   | j         }t          |t          t          f          r|                    ||          r|                                }| j        t          j        k    r'| j         j        |_	        |
                    ||          S t          || j                  }| j         j        |_	        |
                    ||          |
                    ||          z   S |
                    ||          t                      
                    ||          z   S rD   )rw  r^   rn  r  r   rz  rA   r0   r5   r   r   r   r   )rG   r   r   rw  r|  	new_wherer   s         r8   r   zwhere._build_bases\  s	   =h1C DEE 	8''k::	8 "*!D!D!F!F{m444 8<}7K"4)66t[III!"4dkBB	.2m.B	+)66t[II**4==> > (({;;$$T;778 8r7   c                F   | j         }t          |t                    }|r|j        n|j        | j                             ||          rt          nt          t          fd            }t          fd            }t          fd            }t          fd            }	t          j
        fd            }
t          j
        fd            }t          j
        fd            }t          j
        fd            }|r|r|r|n|S |r|	n|S |r|r|n|
S |r|n|S )	Nc                   | d         j         \  }}t          |          D ]b}t          |          D ]P}|d         ||f         } |          s3 |||d         |          dk    r| d         ||f         | d         ||f<   Qcd S r  r*  r   )	r  selector_aggsnynxrs  rr  r  appendr7  s	          r8   combine_cpu_2dz/where._combine_callback.<locals>.combine_cpu_2d  s    !W]FB2YY 6 6r 6 6A)!,QT2E"75>> 6ffQ=;KU.S.SWX.X.X(,Q1Q166 6r7   c                R   | d         j         \  }}}t          |          D ]}t          |          D ]r}t          |          D ]`}|d         |||f         } 
|          sB 	|||d         d d d d |f         |          dk    r| d         |||f         | d         |||f<   asd S r  r  )r  r  r  r  ncatrs  rr  r   r  r  r7  s            r8   combine_cpu_3dz/where._combine_callback.<locals>.combine_cpu_3d  s   7=LBD2YY D Dr D DA$T{{ D D -a 0As ;&wu~~ D&&A}Q?OPQPQPQSTSTSTVYPY?Z9>3@ 3@CD3E 3E15aAs1CDGAq#I.	DDD Dr7   c           
     f   | d         j         \  }}}t          |          D ]}t          |          D ]|}t          |          D ]j}|d         |||f         } |          r nK 
|||d         |          }	|	dk     r n/t          | d         ||f         | d         |||f         |	           k}d S r  r*  r   r.   )r  r  r  r  r  rs  rr  r  r  r  r  r7  s             r8   combine_cpu_n_3dz1where._combine_callback.<locals>.combine_cpu_n_3d  s    QIBA2YY 	X 	Xr X XA"1XX X X -a 0Aq 9"75>> "!E'-vaM!4De'L'L'!++!E(aAQ1a8H,WWWWX	X 	Xr7   c                   | d         j         \  }}}}t          |          D ]}t          |          D ]}t          |          D ]}t          |          D ]}}	|d         ||||	f         }
 |
          r n] |||d         d d d d |d d f         |
          }|dk     r n1t          | d         |||f         | d         ||||	f         |           ~d S r  r#  )r  r  r  r  r   r  rs  rr  r   r  r  r  r  r7  s               r8   combine_cpu_n_4dz1where._combine_callback.<locals>.combine_cpu_n_4d  s1   "1gmOBD!2YY ; ;r 
; 
;A$T{{ 	; 	;!&q ; ;A$1!$4Q3\$BE&wu~~ & %+16!Qa8HAAAsTUTUTU8VX]+^+^L+a// %,T!WQ3Y-?aAsTUAV-9; ; ; ;	;
;; ;r7   c                   | d         j         \  }}t          j        d          \  }}||k     rV||k     rR|d         ||f         } |          s9 |||d         |          dk    r#| d         ||f         | d         ||f<   d S d S d S d S d S Nr   r;   r   r*  r  grid)	r  r  r  r  rr  rs  r  r  r7  s	          r8   combine_cuda_2dz0where._combine_callback.<locals>.combine_cuda_2d  s    !W]FB<??DAq2vv!b&&%a(A.wu~~ 2&&A}Q7G*O*OST*T*T$(GAqDMDGAqDMMM v&&2 2*T*Tr7   c                N   | d         j         \  }}}t          j        d          \  }}}||k     rl||k     rh||k     rd|d         |||f         } 
|          sJ 	|||d         d d d d |f         |          dk    r'| d         |||f         | d         |||f<   d S d S d S d S d S d S Nr   r  r   r)  )r  r  r  r  r   rr  rs  r   r  r  r7  s            r8   combine_cuda_3dz0where._combine_callback.<locals>.combine_cuda_3d  s    7=LBDQIAq#2vv!b&&S4ZZ%a(As3wu~~ <&&A}Q7G111c	7RTY*Z*Z^_*_*_)-aAs);DGAq#I&&& v&&ZZ< <*_*_r7   c                p   | d         j         \  }}}t          j        d          \  }}||k     r||k     r~t          |          D ]p}|d         |||f         } |          r d S  
|||d         |          }	|	dk     r d S t	          | d         ||f         | d         |||f         |	           md S d S d S r(  r*  r  r*  r   r   )r  r  r  r  r  rr  rs  r  r  r  r  r7  s             r8   combine_cuda_n_3dz2where._combine_callback.<locals>.combine_cuda_n_3d  s    QIBA<??DAq2vv!b&&q Y YA)!,Q1W5Ewu~~ #)6!Qa0@%#H#HL#a'')$q'!Q$-aAq9I<XXXX v&&Y Yr7   c                   | d         j         \  }}}}t          j        d          \  }}}||k     r||k     r||k     rt          |          D ]}	|d         ||||	f         }
 |
          r d S  |||d         d d d d |d d f         |
          }|dk     r d S t	          | d         |||f         | d         ||||	f         |           d S d S d S d S r-  r0  )r  r  r  r  r   r  rr  rs  r   r  r  r  r  r7  s               r8   combine_cuda_n_4dz2where._combine_callback.<locals>.combine_cuda_n_4d  s   "1gmOBD!QIAq#2vv!b&&S4ZZq c cA)!,Q3\:Ewu~~ #)6!Qa0@AAAsAAA0NPU#V#VL#a'')$q'!Q)*<d1gaCQRl>SUabbbb v&&ZZc cr7   )rw  r^   r  r  r  r   r   r   r!   r  r  )rG   r   r   r   rw  is_n_reductionr  r!  r$  r&  r+  r.  r1  r3  r  r7  s                 @@r8   _combine_callbackzwhere._combine_callbackp  s)    =#H.@AA 	&*FF%F #m::4MMY((SY		6 	6 	6 	6 	6 
	6 
	D 	D 	D 	D 	D 
	D 
	X 	X 	X 	X 	X 
	X 
	; 	; 	; 	; 	; 
	; 
	2 	2 	2 	2 	2 
	2 
	< 	< 	< 	< 	< 
	< 
	Y 	Y 	Y 	Y 	Y 
	Y 
	c 	c 	c 	c 	c 
	c  	I M,7N((=NN+6L''<LL  I*5J?J)4H~~.Hr7   Fc                H                           ||           fd}|S )Nc                   t          |           dk    rnrst          |           dk    sJ t          j        t                    }|r| d         j        d d         n| d         j        } t          |                   | |           nHt          dt          |                     D ]*} | d         | |         f|d         ||         f           +| d         |d         fS )Nr   r;   r   r   )rb   r^   rw  r  r*  r   r   )r  r  r4  r*  r  combiner   rG   s        r8   wrapped_combinez-where._build_combine.<locals>.wrapped_combine  s    4yyA~~ V4yyA~~~~!+DM;M!N!N.<OQcrc**$q'-)	%(()$>>>>q#d)),, V VAGT!Wd1g.q1A=QRCS0TUUUU7M!,,,r7   )r5  )rG   r   r  r   r   r   r9  r8  s   `  `   @r8   r!  zwhere._build_combine  sG    (({KHH	- 	- 	- 	- 	- 	- 	- r7   c                    | j         fS rD   )rw  r   s      r8   r   zwhere._build_combine_temps  s    r7   c                     t           j        t                    r fdS t                                                    S )Nc                     t          t                                                  | j        j        fz   |          S rD   )r   r   r  rw  r  r  s     r8   r  z%where._build_create.<locals>.<lambda>
  sF     0!uUD/A/A/O/O0! 0!!&$-/);!;\0K 0K r7   )r^   rw  r  r   r  r  s   ``r8   r  zwhere._build_create  s`     dm%788 	:K K K K K K K 77((999r7   c                f    t          | j        t                    r| j        j        nd dfd	}|S )NFc                H     di |}t          j        | d         fi |S )Nr   r6   rJ  )rM  r   rN  add_finalize_kwargss      r8   r  z'where._build_finalize.<locals>.finalize  s;    ".,,66v66<b	44V444r7   r?  )r^   rw  r  r  )rG   r   r  r?  s      @r8   r$  zwhere._build_finalize  sQ    dm%788 	'"&-"D"&	5 	5 	5 	5 	5 	5 r7   rD   )rw  r   rx  r  r=  r>  r?  )r1   r2   r3   r4   rH   r  r   rh  r   r   r   r   r@  r!   r  r  r  r  r  r  r  r   r5  r!  r   r  r$  r   r   s   @r8   r   r     s        28 8 8 8 8 8 8J J J  & & &# # # #A A A] ] ] ] ]    
  U \ 
  U \ W[   \ W[   \$ $ $8 8 8 8 8(|I |I |I|   &     	: 	: 	: 	: 	:      r7   r   c                  F    e Zd ZdZd Zd Zd Zd Zd Ze	d             Z
dS )	summarya%  A collection of named reductions.

    Computes all aggregates simultaneously, output is stored as a
    ``xarray.Dataset``.

    Examples
    --------
    A reduction for computing the mean of column "a", and the sum of column "b"
    for each bin, all in a single pass.

    >>> import datashader as ds
    >>> red = ds.summary(mean_a=ds.mean('a'), sum_b=ds.sum('b'))

    Notes
    -----
    A single pass of the source dataset using antialiased lines can either be
    performed using a single-stage aggregation (e.g. ``self_intersect=True``)
    or two stages (``self_intersect=False``). If a ``summary`` contains a
    ``count`` or ``sum`` reduction with ``self_intersect=False``, or any of
    ``first``, ``last`` or ``min``, then the antialiased line pass will be
    performed in two stages.
    c                x    t          t          |                                           \  }}|| _        || _        d S rD   )zipsorteditemskeysrr   )rG   rN  ksvss       r8   rH   zsummary.__init__5  s2    fV\\^^,,-B	r7   c                    t          t          |           t          | j                  t          | j                  f          S rD   )r  r   r  rF  rr   rJ   s    r8   r  zsummary.__hash__:  s1    T$ZZty!1!153E3EFGGGr7   c                H    | j         D ]}|                                r dS dS NTF)rr   r   )rG   vs     r8   r   zsummary.is_categorical=  s8     	 	A!! ttur7   c                L    | j         D ]}|                    ||          r dS dS rK  )rr   r   )rG   r   r   rL  s       r8   r   zsummary.uses_row_indexC  s<     	 	Ak22 ttur7   c                Z   | j         D ]}|                    |           g }| j         D ]M}t          |t                    r|j        }t          |t
                    r|                    |j                   Nt          t          j
        |                    dk    rt          d          d S )Nr   zKUsing multiple FloatingNReductions with different n values is not supported)rr   r   r^   r   rw  r  r  r  rb   rg   r   r   )rG   r   rL  r  s       r8   r   zsummary.validateI  s     	% 	%AJJ|$$$$  	% 	%A!U## J!/00 %$$$ry""##a'']_ _ _ ('r7   c                r    t          t          t          d | j        D                                           S )Nc              3  $   K   | ]}|j         V  d S rD   )rK   )r  rL  s     r8   r  z!summary.inputs.<locals>.<genexpr>Z  s$      "A"A18"A"A"A"A"A"Ar7   )r  r   r   rr   rJ   s    r8   rK   zsummary.inputsX  s1    VF"A"AT["A"A"AAABBCCCr7   N)r1   r2   r3   r4   rH   r  r   r   r   rO   rK   r6   r7   r8   rA  rA    s         ,  
H H H    _ _ _ D D XD D Dr7   rA  c                  .     e Zd ZdZ fdZd Zd Z xZS )r  z=Abstract base class of max and min row_index reductions.
    c                `    t                                          t          j                   d S )NrE   )r   rH   r0   r5   r   s    r8   rH   z_max_or_min_row_index.__init__`  s'     677777r7   c                4    t          t          j                  S rD   r   r   rh   rg  s        r8   rh  z _max_or_min_row_index.out_dshapec      bhr7   c                    dS rb  r6   r   s      r8   r   z$_max_or_min_row_index.uses_row_indexf  r  r7   )r1   r2   r3   r4   rH   rh  r   r   r   s   @r8   r  r  ]  s`         8 8 8 8 8           r7   r  c                      e Zd ZdZddZeed                         Zeed                         Ze e	j
        d          d	                         Zed
             ZdS )r  zMax reduction operating on row index.

    This is a private class as it is not intended to be used explicitly in
    user code. It is primarily purpose is to support the use of ``last``
    reductions using dask and/or CUDA.
    r   r   c                8    t          t          j        d          fS r  r   r   r  r   s      r8   r   z!_max_row_index._antialias_stage_2q       4 8"==??r7   c                4    |||| f         k    r	|||| f<   dS dS rw  r6   rq  s       r8   r  z_max_row_index._appendt  s.     3q!t9C1I1rr7   c                4    |||| f         k    r	|||| f<   dS dS rw  r6   rx  s        r8   r  z _max_row_index._append_antialias}  s.    
 3q!t9C1I1rr7   Tr  c                h    |dk    r+t           j                            ||| f|          }||k     rdS dS Nr   r   )r  r  rM  rH  s        r8   r  z_max_row_index._append_cuda  s=     B;;.$$S1a&%88CU{{qrr7   c                    | d         }t          dt          |                     D ]}t          j        || |         |            |S )Nr   r   )out)r   rb   rg   maximumr  s      r8   r  z_max_row_index._combine  sO     1gq#d))$$ 	. 	.AJsDG-----
r7   Nr>  )r1   r2   r3   r4   r   r@  r!   r  r  r  r  r  r  r6   r7   r8   r  r  j  s         @ @ @ @ 
  U \ 
  U \ W[   \   \  r7   r  c                      e Zd ZdZd ZddZddZeed                         Z	eed	                         Z
e ej        d
          d                         ZddZed             Zed             ZdS )r  zMin reduction operating on row index.

    This is a private class as it is not intended to be used explicitly in
    user code. It is primarily purpose is to support the use of ``first``
    reductions using dask and/or CUDA.
    c                    dS rb  r6   rJ   s    r8   r   z+_min_row_index._antialias_requires_2_stages  r  r7   r   r   c                8    t          t          j        d          fS r  r   r   rB  r   s      r8   r   z!_min_row_index._antialias_stage_2  rZ  r7   r:   c                    t           j        S rD   r  rJ   s    r8   r   z_min_row_index.uses_cuda_mutex  r  r7   c                \    |dk    r%||| f         dk    s|||| f         k     r	|||| f<   dS dS r^  r6   rq  s       r8   r  z_min_row_index._append  sF     B;;C1IOOus1a4y/@/@C1I1rr7   c                \    |dk    r%||| f         dk    s|||| f         k     r	|||| f<   dS dS r^  r6   rx  s        r8   r  z _min_row_index._append_antialias  F    
 B;;C1IOOus1a4y/@/@C1I1rr7   Tr  c                \    |dk    r%||| f         dk    s|||| f         k     r	|||| f<   dS dS r^  r6   rq  s       r8   r  z_min_row_index._append_cuda  ri  r7   Fc                "    |r| j         S | j        S rD   r  r  s         r8   r!  z_min_row_index._build_combine  r  r7   c                    | d         }t          dt          |                     D ]}t          || |                    |S r  )r   rb   r%   r  s      r8   r  z_min_row_index._combine  sH     1gq#d))$$ 	+ 	+AS$q'****
r7   c                &   | d         }t          |           dk    ru|j        dk    rd | D             } t          |j        d d                   }t	          dt          |                     D ]$}t          |         | d         | |                    %|S )Nr   r   r;   c                8    g | ]}t          j        |d           S )r;   )re   expand_dims)r  rt  s     r8   r  z0_min_row_index._combine_cuda.<locals>.<listcomp>  s$    ???3sA..???r7   r  )rb   r  r   r*  r   r   r  s       r8   r  z_min_row_index._combine_cuda  s    1gt99q==x1}}??$???#CIbqbM22K1c$ii(( E E%k247DGDDDD
r7   Nr>  r=  r?  )r1   r2   r3   r4   r   r   r   r@  r!   r  r  r  r  r  r!  r  r  r6   r7   r8   r  r    s"          @ @ @ @# # # # 
  U \ 
  U \ W[   \! ! ! !   \ 	 	 \	 	 	r7   r  c                  @     e Zd ZdZd fd	Zd ZddZd Zdd
Z xZ	S )r  zAAbstract base class of max_n and min_n row_index reductions.
    r   c                ~    t                                          t          j                   |dk    r|nd| _        d S )NrE   r   )r   rH   r0   r5   r  )rG   r  r   s     r8   rH   z"_max_n_or_min_n_row_index.__init__  s8     67771ff!r7   c                4    t          t          j                  S rD   rT  rg  s        r8   rh  z$_max_n_or_min_n_row_index.out_dshape  rU  r7   r   r:   c                    t           j        S rD   r  rJ   s    r8   r   z)_max_n_or_min_n_row_index.uses_cuda_mutex  r  r7   c                    dS rb  r6   r   s      r8   r   z(_max_n_or_min_n_row_index.uses_row_index  r  r7   Fc                "    |r| j         S | j        S rD   r  r  s         r8   r!  z(_max_n_or_min_n_row_index._build_combine  r  r7   )r   r=  r?  )
r1   r2   r3   r4   rH   rh  r   r   r!  r   r   s   @r8   r  r    s         $ $ $ $ $ $     # # # #  ! ! ! ! ! ! ! !r7   r  c                      e Zd ZdZddZeed                         Zeed                         Ze e	j
        d          d	                         Zed
             Zed             ZdS )r  zMax_n reduction operating on row index.

    This is a private class as it is not intended to be used explicitly in
    user code. It is primarily purpose is to support the use of ``last_n``
    reductions using dask and/or CUDA.
    r   r   c                <    t          t          j        dd          fS Nr   Tr  rY  r   s      r8   r   z#_max_n_row_index._antialias_stage_2	       4 8"$OOOQQr7   c                    |dk    rZ|j         d         }t          |          D ]=}||| |f         dk    s|||| |f         k    rt          ||| f         ||           |c S >dS Nr   r;   r#  r  s         r8   r  z_max_n_row_index._append		       B;; 	!A1XX  q!Qw<2%%Q1W)=)=$SAYq999HHH *> rr7   c                    |dk    rt|j         d         }t          |          D ]W}||| |f         dk    s|||| |f         k    r7t          |dz
  |d          D ]}||| |dz
  f         ||| |f<   |||| |f<   |c S XdS )Nr   r;   r   r  )rr  rs  rt  ru  ry  r  r  js           r8   r  z"_max_n_row_index._append_antialias	  s    
 B;; 	!A1XX  q!Qw<2%%Q1W)=)="1Q32.. 6 6'*1a19~Aq!G#(C1aLHHH *> rr7   Tr  c                    |dk    rZ|j         d         }t          |          D ]=}||| |f         dk    s|||| |f         k    rt          ||| f         ||           |c S >dS r{  r*  r   r   r  s         r8   r  z_max_n_row_index._append_cuda*	      
 B;; 	!A1XX  q!Qw<2%%Q1W)=)=)#ad)UA>>>HHH *> rr7   c                    | d         }t          |           dk    rD|j        dk    rt          | d         | d                    nt          | d         | d                    |S r  )rb   r  r+   r*   r  r  s     r8   r  z_max_n_row_index._combine9	  ]    1gt99q==x1}}%d1gtAw7777%d1gtAw777
r7   c                   | d         }t          |           dk    rlt          |j        d d                   }|j        dk    r#t	          |         | d         | d                    n"t          |         | d         | d                    |S Nr   r   r   r  )rb   r   r*  r  r   r   r  r  r  s      r8   r  z_max_n_row_index._combine_cudaC	  s~    1gt99q==#CIcrcN33Kx1}}*;7QaIIII*;7QaIII
r7   Nr>  )r1   r2   r3   r4   r   r@  r!   r  r  r  r  r  r  r  r6   r7   r8   r  r    s         R R R R 

 
 U \
 
  U \" W[   \   \   \  r7   r  c                      e Zd ZdZd ZddZeed                         Zeed                         Z	e e
j        d	          d
                         Zed             Zed             ZdS )r  zMin_n reduction operating on row index.

    This is a private class as it is not intended to be used explicitly in
    user code. It is primarily purpose is to support the use of ``first_n``
    reductions using dask and/or CUDA.
    c                    dS rb  r6   rJ   s    r8   r   z-_min_n_row_index._antialias_requires_2_stagesV	  r  r7   r   r   c                <    t          t          j        dd          fS rx  re  r   s      r8   r   z#_min_n_row_index._antialias_stage_2Y	  ry  r7   c                    |dk    rZ|j         d         }t          |          D ]=}||| |f         dk    s|||| |f         k     rt          ||| f         ||           |c S >dS r{  r#  r  s         r8   r  z_min_n_row_index._append\	  r|  r7   c                    |dk    rZ|j         d         }t          |          D ]=}||| |f         dk    s|||| |f         k     rt          ||| f         ||           |c S >dS r{  r#  )rr  rs  rt  ru  ry  r  r  s          r8   r  z"_min_n_row_index._append_antialiasj	  s    
 B;; 	!A1XX  q!Qw<2%%Q1W)=)=$SAYq999HHH *> rr7   Tr  c                    |dk    rZ|j         d         }t          |          D ]=}||| |f         dk    s|||| |f         k     rt          ||| f         ||           |c S >dS r{  r  r  s         r8   r  z_min_n_row_index._append_cuday	  r  r7   c                    | d         }t          |           dk    rD|j        dk    rt          | d         | d                    nt          | d         | d                    |S r  )rb   r  r-   r,   r  s     r8   r  z_min_n_row_index._combine	  r  r7   c                   | d         }t          |           dk    rlt          |j        d d                   }|j        dk    r#t	          |         | d         | d                    n"t          |         | d         | d                    |S r  )rb   r   r*  r  r   r   r  s      r8   r  z_min_n_row_index._combine_cuda	  s~    1gt99q==#CIcrcN33Kx1}}*;7QaIIII*;7QaIII
r7   Nr>  )r1   r2   r3   r4   r   r   r@  r!   r  r  r  r  r  r  r  r6   r7   r8   r  r  O	  s           R R R R 

 
 U \
 
  U \ W[   \   \ 	 	 \	 	 	r7   r  c                    g | ]V\  }}t          |t                    r<t          |t                    s	|t          u r|t          t
          t          t          fvT|WS r6   )r^   r   
issubclassr   rA  rB  r  r$  )r  _k_vs      r8   r  r  	  su     ; ; ;52b!"T**;0:2i0H0H;LNRYMM9.D#4b#: : : : : :r7   )s
__future__r   r  enumr   packaging.versionr   numpyrg   datashader.datashaper   r   r   r	   r
   r   toolzr   r   xarrayr_   datashader.antialiasr   r   datashader.utilsr   r   numbar   r  )datashader.transfer_functions._cuda_utilsr   r   r   r   r   r   r   r   r   r   r   r   r   ImportErrorrc   r   re   	Exceptionutilsr    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r0   r:   r@   rQ   rz   r   r   r   r   r   rB  rQ  re  r  r  r  r  r  r  r  r$  r>  rM  rU  rX  r`  ri  rn  r  r  r  r  r  r  r  r  r  r   rA  r  r  r  r  r  r  r   setlocalsrE  __all__r6   r7   r8   <module>r     sM   " " " " " "        % % % % % %     B B B B B B B B B B B B 0 0 0 0 0 0                     F F F F F F F F - - - - - - - - ! ! ! ! ! !U                                U U U 	U+Y8M!#<!#<"$>"$>@U@U@UUKKK   D222                                 	 	 	 	 	D 	 	 	    D          #* #* #* #* #*j #* #* #*L; ; ; ; ; ; ; ;(4 4 4 4 4' 4 4 48#! #! #! #! #!n #! #! #!J3 3 3 3 3 3 3 3l,- ,- ,- ,- ,-( ,- ,- ,-^c5 c5 c5 c5 c5 c5 c5 c5L0 0 0 0 0Y 0 0 0$!C !C !C !C !C-C !C !C !CHA A A A A2 A A AH| | | | | | | ||A A A A A
  A A AH '  '  '  '  '	  '  '  'F0 0 0 0 0	 0 0 0:, :, :, :, :,! :, :, :,zC C C C C(9 C C C>D4 D4 D4 D4 D4
+ D4 D4 D4N8@ 8@ 8@ 8@ 8@	 8@ 8@ 8@v-' -' -' -' -'
 -' -' -'`4' 4' 4' 4' 4'
 4' 4' 4'n9 9 9 9 9 9 9 9) ) ) ) )9 ) ) )() ) ) ) )) ) ) )() ) ) ) )) ) ) )(&1 &1 &1 &1 &1Y &1 &1 &1R#  #  #  #  # N #  #  # L#  #  #  #  # > #  #  # L 6  6  6  6  6/  6  6  6F#1 #1 #1 #1 #1+ #1 #1 #1L+* +* +* +* +*  +* +* +*\* * * * * * * *8J J J J J J J JZM M M M M M M M`T T T T T9 T T TBA A A A A A A AH
<D <D <D <D <Dd <D <D <D~
 
 
 
 
2 
 
 
0 0 0 0 0* 0 0 0fH H H H H* H H HV! ! ! ! ! 2 ! ! !.M M M M M0 M M M`M M M M M0 M M M` $ss ; ;FFHHNN$4$4 ; ; ; < < = = '(:;	<s$   A/ /BBB 	B B 