
    Ug[                   :   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+ 	 d dl-Z-d dl.Z/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*eM      ZN G d+ d,eJ      ZO G d- d.eK      ZP G d/ d0eJ      ZQ G d1 d2eJ      ZR G d3 d4eR      ZS G d5 d6eR      ZT G d7 d8eT      ZU G d9 d:eR      ZV G d; d<eR      ZW G d= d>eR      ZX G d? d@eO      ZY G dA dBeJ      ZZ G dC dDeJ      Z[ G dE dFeJ      Z\ G dG dHeJ      Z] G dI dJe]      Z^ G dK dLe]      Z_ G dM dNeK      Z` G dO dPe`      Za G dQ dRea      Zb G dS dTea      Zc G dU dVe`      Zd G dW dXe`      Ze G dY dZeJ      Zf G d[ d\eR      Zg G d] d^e2      Zh G d_ d`eK      Zi G da dbei      Zj G dc ddei      Zk G de dfe`      Zl G dg dhel      Zm G di djel      Zn eo ep eq       j                         D  cg c]%  \  } } es|et      r eu|eJ      s|ehu r
|eJeKeReVfvr| ' c}}             ddgz   Zvy# e,$ r d\  ZZ Z!Z"Z#Z$Z%Z&Z'Z(Z)Z*Z+Y w xY w# e0$ r dxZ-Z/Y w xY wc c}} w )k    )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y)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.12/site-packages/datashader/reductions.pyr0   r0   -   s     Hr7   r0   c                      e Zd ZdZdZdZdZy)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Fr7   r:   c                  8    e Zd ZdZddZed        Zed        Zy)
Preprocessz#Base clase for preprocessing steps.c                    || _         y NcolumnselfrD   s     r8   __init__zPreprocess.__init__I   s	    r7   c                    | j                   fS rB   rC   rF   s    r8   inputszPreprocess.inputsL   s    ~r7   c                     y rB   r6   rI   s    r8   nan_check_columnzPreprocess.nan_check_columnP   s    r7   NrD   zstr | SpecialColumn | None)r1   r2   r3   r4   rG   propertyrJ   rL   r6   r7   r8   r@   r@   G   s2    -    r7   r@   c                      e Zd ZdZd Zy)extractz=Extract a column from a dataframe as a numpy array of values.c                N   | j                   t        j                  u rdd}t        |t        j
                        r|j                  |   }|j                  d   }n)t        |dd       }t        |xs ||d      }t        |      }t        rt        |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                      j)                  |      S t        j*                  || j                      j-                  |	            S | j                   t        j                  u rT|r)t        j                  |z   t        j                        S t        j                  |z   t        j                        S t        |t        j
                        rn|rSt        || j                      j.                  t        j0                        s"t        j2                  || j                            S || j                      j.                  S || j                      j4                  S )
N_datashader_row_offset_datashader_row_lengthattrsr   dtypef22.02na_valuefillna)rD   r0   r5   
isinstancexrDatasetrT   getattrlencudf	DataFramecparangenpint64rV   kindnanr   __version__to_cupyarrayto_gpu_arraydatandarrayasarrayvalues)rF   dfr   	attr_name
row_offset
row_lengthrT   nullvals           r8   applyzextract.applyW   s   ;;-0000I"bjj)XXi0
XX&>?
GT2$U[b)Q?
 W
Jr4>>2{{m444yyZ*-DBHHUU$++$$))S0&&t''(GG,<<$++...@@88Bt{{O888HII[[M222yyZ*-DBHHUUyyZ*-DBHHUUBJJ'Jr$++';';RZZHzz"T[[/22$+++++dkk?)))r7   N)r1   r2   r3   r4   rw   r6   r7   r8   rP   rP   U   s
    G!*r7   rP   c                  2    e Zd ZdZed        Zd Zd Zd Zy)CategoryPreprocessz*Base class for categorizing preprocessors.c                    | j                   S z"Returns name of categorized columnrC   rI   s    r8   
cat_columnzCategoryPreprocess.cat_column}   s     {{r7   c                    t        d      )z7Returns list of categories corresponding to input shapezcategories not implementedNotImplementedErrorrF   input_dshapes     r8   
categorieszCategoryPreprocess.categories   s    !">??r7   c                    t        d      )zValidates input shapezvalidate not implementedr~   rF   	in_dshapes     r8   validatezCategoryPreprocess.validate   s    !"<==r7   c                    t        d      )z3Applies preprocessor to DataFrame and returns arrayzapply not implementedr~   rF   rr   r   s      r8   rw   zCategoryPreprocess.apply   s    !"9::r7   N)	r1   r2   r3   r4   rN   r|   r   r   rw   r6   r7   r8   ry   ry   {   s'    4 @>;r7   ry   c                  "    e Zd ZdZd Zd Zd Zy)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                H    |j                   | j                     j                  S rB   )measurerD   r   r   s     r8   r   zcategory_codes.categories   s    ##DKK0;;;r7   c                    | j                   |j                  vrt        d      t        |j                  | j                      t
        j                        st        d      y )Nspecified column not foundzinput must be categorical)rD   dict
ValueErrorr]   r   ctCategoricalr   s     r8   r   zcategory_codes.validate   sM    ;;inn,9::)++DKK8"..I899 Jr7   c                   t         rt        |t         j                        rt        t         j                        t        d      k\  r1|| j
                     j                  j                  j                         S || j
                     j                  j                  j                         S || j
                     j                  j                  j                  S NrX   )rb   r]   rc   r   rj   rD   catcodesrk   rm   rq   r   s      r8   rw   zcategory_codes.apply   s    Jr4>>2t''(GG,<<$++**0088::dkk?&&,,99;;dkk?&&,,333r7   N)r1   r2   r3   r4   r   r   rw   r6   r7   r8   r   r      s    	<: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.
    c                @    t         |   |       || _        || _        y rB   )superrG   offsetmodulo)rF   rD   r   r   	__class__s       r8   rG   zcategory_modulo.__init__   s     r7   c                R    t         |          | j                  | j                  fz   S rB   )r   _hashable_inputsr   r   rF   r   s    r8   r   z category_modulo._hashable_inputs   s#    w')T[[$++,FFFr7   c                >    t        t        | j                              S rB   )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      y )Nr   zinput must be an integer column)rD   r   r   r   IntegerTypesr   s     r8   r   zcategory_modulo.validate   sM    ;;inn,9::T[[)1B1BB>?? Cr7   c                6   || j                      | j                  z
  | j                  z  }t        r_t	        |t        j
                        rEt        t        j                        t        d      k\  r|j                         S |j                         S |j                  S r   )rD   r   r   rb   r]   Seriesr   rj   rk   rm   rq   )rF   rr   r   results       r8   rw   zcategory_modulo.apply   so    T[[/DKK/4;;>Jr4;;/t''(GG,<<~~''&&((== r7   )r   )r1   r2   r3   r4   r   bool_uint8uint16uint32uint64int8int16int32rg   r   rG   r   r   r   rw   __classcell__r   s   @r8   r   r      sg     HHbhh		299biiRTRZRZHHbhh(L
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
    c                    t         |   ||dz          || _        ||z
  t        |      z  | _        || _        |rdn|| _        |r|dz
  | _        y || _        y Nr   r   )r   rG   bin0floatbinsizenbins	bin_underbin_over)rF   rD   lowerupperr   include_underinclude_overr   s          r8   rG   zcategory_binning.__init__   sV    +	u5
+$0qer7   c                ~    t         |          | j                  | j                  | j                  | j
                  fz   S rB   )r   r   r   r   r   r   r   s    r8   r   z!category_binning._hashable_inputs   s2    w')TYYdnnVZVcVc,dddr7   c                J    | j                   |j                  vrt        d      y )Nr   )rD   r   r   r   s     r8   r   zcategory_binning.validate   s#    ;;inn,9:: -r7   c                   t         rt        |t         j                        rt        t         j                        t        d      k\  r.|| j
                     j                  t        j                        }n2t        j                  || j
                     j                  d            }t        j                  |      }n2|| j
                     j                         }t        j                  |      }|| j                  z
  | j                  z  }d||<   |j!                  t"              }| j$                  ||dk  <   | j&                  ||| j(                  k\  <   | j(                  ||<   |S )NrX   rY   Tr[   r   )rb   r]   rc   r   rj   rD   rk   rd   ri   rl   rm   isnanto_numpyrf   r   r   astypeintr   r   r   )rF   rr   r   rq   
