a
    Df[                    @  s  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 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+ W n2 e,y   d\ZZ Z!Z"Z#Z$Z%Z&Z'Z(Z)Z*Z+Y n0 zd dl-Z-d dl.Z/W n e0y@   d Z-Z/Y n0 ddl1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@ G dd deZAG dd deZBG dd de2ZCG dd deCZDG dd deCZEG dd deEZFG dd deFZGG dd deGZHG dd  d eEZIG d!d" d"e2ZJG d#d$ d$eJZKG d%d& d&eKZLG d'd( d(eLZMG d)d* d*eMZNG d+d, d,eJZOG d-d. d.eKZPG d/d0 d0eJZQG d1d2 d2eJZRG d3d4 d4eRZSG d5d6 d6eRZTG d7d8 d8eTZUG d9d: d:eRZVG d;d< d<eRZWG d=d> d>eRZXG d?d@ d@eOZYG dAdB dBeJZZG dCdD dDeJZ[G dEdF dFeJZ\G dGdH dHeJZ]G dIdJ dJe]Z^G dKdL dLe]Z_G dMdN dNeKZ`G dOdP dPe`ZaG dQdR dReaZbG dSdT dTeaZcG dUdV dVe`ZdG dWdX dXe`ZeG dYdZ dZeJZfG d[d\ d\eRZgG d]d^ d^e2ZhG d_d` d`eKZiG dadb dbeiZjG dcdd ddeiZkG dedf dfe`ZlG dgdh dhelZmG didj djelZneoepdkdl eq r D ddg ZsdS )m    )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                   @  s   e Zd ZdZdZdS )SpecialColumna  
    Internally datashader identifies the columns required by the user's
    Reductions and extracts them from the supplied source (e.g. DataFrame) to
    pass through the dynamically-generated append function in compiler.py and
    end up as arguments to the Reduction._append* functions. Each column is
    a string name or a SpecialColumn. A column of None is used in Reduction
    classes to denote that no column is required.
    r   N)__name__
__module____qualname____doc__RowIndex r4   r4   b/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/datashader/reductions.pyr.   -   s   r.   c                   @  s   e Zd ZdZdZdZdZdS )UsesCudaMutexaY  
    Enum that encapsulates the need for a Reduction to use a CUDA mutex to
    operate correctly on a GPU. Possible values:

    No: the Reduction append_cuda function is atomic and no mutex is required.
    Local: Reduction append_cuda needs wrapping in a mutex.
    Global: the overall compiled append function needs wrapping in a mutex.
    r   r      N)r/   r0   r1   r2   NoLocalGlobalr4   r4   r4   r5   r6   9   s   r6   c                   @  s6   e Zd ZdZddddZedd Zedd	 Zd
S )
Preprocessz#Base clase for preprocessing steps.str | SpecialColumn | Nonecolumnc                 C  s
   || _ d S Nr=   selfr>   r4   r4   r5   __init__I   s    zPreprocess.__init__c                 C  s   | j fS r?   r=   rA   r4   r4   r5   inputsL   s    zPreprocess.inputsc                 C  s   d S r?   r4   rC   r4   r4   r5   nan_check_columnP   s    zPreprocess.nan_check_columnN)r/   r0   r1   r2   rB   propertyrD   rE   r4   r4   r4   r5   r;   G   s   
r;   c                   @  s   e Zd ZdZdd ZdS )extractz=Extract a column from a dataframe as a numpy array of values.c                 C  s|  | j tju rVd}t|tjr2|j| }|jd }n$t|dd }t|pF||d}t|}t	rt|t	j
r| j tju rtj||| tjdS || j  jjdkrtj}nd}tt	jtdkr|| j  j|dS t|| j  j|d	S | j tju r$|rtj||| tjdS tj||| tjdS nTt|tjrl|r^t|| j  jtjs^t|| j  S || j  jS n|| j  jS d S )
NZ_datashader_row_offsetZ_datashader_row_lengthattrsr   dtypef22.02Zna_valuefillna)r>   r.   r3   
isinstancexrZDatasetrH   getattrlencudf	DataFramecparangenpint64rJ   kindnanr   __version__to_cupyarrayto_gpu_arraydataZndarrayasarrayvalues)rA   dfr   	attr_name
row_offsetZ
row_lengthrH   nullvalr4   r4   r5   applyW   s4    
zextract.applyN)r/   r0   r1   r2   rg   r4   r4   r4   r5   rG   U   s   rG   c                   @  s4   e Zd ZdZedd Zdd Zdd Zdd	 Zd
S )CategoryPreprocessz*Base class for categorizing preprocessors.c                 C  s   | j S z"Returns name of categorized columnr=   rC   r4   r4   r5   
cat_column}   s    zCategoryPreprocess.cat_columnc                 C  s   t ddS )z7Returns list of categories corresponding to input shapezcategories not implementedNNotImplementedErrorrA   input_dshaper4   r4   r5   
categories   s    zCategoryPreprocess.categoriesc                 C  s   t ddS )zValidates input shapezvalidate not implementedNrk   rA   	in_dshaper4   r4   r5   validate   s    zCategoryPreprocess.validatec                 C  s   t ddS )z3Applies preprocessor to DataFrame and returns arrayzapply not implementedNrk   rA   rc   r   r4   r4   r5   rg      s    zCategoryPreprocess.applyN)	r/   r0   r1   r2   rF   rj   ro   rr   rg   r4   r4   r4   r5   rh   {   s   
rh   c                   @  s(   e Zd ZdZdd Zdd Zdd ZdS )	category_codesal  
    Extract just the category codes from a categorical column.

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

    See the implementation of ``category_modulo`` in ``reductions.py``
    for an example.
    c                 C  s   |j | j jS r?   )measurer>   ro   rm   r4   r4   r5   ro      s    zcategory_codes.categoriesc                 C  s4   | j |jvrtdt|j| j  tjs0tdd S )Nspecified column not foundzinput must be categorical)r>   dict
ValueErrorrP   ru   ctZCategoricalrp   r4   r4   r5   rr      s    zcategory_codes.validatec                 C  sZ   t rFt|t jrFtt jtdkr4|| j jj S || j jj	 S || j jjj
S d S NrL   )rT   rP   rU   r   r\   r>   catcodesr]   r_   rb   rs   r4   r4   r5   rg      s
    zcategory_codes.applyN)r/   r0   r1   r2   ro   rr   rg   r4   r4   r4   r5   rt      s   
rt   c                	      sn   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d	Z fddZdd Zd	d
 Zdd Z  ZS )category_moduloz
    A variation on category_codes that assigns categories using an integer column, modulo a base.
    Category is computed as (column_value - offset)%modulo.
    r   c                   s   t  | || _|| _d S r?   )superrB   offsetmodulo)rA   r>   r   r   	__class__r4   r5   rB      s    zcategory_modulo.__init__c                   s   t   | j| jf S r?   )r~   _hashable_inputsr   r   rC   r   r4   r5   r      s    z category_modulo._hashable_inputsc                 C  s   t t| jS r?   )listranger   rp   r4   r4   r5   ro      s    zcategory_modulo.categoriesc                 C  s2   | j |jvrtd|j| j  | jvr.tdd S )Nrv   zinput must be an integer column)r>   rw   rx   ru   IntegerTypesrp   r4   r4   r5   rr      s    zcategory_modulo.validatec                 C  sR   || j  | j | j }trHt|tjrHttjtdkr@| S |	 S |j
S d S rz   )r>   r   r   rT   rP   ZSeriesr   r\   r]   r_   rb   )rA   rc   r   resultr4   r4   r5   rg      s    zcategory_modulo.apply)r   )r/   r0   r1   r2   ry   bool_Zuint8Zuint16uint32Zuint64Zint8int16int32rY   r   rB   r   ro   rr   rg   __classcell__r4   r4   r   r5   r}      s   r}   c                      s>   e Zd ZdZd fdd	Z fddZdd Zd	d
 Z  ZS )category_binninga  
    A variation on category_codes that assigns categories by binning a continuous-valued column.
    The number of categories returned is always nbins+1.
    The last category (nbin) is for NaNs in the data column, as well as for values under/over the
    binned interval (when include_under or include_over is False).

    Parameters
    ----------
    column:   column to use
    lower:    lower bound of first bin
    upper:    upper bound of last bin
    nbins:     number of bins
    include_under: if True, values below bin 0 are assigned to category 0
    include_over:  if True, values above the last bin (nbins-1) are assigned to category nbin-1
    Tc                   sT   t  ||d  || _|| t| | _|| _|r8dn|| _|rJ|d n|| _d S Nr   r   )r~   rB   bin0floatbinsizenbins	bin_underbin_over)rA   r>   lowerupperr   Zinclude_underZinclude_overr   r4   r5   rB      s    zcategory_binning.__init__c                   s   t   | j| j| j| jf S r?   )r~   r   r   r   r   r   rC   r   r4   r5   r      s    z!category_binning._hashable_inputsc                 C  s   | j |jvrtdd S )Nrv   )r>   rw   rx   rp   r4   r4   r5   rr      s    zcategory_binning.validatec                 C  s   t r\t|t jr\tt jtdkr8|| j jtjd}nt	|| j j
