a
    `~                     @   s  d dl mZmZmZ zd dlmZ W n eyB   d dlmZ Y n0 d dlmZ d dl	m
Z
 d dlZd dlZd dlZd dlZd dlmZ d dlmZ d dlmZmZ d dlmZmZmZ d d	lm Z m!Z!m"Z" zd dl#Z#W n e$y   dZ#Y n0 g d
Z%G dd dej&Z'G dd de(Z)dd Z*d@ddZ+dd dd dd e+dZ,dd Z-dd Z.dd Z/dd gedd!d"dddfd#d$Z0dAd%d&Z1dBd)d*Z2ej3d+d, Z4ej3d-d. Z5ej3d/d0 Z6d1d2 Z7d3d4 Z8e7e8d5Z9dCd8d9Z:ej;d:d:d:d;dDd<d=Z<ej;d:d:d:d;dEd>d?Z=dS )F    )absolute_importdivisionprint_function)Iterator)OrderedDictBytesION)	fromarray)rgbSets1to3)composite_op_lookupovervalidate_operator)nansum_missingngjitorient_array)Imagestackshadeset_backgroundspread	dynspreadc                   @   s<   e Zd ZdZdZdZdddZddd	Zd
d Zdd Z	dS )r    F      lowerc                 C   s4   | j }trt|}|dkr&t|n|}t|dS )Nr   ZRGBA)datacupyasnumpynpZflipudr	   )selforiginr   arrr   r   Elib/python3.9/site-packages/datashader/transfer_functions/__init__.pyto_pil#   s
    
zImage.to_pilpngc                 C   s&   t  }| ||| |d |S Nr   )r   r$   saveseek)r    formatr!   fpr   r   r#   
to_bytesio*   s    
zImage.to_bytesioc                 C   s   |    S )z/Supports rich PNG display in a Jupyter notebook)r$   
_repr_png_)r    r   r   r#   r,   0   s    zImage._repr_png_c                 C   s^   ddl m} ddlm} | }|  j|dd dt| j d d||	 
d	 }|S )
z0Supports rich HTML display in a Jupyter notebookr   r   )	b64encoder%   )r)   z!<img style="margin: auto; border:z
px solid" z!src='data:image/png;base64,{0}'/>zutf-8)ior   base64r-   r$   r'   strborderr)   getvaluedecode)r    r   r-   bhr   r   r#   _repr_html_4   s    zImage._repr_html_N)r   )r%   r   )
__name__
__module____qualname__	__slots__Z__array_priority__r1   r$   r+   r,   r6   r   r   r   r#   r      s   

r   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	Imagesz
    A list of HTML-representable objects to display in a table.
    Primarily intended for Image objects, but could be anything
    that has _repr_html_.
    c                 G   s(   |D ]}t |dsJ q|| _d| _dS )z@Makes an HTML table from a list of HTML-representable arguments.r6   N)hasattrimagesnum_cols)r    r=   ir   r   r#   __init__K   s    zImages.__init__c                 C   s
   || _ | S )zk
        Set the number of columns to use in the HTML table.
        Returns self for convenience.
        )r>   )r    nr   r   r#   colsR   s    zImages.colsc                 C   s   g }d}d}| j D ]l}t|dr0|jdur0|jnd}|d| d|   |d7 }| jdur|| jkrd}|d	|  qd
| d| d S )z@Supports rich display in a Jupyter notebook, using an HTML tabler   z#<tr style="background-color:white">nameN z"<td style="text-align: center"><b>z</b><br><br>{0}</td>r   z</tr>z5<table style="width:100%; text-align: center"><tbody>z</tr></tbody></table>)r=   r<   rC   appendr)   r6   r>   join)r    Zhtmlscoltrr?   Zlabelr   r   r#   r6   Z   s"    

zImages._repr_html_N)r7   r8   r9   __doc__r@   rB   r6   r   r   r   r#   r;   D   s   r;   c                  O   s  | st dg }| D ]J}t|ts6tdt|q|sH||j q|r|j|vrt dq|dd}t	|dd }t
| dkr| d	 S tj| d
dd} tjddd, tt|dd | D }W d   n1 s0    Y  t|| d	 j| d	 j|dS )a  Combine images together, overlaying later images onto earlier ones.

    Parameters
    ----------
    imgs : iterable of Image
        The images to combine.
    how : str, optional
        The compositing operator to combine pixels. Default is `'over'`.
    zNo images passed inExpected `Image`, got: `{0}`z,The stacked images must have the same shape.rC   Nhowr   r   r   FZouter)copyrF   ignoredivideinvalidc                 S   s   g | ]
}|j qS r   )r   ).0r?   r   r   r#   
<listcomp>       zstack.<locals>.<listcomp>coordsdimsrC   )
ValueError
isinstancer   	TypeErrorr)   typerE   shapegetr   lenxrZalignr   errstatetzreduceZfliprU   rV   )ZimgskwargsZshapesr?   rC   opoutr   r   r#   r   n   s$    


:r      c                 C   sF  t rt| t jrddlm} nt| tjs4tdntj}|du rF| n| |  }|jtksjt	|jtj
r|jjdkr|d}t| }tt|}tt|d d }||d ||d  }}n.tj||d\}}|dd	 |dd  d
 }| }	|	t|	d	  }	|| ||	| j}
|du r6|
S t|tj|
S )ae  Return a numpy array after histogram equalization.

    For use in `shade`.

    Parameters
    ----------
    data : ndarray
    mask : ndarray, optional
       Boolean array of missing points. Where True, the output will be `NaN`.
    nbins : int, optional
        Number of bins to use. Note that this argument is ignored for integer
        arrays, which bin by the integer values directly.

    Notes
    -----
    This function is adapted from the implementation in scikit-image [1]_.

    References
    ----------
    .. [1] http://scikit-image.org/docs/stable/api/skimage.exposure.html#equalize-hist
    r   )interpzdata must be an ndarrayNuZi8r   )Zbins   )r   rX   ndarray_cuda_utilsrf   r   rY   dtypebool
issubdtypeZintegerkindastypeZbincountZravelaranger]   intZnonzeroZ	histogramZcumsumfloatreshaper[   wherenan)r   maskZnbinsrf   Zdata2ZhistZbin_centersidxZ	bin_edgesZcdfrd   r   r   r#   eq_hist   s&    

ry   c                 C   s   t t |t j| S N)r   Zlog1pru   rv   dmr   r   r#   <lambda>   rS   r~   c                 C   s   t |t j| d S )NgUUUUUU?r   ru   rv   r{   r   r   r#   r~      rS   c                 C   s   t |t j| S rz   r   r{   r   r   r#   r~      rS   )logZcbrtZlinearry   c                 C   s.   t | r| S | tv rt|  S td| d S )Nz!Unknown interpolation method: {0})callable_interpolate_lookuprW   r)   )rK   r   r   r#   _normalize_interpolate_how   s
    r   c                 C   s6  t r$t| jt jr$ddlm}m} nddlm} tj}| j	dkrHt
dt|}	t| }
t|
tjrn|
 }
n|
 }
t|
jtjr|
 }|
tj}
n |
jjdkr|
dk}n
t|
}| rttj| jjtjd| j| j| j|d	S |d u rt |
|  }n&tj!||
jd
d }||
|g|R   |
|8 }
tj"dddx |	|
|}
|d u rt#| |
tj$}t |t%|f}n,|dkrt
d|	d|d |d  gd}W d    n1 s0    Y  t|t&rt'|}t|t'rt!t't(t)t*| \}}}t+|d |d t,|}||
||ddtj-}||
||ddtj-}||
||ddtj-}t#t|
d|tj-}t.||||g}nRt|t/st|t0r^t*|}t1||d }t+|d |d t,|}tj2|
j|d tj-d
}tj2|
j|d tj-d
}tj2|
j|d tj-d
}||
||dddtj-}t.||||g}nt3|r|
|d  |d |d   }t rt|t jrt 4|}||dd}t#t|d|tj-|d d d d df< nt5d6t7||8tj9|
j}t r"t|t jr"t 4|}t|| j| j|dS )Nr   )masked_clip_2drf   r   ri   zagg must be 2Drg   r   )r[   rl   )rU   rV   attrsrC   rl   rM   )rP   rO   ry   +span is not (yet) valid to use with eq_hist   )leftr   rightT)bytes   zVExpected `cmap` of `matplotlib.colors.Colormap`, `list`, `str`, or `tuple`; got: '{0}'rT   ):r   rX   r   rj   rk   r   rf   
_cpu_utilsr   ndimrW   r   r   daArraycomputerL   rn   rl   Zbool_rp   Zint8ro   isnanallr   zerosr[   uint32rU   rV   r   nanminarrayr_   ru   rv   nanmaxr   listzipmapr
   Zlinspacer]   uint8dstackr0   tuplerq   fullr   r   rY   r)   rZ   viewrt   )aggcmaprK   alphaspan	min_alpharC   r   rf   Zinterpolaterr   rw   offsetZmasked_dataZrspanZgspanZbspanrgr4   aZrgbacolorZaspanZscaled_dataimgr   r   r#   _interpolate   s    








:

0
r   c           %   	      s  t r*t| jt jr*ddlm}m}	 t j}
nddlm}	 t	j}t	j}
| j
dksTtd| j| jd  }t|stt	j| jdd t	jd	| jd d t| jd | j| jd  f| jd | j| jd  fg|d
S  d u rtdt tstt|  t t|k r"tdt t| fdd|D }t|
t| \}}}| j| jd f| jd d   }t|g d}t|tjr| }| }|d u rt	|n|}t	j dd\ |dkr||8 }n|dk r|| 7 }|j!j"dkr
|d ur
d||dk < W d    n1 s 0    Y  t#|dd}d|t	$|< t	j dddR |%|| &t	j'}|%|| &t	j'}|%|| &t	j'}W d    n1 s0    Y  t	$| }t	j(|dd}t	j dddR |%|| &t	j'}|%|| &t	j'}|%|| &t	j'}W d    n1 s80    Y  t	j(|dddk}t	)|||}t	)|||}t	)|||}t#|dd}t	$|}|d u r*t	|} |j!j"dkr| dkr||dkB }t	*|s||dk + } t	)| |t	j,}t-|||  |}!t	|!. t	/|!. g}"n|dkr<tdt	j||j!d	d } |j!j"dkrt	|dkr||dkB }t	)| |t	j,}|	||g|R   t-|||  |}!t-|d|d |d  gd}"||!|
|"|
||gddd&t	j'}#t	0||||#g1t	j2|#j}$t r4t|$t jr4t 3|$}$t|$| jd d t| jd | j| jd  f| jd | j| jd  fg|d
S )Nr   )rf   r   r   r   zagg must be 3Drh   r   ri   r   rV   rU   rC   zXColor key must be provided, with at least as many colors as there are categorical fieldszKInsufficient colors provided ({}) for the categorical fields available ({})c                    s   g | ]}t  | qS r   )r
   )rQ   c	color_keyr   r#   rR   J  rS   z_colorize.<locals>.<listcomp>)r   ri   r   rM   )rP   rg   )axisrN   ry   r   r   r   )4r   rX   r   rj   rk   rf   r   r   r   r   r   rW   ZindexesrV   r]   r   r   r[   r   r   rU   dictr   r)   r   Z	transposer   r   r   r   rL   r   r_   rl   ro   r   r   dotrp   r   sumru   r   minrv   r   itemr   r   r   rt   r   )%r   r   rK   r   r   r   rC   color_baselinerf   r   r   ZcatsZcolorsZrsZgsbsZagg_tr   Z
color_dataZbaselineZcolor_totalr   r   r4   Z
color_maskZ	cmask_sumZr2Zg2b2Zmissing_colorstotalrw   r   Za_scaledZ	norm_spanr   valuesr   r   r#   	_colorize.  s    
&
 



,66



"
r   Z	lightblueZdarkbluer   (   c	           	   	   C   s   t | tjstd|du r"| jn|}d|  kr:dkrTn nd|  krRdksdn td||| jdkrt| ||||||S | jdkrt	| |||||||S tddS )	a  Convert a DataArray to an image by choosing an RGBA pixel color for each value.

    Requires a DataArray with a single data dimension, here called the
    "value", indexed using either 2D or 3D coordinates.

    For a DataArray with 2D coordinates, the RGB channels are computed
    from the values by interpolated lookup into the given colormap
    ``cmap``.  The A channel is then set to the given fixed ``alpha``
    value for all non-zero values, and to zero for all zero values.

    DataArrays with 3D coordinates are expected to contain values
    distributed over different categories that are indexed by the
    additional coordinate.  Such an array would reduce to the
    2D-coordinate case if collapsed across the categories (e.g. if one
    did ``aggc.sum(dim='cat')`` for a categorical dimension ``cat``).
    The RGB channels for the uncollapsed, 3D case are computed by
    averaging the colors in the provided ``color_key`` (with one color
    per category), weighted by the array's value for that category.
    The A channel is then computed from the array's total value
    collapsed across all categories at that location, ranging from the
    specified ``min_alpha`` to the maximum alpha value (255).

    Parameters
    ----------
    agg : DataArray
    cmap : list of colors or matplotlib.colors.Colormap, optional
        The colormap to use for 2D agg arrays. Can be either a list of
        colors (specified either by name, RGBA hexcode, or as a tuple
        of ``(red, green, blue)`` values.), or a matplotlib colormap
        object.  Default is ``["lightblue", "darkblue"]``.
    color_key : dict or iterable
        The colors to use for a 3D (categorical) agg array.  Can be
        either a ``dict`` mapping from field name to colors, or an
        iterable of colors in the same order as the record fields,
        and including at least that many distinct colors.
    how : str or callable, optional
        The interpolation method to use, for the ``cmap`` of a 2D
        DataArray or the alpha channel of a 3D DataArray. Valid
        strings are 'eq_hist' [default], 'cbrt' (cube root), 'log'
        (logarithmic), and 'linear'. Callables take 2 arguments - a
        2-dimensional array of magnitudes at each pixel, and a boolean
        mask array indicating missingness. They should return a numeric
        array of the same shape, with ``NaN`` values where the mask was
        True.
    alpha : int, optional
        Value between 0 - 255 representing the alpha value to use for
        colormapped pixels that contain data (i.e. non-NaN values).
        Also used as the maximum alpha value when alpha is indicating
        data value, such as for single colors or categorical plots.
        Regardless of this value, ``NaN`` values are set to be fully
        transparent when doing colormapping.
    min_alpha : float, optional
        The minimum alpha value to use for non-empty pixels when 
        alpha is indicating data value, in [0, 255].  Use a higher value
        to avoid undersaturation, i.e. poorly visible low-value datapoints,
        at the expense of the overall dynamic range.
    span : list of min-max range, optional
        Min and max data values to use for colormap/alpha interpolation, when
        wishing to override autoranging.
    name : string name, optional
        Optional string name to give to the Image object to return,
        to label results for display.
    color_baseline : float or None
        Baseline for calculating how categorical data mixes to
        determine the color of a pixel. The color for each category is
        weighted by how far that category's value is above this
        baseline value, out of the total sum across all categories'
        values. A value of zero is appropriate for counts and for
        other physical quantities for which zero is a meaningful
        reference; each category then contributes to the final color
        in proportion to how much each category contributes to the
        final sum.  However, if values can be negative or if they are
        on an interval scale where values e.g. twice as far from zero
        are not twice as high (such as temperature in Farenheit), then
        you will need to provide a suitable baseline value for use in
        calculating color mixing.  A value of None (the default) means
        to take the minimum across the entire aggregate array, which
        is safe but may not weight the colors as you expect; any
        categories with values near this baseline will contribute
        almost nothing to the final color. As a special case, if the
        only data present in a pixel is at the baseline level, the
        color will be an evenly weighted average of all such
        categories with data (to avoid the color being undefined in
        this case).
    z!agg must be instance of DataArrayNr   r   z7min_alpha ({}) and alpha ({}) must be between 0 and 255ri   r   z!agg must use 2D or 3D coordinates)
rX   r^   	DataArrayrY   rC   rW   r)   r   r   r   )	r   r   r   rK   r   r   r   rC   r   r   r   r#   r     s    X.

r   c                 C   sv   t | tstdt| |du r*| jn|}|du r:| S tt|d 	dd }t
| j|}t|| j| j|dS )a  Return a new image, with the background set to `color`.

    Parameters
    -----------------
    img : Image
    color : color name or tuple, optional
        The background color. Can be specified either by name, hexcode, or as a
        tuple of ``(red, green, blue)`` values.
    rJ   N)r   r   r   rT   )rX   r   rY   r)   rZ   rC   r   r   r
   r   r   r   rU   rV   )r   r   rC   Z
backgroundr   r   r   r#   r     s    

r   r   circlec                    s  t tjstdtt t}|du r6jn|}du rvt |trT|dk r\t	d|dkrhS t
| |n\t tjrjdkrjd jd krjd d dkst	djdkrȈnd|du r|rd	nd
}jd }|d jdd \}}	|d  |	d  fjtjtjfv }
|
r@tjnd|rVt||n&|
rht||nt||jtjkfdd tjdkr }n$t fddtjd D }j|jj|dS )as  Spread pixels in an image.

    Spreading expands each pixel a certain number of pixels on all sides
    according to a given shape, merging pixels using a specified compositing
    operator. This can be useful to make sparse plots more visible.

    Parameters
    ----------
    img : Image or other DataArray
    px : int, optional
        Number of pixels to spread on all sides
    shape : str, optional
        The shape to spread by. Options are 'circle' [default] or 'square'.
    how : str, optional
        The name of the compositing operator to use when combining
        pixels. Default of None uses 'over' operator for Image objects
        and 'add' operator otherwise.
    mask : ndarray, shape (M, M), optional
        The mask to spread over. If provided, this mask is used instead of
        generating one based on `px` and `shape`. Must be a square array
        with odd dimensions. Pixels are spread from the center of the mask to
        locations where the mask is True.
    name : string name, optional
        Optional string name to give to the Image object to return,
        to label results for display.
    z#Expected `xr.DataArray`, got: `{0}`Nr   z``px`` must be an integer >= 0ri   r   z@mask must be a square 2 dimensional ndarray with odd dimensions.rm   r   addc                    s<   t j| jd}| j| |      f  S )Nr   )r   r   rl   r   rL   )Zlayerbuf)extra
fill_valuekernelrw   padded_shaper   r#   apply_kernel\  s    zspread.<locals>.apply_kernelc                    s&   g | ]} d d d d |f qS rz   r   )rQ   category)r   r   r   r#   rR   d  s   zspread.<locals>.<listcomp>r   )rX   r^   r   rY   r)   rZ   r   rC   rr   rW   _mask_lookupr   rj   r   r[   rl   rp   float32float64rv   _build_spread_kernel_build_float_kernel_build_int_kernelr   r]   r   range	__class__rV   rU   )r   pxr[   rK   rw   rC   is_imagewMN
float_typerd   r   )r   r   r   r   r   rw   r   r#   r   !  sJ    


r   c                    s0   t | dd t| d  t fdd}|S )7Build a spreading kernel for a given composite operatorFr   _arrc              
      s   | j \}}t|D ]}t|D ]}| ||f }tD ]}tD ]}	|||	f rB rt|dkrt||| |	| f }
n< r||| |	| f dkr|}
n|||| |	| f }
|
||| |	| f< qBq6qqd S r&   )r[   r   r"   rw   rd   r   r   yxelr?   jresultignore_zeros	mask_sizerc   r   r#   
stencilledo  s    
z%_build_int_kernel.<locals>.stencilledr   r   r   )rK   r   r   r   r   r   r#   r   j  s
    r   c                    s.   t | dd t| d  t fdd}|S )r   Fr   r   c              
      s   | j \}}t|D ]}t|D ]}| ||f }t D ]}t D ]~}	|||	f rBt|rr||| |	| f }
n:t||| |	| f r|}
n|||| |	| f }
|
||| |	| f< qBq6qqd S rz   )r[   r   r   r   r   r   rc   r   r#   r     s    

z'_build_float_kernel.<locals>.stencilledr   )rK   r   r   r   r   r#   r     s
    r   c                    s6   t | dd t|  rdnd  t fdd}|S )r   Tr   rD   r   c              
      s   | j \}}|j d }t|D ]}t|D ]}| ||f } oJt|d? d@ }	  o\t| }
|	sf|
r(t|D ]}t|D ]x}|||f rz|dkr||| || f }||| || f dkr|}n|||| || f }|||| || f< qzqnq(qd S )Nr      r   )r[   r   rr   r   r   )r"   rw   rd   r   r   r   r   r   r   Zprocess_imageZprocess_arrayr?   r   r   r   rc   r   r#   r     s"    

z$_build_spread_kernel.<locals>.kernelr   )rK   r   r   r   r   r#   r     s
    r   c                 C   s&   t | } d|  d }tj||fddS )z9Produce a square mask with sides of length ``2 * px + 1``ri   r   rm   r   )rr   r   Zones)r   r   r   r   r#   _square_mask  s    r   c                 C   sJ   t j|  | d dd}t t |d |dddf d  | d kddS )	z8Produce a circular mask with a diameter of ``2 * r + 1``r   Zi4r   ri   N      ?TF)r   rq   ru   Zsqrt)r   r   r   r   r#   _circle_mask  s    r   )Zsquarer   r   r   c                 C   s  t | t}d|  krdks(n tdt |tr:|dk rBtd| jtjtjfv }d}td|d D ]}	|	}|rt	| j
|	d }
n`t| jdkrt| j
||	d }
n>|rtt| n| dk}tj|ddd}t|j
d|	d }
|
|krf|d } qqf|dkrt| ||||d	S | S d
S )aX  Spread pixels in an image dynamically based on the image density.

    Spreading expands each pixel a certain number of pixels on all sides
    according to a given shape, merging pixels using a specified compositing
    operator. This can be useful to make sparse plots more visible. Dynamic
    spreading determines how many pixels to spread based on a density
    heuristic.  Spreading starts at 1 pixel, and stops when the fraction
    of adjacent non-empty pixels reaches the specified threshold, or
    the max_px is reached, whichever comes first.

    Parameters
    ----------
    img : Image
    threshold : float, optional
        A tuning parameter in [0, 1], with higher values giving more
        spreading.
    max_px : int, optional
        Maximum number of pixels to spread on all sides.
    shape : str, optional
        The shape to spread by. Options are 'circle' [default] or 'square'.
    how : str, optional
        The name of the compositing operator to use when combining
        pixels. Default of None uses 'over' operator for Image objects
        and 'add' operator otherwise.
    r   r   zthreshold must be in [0, 1]zmax_px must be >= 0ri   r   )r   rl   F)r[   rK   rC   N)rX   r   rW   rr   rl   r   r   r   r   _rgb_densityr   r]   r[   _array_densityZlogical_notr   r   r   )r   Z	thresholdZmax_pxr[   rK   rC   r   r   Zpx_r   ZdensityZmaskedZ	flat_maskr   r   r#   r     s,    

r   T)ZnopythonZnogilcachec              	   C   s
  | j \}}d }}td|D ]}td|D ]}| ||f }	|rHt|	rT|s*|	dkr*|d7 }d}
