a
    Dfg                     @   sV  d dl Z d dlmZ d dlmZ d dlZd dlZddlm	Z	m
Z
mZmZmZmZ ddlmZmZ ddlmZ ddlmZ dd	lmZ dd
lmZmZ ddlmZ ddlmZ ddlm Z  ddl!m"Z" ddlm#Z#m$Z$ G dd deZ%G dd dee	e%eZ&G dd de&Z'G dd de&Z(G dd de(Z)G dd dee	eZ*G dd dee	eZ+dS )    N)deepcopy)
itemgetter   )Dataset	Dimension	Element2DOverlayconfigutil)BoundingBoxBoundingRegion)ImageInterface)	DataErrordimension_name)SheetCoordinateSystemSlice   )Curve)Selection2DExpr)TriMesh)Table)categorical_aggregate2dcompute_slice_boundsc                       s   e Zd ZdZejededgddddZejd ddZ	ejed	gd
ddZ
d" fdd	Zdd Zd# fdd	Zd$ fdd	Zd%ddZd&ddZedd Zedd Zdd Zd d! Z  ZS )'Rastera  
    Raster is a basic 2D element type for presenting either numpy or
    dask arrays as two dimensional raster images.

    Arrays with a shape of (N,M) are valid inputs for Raster whereas
    subclasses of Raster (e.g. RGB) may also accept 3D arrays
    containing channel information.

    Raster does not support slicing like the Image or RGB subclasses
    and the extents are in matrix coordinates if not explicitly
    specified.
    xyr   r   Tzk
        The label of the x- and y-dimension of the Raster in form
        of a string or dimension object.defaultboundsconstantdocr   r!   zr   NB
        The dimension description of the data held in the matrix.r   r    r"   Nc                    sj   |d u st |tr$|g kr$td}|d u rJ|jd d \}}dd||f}t j|f|||d| d S )Nr   r   r   r   kdimsvdimsextents)