dd}t|}n|| j  }t|}|| j | j }d||< |t}| j||dk < | j||| jk< | j||< |S )NrL   rM   TrN   r   )rT   rP   rU   r   r\   r>   r]   rV   r[   r^   r_   isnanZto_numpyrX   r   r   Zastypeintr   r   r   )rA   rc   r   rb   Z
nan_valuesZindex_floatindexr4   r4   r5   rg      s    


zcategory_binning.apply)TT)	r/   r0   r1   r2   rB   r   rr   rg   r   r4   r4   r   r5   r      s
   r   c                      sP   e Zd ZdZ fddZedd Zedd Zdd	 Zd
d Z	dd Z
  ZS )category_valueszVExtract a category and a value column from a dataframe as (2,N) numpy array of values.c                   s   t  | || _d S r?   )r~   rB   categorizer)rA   r   Zvalue_columnr   r4   r5   rB     s    zcategory_values.__init__c                 C  s   | j j| jfS r?   r   r>   rC   r4   r4   r5   rD     s    zcategory_values.inputsc                 C  s   | j jS ri   r   rC   r4   r4   r5   rj     s    zcategory_values.cat_columnc                 C  s   | j jS r?   )r   ro   rm   r4   r4   r5   ro     s    zcategory_values.categoriesc                 C  s   | j |S r?   )r   rr   rp   r4   r4   r5   rr     s    zcategory_values.validatec                 C  s  | j ||}trt|tjrdd l}| jtjkr8d}n|| j j	j
dkrRtj}nd}||}| jtjkrttj||}n<ttjtdkr|| j j|d}n||| j |}|j||fddS | jtjkrttj||}n|| j j}tj||fddS d S )Nr   rK   rL   rM   axis)r   rg   rT   rP   rU   cupyr>   r.   r3   rJ   rZ   rX   r[   ra   rG   r   r\   r]   rO   stackrb   )rA   rc   r   ar   rf   br4   r4   r5   rg     s&    
zcategory_values.apply)r/   r0   r1   r2   rB   rF   rD   rj   ro   rr   rg   r   r4   r4   r   r5   r     s   

r   c                   @  s  e Zd ZdZd9ddddZedd Zd	d
ddZdd Zdd Z	edd Z
dd Zdd Zdd Zdd
ddZdd Zdd Zd:d!d"Zd#d$ Zd%d& Zd;d'd(Zd)d* Zed+d, Zed-d. Zed/d0 Zed1d2 Zed3d4 Zed5d6 Zed7d8 ZdS )<	Reductionz"Base class for per-bin reductions.Nr<   r=   c                 C  s   || _ d | _d S r?   )r>   _nan_check_columnr@   r4   r4   r5   rB   7  s    zReduction.__init__c                 C  s   | j d urt| j S d S d S r?   )r   rG   rC   r4   r4   r5   rE   ;  s    

zReduction.nan_check_columnr6   returnc                 C  s   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``.
        )r6   r8   rC   r4   r4   r5   uses_cuda_mutexB  s    zReduction.uses_cuda_mutexc                 C  s   dS )a  Return ``True`` if this Reduction uses a row index virtual column.

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

        Returning ``True`` from this function will cause a row index column to
        be created and passed to the ``append`` functions in the usual manner.
        Fr4   rA   r   partitionedr4   r4   r5   uses_row_indexL  s    zReduction.uses_row_indexc                 C  s@   | j tjkrd S | j |jvr$tdt|j| j  s<tdd S )Nrv   zinput must be numeric)r>   r.   r3   rw   rx   r   ru   rp   r4   r4   r5   rr   ]  s    zReduction.validatec                 C  s   t | jfS r?   rG   r>   rC   r4   r4   r5   rD   e  s    zReduction.inputsc                 C  s   dS )z?Return ``True`` if this is or contains a categorical reduction.Fr4   rC   r4   r4   r5   is_categoricali  s    zReduction.is_categoricalc                 C  s   dS )z]Return ``True`` if this is a ``where`` reduction or directly wraps
        a where reduction.Fr4   rC   r4   r4   r5   is_wherem  s    zReduction.is_wherec                 C  s   dS )NFr4   rC   r4   r4   r5   _antialias_requires_2_stagesr  s    z&Reduction._antialias_requires_2_stagestuple[AntialiasStage2]c                 C  s   t t|  dd S )Nz"._antialias_stage_2 is not defined)rl   typerA   self_intersectarray_moduler4   r4   r5   _antialias_stage_2x  s    zReduction._antialias_stage_2c                 C  s   | fS r?   r4   r   r4   r4   r5   _build_bases~  s    zReduction._build_basesc                 C  s   dS Nr4   r4   r   r4   r4   r5   _build_combine_temps  s    zReduction._build_combine_tempsFc                 C  s   dS r   r4   rA   r   r4   r4   r5   _build_temps  s    zReduction._build_tempsc                 C  s   t |jdd }|d ur>t|jjdkr>|jjd }t|d }t|trRt|j}|ttj	krf| j
S |ttjkrz| 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 d S )Nfieldsr   r   zUnexpected dshape )rR   ru   rS   r   r   rP   r   tyry   r   _create_boolfloat32_create_float32_nanfloat64_create_float64_nanrY   _create_int64r   _create_uint32rl   )rA   required_dshaper   Zfirst_fieldr4   r4   r5   _build_create  s"    

zReduction._build_createc                 C  sr   |r:|r| j d u r| jS |r"| jS | j d u r2| jS | jS n4|rN| j d u rN| jS |rX| jS | j d u rh| jS | jS d S r?   )	r>   _append_no_field_antialias_cuda_append_antialias_cuda_append_no_field_cuda_append_cuda_append_no_field_antialias_append_antialias_append_no_field_appendrA   r   Zschemar   	antialiasr   r4   r4   r5   _build_append  s    

zReduction._build_appendc                 C  s   | j S r?   )_combinerA   r   r   r   r   categoricalr4   r4   r5   _build_combine  s    zReduction._build_combinec                 C  s   | j S r?   )	_finalize)rA   r   r4   r4   r5   _build_finalize  s    zReduction._build_finalizec                 C  s   |j | ddS )NboolrI   zerosshaper   r4   r4   r5   r     s    zReduction._create_boolc                 C  s   |j | |jddS )NZf4rI   fullr[   r   r4   r4   r5   r     s    zReduction._create_float32_nanc                 C  s   |j | |jddS Nf8rI   r   r   r4   r4   r5   r     s    zReduction._create_float64_nanc                 C  s   |j | ddS r   )emptyr   r4   r4   r5   _create_float64_empty  s    zReduction._create_float64_emptyc                 C  s   |j | ddS r   r   r   r4   r4   r5   _create_float64_zero  s    zReduction._create_float64_zeroc                 C  s   |j | dddS )Nr   i8rI   )r   r   r4   r4   r5   r     s    zReduction._create_int64c                 C  s   |j | ddS )Nu4rI   r   r   r4   r4   r5   r     s    zReduction._create_uint32)N)F)F)r/   r0   r1   r2   rB   rF   rE   r   r   rr   rD   r   r   r   r   r   r   r   r   r   r   r   staticmethodr   r   r   r   r   r   r   r4   r4   r4   r5   r   5  sD   










r   c                      sH   e Zd ZdZd fdd	Zedd Z fddZedd
dZ	  Z
S )OptionalFieldReductionzOBase class for things like ``count`` or ``any`` for which the field is optionalNc                   s   t  | d S r?   )r~   rB   r@   r   r4   r5   rB     s    zOptionalFieldReduction.__init__c                 C  s   | j d urt| j fS dS r   )r>   rG   rC   r4   r4   r5   rD     s    zOptionalFieldReduction.inputsc                   s   | j d urt | d S r?   )r>   r~   rr   rp   r   r4   r5   rr     s    
zOptionalFieldReduction.validateFc                 K  s   t j| d fi |S Nr   rQ   	DataArraybasesr   kwargsr4   r4   r5   r     s    z OptionalFieldReduction._finalize)N)F)r/   r0   r1   r2   rB   rF   rD   rr   r   r   r   r4   r4   r   r5   r     s   
r   c                      sB   e Zd ZdZd fdd	Zdd Z fdd	Z fd
dZ  ZS )&SelfIntersectingOptionalFieldReductionz
    Base class for optional field reductions for which self-intersecting
    geometry may or may not be desirable.
    Ignored if not using antialiasing.
    NTc                   s   t  | || _d S r?   r~   rB   r   rA   r>   r   r   r4   r5   rB     s    z/SelfIntersectingOptionalFieldReduction.__init__c                 C  s   | j  S r?   r   rC   r4   r4   r5   r     s    zCSelfIntersectingOptionalFieldReduction._antialias_requires_2_stagesc                   sN   |r:|s:|r$| j d u r| jS | jS n| j d u r4| jS | jS t |||||S r?   )r>   2_append_no_field_antialias_cuda_not_self_intersectZ)_append_antialias_cuda_not_self_intersect-_append_no_field_antialias_not_self_intersect$_append_antialias_not_self_intersectr~   r   r   r   r4   r5   r     s    

z4SelfIntersectingOptionalFieldReduction._build_appendc                   s   t   | jf S r?   r~   r   r   rC   r   r4   r5   r     s    z7SelfIntersectingOptionalFieldReduction._hashable_inputs)NT	r/   r0   r1   r2   rB   r   r   r   r   r4   r4   r   r5   r     s
   r   c                   @  s   e Zd ZdZdd ZddddZeedd	 Zeed