nan_valuesindex_floatindexs          r8   rw   zcategory_binning.apply   s   Jr4>>2t''(GG,<<DKK00"&&0A"T[[/">">d">"KL&)J_--/F&)J		)T\\9 #$J""3'>>eai%)]]etzz!" JJjr7   )TT)	r1   r2   r3   r4   rG   r   r   rw   r   r   s   @r8   r   r      s     <e;r7   r   c                  T     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                2    t         |   |       || _        y rB   )r   rG   categorizer)rF   r   value_columnr   s      r8   rG   zcategory_values.__init__  s    &&r7   c                F    | j                   j                  | j                  fS rB   r   rD   rI   s    r8   rJ   zcategory_values.inputs  s      ''55r7   c                .    | j                   j                  S r{   r   rI   s    r8   r|   zcategory_values.cat_column  s     &&&r7   c                .    | j                   j                  S rB   )r   r   r   s     r8   r   zcategory_values.categories  s    ***r7   c                8    | j                   j                  |      S rB   )r   r   r   s     r8   r   zcategory_values.validate  s    ((33r7   c                   | j                   j                  ||      }t        rWt        |t        j                        r<dd l}| j                  t        j                  k(  rd}n9|| j                     j                  j                  dk(  rt        j                  }nd}|j                  |      }| j                  t        j                  k(  r*t        t        j                        j                  ||      }nrt        t        j                         t        d      k\  r || j                     j#                  |      }n-|j                  || j                     j%                  |            }|j'                  ||fd      S | j                  t        j                  k(  r*t        t        j                        j                  ||      }n|| j                     j(                  }t        j&                  ||fd      S )Nr   rW   rX   rY   axis)r   rw   rb   r]   rc   cupyrD   r0   r5   rV   rh   rf   ri   rp   rP   r   rj   rk   r\   stackrq   )rF   rr   r   ar   rv   bs          r8   rw   zcategory_values.apply  sl   ""2t,Jr4>>2{{m444DKK&&++s2&&QA{{m444M22399"dC))*gg.>>t{{O++W+=LLDKK!7!7!@A::q!f2:..{{m444M22399"dCt{{O**88QF,,r7   )r1   r2   r3   r4   rG   rN   rJ   r|   r   r   rw   r   r   s   @r8   r   r     sB    `' 6 6 ' '+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y) 	Reductionz"Base class for per-bin reductions.Nc                     || _         d | _        y rB   )rD   _nan_check_columnrE   s     r8   rG   zReduction.__init__7  s    !%r7   c                F    | j                   t        | j                         S y rB   )r   rP   rI   s    r8   rL   zReduction.nan_check_column;  s"    !!-41122r7   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<   rI   s    r8   uses_cuda_mutexzReduction.uses_cuda_mutexB  s     r7   c                     y)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   rF   r   partitioneds      r8   uses_row_indexzReduction.uses_row_indexL  s     r7   c                    | j                   t        j                  k(  ry | j                   |j                  vrt	        d      t        |j                  | j                            st	        d      y )Nr   zinput must be numeric)rD   r0   r5   r   r   r   r   r   s     r8   r   zReduction.validate]  s[    ;;-000;;inn,9::**4;;78455 9r7   c                .    t        | j                        fS rB   rP   rD   rI   s    r8   rJ   zReduction.inputse      $&&r7   c                     y)z?Return ``True`` if this is or contains a categorical reduction.Fr6   rI   s    r8   is_categoricalzReduction.is_categoricali  s    r7   c                     y)z]Return ``True`` if this is a ``where`` reduction or directly wraps
        a where reduction.Fr6   rI   s    r8   is_wherezReduction.is_wherem  s     r7   c                     y)NFr6   rI   s    r8   _antialias_requires_2_stagesz&Reduction._antialias_requires_2_stagesr  s     r7   c                0    t        t        |        d      )Nz"._antialias_stage_2 is not defined)r   typerF   self_intersectarray_modules      r8   _antialias_stage_2zReduction._antialias_stage_2x  s     "T$ZL0R"STTr7   c                    | fS rB   r6   r   s      r8   _build_baseszReduction._build_bases~  s	    wr7   c                     yNr6   r6   r   s      r8   _build_combine_tempszReduction._build_combine_temps  s    
 r7   c                     yr   r6   rF   r   s     r8   _build_tempszReduction._build_temps  s     r7   c                   t        |j                  dd       }|It        |j                  j                        dkD  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 )r`   r   ra   r  r   r]   r	   tyr   r   _create_boolfloat32_create_float32_nanfloat64_create_float64_nanrg   _create_int64r   _create_uint32r   )rF   required_dshaper  first_fields       r8   _build_createzReduction._build_create  s   00(DA#o&=&=&D&D"E"I)1188;K$[^4Oov.$_%7%78OfRXX..$$$rzz 22+++rzz 22+++rxx 00%%%ryy 11&&&%(:6(&CDDr7   c                6   |rL|r| j                   | j                  S |r| j                  S | j                   | j                  S | j                  S |r| j                   | j
                  S |r| j                  S | j                   | j                  S | j                  S rB   )	rD   _append_no_field_antialias_cuda_append_antialias_cuda_append_no_field_cuda_append_cuda_append_no_field_antialias_append_antialias_append_no_field_appendrF   r   schemar   	antialiasr   s         r8   _build_appendzReduction._build_append  s    T[[0;;;222$111(((T[[0666---$,,,||#r7   c                    | j                   S rB   )_combinerF   r   r  r   r   categoricals         r8   _build_combinezReduction._build_combine  s    }}r7   c                    | j                   S rB   )	_finalize)rF   r   s     r8   _build_finalizezReduction._build_finalize  s    ~~r7   c                (    |j                  | d      S )NboolrU   zerosshaper   s     r8   r  zReduction._create_bool  s    !!%v!66r7   c                >    |j                  | |j                  d      S )Nf4rU   fullri   r&  s     r8   r  zReduction._create_float32_nan         (8(8 EEr7   c                >    |j                  | |j                  d      S Nf8rU   r*  r&  s     r8   r  zReduction._create_float64_nan  r,  r7   c                (    |j                  | d      S r.  )emptyr&  s     r8   _create_float64_emptyzReduction._create_float64_empty      !!%t!44r7   c                (    |j                  | d      S r.  r$  r&  s     r8   _create_float64_zerozReduction._create_float64_zero  r3  r7   c                *    |j                  | dd      S )Nr   i8rU   )r+  r&  s     r8   r  zReduction._create_int64  s      $ 77r7   c                (    |j                  | d      S )Nu4rU   r$  r&  s     r8   r	  zReduction._create_uint32  r3  r7   rB   rM   returnr:   r;  ztuple[AntialiasStage2]F)r1   r2   r3   r4   rG   rN   rL   r   r   r   rJ   r   r   r   r   r   r   r   r  r  r  r!  staticmethodr  r  r  r2  r5  r  r	  r6   r7   r8   r   r   5  s
   ,&   "6 ' '
U
E.$( 7 7 F F F F 5 5 5 5 8 8 5 5r7   r   c                  P     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 optionalc                $    t         |   |       y rB   )r   rG   rF   rD   r   s     r8   rG   zOptionalFieldReduction.__init__  s     r7   c                J    | j                   t        | j                         fS dS r   )rD   rP   rI   s    r8   rJ   zOptionalFieldReduction.inputs  s"    *.++*A$&IrIr7   c                >    | j                   t        | 	  |       y y rB   )rD   r   r   rF   r   r   s     r8   r   zOptionalFieldReduction.validate  s    ;;"GY' #r7   c                4    t        j                  | d   fi |S Nr   r^   	DataArraybasesr   kwargss      r8   r   z OptionalFieldReduction._finalize      ||E!H///r7   rB   r=  )r1   r2   r3   r4   rG   rN   rJ   r   r>  r   r   r   s   @r8   r@  r@    s:    Y! J J( 0 0r7   r@  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.
    c                2    t         |   |       || _        y rB   r   rG   r   rF   rD   r   r   s      r8   rG   z/SelfIntersectingOptionalFieldReduction.__init__       ,r7   c                    | j                    S rB   r   rI   s    r8   r   zCSelfIntersectingOptionalFieldReduction._antialias_requires_2_stages      &&&&r7   c                    |rL|sJ|r$| j                   | j                  S | j                  S | j                   | j                  S | j                  S t
        |   |||||      S rB   )rD   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  rF   r   r  r   r  r   r   s         r8   r  z4SelfIntersectingOptionalFieldReduction._build_append  si    ^;;&RRRIII;;&MMMDDD w$VVT9nUUr7   c                <    t         |          | j                  fz   S rB   r   r   r   r   s    r8   r   z7SelfIntersectingOptionalFieldReduction._hashable_inputs  !     w')T-@-@,BBBr7   NT	r1   r2   r3   r4   rG   r   r  r   r   r   s   @r8   rO  rO    s$    
-'V"C Cr7   rO  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y)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                        S t        t        j                        S rB   )r   r   r  r   rF   r   r  r   r   s        r8   
out_dshapezcount.out_dshape  s!    %.vbjj!EF2994EEr7   c                    |r%t        t        j                  |j                        fS t        t        j                  |j                        fS rB   r   r   SUM_1AGGri   SUM_2AGGr   s      r8   r   zcount._antialias_stage_2  ?    #$8$A$A<CSCSTVV#$8$A$A<CSCSTVVr7   c                :    t        |      s||| fxx   dz  cc<   yyNr   r   r   r   xyaggfields       r8   r  zcount._append%  s"     e}1INIr7   c                v    t        |      s.t        ||| f         r||z
  ||| f<   y||| fxx   ||z
  z  cc<   yyNr   r   rn  rp  rq  rr  rs  	aa_factorprev_aa_factors         r8   r  zcount._append_antialias-  sQ     e}c!Q$i %6AqD	  AqD	Y77	r7   c                ^    t        |      s"t        ||| f         s
|||| f   kD  r|||| f<   yyru  rn  rv  s         r8   r[  z*count._append_antialias_not_self_intersect8  =     e}c!Q$i IAqD	$9%AqD	r7   c                "    ||| fxx   dz  cc<   yr   r6   rp  rq  rr  s      r8   r  zcount._append_no_fieldA  s     	AqD	Q	r7   c                ^    t        ||| f         r||z
  ||| f<   y||| fxx   ||z
  z  cc<   yrG  rn  rp  rq  rr  rw  rx  s        r8   r  z count._append_no_field_antialiasG  sG     #ad)!N2C1I  1I^33Ir7   c                H    t        ||| f         s
|||| f   kD  r|||| f<   yyru  rn  r~  s        r8   rZ  z3count._append_no_field_antialias_not_self_intersectP  6     #ad)	C1I 5!C1Ir7   Tdevicec                d    ||z  }t        |      s t        ||| f|      }t        |      s||k  ryyru  r   r   rp  rq  rr  rs  rw  rx  valueolds           r8   r  zcount._append_antialias_cudaY  9     ie}$S1a&%8Cc{cEkr7   c                Z    t        |      s t        ||| f|      }t        |      s||k  ryyru  r  rp  rq  rr  rw  rx  r  s         r8   rX  z8count._append_no_field_antialias_cuda_not_self_intersectc  1     i $S1a&)<Cc{cIor7   c                b    t        |      s$t        j                  j                  ||| fd       yyrm  r   nb_cudaatomicaddro  s       r8   r  zcount._append_cudal  s+     e}NNsQFA.r7   c                Z    t        |      s t        ||| f|      }t        |      s||k  ryyru  r  r  s         r8   r  z%count._append_no_field_antialias_cudat  r  r7   c                J    t         j                  j                  ||| fd       yr   )r  r  r  r|  s      r8   r  zcount._append_no_field_cuda}  s!     	3A*r7   c                6    |r| j                   S | j                  S rB   _combine_antialiasr  r  s         r8   r  zcount._build_combine      ***== r7   c                (    | j                  dd      S )Nr   r9  r   rV   sumaggss    r8   r  zcount._combine      xxQdx++r7   c                b    | d   }t        dt        |             D ]  }t        || |           |S Nr   r   )r   ra   r$   r  retis      r8   r  zcount._combine_antialias  3    1gq#d)$ACa) %
r7   Nr<  r=  )r1   r2   r3   r4   rf  r   r>  r!   r  r  r[  r  r  rZ  r  jitr  rX  r  r  r  r  r  r  r6   r7   r8   rc  rc    s   FW 
   
   
   
   
   
   W[[   W[[   W[[   W[[   W[[  ! , ,  r7   rc  c                  R    e Zd ZdZd ZddZeed               Zeed               Z	y)_count_ignore_antialiasingzFCount reduction but ignores antialiasing. Used by mean reduction.
    c                4    t        t        j                        S rB   )r   r   r   re  s        r8   rf  z%_count_ignore_antialiasing.out_dshape  s    bii  r7   c                r    |rt        t        j                  d      fS t        t        j                  d      fS rG  r   r   ri  rj  r   s      r8   r   z-_count_ignore_antialiasing._antialias_stage_2  3    #$8$A$A1EGG#$8$A$A1EGGr7   c                D    t        |      s|dk(  r||| fxx   dz  cc<   yyNg        r   r   r   rn  rv  s         r8   r  z,_count_ignore_antialiasing._append_antialias  )     e}3!61INIr7   c                D    t        |      s|dk(  r||| fxx   dz  cc<   yyr  rn  rv  s         r8   r[  z?_count_ignore_antialiasing._append_antialias_not_self_intersect  r  r7   Nr<  )
r1   r2   r3   r4   rf  r   r>  r!   r  r[  r6   r7   r8   r  r    sJ    !H 
   
  r7   r  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                D   t         |           t        |t              r|| _        n,t        |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                        | _        y | j                  | _        y )NzEfirst argument must be a column name or a CategoryPreprocess instancecolumnsr   rD   )r   rG   r]   ry   r   strr   	TypeErrorrD   r  r`   	reduction
