U
    e»{j’R  ã                   @   sR  d Z ddlZddlmZmZ ddddd	d
ddddddgZd*dd„Zd+dd„ZG dd„ deƒZ	G dd„ de	ƒZ
G dd„ de	ƒZG dd„ de	ƒZG dd	„ d	e	ƒZG dd„ de	ƒZG dd
„ d
eƒZG dd„ de	ƒZG dd „ d e	ƒZG d!d„ de	ƒZG d"d„ de	ƒZG d#d„ de	ƒZG d$d%„ d%e	ƒZG d&d„ de	ƒZG d'd(„ d(e	ƒZG d)d„ dee	ƒZdS ),z€
Classes that deal with stretching, i.e. mapping a range of [0:1] values onto
another set of [0:1] values with a transformation
é    Né   )ÚBaseTransformÚCompositeTransformÚBaseStretchÚLinearStretchÚSqrtStretchÚPowerStretchÚPowerDistStretchÚSquaredStretchÚ
LogStretchÚAsinhStretchÚSinhStretchÚHistEqStretchÚContrastBiasStretchÚCompositeStretchc                 C   sH   |dkrt  |¡t  | ¡ S t j||d� t j|t  | ¡|d� |S dS )zCalculate the log base n of x.N©Úout)ÚnpÚlogÚtrue_divide)ÚnÚxr   © r   ú\/var/www/html/bwave/python/venv/lib/python3.8/site-packages/astropy/visualization/stretch.pyÚ_logn   s
    r   Tc                 C   sF   |rt j| dd|d�S |dkr,t j| dd�S t  | ¡|dd…< |S dS )z“
    Prepare the data by optionally clipping and copying, and return the
    array that should be subsequently used for in-place calculations.
    ç        ç      ð?r   NT)Úcopy)r   ÚclipÚarrayZasarray)Úvaluesr   r   r   r   r   Ú_prepare'   s    r!   c                   @   s:   e Zd ZdZedd„ ƒZdd„ Zddd	„Zed
d„ ƒZdS )r   z´
    Base class for the stretch classes, which, when called with an array
    of values in the range [0:1], return an transformed array of values,
    also in the range [0:1].
    c                 C   s   dS )NFr   ©Úselfr   r   r   Ú_supports_invalid_kw>   s    z BaseStretch._supports_invalid_kwc                 C   s
   t || ƒS ©N)r   )r#   Úotherr   r   r   Ú__add__B   s    zBaseStretch.__add__TNc                 C   s   dS )ac  
        Transform values using this stretch.

        Parameters
        ----------
        values : array-like
            The input values, which should already be normalized to the
            [0:1] range.
        clip : bool, optional
            If `True` (default), values outside the [0:1] range are
            clipped to the [0:1] range.
        out : ndarray, optional
            If specified, the output values will be placed in this array
            (typically used for in-place calculations).

        Returns
        -------
        result : ndarray
            The transformed values.
        Nr   ©r#   r    r   r   r   r   r   Ú__call__E   s    zBaseStretch.__call__c                 C   s   dS )ú5A stretch object that performs the inverse operation.Nr   r"   r   r   r   Úinverse[   s    zBaseStretch.inverse)TN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úpropertyr$   r'   r)   r+   r   r   r   r   r   7   s   

c                       s8   e Zd ZdZd‡ fdd„	Zddd	„Zed
d„ ƒZ‡  ZS )r   as  
    A linear stretch with a slope and offset.

    The stretch is given by:

    .. math::
        y = slope x + intercept

    Parameters
    ----------
    slope : float, optional
        The ``slope`` parameter used in the above formula.  Default is 1.
    intercept : float, optional
        The ``intercept`` parameter used in the above formula.  Default is 0.
    r   r   c                    s   t ƒ  ¡  || _|| _d S r%   )ÚsuperÚ__init__ÚslopeÚ	intercept)r#   r3   r4   ©Ú	__class__r   r   r2   q   s    
zLinearStretch.__init__TNc                 C   sJ   t |||d�}| jdkr*tj|| j|d� | jdkrFtj|| j|d� |S )N©r   r   r   r   r   )r!   r3   r   Úmultiplyr4   Úaddr(   r   r   r   r)   v   s    