d Z	eedd Z
eedd Zeedd Zeedd Zeejdddd Zeejdddd Zeejdddd Zeejdddd Zeejdddd Zd(d!d"Zed#d$ Zed%d& Zd'S ))counta  Count elements in each bin, returning the result as a uint32, or a
    float32 if using antialiasing.

    Parameters
    ----------
    column : str, optional
        If provided, only counts elements in ``column`` that are not ``NaN``.
        Otherwise, counts every element.
    c                 C  s   |rt tjS t tjS r?   )r   ry   r   r   rA   rq   r   r   r   r4   r4   r5   
out_dshape  s    zcount.out_dshaper   r   c                 C  s(   |rt tj|jfS t tj|jfS d S r?   r   r   SUM_1AGGr[   SUM_2AGGr   r4   r4   r5   r     s    zcount._antialias_stage_2c                 C  s$   t |s ||| f  d7  < dS dS Nr   r   r   r   xyaggfieldr4   r4   r5   r   %  s    zcount._appendc                 C  sJ   t |sFt ||| f r*|| ||| f< n||| f  || 7  < dS dS Nr   r   r   r   r   r  r  	aa_factorprev_aa_factorr4   r4   r5   r   -  s    zcount._append_antialiasc                 C  s<   t |s8t ||| f s(|||| f kr8|||| f< dS dS r  r   r  r4   r4   r5   r   8  s
     z*count._append_antialias_not_self_intersectc                 C  s   ||| f  d7  < dS r   r4   r   r   r  r4   r4   r5   r   A  s    zcount._append_no_fieldc                 C  s>   t ||| f r"|| ||| f< n||| f  || 7  < dS r   r   r   r   r  r  r  r4   r4   r5   r   G  s    z count._append_no_field_antialiasc                 C  s4   t ||| f s |||| f kr0|||| f< dS dS r  r   r  r4   r4   r5   r   P  s     z3count._append_no_field_antialias_not_self_intersectTZdevicec                 C  s8   || }t |s4t||| f|}t |s0||k r4dS dS r  r   r   r   r   r  r  r  r  valueoldr4   r4   r5   r   Y  s    zcount._append_antialias_cudac                 C  s0   t |s,t||| f|}t |s(||k r,dS dS r  r
  r   r   r  r  r  r  r4   r4   r5   r   c  s
    z8count._append_no_field_antialias_cuda_not_self_intersectc                 C  s$   t |s tj||| fd dS dS r   r   nb_cudaatomicaddr   r4   r4   r5   r   l  s    zcount._append_cudac                 C  s0   t |s,t||| f|}t |s(||k r,dS dS r  r
  r  r4   r4   r5   r   t  s
    z%count._append_no_field_antialias_cudac                 C  s   t j||| fd dS r   )r  r  r  r  r4   r4   r5   r   }  s    zcount._append_no_field_cudaFc                 C  s   |r
| j S | jS d S r?   _combine_antialiasr   r   r4   r4   r5   r     s    zcount._build_combinec                 C  s   | j dddS )Nr   r   r   rJ   sumaggsr4   r4   r5   r     s    zcount._combinec                 C  s.   | d }t dt| D ]}t|| |  q|S Nr   r   )r   rS   r#   r  retir4   r4   r5   r    s    zcount._combine_antialiasN)F)r/   r0   r1   r2   r   r   r   r    r   r   r   r   r   r   r  jitr   r   r   r   r   r   r   r  r4   r4   r4   r5   r     sR   		






r   c                   @  sF   e Zd ZdZdd ZddddZeedd	 Zeed
d Z	dS )_count_ignore_antialiasingzFCount reduction but ignores antialiasing. Used by mean reduction.
    c                 C  s
   t tjS r?   )r   ry   r   r   r4   r4   r5   r     s    z%_count_ignore_antialiasing.out_dshaper   r   c                 C  s$   |rt tjdfS t tjdfS d S r   r   r   r   r   r   r4   r4   r5   r     s    z-_count_ignore_antialiasing._antialias_stage_2c                 C  s,   t |s(|dkr(||| f  d7  < dS dS Ng        r   r   r   r   r  r4   r4   r5   r     s    z,_count_ignore_antialiasing._append_antialiasc                 C  s,   t |s(|dkr(||| f  d7  < dS dS r!  r   r  r4   r4   r5   r     s    z?_count_ignore_antialiasing._append_antialias_not_self_intersectN)
r/   r0   r1   r2   r   r   r   r    r   r   r4   r4   r4   r5   r    s   r  c                      s   e Zd ZdZe f fdd	Zdd Zd0ddZed	d
 Z	edd Z
dd Zdd Zedd Zdd Zdd Zedd ZddddZdd Zdd  Zd!dd"d#Zd$d% Zd&d' Zd(d) Zd1d*d+Zd,d- Zd.d/ Z  ZS )2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                   s   t    t|tr|| _nt|tr2t|| _ntd| jj| _| jjf| _	t
|dd  }d ur~|  j	|d d d 7  _	n|  j	t
|dd f7  _	|| _| jd urt| j| j| _n| j| _d S )NzEfirst argument must be a column name or a CategoryPreprocess instancecolumnsr   r>   )r~   rB   rP   rh   r   strrt   	TypeErrorr>   r#  rR   	reduction
val_columnr   
preprocess)rA   rj   r&  r#  r   r4   r5   rB     s    




zby.__init__c                 C  s    t t| |  | j | jfS r?   )hashr   r   r   r&  rC   r4   r4   r5   __hash__  s    zby.__hash__Fc                   s   t  fdd j|D S )Nc                 3  s   | ]}t  j|V  qd S r?   r"  r   ).0tmprC   r4   r5   	<genexpr>      z"by._build_temps.<locals>.<genexpr>)tupler&  r   r   r4   rC   r5   r     s    zby._build_tempsc                 C  s
   | j d S r   r#  rC   r4   r4   r5   rj     s    zby.cat_columnc                 C  s
   | j d S Nr   r1  rC   r4   r4   r5   r'    s    zby.val_columnc                 C  s   | j | | j| d S r?   )r(  rr   r&  rp   r4   r4   r5   rr     s    zby.validatec                   s8   | j |}| j|||| tt fdd|D S )Nc                   s   g | ]}| fqS r4   r4   )r,  cZ	red_shaper4   r5   
<listcomp>  r/  z!by.out_dshape.<locals>.<listcomp>)r   ro   r&  r   r   r   )rA   rn   r   r   r   catsr4   r4  r5   r     s    zby.out_dshapec                 C  s   | j fS r?   )r(  rC   r4   r4   r5   rD     s    z	by.inputsc                 C  s   dS NTr4   rC   r4   r4   r5   r     s    zby.is_categoricalc                 C  s
   | j  S r?   )r&  r   rC   r4   r4   r5   r     s    zby.is_wherec                 C  s   | j jS r?   )r&  rE   rC   r4   r4   r5   rE     s    zby.nan_check_columnr6   r   c                 C  s
   | j  S r?   )r&  r   rC   r4   r4   r5   r     s    zby.uses_cuda_mutexc                 C  s   | j ||S r?   )r&  r   r   r4   r4   r5   r     s    zby.uses_row_indexc                 C  s
   | j  S r?   )r&  r   rC   r4   r4   r5   r     s    zby._antialias_requires_2_stagesr   c                 C  s2   | j ||}t|d j|d j|d jddfS )Nr   T)combinationzeron_reductionr   )r&  r   r   r8  r9  r:  rA   r   r   r  r4   r4   r5   r     s    
zby._antialias_stage_2c                   s   t jj  fddS )Nc                   s   j |  f |S r?   )r&  r   r   Zn_catsr   rA   r4   r5   <lambda>  s   
z"by._build_create.<locals>.<lambda>)rS   ru   r   rA   r   r4   r<  r5   r     s    zby._build_createc                   s@    j ||}t|dkr*|d  u r*|S t fdd|D S )Nr   r   c                 3  s   | ]}t  j|V  qd S r?   r+  )r,  baserC   r4   r5   r.    r/  z"by._build_bases.<locals>.<genexpr>)r&  r   rS   r0  )rA   r   r   r   r4   rC   r5   r     s    zby._build_basesc                 C  s   | j |||||S r?   )r&  r   r   r4   r4   r5   r     s    zby._build_appendc                 C  s   | j ||||dS r7  )r&  r   r   r4   r4   r5   r     s    zby._build_combinec                 C  s   | j ||S r?   )r&  r   r   r4   r4   r5   r      s    zby._build_combine_tempsc                   s&   t j d fdd	}|S )NFc                   sH   t |}|d  jg7  <  |d j< j| fd|i|S )Ndimscoordsr   )copydeepcopyrj   r&  r   r   r6  r   rA   r4   r5   finalize&  s    
z$by._build_finalize.<locals>.finalize)F)r   r   ro   rA   r   rE  r4   rD  r5   r   #  s    zby._build_finalize)F)F)r/   r0   r1   r2   r   rB   r*  r   rF   rj   r'  rr   r   rD   r   r   rE   r   r   r   r   r   r   r   r   r   r   r   r4   r4   r   r5   r"    s4   






