
    IR-eR                        d Z ddlZddlmZmZ g dZd)dZd*dZ G d	 d
e          Z	 G d de	          Z
 G d de	          Z G d de	          Z G d de	          Z G d de	          Z G d de          Z G d de	          Z G d de	          Z G d de	          Z G d de	          Z G d d e	          Z G d! d"e	          Z G d# d$e	          Z G d% d&e	          Z G 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                     |)t          j        |          t          j        |           z  S t          j        ||           t          j        |t          j        |           |           |S )zCalculate the log base n of x.Nout)nplogtrue_divide)nxr   s      =lib/python3.11/site-packages/astropy/visualization/stretch.py_lognr      s]     {vayy26!99$$
qc
sBF1II3////
    Tc                     |rt          j        | dd|          S |t          j        | d          S t          j        |           |d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arrayasarray)valuesr!   r   s      r   _preparer%   '   s[    
  wvsCS1111;8F....Z''CFJr   c                   L    e Zd ZdZed             Zd ZddZe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                     dS )NF selfs    r   _supports_invalid_kwz BaseStretch._supports_invalid_kw=   s    ur   c                 "    t          ||           S N)r   )r*   others     r   __add__zBaseStretch.__add__A   s    t,,,r   TNc                     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   s       r   __call__zBaseStretch.__call__D         r   c                     dS )5A stretch object that performs the inverse operation.Nr(   r)   s    r   inversezBaseStretch.inverseZ   r3   r   TN)	__name__
__module____qualname____doc__propertyr+   r/   r2   r6   r(   r   r   r   r   6   s|            X- - -   , D D XD D Dr   r   c                   B     e Zd ZdZd	 fd	Zd
dZed             Z x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                 d    t                                                       || _        || _        d S r-   )super__init__slope	intercept)r*   rA   rB   	__class__s      r   r@   zLinearStretch.__init__p   s+    
"r   TNc                     t          |||          }| j        dk    rt          j        || j        |           | j        dk    rt          j        || j        |           |S )Nr!   r   r   r   r   )r%   rA   r   multiplyrB   addr1   s       r   r2   zLinearStretch.__call__u   se    &t555:??K
7777>QF64>v6666r   c                 N    t          d| j        z  | j         | j        z            S r5   r   )r   rA   rB   r)   s    r   r6   zLinearStretch.inverse}   s&     S4:-/KLLLr   )r   r   r7   	r8   r9   r:   r;   r@   r2   r<   r6   __classcell__rC   s   @r   r   r   _   s}          # # # # # #
    M M XM M M M Mr   r   c                   F    e Zd ZdZed             ZddZed             ZdS )r   zb
    A square root stretch.

    The stretch is given by:

    .. math::
        y = \sqrt{x}
    c                     dS NTr(   r)   s    r   r+   z SqrtStretch._supports_invalid_kw       tr   TNc                     t          |||          }| o|du}t          j        d          5  |r|dk     }t          j        ||           ddd           n# 1 swxY w Y   |r|||<   |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.
        rE   Nignoreinvalidr   r   )r%   r   errstatesqrtr*   r$   r!   r   rU   replace_invalididxs          r   r2   zSqrtStretch.__call__   s    : &t555"(:wd':[*** 	( 	( !qjGF''''	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	(
  	" "F3Ks   AA!Ac                      t          d          S )r5      )r	   r)   s    r   r6   zSqrtStretch.inverse   s     Ar   TNN)r8   r9   r:   r;   r<   r+   r2   r6   r(   r   r   r   r      si            X) ) ) )V   X  r   r   c                   V     e Zd ZdZed             Z fdZddZed             Z x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                     dS rO   r(   r)   s    r   r+   z!PowerStretch._supports_invalid_kw   rP   r   c                     t                                                       |dk    rt          d          || _        d S Nr   za must be > 0)r?   r@   
ValueErrorpowerr*   arC   s     r   r@   zPowerStretch.__init__   s;    66_---