isinstancelistnpzerosshapesuper__init__)selfdatar*   r+   r,   paramsd1Zd2	__class__ e/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/holoviews/element/raster.pyr3   .   s    
zRaster.__init__c                 C   s  ||   v r| |S t| |}t|ts:|td f}ndt|d| j kr`t	dd | j n>t|dkr|d | j
d jtd fvrt	| j
d jddd |d d D }| j|d d d d d }t|r| j|d d	S t|s|S | jtj||d
dd d	S d S )Nr   zCan only slice %d dimensions   r   z' is the only selectable value dimensionc                 S   s   g | ]}t |tqS r:   r-   slice).0slr:   r:   r;   
<listcomp>@       z&Raster.__getitem__.<locals>.<listcomp>)r,   TZaxis)
dimensionsdimension_valuesr
   Zprocess_ellipsesr-   tupler?   lendepthKeyErrorr+   namer5   __getitem__allcloneanyr/   Zexpand_dimsindex)r4   ZslicesZ	slc_typesr5   r:   r:   r;   rL   6   s"    
(zRaster.__getitem__c                    s   |  |}|rr|dkrr| |}| jjdkr8tjtjfS t| jt| j }}|s^||fS t	|||j
|jS t 
|||S )Nr   r   )get_dimension_indexget_dimensionr5   sizer/   nanZnanminZnanmaxr
   dimension_rangerangeZ
soft_ranger2   )r4   dim
data_rangerU   idx	dimensionlowerupperr8   r:   r;   rV   J   s    

zRaster.rangec                    s   |  |}|s,|dkr,tt| jjd S |sN|dkrNtt| jjd S |dv rtjd| jjd d| jjd f | }|r| S |S |dkr| jj}|r| S |S t	 
|S dS )zL
        The set of samples available along a particular dimension.
        r   r   r   r   r   N)rQ   r/   arrayrV   r5   r1   ZmgridflattenTr2   rF   )r4   rW   expandedflatZdim_idxvaluesarrr8   r:   r;   rF   V   s    
*zRaster.dimension_valuesc                    s  |du rg }t |tr(|\}}t||}tjjddjd}t|jksVt|rt|stdd t	fdd|
 D D  }fdd|D }j|d	< t|fi |S tt|
 \}}	t |	trtd
|  du rtd| d fddtjD }
dd tjD } s4dd ndd }||	t d  | < |
d jd}jt|ddd  }tdr dkr|ddd }tt||}|
|d	< t|fi |S dS )a[  
        Sample the Raster along one or both of its dimensions,
        returning a reduced dimensionality type, which is either
        a ItemTable, Curve or Scatter. If two dimension samples
        and a new_xaxis is provided the sample will be the value
        of the sampled unit indexed by the value in the new_xaxis
        tuple.
        NTZonlychangedr+   c                 S   s$   g | ]\}}t |tr|n|gqS r:   )r-   r.   )r@   _cr:   r:   r;   rB   {   rC   z!Raster.sample.<locals>.<listcomp>c                    s   g | ]\}}  ||fqS r:   )rQ   r@   kvr4   r:   r;   rB   |   rC   c                    s"   g | ]}| j  | f qS r:   )_zdata_coord2matrixr@   rh   rl   r:   r;   rB   ~   s   r*   zKRaster sampling requires coordinates not slices,use regular slicing syntax.z
Dimension z not found during samplingc                    s   g | ]\}}| kr|qS r:   r:   )r@   id)
sample_indr:   r;   rB      s   c                 S   s   g | ]}t d qS N)r?   r@   rp   r:   r:   r;   rB      rC   c                 S   s   | dfS Nr   r:   rk   r:   r:   r;   <lambda>   rC   zRaster.sample.<locals>.<lambda>c                 S   s   d| fS ru   r:   rv   r:   r:   r;   rw      rC   r   r   Fr=   r    )r-   rG   zipdictparamrc   r+   rH   ndimssorteditemsr*   r   nextiterr?   
ValueErrorrQ   	Exception	enumeraterV   rn   absrF   rK   rm   hasattrr.   r   )r4   Zsamplesr    Zsample_valuesXYr6   Z
table_datarZ   Zsample_coordother_dimensionsampleZcoord_fnx_valsZydatar5   r:   )rr   r4   r;   r   h   sL    	





$zRaster.samplec                    s  |  |||\}}t|| jkrHt|tjr<|j| jddS || jS n|d   fdd| jD }| 	|d }| 
|d jd}|| j|d}|rt| dr|ddd }t||}	tt| jjd	d
|| jd}
|
dd |
dd t|	fi |
S dS )z
        Reduces the Raster using functions provided via the
        kwargs, where the keyword is the dimension to be reduced.
        Optionally a label_prefix can be provided to prepend to
        the result Element label.
        NrD   r   c                    s   g | ]}|j  kr|qS r:   )rK   r@   rq   rZ   r:   r;   rB      rC   z!Raster.reduce.<locals>.<listcomp>Fr    r=   Tre   r*   r+   r,   )Z_reduce_maprH   r{   r-   r/   Zufuncreducer5   r*   rQ   rF   rK   rm   r   rx   ry   rz   rc   r+   popr   )r4   rE   functionZ
reduce_mapdimsr   Zoidxr   Zreducedr5   r6   r:   r   r;   r      s&    
zRaster.reducec                 C   s
   t | jS rs   )rH   r+   rl   r:   r:   r;   rI      s    zRaster.depthc                 C   s   | j S rs   r5   rl   r:   r:   r;   rm      s    zRaster._zdatac                 C   s    t t|d t t|d fS )Nr   r   )introundr4   Zcoordr:   r:   r;   rn      s    zRaster._coord2matrixc                 C   s   t | jjS rs   )r/   prodrm   r1   rl   r:   r:   r;   __len__   s    zRaster.__len__)NNN)TT)TT)NN)NN)__name__
__module____qualname____doc__rz   Listr   r*   Stringgroupr+   r3   rL   rV   rF   r   r   propertyrI   rm   rn   r   __classcell__r:   r:   r8   r;   r      s(   
5


r   c                       s   e Zd ZdZejee ddZej	g ddZ
ejd ddZej	eded	gd
dddZej	edgdddZejdddZdZd%ddZdd Zd& fdd	Zd' fdd	Zd(ddZd)dd Zd* fd!d"	Zd#d$ Z  ZS )+Imagea<  
    Image represents a regularly sampled 2D grid of an underlying
    continuous space of intensity values, which will be colormapped on
    plotting. The grid of intensity values may be specified as a NxM
    sized array of values along with a bounds, but it may also be
    defined through explicit and regularly spaced x/y-coordinate
    arrays of shape M and N respectively. The two most basic supported
    constructors of an Image therefore include:

        Image((X, Y, Z))

    where X is a 1D array of shape M, Y is a 1D array of shape N and
    Z is a 2D array of shape NxM, or equivalently:

        Image(Z, bounds=(x0, y0, x1, y1))

    where Z is a 2D array of shape NxM defining the intensity values
    and the bounds define the (left, bottom, top, right) edges of four
    corners of the grid. Other gridded formats which support declaring
    of explicit x/y-coordinate arrays such as xarray are also
    supported.

    Note that the interpretation of the orientation of the array
    changes depending on whether bounds or explicit coordinates are
    used.
    zF
        The bounding region in sheet coordinates containing the data.)class_r   r"   )ZgridZxarrayimageZcubeZ	dataframe
dictionary)r   Tr#   r   r   r   zo
        The label of the x- and y-dimension of the Raster in the form
        of a string or dimension object.r   r$   r%   r&   r'   Nz
        The tolerance used to enforce regular sampling for regular, gridded
        data where regular sampling is expected. Expressed as the maximal
        allowable sampling difference between sample locations.)r   r"   r   c	                 K   s*  |}
t |tr:|p|j}|p |j}|p*|j}|d u r:|j}|rB|nd}|d u svt |ttfr`|rvt |tj	r|j
dkrt |tj	r|jdkr|ntd}d}|sd}|sd}n4t |tj	r|j| jk rtdt| j| j|jf |d ur||	d< n
tj|	d< tj| |f|||d|	 | jjs@td	t| j| jj| jj| d
dd d \}}|d u r| dd}t||| j\}}}}| dd}t||| j\}}}}t||f||ffd}nNt|rt|d}n6t |tttj	fr|\}}}}t||f||ffd}d }| jt u r@t |tj	tfs@| j! }t"dd |! D }|rtdd}|r|t#|r||nd}|rt#|r|nd}nH|! \}}}}|r|nt$|||| j}|r|nt$|||| j}t%| ||| |rttj&tj&ftj&tj&ffd| _| '||
 d S )N)NNNNr   r   r(   r   z/%s type expects %d-D array received %d-D array.rtolr)   z{} type expects gridded data, {} is columnar. To display columnar data as gridded use the HeatMap element or aggregate the data (e.g. using rasterize or np.histogram2d).TgriddedF)Zpoints)Zradiusc                 s   s   | ]}t | V  qd S rs   )r
   isfinite)r@   rk   r:   r:   r;   	<genexpr>/  rC   z!Image.__init__.<locals>.<genexpr>)r(   r(   )(r-   r   r    xdensityydensityr   r.   rG   r/   ndarrayrS   ndimr0   _ndimr   typer   r	   Z
image_rtolr   r3   	interfacer   r   formatr1   rF   r
   Zbound_rangeZ
_time_unitr   isscalarr   lbrtrM   r   Zcompute_densityr   rT   	_validate)r4   r5   r*   r+   r    r,   r   r   r   r6   supplied_boundsZdim2Zdim1xvalslrrg   yvalsbtdata_boundsZ
non_finiter:   r:   r;   r3      sv    




$






 zImage.__init__c                    sx  t  jdkr: jd t  jkr:td jt  jf t j} j\}} fdd jD \}}g }|jdkr~|| |jdkr|| |rt |dkrdj	t
