a
    Df                     @   s.  d 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
mZmZmZmZmZmZmZ ddlmZmZmZmZ ddlmZ dd	lmZmZmZmZmZmZm Z m!Z!m"Z" dd
l#m$Z$m%Z% ddl&m'Z'm(Z( ddl)m*Z*m+Z+ ddl,m-Z- ddl.m/Z/ ddl0m1Z1 eeegZ2dd Z3G dd deZ4G dd deZ5G dd deZ6G dd deZ7G dd dej8Z9G dd d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Z>G d&d' d'eZ?G d(d) d)eZ@G d*d+ d+eZAG d,d- d-eZBG d.d/ d/eZCG d0d1 d1eZDG d2d3 d3ej8ZEdS )4zF
Collection of either extremely generic or simple Operation
examples.
    N)Version)
_is_number   )	CollatorDataset	DimensionElement
GridMatrixHoloMap	NdOverlay	OperationOverlay)ArrayInterfaceDictInterfacePandasInterfacedefault_datatype)dask_array_module)	datetime_types	dt_to_intgroup_sanitizeris_cupy_arrayis_dask_arrayis_ibis_expr
isdatetimeisfinitelabel_sanitizer)	HistogramScatter)ContoursPolygons)RGBImage)categorical_aggregate2d)RangeXY)MaxNLocatorc                 C   s   | S N )xkr&   r&   h/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/holoviews/operation/element.pyidentity.       r*   c                   @   sD   e Zd ZdZejdddZejdddZej	e
ddZd
dd	ZdS )	operationa+  
    The most generic operation that wraps any callable into an
    Operation. The callable needs to accept an HoloViews
    component and a key (that may be ignored) and must return a new
    HoloViews component.

    This class may be useful for turning a HoloViews method into an
    operation to define as compositor operation. For instance, the
    following definition:

    operation.instance(op=lambda x, k: x.collapse(np.subtract))

    Could be used to implement a collapse operation to subtracts the
    data between Rasters in an Overlay.
    Na  
       The output element type which may be None to disable type
       checking.

       May be used to declare useful information to other code in
       HoloViews, e.g. required for tab-completion support of operations
       registered with compositors.defaultdocr   zT
        The group assigned to the result after having applied the
        operator.z
        The operation used to generate a new HoloViews object returned
        by the operation. By default, the identity operation is
        applied.c                 C   s@   | j ||}| j jd ur0t|| j js0J d|j| j jdS )Nz5Return value does not match the declared output type.group)popoutput_type
isinstanceZrelabelr1   )selfviewkeyretvalr&   r&   r)   _processR   s    zoperation._process)N)__name__
__module____qualname____doc__param	Parameterr4   Stringr1   Callabler*   r3   r:   r&   r&   r&   r)   r,   0   s
   r,   c                   @   sD   e Zd ZdZejeddZejg ddZ	ej
i ddZd	ddZdS )
factoryz
    Simple operation that constructs any element that accepts some
    other element as input. For instance, RGB and HSV elements can be
    created from overlays of Image elements.
    z
        The output type of the factor operation.

        By default, if three overlaid Images elements are supplied,
        the corresponding RGB element will be returned. r-   z?
        The list of positional argument to pass to the factoryz=
        The dict of keyword arguments to pass to the factoryNc                 C   s"   | j j|g| j jR i | j jS r%   )r2   r4   argskwargs)r6   r7   r8   r&   r&   r)   r:   m   s    zfactory._process)N)r;   r<   r=   r>   r?   r@   r    r4   ListrD   DictrE   r:   r&   r&   r&   r)   rC   Z   s
   rC   c                   @   s`   e Zd ZejeddZejeddZejdd ddZ	ej
g ddZeji d	dZdddZd
S )function0
        The output type of the method operationclass_r/   1
        The object type the method is defined onc                 O   s   | S r%   r&   )elrD   rE   r&   r&   r)   <lambda>y   r+   zfunction.<lambda>z
        The function to apply.r-   >
        The list of positional argument to pass to the method<
        The dict of keyword arguments to pass to the methodNc                 C   s"   | j j|g| j jR i | j jS r%   )r2   fnrD   rE   r6   elementr8   r&   r&   r)   r:      s    zfunction._process)N)r;   r<   r=   r?   ClassSelectortyper4   
input_typerB   rQ   rF   rD   rG   rE   r:   r&   r&   r&   r)   rH   q   s   rH   c                   @   s`   e Zd ZdZejeddZejeddZej	dddZ
ejg ddZeji d	dZdddZd
S )methodz,
    Operation that wraps a method call
    rI   rJ   rL   __call__z
        The method namer-   rO   rP   Nc                 C   s0   t | jj| jj}||g| jjR i | jjS r%   )getattrr2   rV   method_namerD   rE   )r6   rS   r8   rQ   r&   r&   r)   r:      s    zmethod._process)N)r;   r<   r=   r>   r?   rT   rU   r4   rV   rA   rZ   rF   rD   rG   rE   r:   r&   r&   r&   r)   rW      s   rW   c                   @   s>   e Zd ZdZejdd dZejddZdddZd	d
 Z	dS )