r"  c                   @  s   e Zd ZdZdd ZddddZeedd	 Zeed
d Z	eedd Z
eedd ZeZe
ZdddZedd Zedd ZdS )anyzWhether any elements in ``column`` map to each bin.

    Parameters
    ----------
    column : str, optional
        If provided, any elements in ``column`` that are ``NaN`` are skipped.
    c                 C  s   |rt tjS t tjS r?   )r   ry   r   r   r   r4   r4   r5   r   8  s    zany.out_dshaper   r   c                 C  s   t tj|jfS r?   r   r   MAXr[   r   r4   r4   r5   r   ;  s    zany._antialias_stage_2c                 C  s   t |sd||| f< dS dS )NTr   r   r   r   r4   r4   r5   r   ?  s    zany._appendc                 C  s<   t |s8t ||| f s(|||| f kr8|||| f< dS dS r  r   r  r4   r4   r5   r   G  s
     zany._append_antialiasc                 C  s   d||| f< dS )NTr   r4   r  r4   r4   r5   r   P  s    zany._append_no_fieldc                 C  s4   t ||| f s |||| f kr0|||| f< dS dS r  r   r  r4   r4   r5   r   V  s     zany._append_no_field_antialiasFc                 C  s   |r
| j S | jS d S r?   r  r   r4   r4   r5   r   b  s    zany._build_combinec                 C  s   | j dddS )Nr   r   r  r  r  r4   r4   r5   r   h  s    zany._combinec                 C  s.   | d }t dt| D ]}t|| |  q|S r  )r   rS   r"   r  r4   r4   r5   r  l  s    zany._combine_antialiasN)F)r/   r0   r1   r2   r   r   r   r    r   r   r   r   r   r   r   r   r  r4   r4   r4   r5   rG  0  s,   

rG  c                   @  sn   e Zd ZdZdd ZedddZedd Zd	d
 Z	ee
dd Zeejdddd Zedd ZdS )	_upsamplez+"Special internal class used for upsamplingc                 C  s   t ttjS r?   r   r   ry   r   r   r4   r4   r5   r   v  s    z_upsample.out_dshapeFc                 K  s   t j| d fi |S r   r   r   r4   r4   r5   r   y  s    z_upsample._finalizec                 C  s   t | jfS r?   r   rC   r4   r4   r5   rD   }  s    z_upsample.inputsc                 C  s   | j S r?   )r   r>  r4   r4   r5   r     s    z_upsample._build_createc                 C  s   d S r?   r4   r   r4   r4   r5   r     s    z_upsample._appendTr	  c                 C  s   d S r?   r4   r   r4   r4   r5   r     s    z_upsample._append_cudac                 C  s   t j| ddS Nr   r   rX   Znanmaxr  r4   r4   r5   r     s    z_upsample._combineN)F)r/   r0   r1   r2   r   r   r   rF   rD   r   r    r   r  r  r   r   r4   r4   r4   r5   rJ  t  s   

rJ  c                   @  s&   e Zd ZdZdd ZedddZdS )	FloatingReductionzBBase classes for reductions that always have floating-point dtype.c                 C  s   t ttjS r?   rK  r   r4   r4   r5   r     s    zFloatingReduction.out_dshapeFc                 K  s   t j| d fi |S r   r   r   r4   r4   r5   r     s    zFloatingReduction._finalizeN)F)r/   r0   r1   r2   r   r   r   r4   r4   r4   r5   rN    s   rN  c                   @  sz   e Zd ZdZddddZdd Zeedd	 Zeed
d Z	eedd Z
eejdddd Zedd ZdS )	_sum_zerozSum of all elements in ``column``.

    Parameters
    ----------
    column : str
        Name of the column to aggregate over. Column data type must be numeric.
    r   r   c                 C  s$   |rt tjdfS t tjdfS d S r   r   r   r4   r4   r5   r     s    z_sum_zero._antialias_stage_2c                 C  s   | j S r?   r   r>  r4   r4   r5   r     s    z_sum_zero._build_createc                 C  s$   t |s ||| f  |7  < dS dS r  r   r   r4   r4   r5   r     s    z_sum_zero._appendc                 C  s0   |||  }t |s,||| f  |7  < dS dS r  r   r   r   r  r  r  r  r  r4   r4   r5   r     s
    z_sum_zero._append_antialiasc                 C  s4   || }t |s0|||| f kr0|||| f< dS dS r  r   rQ  r4   r4   r5   r     s
    z._sum_zero._append_antialias_not_self_intersectTr	  c                 C  s$   t |s tj||| f| dS dS r  r  r   r4   r4   r5   r     s    z_sum_zero._append_cudac                 C  s   | j dddS )Nr   r   r  r  r  r4   r4   r5   r     s    z_sum_zero._combineN)r/   r0   r1   r2   r   r   r   r    r   r   r   r  r  r   r   r4   r4   r4   r5   rO    s"   	
rO  c                      sB   e Zd ZdZd fdd	Zdd Z fdd	Z fd
dZ  ZS )!SelfIntersectingFloatingReductionz
    Base class for floating reductions for which self-intersecting geometry
    may or may not be desirable.
    Ignored if not using antialiasing.
    NTc                   s   t  | || _d S r?   r   r   r   r4   r5   rB     s    z*SelfIntersectingFloatingReduction.__init__c                 C  s   | j  S r?   r   rC   r4   r4   r5   r     s    z>SelfIntersectingFloatingReduction._antialias_requires_2_stagesc                   s@   |r,|s,|rt dn| jd u r&| jS | jS t |||||S )Nr   )rl   r>   r   r   r~   r   r   r   r4   r5   r     s    

z/SelfIntersectingFloatingReduction._build_appendc                   s   t   | jf S r?   r   rC   r   r4   r5   r     s    z2SelfIntersectingFloatingReduction._hashable_inputs)NTr   r4   r4   r   r5   rR    s
   rR  c                   @  sp   e Zd ZdZddddZdd Zeedd	 Zeed
d Z	eedd Z
edd ZedddZdS )r  a  Sum of all elements in ``column``.

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

    Parameters
    ----------
    column : str
        Name of the column to aggregate over. Column data type must be numeric.
        ``NaN`` values in the column are skipped.
    r   r   c                 C  s(   |rt tj|jfS t tj|jfS d S r?   r   r   r4   r4   r5   r     s    zsum._antialias_stage_2c                 C  s"   |rt | jt| jfS | fS d S r?   )rO  r>   rG  r   r4   r4   r5   r     s    zsum._build_basesc                 C  sB   t |s>t ||| f r&|||| f< n||| f  |7  < dS dS r  r   r   r4   r4   r5   r     s    zsum._appendc                 C  sN   |||  }t |sJt ||| f r2|||| f< n||| f  |7  < dS dS r  r   rQ  r4   r4   r5   r      s    zsum._append_antialiasc                 C  sD   || }t |s@t ||| f s0|||| f kr@|||| f< dS dS r  r   rQ  r4   r4   r5   r   ,  s     z(sum._append_antialias_not_self_intersectc                 C  s   t | ddS rL  )r!   r  r4   r4   r5   r   6  s    zsum._combineFc                 K  sH   |r.| \}}t ||t j}tj|fi |S tj| d fi |S d S r   )rX   wherer[   rQ   r   )r   r   r   sumsZanysr   r4   r4   r5   r   :  s
    zsum._finalizeN)F)r/   r0   r1   r2   r   r   r   r    r   r   r   r   r   r4   r4   r4   r5   r    s    
	

r  c                      st   e Zd ZdZddddZ fddZdd	 ZdddZee	dd Z
eejdddd Zedd Z  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.
    r6   r   c                 C  s   t jS r?   )r6   r:   rC   r4   r4   r5   r   P  s    zm2.uses_cuda_mutexc                   s   t t| |||||S r?   )r~   rU  r   r   r   r4   r5   r   S  s    zm2._build_appendc                 C  s   | j S r?   rP  r>  r4   r4   r5   r   V  s    zm2._build_createFc                 C  s   t | jt| jfS r?   )rO  r>   r   r   r4   r4   r5   r   Y  s    zm2._build_tempsc                 C  s\   t |sX|dkrXt|| }t|| |d  }||| f  || ||  7  < dS dS Nr   r   r   r   rX   r   r   r   rU  r  r  r   u1ur4   r4   r5   r   ]  s     z