val_columnr   
preprocess)rF   r|   r  r  r   s       r8   rG   zby.__init__  s     j"45)D
C(-j9Dcdd&&--((//1y)T::GG LLGDbDM)LLLWY$?AAL"??&-d.>.>PDO"..DOr7   c                    t        t        |       | j                         | j                  j                         | j                  f      S rB   )hashr   r   r   r  rI   s    r8   __hash__zby.__hash__  s=    T$Z!6!6!8$:J:J:[:[:]^^% & 	&r7   c                ^     t         fd j                  j                  |      D              S )Nc              3  J   K   | ]  }t        j                  |        y wrB   r  r   ).0tmprF   s     r8   	<genexpr>z"by._build_temps.<locals>.<genexpr>  s!     \:[3R((#.:[    #)tupler  r   r   s   ` r8   r   zby._build_temps  s$    \$..:U:UVZ:[\\\r7   c                     | j                   d   S rG  r  rI   s    r8   r|   zby.cat_column      ||Ar7   c                     | j                   d   S Nr   r  rI   s    r8   r  zby.val_column  r  r7   c                p    | j                   j                  |       | j                  j                  |       y rB   )r  r   r  r   s     r8   r   zby.validate  s&      +	*r7   c           	         | j                   j                  |      }| j                  j                  ||||      }t	        t        |D cg c]  }||f c}            S c c}w rB   )r   r   r  rf  r   r   )rF   r   r  r   r   cats	red_shapecs           r8   rf  zby.out_dshape  sX    **<8NN--lIt[Y	fd;dq)nd;<==;s   Ac                    | j                   fS rB   )r  rI   s    r8   rJ   z	by.inputs  s    !!r7   c                     yr`  r6   rI   s    r8   r   zby.is_categorical      r7   c                6    | j                   j                         S rB   )r  r   rI   s    r8   r   zby.is_where  s    ~~&&((r7   c                .    | j                   j                  S rB   )r  rL   rI   s    r8   rL   zby.nan_check_column  s    ~~...r7   c                6    | j                   j                         S rB   )r  r   rI   s    r8   r   zby.uses_cuda_mutex  s    ~~--//r7   c                :    | j                   j                  ||      S rB   )r  r   r   s      r8   r   zby.uses_row_index  s    ~~,,T;??r7   c                6    | j                   j                         S rB   )r  r   rI   s    r8   r   zby._antialias_requires_2_stages  s    ~~::<<r7   c                    | j                   j                  ||      }t        |d   j                  |d   j                  |d   j
                  d      fS )Nr   T)combinationzeron_reductionr  )r  r   r   r  r  r  rF   r   r   r  s       r8   r   zby._antialias_stage_2  sQ    nn//MCF,>,>%(V[[,/F,>,>,02 4 	4r7   c                T     t        j                  j                         fdS )Nc                P     j                   j                        | fz   |      S rB   )r  r  )r'  r   n_catsr
  rF   s     r8   <lambda>z"by._build_create.<locals>.<lambda>  s-     ,4>>+G+G,"fY.,>r7   )ra   r   r  )rF   r
  r  s   ``@r8   r  zby._build_create  s#    _,,334> 	>r7   c                      j                   j                  ||      }t        |      dk(  r	|d    u r|S t         fd|D              S )Nr   r   c              3  J   K   | ]  }t        j                  |        y wrB   r  )r  baserF   s     r8   r  z"by._build_bases.<locals>.<genexpr>  s     BEDR(($/Er  )r  r   ra   r  )rF   r   r   rK  s   `   r8   r   zby._build_bases  sE    ++D+>u:?uQx4/LBEBBBr7   c                @    | j                   j                  |||||      S rB   )r  r  r  s         r8   r  zby._build_append  s    ~~++FFD)^\\r7   c                @    | j                   j                  ||||d      S r`  )r  r  r  s         r8   r  zby._build_combine  s     ~~,,VYkSWXXr7   c                :    | j                   j                  ||      S rB   )r  r   r   s      r8   r   zby._build_combine_temps   s    ~~224EEr7   c                d     t         j                  j                              d fd	}|S )Nc                    t        j                  |      }|dxx   j                  gz  cc<   |d   j                  <    j                  j	                        | fd|i|S )Ndimscoordsr   )copydeepcopyr|   r  r!  )rK  r   rL  r  r   rF   s      r8   finalizez$by._build_finalize.<locals>.finalize&  sa     ]]6*F6Nt//N04F8T__-94>>11&9%UdUfUUr7   r=  )r   r   r   )rF   r   r  r  s   `` @r8   r!  zby._build_finalize#  s+    D$$//78	V r7   r=  r:  r<  )r1   r2   r3   r4   rc  rG   r  r   rN   r|   r  r   rf  rJ   r   r   rL   r   r   r   r   r  r   r  r  r   r!  r   r   s   @r8   r  r    s    	 .3W /8&]    +>
 " ") / /0@=4>