apply_whena  
    Applies a selection depending on the current zoom range. If the
    supplied predicate function returns a True it will apply the
    operation otherwise it will return the raw element after the
    selection. For example the following will apply datashading if
    the number of points in the current viewport exceed 1000 otherwise
    just returning the selected points element:

       apply_when(points, operation=datashade, predicate=lambda x: x > 1000)
    c                 C   s   | S r%   r&   r'   r&   r&   r)   rN      r+   zapply_when.<lambda>r.   NFc                 C   sb   |}|d ur |d ur |||f }|  |}|s2|s:|r>|s>|S |jjrP|g S |jd d S d S Nr   )	predicate	interfaceZgriddedcloneiloc)r6   rS   x_rangey_rangeinvertselected	conditionr&   r&   r)   _apply   s    

zapply_when._applyc                 K   sl   d|v r| d}nt g}| jjf i | | js<td| |j| j|d}|j| j|dd}|| S )NstreamszpMust provide a predicate function to determine when to apply the operation and when to return the selected data.)ri   T)ri   re   )	popr#   r?   updater_   
ValueErrorr,   applyrh   )r6   objparamsri   Zappliedrawr&   r&   r)   rX      s    zapply_when.__call__)F)
r;   r<   r=   r>   r?   rB   r,   r_   rh   rX   r&   r&   r&   r)   r[      s
   
r[   c                   @   sP   e Zd ZdZejeddZejdddZ	ej
g eddZdd	d
ZdddZdS )chainae  
    Defining an Operation chain is an easy way to define a new
    Operation from a series of existing ones. The argument is a
    list of Operation (or Operation instances) that are
    called in sequence to generate the returned element.

    chain(operations=[gradient, threshold.instance(level=2)])

    This operation can accept an Image instance and would first
    compute the gradient before thresholding the result at a level of
    2.0.

    Instances are only required when arguments need to be passed to
    individual operations so the resulting object is a function over a
    single argument.
    z|
        The output type of the chain operation. Must be supplied if
        the chain is to be used as a channel operation.r-    z
        The group assigned to the result after having applied the chain.
        Defaults to the group produced by the last operation in the chainzn
       A list of Operations (or Operation instances)
       that are applied on the input from left to right.)r.   Z	item_typer/   Nc                 C   sF   |}| j jD ]}|j||| j jd}q| j js2|S |j| j jdS d S )N)input_rangesr0   )r2   
operationsZprocess_elementrs   r1   ra   )r6   r7   r8   	processedr,   r&   r&   r)   r:      s    
zchain._processTc                 C   s>   d}| j ddd D ]$}t||r*|} q:|js|r q:q|S )z
        Returns the first found occurrence of an operation while
        performing a backward traversal of the chain pipeline.
        N)rt   r5   link_inputs)r6   r,   Zskip_nonlinkedfoundr3   r&   r&   r)   find   s    

z
chain.find)N)T)r;   r<   r=   r>   r?   r@   r!   r4   rA   r1   rF   r   rt   r:   ry   r&   r&   r&   r)   rq      s   
rq   c                   @   s8   e Zd ZdZeZejdddZej	ddZ
d
dd	ZdS )	transforma}  
    Generic Operation to transform an input Image or RGBA
    element into an output Image. The transformation is defined by
    the supplied callable that accepts the data of the input Image
    (typically a numpy array) and returns the transformed data of the
    output Image.

    This operator is extremely versatile; for instance, you could
    implement an alternative to the explicit threshold operator with:

    operator=lambda x: np.clip(x, 0, 0.5)

    Alternatively, you can implement a transform computing the 2D
    autocorrelation using the scipy library with:

    operator=lambda x: scipy.signal.correlate2d(x, x)
    	TransformzO
        The group assigned to the result after applying the
        transform.r-   z
       Function of one argument that transforms the data in the input
       Image to the data in the output Image. By default, acts as
       the identity function such that the output matches the input.r/   Nc                 C   s.   | j js|jn| j |j}|j|| j jdS )Nr0   )r2   operatordatara   r1   )r6   imgr8   ru   r&   r&   r)   r:   #  s    ztransform._process)N)r;   r<   r=   r>   r!   r4   r?   rA   r1   rB   r}   r:   r&   r&   r&   r)   rz     s
   rz   c                   @   sf   e Zd ZdZeZejddZej	ddZ