m2._appendTr	  c                 C  s\   t |sX|dkrXt|| }t|| |d  }||| f  || ||  7  < dS dS rV  rW  rX  r4   r4   r5   r   k  s     zm2._append_cudac                 C  sn   t jdddL t j|dd|jdd }t j| ||| | d   ddW  d    S 1 s`0    Y  d S )Nignoredivideinvalidr   r   r7   )rX   errstateZnansumr  )ZMsrT  nsmur4   r4   r5   r   x  s    zm2._combine)F)r/   r0   r1   r2   r   r   r   r   r   r    r   r  r  r   r   r   r4   r4   r   r5   rU  D  s   

rU  c                   @  sj   e Zd ZdZdd ZddddZeedd	 Zeed
d Z	ee
jdddd Zedd ZdS )minzMinimum value of all elements in ``column``.

    Parameters
    ----------
    column : str
        Name of the column to aggregate over. Column data type must be numeric.
        ``NaN`` values in the column are skipped.
    c                 C  s   dS r7  r4   rC   r4   r4   r5   r     s    z min._antialias_requires_2_stagesr   r   c                 C  s   t tj|jfS r?   r   r   MINr[   r   r4   r4   r5   r     s    zmin._antialias_stage_2c                 C  s<   t |s8t ||| f s(||| f |kr8|||| f< dS dS r  r   r   r4   r4   r5   r     s    (zmin._appendc                 C  sD   || }t |s@t ||| f s0|||| f kr@|||| f< dS dS r  r   rQ  r4   r4   r5   r     s
    (zmin._append_antialiasTr	  c                 C  s0   t |s,t||| f|}t |s(||kr,dS dS r  )r   r   r   r   r  r  r  r4   r4   r5   r     s
    zmin._append_cudac                 C  s   t j| ddS rL  )rX   Znanminr  r4   r4   r5   r     s    zmin._combineN)r/   r0   r1   r2   r   r   r   r    r   r   r  r  r   r   r4   r4   r4   r5   rb    s   
rb  c                   @  sz   e Zd ZdZddddZeedd Zeedd	 Zee	j
d
ddd Zee	j
d
ddd Zedd ZdS )maxzMaximum value of all elements in ``column``.

    Parameters
    ----------
    column : str
        Name of the column to aggregate over. Column data type must be numeric.
        ``NaN`` values in the column are skipped.
    r   r   c                 C  s   t tj|jfS r?   rH  r   r4   r4   r5   r     s    zmax._antialias_stage_2c                 C  s<   t |s8t ||| f s(||| f |k r8|||| f< dS dS r  r   r   r4   r4   r5   r     s    (zmax._appendc                 C  sD   || }t |s@t ||| f s0|||| f kr@|||| f< dS dS r  r   rQ  r4   r4   r5   r     s
    (zmax._append_antialiasTr	  c                 C  s8   || }t |s4t||| f|}t |s0||k r4dS dS r  r
  r  r4   r4   r5   r     s    zmax._append_antialias_cudac                 C  s0   t |s,t||| f|}t |s(||k r,dS dS r  r
  re  r4   r4   r5   r     s
    zmax._append_cudac                 C  s   t j| ddS rL  rM  r  r4   r4   r5   r     s    zmax._combineN)r/   r0   r1   r2   r   r   r    r   r   r  r  r   r   r   r4   r4   r4   r5   rf    s    

rf  c                      s    e Zd ZdZ fddZ  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                   s   t t| |t  d S r?   )r~   rg  rB   r   r@   r   r4   r5   rB     s    zcount_cat.__init__)r/   r0   r1   r2   rB   r   r4   r4   r   r5   rg    s   
rg  c                   @  s&   e Zd ZdZdd ZedddZdS )	meanzMean of all elements in ``column``.

    Parameters
    ----------
    column : str
        Name of the column to aggregate over. Column data type must be numeric.
        ``NaN`` values in the column are skipped.
    c                 C  s   t | jt| jfS r?   )rO  r>   r  r   r4   r4   r5   r     s    zmean._build_basesFc                 K  s`   | \}}t jddd( t |dk|| t j}W d    n1 sD0    Y  tj|fi |S Nr[  r\  r   rX   r_  rS  r[   rQ   r   )r   r   r   rT  countsr   r4   r4   r5   r     s    6zmean._finalizeN)Fr/   r0   r1   r2   r   r   r   r4   r4   r4   r5   rh    s   rh  c                   @  s&   e Zd ZdZdd ZedddZdS )	varzVariance of all elements in ``column``.

    Parameters
    ----------
    column : str
        Name of the column to aggregate over. Column data type must be numeric.
        ``NaN`` values in the column are skipped.
    c                 C  s   t | jt| jt| jfS r?   rO  r>   r   rU  r   r4   r4   r5   r     s    zvar._build_basesFc                 K  sb   | \}}}t jddd( t |dk|| t j}W d    n1 sF0    Y  tj|fi |S ri  rj  r   r   r   rT  rk  Zm2sr   r4   r4   r5   r     s    
6zvar._finalizeN)Frl  r4   r4   r4   r5   rm  	  s   rm  c                   @  s&   e Zd ZdZdd ZedddZdS )	stdzStandard Deviation of all elements in ``column``.

    Parameters
    ----------
    column : str
        Name of the column to aggregate over. Column data type must be numeric.
        ``NaN`` values in the column are skipped.
    c                 C  s   t | jt| jt| jfS r?   rn  r   r4   r4   r5   r   &  s    zstd._build_basesFc                 K  sh   | \}}}t jddd. t |dkt || t j}W d    n1 sL0    Y  tj|fi |S ri  )rX   r_  rS  sqrtr[   rQ   r   ro  r4   r4   r5   r   )  s    
<zstd._finalizeN)Frl  r4   r4   r4   r5   rp    s   rp  c                      sZ   e Zd ZdZdd Zdd Zdd Z fdd	Zed
d Z	dd Z
edddZ  ZS )_first_or_lastz6Abstract base class of first and last reductions.
    c                 C  s
   t tjS r?   r   ry   r   r   r4   r4   r5   r   4  s    z_first_or_last.out_dshapec                 C  s   |p|S r?   r4   r   r4   r4   r5   r   7  s    z_first_or_last.uses_row_indexc                 C  s   dS r7  r4   rC   r4   r4   r5   r   :  s    z+_first_or_last._antialias_requires_2_stagesc                   sN   |  ||r<|  }t|| jd}| j|_||||f S t ||S d S Nselectorlookup_columnr   _create_row_index_selectorrS  r>   r   r   r~   rA   r   r   row_index_selectorwrapperr   r4   r5   r   =  s    z_first_or_last._build_basesc                 C  s   t | dkrtd| d S Nr   z6_combine should never be called with more than one aggr   rS   RuntimeErrorr  r4   r4   r5   r   G  s    z_first_or_last._combinec                 C  s   d S r?   r4   rC   r4   r4   r5   ry  O  s    z)_first_or_last._create_row_index_selectorFc                 K  s   t j| d fi |S Nr   r   r   r4   r4   r5   r   R  s    z_first_or_last._finalize)F)r/   r0   r1   r2   r   r   r   r   r   r   ry  r   r   r4   r4   r   r5   rr  1  s   

rr  c                   @  sF   e Zd ZdZddddZeedd Zeedd	 Zd
d Z	dS )firsta  First value encountered in ``column``.

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

    Currently only supported for rasters, externally to this class.

    Parameters
    ----------
    column : str
        Name of the column to aggregate over. If the data type is floating point,
        ``NaN`` values in the column are skipped.
    r   r   c                 C  s   t tj|jfS r?   r   r   ZFIRSTr[   r   r4   r4   r5   r   h  s    zfirst._antialias_stage_2c                 C  s,   t |s(t ||| f r(|||| f< dS dS r  r   r   r4   r4   r5   r   k  s    zfirst._appendc                 C  sD   || }t |s@t ||| f s0|||| f kr@|||| f< dS dS r  r   rQ  r4   r4   r5   r   s  s
    (zfirst._append_antialiasc                 C  s   t  S r?   )_min_row_indexrC   r4   r4   r5   ry  |  s    z first._create_row_index_selectorN
r/   r0   r1   r2   r   r   r    r   r   ry  r4   r4   r4   r5   r  Z  s   r  c                   @  sF   e Zd ZdZddddZeedd Zeedd	 Zd
d Z	dS )lasta  Last value encountered in ``column``.

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

    Currently only supported for rasters, externally to this class.

    Parameters
    ----------
    column : str
        Name of the column to aggregate over. If the data type is floating point,
        ``NaN`` values in the column are skipped.
    r   r   c                 C  s   t tj|jfS r?   r   r   ZLASTr[   r   r4   r4   r5   r     s    zlast._antialias_stage_2c                 C  s   t |s|||| f< dS dS r  r   r   r4   r4   r5   r     s    zlast._appendc                 C  sD   || }t |s@t ||| f s0|||| f kr@|||| f< dS dS r  r   rQ  r4   r4   r5   r     s
    (zlast._append_antialiasc                 C  s   t  S r?   )_max_row_indexrC   r4   r4   r5   ry    s    zlast._create_row_index_selectorNr  r4   r4   r4   r5   r    s   r  c                      sN   e Zd Zd fdd	Zdd Zdd Z fd	d
Zdd Z fddZ  Z	S )FloatingNReductionNr   c                   s"   t  | |dkr|nd| _d S r2  )r~   rB   n)rA   r>   r  r   r4   r5   rB     s    zFloatingNReduction.__init__c                 C  s
   t tjS r?   rs  r   r4   r4   r5   r     s    zFloatingNReduction.out_dshapec                 K  s<   d}t | j}t|}|d  |g7  < ||d |< |S )Nr  r@  rA  )rX   rW   r  rB  rC  )rA   r   Zn_namen_valuesr4   r4   r5   _add_finalize_kwargs  s    