C]Y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y)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                        S t        t        j                        S rB   )r   r   r  r   re  s        r8   rf  zany.out_dshape8  s!    %.vbjj!DF2884DDr7   c                L    t        t        j                  |j                        fS rB   r   r   MAXri   r   s      r8   r   zany._antialias_stage_2;       4 8 8,:J:JKMMr7   c                *    t        |      sd||| f<   yy)NTr   r   rn  ro  s       r8   r  zany._append?  s     e}C1Ir7   c                ^    t        |      s"t        ||| f         s
|||| f   kD  r|||| f<   yyru  rn  rv  s         r8   r  zany._append_antialiasG  rz  r7   c                    d||| f<   y)NTr   r6   r|  s      r8   r  zany._append_no_fieldP  s     AqD	r7   c                H    t        ||| f         s
|||| f   kD  r|||| f<   yyru  rn  r~  s        r8   r  zany._append_no_field_antialiasV  r  r7   c                6    |r| j                   S | j                  S rB   r  r  s         r8   r  zany._build_combineb  r  r7   c                (    | j                  dd      S )Nr   r#  r  r  r  s    r8   r  zany._combineh  s    xxQfx--r7   c                b    | d   }t        dt        |             D ]  }t        || |           |S r  )r   ra   r#   r  s      r8   r  zany._combine_antialiasl  r  r7   Nr<  r=  )r1   r2   r3   r4   rf  r   r>  r!   r  r  r  r  r  r  r  r  r  r6   r7   r8   r  r  0  s    EN 
   
   
   
   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y)	_upsamplez+"Special internal class used for upsamplingc                F    t        t        t        j                              S rB   r   r	   r   r  re  s        r8   rf  z_upsample.out_dshapev      fRZZ())r7   c                4    t        j                  | d   fi |S rG  rH  rJ  s      r8   r   z_upsample._finalizey  rM  r7   c                .    t        | j                        fS rB   r   rI   s    r8   rJ   z_upsample.inputs}  r   r7   c                    | j                   S rB   )r2  rF   r
  s     r8   r  z_upsample._build_create  s     )))r7   c                     y rB   r6   ro  s       r8   r  z_upsample._append       	r7   Tr  c                     y rB   r6   ro  s       r8   r  z_upsample._append_cuda  r  r7   c                0    t        j                  | d      S Nr   r   rf   nanmaxr  s    r8   r  z_upsample._combine      yyA&&r7   Nr=  )r1   r2   r3   r4   rf  r>  r   rN   rJ   r  r!   r  r  r  r  r  r6   r7   r8   r  r  t  s    5* 0 0 ' '*
 
   W[[   ' 'r7   r  c                  (    e Zd ZdZd Zedd       Zy)FloatingReductionzBBase classes for reductions that always have floating-point dtype.c                F    t        t        t        j                              S rB   r  re  s        r8   rf  zFloatingReduction.out_dshape  r  r7   c                4    t        j                  | d   fi |S rG  rH  rJ  s      r8   r   zFloatingReduction._finalize  rM  r7   Nr=  )r1   r2   r3   r4   rf  r>  r   r6   r7   r8   r  r    s    L* 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y)	_sum_zerozSum of all elements in ``column``.

    Parameters
    ----------
    column : str
        Name of the column to aggregate over. Column data type must be numeric.
    c                r    |rt        t        j                  d      fS t        t        j                  d      fS rG  r  r   s      r8   r   z_sum_zero._antialias_stage_2  r  r7   c                    | j                   S rB   r5  r  s     r8   r  z_sum_zero._build_create      (((r7   c                :    t        |      s||| fxx   |z  cc<   yyru  rn  ro  s       r8   r  z_sum_zero._append  s#     e}1IIr7   c                J    |||z
  z  }t        |      s||| fxx   |z  cc<   yyru  rn  rp  rq  rr  rs  rw  rx  r  s          r8   r  z_sum_zero._append_antialias  s2     y>12e}1IIr7   c                H    ||z  }t        |      s|||| f   kD  r|||| f<   yyru  rn  r  s          r8   r[  z._sum_zero._append_antialias_not_self_intersect  s6     ie}QT!2C1Ir7   Tr  c                b    t        |      s$t        j                  j                  ||| f|       yyru  r  ro  s       r8   r  z_sum_zero._append_cuda  s+     e}NNsQFE2r7   c                (    | j                  dd      S )Nr   r/  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) 
   
   
   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.
    c                2    t         |   |       || _        y rB   rQ  rR  s      r8   rG   z*SelfIntersectingFloatingReduction.__init__  rS  r7   c                    | j                    S rB   rU  rI   s    r8   r   z>SelfIntersectingFloatingReduction._antialias_requires_2_stages  rV  r7   c                    |r3|s1|rt        d      | j                  | j                  S | j                  S t        |   |||||      S )NrO  )r   rD   rZ  r[  r   r  r\  s         r8   r  z/SelfIntersectingFloatingReduction._build_append  sQ    ^)*RSS;;&MMMDDDw$VVT9nUUr7   c                <    t         |          | j                  fz   S rB   r^  r   s    r8   r   z2SelfIntersectingFloatingReduction._hashable_inputs  r_  r7   r`  ra  r   s   @r8   r  r    s$    
-'
V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y	)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.
    c                    |r%t        t        j                  |j                        fS t        t        j                  |j                        fS rB   rh  r   s      r8   r   zsum._antialias_stage_2  rk  r7   c                `    |r*t        | j                        t        | j                        fS | fS rB   )r  rD   r  r   s      r8   r   zsum._build_bases  s)    dkk*C,<==7Nr7   c                j    t        |      s(t        ||| f         r|||| f<   y||| fxx   |z  cc<   yyru  rn  ro  s       r8   r  zsum._append  sG     e}c!Q$i !AqD	  AqD	U"	r7   c                z    |||z
  z  }t        |      s(t        ||| f         r|||| f<   y||| fxx   |z  cc<   yyru  rn  r  s          r8   r  zsum._append_antialias   sV     y>12e}c!Q$i !AqD	  AqD	U"	r7   c                h    ||z  }t        |      s"t        ||| f         s
|||| f   kD  r|||| f<   yyru  rn  r  s          r8   r[  z(sum._append_antialias_not_self_intersect,  sF     ie}c!Q$i EC1I$5!AqD	r7   c                    t        | d      S r  )r"   r  s    r8   r  zsum._combine6  s    d++r7   c                    |r@| \  }}t        j                  ||t         j                        }t        j                  |fi |S t        j                  | d   fi |S rG  )rf   whereri   r^   rI  )rK  r   rL  sumsanysrp  s         r8   r   zsum._finalize:  sP    JD$tRVV,A<<,V,,<<a3F33r7   Nr<  r=  )r1   r2   r3   r4   r   r   r>  r!   r  r  r[  r  r   r6   r7   r8   r  r    s    	W 
   
   
   , , 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.
    c                "    t         j                  S rB   )r:   r>   rI   s    r8   r   zm2.uses_cuda_mutexP  s    ###r7   c                *    t         |   |||||      S rB   )r   r  r\  s         r8   r  zm2._build_appendS  s    w$VVT9nUUr7   c                    | j                   S rB   r  r  s     r8   r  zm2._build_createV  r  r7   c                V    t        | j                        t        | j                        fS rB   )r  rD   rc  r   s     r8   r   zm2._build_tempsY  s    $++&dkk(:;;r7   c                    t        |      sT|dkD  rOt        j                  |      |z  }t        j                  ||z         |dz   z  }||| fxx   ||z
  ||z
  z  z  cc<   yyNr   r   r   r   rf   r  rp  rq  r,  rs  r  rc  u1us           r8   r  z
m2._append]  d    
 e}qyZZ_u,JJsU{+uqy91a4URZEAI66r7   Tr  c                    t        |      sT|dkD  rOt        j                  |      |z  }t        j                  ||z         |dz   z  }||| fxx   ||z
  ||z
  z  z  cc<   yyr2  r3  r4  s           r8   r  zm2._append_cudak  r7  r7   c                    t        j                  dd      5  t        j                  |d      |j                  d      z  }t        j                  | |||z  |z
  dz  z  z   d      cd d d        S # 1 sw Y   y xY w)Nignoredivideinvalidr   r   r;   )rf   errstatenansumr  )Msr)  nsmus       r8   r  zm2._combinex  s`    [[(;4a(266q6>9B99R"d2glQ%6"66Q? <;;s   AA22A;r:  r=  )r1   r2   r3   r4   r   r  r  r   r>  r!   r  r  r  r  r  r   r   s   @r8   r,  r,  D  sv    
