
    hMh!                        d dl mZ d dlmZ d dlmZmZ d dlZd dlm	Z	 d dl
Zd dl
mZ 	 d dlmZ dZn# e$ r d dlmZ d	ZY nw xY wd d
lmZ d dlmZ d dlmZ e G d de                      ZdS )    )annotations)	dataclass)AnyCallableN)ndarray)	DataFrame)gaussian_kdeFT)GroupBy)Scale)Statc                      e Zd ZU dZdZded<   dZded<   dZd	ed
<   dZd	ed<   dZ	ded<   dZ
ded<   dZded<   d Zd-dZd.d Zd/d"Zd0d$Zd1d&Zd2d+Zd,S )3KDEaB  
    Compute a univariate kernel density estimate.

    Parameters
    ----------
    bw_adjust : float
        Factor that multiplicatively scales the value chosen using
        `bw_method`. Increasing will make the curve smoother. See Notes.
    bw_method : string, scalar, or callable
        Method for determining the smoothing bandwidth to use. Passed directly
        to :class:`scipy.stats.gaussian_kde`; see there for options.
    common_norm : bool or list of variables
        If `True`, normalize so that the areas of all curves sums to 1.
        If `False`, normalize each curve independently. If a list, defines
        variable(s) to group by and normalize within.
    common_grid : bool or list of variables
        If `True`, all curves will share the same evaluation grid.
        If `False`, each evaluation grid is independent. If a list, defines
        variable(s) to group by and share a grid within.
    gridsize : int or None
        Number of points in the evaluation grid. If None, the density is
        evaluated at the original datapoints.
    cut : float
        Factor, multiplied by the kernel bandwidth, that determines how far
        the evaluation grid extends past the extreme datapoints. When set to 0,
        the curve is truncated at the data limits.
    cumulative : bool
        If True, estimate a cumulative distribution function. Requires scipy.

    Notes
    -----
    The *bandwidth*, or standard deviation of the smoothing kernel, is an
    important parameter. Much like histogram bin width, using the wrong
    bandwidth can produce a distorted representation. Over-smoothing can erase
    true features, while under-smoothing can create false ones. The default
    uses a rule-of-thumb that works best for distributions that are roughly
    bell-shaped. It is a good idea to check the default by varying `bw_adjust`.

    Because the smoothing is performed with a Gaussian kernel, the estimated
    density curve can extend to values that may not make sense. For example, the
    curve may be drawn over negative values when data that are naturally
    positive. The `cut` parameter can be used to control the evaluation range,
    but datasets that have many observations close to a natural boundary may be
    better served by a different method.

    Similar distortions may arise when a dataset is naturally discrete or "spiky"
    (containing many repeated observations of the same value). KDEs will always
    produce a smooth curve, which could be misleading.

    The units on the density axis are a common source of confusion. While kernel
    density estimation produces a probability distribution, the height of the curve
    at each point gives a density, not a probability. A probability can be obtained
    only by integrating the density across a range. The curve is normalized so
    that the integral over all possible values is 1, meaning that the scale of
    the density axis depends on the data values.

    If scipy is installed, its cython-accelerated implementation will be used.

    Examples
    --------
    .. include:: ../docstrings/objects.KDE.rst

       float	bw_adjustscottz-str | float | Callable[[gaussian_kde], float]	bw_methodTzbool | list[str]common_normcommon_grid   z
int | Nonegridsize   cutFbool
cumulativec                D    | j         rt          rt          d          d S d S )Nz(Cumulative KDE evaluation requires scipy)r   	_no_scipyRuntimeError)selfs    V/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/seaborn/_stats/density.py__post_init__zKDE.__post_init__^   s<    ? 	Ky 	KIJJJ	K 	K 	K 	K    paramstrgrouping_varsr   returnNonec                "   t          | |          }t          |t                    sQt          |t                    rt	          d |D                       s#| j        j         d| }t          | d          |                     ||d           dS )z'Do input checks on grouping parameters.c              3  @   K   | ]}t          |t                    V  d S )N)
isinstancer$   ).0vs     r    	<genexpr>z1KDE._check_var_list_or_boolean.<locals>.<genexpr>h   s,      /R/Rq
1c0B0B/R/R/R/R/R/Rr"   .z& must be a boolean or list of strings.r   )
stacklevelN)	getattrr*   r   listall	__class____name__	TypeError_check_grouping_vars)r   r#   r%   value
param_names        r    _check_var_list_or_booleanzKDE._check_var_list_or_booleanc   s    e$$ud##	S5$''	S,//R/RE/R/R/R,R,R	S !N3==e==JzQQQRRR!!%1!EEEEEr"   datar   orientr	   c                    d| j         i}d|v r|d         |d<   t          ||         fi |}|                    |j        | j        z             |S )zFit and return a KDE object.r   weightweights)r   r	   set_bandwidthfactorr   )r   r:   r;   fit_kwskdes        r    _fitzKDE._fitn   sd     $/"?t!%hGI4<33733#*t~5666
r"   r   c                   | j         ||                                         S |                     ||          }t          j        |j                                                  }||                                         || j        z  z
  }||         	                                || j        z  z   }t          j
        ||| j                   S )z2Define the grid that the KDE will be evaluated on.)r   to_numpyrC   npsqrt