zLinearStretch.__call__c                 C   s   t d| j | j | j ƒS ©r*   r   )r   r3   r4   r"   r   r   r   r+   ~   s    zLinearStretch.inverse)r   r   )TN©	r,   r-   r.   r/   r2   r)   r0   r+   Ú__classcell__r   r   r5   r   r   `   s
   
c                   @   s2   e Zd ZdZedd„ ƒZd
dd„Zedd	„ ƒZdS )r   zb
    A square root stretch.

    The stretch is given by:

    .. math::
        y = \sqrt{x}
    c                 C   s   dS ©NTr   r"   r   r   r   r$   Ž   s    z SqrtStretch._supports_invalid_kwTNc              	   C   s^   t |||d�}| o|dk	}tjdd��  |r6|dk }tj||d� W 5 Q R X |rZ|||< |S )áZ  
        Transform values using this stretch.

        Parameters
        ----------
        values : array-like
            The input values, which should already be normalized to the
            [0:1] range.
        clip : bool, optional
            If `True` (default), values outside the [0:1] range are
            clipped to the [0:1] range.
        out : ndarray, optional
            If specified, the output values will be placed in this array
            (typically used for in-place calculations).
        invalid : None or float, optional
            Value to assign NaN values generated by this class.  NaNs in
            the input ``values`` array are not changed.  This option is
            generally used with matplotlib normalization classes, where
            the ``invalid`` value should map to the matplotlib colormap
            "under" value (i.e., any finite value < 0).  If `None`, then
            NaN values are not replaced.  This keyword has no effect if
            ``clip=True``.

        Returns
        -------
        result : ndarray
            The transformed values.
        r7   NÚignore©Úinvalidr   r   )r!   r   ÚerrstateÚsqrt©r#   r    r   r   rA   Zreplace_invalidÚidxr   r   r   r)   ’   s    zSqrtStretch.__call__c                 C   s   t dƒS )r*   é   )r   r"   r   r   r   r+   ¾   s    zSqrtStretch.inverse)TNN)r,   r-   r.   r/   r0   r$   r)   r+   r   r   r   r   r   „   s   	

,c                       sB   e Zd ZdZedd„ ƒZ‡ fdd„Zddd	„Zed
d„ ƒZ‡  Z	S )r   zÜ
    A power stretch.

    The stretch is given by:

    .. math::
        y = x^a

    Parameters
    ----------
    a : float
        The power index (see the above formula).  ``a`` must be greater
        than 0.
    c                 C   s   dS r=   r   r"   r   r   r   r$   Ô   s    z!PowerStretch._supports_invalid_kwc                    s$   t ƒ  ¡  |dkrtdƒ‚|| _d S ©Nr   za must be > 0)r1   r2   Ú
ValueErrorÚpower©r#   Úar5   r   r   r2   Ø   s    
zPowerStretch.__init__TNc              	   C   s–   t |||d�}| oN|dk	oNd| j  k o0dk n  pNd| j  k oJdk n  }tjdd��$ |rj|dk }tj|| j|d� W 5 Q R X |r’|||< |S )	r>   r7   Néÿÿÿÿr   r   r?   r@   r   )r!   rI   r   rB   rD   r   r   r   r)   Þ   s    ÿ2ýzPowerStretch.__call__c                 C   s   t d| j ƒS r:   )r   rI   r"   r   r   r   r+     s    zPowerStretch.inverse)TNN©
r,   r-   r.   r/   r0   r$   r2   r)   r+   r<   r   r   r5   r   r   Ä   s   

0c                       s8   e Zd ZdZd‡ fdd„	Zddd„Zed	d
„ ƒZ‡  ZS )r	   aI  
    An alternative power stretch.

    The stretch is given by:

    .. math::
        y = \frac{a^x - 1}{a - 1}

    Parameters
    ----------
    a : float, optional
        The ``a`` parameter used in the above formula.  ``a`` must be
        greater than or equal to 0, but cannot be set to 1.  Default is
        1000.
    ç     @�@c                    s,   |dk s|dkrt dƒ‚tƒ  ¡  || _d S ©Nr   r   z&a must be >= 0, but cannot be set to 1©rH   r1   r2   ÚexprJ   r5   r   r   r2   %  s    