$V)< 
	  	 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y
)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                     yr`  r6   rI   s    r8   r   z min._antialias_requires_2_stages  r  r7   c                L    t        t        j                  |j                        fS rB   r   r   MINri   r   s      r8   r   zmin._antialias_stage_2  r  r7   c                ^    t        |      s"t        ||| f         s
||| f   |kD  r|||| f<   yyru  rn  ro  s       r8   r  zmin._append  <     e}&QT"3s1a4y57HC1Ir7   c                h    ||z  }t        |      s"t        ||| f         s
|||| f   kD  r|||| f<   yyru  rn  r  s          r8   r  zmin._append_antialias  E     ie}&QT"3us1a4y7HC1Ir7   Tr  c                Z    t        |      s t        ||| f|      }t        |      s||kD  ryyru  )r   r   rp  rq  rr  rs  r  s        r8   r  zmin._append_cuda  0     e}$S1a&%8Cc{cEkr7   c                0    t        j                  | d      S r  )rf   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   rD  rD    s    N 
   
   W[[   ' 'r7   rD  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y
)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.
    c                L    t        t        j                  |j                        fS rB   r  r   s      r8   r   zmax._antialias_stage_2  r  r7   c                ^    t        |      s"t        ||| f         s
||| f   |k  r|||| f<   yyru  rn  ro  s       r8   r  zmax._append  rJ  r7   c                h    ||z  }t        |      s"t        ||| f         s
|||| f   kD  r|||| f<   yyru  rn  r  s          r8   r  zmax._append_antialias  rL  r7   Tr  c                d    ||z  }t        |      s t        ||| f|      }t        |      s||k  ryyru  r  r  s           r8   r  zmax._append_antialias_cuda  r  r7   c                Z    t        |      s t        ||| f|      }t        |      s||k  ryyru  r  rN  s        r8   r  zmax._append_cuda  rO  r7   c                0    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   rS  rS    s    N 
   
   W[[   W[[   ' 'r7   rS  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                6    t         |   |t                      y rB   )r   rG   rc  rB  s     r8   rG   zcount_cat.__init__  s    )r7   )r1   r2   r3   r4   rG   r   r   s   @r8   r[  r[    s    	* *r7   r[  c                  (    e Zd ZdZd Zedd       Zy)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                V    t        | j                        t        | j                        fS rB   )r  rD   r  r   s      r8   r   zmean._build_bases  s     $++&(B4;;(OPPr7   c                    | \  }}t        j                  dd      5  t        j                  |dkD  ||z  t         j                        }d d d        t	        j
                  fi |S # 1 sw Y   xY wNr:  r;  r   rf   r>  r(  ri   r^   rI  )rK  r   rL  r)  countsrp  s         r8   r   zmean._finalize  sY    f[[(;!T&["&&9A <||A((( <;s   ,A''A0Nr=  r1   r2   r3   r4   r   r>  r   r6   r7   r8   r^  r^    s!    Q ) )r7   r^  c                  (    e Zd ZdZd Zedd       Zy)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                ~    t        | j                        t        | j                        t        | j                        fS rB   r  rD   rc  r,  r   s      r8   r   zvar._build_bases  )    $++&dkk(:Bt{{OLLr7   c                    | \  }}}t        j                  dd      5  t        j                  |dkD  ||z  t         j                        }d d d        t	        j
                  fi |S # 1 sw Y   xY wra  rb  rK  r   rL  r)  rc  m2srp  s          r8   r   zvar._finalize  s\    !fc[[(;!S6\266:A <||A((( <;s   ,A((A1Nr=  rd  r6   r7   r8   rf  rf  	  !    M ) )r7   rf  c                  (    e Zd ZdZd Zedd       Zy)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                ~    t        | j                        t        | j                        t        | j                        fS rB   rh  r   s      r8   r   zstd._build_bases&  ri  r7   c                   | \  }}}t        j                  dd      5  t        j                  |dkD  t        j                  ||z        t         j                        }d d d        t        j                  fi |S # 1 sw Y   xY wra  )rf   r>  r(  sqrtri   r^   rI  rk  s          r8   r   zstd._finalize)  se    !fc[[(;!RWWS6\%:BFFCA <||A((( <;s   ?A;;BNr=  rd  r6   r7   r8   ro  ro    rm  r7   ro  c                  \     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 rB   r   r   r  re  s        r8   rf  z_first_or_last.out_dshape4      bjj!!r7   c                    |xs |S rB   r6   r   s      r8   r   z_first_or_last.uses_row_index7      "{"r7   c                     yr`  r6   rI   s    r8   r   z+_first_or_last._antialias_requires_2_stages:  r  r7   c                    | j                  ||      rN| j                         }t        || j                        }| j                  |_        |j                  ||      |fz   S t        |   ||      S N)selectorlookup_columnr   _create_row_index_selectorr(  rD   r   r   r   rF   r   r   row_index_selectorwrapperr   s        r8   r   z_first_or_last._build_bases=  k    t[1!%!@!@!B%7t{{SG(,G%%224E
RR7'k::r7   c                >    t        |       dkD  rt        d      | d   S Nr   z6_combine should never be called with more than one aggr   ra   RuntimeErrorr  s    r8   r  z_first_or_last._combineG  $     t9q=WXXAwr7   c                     y rB   r6   rI   s    r8   r  z)_first_or_last._create_row_index_selectorO      r7   c                4    t        j                  | d   fi |S Nr   rH  rJ  s      r8   r   z_first_or_last._finalizeR      
 ||E"I000r7   r=  )r1   r2   r3   r4   rf  r   r   r   r>  r  r  r   r   r   s   @r8   rt  rt  1  sI    "#;   1 1r7   rt  c                  R    e Zd ZdZddZeed               Zeed               Zd Z	y)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.
    c                L    t        t        j                  |j                        fS rB   r   r   FIRSTri   r   s      r8   r   zfirst._antialias_stage_2h  s     4 : :L<L<LMOOr7   c                J    t        |      st        ||| f         r|||| f<   yyru  rn  ro  s       r8   r  zfirst._appendk  s-     e}AqD	!2C1Ir7   c                h    ||z  }t        |      s"t        ||| f         s
|||| f   kD  r|||| f<   yyru  rn  r  s          r8   r  zfirst._append_antialiass  rL  r7   c                    t               S rB   )_min_row_indexrI   s    r8   r  z first._create_row_index_selector|      r7   Nr<  
r1   r2   r3   r4   r   r>  r!   r  r  r  r6   r7   r8   r  r  Z  sJ    P 
   
   r7   r  c                  R    e Zd ZdZddZeed               Zeed               Zd Z	y)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.
    c                L    t        t        j                  |j                        fS rB   r   r   LASTri   r   s      r8   r   zlast._antialias_stage_2  s     4 9 9<;K;KLNNr7   c                *    t        |      s|||| f<   yyru  rn  ro  s       r8   r  zlast._append  s     e}C1Ir7   c                h    ||z  }t        |      s"t        ||| f         s
|||| f   kD  r|||| f<   yyru  rn  r  s          r8   r  zlast._append_antialias  rL  r7   c                    t               S rB   )_max_row_indexrI   s    r8   r  zlast._create_row_index_selector  r  r7   Nr<  r  r6   r7   r8   r  r    sJ    O 
   
   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 )FloatingNReductionc                L    t         |   |       |dk\  r|| _        y d| _        y r  )r   rG   n)rF   rD   r  r   s      r8   rG   zFloatingNReduction.__init__  s#     1f!r7   c                4    t        t        j                        S rB   rv  re  s        r8   rf  zFloatingNReduction.out_dshape  rw  r7   c                    d}t        j                  | j                        }t        j                  |      }|dxx   |gz  cc<   ||d   |<   |S )Nr  r  r  )rf   re   r  r  r  )rF   rL  n_namen_valuess       r8   _add_finalize_kwargsz'FloatingNReduction._add_finalize_kwargs  sM    99TVV$ v&v6("#+x r7   c                      fdS )Nc                R    t        t                | j                  fz   |      S rB   )r   r  r  r  r'  r   r   r
  rF   s     r8   r  z2FloatingNReduction._build_create.<locals>.<lambda>  s*    51CT+X,"dffY.,>r7   r6   rF   r
  r   s   ``r8   r  z FloatingNReduction._build_create  s    > 	>r7   c                     d fd	}|S )Nc                R     j                   di |} j                  | fd|i|S )Nr   r6   )r  r   )rK  r   rL  rF   s      r8   r  z4FloatingNReduction._build_finalize.<locals>.finalize  s3    .T..88F!4>>%=d=f==r7   r=  r6   )rF   r   r  s   `  r8   r!  z"FloatingNReduction._build_finalize  s    	> r7   c                <    t         |          | j                  fz   S rB   )r   r   r  r   s    r8   r   z#FloatingNReduction._hashable_inputs  s    w')TVVI55r7   r  )
r1   r2   r3   rG   rf  r  r  r!  r   r   r   s   @r8   r  r    s&    $"
>6 6r7   r  c                  V     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                    |xs |S rB   r6   r   s      r8   r   z!_first_n_or_last_n.uses_row_index  ry  r7   c                     yr`  r6   rI   s    r8   r   z/_first_n_or_last_n._antialias_requires_2_stages  r  r7   c                    | j                  ||      rN| j                         }t        || j                        }| j                  |_        |j                  ||      |fz   S t        |   ||      S r|  r  r  s        r8   r   z_first_n_or_last_n._build_bases  r  r7   c                >    t        |       dkD  rt        d      | d   S r  r  r  s    r8   r  z_first_n_or_last_n._combine  r  r7   c                     y rB   r6   rI   s    r8   r  z-_first_n_or_last_n._create_row_index_selector  r  r7   c                4    t        j                  | d   fi |S r  rH  rJ  s      r8   r   z_first_n_or_last_n._finalize  r  r7   r=  )r1   r2   r3   r4   r   r   r   r>  r  r  r   r   r   s   @r8   r  r    sD    #;   1 1r7   r  c                  N    e Zd ZddZeed               Zeed               Zd Zy)first_nc                P    t        t        j                  |j                  d      fS NTr  r  r   s      r8   r   zfirst_n._antialias_stage_2  s"     4 : :L<L<LZ^_aar7   c                    t        |      sQ|j                  d   }t        ||| |dz
  f         syt        |      D ]  }t        ||| |f         s|||| |f<   |c S  yNr;   r   r   r   r'  r   rp  rq  rr  rs  r  r  s         r8   r  zfirst_n._append  sl     e}		!A#aAaCi.) 1X#aAg,'#(C1aLH	 
 r7   c                    ||z  }t        |      sQ|j                  d   }t        ||| |dz
  f         syt        |      D ]  }t        ||| |f         s|||| |f<   |c S  yr  r  	rp  rq  rr  rs  rw  rx  r  r  r  s	            r8   r  zfirst_n._append_antialias  su     ie}		!A#aAaCi.) 1X#aAg,'#(C1aLH	 
 r7   c                .    t        | j                        S N)r  )_min_n_row_indexr  rI   s    r8   r  z"first_n._create_row_index_selector      $&&))r7   Nr<  	r1   r2   r3   r   r>  r!   r  r  r  r6   r7   r8   r  r    sE    b 
    
  "*r7   r  c                  N    e Zd ZddZeed               Zeed               Zd Zy)last_nc                P    t        t        j                  |j                  d      fS r  r  r   s      r8   r   zlast_n._antialias_stage_2  s"     4 9 9<;K;KY]^``r7   c                @    t        |      st        ||| f   |d       yyru  r   r.   ro  s       r8   r  zlast_n._append"  s%     e}SAYq1r7   c                J    ||z  }t        |      st        ||| f   |d       yyru  r  r  s          r8   r  zlast_n._append_antialias+  s.     ie}SAYq1r7   c                .    t        | j                        S r  )_max_n_row_indexr  rI   s    r8   r  z!last_n._create_row_index_selector5  r  r7   Nr<  r  r6   r7   r8   r  r    sE    a 
   
  *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y)max_nc                "    t         j                  S rB   r:   r=   rI   s    r8   r   zmax_n.uses_cuda_mutex:      """r7   c                P    t        t        j                  |j                  d      fS r  r  r   s      r8   r   zmax_n._antialias_stage_2=  "     4 8 8,:J:JX\]__r7   c                    t        |      sQ|j                  d   }t        |      D ]4  }t        ||| |f         s|||| |f   kD  s t        ||| f   ||       |c S  yNr;   r   r   r'  r   r.   r  s         r8   r  zmax_n._appendA  i     e} 		!A1X#aAg,'53q!Qw<+?$SAYq9H  r7   c                    ||z  }t        |      sQ|j                  d   }t        |      D ]4  }t        ||| |f         s|||| |f   kD  s t        ||| f   ||       |c S  yr  r  r  s	            r8   r  zmax_n._append_antialiasN  r     ie} 		!A1X#aAg,'53q!Qw<+?$SAYq9H  r7   Tr  c                    t        |      sQ|j                  d   }t        |      D ]4  }t        ||| |f         s|||| |f   kD  s t        ||| f   ||       |c S  yr  r   r'  r   r   r  s         r8   r  zmax_n._append_cuda]  i     e} 		!A1X#aAg,'53q!Qw<+?)#ad)UA>H  r7   c                6    |r| j                   S | j                  S rB   _combine_cudar  r  s         r8   r  zmax_n._build_combinej      %%%== r7   c                    | d   }t        dt        |             D ]6  }|j                  dk(  rt        | d   | |          %t	        | d   | |          8 |S Nr   r      )r   ra   ndimr'   r&   r  s      r8   r  zmax_n._combinep  V    1gq#d)$Axx1}$T!Wd1g6$T!Wd1g6	 %
 