ejdddZejd	d
dZedd Zdd ZdddZdS )image_overlaya  
    Operation to build a overlay of images to a specification from a
    subset of the required elements.

    This is useful for reordering the elements of an overlay,
    duplicating layers of an overlay or creating blank image elements
    in the appropriate positions.

    For instance, image_overlay may build a three layered input
    suitable for the RGB factory operation even if supplied with one
    or two of the required channels (creating blank channels for the
    missing elements).

    Note that if there is any ambiguity regarding the match, the
    strongest match will be used. In the case of a tie in match
    strength, the first layer in the input is used. One successful
    match is always required.
    a  
       Specification of the output Overlay structure. For instance:

       Image.R * Image.G * Image.B

       Will ensure an overlay of this structure is created even if
       (for instance) only (Image.R * Image.B) is supplied.

       Elements in the input overlay that match are placed in the
       appropriate positions and unavailable specification elements
       are created with the specified fill group.r|   r   r]   r      z}
        The default range that will be set on the value_dimension of
        any automatically created blank image elements.r-   r{   z5
        The group assigned to the resulting overlay.c                 C   s   t tg d|d}t|tr.|d dkr6tdttd}d}dD ]8}|| t||}||v rH|| |krx dS |d7 }qH|S )	z3Return the strength of the match (None if no match)rU   r1   label.rU   r!   zOnly Image currently supportedr1   r   r   N)	dictzipsplitr5   r!   NotImplementedErrorr   r   rY   )clsrM   spec	spec_dictZ
sanitizersstrengthr8   
attr_valuer&   r&   r)   _matchU  s    

zimage_overlay._matchc           	      C   s   dgt | }dgt | }t|tr.| n|g}|D ]R}tt |D ]@}| ||| }|du rhqHqH||| krxqHqH|||< |||< qHq8||fS )z
        Given a raster or input overlay, generate a list of matched
        elements (None if no match) and corresponding tuple of match
        strength values.
        Nr   )lenr5   r   valuesranger   )	r6   rasterZoverlay_specordering	strengthselementsrM   posr   r&   r&   r)   _match_overlayf  s    zimage_overlay._match_overlayNc                 C   s   t dd | jjdD }| ||\}}tdd |D rFtdg }|t| }t	||D ]r\}}	|d u rt
t	g d|	d}
tt|jj| jj |
dd	|
d
dd}| jj|jd _|| qbt|S )Nc                 s   s   | ]}|  V  qd S r%   )strip.0rM   r&   r&   r)   	<genexpr>}  r+   z)image_overlay._process.<locals>.<genexpr>*c                 s   s   | ]}|d u V  qd S r%   r&   r   r&   r&   r)   r     r+   z7The image_overlay operation requires at least one matchr   r   r1   r!   r   rr   r   r   )tupler2   r   r   r   all	ExceptionnpZargmaxr   r   r!   onesr~   shapefillgetdefault_rangevdimsr   appendprod)r6   r   r8   specsr   r   Z	completedZ	strongestrM   r   r   r&   r&   r)   r:   |  s     

zimage_overlay._process)N)r;   r<   r=   r>   r   r4   r?   rA   r   Numberr   Tupler   r1   classmethodr   r   r:   r&   r&   r&   r)   r   )  s   
r   c                   @   sZ   e Zd ZdZeZejdddZejdddZ	ejdddZ
ejd	d
dZdZdddZdS )	thresholdz
    Threshold a given Image whereby all values higher than a given
    level map to the specified high value and all values lower than
    that level map to the specified low value.
    g      ?z
       The value at which the threshold is applied. Values lower than
       the threshold map to the 'low' value and values above map to
       the 'high' value.r-   g      ?zR
      The value given to elements greater than (or equal to) the
      threshold.g        z7
      The value given to elements below the threshold.Z	Thresholdz5
       The group assigned to the thresholded output.TNc                 C   sh   t |tstd|j}t|j| jj }t|j| jj	 }t