z'FloatingNReduction._add_finalize_kwargsc                   s    fddS )Nc                   s   t t| jf |S r?   )r~   r  r   r  r   r   r   rA   r4   r5   r=    s   
z2FloatingNReduction._build_create.<locals>.<lambda>r4   r>  r   r   rA   r5   r     s    z FloatingNReduction._build_createc                   s   d fdd	}|S )NFc                   s&    j f i |} j| fd|i|S )Nr   )r  r   r   rC   r4   r5   rE    s    z4FloatingNReduction._build_finalize.<locals>.finalize)Fr4   rF  r4   rC   r5   r     s    z"FloatingNReduction._build_finalizec                   s   t   | jf S r?   )r~   r   r  rC   r   r4   r5   r     s    z#FloatingNReduction._hashable_inputs)Nr   )
r/   r0   r1   rB   r   r  r   r   r   r   r4   r4   r   r5   r    s   r  c                      sR   e Zd ZdZdd Zdd Z fddZedd	 Zd
d Z	edddZ
  ZS )_first_n_or_last_nz:Abstract base class of first_n and last_n reductions.
    c                 C  s   |p|S r?   r4   r   r4   r4   r5   r     s    z!_first_n_or_last_n.uses_row_indexc                 C  s   dS r7  r4   rC   r4   r4   r5   r     s    z/_first_n_or_last_n._antialias_requires_2_stagesc                   sN   |  ||r<|  }t|| jd}| j|_||||f S t ||S d S rt  rx  rz  r   r4   r5   r     s    z_first_n_or_last_n._build_basesc                 C  s   t | dkrtd| d S r}  r~  r  r4   r4   r5   r     s    z_first_n_or_last_n._combinec                 C  s   d S r?   r4   rC   r4   r4   r5   ry    s    z-_first_n_or_last_n._create_row_index_selectorFc                 K  s   t j| d fi |S r  r   r   r4   r4   r5   r     s    z_first_n_or_last_n._finalize)F)r/   r0   r1   r2   r   r   r   r   r   ry  r   r   r4   r4   r   r5   r    s   

r  c                   @  sB   e Zd ZddddZeedd Zeedd Zd	d
 ZdS )first_nr   r   c                 C  s   t tj|jddfS NTr:  r  r   r4   r4   r5   r     s    zfirst_n._antialias_stage_2c                 C  sf   t |sb|jd }t ||| |d f s,dS t|D ],}t ||| |f r4|||| |f< |  S q4dS Nr7   r   r   r   r   r   r   r   r  r  r  r  r4   r4   r5   r     s    

zfirst_n._appendc           	      C  sn   || }t |sj|jd }t ||| |d f s4dS t|D ],}t ||| |f r<|||| |f< |  S q<dS r  r  	r   r   r  r  r  r  r  r  r  r4   r4   r5   r     s    

zfirst_n._append_antialiasc                 C  s   t | jdS N)r  )_min_n_row_indexr  rC   r4   r4   r5   ry    s    z"first_n._create_row_index_selectorN	r/   r0   r1   r   r   r    r   r   ry  r4   r4   r4   r5   r    s   r  c                   @  sB   e Zd ZddddZeedd Zeedd Zd	d
 ZdS )last_nr   r   c                 C  s   t tj|jddfS r  r  r   r4   r4   r5   r     s    zlast_n._antialias_stage_2c                 C  s$   t |s t||| f |d dS dS r  r   r-   r   r4   r4   r5   r   "  s    zlast_n._appendc                 C  s,   || }t |s(t||| f |d dS dS r  r  rQ  r4   r4   r5   r   +  s
    zlast_n._append_antialiasc                 C  s   t | jdS r  )_max_n_row_indexr  rC   r4   r4   r5   ry  5  s    z!last_n._create_row_index_selectorNr  r4   r4   r4   r5   r    s   r  c                   @  s   e Zd ZddddZddddZeedd	 Zeed
d Zee	j
dddd ZdddZedd Zedd ZdS )max_nr6   r   c                 C  s   t jS r?   r6   r9   rC   r4   r4   r5   r   :  s    zmax_n.uses_cuda_mutexr   c                 C  s   t tj|jddfS r  rH  r   r4   r4   r5   r   =  s    zmax_n._antialias_stage_2c                 C  sd   t |s`|jd }t|D ]D}t ||| |f sB|||| |f krt||| f || |  S qdS Nr7   r   r   r   r   r-   r  r4   r4   r5   r   A  s    
$
zmax_n._appendc           	      C  sl   || }t |sh|jd }t|D ]D}t ||| |f sJ|||| |f kr"t||| f || |  S q"dS r  r  r  r4   r4   r5   r   N  s    
$
zmax_n._append_antialiasTr	  c                 C  sd   t |s`|jd }t|D ]D}t ||| |f sB|||| |f krt||| f || |  S qdS r  r   r   r   r   r  r4   r4   r5   r   ]  s    
$
zmax_n._append_cudaFc                 C  s   |r
| j S | jS d S r?   _combine_cudar   r   r4   r4   r5   r   j  s    zmax_n._build_combinec                 C  sP   | d }t dt| D ]4}|jdkr8t| d | |  qt| d | |  q|S Nr   r      )r   rS   ndimr&   r%   r  r4   r4   r5   r   p  s    
zmax_n._combinec                 C  sj   | d }t |jd d }tdt| D ]<}|jdkrNt| | d | |  q(t| | d | |  q(|S Nr   r   r   r  )r   r   r   rS   r  r   r   r  r  kernel_argsr  r4   r4   r5   r  z  s    
zmax_n._combine_cudaN)F)r/   r0   r1   r   r   r   r    r   r   r  r  r   r   r   r  r4   r4   r4   r5   r  9  s    


	r  c                   @  s   e Zd ZddddZdd Zdddd	Zeed
