U
    e»{jû5  ã                   @   sî   d Z ddlZddlZddlmZ ddlmZmZmZm	Z	m
Z
 ddlmZmZmZmZmZmZmZ z$ddlZddlmZ ddlmZ W n" ek
r¦   G d	d
„ d
ƒZY nX dddgZddgiZG dd„ deƒZddd„Ze e¡Zddd„ZdS )zK
Normalization class for Matplotlib that can be used to produce
colorbars.
é    N)Úmaé   )ÚAsymmetricPercentileIntervalÚBaseIntervalÚManualIntervalÚMinMaxIntervalÚPercentileInterval)ÚAsinhStretchÚBaseStretchÚLinearStretchÚ
LogStretchÚPowerStretchÚSinhStretchÚSqrtStretch)Úpyplot)Ú	Normalizec                   @   s   e Zd Zdd„ ZdS )r   c                 O   s   t dƒ‚d S )Nz2matplotlib is required in order to use this class.)ÚImportError)ÚselfÚargsÚkwargs© r   úb/var/www/html/bwave/python/venv/lib/python3.8/site-packages/astropy/visualization/mpl_normalize.pyÚ__init__#   s    zNormalize.__init__N)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   "   s   r   ÚImageNormalizeÚsimple_normÚimshow_normÚ*Ú
matplotlibc                       sN   e Zd ZdZddddeƒ ddf‡ fdd„	Zdd„ Zdd	d
„Zddd„Z‡  Z	S )r   aƒ  
    Normalization class to be used with Matplotlib.

    Parameters
    ----------
    data : ndarray, optional
        The image array.  This input is used only if ``interval`` is
        also input.  ``data`` and ``interval`` are used to compute the
        vmin and/or vmax values only if ``vmin`` or ``vmax`` are not
        input.
    interval : `~astropy.visualization.BaseInterval` subclass instance, optional
        The interval object to apply to the input ``data`` to determine
        the ``vmin`` and ``vmax`` values.  This input is used only if
        ``data`` is also input.  ``data`` and ``interval`` are used to
        compute the vmin and/or vmax values only if ``vmin`` or ``vmax``
        are not input.
    vmin, vmax : float, optional
        The minimum and maximum levels to show for the data.  The
        ``vmin`` and ``vmax`` inputs override any calculated values from
        the ``interval`` and ``data`` inputs.
    stretch : `~astropy.visualization.BaseStretch` subclass instance
        The stretch object to apply to the data.  The default is
        `~astropy.visualization.LinearStretch`.
    clip : bool, optional
        If `True`, data values outside the [0:1] range are clipped to
        the [0:1] range.
    invalid : None or float, optional
        Value to assign NaN values generated by this class.  NaNs in the
        input ``data`` array are not changed.  For matplotlib
        normalization, 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``.
    NFç      ð¿c                    s�   t ƒ j|||d� || _|| _|d kr.tdƒ‚t|tƒs@tdƒ‚|| _|d k	r`t|t	ƒs`tdƒ‚|| _
|j| _|| _|| _|d k	rŒ|  |¡ d S )N)ÚvminÚvmaxÚclipzstretch must be inputz5stretch must be an instance of a BaseStretch subclassz7interval must be an instance of a BaseInterval subclass)Úsuperr   r"   r#   Ú
ValueErrorÚ
isinstancer
   Ú	TypeErrorÚstretchr   ÚintervalÚinverseÚinverse_stretchr$   ÚinvalidÚ_set_limits)r   Údatar*   r"   r#   r)   r$   r-   ©Ú	__class__r   r   r   P   s     
zImageNormalize.__init__c                 C   s˜   | j d k	r| jd k	rd S | jd krd| j d krBt |t |¡ ¡| _ | jd kr”t |t |¡ ¡| _n0| j |¡\}}| j d kr„|| _ | jd kr”|| _d S )N)r"   r#   r*   ÚnpÚminÚisfiniteÚmaxÚ
get_limits)r   r/   Z_vminZ_vmaxr   r   r   r.   r   s    