| n|d  }	t| d	|	 d
t| j}
t| j}d}d }	|
s|r
d| dnd| d| d}	n|s0d| d}	|	rP j|j	||	 jd |sZd S |d u r fdd jD \\}}\}}d j d }d j d }t|tjs|| ||  }}t|tjs|| ||  }}||||f}n|}d}t| j D ]^\}}t|tjr&t|}t|tjr>t|}t|rtj|| jdsd}q|rttdd S )Nr<   r   z8Input array has shape %r but %d value dimensions definedc                 3   s   | ]} j |d d dV  qdS )F)ra   rb   NrF   r   rl   r:   r;   r   F  s   z"Image._validate.<locals>.<genexpr>r   z	{} and {}r   z  coordinates must be 1D arrays, z dimension(s) were found to have multiple dimensions. Either supply 1D arrays or use the QuadMesh element for curvilinear coordinates.z{clsname} dimension{dims} not evenly sampled to relative tolerance of {rtol}. Please use the QuadMesh element for irregularly sampled data or set a higher tolerance on hv.config.image_rtol or the rtol parameter in the {clsname} constructor. z is z(s) z and z arez is)clsnamer   r   c                 3   s   | ]} j  |jV  qd S rs   )r   rV   rK   r@   Zkdrl   r:   r;   r   i  rC   g      ?g       @F)r   TzSupplied Image bounds do not match the coordinates defined in the data. Bounds only have to be declared if no coordinates are supplied, otherwise they must match the data. To change the displayed extents set the range on the x- and y-dimensions.)rH   r1   r+   r   r   r   r*   r   appendr   rG   r
   Zvalidate_regular_samplingr   rz   warningr   r   r-   Zdatetime_typesrx   r    r   Z	dt_to_intr   r/   isclose)r4   r   r   r   xdimydimr   r   invalidr   ZxvalidZyvalidmsgx0x1y0y1ZxstepZystepr    Z	not_closer   rh   r:   rl   r;   r   =  sl    







