
    IR-e1                     h    d Z ddlZddlmZ g dZddZddZdd
ZddZddZ	 G d d          Z
dS )zo
Methods for selecting the bin width of histograms.

Ported from the astroML project: https://www.astroml.org/
    N   )bayesian_blocks)	histogramscott_bin_widthfreedman_bin_widthknuth_bin_widthcalculate_bin_edges
   c                    || | |d         k    | |d         k    z           } t          |t                    rt          j        |                                           } |t          d          |dk    rt          |           }na|dk    rt          | d          \  }}nG|dk    rt          | d          \  }}n-|d	k    rt          | d          \  }}nt          d
| d          |r:|d         |d         k    r|d         |d<   |d         |d         k    r|d         |d<   n0t          j        |          dk    rt          j        | |||          }|S )aW  
    Calculate histogram bin edges like ``numpy.histogram_bin_edges``.

    Parameters
    ----------
    a : array-like
        Input data. The bin edges are calculated over the flattened array.

    bins : int, list, or str, optional
        If ``bins`` is an int, it is the number of bins. If it is a list
        it is taken to be the bin edges. If it is a string, it must be one
        of  'blocks', 'knuth', 'scott' or 'freedman'. See
        `~astropy.stats.histogram` for a description of each method.

    range : tuple or None, optional
        The minimum and maximum range for the histogram.  If not specified,
        it will be (a.min(), a.max()). However, if bins is a list it is
        returned unmodified regardless of the range argument.

    weights : array-like, optional
        An array the same shape as ``a``. If given, the histogram accumulates
        the value of the weight corresponding to ``a`` instead of returning the
        count of values. This argument does not affect determination of bin
        edges, though they may be used in the future as new methods are added.
    Nr   r   z8weights are not yet supported for the enhanced histogramblocksknuthTscottfreedmanzunrecognized bin code: '')rangeweights)