r7   c                    | d   }t        |j                  d d       }t        dt        |             D ]<  }|j                  dk(  rt        |   | d   | |          (t        |   | d   | |          > |S Nr   r   r   r  )r   r'  r   ra   r  r   r   r  r  kernel_argsr  s       r8   r  zmax_n._combine_cudaz  t    1g		#2/q#d)$Axx1})+6tAwQH)+6tAwQ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  9  s    #` 
	  	 

  
 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y)min_nc                "    t         j                  S rB   r  rI   s    r8   r   zmin_n.uses_cuda_mutex  r  r7   c                     yr`  r6   rI   s    r8   r   z"min_n._antialias_requires_2_stages  r  r7   c                P    t        t        j                  |j                  d      fS r  rG  r   s      r8   r   zmin_n._antialias_stage_2  r  r7   c                    t        |      sQ|j                  d   }t        |      D ]4  }t        ||| |f         s|||| |f   k  s t        ||| f   ||       |c S  yr  r  r  s         r8   r  zmin_n._append  r  r7   c                    ||z  }t        |      sQ|j                  d   }t        |      D ]4  }t        ||| |f         s|||| |f   k  s t        ||| f   ||       |c S  yr  r  r  s	            r8   r  zmin_n._append_antialias  r  r7   Tr  c                    t        |      sQ|j                  d   }t        |      D ]4  }t        ||| |f         s|||| |f   k  s t        ||| f   ||       |c S  yr  r  r  s         r8   r  zmin_n._append_cuda  r  r7   c                6    |r| j                   S | j                  S rB   r  r  s         r8   r  zmin_n._build_combine  r  r7   c                    | d   }t        dt        |             D ]6  }|j                  dk(  rt        | d   | |          %t	        | d   | |          8 |S r  )r   ra   r  r)   r(   r  s      r8   r  zmin_n._combine  r  r7   c                    | d   }t        |j                  d d       }t        dt        |             D ]<  }|j                  dk(  rt        |   | d   | |          (t        |   | d   | |          > |S r  )r   r'  r   ra   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    s    #` 
	  	 

  
 W[[	  	!    r7   r  c                  F    e Zd ZdZd Zed        Zed        Zed        Zy)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                F    t        t        t        j                              S rB   r  re  s        r8   rf  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Sr7   c                    t        d      r   r~   r  s    r8   r  zmode._combine  r  r7   c                    t        d      r   r~   )rK  rL  s     r8   r   zmode._finalize  r  r7   N)	r1   r2   r3   r4   rf  r>  r  r  r   r6   r7   r8   r  r    sN     * T T T T T Tr7   r  c                  8    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.
    c                   t        |t        t        t        t        t
        t        t        t        t        t        f
      st        d      |t        j                  }t        | =  |       || _        |j"                  |f| _        y )NzWselector can only be a first, first_n, last, last_n, max, max_n, min or min_n reduction)r]   r  r  r  r  rS  r  rD  r  _max_or_min_row_index_max_n_or_min_n_row_indexr  r0   r5   r   rG   r}  rD   r  )rF   r}  r~  r   s      r8   rG   zwhere.__init__  st    (UGT63sTY%:<U%W X56 6  )22M'  7r7   c                `    t        t        |       | j                         | j                  f      S rB   )r  r   r   r}  rI   s    r8   r  zwhere.__hash__  s%    T$Z!6!6!8$--HIIr7   c                     yr`  r6   rI   s    r8   r   zwhere.is_where!  r  r7   c                    | j                   t        j                  k(  rt        t        j
                        S t        t        j                        S rB   )rD   r0   r5   r   r   rg   r  )rF   r   r  r   r   s        r8   rf  zwhere.out_dshape$  s2    ;;-000"((##"**%%r7   c                "    t         j                  S rB   r  rI   s    r8   r   zwhere.uses_cuda_mutex*  r  r7   c                x    | j                   t        j                  k(  xs | j                  j	                  ||      S rB   )rD   r0   r5   r}  r   r   s      r8   r   zwhere.uses_row_index-  s4    }555 @,,T;?	Ar7   c                ,   | j                   t        j                  k7  rt        |   |       | j
                  j	                  |       | j                   t        j                  k7  r/| j                   | j
                  j                   k(  rt        d      y y )Nz<where and its contained reduction cannot use the same column)rD   r0   r5   r   r   r}  r   rE  s     r8   r   zwhere.validate1  sp    ;;-000GY'y);;-000T[[DMMDXDX5X[\\ 6Y0r7   c                    | j                   j                  ||      }| j                  t        j                  k(  r)t        |d   j                  d|d   j                        f}|S )Nr   r   )r  r  r  )r}  r   rD   r0   r5   r   r  r  r  s       r8   r   zwhere._antialias_stage_28  s^    mm..~|L;;-000"s1v/A/A(*/21v/A/AC EC 
r7   c                Z    |j                   dkD  rt        ||| f   ||       |S |||| f<   |S Nr;   r  r.   rp  rq  rr  rs  update_indexs        r8   r  zwhere._appendF  s@     88a<SAY|<  C1Ir7   c                V    |j                   dkD  rt        ||| f   ||       y |||| f<   y r  r  rp  rq  rr  rs  rw  rx  r  s          r8   r  zwhere._append_antialiasO  s0     88a<SAY|<C1Ir7   Tr  c                Z    |j                   dkD  rt        ||| f   ||       |S |||| f<   |S r  r  r   r  s          r8   r  zwhere._append_antialias_cudaX  s@     88a<!#ad)ULA  C1Ir7   c                Z    |j                   dkD  rt        ||| f   ||       |S |||| f<   |S r  r  r  s        r8   r  zwhere._append_cudab  s@     88a<!#ad)ULA  C1Ir7   c                n    |r|r| j                   S | j                  S |r| j                  S | j                  S rB   )r  r  r  r  r  s         r8   r  zwhere._build_appendk  s;     222(((---||#r7   c                   | j                   }t        |t        t        f      r|j	                  ||      r|j                         }| j                  t        j                  k(  r-| j                   j                  |_	        |j                  ||      S t        || j                        }| j                   j                  |_	        |j                  ||      |j                  ||      z   S |j                  ||      t        | )  ||      z   S rB   )r}  r]   rt  r  r   r  rD   r0   r5   r   r   r(  r   )rF   r   r   r}  r  	new_wherer   s         r8   r   zwhere._build_basesy  s    ==h1C DE''k:!)!D!D!F{{m444 8<}}7K7K"4)66t[II!"4dkkB	.2mm.B.B	+)66t[I**4=> > (({;$T;78 8r7   c                @   | j                   }t        |t              }|r|j                  n|j                  | 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|S |S |r|	S |S |r|r|S |
S |r|S |S )	Nc                    | d   j                   \  }}t        |      D ]I  }t        |      D ]9  }|d   ||f   } |      r |||d   |      dk\  s(| d   ||f   | d   ||f<   ; K y r  r'  r   )	r  selector_aggsnynxrq  rp  r  appendr=  s	          r8   combine_cpu_2dz/where._combine_callback.<locals>.combine_cpu_2d  s    !W]]FB2YrA)!,QT2E"5>fQ=;KU.SWX.X(,Q1Q1 # r7   c                   | d   j                   \  }}}t        |      D ]f  }t        |      D ]V  }t        |      D ]F  }|d   |||f   } 
|      r 	|||d   d d d d |f   |      dk\  s3| d   |||f   | d   |||f<   H X h y r  r  )r  r  r   r!  ncatrq  rp  r   r  r"  r=  s            r8   combine_cpu_3dz/where._combine_callback.<locals>.combine_cpu_3d  s    7==LBD2YrA$T{ -a 0As ;&u~&A}Q?OPQSTVYPY?Z9>3@CD3E15aAs1CDGAq#I.	  + # r7   c           
        | d   j                   \  }}}t        |      D ]j  }t        |      D ]Z  }t        |      D ]J  }|d   |||f   } |      r & 