$
&
 

 zImage._validatec                    s^   |du r>|du st |tr>t| j| j| jd}t|fi |}t j||||g|R i |S )aK  
        Returns a clone of the object with matching parameter values
        containing the specified args and kwargs.

        If shared_data is set to True and no data explicitly supplied,
        the clone will share data with the original. May also supply
        a new_type, which will inherit all shared parameters.
        Nr    r   r   )
issubclassr   ry   r    r   r   r2   rN   )r4   r5   Zshared_datanew_typelinkargsZ	overridesZsheet_paramsr8   r:   r;   rN     s    

zImage.clonec                    s<   t  j|||fi |}t|tr8t| jdkr8t|S |S )Nr   )r2   	aggregater-   r   rH   r+   r   )r4   rE   r   ZspreadfnkwargsZaggr8   r:   r;   r     s    zImage.aggregatec                    s  |rt fdd|D sS fdd  D  t fddjD }jjdd}t dd	 |D rt||d
d }j\}}| \}}	}
}t|\}}}}|d | |d |  }}t	||t	||f d}n4
|d |d \}}|d | d }||f d}ttjjgj }j }|st|rT|S t|trn|jd
 }j|g t|dS j|j|j|dS d
S )a  
        Allows selecting data by the slices, sets and scalar values
        along a particular dimension. The indices should be supplied as
        keywords mapping between the selected dimension and
        value. Additionally selection_specs (taking the form of a list
        of type.group.label strings, types or functions) may be
        supplied, which will ensure the selection is only applied if the
        specs match the selected object.
        c                 3   s   | ]}  |V  qd S rs   )matches)r@   sprl   r:   r;   r     rC   zImage.select.<locals>.<genexpr>c                    s:   i | ]2\}}| j v r |jt|tr2t| n|qS r:   )r*   rR   rK   r-   rG   r?   )r@   rj   selrl   r:   r;   
<dictcomp>  s   z Image.select.<locals>.<dictcomp>c                 3   s*   | ]"}|j  v r |j  ntd V  qd S rs   )rK   r?   r   )	selectionr:   r;   r     s   Tr   c                 S   s   g | ]}t |tqS r:   r>   r@   elr:   r:   r;   rB     rC   z Image.select.<locals>.<listcomp>Nr   r   r   F)r*   r   datatype)r   r   r   r    )rO   r}   rG   r*   r   r1   r   r   r   r?   sheet2matrixidxr.   r
   Zunique_iteratorr   Zndlocr/   r   r-   r{   rN   r   r   r   )r4   Zselection_specsr   coordsr1   r    r   r   r   r   r   r   r   r   r   r   Zslicedr   r   r   r5   r:   )r   r4   r;   select  sD    