zImageNormalize._set_limitsc                 C   sú   |dkr| j }|dkr| j}t|tjƒrF|r2d}n|j}| | j¡}nd}t 	|¡rftj
|gtd�}ntj
|dtd�}|  |¡ tj|| j|d� tj|| j| j |d� |rÀtj |dd|d�}| jjrÜ| j||d|d	�}n| j||dd
�}tj
||d�S )ar  
        Transform values using this normalization.

        Parameters
        ----------
        values : array-like
            The input values.
        clip : bool, optional
            If `True`, values outside the [0:1] range are clipped to the
            [0:1] range.  If `None` then the ``clip`` value from the
            `ImageNormalize` instance is used (the default of which is
            `False`).
        invalid : None or float, optional
            Value to assign NaN values generated by this class.  NaNs in
            the input ``data`` array are not changed.  For matplotlib
            normalization, the ``invalid`` value should map to the
            matplotlib colormap "under" value (i.e., any finite value <
            0).  If `None`, then the `ImageNormalize` instance value is
            used.  This keyword has no effect if ``clip=True``.
        NF)ÚdtypeT)Úcopyr7   )Úoutç        ç      ð?)r9   r$   r-   )r9   r$   )Úmask)r$   r-   r'   r   ZMaskedArrayr<   Zfilledr#   r2   ZisscalarÚarrayÚfloatr.   Úsubtractr"   Ztrue_divider)   Ú_supports_invalid_kw)r   Úvaluesr$   r-   r<   r   r   r   Ú__call__ƒ   s,    