|| jjk||}|j|| jjdS )Nz2The threshold operation requires a Image as input.r0   )r5   r!   	TypeErrorr~   r   r   r   r2   highlowwherelevelra   r1   )r6   matrixr8   arrr   r   Zthresholdedr&   r&   r)   r:     s    
zthreshold._process)N)r;   r<   r=   r>   r!   r4   r?   r   r   r   r   rA   r1   _per_elementr:   r&   r&   r&   r)   r     s   r   c                   @   s0   e Zd ZdZeZejdddZdZ	d	ddZ
dS )
gradientz
    Compute the gradient plot of the supplied Image.

    If the Image value dimension is cyclic, the smallest step is taken
    considered the cyclic range
    ZGradientz6
    The group assigned to the output gradient matrix.r-   TNc                 C   sR  t |jdkrtd|jd }t|j|dd}|j\}}|jrVd |jv rVt	d|js`d n
t
|j}|d ur||jd  }tj
|dddd|d d|d f }tj
|dddd|d d|d f }	|d ur,|| }|	| }	|| }
|	| }tt|
|k |
|}tt||	k ||	}	tt|| |	|	  |j| jjdS )	Nr   zDInput matrix to gradient operation must have single value dimension.r   FZflatzKCyclic range must be specified to compute the gradient of cyclic quantitiesZaxisboundsr1   )r   r   rl   r   flipuddimension_valuesr   Zcyclicr   r   diffr   absr!   sqrtr   r2   r1   )r6   r   r8   Z
matrix_dimr~   rcZcyclic_rangeZdxZdyZdx_negativesZdy_negativesr&   r&   r)   r:     s(    

((
zgradient._process)N)r;   r<   r=   r>   r!   r4   r?   rA   r1   r   r:   r&   r&   r&   r)   r     s
   r   c                   @   s@   e Zd ZdZeZejdddZej	ddddZ
d	ZdddZd
S )convolvez
    Apply a convolution to an overlay using the top layer as the
    kernel for convolving the bottom layer. Both Image elements in
    the input overlay should have a single value dimension.
    ZConvolutionz4
        The group assigned to the convolved output.r-   r   r   r   r      z
        A 2-dimensional slice of the kernel layer to use in the
        convolution in lbrt (left, bottom, right, top) format. By
        default, no slicing is applied.r.   lengthr/   TNc                 C   s.  t |dkrtd|d|d }}t |jdkr@tdt| jjd | jjd }t| jjd | jjd }| jjdkr|jn|||f j}t	|j
ddd	}tj|}	tjj||jd
}
tj|	|
 j}|j\}}tjtj||d  dd|d  dd}|t|  }t||j| jjdS )Nr   z'Overlay must contain at least to items.r   r   z9Convolution requires inputs with single value dimensions.   r   Fr   )srv   r   r   )r   r   r   r   slicer2   
kernel_roir~   r   r   r   Zfftfft2r   Zifft2realZrollfloatsumr!   r   r1   )r6   overlayr8   targetkernelZxsliceZyslicer(   r~   Zfft1r   Zconvolved_rawZk_rowsZk_colsZrolledZ	convolvedr&   r&   r)   r:     s      
(zconvolve._process)N)r;   r<   r=   r>   r!   r4   r?   rA   r1   NumericTupler   r   r:   r&   r&   r&   r)   r     s   r   c                   @   s`   e Zd ZdZeZejdee	fddZ
ejdddZejdd	dZejdd
dZdZdddZdS )contoursz
    Given a Image with a single channel, annotate it with contour
    lines for a given set of contour levels.

    The return is an NdOverlay with a Contours layer for each given
    level, overlaid on top of the input Image.
    
   zD
        A list of scalar values used to specify the contour levels.r.   rK   r/   ZLevelz3
        The group assigned to the output contours.r-   Fz,
        Whether to generate filled contoursz@
        Whether to overlay the contour on the supplied Element.TNc           &   	      s  zddl m}m}m}m} W n ty8   tdd Y n0 |jdddd}|jdddd}|jddd}	|jd |	jd kr|d d	 tj	|dd