zImage.selectc                    s*  |du rg }|r|rt d|rg }g  | D ]\}}|t|rh|r\d|fn|df nZt|trfdd|D }t|dt|fvrt dn t|rfddt	||D }  q0nddg t
t   t|dkr|d }t|tr j| S  fd	d|D S dS )
z
        Given a single coordinate or multiple coordinates as
        a tuple or list of tuples or keyword arguments matching
        the dimension closest will find the closest actual x/y
        coordinates.
        NzDSpecify coordinate using as either a list keyword arguments not bothr   c                    s    g | ]} rd |fn|d fqS r   r:   ro   rY   r:   r;   rB     rC   z!Image.closest.<locals>.<listcomp>zLength of samples must matchc                    s<   g | ]4\}} r$|t  d   |fn||t  d   fqS )r   )r   )r@   rh   r   r   r:   r;   rB     s   r   c                    s   g | ]} j | qS r:   )closest_cell_centerr   )getterr4   r:   r;   rB     rC   )r   r}   rQ   r/   r   r   r-   r.   rH   rx   r   r|   rG   r   )r4   r   r   rj   rk   r:   )r   rY   r4   r;   closest  s6    




zImage.closestc           
         sd   |  |}| |}|dv rP|rP|jdkrP| j \}}}}	|rH||	fS ||fS t |||S d S )Nr]   )NN)rQ   rR   rV   r    r   r2   )
r4   rW   rX   rU   rY   rZ   r   r   r   r   r8   r:   r;   rV     s    

zImage.rangec                 C   s
   | j | S rs   )r   r   r:   r:   r;   rn     s    zImage._coord2matrix)NNNNNNN)NTNT)NNN)N)N)TT)r   r   r   r   rz   ClassSelectorr   r   r    r   r   r   r   r   r*   r+   Numberr   r   r3   r   rN   r   r   r   rV   rn   r   r:   r:   r8   r;   r      s,     
@E
2
'	r   c                       sH   e Zd ZdZejddZejd ddZdZ	de
iZd fd	d
	Z  ZS )
ImageStacka  
    ImageStack expands the capabilities of Image to by supporting
    multiple layers of images.

    As there is many ways to represent multiple layers of images,
    the following options are supported:

        1) A 3D Numpy array with the shape (y, x, level)
        2) A list of 2D Numpy arrays with identical shape (y, x)
        3) A dictionary where the keys will be set as the vdims and the
            values are 2D Numpy arrays with identical shapes (y, x).
            If the dictionary's keys matches the kdims of the element,
            they need to be 1D arrays.
        4) A tuple containing (x, y, level_0, level_1, ...),
            where the level is a 2D Numpy array in the shape of (y, x).
        5) An xarray DataArray or Dataset where its `coords` contain the kdims.

    If no kdims are supplied, x and y are used.

    If no vdims are supplied, and the naming can be inferred like with a dictionary
    the levels will be named level_0, level_1, etc.
    r&   )r"   Tr#   r<   r   Nc                    s  |p| j  ttrVtrVtd jd }td jd }||gR nDttrt fdd	 D }t
t \}}	|vrt|jd |< |	vrt|jd |	< nttjr(jdkr(tjd }tjd }fddtjd D }
||g|
R nrttrtdkrtd tjrd jdkrfddtd jd D }
d d g|
R |d u rttrd	d
 ttdd  D }n"ttr fdd
 D }t jf||d| d S )Nr   r   c                 3   s   | ]\}}| vr|V  qd S rs   r:   ri   _kdimsr:   r;   r   %  rC   z&ImageStack.__init__.<locals>.<genexpr>r<   c                 3   s$   | ]} d d d d |f V  qd S rs   r:   r@   nr   r:   r;   r   .  rC   r   c                 3   s(   | ] } d  dddd|f V  qdS )r   Nr:   r   r   r:   r;   r   4  rC   c                 S   s   g | ]}t d | qS )Zlevel_r   rt   r:   r:   r;   rB   9  rC   z'ImageStack.__init__.<locals>.<listcomp>c                    s   g | ]}| vrt |qS r:   r   )r@   keyr   r:   r;   rB   ;  rC   r   )r*   r-   r.   rH   r/   aranger1   ry   r~   r}   mapstrr   r   rV   rG   keysr2   r3   )r4   r5   r*   r+   r6   r   r   firstr   r   rd   r8   )r   r5   r;   r3     sB    


 
 zImageStack.__init__)NN)r   r   r   r   rz   r   r+   r   r   r   r   _vdim_reductionsr3   r   r:   r:   r8   r;   r     s   r   c                       s   e Zd ZdZejd ddZejedddeddd	Z	ej
