a
    ߙfb1                     @   s   d Z ddlZddlZddlmZ ddlmZ dgZdZ	G dd dZ
G dd	 d	e
ejZG d
d de
ejZG dd dZG dd deeZee
jej< dS )z.
Distribution class and associated machinery.
    N)units)statsDistributiongtV?c                   @   s   e Zd ZdZi Zdd Zedd Zdd Zedd	 Z	dddZ
dddZdddZdddZd ddZd!ddZdd Zdd Zd
S )"r   am  
    A scalar value or array values with associated uncertainty distribution.

    This object will take its exact type from whatever the ``samples`` argument
    is. In general this is expected to be an `~astropy.units.Quantity` or
    `numpy.ndarray`, although anything compatible with `numpy.asanyarray` is
    possible.

    See also: https://docs.astropy.org/en/stable/uncertainty/

    Parameters
    ----------
    samples : array-like
        The distribution, with sampling along the *leading* axis. If 1D, the
        sole dimension is used as the sampling axis (i.e., it is a scalar
        distribution).
    c                 C   s   t |tr|j}ntj|dd}|jdkr2tdtdg|j|jd ffgd}t|}| j	
|}|d u r|j| j }t|t|tfd|i}|| j	|< |j||d	}|jd d |_|S )
NC)order z4Attempted to initialize a Distribution with a scalarsamples)namesZformats_samples_cls)dtypetype)
isinstancer   distributionnpZ
asanyarrayshape	TypeErrorr   r   _generated_subclassesget__name___DistributionReprArrayDistributionview)clsr   Z	new_dtypeZsamples_clsZnew_clsnew_nameselfr   r   7lib/python3.9/site-packages/astropy/uncertainty/core.py__new__*   s*    

	
zDistribution.__new__c                 C   s   | d S Nr   r   r   r   r   r   r   K   s    zDistribution.distributionc                 O   sj  g }| dd }|r*tdd |D |d< |dv rn|dd }|d u rnt|d tsXJ tt|d j|d< |D ]L}t|tr||j qrt	|dd}	|	r||d	t
jf  qr|| qrt	|||i |}
t|
ts|
f}
|d u rd
t|
 }g }t|
|D ]H\}}|d ur$|| n(t	|ddrB|t| n
|| qt|dkrb|S |d S )Noutc                 s   s"   | ]}t |tr|jn|V  qd S N)r   r   r   ).0outputr   r   r   	<genexpr>S   s   z/Distribution.__array_ufunc__.<locals>.<genexpr>>   reduce
accumulateZreduceataxisr   r   r   .r!   F   )poptupler   r   r   rangendimappendr   getattrr   Znewaxislenzip)r   ZufuncmethodZinputskwargsZ	convertedZoutputsr'   Zinput_r   ZresultsZfinalsresultr#   r   r   r   __array_ufunc__O   s>    


zDistribution.__array_ufunc__c                 C   s   | j d jd S )zN
        The number of samples of this distribution.  A single `int`.
        r   r   )r   r   r   r   r   r   	n_samplesy   s    zDistribution.n_samplesNc                 C   s   | j jd||dS )z\
        The mean of this distribution.

        Arguments are as for `numpy.mean`.
        r	   )r'   r   r    )r   Zmean)r   r   r    r   r   r   pdf_mean   s    zDistribution.pdf_meanr   c                 C   s   | j jd|||dS )zi
        The standard deviation of this distribution.

        Arguments are as for `numpy.std`.
        r	   r'   r   r    ddof)r   Zstdr   r   r    r8   r   r   r   pdf_std   s    zDistribution.pdf_stdc                 C   s   | j jd|||dS )z_
        The variance of this distribution.

        Arguments are as for `numpy.var`.
        r	   r7   )r   varr9   r   r   r   pdf_var   s    zDistribution.pdf_varc                 C   s   t j| jd|dS )aP  
        The median of this distribution.

        Parameters
        ----------
        out : array, optional
            Alternative output array in which to place the result. It must
            have the same shape and buffer length as the expected output,
            but the type (of the output) will be cast if necessary.
        r	   )r'   r    )r   medianr   )r   r    r   r   r   
pdf_median   s    zDistribution.pdf_medianc                 C   s.   | j |d}t| | }tj|jd|ddS )ac  
        The median absolute deviation of this distribution.

        Parameters
        ----------
        out : array, optional
            Alternative output array in which to place the result. It must
            have the same shape and buffer length as the expected output,
            but the type (of the output) will be cast if necessary.
        r    r	   T)r'   r    Zoverwrite_input)r>   r   absr=   r   )r   r    r=   Zabsdiffr   r   r   pdf_mad   s
    zDistribution.pdf_madc                 C   s   | j |d}|t9 }|S )a  
        The median absolute deviation of this distribution rescaled to match the
        standard deviation for a normal distribution.

        Parameters
        ----------
        out : array, optional
            Alternative output array in which to place the result. It must
            have the same shape and buffer length as the expected output,
            but the type (of the output) will be cast if necessary.
        r?   )rA   SMAD_SCALE_FACTOR)r   r    r3   r   r   r   pdf_smad   s    zDistribution.pdf_smadc                 K   sJ   t |t jj}tj| j|fddi|}t| jdrB| j|S |S dS )a  
        Compute percentiles of this Distribution.

        Parameters
        ----------
        percentile : float or array of float or `~astropy.units.Quantity`
            The desired percentiles of the distribution (i.e., on [0,100]).
            `~astropy.units.Quantity` will be converted to percent, meaning
            that a ``dimensionless_unscaled`` `~astropy.units.Quantity` will
            be interpreted as a quantile.

        Additional keywords are passed into `numpy.percentile`.

        Returns
        -------
        percentiles : `~astropy.units.Quantity` ['dimensionless']
            The ``fracs`` percentiles of this distribution.
        r'   r	   	_new_viewN)	uZQuantityZpercentvaluer   