d  }|jd |	jd kr|d d d d	f tj	|dd
d  }|jd |	jd kr|d d	 tj	|dd
d  }|jd |	jd krD|d d d d	f tj	|dd
d  }|||	f}
t
dd t|
D trtd d r| jjrtdzddlmm W n ty   tdd Y n0 t
fddt|
D }
|jddd\}}| jjrt}nt}|jd d }| jj}|d\}}t|tr||krh|g ||g|}| jjrd|| S |S t|d }|||}n
t|}d r|}|  |! f}| jjr|d j"|dg}t#|t#dkr|j$}n|j%}||
||j&d}fdd  fdd}g }| jjrttj'tj'gg}t|d d	 |dd  D ]D\}}|||}|d d  d u rqRg }g }tdd r||d d |d d }t|d d	 |dd  D ]`\} }!|r|(| |(|  | d    fddt| d |!D }"|(|" q|| d }#|jd j)d r`|#n|#||f|rxt*|ng i}$|r||$d< |(|$ qRn|D ]}#|+|#}%|%d d  d u rȐqtdd r|||j%kr:|%d d d ur:t,dkr:dd	 -tj.tj/tj'dd
|jd j)d rV|#n|#||fd urlng i}$|(|$ q|||j0|j1|d}| jjr|| }|S )Nr   )FillTypeLineType__version__contour_generatorz&contours operation requires contourpy.TFr   r   r   rv   r   g       @c                 s   s   | ]\}}t |V  qd S r%   )r   )r   r(   r   r&   r&   r)   r   M  r+   z$contours._process.<locals>.<genexpr>zODatetime spatial coordinates are not supported for filled contour calculations.)date2numnum2datez7contours operation using datetimes requires matplotlib.c                 3   s"   | ]\}}|r |n|V  qd S r%   r&   )r   dis_datetime)r   r&   r)   r   X  s   r8   r   )r   z1.2)	line_typeZ	fill_typec                    s@   t | }t |}|r d| |< t  | } |r<t j| |< | S r^   )r   isnananyarraynan)ZcoordsZnan_maskZany_nan)r   r&   r)   coords_to_datetime  s    


z-contours._process.<locals>.coords_to_datetimec                    sF   t j| ddd\}}d r$ |}d r4 |}t j||fddS )Nr   r   r   r   )r   r   concatenate)pointsxsys)r   data_is_datetimer&   r)   points_to_datetime  s    z-contours._process.<locals>.points_to_datetimec                    s$   g | ]} |  |d    qS )r   r&   )r   j)offsetsr   r&   r)   
<listcomp>  r+   z%contours._process.<locals>.<listcomp>Zholes)r   kdimsr   )2Z	contourpyr   r   r   r   ImportErrorr   r   r   r   r   	enumerater   r2   filledRuntimeErrorZmatplotlib.datesr   r   r   
dimensionsr   r   r   levelsr   r5   intoverlaidr$   Ztick_valuesr   minmaxra   r   ZChunkCombinedNanZChunkCombinedOffsetZChunkCombinedOffsetOffsetr   r   namer   linesr   astypeint64insertr   r   )&r6   rS   r8   r   r   Zcontourpy_versionr   r   r   Zzsr~   xdimydimZcontour_typer   r   ZzminZzmaxr   locatorZcranger   Zcont_genr   pathsemptyZlower_levelZupper_levelr   Z	exteriorsZ	interiorsZouter_offsetsZjstartZjendZinteriorr   Zgeomr   r&   )r   r   r   r   r   r   r)   r:   2  s    &&






	
$"




zcontours._process)N)r;   r<   r=   r>   r   r4   r?   rT   listr   r   rA   r1   Booleanr   r   r   r:   r&   r&   r&   r)   r     s   r   c                   @   s   e Zd ZdZejddddZejdej	e
eefddZejdd	d
Zejddd
Zejddd
ZejdeefddZejddd
Zejddd
Zejdg dddZejddd
Zejddd
Zejddd
ZejddddZdddZdS )	histogramz
    Returns a Histogram of the input element data, binned into
    num_bins over the bin_range (if specified) along the specified
    dimension.
    Nr   z<
      Specifies the range within which to compute the bins.r   z
      An explicit set of bin edges or a method to find the optimal
      set of bin edges, e.g. 'auto', 'fd', 'scott' etc. For more
      documentation on these approaches see the np.histogram_bin_edges
      documentation.r   Fz2
      Whether to compute the cumulative histogramr-   zE
      Along which dimension of the Element to compute the histogram.zT
      Format string defining the label of the frequency dimension of the Histogram.zW
      Defines a dimension to group the Histogram returning an NdOverlay of Histograms.zD
      Whether to use base 10 logarithmic samples for the bin edges.z5
      Whether the weighted frequencies are averaged.)TFintegralheighta^  
      Controls normalization behavior.  If `True` or `'integral'`, then
      `density=True` is passed to np.histogram, and the distribution
      is normalized such that the integral is unity.  If `False`,
      then the frequencies will be raw counts. If `'height'`, then the
      frequencies are normalized such that the max bin height is unity.)r.   objectsr/   zF
      Whether to use only nonzero values when computing the histogram   z(
      Number of bins in the histogram .z@
       Name of the dimension the weighting should be drawn fromzT
      Used for setting a common style for histograms in a HoloMap or AdjointLayout.)r.   Z