ed
ddedddedddgdddZdZdeiZedd ZedddZd fdd	Z  ZS )RGBak  
    RGB represents a regularly spaced 2D grid of an underlying
    continuous space of RGB(A) (red, green, blue and alpha) color
    space values. The definition of the grid closely matches the
    semantics of an Image and in the simplest case the grid may be
    specified as a NxMx3 or NxMx4 array of values along with a bounds,
    but it may also be defined through explicit and regularly spaced
    x/y-coordinate arrays. The two most basic supported constructors
    of an RGB element therefore include:

        RGB((X, Y, R, G, B))

    where X is a 1D array of shape M, Y is a 1D array of shape N and
    R/G/B are 2D array of shape NxM, or equivalently:

        RGB(Z, bounds=(x0, y0, x1, y1))

    where Z is a 3D array of stacked R/G/B arrays with shape NxMx3/4
    and the bounds define the (left, bottom, top, right) edges of the
    four corners of the grid. Other gridded formats which support
    declaring of explicit x/y-coordinate arrays such as xarray are
    also supported.

    Note that the interpretation of the orientation changes depending
    on whether bounds or explicit coordinates are used.
    Tr#   Ar]   rV   Fl
        The alpha dimension definition to add the value dimensions if
        an alpha channel is supplied.r   r   Zinstantiater"   RGBr<      z
        The dimension description of the data held in the matrix.

        If an alpha channel is supplied, the defined alpha_dimension
        is automatically appended to this list.r'   r<   r   c                 C   s   | S )z
        Returns the corresponding RGB element.

        Other than the updating parameter definitions, this is the
        only change needed to implemented an arbitrary colorspace as a
        subclass of RGB.
        r:   rl   r:   r:   r;   rgbm  s    	zRGB.rgbNc                 K   s   zddl m} W n ty,   tddY n0 t|d(}t||}	|	d }	W d   n1 sf0    Y  |rx|	S |	j\}
}}|du rt||
 }|| d |
| d  }}| | ||f}| |	fd|i|}|r|jddd	 |S )
aC  Load an image from a file and return an RGB element or array

        Args:
            filename: Filename of the image to be loaded
            height: Determines the bounds of the image where the width
                    is scaled relative to the aspect ratio of the image.
            array: Whether to return an array (rather than RGB default)
            bounds: Bounds for the returned RGB (overrides height)
            bare: Whether to hide the axes
            kwargs: Additional kwargs to the RGB constructor

        Returns:
            RGB element or array
        r   )r   z(RGB.load_image requires PIL (or Pillow).Nrbg     o@r   r    )ZxaxisZyaxis)	ZPILr   ImportErroropenr/   r^   r1   floatopts)clsfilenameheightr^   r    Zbarer   r   fr5   hwrg   ZxoffsetZyoffsetr   r:   r:   r;   
load_imagex  s$    &zRGB.load_imagec           	         s  t |tr| }tdd |D s,tddd |D  t fdd D sXtddd |D }td	d |D rtd
dd t||D }t|}|d u rt	| j
}nt |trt|n|g}| j}t|dr|jd dkrt|dksrt |trFt |d tjrF|d jdkrF|d jd dkrFt|dksrt |tr|tdd |D |jf |v r||| t j|f||d| d S )Nc                 s   s   | ]}t |tV  qd S rs   )r-   r   r@   imr:   r:   r;   r     rC   zRGB.__init__.<locals>.<genexpr>z.Input overlay must only contain Image elementsc                 S   s   g | ]}|j jqS r:   )r5   r1   r  r:   r:   r;   rB     rC   z RGB.__init__.<locals>.<listcomp>c                 3   s   | ]}| d  kV  qdS )r   Nr:   )r@   r1   Zshapesr:   r;   r     rC   zFImages in the input overlays must contain data of the consistent shapec                 S   s   g | ]}|j d  jqS r   )r+   rV   r  r:   r:   r;   rB     rC   c                 s   s   | ]}d |v V  qd S rs   r:   )r@   r   r:   r:   r;   r     rC   zDRanges must be defined on all the value dimensions of all the Imagesc                 S   s.   g | ]&\}}|j |d   |d |d    qS r]   r   )r@   r   r	  r:   r:   r;   rB     rC   r1   r=   r   r<   c                 s   s   | ]}t |V  qd S rs   r   )r@   vdr:   r:   r;   r     rC   r   )r-   r   rc   rM   r   rO   rx   r/   Zdstackr   r+   r.   alpha_dimensionr   r1   rH   rG   r   r   ry   rK   r   r2   r3   )	r4   r5   r*   r+   r6   ZimagesrangesZarraysalphar8   r
  r;   r3     sB    