isinstancestrnpasarrayravelNotImplementedErrorr   r   r   r   
ValueErrorndimhistogram_bin_edges)abinsr   r   das        7lib/python3.11/site-packages/astropy/stats/histogram.pyr	   r	      s   8 qE!H}eAh/0 $ !MJqMM!! %J   8"1%%DDW__&q$//HBW__&q$//HBZ)!T22HB????@@@ 	$
 Aw%(""(QBx58## 8R	!		%aUGLLLK    c                 T    t          | |||          }t          j        | f|||d|S )a  Enhanced histogram function, providing adaptive binnings.

    This is a histogram function that enables the use of more sophisticated
    algorithms for determining bins.  Aside from the ``bins`` argument allowing
    a string specified how bins are computed, the parameters are the same
    as ``numpy.histogram()``.

    Parameters
    ----------
    a : array-like
        array of data to be histogrammed

    bins : int, list, or str, optional
        If bins is a string, then it must be one of:

        - 'blocks' : use bayesian blocks for dynamic bin widths

        - 'knuth' : use Knuth's rule to determine bins

        - 'scott' : use Scott's rule to determine bins

        - 'freedman' : use the Freedman-Diaconis rule to determine bins

    range : tuple or None, optional
        the minimum and maximum range for the histogram.  If not specified,
        it will be (x.min(), x.max())

    weights : array-like, optional
        An array the same shape as ``a``. If given, the histogram accumulates
        the value of the weight corresponding to ``a`` instead of returning the
        count of values. This argument does not affect determination of bin
        edges.

    other keyword arguments are described in numpy.histogram().

    Returns
    -------
    hist : array
        The values of the histogram. See ``density`` and ``weights`` for a
        description of the possible semantics.
    bin_edges : array of dtype float
        Return the bin edges ``(length(hist)+1)``.

    See Also
    --------
    numpy.histogram
    )r   r   r   )r	   r   r   )r   r   r   r   kwargss        r    r   r   \   s<    ` qt5'JJJD<ME7MMfMMMr!   Fc                    t          j        |           } | j        dk    rt          d          | j        }t          j        |           }d|z  |dz  z  }|rt          j        |                                 |                                 z
  |z            }t          d|          }|                                 |t          j	        |dz             z  z   }||fS |S )a'  Return the optimal histogram bin width using Scott's rule.

    Scott's rule is a normal reference rule: it minimizes the integrated
    mean squared error in the bin approximation under the assumption that the
    data is approximately Gaussian.

    Parameters
    ----------
    data : array-like, ndim=1
        observed (one-dimensional) data
    return_bins : bool, optional
        if True, then return the bin edges

    Returns
    -------
    width : float
        optimal bin width using Scott's rule
    bins : ndarray
        bin edges: returned if ``return_bins`` is True

    Notes
    -----
    The optimal bin width is

    .. math::
        \Delta_b = \frac{3.5\sigma}{n^{1/3}}

    where :math:`\sigma` is the standard deviation of the data, and
    :math:`n` is the number of data points [1]_.

    References
    ----------
    .. [1] Scott, David W. (1979). "On optimal and data-based histograms".
       Biometricka 66 (3): 605-610

    See Also
    --------
    knuth_bin_width
    freedman_bin_width
    bayesian_blocks
    histogram
    r   data should be one-dimensionalg      @UUUUUU?)
r   r   r   r   sizestdceilmaxminarange)datareturn_binsnsigmadxNbinsr   s          r    r   r      s    V :dDyA~~9:::	AF4LLE	ue	%B dhhjj0B677AuxxzzB519!5!5554x	r!   c                 H   t          j        |           } | j        dk    rt          d          | j        }|dk     rt          d          t          j        | ddg          \  }}d||z
  z  |dz  z  }|r|                                 |                                 }}t          dt          j        ||z
  |z                      }	 ||t          j	        |dz             z  z   }	n:# t          $ r-}
d	t          |
          v rt          d
|dz    d           d}
~
ww xY w||	fS |S )a  Return the optimal histogram bin width using the Freedman-Diaconis rule.

    The Freedman-Diaconis rule is a normal reference rule like Scott's
    rule, but uses rank-based statistics for results which are more robust
    to deviations from a normal distribution.

    Parameters
    ----------
    data : array-like, ndim=1
        observed (one-dimensional) data
    return_bins : bool, optional
        if True, then return the bin edges

    Returns
    -------
    width : float
        optimal bin width using the Freedman-Diaconis rule
    bins : ndarray
        bin edges: returned if ``return_bins`` is True

    Notes
    -----
    The optimal bin width is

    .. math::
        \Delta_b = \frac{2(q_{75} - q_{25})}{n^{1/3}}

    where :math:`q_{N}` is the :math:`N` percent quartile of the data, and
    :math:`n` is the number of data points [1]_.

    References
    ----------
    .. [1] D. Freedman & P. Diaconis (1981)
       "On the histogram as a density estimator: L2 theory".
       Probability Theory and Related Fields 57 (4): 453-476

    See Also
    --------
    knuth_bin_width
    scott_bin_width
    bayesian_blocks
    histogram
    r   r%      z(data should have more than three entries   K      r&   zMaximum allowed size exceededzVThe inter-quartile range of the data is too small: failed to construct histogram with z: bins. Please use another bin method, such as bins="scott"N)r   r   r   r   r'   
percentiler+   r*   r)   r,   r   )r-   r.   r/   v25v75r1   dmindmaxr2   r   es              r    r   r      sU   X :dDyA~~9:::	A1uuCDDD}TB8,,HC	
cCiA%L	)B XXZZdArwtr12233	"ry3333DD 		 		 		.#a&&88 #:?!)# # #   		 4x	s   C$ $
D.(DDTc                    ddl m} t          |           }t          | d          \  }}|                    |t          |          |           d         }|                    |          }|d         |d         z
  }	|r|	|fS |	S )a/  Return the optimal histogram bin width using Knuth's rule.

    Knuth's rule is a fixed-width, Bayesian approach to determining
    the optimal bin width of a histogram.

    Parameters
    ----------
    data : array-like, ndim=1
        observed (one-dimensional) data
    return_bins : bool, optional
        if True, then return the bin edges
    quiet : bool, optional
        if True (default) then suppress stdout output from scipy.optimize

    Returns
    -------
    dx : float
        optimal bin width. Bins are measured starting at the first data point.
    bins : ndarray
        bin edges: returned if ``return_bins`` is True

    Notes
    -----
    The optimal number of bins is the value M which maximizes the function

    .. math::
        F(M|x,I) = n\log(M) + \log\Gamma(\frac{M}{2})
        - M\log\Gamma(\frac{1}{2})
        - \log\Gamma(\frac{2n+M}{2})
        + \sum_{k=1}^M \log\Gamma(n_k + \frac{1}{2})

    where :math:`\Gamma` is the Gamma function, :math:`n` is the number of
    data points, :math:`n_k` is the number of measurements in bin :math:`k`
    [1]_.

    References
    ----------
    .. [1] Knuth, K.H. "Optimal Data-Based Binning for Histograms".
       arXiv:0605197, 2006

    See Also
    --------
    freedman_bin_width
    scott_bin_width
    bayesian_blocks
    histogram
    r   )optimizeT)dispr   )scipyr?   _KnuthFr   fminlenr   )
r-   r.   quietr?   knuthFdx0bins0Mr   r1   s
             r    r   r     s    b T]]F#D$//JCfc%jj5y99!<A;;q>>D	a47	B 4x	r!   c                   *    e Zd ZdZd Zd Zd Zd ZdS )rB   ah  Class which implements the function minimized by knuth_bin_width.

    Parameters
    ----------
    data : array-like, one dimension
        data to be histogrammed

    Notes
    -----
    the function F is given by

    .. math::
        F(M|x,I) = n\log(M) + \log\Gamma(\frac{M}{2})
        - M\log\Gamma(\frac{1}{2})
        - \log\Gamma(\frac{2n+M}{2})
        + \sum_{k=1}^M \log\Gamma(n_k + \frac{1}{2})

    where :math:`\Gamma` is the Gamma function, :math:`n` is the number of
    data points, :math:`n_k` is the number of measurements in bin :math:`k`.

    See Also
    --------
    knuth_bin_width
    c                     t          j        |d          | _        | j        j        dk    rt	          d          | j                                         | j        j        | _        ddlm	} |j
        | _
        d S )NT)copyr   zdata should be 1-dimensionalr   )special)r   arrayr-   r   r   sortr'   r/   rA   rM   gammaln)selfr-   rM   s      r    __init__z_KnuthF.__init__r  sv    HT---	9>Q;<<<	
 	"!!!!! r!   c                 z    t          j        | j        d         | j        d         t          |          dz             S )z,Return the bin edges given M number of bins.r   r   r   )r   linspacer-   intrQ   rI   s     r    r   z_KnuthF.bins  s,    {49Q<2A
CCCr!   c                 ,    |                      |          S )N)evalrV   s     r    __call__z_KnuthF.__call__  s    yy||r!   c                    t          j        |          s|d         }t          |          }|dk    rt           j        S |                     |          }t          j        | j        |          \  }}| j        t          j        |          z  | 	                    d|z            z   || 	                    d          z  z
  | 	                    | j        d|z  z             z
  t          j
        | 	                    |dz                       z    S )a!  Evaluate the Knuth function.

        Parameters
        ----------
        M : int
            Number of bins

        Returns
        -------
        F : float
            evaluation of the negative Knuth loglikelihood function:
            smaller values indicate a better fit.
        r   g      ?)r   isscalarrU   infr   r   r-   r/   logrP   sum)rQ   rI   r   nks       r    rX   z_KnuthF.eval  s     {1~~ 	!AFF666Myy||<	400D FRVAYYll37##$$,,s###$ ll46C!G+,,- fT\\"s(++,,	-
 	
r!   N)__name__
__module____qualname____doc__rR   r   rY   rX    r!   r    rB   rB   X  s]         2' ' 'D D D  
 
 
 
 
r!   rB   )r
   NN)F)FT)rc   numpyr   r   __all__r	   r   r   r   r   rB   rd   r!   r    <module>rg      s         , , , , , ,  C C C CL2N 2N 2N 2Nj: : : :zH H H HV< < < <~N
 N
 N
 N
 N
 N
 N
 N
 N
 N
r!   