B
    dR                 @   sZ  d Z ddlZddlmZ ddl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   j/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/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.
    g        g      ?)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_kw6   s    z BaseStretch._supports_invalid_kwc             C   s
   t || S )N)r   )r   otherr   r   r   __add__:   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__=   s    zBaseStretch.__call__c             C   s   dS )z5A stretch object that performs the inverse operation.Nr   )r   r   r   r   inverseS   s    zBaseStretch.inverse)TN)	__name__
__module____qualname____doc__propertyr    r"   r#   r$   r   r   r   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   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 )N)super__init__slope	intercept)r   r,   r-   )	__class__r   r   r+   i   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   r,   r   multiplyr-   add)r   r   r   r   r   r   r   r#   n   s    

zLinearStretch.__call__c             C   s   t d| j | j | j S )z5A stretch object that performs the inverse operation.g      ?)r   r,   r-   )r   r   r   r   r$   v   s    zLinearStretch.inverse)r   r   )TN)	r%   r&   r'   r(   r+   r#   r)   r$   __classcell__r   r   )r.   r   r   X   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 dQ R X |rZ|||< |S )aZ  
        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.
        )r   r   Nignore)invalidr   )r   )r   r   errstatesqrt)r   r   r   r   r3   replace_invalididxr   r   r   r#      s    zSqrtStretch.__call__c             C   s   t dS )z5A stretch object that performs the inverse operation.   )r   )r   r   r   r   r$      s    zSqrtStretch.inverse)TNN)r%   r&   r'   r(   r)   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 )NTr   )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)r*   r+   
ValueErrorpower)r   a)r.   r   r   r+      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 dQ R X |r|||< |S )	aZ  
        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.
        )r   r   Nr   r   r2   )r3   )r   )r   r:   r   r4   )r   r   r   r   r3   r6   r7   r   r   r   r#      s    zPowerStretch.__call__c             C   s   t d| j S )z5A stretch object that performs the inverse operation.g      ?)r   r:   )r   r   r   r   r$     s    zPowerStretch.inverse)TNN)
r%   r&   r'   r(   r)   r    r+   r#   r$   r1   r   r   )r.   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	   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)r9   r*   r+   exp)r   r;   )r.   r   r   r+     s    
zPowerDistStretch.__init__TNc             C   sJ   t |||d}tj| j||d tj|d|d tj|| jd |d |S )N)r   r   )r   r   g      ?)r   r   r:   r>   subtractr   )r   r   r   r   r   r   r   r#   !  s
    zPowerDistStretch.__call__c             C   s   t | jdS )z5A stretch object that performs the inverse operation.)r;   )InvertedPowerDistStretchr>   )r   r   r   r   r$   (  s    zPowerDistStretch.inverse)r=   )TN)	r%   r&   r'   r(   r+   r#   r)   r$   r1   r   r   )r.   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@   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.
         @@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)r9   r*   r+   r>   )r   r;   )r.   r   r   r+   @  s    
z!InvertedPowerDistStretch.__init__TNc             C   sH   t |||d}tj|| jd |d tj|d|d t| j||d |S )N)r   r   g      ?)r   r   )r   r   r/   r>   r0   r   )r   r   r   r   r   r   r   r#   F  s
    z!InvertedPowerDistStretch.__call__c             C   s   t | jdS )z5A stretch object that performs the inverse operation.)r;   )r	   r>   )r   r   r   r   r$   M  s    z InvertedPowerDistStretch.inverse)rA   )TN)	r%   r&   r'   r(   r+   r#   r)   r$   r1   r   r   )r.   r   r@   .  s   