&

zRGB.__init__)r   FNF)NN)r   r   r   r   rz   r   r   r   r   r  r   r+   r   r   r   r   r   classmethodr  r3   r   r:   r:   r8   r;   r   ?  s$   


%r   c                   @   s   e Zd ZdZejd ddZejedddeddd	Z	ej
ed
dddedddedddgdddZeejZedd ZdS )HSVav  
    HSV represents a regularly spaced 2D grid of an underlying
    continuous space of HSV (hue, saturation and value) color space
    values. The definition of the grid closely matches the semantics
    of an Image or RGB element and in the simplest case the grid may
    be specified as a NxMx3 or NxMx4 array of values along with a
    bounds, but it may also be defined through explicit and regularly
    spaced x/y-coordinate arrays. The two most basic supported
    constructors of an HSV element therefore include:

        HSV((X, Y, H, S, V))

    where X is a 1D array of shape M, Y is a 1D array of shape N and
    H/S/V are 2D array of shape NxM, or equivalently:

        HSV(Z, bounds=(x0, y0, x1, y1))

    where Z is a 3D array of stacked H/S/V arrays with shape NxMx3/4
    and the bounds define the (left, bottom, top, right) edges of the
    four corners of the grid. Other gridded formats which support
    declaring of explicit x/y-coordinate arrays such as xarray are
    also supported.

    Note that the interpretation of the orientation changes depending
    on whether bounds or explicit coordinates are used.
    Tr#   r   r]   r   Fr   r   H)rV   ZcyclicSVr   z
        The dimension description of the data held in the array.

        If an alpha channel is supplied, the defined alpha_dimension
        is automatically appended to this list.r'   c                    s   t  fdd jD } fdd jD } j|dd  }t jdkrZ||d f7 }t }|d= t|| f j j	 j
d	|S )
z-
        Conversion from HSV to RGB.
        c                 3   s   | ]} j |d dV  qdS )F)ra   Nr   r   rl   r:   r;   r     s   zHSV.rgb.<locals>.<genexpr>c                    s   g | ]} j |d dqS )Frb   r   r   rl   r:   r;   rB     s   zHSV.rgb.<locals>.<listcomp>Nr<   r   r+   r   )rG   r*   r+   
hsv_to_rgbrH   r
   get_param_valuesr   r    r   r   )r4   r   r5   Zhsvr6   r:   rl   r;   r     s     

zHSV.rgbN)r   r   r   r   rz   r   r   r   r   r  r   r+   r/   Z	vectorizecolorsysr  r   r   r:   r:   r:   r;   r    s   

	r  c                       sl   e Zd ZdZejd ddZejededgdddZ	ejedgd	d
Z
dZd fdd	Zdd Z  ZS )QuadMeshak  
    A QuadMesh represents 2D rectangular grid expressed as x- and
    y-coordinates defined as 1D or 2D arrays. Unlike the Image type
    a QuadMesh may be regularly or irregularly spaced and contain
    either bin edges or bin centers. If bin edges are supplied the
    shape of the x/y-coordinate arrays should be one greater than the
    shape of the value array.

    The default interface expects data to be specified in the form:

        QuadMesh((X, Y, Z))

    where X and Y may be 1D or 2D arrays of the shape N(+1) and M(+1)
    respectively or N(+1)xM(+1) and the Z value array should be of
    shape NxM. Other gridded formats such as xarray are also supported
    if installed.

    The grid orientation follows the standard matrix convention: An
    array Z with shape (nrows, ncolumns) is plotted with the column
    number as X and the row number as Y.
    Tr#   r   r   r   r   r    r!   r$   r%   )r   r    Nc                    sh   |d u st |tr*|g kr*g g tdf}t j|||fi | | jjsdtd	t