zPowerDistStretch.__init__TNc                 C   sJ   t |||d�}tj| j||d� tj|d|d� tj|| jd |d� |S )Nr7   r   r   r   )r!   r   rI   rQ   Úsubtractr   r(   r   r   r   r)   +  s
    zPowerDistStretch.__call__c                 C   s   t | jd�S ©r*   ©rK   )ÚInvertedPowerDistStretchrQ   r"   r   r   r   r+   2  s    zPowerDistStretch.inverse)rN   )TNr;   r   r   r5   r   r	     s
   
c                       s8   e Zd ZdZd‡ fdd„	Zddd„Zed	d
„ ƒZ‡  ZS )rU   a€  
    Inverse transformation for
    `~astropy.image.scaling.PowerDistStretch`.

    The stretch is given by:

    .. math::
        y = \frac{\log(y (a-1) + 1)}{\log a}

    Parameters
    ----------
    a : float, optional
        The ``a`` parameter used in the above formula.  ``a`` must be
        greater than or equal to 0, but cannot be set to 1.  Default is
        1000.
    rN   c                    s,   |dk s|dkrt dƒ‚tƒ  ¡  || _d S rO   rP   rJ   r5   r   r   r2   J  s    
z!InvertedPowerDistStretch.__init__TNc                 C   sH   t |||d�}tj|| jd |d� tj|d|d� t| j||d� |S )Nr7   r   r   r   )r!   r   r8   rQ   r9   r   r(   r   r   r   r)   P  s
    z!InvertedPowerDistStretch.__call__c                 C   s   t | jd�S rS   )r	   rQ   r"   r   r   r   r+   W  s    z InvertedPowerDistStretch.inverse)rN   )TNr;   r   r   r5   r   rU   8  s
   
rU   c                       s,   e Zd ZdZ‡ fdd„Zedd„ ƒZ‡  ZS )r
   zt
    A convenience class for a power stretch of 2.

    The stretch is given by:

    .. math::
        y = x^2
    c                    s   t ƒ  d¡ d S )NrF   )r1   r2   r"   r5   r   r   r2   g  s    zSquaredStretch.__init__c                 C   s   t ƒ S ©r*   )r   r"   r   r   r   r+   j  s    zSquaredStretch.inverse)r,   r-   r.   r/   r2   r0   r+   r<   r   r   r5   r   r
   ]  s   	c                       sD   e Zd ZdZedd„ ƒZd‡ fdd„	Zdd	d
„Zedd„ ƒZ‡  Z	S )r   a  
    A log stretch.

    The stretch is given by:

    .. math::
        y = \frac{\log{(a x + 1)}}{\log{(a + 1)}}

    Parameters
    ----------
    a : float
        The ``a`` parameter used in the above formula.  ``a`` must be
        greater than 0.  Default is 1000.
    c                 C   s   dS r=   r   r"   r   r   r   r$   €  s    zLogStretch._supports_invalid_kwrN   c                    s$   t ƒ  ¡  |dkrtdƒ‚|| _d S rG   ©r1   r2   rH   rQ   rJ   r5   r   r   r2   „  s    
zLogStretch.__init__TNc              	   C   sœ   t |||d�}| o|dk	}tjdd��^ |r6|dk }tj|| j|d� tj|d|d� tj||d� tj|t | jd ¡|d� W 5 Q R X |r˜|||< |S )r>   r7   Nr?   r@   r   r   r   )r!   r   rB   r8   rQ   r9   r   r   rD   r   r   r   r)   Š  s    &zLogStretch.__call__c                 C   s
   t | jƒS rV   )ÚInvertedLogStretchrQ   r"   r   r   r   r+   ¹  s    zLogStretch.inverse)rN   )TNNrM   r   r   r5   r   r   p  s   

/c                       s6   e Zd ZdZ‡ fdd„Zd
dd„Zedd	„ ƒZ‡  ZS )rX   at  
    Inverse transformation for `~astropy.image.scaling.LogStretch`.

    The stretch is given by:

    .. math::
        y = \frac{e^{y \log{a + 1}} - 1}{a} \\
        y = \frac{e^{y} (a + 1) - 1}{a}

    Parameters
    ----------
    a : float, optional
        The ``a`` parameter used in the above formula.  ``a`` must be
        greater than 0.  Default is 1000.
    c                    s$   t ƒ  ¡  |dkrtdƒ‚|| _d S rG   rW   rJ   r5   r   r   r2   Ð  s    
