a
    `c                     @   s   d dl mZmZmZ d dlmZ d dlmZmZm	Z	m
Z
mZ d dlZd dlZddlmZ ddlmZmZmZ ddlmZ d	gZeddd	Zdd Zdd Zdd Zdd Zdd Zdd Zdd Z dS )    )absolute_importdivisionprint_function)count)uniqueconcatpluckgetmemoizeN   )_exec)bycategory_codessummary)ngjitcompile_componentsFc                    s   t t| }t tt fdd|D }fdd|D } fddt||D }t tttd|}t td|}	t|| }
t|}t||||t	| t
}t|||	}t||  }|
||||fS )a  Given a ``Aggregation`` object and a schema, return 5 sub-functions.

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

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

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

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

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

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

    ``finalize(aggs)``
        Given a tuple of base numpy arrays, returns the finalized ``DataArray``
        or ``Dataset``.
    c                 3   s   | ]}|  V  qd S N)_build_bases).0r)cuda 2lib/python3.9/site-packages/datashader/compiler.py	<genexpr>6       z%compile_components.<locals>.<genexpr>c                    s   g | ]}|  qS r   )Z
out_dshaper   b)schemar   r   
<listcomp>7   r   z&compile_components.<locals>.<listcomp>c                    s   g | ]\}}t || qS r   )_get_call_tuplesr   r   dr   r   r   r   r   9   r         )listtraverse_aggregationr   r   zipr   make_create	make_infomake_append
isinstancer   make_combinemake_finalize)aggr   glyphr   ZredsbasesdshapescallscolstempsZcreateinfoappendcombinefinalizer   r"   r   r      s    "c                 c   s6   t | tr,| jD ]}t|D ]
}|V  qqn| V  dS )z/Yield a left->right traversal of an aggregationN)r+   r   valuesr&   )r.   aZa2r   r   r   r&   H   s
    

r&   c                 C   s    |  |||| f| j| |fS r   )Z_build_appendinputsZ_build_temps)baseZdshaper   r   r   r   r   r   R   s    r   c                    s8   dd t | |D |r&dd l}| nt  fddS )Nc                 S   s   g | ]\}}| |qS r   )Z_build_creater    r   r   r   r   X   r   zmake_create.<locals>.<listcomp>r   c                    s   t  fddD S )Nc                 3   s   | ]}| V  qd S r   r   r   c)array_moduleshaper   r   r   ^   r   z0make_create.<locals>.<lambda>.<locals>.<genexpr>tupler@   r?   ZcreatorsrC   r   <lambda>^   r   zmake_create.<locals>.<lambda>)r'   cupynp)r0   r1   r   rF   r   rD   r   r(   W   s    r(   c                    s    fddS )Nc                    s   t  fddD S )Nc                 3   s   | ]}|  V  qd S r   )Zapplyr=   Zdfr   r   r   b   r   z.make_info.<locals>.<lambda>.<locals>.<genexpr>rA   rH   r3   rH   r   rE   b   r   zmake_info.<locals>.<lambda>r   rI   r   rI   r   r)   a   s    r)   c              	      s2  dd t  D t| t| }fdd|D }tt|| i i }g }|j}	|	d urtddd t|	D nd |D ]\}
} }}t|fdd|D  t}|
||<  fdd| D }|rt	|d	 t
rn^|	d u r| fd
d|D  n<|r | fdd|D  n| fdd|D  |fdd|D  |d|d| q| fdd D | }|rt	|d	 t
rdnd}d|d |}dd |d t| D }|g| | }|	d u rdd|d|}ndd|d|}t|| t|d S )Nc                 s   s   | ]}d  |V  qdS )z_{0}Nformatr   ir   r   r   r   f   r   zmake_append.<locals>.<genexpr>c                    s   g | ]}t  qS r   nextrL   namesr   r   r   h   r   zmake_append.<locals>.<listcomp>z, c                 S   s   g | ]}d t | qS )rM   )str)r   nr   r   r   r   o   r   c                 3   s   | ]}t  V  qd S r   rN   rL   rP   r   r   r   t   r   c                    s   g | ]} | qS r   r   rL   arg_lkr   r   r   w   r   r   c                 3   s   | ]}d   | V  qdS )z{0}NrJ   rL   rT   r   r   r   {   r   c                 3   s   | ]}d   | V  qdS )z{0}[{1}][1]NrJ   rL   rU   	subscriptr   r   r   }   s   c                 3   s   | ]}d   | V  qdS )z{0}[{1}]NrJ   rL   rV   r   r   r      s   c                    s   g | ]} | qS r   r   rL   )local_lkr   r   r      r   z{0}(x, y, {1})c                    s    g | ]\}}d  | | qS )z{0} = {1}[y, x]rJ   )r   r.   namerT   r   r   r      s    z[0]zcat = int({0}[{1}]{2})c                 S   s   g | ]}d  |qS )z{0} = {0}[:, :, cat]rJ   )r   sr   r   r   r      r   zdef append(x, y, {0}):
    {1}z
    z#def append({0}, x, y, {1}):
    {2}r6   )r   r%   dictr'   ndimsjoinrangeupdaterO   r+   r   extendr6   rK   itemslenr   r   )r0   r3   r2   r/   Zcategoricalr;   Z	signature	namespacebodyr^   funcr4   Z	func_nameargsZ	col_indexZcat_varZaggscoder   )rU   rX   rQ   rW   r   r*   e   s`    