r   TNc                 <   t          |||          }| o-|duo)d| j        cxk     odk     nc pd| j        cxk     odk     nc }t          j        d          5  |r|dk     }t          j        || j        |           ddd           n# 1 swxY w Y   |r|||<   |S )	rR   rE   Nr   r   rS   rT   r   )r%   rc   r   rV   rX   s          r   r2   zPowerStretch.__call__   s(   : &t555H @t#@tz%%%%A%%%%>1tz+=+=+=+=A+=+=+=+= 	
 [*** 	5 	5 !qjHVTZV4444	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5
  	" "F3Ks   %B

BBc                 0    t          d| j        z            S rI   )r	   rc   r)   s    r   r6   zPowerStretch.inverse  s     C$*,---r   r]   
r8   r9   r:   r;   r<   r+   r@   r2   r6   rK   rL   s   @r   r	   r	      s            X    - - - -^ . . X. . . . .r   r	   c                   B     e Zd ZdZd fd	Zd	dZed             Z x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                     |dk     s|dk    rt          d          t                                                       || _        d S Nr   r   z&a must be >= 0, but cannot be set to 1rb   r?   r@   exprd   s     r   r@   zPowerDistStretch.__init__"  D    q55AFFEFFFr   TNc                     t          |||          }t          j        | j        ||           t          j        |d|           t          j        || j        dz
  |           |S )NrE   r   r   r   )r%   r   rc   ro   subtractr   r1   s       r   r2   zPowerDistStretch.__call__(  sh    &t555
6v....
FA6****
vtx#~6::::r   c                 ,    t          | j                  S r5   re   )InvertedPowerDistStretchro   r)   s    r   r6   zPowerDistStretch.inverse/  s     ($(3333r   rk   r7   rJ   rL   s   @r   r
   r
     sv                   4 4 X4 4 4 4 4r   r
   c                   B     e Zd ZdZd fd	Zd	dZed             Z xZS )
rv   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.
    rk   c                     |dk     s|dk    rt          d          t                                                       || _        d S rm   rn   rd   s     r   r@   z!InvertedPowerDistStretch.__init__G  rp   r   TNc                     t          |||          }t          j        || j        dz
  |           t          j        |d|           t          | j        ||           |S )NrE   r   r   r   )r%   r   rF   ro   rG   r   r1   s       r   r2   z!InvertedPowerDistStretch.__call__M  sf    &t555
FDHsN7777
vqf%%%%dhF++++r   c                 ,    t          | j                  S rt   )r
   ro   r)   s    r   r6   z InvertedPowerDistStretch.inverseT  s      $(++++r   rw   r7   rJ   rL   s   @r   rv   rv   5  sv         "         , , X, , , , ,r   rv   c                   8     e Zd ZdZ fdZed             Z xZS )r   zt
    A convenience class for a power stretch of 2.

    The stretch is given by:

    .. math::
        y = x^2
    c                 J    t                                          d           d S )Nr\   )r?   r@   )r*   rC   s    r   r@   zSquaredStretch.__init__d  s!    r   c                     t                      S r5   )r   r)   s    r   r6   zSquaredStretch.inverseg  s     }}r   )r8   r9   r:   r;   r@   r<   r6   rK   rL   s   @r   r   r   Z  s]                X    r   r   c                   X     e Zd ZdZed             Zd	 fd	Zd