zInvertedLogStretch.__init__TNc                 C   s^   t |||d�}tj|t | jd ¡|d� tj||d� tj|d|d� tj|| j|d� |S )Nr7   r   r   )r!   r   r8   r   rQ   rR   r   r(   r   r   r   r)   Ö  s    zInvertedLogStretch.__call__c                 C   s
   t | jƒS rV   )r   rQ   r"   r   r   r   r+   Þ  s    zInvertedLogStretch.inverse)TNr;   r   r   r5   r   rX   ¿  s
   
rX   c                       s8   e Zd ZdZd‡ fdd„	Zddd„Zed	d
„ ƒZ‡  ZS )r   a  
    An asinh stretch.

    The stretch is given by:

    .. math::
        y = \frac{{\rm asinh}(x / a)}{{\rm asinh}(1 / a)}.

    Parameters
    ----------
    a : float, optional
        The ``a`` parameter used in the above formula.  The value of
        this parameter is where the asinh curve transitions from linear
        to logarithmic behavior, expressed as a fraction of the
        normalized image.  ``a`` must be greater than 0 and less than or
        equal to 1 (0 < a <= 1).  Default is 0.1.
    çš™™™™™¹?c                    s,   t ƒ  ¡  |dks|dkr"tdƒ‚|| _d S ©Nr   r   za must be > 0 and <= 1©r1   r2   rH   rK   rJ   r5   r   r   r2   ÷  s    
zAsinhStretch.__init__TNc                 C   sN   t |||d�}tj|| j|d� tj||d� tj|t d| j ¡|d� |S ©Nr7   r   r   )r!   r   r   rK   Úarcsinhr(   r   r   r   r)   ý  s
    zAsinhStretch.__call__c                 C   s   t dt d| j ¡ d�S ©r*   r   rT   )r   r   r]   rK   r"   r   r   r   r+     s    zAsinhStretch.inverse)rY   )TNr;   r   r   r5   r   r   ä  s
   
c                       s8   e Zd ZdZd‡ fdd„	Zddd„Zed	d
„ ƒZ‡  ZS )r   aT  
    A sinh stretch.

    The stretch is given by:

    .. math::
        y = \frac{{\rm sinh}(x / a)}{{\rm sinh}(1 / a)}

    Parameters
    ----------
    a : float, optional
        The ``a`` parameter used in the above formula.  ``a`` must be
        greater than 0 and less than or equal to 1 (0 < a <= 1).
        Default is 1/3.
    çUUUUUUÕ?c                    s,   t ƒ  ¡  |dks|dkr"tdƒ‚|| _d S rZ   r[   rJ   r5   r   r   r2     s    