r*   c                    s@   t dd t| D   fddt| ||D fdd}|S )Nc                 s   s   | ]\}}||fV  qd S r   r   r   vkr   r   r   r      r   zmake_combine.<locals>.<genexpr>c                    s4   g | ],\}}}| | fd d|f| D fqS )c                    s   g | ]} | qS r   r   rL   rT   r   r   r      r   z+make_combine.<locals>.<listcomp>.<listcomp>)Z_build_combine)r   r   r!   trT   r   r   r      s   z make_combine.<locals>.<listcomp>c                    s,   t dd t|  D  t  fddD S )Nc                 s   s   | ]}t |V  qd S r   )rG   stack)r   bsr   r   r   r      r   z0make_combine.<locals>.combine.<locals>.<genexpr>c                 3   s    | ]\}}|t |  V  qd S r   r	   )r   findsr0   r   r   r      r   )rB   r'   )Zbase_tuplesr2   rs   r   r7      s    zmake_combine.<locals>.combine)r]   	enumerater'   )r0   r1   r4   r7   r   rU   r2   r   r,      s    

r,   c           	   	      s   t dd t| D  t|trg t|j|jD ]Z\}}t| |||}z||} W n t	yh   Y n0  fdd| D }
|||f q2dfdd	}|S ||S d S )	Nc                 s   s   | ]\}}||fV  qd S r   r   rj   r   r   r   r      r   z make_finalize.<locals>.<genexpr>c                    s   g | ]} | qS r   r   r   rT   r   r   r      r   z!make_finalize.<locals>.<listcomp>Fc                    s     fddD }t |S )Nc                    s,   i | ]$\}}}||t | fi qS r   rp   )r   key	finalizerrr   r0   r   kwargsr   r   
<dictcomp>   s   z3make_finalize.<locals>.finalize.<locals>.<dictcomp>)xrZDataset)r0   r   rz   datart   ry   r   r8      s    zmake_finalize.<locals>.finalize)F)r]   ru   r+   r   r'   keysr9   r-   r   AttributeErrorr6   Z_build_finalize)	r0   r.   r   r   rw   valrq   rr   r8   r   rv   r   r-      s    
r-   )F)!Z
__future__r   r   r   	itertoolsr   Ztoolzr   r   r   r	   r
   ZnumpyrG   Zxarrayr|   Zcompatibilityr   Z
reductionsr   r   r   Zutilsr   __all__r   r&   r   r(   r)   r*   r,   r-   r   r   r   r   <module>   s"   6

6