allow_Noner/   c           !      C   sF  | j jrDt|tstd|j| j jttd}d | j _|| jtS | j jrX| j j	rXdn| j j
}| j jrr| j j}ntdd |j|j D }||}t|dr|jj||dd}n
||}t|}|r|dd	}t}	t|}
|
r2d
d l}t|jtdk}|s,|s| j j	r,||}d}
n|j}	t|rddlm} | }| j j rf||d
k@ }| rx|! }n|" }|| }t#|$d%  }||j& }nN|	|}| j j r||d
k@ }|| }t' }|rt||j(rdnt#| }| j j	r<t|dr$|jj|| j j	dd}n|| j j	}|| }nd }t| j j)t*rz|
r`||n|}t+j,|| j j)d}n\t| j j)t-t+j.fr| j j)}t|r|dd	}n | j j/p|0|}t12 ( t1j3ddt4d |dk}W d    n1 s0    Y  |s&t5dd |D r*d}| j j6d }|\}}t|t*sbt|t*sbt|t*rjtd|rt7|dt7|d }}| j j8rt9t:|||d
k ; g}t+<t+=|t+=||}nt+>|||}|
r|?|}t@|s(|r(| j j)d u r| j j6nt#| j j)d }t+A|}nt|dr|r>dnd}|jjB||||d\}}|dkrp||9  }| j j	r"| j jr"|jjB|d|d\}}|| }n~|rt+jB|d||d\}}|dkr"||9  }nJt+jB||||d\}}| j j	r"| j jr"t+jB|d| j j6d\}}|| }d
|t+C|< |rD|d d}i }| j j	rh|| j j	g|d< n`| j jDr| j jDjE|jFd } tGd!| d"g|d< n0|rd!nd#} tG|j& d$| H  | d"g|d< |jI|jJjKkr|jI|d%< | j jLrt+M|}| j j
d&v r||d |d
  9 }t-|| _)tN||ff||g|jOd'|S )(Nz3Cannot use histogram groupby on non-Dataset Element)Z
group_typeZcontainer_typeFc                 s   s   | ]}|j V  qd S r%   )r   r   r   r&   r&   r)   r     r+   z%histogram._process.<locals>.<genexpr>r`   )Zcomputedatetime64[ns]r   r   z8.0r   )ibis5r   )binsignorezelementwise comparison failed)actionmessagecategory)r   r   c                 s   s   | ]}t | V  qd S r%   )r   )r   r   r&   r&   r)   r   i  r+   r   zFCategorical data found. Cannot create histogram from categorical data.nsT)densityweightsr
  )r  r  )r  r  r  g     @@zdatetime64[us]r   )dimZ	Frequencyr   ZCount_r1   )Tr	  )r   r   )Pr2   groupbyr5   r   rl   r   mapr:   mean_weightedweight_dimensionnormed	dimensionnextr   r   Zget_dimensionhasattrr`   r   r   r   r   r   r   cupyr   r   Zasnumpyr   Zcore.data.ibisr  ZnotnullnonzeroZas_tableZto_projectionr   headexecuter   r   Arrayr  strr   Zhistogram_bin_edgesr  ndarray	bin_ranger   warningscatch_warningsfilterwarningsDeprecationWarningr   num_binsr   logr   r   r   Zlogspacelog10ZlinspaceZasarrayr   zerosr  r   frequency_labelformatZpprint_labelr   lowerr1   	__class__r;   
cumulativeZcumsumr   r   )!r6   rS   r8   Zgroupedr  Zselected_dimr  r~   r   	is_finiteZis_cupyr"  Zfull_cupy_supportr  maskZno_datadar  Zbin_dataedgesZ
hist_rangeZnull_hist_rangestepsstartendZbin_minZnbinshistr  Z	hist_meanr  ro   r   r&   r&   r)   r:     s    









"



($

&











zhistogram._process)N)r;   r<   r=   r>   r?   r   r)  rT   r   r(  r  r   r'  r  r  r6  rA   r  r2  r   r  r/  r  ObjectSelectorr  r#  Integerr.  r  Zstyle_prefixr:   r&   r&   r&   r)   r    s$   	r  c                   @   s   e Zd ZdZejdddZejdddZejdddZ	ejdd	dZ
ejegeefd
dZejddddZejddddZdZdddZdddZdS )decimatea  
    Decimates any column based Element to a specified number of random
    rows if the current element defined by the x_range and y_range
    contains more than max_samples. By default the operation returns a
    DynamicMap with a RangeXY stream allowing dynamic downsampling.
    Tz.
       Enables dynamic processing by default.r-   z
         By default, the link_inputs parameter is set to True so that
         when applying shade, backends that support linked streams
         update RangeXY streams on the inputs of the shade operation.i  z?
        Maximum number of samples to display at the same time.*   z/
        Seed used to initialize randomization.zr
        List of streams that are applied if dynamic=True, allowing
        for dynamic interaction with the plot.r   Nr   zY
       The x_range as a tuple of min and max x-value. Auto-ranges
       if set to None.r   zY
       The x_range as a tuple of min and max y-value. Auto-ranges
       if set to None.c                 C   s   t |tstd|jtvr2|t|  }| j	j