zSinhStretch.__init__TNc                 C   sN   t |||d�}tj|| j|d� tj||d� tj|t d| j ¡|d� |S r\   )r!   r   r   rK   Úsinhr(   r   r   r   r)   !  s
    zSinhStretch.__call__c                 C   s   t dt d| j ¡ d�S r^   )r   r   r`   rK   r"   r   r   r   r+   (  s    zSinhStretch.inverse)r_   )TNr;   r   r   r5   r   r   
  s
   
c                   @   s0   e Zd ZdZd
dd„Zddd„Zedd	„ ƒZdS )r   a  
    A histogram equalization stretch.

    Parameters
    ----------
    data : array-like
        The data defining the equalization.
    values : array-like, optional
        The input image values, which should already be normalized to
        the [0:1] range.
    Nc                 C   sv   t  | ¡ ¡| _| jt  | j¡ | _| j ¡ }| j ¡ }| j| ||  | _|d krlt  ddt| jƒ¡| _	n|| _	d S ©Nr   r   )
r   ÚsortZravelÚdataÚisfiniteÚminÚmaxÚlinspaceÚlenr    )r#   rc   r    ZvminZvmaxr   r   r   r2   ;  s    

zHistEqStretch.__init__Tc                 C   s,   t |||d�}t || j| j¡|d d …< |S ©Nr7   )r!   r   Úinterprc   r    r(   r   r   r   r)   I  s    zHistEqStretch.__call__c                 C   s   t | j| jd�S ©r*   )r    )ÚInvertedHistEqStretchrc   r    r"   r   r   r   r+   N  s    zHistEqStretch.inverse)N)TN©r,   r-   r.   r/   r2   r)   r0   r+   r   r   r   r   r   .  s
   

c                   @   s0   e Zd ZdZd
dd„Zddd„Zedd	„ ƒZdS )rl   a.  
    Inverse transformation for `~astropy.image.scaling.HistEqStretch`.

    Parameters
    ----------
    data : array-like
        The data defining the equalization.
    values : array-like, optional
        The input image values, which should already be normalized to
        the [0:1] range.
    Nc                 C   s:   |t  |¡ | _|d kr0t  ddt| jƒ¡| _n|| _d S ra   )r   rd   rc   rg   rh   r    )r#   rc   r    r   r   r   r2   a  s    zInvertedHistEqStretch.__init__Tc                 C   s,   t |||d�}t || j| j¡|d d …< |S ri   )r!   r   rj   r    rc   r(   r   r   r   r)   h  s    zInvertedHistEqStretch.__call__c                 C   s   t | j| jd�S rk   )r   rc   r    r"   r   r   r   r+   m  s    zInvertedHistEqStretch.inverse)N)TNrm   r   r   r   r   rl   T  s
   

rl   c                       s6   e Zd ZdZ‡ fdd„Zd
dd„Zedd	„ ƒZ‡  ZS )r   aŒ  
    A stretch that takes into account contrast and bias.

    The stretch is given by:

    .. math::
        y = (x - {\rm bias}) * {\rm contrast} + 0.5

    and the output values are clipped to the [0:1] range.

    Parameters
    ----------
    contrast : float
        The contrast parameter (see the above formula).

    bias : float
        The bias parameter (see the above formula).
    c                    s   t ƒ  ¡  || _|| _d S r%   ©r1   r2   ÚcontrastÚbias©r#   ro   rp   r5   r   r   r2   ‡  s    
zContrastBiasStretch.__init__TNc                 C   s\   t |d|d�}tj|| j|d� tj|| j|d� tj|d|d� |rXtj|dd|d� |S )NFr7   r   ç      à?r   r   )r!   r   rR   rp   r8   ro   r9   r   r(   r   r   r   r)   Œ  s    zContrastBiasStretch.__call__c                 C   s   t | j| jƒS rV   )ÚInvertedContrastBiasStretchro   rp   r"   r   r   r   r+   š  s    zContrastBiasStretch.inverse)TNr;   r   r   r5   r   r   s  s
   
c                       s6   e Zd ZdZ‡ fdd„Zd
dd„Zedd	„ ƒZ‡  ZS )rs   a1  
    Inverse transformation for ContrastBiasStretch.

    Parameters
    ----------
    contrast : float
        The contrast parameter (see
        `~astropy.visualization.ConstrastBiasStretch).

    bias : float
        The bias parameter (see
        `~astropy.visualization.ConstrastBiasStretch).
    c                    s   t ƒ  ¡  || _|| _d S r%   rn   rq   r5   r   r   r2   ¯  s    
z$InvertedContrastBiasStretch.__init__TNc                 C   s\   t |d|d�}tj|d|d� tj|| j|d� tj|| j|d� |rXtj|dd|d� |S )NFr7   rr   r   r   r   )r!   r   rR   r   ro   r9   rp   r   r(   r   r   r   r)   ´  s    z$InvertedContrastBiasStretch.__call__c                 C   s   t | j| jƒS rV   )r   ro   rp   r"   r   r   r   r+   Á  s    z#InvertedContrastBiasStretch.inverse)TNr;   r   r   r5   r   rs      s
   
rs   c                   @   s   e Zd ZdZddd„ZdS )r   a  
    A combination of two stretches.

    Parameters
    ----------
    stretch_1 : :class:`astropy.visualization.BaseStretch`
        The first stretch to apply.
    stretch_2 : :class:`astropy.visualization.BaseStretch`
        The second stretch to apply.
    TNc                 C   s   | j | j|||d�||d�S ri   )Ztransform_2Ztransform_1r(   r   r   r   r)   Ó  s
      ÿzCompositeStretch.__call__)TN)r,   r-   r.   r/   r)   r   r   r   r   r   Ç  s   )N)TN)r/   Únumpyr   Z	transformr   r   Ú__all__r   r!   r   r   r   r   r	   rU   r
   r   rX   r   r   r   rl   r   rs   r   r   r   r   r   Ú<module>   sB   ô

)$@P$%O%&$&-'