dZed             Z x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                     dS rO   r(   r)   s    r   r+   zLogStretch._supports_invalid_kw}  rP   r   rk   c                     t                                                       |dk    rt          d          || _        d S ra   r?   r@   rb   ro   rd   s     r   r@   zLogStretch.__init__  ;    66_---r   TNc                    t          |||          }| o|du}t          j        d          5  |r|dk     }t          j        || j        |           t          j        |d|           t          j        ||           t          j        |t          j        | j        dz             |           ddd           n# 1 swxY w Y   |r|||<   |S )rR   rE   NrS   rT   r   r   r   )r%   r   rV   rF   ro   rG   r   r   rX   s          r   r2   zLogStretch.__call__  s9   : &t555"(:wd':[*** 	G 	G !qjKf5555F63F++++F6v&&&&N626$(S.#9#9vFFFF	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G  	" "F3Ks   BB>>CCc                 *    t          | j                  S r   )InvertedLogStretchro   r)   s    r   r6   zLogStretch.inverse  s     "$(+++r   rw   r]   ri   rL   s   @r   r   r   m  s            X     , , , ,\ , , X, , , , ,r   r   c                   @     e Zd ZdZ fdZddZed             Z xZS )r   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                     t                                                       |dk    rt          d          || _        d S ra   r   rd   s     r   r@   zInvertedLogStretch.__init__  r   r   TNc                    t          |||          }t          j        |t          j        | j        dz             |           t          j        ||           t          j        |d|           t          j        || j        |           |S )NrE   r   r   )r%   r   rF   r   ro   rr   r   r1   s       r   r2   zInvertedLogStretch.__call__  s    &t555
FBF48c>22????
v6""""
FCV,,,,
vtxV4444r   c                 *    t          | j                  S r   )r   ro   r)   s    r   r6   zInvertedLogStretch.inverse  s     $(###r   r7   rJ   rL   s   @r   r   r     sq                  $ $ X$ $ $ $ $r   r   c                   B     e Zd ZdZd fd	Zd	dZed             Z x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                     t                                                       |dk    s|dk    rt          d          || _        d S Nr   r   za must be > 0 and <= 1r?   r@   rb   re   rd   s     r   r@   zAsinhStretch.__init__  D    66QUU5666r   TNc                     t          |||          }t          j        || j        |           t          j        ||           t          j        |t          j        d| j        z            |           |S NrE   r   r   )r%   r   r   re   arcsinhr1   s       r   r2   zAsinhStretch.__call__  sp    &t555
vtv62222

6v&&&&
vrz#,77VDDDDr   c                 \    t          dt          j        d| j        z            z            S r5   r   ru   )r   r   r   re   r)   s    r   r6   zAsinhStretch.inverse   s*     S2:cDFl#;#;;<<<<r   )r   r7   rJ   rL   s   @r   r   r     sv         $         = = X= = = = =r   r   c                   B     e Zd ZdZd fd	Zd	dZed             Z x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                     t                                                       |dk    s|dk    rt          d          || _        d S r   r   rd   s     r   r@   zSinhStretch.__init__  r   r   TNc                     t          |||          }t          j        || j        |           t          j        ||           t          j        |t          j        d| j        z            |           |S r   )r%   r   r   re   sinhr1   s       r   r2   zSinhStretch.__call__  sp    &t555
vtv62222
F####
vrwsTV|44&AAAAr   c                 \    t          dt          j        d| j        z            z            S r   )r   r   r   re   r)   s    r   r6   zSinhStretch.inverse$  s*     cBGC$&L$9$99::::r   )r   r7   rJ   rL   s   @r   r   r     sv                   ; ; X; ; ; ; ;r   r   c                   8    e Zd ZdZddZddZe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                    t          j        |                                          | _        | j        t          j        | j                           | _        | j                                        }| j                                        }| j        |z
  ||z
  z  | _        |/t          j        ddt          | j                            | _	        d S || _	        d S Nr   r   )
r   sortraveldataisfiniteminmaxlinspacelenr$   )r*   r   r$   vminvmaxs        r   r@   zHistEqStretch.__init__7  s    GDJJLL))	Ibk$)445	y}}y}}Y%$+6	 >+c3DI??DKKK DKKKr   Tc                 t    t          |||          }t          j        || j        | j                  |d d <   |S NrE   )r%   r   interpr   r$   r1   s       r   r2   zHistEqStretch.__call__E  s:    &t555Ifdi==qqq	r   c                 8    t          | j        | j                  S r5   )r$   )InvertedHistEqStretchr   r$   r)   s    r   r6   zHistEqStretch.inverseJ  s     %TYt{CCCCr   r-   r7   r8   r9   r:   r;   r@   r2   r<   r6   r(   r   r   r   r   *  sf        
 
! ! ! !   
 D D XD D Dr   r   c                   8    e Zd ZdZddZddZed             ZdS )	r   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                     |t          j        |                   | _        |/t          j        ddt	          | j                            | _        d S || _        d S r   )r   r   r   r   r   r$   )r*   r   r$   s      r   r@   zInvertedHistEqStretch.__init__]  sI    T**+	>+c3DI??DKKK DKKKr   Tc                 t    t          |||          }t          j        || j        | j                  |d d <   |S r   )r%   r   r   r$   r   r1   s       r   r2   zInvertedHistEqStretch.__call__d  s:    &t555Ifdk49==qqq	r   c                 8    t          | j        | j                  S r   )r   r   r$   r)   s    r   r6   zInvertedHistEqStretch.inversei  s     TYt{;;;;r   r-   r7   r   r(   r   r   r   r   P  sa        
 
! ! ! !   
 < < X< < <r   r   c                   @     e Zd ZdZ fdZddZed             Z x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                 d    t                                                       || _        || _        d S r-   r?   r@   contrastbiasr*   r   r   rC   s      r   r@   zContrastBiasStretch.__init__  +     			r   TNc                     t          |d|          }t          j        || j        |           t          j        || j        |           t          j        |d|           |rt          j        |dd|           |S )NFrE   r         ?r   r   )r%   r   rr   r   rF   r   rG   r!   r1   s       r   r2   zContrastBiasStretch.__call__  s     &u#666
FDI62222
FDMv6666
vs'''' 	.GFAqf----r   c                 6    t          | j        | j                  S r   )InvertedContrastBiasStretchr   r   r)   s    r   r6   zContrastBiasStretch.inverse  s     +4=$)DDDr   r7   rJ   rL   s   @r   r   r   o  sx         &    
    E E XE E E E Er   r   c                   @     e Zd ZdZ fdZddZed             Z xZS )r   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                 d    t                                                       || _        || _        d S r-   r   r   s      r   r@   z$InvertedContrastBiasStretch.__init__  r   r   TNc                     t          |d|          }t          j        |d|           t          j        || j        |           t          j        || j        |           |rt          j        |dd|           |S )NFrE   r   r   r   r   )r%   r   rr   r   r   rG   r   r!   r1   s       r   r2   z$InvertedContrastBiasStretch.__call__  s     &u#666
FCV,,,,
vt}&9999
vtyf---- 	.GFAqf----r   c                 6    t          | j        | j                  S r   )r   r   r   r)   s    r   r6   z#InvertedContrastBiasStretch.inverse  s     #4=$)<<<r   r7   rJ   rL   s   @r   r   r     sq             
    = = X= = = = =r   r   c                       e Zd ZdZ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                 ^    |                      |                     |||          ||          S r   )transform_2transform_1r1   s       r   r2   zCompositeStretch.__call__  s;    V$C88t   
 
 	
r   r7   )r8   r9   r:   r;   r2   r(   r   r   r   r     s2        	 	
 
 
 
 
 
r   r   r-   r7   )r;   numpyr   	transformr   r   __all__r   r%   r   r   r   r	   r
   rv   r   r   r   r   r   r   r   r   r   r   r(   r   r   <module>r      sX   
     8 8 8 8 8 8 8 8         &D &D &D &D &D- &D &D &DR!M !M !M !M !MK !M !M !MH< < < < <+ < < <~L. L. L. L. L.; L. L. L.^!4 !4 !4 !4 !4{ !4 !4 !4H", ", ", ", ",{ ", ", ",J    \   &K, K, K, K, K, K, K, K,\"$ "$ "$ "$ "$ "$ "$ "$J#= #= #= #= #=; #= #= #=L!; !; !; !; !;+ !; !; !;H#D #D #D #D #DK #D #D #DL< < < < <K < < <>*E *E *E *E *E+ *E *E *EZ$= $= $= $= $=+ $= $= $=N
 
 
 
 
); 
 
 
 
 
r   