rB| j	j
n|d\}}| j	jr`| j	jn|d\}}|jdddd \}}|jf i |||f|||fi}	t|	| j	jkrtj| j	j}
|
t|	| j	jd}|	jt| S |	S )Nz$Cannot downsample non-Dataset types.r   r   Tr   r   F)r5   r   rl   r`   column_interfacesra   r   columnsr   r2   rc   r   rd   r   selectr   max_samplesr   randomZRandomStaterandom_seedchoicerb   sort)r6   rS   r8   ZxstartZxendZystartZyendr  r  ZslicedZprngrI  r&   r&   r)   _process_layer  s    

zdecimate._process_layerc                 C   s   | | jtS r%   r  rK  r   rR   r&   r&   r)   r:     s    zdecimate._process)N)N)r;   r<   r=   r>   r?   r  Zdynamicrw   r@  rF  rH  rT   r#   r   r  ri   r   rc   rd   r   rK  r:   r&   r&   r&   r)   rA    s   
rA  c                   @   s`   e Zd ZdZejg ddddZdZedd Z	ed	d
 Z
edd ZdddZdddZdS )interpolate_curvezw
    Resamples a Curve using the defined interpolation method, e.g.
    to represent changes in y-values as steps.
    )	steps-pre	steps-mid
steps-postZlinearrO  zC
       Controls the transition point of the step along the x-axis.)r  r.   r/   Tc                    s   t dt  d }t fdd|D } |dd d< |ddd |dd d< g }t||D ]8\}}||dd d< |dd d |dd d< || q`|t|fS )Nr   r   c                 3   s*   | ]"}t jd t  d |jdV  qdS r   r   dtypeNr   r  r   rS  r   vr\   r&   r)   r      r+   z3interpolate_curve.pts_to_prestep.<locals>.<genexpr>r   r   r   r1  r   r   r   r   r   r'   r   r;  Zvalue_stepsZ
