a
    Dfk&                     @   s   d dl Zd dlZddlmZmZmZ ddlmZ ddl	m
Z
mZ ddlmZmZmZmZmZmZmZ ddlmZ d	d
 ZG dd deZG dd deZdS )    N   )Dataset	Dimension	NdOverlay)	Operation)cartesian_productisfinite)Area	BivariateContoursCurveDistributionImagePolygons   )contoursc                 C   sd   | d ||  | d ||   }}t |d r<t||d }t |d rVt||d }t|||S )z0Establish support for a kernel density estimate.r   r   )r   maxminnplinspace)	bin_rangebwZgridsizecutZclipZkminZkmax r   f/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/holoviews/operation/stats.py_kde_support   s    "r   c                   @   s   e Zd ZdZejdddgddZejdddZejd	d
dZ	ej
d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eefddZdZdddZdS )univariate_kdea  
    Computes a 1D kernel density estimate (KDE) along the supplied
    dimension. Kernel density estimation is a non-parametric way to
    estimate the probability density function of a random variable.

    The KDE works by placing a Gaussian kernel at each sample with
    the supplied bandwidth. These kernels are then summed to produce
    the density estimate. By default a good bandwidth is determined
    using the bw_method but it may be overridden by an explicit value.
    scott	silverman:
        Method of automatically determining KDE bandwidthdefaultZobjectsdocNzd
        Allows supplying explicit bandwidth value rather than relying on scott or silverman method.r!   r"      D
        Draw the estimate to cut * bw from the extreme data points.r   z=
        Specifies the range within which to compute the KDE.r!   lengthr"   zA
        Along which dimension of the Element to compute the KDE.Tz;
        Controls whether to return filled or unfilled KDE.d   3
        Number of samples to compute the KDE over.zW
      Defines a dimension to group the Histogram returning an NdOverlay of Histograms.r!   class_r"   c                 C   s  | j jrDt|tstd|j| j jttd}d | j _|| jtS zddlm	} ddl
m} W n( ty   tt| j dd Y n0 i }t|tr|jd }|jt|jkr|j|d< |j|d< |jd }|j d	}	|jd
kr|j|	d
dn|g}
n`| j jr|| j j}n,|j|j }|s:tdt|j |d }|j d	}	t|	d
dg}
||}| j jpx||}|dkstdd |D rd}n*|d |d kr|d d |d d f}| j jrtnt}t |r|t!| ng }t |dkrz|"|}W n( |y8   |g ||
fi | Y S 0 | j j#rR|$| j j# |% |j&dd }| j jrt'(|d |d | j j)}nt*||| j j)| j j+|j}|,|}n&t'(|d |d | j j)}t'-|d}|||ff|g|
d|S )Nz3Cannot use histogram groupby on non-Dataset Element)Z
group_typeZcontainer_typer   stats)LinAlgError* operation requires SciPy to be installed.grouplabelZ_densityDensity)r1   zU%s element does not declare any dimensions to compute the kernel density estimate on.)r   r   c                 s   s   | ]}t | V  qd S Nr   ).0rr   r   r   	<genexpr>a       z*univariate_kde._process.<locals>.<genexpr>)r   r   r         ?Zddofkdimsvdims).pgroupby
isinstancer   
ValueErrorr   map_processscipyr-   Zscipy.linalgr.   ImportErrortype__name__r   r<   r0   r1   r=   nameclone	dimensionZget_dimensionr   Zdimension_valuesr   rangeanyfilledr	   r   lenr   gaussian_kde	bandwidthset_bandwidthscotts_factorstdr   r   	n_samplesr   r   evaluateZ	full_like)selfelementkeyZgroupedr-   r.   paramsZselected_dimvdimZ	vdim_namer=   
dimensionsdatar   Zelement_typekder   xsysr   r   r   rC   ;   sj    





 



zunivariate_kde._process)N)rG   
__module____qualname____doc__paramObjectSelector	bw_methodNumberrP   r   NumericTupler   StringrJ   BooleanrM   IntegerrT   ClassSelectorstrr   r?   _per_elementrC   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g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dZejd	dddZdZdddZd	S )bivariate_kdea  
    Computes a 2D kernel density estimate (KDE) of the first two
    dimensions in the input data. Kernel density estimation is a
    non-parametric way to estimate the probability density function of
    a random variable.

    The KDE works by placing 2D Gaussian kernel at each sample with
    the supplied bandwidth. These kernels are then summed to produce
    the density estimate. By default a good bandwidth is determined
    using the bw_method but it may be overridden by an explicit value.
    Tzv
        Whether to compute contours from the KDE, determines whether to
        return an Image or Contours/Polygons.r#   r   r   r   r    Nzl
        Allows supplying explicit bandwidth value rather than relying
        on scott or silverman method.r$   r%   Fz@
        Controls whether to return filled or unfilled contours.
   zD
        A list of scalar values used to specify the contour levels.r*   r(   r)   r   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   sh  zddl m} W n( ty8   tt| j dd Y n0 t| dk rRtd| d d \}}i }t|t	r|j
t|jkr|j
|d< |j|d< |jd }nd}|dd	gj}| jjp|d\}	}
| jjp|d	\}}td
d |	|
fD rd\}	}
n|	|
kr"|	d |
d  }	}
tdd ||fD rDd\}}n||kr`|d |d  }}|jd	 d	kr|d d t|jddf ntd}|jd	 d	kr||}| jjr|| jj | |jd	d }| jjrt|	|
| jj}nt |	|
f|| jj| jj!|j}| jjr6t||| jj}nt ||f|| jj| jj!|j}t"||gd\}}t#|$ |$ g}t%||j|j}nj| jj&r| jj'rt(nt)}|g ||g|gdS t|	|
| jj}t||| jj}t*| jj| jjf}t+|||jff| d d |gd|}| jj&rdt&|| jj'| jj,d}|j-|j.d	d  fi |S |S )Nr   r,   r/   r   zQbivariate_kde can only be computed on elements declaring at least two dimensions.r0   r1   r2   r   c                 s   s   | ]}t | V  qd S r3   r4   r5   vr   r   r   r7      r8   z)bivariate_kde._process.<locals>.<genexpr>)g      r9   r9   c                 s   s   | ]}t | V  qd S r3   r4   rp   r   r   r   r7      r8   )Zaxis)r   r   r:   Fr;   )rM   levels)/rD   r-   rE   rF   rG   rN   r[   rA   r@   r
   r0   r1   r=   arrayTr>   x_rangerK   y_rangerL   shaper   r   r   emptyrO   rP   rQ   rR   rS   r   rT   r   r   r   ZvstackZravelZreshaper   rM   r   r   zerosr   rr   rI   r\   )rV   rW   rX   r-   ZxdimZydimrY   rZ   r\   ZxminZxmaxZyminZymaxr]   r   r^   r_   xxyyZ	positionsfZeltypeimgZcntrr   r   r   rC      sf    






6




,
zbivariate_kde._process)N)rG   r`   ra   rb   rc   ri   r   rd   re   rf   rP   r   rM   rk   listintrr   rj   rT   rg   ru   rv   rm   rC   r   r   r   r   rn   }   s   rn   )numpyr   rc   corer   r   r   Zcore.operationr   Z	core.utilr   r   rW   r	   r
   r   r   r   r   r   r   r   r   rn   r   r   r   r   <module>   s   $
h