
    ek&                         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	 Z G d
 de          Z G d de          ZdS )    N   )Dataset	Dimension	NdOverlay)	Operation)cartesian_productisfinite)Area	BivariateContoursCurveDistributionImagePolygons   )contoursc                    | d         ||z  z
  | d         ||z  z   }}t          |d                   rt          ||d                   }t          |d                   rt          ||d                   }t          j        |||          S )z0Establish support for a kernel density estimate.r   r   )r	   maxminnplinspace)	bin_rangebwgridsizecutclipkminkmaxs          9lib/python3.11/site-packages/holoviews/operation/stats.py_kde_supportr       s    1S()A,c*A$DQ "4a!!Q "4a!!;tT8,,,    c                   L   e Zd ZdZ ej        dddgd          Z ej        dd          Z ej        d	d
          Z	 ej
        ddd          Z ej        dd          Z ej        dd          Z ej        dd          Z ej        deefd          ZdZ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default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                    | j         j        r|t          |t                    st	          d          |                    | j         j        t          t
                    }d | j         _        |                    | j        t                    S 	 ddlm	} ddl
m} n3# t          $ r& t          t          |           j         d          d w xY wi }t          |t                    r{|j        d         }|j        t          |          j        k    r
|j        |d<   |j        |d<   |j        d         }|j         d	}	|j        d
k    r|                    |	d
          n|g}
n| j         j        r |                    | j         j                  }n=|j        |j        z   }|s$t	          dt          |          j        z            |d         }|j         d	}	t1          |	d
          g}
|                    |          }| j         j        p|                    |          }|dk    st9          d |D                       rd}n(|d         |d         k    r|d         dz
  |d         dz   f}| j         j        rt<          nt>          }tA          |          r|tC          |                   ng }tA          |          dk    r	 |"                    |          }n# |$ r  |g ||
fi |cY S w xY w| j         j#        r|$                    | j         j#                   |%                                |&                    d          z  }| j         j        r-tO          j(        |d         |d         | j         j)                  }n,tU          ||| j         j)        | j         j+        |j                  }|,                    |          }nAtO          j(        |d         |d         | j         j)                  }tO          j-        |d          } |||ff|g|
d|S )Nz3Cannot use histogram groupby on non-Dataset Element)
group_typecontainer_typer   stats)LinAlgError* operation requires SciPy to be installed.grouplabel_densityDensity)r<   zU%s element does not declare any dimensions to compute the kernel density estimate on.)r   r   c              3   6   K   | ]}t          |           V  d S Nr	   ).0rs     r   	<genexpr>z*univariate_kde._process.<locals>.<genexpr>a   s*      %I%I!(1++o%I%I%I%I%I%Ir!   )r   r   r         ?ddofkdimsvdims).pgroupby
isinstancer   
ValueErrorr   map_processscipyr8   scipy.linalgr9   ImportErrortype__name__r   rI   r;   r<   rJ   nameclone	dimensionget_dimensionr   dimension_valuesr   rangeanyfilledr
   r   lenr	   gaussian_kde	bandwidthset_bandwidthscotts_factorstdr   r   	n_samplesr    r   evaluate	full_like)selfelementkeygroupedr8   r9   paramsselected_dimvdim	vdim_namerJ   