ttd|| t|| d |D ]^}ttd|| t|| d |D ]6}|rt| ||f r|s| ||f dkr|
d7 }
qq|
dkr*|d7 }q*q|r|| S tjS )zCompute a density heuristic of an array.

    The density is a number in [0, 1], and indicates the normalized mean number
    of non-empty pixels that have neighbors in the given px radius.
    r   r   )r[   r   r   r   maxr   inf)r"   r   r   r   r   cnthas_neighborsr   r   r   	neighborsr?   r   r   r   r#   r      s&    
&&r   c                 C   s   | j \}}d }}td|D ]}td|D ]}| ||f d? d@ r*|d7 }d}ttd|| t|| d |D ]H}	ttd|| t|| d |D ] }
| |	|
f d? d@ r|d7 }qqp|dkr*|d7 }q*q|r|| S tjS )zCompute a density heuristic of an image.

    The density is a number in [0, 1], and indicates the normalized mean number
    of non-empty pixels that have neighbors in the given px radius.
    r   r   r   r   )r[   r   r   r   r   r   )r"   r   r   r   r   r   r   r   r   r?   r   r   r   r#   r     s    
&&r   )Nre   )NN)r   r   NNN)r   r   r   NN)r   )r   )>Z
__future__r   r   r   collections.abcr   ImportErrorcollectionsr   r.   r   Znumpyr   ZnumbanbZtoolzr`   Zxarrayr^   Z
dask.arrayr   r   Z	PIL.Imager	   Zdatashader.colorsr
   r   Zdatashader.compositer   r   r   Zdatashader.utilsr   r   r   r   	Exception__all__r   r   objectr;   r   ry   r   r   r   r   r   r   r   Zmemoizer   r   r   r   r   r   r   Zjitr   r   r   r   r   r#   <module>   sl   
&*
.ex

g

I



6