percentiler   hasattrrD   )r   rG   r2   Zpercsr   r   r   pdf_percentiles   s
    zDistribution.pdf_percentilesc                 K   s   | j }||j|jd  |jd }g }g }|D ].}tj|fi |\}}|| || q0t|}| j|j| j f }	t|}| j|j| j f }
||	||
fS )a\  
        Compute histogram over the samples in the distribution.

        Parameters
        ----------
        All keyword arguments are passed into `astropy.stats.histogram`. Note
        That some of these options may not be valid for some multidimensional
        distributions.

        Returns
        -------
        hist : array
            The values of the histogram. Trailing dimension is the histogram
            dimension.
        bin_edges : array of dtype float
            Return the bin edges ``(length(hist)+1)``. Trailing dimension is the
            bin histogram dimension.
        r	   )	r   Zreshapesizer   r   Z	histogramr-   r   Zarray)r   r2   ZdistrZraveled_distrZnhistsZ	bin_edgesdZnhistZbin_edgeZnh_shapeZbe_shaper   r   r   pdf_histogram   s    


zDistribution.pdf_histogram)NN)NNr   )NNr   )N)N)N)r   
__module____qualname____doc__r   r   propertyr   r4   r5   r6   r:   r<   r>   rA   rC   rI   rL   r   r   r   r   r      s    !
*






c                   @   s   e Zd ZdZdS )ScalarDistributionzScalar distribution.

    This class mostly exists to make `~numpy.array2print` possible for
    all subclasses.  It is a scalar element, still with n_samples samples.
    N)r   rM   rN   rO   r   r   r   r   rQ     s   rQ   c                       s0   e Zd ZejZd fdd	Z fddZ  ZS )r   Nc                    s   |du r(t |tjr(t|tjr(|}d}dd ||fD }|du sXt |tjr~t|tr~|durr|| jkrrtdt	 j
| S | jj
| }t|S )a2  New view of array with the same data.

        Like `~numpy.ndarray.view` except that the result will always be a new
        `~astropy.uncertainty.Distribution` instance.  If the requested
        ``type`` is a `~astropy.uncertainty.Distribution`, then no change in
        ``dtype`` is allowed.

        Nc                 S   s   g | ]}|d ur|qS r!   r   )r"   itemr   r   r   
<listcomp>#      z*ArrayDistribution.view.<locals>.<listcomp>z6cannot view as Distribution subclass with a new dtype.)r   builtinsr   
issubclassr   ndarrayr   r   
ValueErrorsuperr   r   )r   r   r   Z	view_argsr3   	__class__r   r   r     s    	
zArrayDistribution.viewc                    sJ   t  |}|dkr&t t|| jS t|tjrB|t|j	fS |S d S r   )
rY   __getitem__r   r   r   r   r   voidrQ   r   )r   rR   r3   rZ   r   r   r\   0  s    zArrayDistribution.__getitem__)NN)	r   rM   rN   r   rW   r   r   r\   __classcell__r   r   rZ   r   r     s   r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )r   c                 C   st   t | j}|drB|d}||d d }d| jj|| jS |d}||d  }| jj | d| j S d S )N> r(   r	   z<{} {} with n_samples={}>( with n_samples=)reprr   endswithfindformatr[   r   r5   )r   ZreprarrZ
firstspaceZ
firstparenr   r   r   __repr__<  s    




z_DistributionRepr.__repr__c                 C   s   t | j}d| j }|| S )Nrb   )strr   r5   )r   Zdistrstrtoaddr   r   r   __str__I  s    
z_DistributionRepr.__str__c                 C   sB   t | jdr:| j }d| j }|d d | |d  S d S d S )N_repr_latex_z, \; n_{\rm samp}=r	   )rH   r   rk   r5   )r   Z
superlatexri   r   r   r   rk   N  s
    
z_DistributionRepr._repr_latex_N)r   rM   rN   rg   rj   rk   r   r   r   r   r   ;  s   r   c                   @   s   e Zd ZdS )NdarrayDistributionN)r   rM   rN   r   r   r   r   rl   W  s   rl   )rO   rU   Znumpyr   Zastropyr   rE   r   __all__rB   r   r]   rQ   rW   r   r   rl   r   r   r   r   r   <module>   s    p	-