val_arraysrV  r   r&   r\   r)   pts_to_prestep  s    z interpolate_curve.pts_to_prestepc                    s   t dt  }t fdd|D } d d  dd   d d  d   |ddd< |dd d<  d  d  |d< |d< g }t||D ]8\}}||dd d< |dd d |dd d< || q|t|fS )Nr   c                 3   s&   | ]}t jd t  |jdV  qdS )r   rR  NrT  rU  r\   r&   r)   r     r+   z3interpolate_curve.pts_to_midstep.<locals>.<genexpr>rv   r   r   rW  rX  r&   r\   r)   pts_to_midstep  s    @z interpolate_curve.pts_to_midstepc                    s   t dt  d }t fdd|D } |dd d< |dd d |dd d< g }t||D ]8\}}||dd d< |ddd |dd d< || q`|t|fS )Nr   r   c                 3   s*   | ]"}t jd t  d |jdV  qdS rQ  rT  rU  r\   r&   r)   r      r+   z4interpolate_curve.pts_to_poststep.<locals>.<genexpr>r   r   rW  rX  r&   r\   r)   pts_to_poststep  s    z!interpolate_curve.pts_to_poststepNc           	         s   | j | j| jd}| jj|vr" S  d}t|}|rFd}||}t fdd 	 dd  D }|| jj ||\}}|r||} 
|f| S )N)rN  rO  rP  r   r  c                 3   s   | ]}  |V  qd S r%   )r   r  rS   r&   r)   r   8  r+   z3interpolate_curve._process_layer.<locals>.<genexpr>r   )rY  rZ  r[  r2   interpolationr   r   r   r   r   ra   )	r6   rS   r8   ZINTERPOLATE_FUNCSr'   r   Zdt_typeZdvalsr   r&   r\  r)   rK  -  s     

"
z interpolate_curve._process_layerc                 C   s   | | jtS r%   rL  rR   r&   r&   r)   r:   >  s    zinterpolate_curve._process)N)N)r;   r<   r=   r>   r?   r?  r]  r   r   rY  rZ  r[  rK  r:   r&   r&   r&   r)   rM    s   




rM  c                   @   s*   e Zd ZdZejejddZdddZ	dS )collapsea*  
    Given an overlay of Element types, collapse into single Element
    object using supplied function. Collapsing aggregates over the
    key dimensions of each object applying the supplied fn to each group.

    This is an example of an Operation that does not involve
    any Raster types.
    z[
        The function that is used to collapse the curve y-values for
        each x-value.r-   Nc                 C   s:   t |trt|}ntdd t|D }|j| jjdS )Nc                 S   s   i | ]\}}||qS r&   r&   )r   irM   r&   r&   r)   
<dictcomp>Y  r+   z%collapse._process.<locals>.<dictcomp>)rH   )r5   r   r
   r   r^  r2   rQ   )r6   r   r8   Zcollapse_mapr&   r&   r)   r:   U  s    

zcollapse._process)N)
r;   r<   r=   r>   r?   rB   r   meanrQ   r:   r&   r&   r&   r)   r^  G  s   	r^  c                   @   sZ   e Zd ZdZejeddZejdddZeje	ddZ
ejg ddZdd	 Zdd
dZdS )
gridmatrixaT  
    The gridmatrix operation takes an Element or HoloMap
    of Elements as input and creates a GridMatrix object,
    which plots each dimension in the Element against
    each other dimension. This provides a very useful
    overview of high-dimensional data and is inspired
    by pandas and seaborn scatter_matrix implementations.
    zV
        The Element type used to display bivariate distributions
        of the data.r-   Nz
       The Element type along the diagonal, may be a Histogram or any
       other plot type which can visualize a univariate distribution.
       This parameter overrides diagonal_operation.z
       The operation applied along the diagonal, may be a histogram-operation
       or any other function which returns a viewable element.zf
       If a HoloMap is supplied, this will allow overlaying one or
       more of its key dimensions.c                    s   t |t ttfr fdd  D  fdd  D  t t	 d  j
r~ fddtf  S t tr  t S d S )Nc                    s   i | ]}|j  |qS r&   )r   r   r  )r~   r&   r)   r`  |  r+   z'gridmatrix.__call__.<locals>.<dictcomp>c              	      s$   i | ]\}}|t  |qS r&   )r	   r:   )r   r(   rV  )r2   rangesr6   r&   r)   r`  }  s   )Z
merge_typec                    s   |   jS r%   )r   overlay_dimsr\   )r2   r&   r)   rN     r+   z%gridmatrix.__call__.<locals>.<lambda>)r?   ZParamOverridesr5   r
   r   r   ra   itemsr   rU   rd  r  r   r:   r	   )r6   r~   ro   r&   )r~   r2   rc  r6   r)   rX   x  s    
zgridmatrix.__call__c                    s  |d u ri }t jtjr&t}nj}ttjtjft	 fdd
 D   fdd D }|jtu r~d |_t|_i }|D ]
\}}||krD|jd ur|jjr|j||g|gtgd}	n|}
|j|
|gd}	n`|jd u rqnR|jtu s
t |jtr2||j|}|j|j|d}	n|j|jd}	n>t|jjdkrb||gg fn||f\}}|j|||tgd}	|	||j|jf< q|S )	Nc                    s2   g | ]*}t  |d  rt |s|qS )r   )r   r   
issubclassZget_dimension_typer  )rS   typesr&   r)   r     s   z'gridmatrix._process.<locals>.<listcomp>c                    s(   g | ] } d d d D ]}||fqqS )Nrv   r&   )r   d1d2)dimsr&   r)   r     s   )r   r   datatype)r   )r  r)  )r  r   )r5   r~   r   r(  tabler   r'  Zstr_Zobject_r   r   diagonal_typer   r  diagonal_operationZ_auto_indexable_1dr   r   r   r   r   
chart_typer   )r6   r2   rS   rc  Zel_dataZpermuted_dimsr~   rh  ri  rM   r   r)  r   r   r&   )rj  rS   rg  r)   r:     sB    




*
zgridmatrix._process)N)r;   r<   r=   r>   r?   r@   r   ro  rm  r  rn  rF   rd  rX   r:   r&   r&   r&   r)   rb  ]  s   	rb  )Fr>   r*  numpyr   r?   Zpackaging.versionr   r   corer   r   r   r   r	   r
   r   r   r   Z	core.datar   r   r   r   Zcore.data.utilr   Z	core.utilr   r   r   r   r   r   r   r   r   Zelement.chartr   r   Zelement.pathr   r   Zelement.rasterr    r!   Zelement.utilr"   ri   r#   Zutil.locatorr$   rC  r*   r,   rC   rH   rW   ZParameterizedFunctionr[   rq   rz   r   r   r   r   r   r  rA  rM  r^  rb  r&   r&   r&   r)   <module>   sH   ,,
*-9$g'4. A [=W