covariancesqueezeminr   maxlinspace)r   r:   r;   rB   bwgridmingridmaxs          r    _get_supportzKDE._get_supportz   s    = <((***iif%%WS^++--..v,""$$rDH}4v,""$$rDH}4{7GT];;;r"   supportc                   t          j        |ddgt                    }t          |          dk     r|S 	 |                     ||          n# t
          j        j        $ r |cY S w xY w| j        r*|d         t          j	        fd|D                       }n |          }|d         
                                }t          j        ||d|d|i          S )zITransform single group by fitting a KDE and evaluating on a support grid.r=   densitycolumnsdtype   r   c                <    g | ]}                     |          S  )integrate_box_1d)r+   s_irB   s_0s     r    
<listcomp>z)KDE._fit_and_evaluate.<locals>.<listcomp>   s)    RRR3 4 4S# > >RRRr"   )pdr   r   lenrC   rF   linalgLinAlgErrorr   arraysum)	r   r:   r;   rQ   emptyrS   r=   rB   r\   s	          @@r    _fit_and_evaluatezKDE._fit_and_evaluate   s     fh	%B%PPPt99q==L	))D&))CCy$ 	 	 	LLL	 ? 	#!*ChRRRRR'RRRSSGGc'llGh##%%|VWh	7STTTs   A A'&A'	list[str]c                   t          j        g j        dt                    }t	                    dk     r|S 	 |                     |          }n# t          j        j        $ r |cY S w xY wfd|D             }|s| 	                    ||          S t          |          }|                    | j	        ||          S )z9Transform multiple groups by fitting KDEs and evaluating.rS   rT   rW   c                P    g | ]"}|                                          d k     |#S )r   )nunique)r+   xr:   s     r    r]   z"KDE._transform.<locals>.<listcomp>   s1    KKKqT!W__5F5F5J5J5J5J5Jr"   )r^   r   rU   r   r_   rP   rF   r`   ra   re   r
   apply)r   r:   r;   r%   rd   rQ   groupbys    `     r    
_transformzKDE._transform   s     %?t|%?Y%?uMMMt99q==L	''f55GGy$ 	 	 	LLL	 LKKKMKKK 	A))$@@@-((}}T4#967KKKs   A A,+A,rl   r
   scalesdict[str, Scale]c                  	 d|vr|                     d          }|                    |dg          }fd|D             		r	| j        du r|                     ||	          }n_| j        du r	}n)|                     d	           	fd	| j        D             }t          |                              || j        |	          }	r	| j        du r/|                     |d                                         
          }n| j        du r	}n)|                     d	           	fd| j        D             }|	                    |
                    |          d                                                             d          |          }|dxx         |                    d          z  cc<   ddd|         }|d         ||<   |                    ddgd          S )Nr=   r   )r=   )subsetc                @    g | ]}|j         v t          |          S rY   )orderr$   )r+   r,   rl   s     r    r]   z KDE.__call__.<locals>.<listcomp>   s+    DDDAgm1C1CQ1C1C1Cr"   TFr   c                    g | ]}|v |	S rY   rY   r+   r,   r%   s     r    r]   z KDE.__call__.<locals>.<listcomp>   #    OOO1A<N<NQ<N<N<Nr"   )group_weightr   c                    g | ]}|v |	S rY   rY   ru   s     r    r]   z KDE.__call__.<locals>.<listcomp>   rv   r"   rw   )onrS   zweight / group_weightyrj   )rj   rz   )axis)assigndropnar   rm   r9   r
   rk   r   rc   joinrl   renameevaldrop)
r   r:   rl   r;   rn   res	grid_vars	norm_varsr7   r%   s
     `      @r    __call__zKDE.__call__   s    4;;a;((D{{68"4{55 EDDDDDD 	 0D 8 8//$>>CC5(()		//}MMMOOOO(8OOO	 	""tT_fmDD   	 0D 8 8**$x.*<*<*>*>*??CC5(()		//}MMMOOOO(8OOO	((Y''15577>>~NN   C
 	I#((#:;;;$$V,^E
xx>2x;;;r"   N)r#   r$   r%   r   r&   r'   )r:   r   r;   r$   r&   r	   )r:   r   r;   r$   r&   r   )r:   r   r;   r$   rQ   r   r&   r   )r:   r   r;   r$   r%   rf   r&   r   )
r:   r   rl   r
   r;   r$   rn   ro   r&   r   )r4   
__module____qualname____doc__r   __annotations__r   r   r   r   r   r   r!   r9   rC   rP   re   rm   r   rY   r"   r    r   r      s,        > >~ I?FIFFFF$(K(((($(K((((HCNNNNJK K K
	F 	F 	F 	F
 
 
 
	< 	< 	< 	<U U U U*L L L L$*< *< *< *< *< *<r"   r   )
__future__r   dataclassesr   typingr   r   numpyrF   r   pandasr^   r   scipy.statsr	   r   ImportErrorseaborn.external.kdeseaborn._core.groupbyr
   seaborn._core.scalesr   seaborn._stats.baser   r   rY   r"   r    <module>r      sS   " " " " " " ! ! ! ! ! !                                    ((((((II   111111III * ) ) ) ) ) & & & & & & $ $ $ $ $ $ @< @< @< @< @<$ @< @< @< @< @<s   3 AA