zImageNormalize.__call__c                 C   s>   | j jr| j |d|d�}n| j |dd�}|| j| j  | j S )NF)r$   r-   )r$   )r,   r@   r#   r"   )r   rA   r-   Zvalues_normr   r   r   r+   Ã   s    zImageNormalize.inverse)NN)N)
r   r   r   Ú__doc__r   r   r.   rB   r+   Ú__classcell__r   r   r0   r   r   ,   s   %ø"
@Úlinearr;   çš™™™™™¹?Féè  r!   ç333333Ó?c                 C   sð   |dk	rt |ƒ}nF|dk	s"|dk	r6t|p*d|p0dƒ}n"|dk	sF|dk	rRt||ƒ}ntƒ }|dkrhtƒ }nh|dkrxtƒ }nX|dkrŠt|ƒ}nF|dkrœt|
ƒ}n4|dkr®t|ƒ}n"|d	krÀt	|ƒ}nt
d
|› d�ƒ‚| | ¡\}}t||||	|d�S )aª  
    Return a Normalization class that can be used for displaying images
    with Matplotlib.

    This function enables only a subset of image stretching functions
    available in `~astropy.visualization.mpl_normalize.ImageNormalize`.

    This function is used by the
    ``astropy.visualization.scripts.fits2bitmap`` script.

    Parameters
    ----------
    data : ndarray
        The image array.

    stretch : {'linear', 'sqrt', 'power', log', 'asinh', 'sinh'}, optional
        The stretch function to apply to the image.  The default is
        'linear'.

    power : float, optional
        The power index for ``stretch='power'``.  The default is 1.0.

    asinh_a : float, optional
        For ``stretch='asinh'``, the value 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.  The default is 0.1.

    min_cut : float, optional
        The pixel value of the minimum cut level.  Data values less than
        ``min_cut`` will set to ``min_cut`` before stretching the image.
        The default is the image minimum.  ``min_cut`` overrides
        ``min_percent``.

    max_cut : float, optional
        The pixel value of the maximum cut level.  Data values greater
        than ``min_cut`` will set to ``min_cut`` before stretching the
        image.  The default is the image maximum.  ``max_cut`` overrides
        ``max_percent``.

    min_percent : float, optional
        The percentile value used to determine the pixel value of
        minimum cut level.  The default is 0.0.  ``min_percent``
        overrides ``percent``.

    max_percent : float, optional
        The percentile value used to determine the pixel value of
        maximum cut level.  The default is 100.0.  ``max_percent``
        overrides ``percent``.

    percent : float, optional
        The percentage of the image values used to determine the pixel
        values of the minimum and maximum cut levels.  The lower cut
        level will set at the ``(100 - percent) / 2`` percentile, while
        the upper cut level will be set at the ``(100 + percent) / 2``
        percentile.  The default is 100.0.  ``percent`` is ignored if
        either ``min_percent`` or ``max_percent`` is input.

    clip : bool, optional
        If `True`, data values outside the [0:1] range are clipped to
        the [0:1] range.

    log_a : float, optional
        The log index for ``stretch='log'``. The default is 1000.

    invalid : None or float, optional
        Value to assign NaN values generated by the normalization.  NaNs
        in the input ``data`` array are not changed.  For matplotlib
        normalization, 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``.

    sinh_a : float, optional
        The scaling parameter for ``stretch='sinh'``. The default is
        0.3.

    Returns
    -------
    result : `ImageNormalize` instance
        An `ImageNormalize` instance that can be used for displaying
        images with Matplotlib.
    Nr:   g      Y@rE   ÚsqrtÚpowerÚlogÚasinhÚsinhzUnknown stretch: Ú.)r"   r#   r)   r$   r-   )r   r   r   r   r   r   r   r   r	   r   r&   r6   r   )r/   r)   rJ   Zasinh_aZmin_cutZmax_cutZmin_percentZmax_percentÚpercentr$   Zlog_ar-   Zsinh_ar*   r"   r#   r   r   r   r   Î   s>    c
 ÿ



    ÿc                 K   s’   d|krt dƒ‚d|kr t dƒ‚t|ƒ}d| i}tjD ]}||kr6| |¡||< q6tf |Ž|d< |dkrxtj| f|Ž}n|j| f|Ž}||d fS )a*  A convenience function to call matplotlib's `matplotlib.pyplot.imshow`
    function, using an `ImageNormalize` object as the normalization.

    Parameters
    ----------
    data : 2D or 3D array-like
        The data to show. Can be whatever `~matplotlib.pyplot.imshow` and
        `ImageNormalize` both accept. See `~matplotlib.pyplot.imshow`.
    ax : None or `~matplotlib.axes.Axes`, optional
        If None, use pyplot's imshow.  Otherwise, calls ``imshow`` method of
        the supplied axes.
    **kwargs : dict, optional
        All other keyword arguments are parsed first by the
        `ImageNormalize` initializer, then to
        `~matplotlib.pyplot.imshow`.

    Returns
    -------
    result : tuple
        A tuple containing the `~matplotlib.image.AxesImage` generated
        by `~matplotlib.pyplot.imshow` as well as the `ImageNormalize`
        instance.

    Notes
    -----
    The ``norm`` matplotlib keyword is not supported.

    Examples
    --------
    .. plot::
        :include-source:

        import numpy as np
        import matplotlib.pyplot as plt
        from astropy.visualization import (imshow_norm, MinMaxInterval,
                                           SqrtStretch)

        # Generate and display a test image
        image = np.arange(65536).reshape((256, 256))
        fig = plt.figure()
        ax = fig.add_subplot(1, 1, 1)
        im, norm = imshow_norm(image, ax, origin='lower',
                               interval=MinMaxInterval(),
                               stretch=SqrtStretch())
        fig.colorbar(im)
    ÚXz#Cannot give both ``X`` and ``data``ZnormzoThere is no point in using imshow_norm if you give the ``norm`` keyword - use imshow directly if you want that.r/   N)r&   ÚdictÚ	_norm_sigÚ
parametersÚpopr   ÚpltZimshow)r/   Zaxr   Zimshow_kwargsZnorm_kwargsZpnameZimshow_resultr   r   r   r   V  s     /ÿ
)rE   r;   rF   NNNNNFrG   r!   rH   )N) rC   ÚinspectÚnumpyr2   r   r*   r   r   r   r   r   r)   r	   r
   r   r   r   r   r   r    r   rU   Zmatplotlib.colorsr   r   Ú__all__Z__doctest_requires__r   r   Ú	signaturerR   r   r   r   r   r   Ú<module>   s>   $


 %            ó
 