|||d   |      }	|	dk  r ;t        | d   ||f   | d   |||f   |	       L \ l y r  r'  r   r.   )r  r  r   r!  r  rq  rp  r  r  r  r"  r=  s             r8   combine_cpu_n_3dz1where._combine_callback.<locals>.combine_cpu_n_3d  s    QIBA2YrA"1X -a 0Aq 9"5>!'-aM!4De'L'!+!(aAQ1a8H,W & # r7   c                ^   | d   j                   \  }}}}t        |      D ]  }t        |      D ]z  }t        |      D ]j  }t        |      D ]Z  }	|d   ||||	f   }
 |
      r ' |||d   d d d d |d d f   |
      }|dk  r It        | d   |||f   | d   ||||	f   |       \ l |  y r  r(  )r  r  r   r!  r%  r  rq  rp  r   r  r  r  r"  r=  s               r8   combine_cpu_n_4dz1where._combine_callback.<locals>.combine_cpu_n_4d  s    "1gmmOBD!2YrA$T{!&qA$1!$4Q3\$BE&u~ %+1!Qa8HAsTU8VX]+^L+a/ %,T!WQ3Y-?aAsTUAV-9; "*  + # r7   c                    | d   j                   \  }}t        j                  d      \  }}||k  r>||k  r8|d   ||f   } |      s% |||d   |      dk\  r| d   ||f   | d   ||f<   y y y y y Nr   r;   r   r'  r  grid)	r  r  r   r!  rp  rq  r  r"  r=  s	          r8   combine_cuda_2dz0where._combine_callback.<locals>.combine_cuda_2d  s    !W]]FB<<?DAq2v!b&%a(A.u~&A}Q7G*OST*T$(GAqDMDGAqDM +U~ !vr7   c                
   | d   j                   \  }}}t        j                  d      \  }}}||k  rQ||k  rK||k  rE|d   |||f   } 
|      s1 	|||d   d d d d |f   |      dk\  r| d   |||f   | d   |||f<   y y y y y y Nr   r  r   r.  )r  r  r   r!  r%  rp  rq  r   r  r"  r=  s            r8   combine_cuda_3dz0where._combine_callback.<locals>.combine_cuda_3d  s    7==LBDQIAq#2v!b&S4Z%a(As3u~&A}Q7G1c	7RTY*Z^_*_)-aAs);DGAq#I& +`~ &0&vr7   c                $   | d   j                   \  }}}t        j                  d      \  }}||k  r_||k  rYt        |      D ]J  }|d   |||f   } |      r y  
|||d   |      }	|	dk  r y t	        | d   ||f   | d   |||f   |	       L y y y r-  r'  r  r/  r   r   )r  r  r   r!  r  rp  rq  r  r  r  r"  r=  s             r8   combine_cuda_n_3dz2where._combine_callback.<locals>.combine_cuda_n_3d  s    QIBA<<?DAq2v!b&qA)!,Q1W5Eu~#)!Qa0@%#HL#a')$q'!Q$-aAq9I<X " !vr7   c                T   | d   j                   \  }}}}t        j                  d      \  }}}||k  ru||k  ro||k  rit        |      D ]Z  }	|d   ||||	f   }
 |
      r y  |||d   d d d d |d d f   |
      }|dk  r y t	        | d   |||f   | d   ||||	f   |       \ y y y y r2  r5  )r  r  r   r!  r%  r  rp  rq  r   r  r  r  r"  r=  s               r8   combine_cuda_n_4dz2where._combine_callback.<locals>.combine_cuda_n_4d  s    "1gmmOBD!QIAq#2v!b&S4ZqA)!,Q3\:Eu~#)!Qa0@AsA0NPU#VL#a')$q'!Q)*<d1gaCQRl>SUab " &0&vr7   )r}  r]   r  r  r  r   r   r   r!   r  r  )rF   r   r   r  r}  is_n_reductionr#  r&  r)  r+  r0  r3  r6  r8  r"  r=  s                 @@r8   _combine_callbackzwhere._combine_callback  sL    ==#H.@A**F%%F #mm::4M(SY		6 
	6 
	D 
	D 
	X 
	X 
	; 
	; 
	2 
	2 
	< 
	< 
	Y 
	Y 
	c 
	c ,7(N=NN+6'L<LL *5J?J)4~H.Hr7   c                @      j                  ||       fd}|S )Nc                f   t        |       dk(  rnrct        |       dk(  sJ t        j                  t              }|r| d   j                  d d n| d   j                  } t        |         | |       n3t        dt        |             D ]  } | d   | |   f|d   ||   f        | d   |d   fS )Nr   r;   r   r   )ra   r]   r}  r  r'  r   r   )r  r  r9  r'  r  combiner   rF   s        r8   wrapped_combinez-where._build_combine.<locals>.wrapped_combine  s    4yA~4yA~%~!+DMM;M!N.<Qcr*$q'--)	%()$>q#d),AT!Wd1g.q1A=QRCS0TU - 7M!,,,r7   )r:  )rF   r   r  r   r   r  r>  r=  s   `  `   @r8   r  zwhere._build_combine  s$    (({KH	- r7   c                    | j                   fS rB   )r}  r   s      r8   r   zwhere._build_combine_temps  s    r7   c                h     t         j                  t              r fdS t                 S )Nc                f    t        t                | j                  j                  fz   |      S rB   )r   r(  r  r}  r  r  s     r8   r  z%where._build_create.<locals>.<lambda>'  s/    uUD/O0!!&$--//);!;\0Kr7   )r]   r}  r  r   r  r  s   ``r8   r  zwhere._build_create!  s2     dmm%78K K 7(99r7   c                z    t        | j                  t              r| j                  j                  nd dfd	}|S )Nc                J     di |}t        j                  | d   fi |S )Nr   r6   rH  )rK  r   rL  add_finalize_kwargss      r8   r  z'where._build_finalize.<locals>.finalize2  s.    ".,6v6<<b	4V44r7   r=  )r]   r}  r  r  )rF   r   r  rD  s      @r8   r!  zwhere._build_finalize,  s4    dmm%78"&--"D"D"&	5 r7   rB   )r}  r   r~  z
str | Noner:  r<  r=  )r1   r2   r3   r4   rG   r  r   rf  r   r   r   r   r>  r!   r  r  r  r  r  r  r  r   r:  r  r   r  r!  r   r   s   @r8   r(  r(    s    28J&#A] 
   
   W[[   W[[  $8(|I|& 	:r7   r(  c                  >    e Zd ZdZd Zd Zd Zd Zd Ze	d        Z
y)	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                d    t        t        |j                                \  }}|| _        || _        y rB   )zipsorteditemskeysrq   )rF   rL  ksvss       r8   rG   zsummary.__init__R  s)    fV\\^,-B	r7   c                |    t        t        |       t        | j                        t        | j                        f      S rB   )r  r   r  rK  rq   rI   s    r8   r  zsummary.__hash__W  s*    T$Ztyy!153EFGGr7   c                J    | j                   D ]  }|j                         s y yNTF)rq   r   )rF   vs     r8   r   zsummary.is_categoricalZ  s$    A!  r7   c                N    | j                   D ]  }|j                  ||      s y yrP  )rq   r   )rF   r   r   rQ  s       r8   r   zsummary.uses_row_index`  s(    Ak2  r7   c                V   | j                   D ]  }|j                  |        g }| j                   D ]J  }t        |t              r|j                  }t        |t
              s0|j                  |j                         L t        t        j                  |            dkD  rt        d      y )Nr   zKUsing multiple FloatingNReductions with different n values is not supported)rq   r   r]   r(  r}  r  r"  r  ra   rf   r   r   )rF   r   rQ  r  s       r8   r   zsummary.validatef  s    AJJ|$  A!U#JJ!/0$	 
 ryy"#a']_ _ (r7   c                ^    t        t        t        d | j                  D                          S )Nc              3  4   K   | ]  }|j                     y wrB   )rJ   )r  rQ  s     r8   r  z!summary.inputs.<locals>.<genexpr>w  s     "A[188[s   )r  r   r   rq   rI   s    r8   rJ   zsummary.inputsu  s!    VF"AT[["AABCCr7   N)r1   r2   r3   r4   rG   r  r   r   r   rN   rJ   r6   r7   r8   rF  rF  ;  s8    ,
H_ D Dr7   rF  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                B    t         |   t        j                         y )NrC   )r   rG   r0   r5   r   s    r8   rG   z_max_or_min_row_index.__init__}  s     6 67r7   c                4    t        t        j                        S rB   r   r   rg   re  s        r8   rf  z _max_or_min_row_index.out_dshape      bhhr7   c                     yr`  r6   r   s      r8   r   z$_max_or_min_row_index.uses_row_index  r  r7   )r1   r2   r3   r4   rG   rf  r   r   r   s   @r8   r  r  z  s    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y	)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.
    c                8    t        t        j                  d      fS r  r   r   r  r   s      r8   r   z!_max_row_index._antialias_stage_2       4 8 8"=??r7   c                (    |||| f   kD  r|||| f<   yyru  r6   ro  s       r8   r  z_max_row_index._append  s'     3q!t9C1Ir7   c                (    |||| f   kD  r|||| f<   yyru  r6   rv  s         r8   r  z _max_row_index._append_antialias  s'    
 3q!t9C1Ir7   Tr  c                `    |dk7  r)t         j                  j                  ||| f|      }||k  ryyNr   r   )r  r  rS  rN  s        r8   r  z_max_row_index._append_cuda  s4     B;..$$S1a&%8CU{r7   c                z    | d   }t        dt        |             D ]  }t        j                  || |   |        |S )Nr   r   )out)r   ra   rf   maximumr  s      r8   r  z_max_row_index._combine  s=     1gq#d)$AJJsDG- % 
r7   Nr<  )r1   r2   r3   r4   r   r>  r!   r  r  r  r  r  r  r6   r7   r8   r  r    s    @ 
   
   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y)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                     yr`  r6   rI   s    r8   r   z+_min_row_index._antialias_requires_2_stages  r  r7   c                8    t        t        j                  d      fS r  r   r   rH  r   s      r8   r   z!_min_row_index._antialias_stage_2  r_  r7   c                "    t         j                  S rB   r  rI   s    r8   r   z_min_row_index.uses_cuda_mutex  r  r7   c                F    |dk7  r||| f   dk(  s
