B
    d$                 @   s   d Z ddlZddlZddlmZ ddddd	d
g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 computing intervals from arrays of values based on
various criteria.
    N   )BaseTransformBaseIntervalManualIntervalMinMaxIntervalAsymmetricPercentileIntervalPercentileIntervalZScaleIntervalc               @   s(   e Zd ZdZejdd ZdddZdS )	r   z
    Base class for the interval classes, which, when called with an
    array of values, return an interval computed following different
    algorithms.
    c             C   s   t ddS )aH  
        Return the minimum and maximum value in the interval based on
        the values provided.

        Parameters
        ----------
        values : ndarray
            The image values.

        Returns
        -------
        vmin, vmax : float
            The mininium and maximum image value in the interval.
        z&Needs to be implemented in a subclass.N)NotImplementedError)selfvalues r   k/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/astropy/visualization/interval.py
get_limits   s    zBaseInterval.get_limitsTNc             C   s   |  |\}}|dkr(t|t|}n(|jjdkr<tdtj|t||d}|| dkrptj||| |d |rtj|dd|d |S )a!  
        Transform values using this interval.

        Parameters
        ----------
        values : array-like
            The input values.
        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.
        Nfz6Can only do in-place scaling for floating-point arrays)outr   g        g      ?)	r   npsubtractfloatdtypekind	TypeErrorZtrue_divideclip)r   r   r   r   vminvmaxr   r   r   __call__-   s    zBaseInterval.__call__)TN)__name__
__module____qualname____doc__abcabstractmethodr   r   r   r   r   r   r      s   c               @   s"   e Zd ZdZdddZdd ZdS )r   aI  
    Interval based on user-specified values.

    Parameters
    ----------
    vmin : float, optional
        The minimum value in the scaling.  Defaults to the image
        minimum (ignoring NaNs)
    vmax : float, optional
        The maximum value in the scaling.  Defaults to the image
        maximum (ignoring NaNs)
    Nc             C   s   || _ || _d S )N)r   r   )r   r   r   r   r   r   __init__c   s    zManualInterval.__init__c             C   sX   t | }|t | }| jd kr0t |n| j}| jd krJt |n| j}||fS )N)r   asarrayravelisfiniter   minr   max)r   r   r   r   r   r   r   r   g   s
    zManualInterval.get_limits)NN)r   r   r   r   r"   r   r   r   r   r   r   U   s   
c               @   s   e Zd ZdZdd ZdS )r   zG
    Interval based on the minimum and maximum values in the data.
    c             C   s0   t | }|t | }t |t |fS )N)r   r#   r$   r%   r&   r'   )r   r   r   r   r   r   x   s    zMinMaxInterval.get_limitsN)r   r   r   r   r   r   r   r   r   r   s   s   c               @   s"   e Zd ZdZdddZdd ZdS )r   a  
    Interval based on a keeping a specified fraction of pixels (can be
    asymmetric).

    Parameters
    ----------
    lower_percentile : float
        The lower percentile below which to ignore pixels.
    upper_percentile : float
        The upper percentile above which to ignore pixels.
    n_samples : int, optional
        Maximum number of values to use. If this is specified, and there
        are more values in the dataset as this, then values are randomly
        sampled from the array (with replacement).
    Nc             C   s   || _ || _|| _d S )N)lower_percentileupper_percentile	n_samples)r   r(   r)   r*   r   r   r   r"      s    z%AsymmetricPercentileInterval.__init__c             C   sb   t | }| jd k	r4|j| jkr4t j|| j}|t | }t || j	| j
f\}}||fS )N)r   r#   r$   r*   sizerandomchoicer%   
percentiler(   r)   )r   r   r   r   r   r   r   r      s    
z'AsymmetricPercentileInterval.get_limits)N)r   r   r   r   r"   r   r   r   r   r   r      s   
c                   s"   e Zd ZdZd fdd	Z  ZS )r   a  
    Interval based on a keeping a specified fraction of pixels.

    Parameters
    ----------
    percentile : float
        The fraction of pixels to keep. The same fraction of pixels is
        eliminated from both ends.
    n_samples : int, optional
        Maximum number of values to use. If this is specified, and there
        are more values in the dataset as this, then values are randomly
        sampled from the array (with replacement).
    Nc                s*   d| d }d| }t  j|||d d S )Nd   g      ?)r*   )superr"   )r   r.   r*   r(   r)   )	__class__r   r   r"      s    zPercentileInterval.__init__)N)r   r   r   r   r"   __classcell__r   r   )r1   r   r      s   c               @   s"   e Zd ZdZdddZd	d
 ZdS )r	   aY  
    Interval based on IRAF's zscale.

    https://iraf.net/forum/viewtopic.php?showtopic=134139

    Original implementation:
    https://github.com/spacetelescope/stsci.numdisplay/blob/master/lib/stsci/numdisplay/zscale.py

    Licensed under a 3-clause BSD style license (see AURA_LICENSE.rst).

    Parameters
    ----------
    nsamples : int, optional
        The number of points in the array to sample for determining
        scaling factors.  Defaults to 1000.
    contrast : float, optional
        The scaling factor (between 0 and 1) for determining the minimum
        and maximum value.  Larger values increase the difference
        between the minimum and maximum values used for display.
        Defaults to 0.25.
    max_reject : float, optional
        If more than ``max_reject * npixels`` pixels are rejected, then
        the returned values are the minimum and maximum of the data.
        Defaults to 0.5.
    min_npixels : int, optional
        If there are less than ``min_npixels`` pixels remaining after
        the pixel rejection, then the returned values are the minimum
        and maximum of the data.  Defaults to 5.
    krej : float, optional
        The number of sigma used for the rejection. Defaults to 2.5.
    max_iterations : int, optional
        The maximum number of iterations for the rejection. Defaults to
        5.
            ?      ?         @c             C   s(   || _ || _|| _|| _|| _|| _d S )N)nsamplescontrast
max_rejectmin_npixelskrejmax_iterations)r   r8   r9   r:   r;   r<   r=   r   r   r   r"      s    zZScaleInterval.__init__c             C   s  t |}|t | }ttd|j| j }|d d | d | j }|  t|}|d }|d }t| j	t|| j
 }t |}|}	|d }
t j|td}tdt|d }t j|td}xt| jD ]}|	|
ks|	|k rP t j||d| td}t ||}|| }| j||    }d||| k ||kB < t j||d	d
}|	}
t | }	qW |	|kr|\}}| jdkr|| j }|d d }t |}t|||d |  }t|||| |  }||fS )Ng      ?r   r   )r   g{Gz?)degwTZsame)mode   )r   r#   r%   intr'   r+   r8   sortlenr;   r:   ZarangeZzerosboolZonesranger=   ZpolyfitZastypeZpoly1dr<   ZstdZconvolvesumr9   medianr&   )r   r   ZstrideZsamplesZnpixr   r   ZminpixxZngoodpixZlast_ngoodpixZbadpixZngrowZkernel_fitZfittedZflat	thresholdZslopeZcenter_pixelrI   r   r   r   r      sF    




zZScaleInterval.get_limitsN)r3   r4   r5   r6   r7   r6   )r   r   r   r   r"   r   r   r   r   r   r	      s   " 
)r   r    numpyr   Z	transformr   __all__r   r   r   r   r   r	   r   r   r   r   <module>   s   B)