B
    q\7                 @   sJ  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gZd*ddZd+ddZ	G dd dee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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)InheritDocstrings   )BaseTransformBaseStretchLinearStretchSqrtStretchPowerStretchPowerDistStretchSquaredStretch
LogStretchAsinhStretchSinhStretchHistEqStretchContrastBiasStretchc             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   <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   clipZarrayZasarray)valuesr   r   r   r   r   _prepare   s    r   c               @   s&   e Zd Z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].
    TNc             C   s   dS )au  
        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 : `~numpy.ndarray`, optional
            If specified, the output values will be placed in this array
            (typically used for in-place calculations).

        Returns
        -------
        result : `~numpy.ndarray`
            The transformed values.
        Nr   )selfr   r   r   r   r   r   __call__6   s    zBaseStretch.__call__c             C   s   dS )z5A stretch object that performs the inverse operation.Nr   )r   r   r   r   inverseL   s    zBaseStretch.inverse)TN)__name__
__module____qualname____doc__r   propertyr   r   r   r   r   r   /   s   
)	metaclassc               @   s&   e Zd ZdZdddZedd ZdS )	r   zV
    A linear stretch.

    The stretch is given by:

    .. math::
        y = x
    TNc             C   s   t |||dS )N)r   r   )r   )r   r   r   r   r   r   r   r   [   s    zLinearStretch.__call__c             C   s   t  S )z5A stretch object that performs the inverse operation.)r   )r   r   r   r   r   ^   s    zLinearStretch.inverse)TN)r    r!   r"   r#   r   r$   r   r   r   r   r   r   Q   s   
c               @   s&   e Zd Z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}
    TNc          	   C   s8   t |||d}tjdd tj||d W d Q R X |S )N)r   r   ignore)Zinvalid)r   )r   r   ZerrstateZsqrt)r   r   r   r   r   r   r   r   n   s    zSqrtStretch.__call__c             C   s   t dS )z5A stretch object that performs the inverse operation.   )r   )r   r   r   r   r   t   s    zSqrtStretch.inverse)TN)r    r!   r"   r#   r   r$   r   r   r   r   r   r   d   s   
c                   s6   e Zd Z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).
    c                s   t    || _d S )N)super__init__power)r   a)	__class__r   r   r)      s    
zPowerStretch.__init__TNc             C   s$   t |||d}tj|| j|d |S )N)r   r   )r   )r   r   r*   )r   r   r   r   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)TN)	r    r!   r"   r#   r)   r   r$   r   __classcell__r   r   )r,   r   r   z   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  
    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.  Default is 1000.
        ``a`` cannot be set to 1.
         @@c                s$   |dkrt dt   || _d S )Nr   za cannot be set to 1)
ValueErrorr(   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*   r0   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+   )InvertedPowerDistStretchr0   )r   r   r   r   r      s    zPowerDistStretch.inverse)r.   )TN)	r    r!   r"   r#   r)   r   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 )r2   aO  
    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.  Default is 1000.
        ``a`` cannot be set to 1.
         @@c                s$   |dkrt dt   || _d S )Nr   za cannot be set to 1)r/   r(   r)   r0   )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   multiplyr0   addr   )r   r   r   r   r   r   r   r      s
    z!InvertedPowerDistStretch.__call__c             C   s   t | jdS )z5A stretch object that performs the inverse operation.)r+   )r	   r0   )r   r   r   r   r      s    z InvertedPowerDistStretch.inverse)r3   )TN)	r    r!   r"   r#   r)   r   r$   r   r-   r   r   )r,   r   r2      s   
r2   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 )Nr'   )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   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   z
    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.  Default is 1000.
         @@c                s   t    || _d S )N)r(   r)   r0   )r   r+   )r,   r   r   r)     s    
