ó
šßÈ[c           @` s2  d  d l  m Z m Z m Z m Z d  d l Z d  d l m Z m	 Z	 m
 Z
 d  d l m Z d  d l Z d d l m Z d d l m Z m Z d d l m Z d	 d
 d d d g Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ e j e ƒ d e f d „  ƒ  Yƒ Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   i    (   t   absolute_importt   divisiont   print_functiont   unicode_literalsN(   t   ABCMetat   abstractpropertyt   abstractmethod(   t   deepcopyi   (   t   log(   t   Unitt   Quantity(   t   sixu   MissingDataAssociationExceptionu"   IncompatibleUncertaintiesExceptionu   NDUncertaintyu   StdDevUncertaintyu   UnknownUncertaintyt"   IncompatibleUncertaintiesExceptionc           B` s   e  Z d  Z RS(   u   This exception should be used to indicate cases in which uncertainties
    with two different classes can not be propagated.
    (   t   __name__t
   __module__t   __doc__(    (    (    s;   lib/python2.7/site-packages/astropy/nddata/nduncertainty.pyR      s   t   MissingDataAssociationExceptionc           B` s   e  Z d  Z RS(   u–   This exception should be used to indicate that an uncertainty instance
    has not been associated with a parent `~astropy.nddata.NDData` object.
    (   R   R   R   (    (    (    s;   lib/python2.7/site-packages/astropy/nddata/nduncertainty.pyR      s   t   NDUncertaintyc           B` sï   e  Z d  Z d e d d „ Z e d „  ƒ Z e d „  ƒ Z	 e d „  ƒ Z
 e
 j d „  ƒ Z
 e d „  ƒ Z e d „  ƒ Z e j d „  ƒ Z d	 „  Z d
 „  Z d „  Z d „  Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z RS(   u  This is the metaclass for uncertainty classes used with `NDData`.

    Parameters
    ----------
    array : any type, optional
        The array or value (the parameter name is due to historical reasons) of
        the uncertainty. `numpy.ndarray`, `~astropy.units.Quantity` or
        `NDUncertainty` subclasses are recommended.
        If the `array` is `list`-like or `numpy.ndarray`-like it will be cast
        to a plain `numpy.ndarray`.
        Default is ``None``.

    unit : `~astropy.units.Unit` or str, optional
        Unit for the uncertainty ``array``. Strings that can be converted to a
        `~astropy.units.Unit` are allowed.
        Default is ``None``.

    copy : `bool`, optional
        Indicates whether to save the `array` as a copy. ``True`` copies it
        before saving, while ``False`` tries to save every parameter as
        reference. Note however that it is not always possible to save the
        input as reference.
        Default is ``True``.

    Raises
    ------
    IncompatibleUncertaintiesException
        If given another `NDUncertainty`-like class as ``array`` if their
        ``uncertainty_type`` is different.
    c         C` sJ  t  | t ƒ r| | j |  j k r* t ‚ n  | d  k	 rU | | j k rU t j d ƒ n | j d  k	 rp | j } n  | j	 } np t  | t
 ƒ rì | d  k	 rÅ | j d  k	 rÅ | | j k rÅ t j d ƒ n | j d  k	 rà | j } n  | j } n  | d  k rd  |  _ n t | ƒ |  _ | r4t | ƒ } t | ƒ } n  | |  _	 d  |  _ d  S(   Nu;   overwriting Uncertainty's current unit with specified unit.u8   overwriting Quantity's current unit with specified unit.(   t
   isinstanceR   t   uncertainty_typeR   t   Nonet   _unitR   t   infot   unitt   arrayR
   t   valueR	   R   t   parent_nddata(   t   selfR   t   copyR   (    (    s;   lib/python2.7/site-packages/astropy/nddata/nduncertainty.pyt   __init__B   s.    		c         C` s   d S(   u‹   `str` : Short description of the type of uncertainty.

        Defined as abstract property so subclasses *have* to override this.
        N(   R   (   R   (    (    s;   lib/python2.7/site-packages/astropy/nddata/nduncertainty.pyR   g   s    c         C` s   t  S(   u   `bool` : Supports uncertainty propagation with correlated                  uncertainties?

        .. versionadded:: 1.2
        (   t   False(   R   (    (    s;   lib/python2.7/site-packages/astropy/nddata/nduncertainty.pyt   supports_correlatedo   s    c         C` s   |  j  S(   u3   `numpy.ndarray` : the uncertainty's value.
        (   t   _array(   R   (    (    s;   lib/python2.7/site-packages/astropy/nddata/nduncertainty.pyR   x   s    c         C` sC   t  | t t j f ƒ r6 t j | d t d t ƒ} n  | |  _ d  S(   Nt   subokR   (   R   t   listt   npt   ndarrayR   R   R    (   R   R   (    (    s;   lib/python2.7/site-packages/astropy/nddata/nduncertainty.pyR   ~   s    c         C` sH   |  j  d k rA |  j d k s0 |  j j d k r4 d S|  j j Sn  |  j  S(   u@  `~astropy.units.Unit` : The unit of the uncertainty, if any.

        Even though it is not enforced the unit should be convertible to the
        ``parent_nddata`` unit. Otherwise uncertainty propagation might give
        wrong results.

        If the unit is not set the unit of the parent will be returned.
        N(   R   R   t   _parent_nddataR   R   (   R   (    (    s;   lib/python2.7/site-packages/astropy/nddata/nduncertainty.pyR   „   s    
c         C` s   d } yU |  j  d k r' t | ƒ ‚ n3 t |  j  t j ƒ rF |  j  ƒ  St j d ƒ |  j  SWn t k
 rz t | ƒ ‚ n Xd S(   uù   `NDData` : reference to `NDData` instance with this uncertainty.

        In case the reference is not set uncertainty propagation will not be
        possible since propagation might need the uncertain data besides the
        uncertainty.
        u3   uncertainty is not associated with an NDData objectu6   parent_nddata should be a weakref to an NDData object.N(	   R%   R   R   R   t   weakreft   refR   R   t   AttributeError(   R   t   message(    (    s;   lib/python2.7/site-packages/astropy/nddata/nduncertainty.pyR   –   s    
c         C` s>   | d  k	 r1 t | t j ƒ r1 t j | ƒ } n  | |  _ d  S(   N(   R   R   R&   R'   R%   (   R   R   (    (    s;   lib/python2.7/site-packages/astropy/nddata/nduncertainty.pyR   ®   s    c         C` sk   |  j  j d } y" t j |  j d d d | ƒ} Wn  t k
 rT t |  j ƒ } n Xd j | | d g ƒ S(   Nu   (t	   separatoru   , t   prefixu    u   )(   t	   __class__R   R#   t   array2stringR   R(   t   strt   join(   R   R+   t   body(    (    s;   lib/python2.7/site-packages/astropy/nddata/nduncertainty.pyt   __repr__·   s    "c         C` s#   |  j  |  j | d |  j d t ƒS(   uK   Normal slicing on the array, keep the unit and return a reference.
        R   R   (   R,   R   R   R   (   R   t   item(    (    s;   lib/python2.7/site-packages/astropy/nddata/nduncertainty.pyt   __getitem__À   s    c         C` s  |  j  sH t | t j ƒ s' | d k rH t d j |  j j ƒ ƒ ‚ qH n  |  j | j	 ƒ } | j d k r |  j
 | | | ƒ } n | j d k r¨ |  j | | | ƒ } nZ | j d k rÏ |  j | | | ƒ } n3 | j d
 k rö |  j | | | ƒ } n t d ƒ ‚ |  j | d	 t ƒS(   uh  Calculate the resulting uncertainty given an operation on the data.

        .. versionadded:: 1.2

        Parameters
        ----------
        operation : callable
            The operation that is performed on the `NDData`. Supported are
            `numpy.add`, `numpy.subtract`, `numpy.multiply` and
            `numpy.true_divide` (or `numpy.divide`).

        other_nddata : `NDData` instance
            The second operand in the arithmetic operation.

        result_data : `~astropy.units.Quantity` or `numpy.ndarray`
            The result of the arithmetic operations on the data.

        correlation : `numpy.ndarray` or number
            The correlation (rho) is defined between the uncertainties in
            sigma_AB = sigma_A * sigma_B * rho. A value of ``0`` means
            uncorrelated operands.

        Returns
        -------
        resulting_uncertainty : `NDUncertainty` instance
            Another instance of the same `NDUncertainty` subclass containing
            the uncertainty of the result.

        Raises
        ------
        ValueError
            If the ``operation`` is not supported or if correlation is not zero
            but the subclass does not support correlated uncertainties.

        Notes
        -----
        First this method checks if a correlation is given and the subclass
        implements propagation with correlated uncertainties.
        Then the second uncertainty is converted (or an Exception is raised)
        to the same class in order to do the propagation.
        Then the appropriate propagation method is invoked and the result is
        returned.
        i    u>   {0} does not support uncertainty propagation with correlation.u   addu   subtractu   multiplyu   true_divideu   divideu   unsupported operationR   (   u   true_divideu   divide(   R   R   R#   R$   t
   ValueErrort   formatR,   R   t   _convert_uncertaintyt   uncertaintyt   _propagate_addt   _propagate_subtractt   _propagate_multiplyt   _propagate_divideR   (   R   t	   operationt   other_nddatat   result_datat   correlationt   other_uncertt   result(    (    s;   lib/python2.7/site-packages/astropy/nddata/nduncertainty.pyt	   propagateÅ   s&    -		c         C` s?   t  | t ƒ r5 |  j | j k r% | S|  j | ƒ Sn t ‚ d S(   u  Checks if the uncertainties are compatible for propagation.

        Checks if the other uncertainty is `NDUncertainty`-like and if so
        verify that the uncertainty_type is equal. If the latter is not the
        case try returning ``self.__class__(other_uncert)``.

        Parameters
        ----------
        other_uncert : `NDUncertainty` subclass
            The other uncertainty.

        Returns
        -------
        other_uncert : `NDUncertainty` subclass
            but converted to a compatible `NDUncertainty` subclass if
            possible and necessary.

        Raises
        ------
        IncompatibleUncertaintiesException:
            If the other uncertainty cannot be converted to a compatible
            `NDUncertainty` subclass.
        N(   R   R   R   R,   R   (   R   R@   (    (    s;   lib/python2.7/site-packages/astropy/nddata/nduncertainty.pyR6     s
    c         C` s   d  S(   N(   R   (   R   R@   R>   R?   (    (    s;   lib/python2.7/site-packages/astropy/nddata/nduncertainty.pyR8   ,  s    c         C` s   d  S(   N(   R   (   R   R@   R>   R?   (    (    s;   lib/python2.7/site-packages/astropy/nddata/nduncertainty.pyR9   0  s    c         C` s   d  S(   N(   R   (   R   R@   R>   R?   (    (    s;   lib/python2.7/site-packages/astropy/nddata/nduncertainty.pyR:   4  s    c         C` s   d  S(   N(   R   (   R   R@   R>   R?   (    (    s;   lib/python2.7/site-packages/astropy/nddata/nduncertainty.pyR;   8  s    N(   R   R   R   R   t   TrueR   R   R   t   propertyR   R   t   setterR   R   R1   R3   RB   R6   R   R8   R9   R:   R;   (    (    (    s;   lib/python2.7/site-packages/astropy/nddata/nduncertainty.pyR   !   s"   %						G	 t   UnknownUncertaintyc           B` sY   e  Z d  Z e d „  ƒ Z e d „  ƒ Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 RS(   uî   This class implements any unknown uncertainty type.

    The main purpose of having an unknown uncertainty class is to prevent
    uncertainty propagation.

    Parameters
    ----------
    args, kwargs :
        see `NDUncertainty`
    c         C` s   t  S(   uL   `False` : Uncertainty propagation is *not* possible for this class.
        (   R   (   R   (    (    s;   lib/python2.7/site-packages/astropy/nddata/nduncertainty.pyR   I  s    c         C` s   d S(   uq   ``"unknown"`` : `UnknownUncertainty` implements any unknown                            uncertainty type.
        u   unknown(    (   R   (    (    s;   lib/python2.7/site-packages/astropy/nddata/nduncertainty.pyR   O  s    c         C` s   d } t  | ƒ ‚ d S(   uc   Raise an Exception because unknown uncertainty types cannot
        implement propagation.
        u3   Uncertainties of unknown type cannot be propagated.N(   R   (   R   R@   t   msg(    (    s;   lib/python2.7/site-packages/astropy/nddata/nduncertainty.pyR6   V  s    c         C` s   d S(   u4   Not possible for unknown uncertainty types.
        N(   R   (   R   R@   R>   R?   (    (    s;   lib/python2.7/site-packages/astropy/nddata/nduncertainty.pyR8   ]  s    c         C` s   d  S(   N(   R   (   R   R@   R>   R?   (    (    s;   lib/python2.7/site-packages/astropy/nddata/nduncertainty.pyR9   b  s    c         C` s   d  S(   N(   R   (   R   R@   R>   R?   (    (    s;   lib/python2.7/site-packages/astropy/nddata/nduncertainty.pyR:   e  s    c         C` s   d  S(   N(   R   (   R   R@   R>   R?   (    (    s;   lib/python2.7/site-packages/astropy/nddata/nduncertainty.pyR;   h  s    (   R   R   R   RD   R   R   R6   R8   R9   R:   R;   (    (    (    s;   lib/python2.7/site-packages/astropy/nddata/nduncertainty.pyRF   =  s   
				t   StdDevUncertaintyc           B` sY   e  Z d  Z e d „  ƒ Z e d „  ƒ Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 RS(   u  Standard deviation uncertainty assuming first order gaussian error
    propagation.

    This class implements uncertainty propagation for ``addition``,
    ``subtraction``, ``multiplication`` and ``division`` with other instances
    of `StdDevUncertainty`. The class can handle if the uncertainty has a
    unit that differs from (but is convertible to) the parents `NDData` unit.
    The unit of the resulting uncertainty will have the same unit as the
    resulting data. Also support for correlation is possible but requires the
    correlation as input. It cannot handle correlation determination itself.

    Parameters
    ----------
    args, kwargs :
        see `NDUncertainty`

    Examples
    --------
    `StdDevUncertainty` should always be associated with an `NDData`-like
    instance, either by creating it during initialization::

        >>> from astropy.nddata import NDData, StdDevUncertainty
        >>> ndd = NDData([1,2,3],
        ...              uncertainty=StdDevUncertainty([0.1, 0.1, 0.1]))
        >>> ndd.uncertainty  # doctest: +FLOAT_CMP
        StdDevUncertainty([0.1, 0.1, 0.1])

    or by setting it manually on the `NDData` instance::

        >>> ndd.uncertainty = StdDevUncertainty([0.2], unit='m', copy=True)
        >>> ndd.uncertainty  # doctest: +FLOAT_CMP
        StdDevUncertainty([0.2])

    the uncertainty ``array`` can also be set directly::

        >>> ndd.uncertainty.array = 2
        >>> ndd.uncertainty
        StdDevUncertainty(2)

    .. note::
        The unit will not be displayed.
    c         C` s   t  S(   uÍ   `True` : `StdDevUncertainty` allows to propagate correlated                     uncertainties.

        ``correlation`` must be given, this class does not implement computing
        it by itself.
        (   RC   (   R   (    (    s;   lib/python2.7/site-packages/astropy/nddata/nduncertainty.pyR   ˜  s    c         C` s   d S(   uG   ``"std"`` : `StdDevUncertainty` implements standard deviation.
        u   std(    (   R   (    (    s;   lib/python2.7/site-packages/astropy/nddata/nduncertainty.pyR   ¢  s    c         C` s   t  | t ƒ r | St ‚ d  S(   N(   R   RH   R   (   R   R@   (    (    s;   lib/python2.7/site-packages/astropy/nddata/nduncertainty.pyR6   ¨  s    c         C` s”  |  j  d  k r] | j d  k	 rM | j | j k rM | j  | j j | j ƒ j St | j  ƒ Sn3| j  d  k r¹ |  j d  k	 r© |  j |  j j k r© |  j j | j |  j  ƒ St |  j  ƒ Sn× |  j | j k rî |  j  |  j } | j  | j } n |  j  } | j  } t | t j	 ƒ s| d k rRd | | | } t j
 | d | d | ƒ } n t j
 | d | d ƒ } t | t ƒ rŒ| j | j ƒ S| Sd  S(   Ni    i   (   R   R   R   t   toR   R   R   R   R#   R$   t   sqrtR
   t   to_value(   R   R@   R>   R?   t   thist   othert   corrRA   (    (    s;   lib/python2.7/site-packages/astropy/nddata/nduncertainty.pyR8   ®  s,    $		"c         C` s”  |  j  d  k r] | j d  k	 rM | j | j k rM | j  | j j | j ƒ j St | j  ƒ Sn3| j  d  k r¹ |  j d  k	 r© |  j |  j j k r© |  j j | j |  j  ƒ St |  j  ƒ Sn× |  j | j k rî |  j  |  j } | j  | j } n |  j  } | j  } t | t j	 ƒ s| d k rRd | | | } t j
 | d | d | ƒ } n t j
 | d | d ƒ } t | t ƒ rŒ| j | j ƒ S| Sd  S(   Ni    i   (   R   R   R   RI   R   R   R   R   R#   R$   RJ   R
   RK   (   R   R@   R>   R?   RL   RM   RN   RA   (    (    s;   lib/python2.7/site-packages/astropy/nddata/nduncertainty.pyR9   ë  s,    $		"c   	      C` s  t  | t ƒ r | j } n  |  j d  k r„ | j | j j k rd | j | j j | j j ƒ j } n	 | j } t j	 |  j j
 | ƒ S| j d  k rí |  j |  j j k rÍ |  j |  j j |  j j ƒ j } n	 |  j } t j	 | j j
 | ƒ S|  j |  j j k r1|  j |  j j |  j j ƒ j | j j
 } n |  j | j j
 } | j | j j k rˆ| j | j j | j j ƒ j |  j j
 } n | j |  j j
 } t  | t j ƒ s¹| d k rèd | | | } t j | d | d | ƒ St j | d | d ƒ Sd  S(   Ni    i   (   R   R
   R   R   R   R   R   RI   R#   t   abst   dataR$   RJ   (	   R   R@   R>   R?   RM   RL   t   leftt   rightRN   (    (    s;   lib/python2.7/site-packages/astropy/nddata/nduncertainty.pyR:     s8    		c         C` s  t  | t ƒ r | j } n  |  j d  k r’ | j | j j k rn | j | j j | j j ƒ j | j j } n | j | j j } t	 j
 | | ƒ S| j d  k rû |  j |  j j k rÛ |  j |  j j |  j j ƒ j } n	 |  j } t	 j
 | | j j ƒ S|  j |  j j k r?|  j |  j j |  j j ƒ j | j j } n |  j | j j } | j | j j k rš| j | j j | j j ƒ j | j j | } n | | j | j j } t  | t	 j ƒ sÏ| d k rþd | | | } t	 j | d | d | ƒ St	 j | d | d ƒ Sd  S(   Ni    i   (   R   R
   R   R   R   R   R   RI   RP   R#   RO   R$   RJ   (   R   R@   R>   R?   RR   RQ   RN   (    (    s;   lib/python2.7/site-packages/astropy/nddata/nduncertainty.pyR;   M  s<    	
(   R   R   R   RD   R   R   R6   R8   R9   R:   R;   (    (    (    s;   lib/python2.7/site-packages/astropy/nddata/nduncertainty.pyRH   l  s   *
		=	$	>(   t
   __future__R    R   R   R   t   numpyR#   t   abcR   R   R   R   R   R&   t    R   t   unitsR	   R
   t   externR   t   __all__t	   ExceptionR   R   t   add_metaclasst   objectR   RF   RH   (    (    (    s;   lib/python2.7/site-packages/astropy/nddata/nduncertainty.pyt   <module>   s"   "ÿ /