d Zeedd Z	ee
jdddd ZdddZedd Zedd ZdS )min_nr6   r   c                 C  s   t jS r?   r  rC   r4   r4   r5   r     s    zmin_n.uses_cuda_mutexc                 C  s   dS r7  r4   rC   r4   r4   r5   r     s    z"min_n._antialias_requires_2_stagesr   c                 C  s   t tj|jddfS r  rc  r   r4   r4   r5   r     s    zmin_n._antialias_stage_2c                 C  sd   t |s`|jd }t|D ]D}t ||| |f sB|||| |f k rt||| f || |  S qdS r  r  r  r4   r4   r5   r     s    
$
zmin_n._appendc           	      C  sl   || }t |sh|jd }t|D ]D}t ||| |f sJ|||| |f k r"t||| f || |  S q"dS r  r  r  r4   r4   r5   r     s    
$
zmin_n._append_antialiasTr	  c                 C  sd   t |s`|jd }t|D ]D}t ||| |f sB|||| |f k rt||| f || |  S qdS r  r  r  r4   r4   r5   r     s    
$
zmin_n._append_cudaFc                 C  s   |r
| j S | jS d S r?   r  r   r4   r4   r5   r     s    zmin_n._build_combinec                 C  sP   | d }t dt| D ]4}|jdkr8t| d | |  qt| d | |  q|S r  )r   rS   r  r(   r'   r  r4   r4   r5   r     s    
zmin_n._combinec                 C  sj   | d }t |jd d }tdt| D ]<}|jdkrNt| | d | |  q(t| | d | |  q(|S r  )r   r   r   rS   r  r   r   r  r4   r4   r5   r    s    
zmin_n._combine_cudaN)F)r/   r0   r1   r   r   r   r   r    r   r   r  r  r   r   r   r  r4   r4   r4   r5   r    s"   


	r  c                   @  s<   e Zd ZdZdd Zedd Zedd Zedd	 Zd
S )modeat  Mode (most common value) of all the values encountered in ``column``.

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

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

    Parameters
    ----------
    column : str
        Name of the column to aggregate over. If the data type is floating point,
        ``NaN`` values in the column are skipped.
    c                 C  s   t ttjS r?   rK  r   r4   r4   r5   r     s    zmode.out_dshapec                 C  s   t dd S Nz.mode is currently implemented only for rastersrk   r  r4   r4   r5   r     s    zmode._appendc                 C  s   t dd S r  rk   r  r4   r4   r5   r     s    zmode._combinec                 K  s   t dd S r  rk   )r   r   r4   r4   r5   r     s    zmode._finalizeN)	r/   r0   r1   r2   r   r   r   r   r   r4   r4   r4   r5   r    s   

r  c                      s  e Zd ZdZd2ddd fddZdd	 Zd
d Zdd ZddddZdd Z	 fddZ
ddddZeedd Zeedd Zeejdddd  Zeejddd!d" Zd#d$ Z fd%d&Zd'd( Zd3d*d+Zd,d- Z fd.d/Zd0d1 Z  ZS )4rS  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.
    Nr   z
str | Noneru  c                   sV   t |ttttttttt	t
f
s&td|d u r4tj}t | || _|j|f| _d S )NzWselector can only be a first, first_n, last, last_n, max, max_n, min or min_n reduction)rP   r  r  r  r  rf  r  rb  r  _max_or_min_row_index_max_n_or_min_n_row_indexr%  r.   r3   r~   rB   rv  r>   r#  )rA   rv  rw  r   r4   r5   rB     s    zwhere.__init__c                 C  s   t t| |  | jfS r?   )r)  r   r   rv  rC   r4   r4   r5   r*    s    zwhere.__hash__c                 C  s   dS r7  r4   rC   r4   r4   r5   r   !  s    zwhere.is_wherec                 C  s$   | j tjkrttjS ttjS d S r?   )r>   r.   r3   r   ry   rY   r   )rA   rn   r   r   r   r4   r4   r5   r   $  s    
zwhere.out_dshaper6   r   c                 C  s   t jS r?   r  rC   r4   r4   r5   r   *  s    zwhere.uses_cuda_mutexc                 C  s   | j tjkp| j||S r?   )r>   r.   r3   rv  r   r   r4   r4   r5   r   -  s    zwhere.uses_row_indexc                   sJ   | j tjkrt | | j| | j tjkrF| j | jj krFtdd S )Nz<where and its contained reduction cannot use the same column)r>   r.   r3   r~   rr   rv  rx   rp   r   r4   r5   rr   1  s
    zwhere.validater   c                 C  s:   | j ||}| jtjkr6t|d jd|d jdf}|S )Nr   r   )r8  r9  r:  )rv  r   r>   r.   r3   r   r8  r:  r;  r4   r4   r5   r   8  s    
zwhere._antialias_stage_2c                 C  s0   |j dkr t||| f || n|||| f< |S Nr7   r  r-   r   r   r  r  update_indexr4   r4   r5   r   F  s    
zwhere._appendc                 C  s0   |j dkr t||| f || n|||| f< d S r  r  r   r   r  r  r  r  r  r4   r4   r5   r   O  s    
zwhere._append_antialiasTr	  c                 C  s0   |j dkr t||| f || n|||| f< |S r  r  r   r  r4   r4   r5   r   X  s    
zwhere._append_antialias_cudac                 C  s0   |j dkr t||| f || n|||| f< |S r  r  r  r4   r4   r5   r   b  s    
zwhere._append_cudac                 C  s*   |r|r| j S | jS n|r | jS | jS d S r?   )r   r   r   r   r   r4   r4   r5   r   k  s    zwhere._build_appendc                   s   | j }t|ttfrz|||rz| }| jtjkrJ| j j|_	|
||S t|| j}| j j|_	|
|||
|| S n|
||t 
|| S d S r?   )rv  rP   rr  r  r   ry  r>   r.   r3   r   r   rS  r~   )rA   r   r   rv  r{  Z	new_wherer   r4   r5   r   y  s     





zwhere._build_basesc                   s  | j }t|t}|r|j n|j | j ||r4tntt fdd}t fdd}t fdd}t fdd}	t	j
 fd	d
}
t	j
 fdd}t	j
 fdd}t	j
 fdd}|r|r|r|S |S |r|	S |S n"|r|r|S |
S |r|S |S d S )Nc                   sx   | d j \}}t|D ]\}t|D ]N}|d ||f }|s" |||d |dkr"| d ||f | d ||f< q"qd S r  r   r   )r  selector_aggsnynxr   r   r  appendr^  r4   r5   combine_cpu_2d  s    z/where._combine_callback.<locals>.combine_cpu_2dc           	   
     s   | d j \}}}t|D ]}t|D ]t}t|D ]f}|d |||f }|s0 |||d d d d d |f |dkr0| d |||f | d |||f< q0q$qd S r  r  )	r  r  r  r  ncatr   r   r{   r  r  r4   r5   combine_cpu_3d  s    &z/where._combine_callback.<locals>.combine_cpu_3dc           
   	     s   | d j \}}}t|D ]}t|D ]t}t|D ]f}|d |||f }|rR q$ |||d |}	|	dk rp q$t| d ||f | d |||f |	 q0q$qd S r  r   r   r-   )
r  r  r  r  r  r   r   r  r  r  r  r4   r5   combine_cpu_n_3d  s    z1where._combine_callback.<locals>.combine_cpu_n_3dc                   s   | d j \}}}}t|D ]}t|D ]}t|D ]}t|D ]}	|d ||||	f }
|
rb q2 |||d d d d d |d d f |
}|dk r q2t| d |||f | d ||||	f | q>q2q&qd S r  r  )r  r  r  r  r  r  r   r   r{   r  r  r  r  r4   r5   combine_cpu_n_4d  s    *$z1where._combine_callback.<locals>.combine_cpu_n_4dc                   sz   | d j \}}td\}}||k rv||k rv|d ||f }|sv |||d |dkrv| d ||f | d ||f< d S Nr   r7   r   r   r  grid)r  r  r  r  r   r   r  r  r4   r5   combine_cuda_2d  s    z0where._combine_callback.<locals>.combine_cuda_2dc           	        s   | d j \}}}td\}}}||k r||k r||k r|d |||f }|s |||d d d d d |f |dkr| d |||f | d |||f< d S Nr   r  r   r  )	r  r  r  r  r  r   r   r{   r  r  r4   r5   combine_cuda_3d  s    0z0where._combine_callback.<locals>.combine_cuda_3dc           
        s   | d j \}}}td\}}||k r||k rt|D ]f}|d |||f }|rX q |||d |}	|	dk rv qt| d ||f | d |||f |	 q6d S r  r   r  r  r   r   )
r  r  r  r  r  r   r   r  r  r  r  r4   r5   combine_cuda_n_3d  s    z2where._combine_callback.<locals>.combine_cuda_n_3dc              
     s   | d j \}}}}td\}}}||k r||k r||k rt|D ]}	|d ||||	f }
|
rf qȈ |||d d d d d |d d f |
}|dk r qt| d |||f | d ||||	f | qBd S r  r  )r  r  r  r  r  r  r   r   r{   r  r  r  r  r4   r5   combine_cuda_n_4d  s    *z2where._combine_callback.<locals>.combine_cuda_n_4d)rv  rP   r  r   r   r   r   r   r    r  r  )rA   r   r   r   rv  is_n_reductionr  r  r  r  r  r  r  r  r4   r  r5   _combine_callback  s:    

zwhere._combine_callbackFc                   s"    ||  fdd}|S )Nc                   s   t | dkrnrbt | dks"J tjt}|rD| d jd d n| d j} t| | | n6tdt | D ]&} | d | | f|d || f qp| d |d fS )Nr   r7   r   r   )rS   rP   rv  r  r   r   r   )r  r  r  r   r  combiner   rA   r4   r5   wrapped_combine  s     $z-where._build_combine.<locals>.wrapped_combine)r  )rA   r   r   r   r   r   r  r4   r  r5   r     s    zwhere._build_combinec                 C  s   | j fS r?   )rv  r   r4   r4   r5   r     s    zwhere._build_combine_tempsc                   s,   t jtr fddS t  S d S )Nc                   s    t t| jjf |S r?   )r~   rS  r   rv  r  r   r  r4   r5   r=  '  s   
z%where._build_create.<locals>.<lambda>)rP   rv  r  r~   r   r>  r   r  r5   r   !  s    zwhere._build_createc                   s,   t | jtr| jj nd  d fdd	}|S )NFc                   s,    d ur f i |}t j| d fi |S r  r   r   Zadd_finalize_kwargsr4   r5   rE  2  s    z'where._build_finalize.<locals>.finalize)F)rP   rv  r  r  rF  r4   r  r5   r   ,  s
    
zwhere._build_finalize)N)F)r/   r0   r1   r2   rB   r*  r   r   r   r   rr   r   r   r    r   r   r  r  r   r   r   r   r  r   r   r   r   r   r4   r4   r   r5   rS    s8   

~
rS  c                   @  sD   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Ze	dd Z
dS )summarya%  A collection of named reductions.

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

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

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

    Notes
    -----
    A single pass of the source dataset using antialiased lines can either be
    performed using a single-stage aggregation (e.g. ``self_intersect=True``)
    or two stages (``self_intersect=False``). If a ``summary`` contains a
    ``count`` or ``sum`` reduction with ``self_intersect=False``, or any of
    ``first``, ``last`` or ``min``, then the antialiased line pass will be
    performed in two stages.
    c                 K  s$   t t|  \}}|| _|| _d S r?   )zipsorteditemskeysrb   )rA   r   ksvsr4   r4   r5   rB   R  s    zsummary.__init__c                 C  s   t t| t| jt| jfS r?   )r)  r   r0  r  rb   rC   r4   r4   r5   r*  W  s    zsummary.__hash__c                 C  s   | j D ]}| r dS qdS NTF)rb   r   )rA   vr4   r4   r5   r   Z  s    
zsummary.is_categoricalc                 C  s"   | j D ]}|||r dS qdS r  )rb   r   )rA   r   r   r  r4   r4   r5   r   `  s    
zsummary.uses_row_indexc                 C  sj   | j D ]}|| qg }| j D ]*}t|tr4|j}t|tr ||j q tt	
|dkrftdd S )Nr   zKUsing multiple FloatingNReductions with different n values is not supported)rb   rr   rP   rS  rv  r  r  r  rS   rX   r   rx   )rA   rn   r  r  r4   r4   r5   rr   f  s    



zsummary.validatec                 C  s   t ttdd | jD S )Nc                 s  s   | ]}|j V  qd S r?   )rD   )r,  r  r4   r4   r5   r.  w  r/  z!summary.inputs.<locals>.<genexpr>)r0  r   r
   rb   rC   r4   r4   r5   rD   u  s    zsummary.inputsN)r/   r0   r1   r2   rB   r*  r   r   rr   rF   rD   r4   r4   r4   r5   r  ;  s   r  c                      s0   e Zd ZdZ fddZdd Zdd Z  ZS )r  z=Abstract base class of max and min row_index reductions.
    c                   s   t  jtjd d S )Nr=   )r~   rB   r.   r3   rC   r   r4   r5   rB   }  s    z_max_or_min_row_index.__init__c                 C  s
   t tjS r?   r   ry   rY   r   r4   r4   r5   r     s    z _max_or_min_row_index.out_dshapec                 C  s   dS r7  r4   r   r4   r4   r5   r     s    z$_max_or_min_row_index.uses_row_index)r/   r0   r1   r2   rB   r   r   r   r4   r4   r   r5   r  z  s   r  c                   @  sb   e Zd ZdZddddZeedd Zeedd	 Zee	j