r@   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 )Nr8   )r*   r+   )r   )r.   r   r   r+   ]  s    zSquaredStretch.__init__c             C   s   t  S )z5A stretch object that performs the inverse operation.)r   )r   r   r   r   r$   `  s    zSquaredStretch.inverse)r%   r&   r'   r(   r+   r)   r$   r1   r   r   )r.   r   r
   S  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 )NTr   )r   r   r   r   r    v  s    zLogStretch._supports_invalid_kw     @@c                s$   t    |dkrtd|| _d S )Nr   za must be > 0)r*   r+   r9   r>   )r   r;   )r.   r   r   r+   z  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 dQ R X |r|||< |S )aZ  
        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.
        )r   r   Nr2   )r3   r   )r   g      ?)r   r   r4   r/   r>   r0   r   r   )r   r   r   r   r3   r6   r7   r   r   r   r#     s    &zLogStretch.__call__c             C   s
   t | jS )z5A stretch object that performs the inverse operation.)InvertedLogStretchr>   )r   r   r   r   r$     s    zLogStretch.inverse)rB   )TNN)
r%   r&   r'   r(   r)   r    r+   r#   r$   r1   r   r   )r.   r   r   f  s
   
/c                   s6   e Zd ZdZ fddZd
ddZedd	 Z  ZS )rC   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 )Nr   za must be > 0)r*   r+   r9   r>   )r   r;   )r.   r   r   r+     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 )N)r   r   g      ?)r   )r   r   r/   r   r>   r?   r   )r   r   r   r   r   r   r   r#     s    zInvertedLogStretch.__call__c             C   s
   t | jS )z5A stretch object that performs the inverse operation.)r   r>   )r   r   r   r   r$     s    zInvertedLogStretch.inverse)TN)	r%   r&   r'   r(   r+   r#   r)   r$   r1   r   r   )r.   r   rC     s   
rC   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)r*   r+   r9   r;   )r   r;   )r.   r   r   r+     s    
zAsinhStretch.__init__TNc             C   sN   t |||d}tj|| j|d tj||d tj|td| j |d |S )N)r   r   )r   g      ?)r   r   r   r;   arcsinh)r   r   r   r   r   r   r   r#     s
    zAsinhStretch.__call__c             C   s   t dtd| j  dS )z5A stretch object that performs the inverse operation.g      ?)r;   )r   r   rE   r;   )r   r   r   r   r$     s    zAsinhStretch.inverse)rD   )TN)	r%   r&   r'   r(   r+   r#   r)   r$   r1   r   r   )r.   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 )Nr   r   za must be > 0 and <= 1)r*   r+   r9   r;   )r   r;   )r.   r   r   r+     s    
zSinhStretch.__init__TNc             C   sN   t |||d}tj|| j|d tj||d tj|td| j |d |S )N)r   r   )r   g      ?)r   r   r   r;   sinh)r   r   r   r   r   r   r   r#     s
    zSinhStretch.__call__c             C   s   t dtd| j  dS )z5A stretch object that performs the inverse operation.g      ?)r;   )r   r   rG   r;   )r   r   r   r   r$     s    zSinhStretch.inverse)rF   )TN)	r%   r&   r'   r(   r+   r#   r)   r$   r1   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 )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 )Ng        g      ?)
r   sortZraveldataisfiniteminmaxlinspacelenr   )r   rI   r   ZvminZvmaxr   r   r   r+   1  s    

zHistEqStretch.__init__Tc             C   s,   t |||d}t|| j| j|d d < |S )N)r   r   )r   r   interprI   r   )r   r   r   r   r   r   r   r#   ?  s    zHistEqStretch.__call__c             C   s   t | j| jdS )z5A stretch object that performs the inverse operation.)r   )InvertedHistEqStretchrI   r   )r   r   r   r   r$   D  s    zHistEqStretch.inverse)N)TN)r%   r&   r'   r(   r+   r#   r)   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 )rP   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 )Ng        g      ?)r   rJ   rI   rM   rN   r   )r   rI   r   r   r   r   r+   W  s    zInvertedHistEqStretch.__init__Tc             C   s,   t |||d}t|| j| j|d d < |S )N)r   r   )r   r   rO   r   rI   )r   r   r   r   r   r   r   r#   ^  s    zInvertedHistEqStretch.__call__c             C   s   t | j| jdS )z5A stretch object that performs the inverse operation.)r   )r   rI   r   )r   r   r   r   r$   c  s    zInvertedHistEqStretch.inverse)N)TN)r%   r&   r'   r(   r+   r#   r)   r$   r   r   r   r   rP   J  s   

rP   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 )N)r*   r+   contrastbias)r   rQ   rR   )r.   r   r   r+   }  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 )NF)r   r   )r   g      ?r   r   )r   r   r?   rR   r/   rQ   r0   r   )r   r   r   r   r   r   r   r#     s    zContrastBiasStretch.__call__c             C   s   t | j| jS )z5A stretch object that performs the inverse operation.)InvertedContrastBiasStretchrQ   rR   )r   r   r   r   r$     s    zContrastBiasStretch.inverse)TN)	r%   r&   r'   r(   r+   r#   r)   r$   r1   r   r   )r.   r   r   i  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 )N)r*   r+   rQ   rR   )r   rQ   rR   )r.   r   r   r+     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 )NF)r   r   g      ?)r   r   r   )r   r   r?   r   rQ   r0   rR   r   )r   r   r   r   r   r   r   r#     s    z$InvertedContrastBiasStretch.__call__c             C   s   t | j| jS )z5A stretch object that performs the inverse operation.)r   rQ   rR   )r   r   r   r   r$     s    z#InvertedContrastBiasStretch.inverse)TN)	r%   r&   r'   r(   r+   r#   r)   r$   r1   r   r   )r.   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 )N)r   r   )Ztransform_2Ztransform_1)r   r   r   r   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	   r@   r
   r   rC   r   r   r   rP   r   rS   r   r   r   r   r   <module>   s2   

)$@N$%O%&$&-'