| j| jjd S )Nr(   z{} type expects gridded data, {} is columnar. To display columnar data as gridded use the HeatMap element or aggregate the data (e.g. using np.histogram2d).)r-   r.   r/   r0   r2   r3   r   r   r   r   r   r   r4   r5   r*   r+   r6   r8   r:   r;   r3     s    zQuadMesh.__init__c                 C   s,  | j j| ddd}| j j| ddd}|jdkrt|dd |dd k rZ|ddd }t|dd |dd k r|ddd }t|ddtjf t|jt|ddtjf t| }}|j	 |j	 f}| j
ddd	j}|d }tt|}|| }||d  ||  }|d }	|d |d  ||  }
|	}|
}|
d }t||g}t|	|g}t|
|g}|||f}| jD ]$}| 
|}|t||gf }qt| }|d
 tjjdd  |d
< tj|tt|d f fi dd | D }t|f|ffi dd | D S )z5
        Converts a QuadMesh into a TriMesh.
        r   T)edgesr   Nr=   r   Fr  r*   c                 S   s   i | ]\}}|d kr||qS rf   r:   ri   r:   r:   r;   r   P  s   z$QuadMesh.trimesh.<locals>.<dictcomp>c                 S   s   i | ]\}}|d kr||qS )r*   r:   ri   r:   r:   r;   r   R  s   )r   r   r   r/   rM   ZtileZnewaxisrH   r`   r_   rF   r1   r   r   Zconcatenater+   r
   r  r   Z	node_typer*   r}   )r4   xsZysZverticesr1   s0t1ZjsZt1sZt2sZt3sZt4sZt5sZt6st2t3tsr  Zzsr6   nodesr:   r:   r;   trimesh(  sD    




zQuadMesh.trimesh)NN)r   r   r   r   rz   r   r   r   r   r*   r+   _binnedr3   r#  r   r:   r:   r8   r;   r    s   
r  c                       s   e Zd ZdZejd ddZejededgdddZ	ejedgddZ
d fd
d	Zedd Zedd Zd fdd	Z  ZS )HeatMapa  
    HeatMap represents a 2D grid of categorical coordinates which can
    be computed from a sparse tabular representation. A HeatMap does
    not automatically aggregate the supplied values, so if the data
    contains multiple entries for the same coordinate on the 2D grid
    it should be aggregated using the aggregate method before display.

    The HeatMap constructor will support any tabular or gridded data
    format with 2 coordinates and at least one value dimension. A
    simple example:

        HeatMap([(x1, y1, z1), (x2, y2, z2), ...])

    However any tabular and gridded format, including pandas
    DataFrames, dictionaries of columns, xarray DataArrays and more
    are supported if the library is importable.
    Tr#   r   r   r   r  r$   Nc                    s$   t  j|f||d| d | _d S )Nr   )r2   r3   _griddedr  r8   r:   r;   r3   q  s    zHeatMap.__init__c                 C   s   | j d u rt| | _ | j S rs   )r&  r   rl   r:   r:   r;   r   u  s    

zHeatMap.griddedc                 C   s   | j jdkS )z3
        Reports if the Dataset is unique.
        z
non-unique)r   labelrl   r:   r:   r;   _unique{  s    zHeatMap._uniquec                    s   |  |}|| jv rzfzBd| j_| j| u rFt |||W W d| j_S | j|||}W n tyn   d}Y n0 W d| j_n
d| j_0 |dur|S t |||S )a  Return the lower and upper bounds of values along dimension.

        Args:
            dimension: The dimension to compute the range on.
            data_range (bool): Compute range from data values
            dimension_range (bool): Include Dimension ranges
                Whether to include Dimension range and soft_range
                in range calculation

        Returns:
            Tuple containing the lower and upper bound
        TFN)rR   r*   r   r$  r2   rV   r   )r4   rW   rX   rU   Zdranger8   r:   r;   rV     s    


zHeatMap.range)NN)TT)r   r   r   r   rz   r   r   r   r   r*   r+   r3   r   r   r(  rV   r   r:   r:   r8   r;   r%  W  s   

r%  ),r  copyr   operatorr   numpyr/   rz   corer   r   r   r   r	   r
   Zcore.boundingregionr   r   Z	core.datar   Zcore.data.interfacer   Zcore.dimensionr   Zcore.sheetcoordsr   r   Zchartr   Zgeomr   Zgraphsr   Ztabularr   r   r   r   r   r   r   r  r  r%  r:   r:   r:   r;   <module>   s2     6  5B|CY