|||| f   k  r|||| f<   yyrc  r6   ro  s       r8   r  z_min_row_index._append  s;     B;C1IOus1a4y/@C1Ir7   c                F    |dk7  r||| f   dk(  s
|||| f   k  r|||| f<   yyrc  r6   rv  s         r8   r  z _min_row_index._append_antialias  ;    
 B;C1IOus1a4y/@C1Ir7   Tr  c                F    |dk7  r||| f   dk(  s
|||| f   k  r|||| f<   yyrc  r6   ro  s       r8   r  z_min_row_index._append_cuda  rn  r7   c                6    |r| j                   S | j                  S rB   r  r  s         r8   r  z_min_row_index._build_combine  r  r7   c                b    | d   }t        dt        |             D ]  }t        || |           |S r  )r   ra   r%   r  s      r8   r  z_min_row_index._combine  s7     1gq#d)$AS$q'* % 
r7   c                (   | d   }t        |       dkD  ry|j                  dk(  r#| D cg c]  }t        j                  |d       } }t	        |j
                  d d       }t        dt        |             D ]  }t        |   | d   | |           |S c c}w )Nr   r   r;   r  )ra   r  rd   expand_dimsr   r'  r   r   )r  r  rr  r  r  s        r8   r  z_min_row_index._combine_cuda  s    1gt9q=xx1}:>?$3sA.$?#CIIbqM2K1c$i(%k247DGD )
	 @s   B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    @# 
   
   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.
    c                j    t         |   t        j                         |dk\  r|| _        y d| _        y )NrC   r   )r   rG   r0   r5   r  )rF   r  r   s     r8   rG   z"_max_n_or_min_n_row_index.__init__	  s,     6 671f!r7   c                4    t        t        j                        S rB   rY  re  s        r8   rf  z$_max_n_or_min_n_row_index.out_dshape	  rZ  r7   c                "    t         j                  S rB   r  rI   s    r8   r   z)_max_n_or_min_n_row_index.uses_cuda_mutex	  r  r7   c                     yr`  r6   r   s      r8   r   z(_max_n_or_min_n_row_index.uses_row_index	  r  r7   c                6    |r| j                   S | j                  S rB   r  r  s         r8   r  z(_max_n_or_min_n_row_index._build_combine	  r  r7   )r   r:  r=  )
r1   r2   r3   r4   rG   rf  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y
)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.
    c                <    t        t        j                  dd      fS Nr   Tr  r^  r   s      r8   r   z#_max_n_row_index._antialias_stage_2#	       4 8 8"$OQQr7   c                    |dk7  rK|j                   d   }t        |      D ].  }||| |f   dk(  s|||| |f   kD  st        ||| f   ||       |c S  yNr   r;   r(  r  s         r8   r  z_max_n_row_index._append&	  i     B; 		!A1Xq!Qw<2%Q1W)=$SAYq9H  r7   c                    |dk7  rg|j                   d   }t        |      D ]J  }||| |f   dk(  s|||| |f   kD  st        |dz
  |d      D ]  }||| |dz
  f   ||| |f<    |||| |f<   |c S  y)Nr   r;   r   r  )	rp  rq  rr  rs  rw  rx  r  r  js	            r8   r  z"_max_n_row_index._append_antialias4	  s    
 B; 		!A1Xq!Qw<2%Q1W)="1Q32.'*1a19~Aq!G /#(C1aLH  r7   Tr  c                    |dk7  rK|j                   d   }t        |      D ].  }||| |f   dk(  s|||| |f   kD  st        ||| f   ||       |c S  yr  r'  r   r   r  s         r8   r  z_max_n_row_index._append_cudaG	  i    
 B; 		!A1Xq!Qw<2%Q1W)=)#ad)UA>H  r7   c                    | d   }t        |       dkD  r5|j                  dk(  rt        | d   | d          |S t        | d   | d          |S r  )ra   r  r+   r*   r  r  s     r8   r  z_max_n_row_index._combineV	  S    1gt9q=xx1}%d1gtAw7 
 &d1gtAw7
r7   c                    | d   }t        |       dkD  rSt        |j                  d d       }|j                  dk(  rt	        |   | d   | d          |S t        |   | d   | d          |S Nr   r   r   r  )ra   r   r'  r  r   r   r  r  r  s      r8   r  z_max_n_row_index._combine_cuda`	  sq    1gt9q=#CIIcrN3Kxx1}*;7QaI 
 +;7QaI
r7   Nr<  )r1   r2   r3   r4   r   r>  r!   r  r  r  r  r  r  r  r6   r7   r8   r  r  	  s    R 

  
 
  " 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y)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                     yr`  r6   rI   s    r8   r   z-_min_n_row_index._antialias_requires_2_stagess	  r  r7   c                <    t        t        j                  dd      fS r|  rj  r   s      r8   r   z#_min_n_row_index._antialias_stage_2v	  r}  r7   c                    |dk7  rK|j                   d   }t        |      D ].  }||| |f   dk(  s|||| |f   k  st        ||| f   ||       |c S  yr  r(  r  s         r8   r  z_min_n_row_index._appendy	  r  r7   c                    |dk7  rK|j                   d   }t        |      D ].  }||| |f   dk(  s|||| |f   k  st        ||| f   ||       |c S  yr  r(  )rp  rq  rr  rs  rw  rx  r  r  s           r8   r  z"_min_n_row_index._append_antialias	  si    
 B; 		!A1Xq!Qw<2%Q1W)=$SAYq9H  r7   Tr  c                    |dk7  rK|j                   d   }t        |      D ].  }||| |f   dk(  s|||| |f   k  st        ||| f   ||       |c S  yr  r  r  s         r8   r  z_min_n_row_index._append_cuda	  r  r7   c                    | d   }t        |       dkD  r5|j                  dk(  rt        | d   | d          |S t        | d   | d          |S r  )ra   r  r-   r,   r  s     r8   r  z_min_n_row_index._combine	  r  r7   c                    | d   }t        |       dkD  rSt        |j                  d d       }|j                  dk(  rt	        |   | d   | d          |S t        |   | d   | d          |S r  )ra   r   r'  r  r   r   r  s      r8   r  z_min_n_row_index._combine_cuda	  sq    1gt9q=#CIIcrN3Kxx1}*;7QaI 
 +;7QaI
r7   Nr<  )r1   r2   r3   r4   r   r   r>  r!   r  r  r  r  r  r  r  r6   r7   r8   r  r  l	  s    R 

  
 
   W[[     	 	r7   r  )w
__future__r   r  enumr   packaging.versionr   numpyrf   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   ImportErrorrb   r   rd   	Exceptionutilsr    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r0   r:   r@   rP   ry   r   r   r   r   r   r@  rO  rc  r  r  r  r  r  r  r  r  r,  rD  rS  r[  r^  rf  ro  rt  r  r  r  r  r  r  r  r  r  r(  rF  r  r  r  r  r  r  r   setlocalsrJ  r]   r   
issubclass__all__)_k_vs   00r8   <module>r     s   "   %  B B 0    F - !U       	D 	D  #*j #*L; ;(4' 48#!n #!J3 3l,-( ,-^c5 c5L0Y 0$!C-C !CHA2 AH :| ||A
  AH '	  'F0	 0:,! :,zC(9 C>D4
+ D4N8@	 8@v-'
 -'`4'
 4'n* *)9 )()) )()) )(&1Y &1R# N # L# > # L 6/  6F#1+ #1L+*  +*\* *8J JZM M`T9 TBA AH
<Dd <D~
2 
0* 0fH* HV! 2 !.M0 M`M0 M` sFHNN$4 ;$452b!"T*
2i0HBRYM9.D#4b#: : $4 ; < = '(:;	<KM  U 	U+Y8M!#<!#<"$>"$>@UU  D2tL;s*   K+ /L /*L+LL	LL