zLogStretch.__init__TNc             C   s^   t |||d}tj|| j|d tj|d|d tj||d tj|t| jd |d |S )N)r   r   )r   g      ?)r   r   r4   r0   r5   r   r   )r   r   r   r   r   r   r   r     s    zLogStretch.__call__c             C   s
   t | jS )z5A stretch object that performs the inverse operation.)InvertedLogStretchr0   )r   r   r   r   r     s    zLogStretch.inverse)r6   )TN)	r    r!   r"   r#   r)   r   r$   r   r-   r   r   )r,   r   r      s   
c                   s6   e Zd ZdZ fddZd
ddZedd	 Z  ZS )r7   a  
    Inverse transformation for `~astropy.image.scaling.LogStretch`.

    The stretch is given by:

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

    Parameters
    ----------
    a : float, optional
        The ``a`` parameter used in the above formula.  Default is 1000.
    c                s   t    || _d S )N)r(   r)   r0   )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   r4   r   r0   r1   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   r0   )r   r   r   r   r   .  s    zInvertedLogStretch.inverse)TN)	r    r!   r"   r#   r)   r   r$   r   r-   r   r   )r,   r   r7     s   
r7   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.  Must be in the range between 0 and 1.
        Default is 0.1
    皙?c                s   t    || _d S )N)r(   r)   r+   )r   r+   )r,   r   r   r)   G  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   K  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   r9   r+   )r   r   r   r   r   R  s    zAsinhStretch.inverse)r8   )TN)	r    r!   r"   r#   r)   r   r$   r   r-   r   r   )r,   r   r   4  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   z
    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.  Default is 1/3.
    UUUUUU?c                s   t    || _d S )N)r(   r)   r+   )r   r+   )r,   r   r   r)   g  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   k  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   r;   r+   )r   r   r   r   r   r  s    zSinhStretch.inverse)r:   )TN)	r    r!   r"   r#   r)   r   r$   r   r-   r   r   )r,   r   r   X  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   sb   t | | _| j }| j }| j| ||  | _|d krXt ddt| j| _n|| _d S )Ng        g      ?)	r   sortZraveldataminmaxlinspacelenr   )r   r=   r   ZvminZvmaxr   r   r   r)     s    

zHistEqStretch.__init__Tc             C   s,   t |||d}t|| j| j|d d < |S )N)r   r   )r   r   interpr=   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   )InvertedHistEqStretchr=   r   )r   r   r   r   r     s    zHistEqStretch.inverse)N)TN)r    r!   r"   r#   r)   r   r$   r   r   r   r   r   r   x  s   

c               @   s0   e Zd ZdZd
ddZdddZedd	 ZdS )rC   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   s0   || _ |d kr&tddt| j | _n|| _d S )Ng        g      ?)r=   r   r@   rA   r   )r   r=   r   r   r   r   r)     s    zInvertedHistEqStretch.__init__Tc             C   s,   t |||d}t|| j| j|d d < |S )N)r   r   )r   r   rB   r   r=   )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   r=   r   )r   r   r   r   r     s    zInvertedHistEqStretch.inverse)N)TN)r    r!   r"   r#   r)   r   r$   r   r   r   r   r   rC     s   

rC   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   rD   rE   )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   r1   rE   r4   rD   r5   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.)InvertedContrastBiasStretchrD   rE   )r   r   r   r   r     s    zContrastBiasStretch.inverse)TN)	r    r!   r"   r#   r)   r   r$   r   r-   r   r   )r,   r   r     s   
c                   s6   e Zd ZdZ fddZd
ddZedd	 Z  ZS )rF   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)   rD   rE   )r   rD   rE   )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   r1   r   rD   r5   rE   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   rD   rE   )r   r   r   r   r     s    z#InvertedContrastBiasStretch.inverse)TN)	r    r!   r"   r#   r)   r   r$   r   r-   r   r   )r,   r   rF     s   
rF   )N)TN)r#   Znumpyr   Zastropy.utils.miscr   Z	transformr   __all__r   r   r   r   r   r   r	   r2   r
   r   r7   r   r   r   rC   r   rF   r   r   r   r   <module>   s.   


"#$!!$ &-