dimensionsdatar   element_typekder   xsyss                      r   rP   zunivariate_kde._process;   s7   6> 	7gw// X !VWWWoodfnYboccG!DFN;;t}g666	l######0000000 	l 	l 	ld!4```aagkk	l g|,, 	<"=+L}W 666")-w%mF7O=#D',666I?CyI?U?UTZZ	Z;;;[_`EEv -&44TV5EFF$]7=8
! =$ &R%)']]%;&< = = =  *!}',666Iy	:::;E''55F$Cl(C(C	#%I%Iy%I%I%I"I"IIIq\Yq\))"1c)9Q<+;<I#v}7tt%'*4yy8tHTNN##bt99q==G((.. G G G#|BeFFvFFFFFGv 4!!$&"2333""$$txxQx'7'77Bv c[1y|TV=MNN!)R1A46:|Oabbb!!BBYq\9Q<9IJJBb!$$B|RHR\N%RR6RRRs   
B 0C5K KKr@   )rU   
__module____qualname____doc__paramObjectSelector	bw_methodNumberr`   r   NumericTupler   StringrX   Booleanr]   Integerrd   ClassSelectorstrr   rL   _per_elementrP    r!   r   r#   r#      sq       	 	 %$Ww>T [= > > >I T 0g h h hI %,q 'G H H HC #"4 A@ A A AI T 0D E E EI U]4 .> ? ? ?F c 06 7 7 7I "e!$Y7G NZ [ [ [G L>S >S >S >S >S >Sr!   r#   c                   r   e Zd ZdZ ej        dd          Z ej        dddgd          Z ej	        d	d
          Z
 ej	        dd          Z ej        dd          Z ej        deefd          Z ej        dd          Z ej        d	dd          Z ej        d	dd          ZdZ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.r2   r0   r1   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                 	   	 ddl m} n3# t          $ r& t          t          |           j         d          d w xY wt          |                                          dk     rt          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
 |	|
fD                       rd\  }	}
n|	|
k    r
|	dz
  |
dz   }
}	t'          d ||fD                       rd\  }}n||k    r
|dz
  |dz   }}|j        d	         d	k    r-|d d t+          |                              d          f         nt/          j        d          }|j        d	         d	k    r|                    |          }| j        j        r|                    | j        j                   |                                |                    d	          z  }| j        j        r!t/          j        |	|
| j        j                  }n.tA          |	|
f|| j        j        | j        j!        |j                  }| j        j        r!t/          j        ||| j        j                  }n.tA          ||f|| j        j        | j        j!        |j                  }tE          ||gd          \  }}t/          j#        |$                                |$                                g          }t/          j%         ||          j        |j                  }n| j        j&        r+| j        j'        rtP          ntR          } |g ||g|g          S t/          j        |	|
| j        j                  }t/          j        ||| j        j                  }t/          j*        | j        j        | j        j        f          }tW          |||j        ff|                                d d         |gd|}| j        j&        rAtM          || j        j'        | j        j,                  } |j-        |j.        d	d          fi |S |S )Nr   r7   r:   r   zQbivariate_kde can only be computed on elements declaring at least two dimensions.r;   r<   r>   r   c              3   6   K   | ]}t          |           V  d S r@   rA   rB   vs     r   rD   z)bivariate_kde._process.<locals>.<genexpr>   *      5518A;;555555r!   )g      rE   rE   c              3   6   K   | ]}t          |           V  d S r@   rA   r   s     r   rD   z)bivariate_kde._process.<locals>.<genexpr>   r   r!   )axis)r   r   rF   FrH   )r]   levels)/rQ   r8   rS   rT   rU   r^   ro   rN   rM   r   r;   r<   rJ   arrayTrK   x_ranger[   y_ranger\   shaper	   r   r   emptyr_   r`   ra   rb   rc   r   rd   r    r   r   vstackravelreshaper   r]   r   r   zerosr   r   rW   rp   )rg   rh   ri   r8   xdimydimrk   rm   rp   xminxmaxyminymaxrr   r   rs   rt   xxyy	positionsfeltypeimgcntrs                           r   rP   zbivariate_kde._process   s   	l####### 	l 	l 	ld!4```aagkk	l w!!##$$q(( B C C C''))"1"-
dgy)) 	}W 666")-w%mF7O=#DDD}}aV$$&V^7w}}Q'7'7
dV^7w}}Q'7'7
d55t55555 	,"JD$$T\\c48$D55t55555 	,"JD$$T\\c48$D6:jma6G6GtAAAx~~))q)11122RXV\M]M]:a=1$$T**Cv 4!!$&"2333""$$txxQx'7'77Bv~ ^[tTV-=>>!4,DF4DdfjRVR\]]v~ ^[tTV-=>>!4,DF4DdfjRVR\]]&Bx77FB	288::rxxzz":;;I
33y>>+RX66AAV_ 	?!%<XXHF6"T4L????T4)9::BT4)9::B$&*DF,<=>>ARQSMZ););)=)=bqb)A$ZZSYZZ6? 	7CdfmLLLD4:dim66v666
s   	 09r@   )rU   ru   rv   rw   rx   r~   r   ry   rz   r{   r`   r   r]   r   listintr   r   rd   r|   r   r   r   rP   r   r!   r   r   r   }   s       
 
 u}T 01 2 2 2H %$Ww>T [= > > >I T 0) * * *I %,q 'G H H HC U]5 /C D D DF !U T3K FG H H HF c 06 7 7 7I "u!$q ?   G "u!$q ?   G L< < < < < <r!   r   )numpyr   rx   corer   r   r   core.operationr   	core.utilr   r	   rh   r
   r   r   r   r   r   r   r   r    r#   r   r   r!   r   <module>r      s2        0 0 0 0 0 0 0 0 0 0 & & & & & & 3 3 3 3 3 3 3 3 U U U U U U U U U U U U U U U U U U      - - -dS dS dS dS dSY dS dS dSPj j j j jI j j j j jr!   