d
ddd Zedd ZdS )r  zMax reduction operating on row index.

    This is a private class as it is not intended to be used explicitly in
    user code. It is primarily purpose is to support the use of ``last``
    reductions using dask and/or CUDA.
    r   r   c                 C  s   t tjdfS r  r   r   rI  r   r4   r4   r5   r     s    z!_max_row_index._antialias_stage_2c                 C  s$   |||| f kr |||| f< dS dS r  r4   r   r4   r4   r5   r     s    z_max_row_index._appendc                 C  s$   |||| f kr |||| f< dS dS r  r4   r  r4   r4   r5   r     s    z _max_row_index._append_antialiasTr	  c                 C  s,   |dkr(t j||| f|}||k r(dS dS Nr   r   )r  r  rf  re  r4   r4   r5   r     s
    z_max_row_index._append_cudac                 C  s4   | d }t dt| D ]}tj|| | |d q|S )Nr   r   )out)r   rS   rX   maximumr  r4   r4   r5   r     s    z_max_row_index._combineN)r/   r0   r1   r2   r   r   r    r   r   r  r  r   r   r4   r4   r4   r5   r    s   	
r  c                   @  s   e Zd ZdZdd ZddddZddd	d
Zeedd Z	eedd Z
eejdddd ZdddZedd Zedd ZdS )r  zMin reduction operating on row index.

    This is a private class as it is not intended to be used explicitly in
    user code. It is primarily purpose is to support the use of ``first``
    reductions using dask and/or CUDA.
    c                 C  s   dS r7  r4   rC   r4   r4   r5   r     s    z+_min_row_index._antialias_requires_2_stagesr   r   c                 C  s   t tjdfS r  r   r   rd  r   r4   r4   r5   r     s    z!_min_row_index._antialias_stage_2r6   c                 C  s   t jS r?   r  rC   r4   r4   r5   r     s    z_min_row_index.uses_cuda_mutexc                 C  s<   |dkr8||| f dks(|||| f k r8|||| f< dS dS r  r4   r   r4   r4   r5   r     s    (z_min_row_index._appendc                 C  s<   |dkr8||| f dks(|||| f k r8|||| f< dS dS r  r4   r  r4   r4   r5   r     s    (z _min_row_index._append_antialiasTr	  c                 C  s<   |dkr8||| f dks(|||| f k r8|||| f< dS dS r  r4   r   r4   r4   r5   r     s    (z_min_row_index._append_cudaFc                 C  s   |r
| j S | jS d S r?   r  r   r4   r4   r5   r     s    z_min_row_index._build_combinec                 C  s.   | d }t dt| D ]}t|| |  q|S r  )r   rS   r$   r  r4   r4   r5   r     s    z_min_row_index._combinec                 C  sl   | d }t | dkrh|jdkr,dd | D } t|jd d }tdt | D ]}t| | d | |  qL|S )Nr   r   r7   c                 S  s   g | ]}t |d qS )r7   )rV   Zexpand_dims)r,  r  r4   r4   r5   r5    r/  z0_min_row_index._combine_cuda.<locals>.<listcomp>r  )rS   r  r   r   r   r   r  r4   r4   r5   r    s    
z_min_row_index._combine_cudaN)F)r/   r0   r1   r2   r   r   r   r   r    r   r   r  r  r   r   r   r  r4   r4   r4   r5   r    s$   	


r  c                      sJ   e Zd ZdZd fdd	Zdd Zddd	d
Zdd ZdddZ  Z	S )r  zAAbstract base class of max_n and min_n row_index reductions.
    r   c                   s&   t  jtjd |dkr|nd| _d S )Nr=   r   )r~   rB   r.   r3   r  )rA   r  r   r4   r5   rB   	  s    z"_max_n_or_min_n_row_index.__init__c                 C  s
   t tjS r?   r  r   r4   r4   r5   r   	  s    z$_max_n_or_min_n_row_index.out_dshaper6   r   c                 C  s   t jS r?   r  rC   r4   r4   r5   r   	  s    z)_max_n_or_min_n_row_index.uses_cuda_mutexc                 C  s   dS r7  r4   r   r4   r4   r5   r   	  s    z(_max_n_or_min_n_row_index.uses_row_indexFc                 C  s   |r
| j S | jS d S r?   r  r   r4   r4   r5   r   	  s    z(_max_n_or_min_n_row_index._build_combine)r   )F)
r/   r0   r1   r2   rB   r   r   r   r   r   r4   r4   r   r5   r  	  s   r  c                   @  sn   e Zd ZdZddddZeedd Zeedd	 Zee	j
d
ddd Zedd Zedd ZdS )r  zMax_n reduction operating on row index.

    This is a private class as it is not intended to be used explicitly in
    user code. It is primarily purpose is to support the use of ``last_n``
    reductions using dask and/or CUDA.
    r   r   c                 C  s   t tjdddfS Nr   Tr  r  r   r4   r4   r5   r   #	  s    z#_max_n_row_index._antialias_stage_2c                 C  sd   |dkr`|j d }t|D ]D}||| |f dksB|||| |f krt||| f || |  S qdS Nr   r7   r  r  r4   r4   r5   r   &	  s    
$
z_max_n_row_index._appendc           	      C  s   |dkr|j d }t|D ]p}||| |f dksB|||| |f krt|d |dD ] }||| |d f ||| |f< qR|||| |f< |  S qdS )Nr   r7   r   r  )	r   r   r  r  r  r  r  r  jr4   r4   r5   r   4	  s    
$
z"_max_n_row_index._append_antialiasTr	  c                 C  sd   |dkr`|j d }t|D ]D}||| |f dksB|||| |f krt||| f || |  S qdS r  r   r   r   r  r4   r4   r5   r   G	  s    
$
z_max_n_row_index._append_cudac                 C  sH   | d }t | dkrD|jdkr2t| d | d  nt| d | d  |S r  )rS   r  r*   r)   r  r  r4   r4   r5   r   V	  s    
z_max_n_row_index._combinec                 C  sb   | d }t | dkr^t|jd d }|jdkrHt| | d | d  nt| | d | d  |S Nr   r   r   r  )rS   r   r   r  r   r   r  r  r  r4   r4   r5   r  `	  s    
z_max_n_row_index._combine_cudaN)r/   r0   r1   r2   r   r   r    r   r   r  r  r   r   r  r4   r4   r4   r5   r  	  s   

	r  c                   @  sv   e Zd ZdZdd ZddddZeedd	 Zeed
d Z	ee
jdddd Zedd Zedd ZdS )r  zMin_n reduction operating on row index.

    This is a private class as it is not intended to be used explicitly in
    user code. It is primarily purpose is to support the use of ``first_n``
    reductions using dask and/or CUDA.
    c                 C  s   dS r7  r4   rC   r4   r4   r5   r   s	  s    z-_min_n_row_index._antialias_requires_2_stagesr   r   c                 C  s   t tjdddfS r  r  r   r4   r4   r5   r   v	  s    z#_min_n_row_index._antialias_stage_2c                 C  sd   |dkr`|j d }t|D ]D}||| |f dksB|||| |f k rt||| f || |  S qdS r  r  r  r4   r4   r5   r   y	  s    
$
z_min_n_row_index._appendc                 C  sd   |dkr`|j d }t|D ]D}||| |f dksB|||| |f k rt||| f || |  S qdS r  r  )r   r   r  r  r  r  r  r  r4   r4   r5   r   	  s    
$
z"_min_n_row_index._append_antialiasTr	  c                 C  sd   |dkr`|j d }t|D ]D}||| |f dksB|||| |f k rt||| f || |  S qdS r  r  r  r4   r4   r5   r   	  s    
$
z_min_n_row_index._append_cudac                 C  sH   | d }t | dkrD|jdkr2t| d | d  nt| d | d  |S r  )rS   r  r,   r+   r  r4   r4   r5   r   	  s    
z_min_n_row_index._combinec                 C  sb   | d }t | dkr^t|jd d }|jdkrHt| | d | d  nt| | d | d  |S r  )rS   r   r   r  r   r   r  r4   r4   r5   r  	  s    
z_min_n_row_index._combine_cudaN)r/   r0   r1   r2   r   r   r   r    r   r   r  r  r   r   r  r4   r4   r4   r5   r  l	  s    

	r  c                 C  s@   g | ]8\}}t |trt|ts(|tu r|ttttfvr|qS r4   )rP   r   
issubclassr   r  r   rN  rU  )r,  Z_kZ_vr4   r4   r5   r5  	  s
   r5  )t
__future__r   rB  enumr   Zpackaging.versionr   numpyrX   Zdatashader.datashaper   r   r   r   r	   ry   Ztoolzr
   r   ZxarrayrQ   Zdatashader.antialiasr   r   Zdatashader.utilsr   r   Znumbar   r  Z)datashader.transfer_functions._cuda_utilsr   r   r   r   r   r   r   r   r   r   r   r   r   ImportErrorrT   r   rV   	Exceptionutilsr   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r6   r;   rG   rh   rt   r}   r   r   r   r   r   r   r  r"  rG  rJ  rN  rO  rR  r  rU  rb  rf  rg  rh  rm  rp  rr  r  r  r  r  r  r  r  r  r  rS  r  r  r  r  r  r  r  r   setlocalsr  __all__r4   r4   r4   r5   <module>   s   @
D&%6/ '$ ~D#
=G;07)&&#&.MP!  F?3KPP