
[c           @` s  d  Z  d d l m Z m Z m Z d d l Z d d l Z d d l Z d d l m	 Z	 d d l
 Z d d l m Z d d l Z d d l Z d d l m Z d d l m Z d d	 l m Z d d
 l m Z m Z d d l m Z d d d d d d g Z e d e d d  f Z i d e  d f 6d e  d f 6d e! d f 6d e! d f 6Z" e d    Z# d   Z$ d   Z% d   Z& d d  Z( d e) f d      YZ* d e* f d!     YZ+ d e+ f d"     YZ, d e, f d#     YZ- d S($   s  This module contains functions and methods that relate to the DataInfo class
which provides a container for informational attributes as well as summary info
methods.

A DataInfo object is attached to the Quantity, SkyCoord, and Time classes in
astropy.  Here it allows those classes to be used in Tables and uniformly carry
table column attributes such as name, format, dtype, meta, and description.
i    (   t   absolute_importt   divisiont   print_functionN(   t   deepcopy(   t   partial(   t   OrderedDict(   t   contextmanageri   (   t   six(   t   zipt	   cStringIOi   (   t   metadatat   data_info_factoryt   dtype_info_namet   BaseColumnInfot   DataInfot	   MixinInfot   ParentDtypeInfot   categoryt   messages3   All-NaN|Mean of empty slice|Degrees of freedom <= 0t   strt   St   unicodet   Ut   bytesc         c` s$   t  j } |  t  _ d V| t  _ d S(   sk  Set context for serialization.

    This will allow downstream code to understand the context in which a column
    is being serialized.  Objects like Time or SkyCoord will have different
    default serialization representations depending on context.

    Parameters
    ----------
    context : str
        Context name, e.g. 'fits', 'hdf5', 'ecsv', 'yaml'
    N(   R   t   _serialize_context(   t   contextt   old_context(    (    s6   lib/python2.7/site-packages/astropy/utils/data_info.pyt   serialize_context_as0   s    		c         C` sm   t  j |   }  |  j d k r` t j d |  j  j d  } t t j	 |  j f } | | } n	 |  j
 } | S(   s  Return a human-oriented string name of the ``dtype`` arg.
    This can be use by astropy methods that present type information about
    a data object.

    The output is mostly equivalent to ``dtype.name`` which takes the form
    <type_name>[B] where <type_name> is like ``int`` or ``bool`` and [B] is an
    optional number of bits which gets included only for numeric types.

    For bytes, string and unicode types, the output is shown below, where <N>
    is the number of characters.  This representation corresponds to the Python
    type that matches the dtype::

      Numpy          S<N>      U<N>
      Python 2      str<N>  unicode<N>
      Python 3    bytes<N>   str<N>

    Parameters
    ----------
    dtype : str, np.dtype, type
        Input dtype as an object that can be converted via np.dtype()

    Returns
    -------
    dtype_info_name : str
        String name of ``dtype``
    R   R   s   (\d+)i   (   R   R   (   t   npt   dtypet   kindt   ret   searchR   t   groupt   STRING_TYPE_NAMESR   t   PY2t   name(   R   t   lengtht	   type_namet   out(    (    s6   lib/python2.7/site-packages/astropy/utils/data_info.pyR   C   s    	c         ` s      f d   } | S(   s8  
    Factory to create a function that can be used as an ``option``
    for outputting data object summary information.

    Examples
    --------
    >>> from astropy.utils.data_info import data_info_factory
    >>> from astropy.table import Column
    >>> c = Column([4., 3., 2., 1.])
    >>> mystats = data_info_factory(names=['min', 'median', 'max'],
    ...                             funcs=[np.min, np.median, np.max])
    >>> c.info(option=mystats)
    min = 1.0
    median = 2.5
    max = 4.0
    n_bad = 0
    length = 4

    Parameters
    ----------
    names : list
        List of information attribute names
    funcs : list
        List of functions that compute the corresponding information attribute

    Returns
    -------
    func : function
        Function that can be used as a data info option
    c         ` s   g  } x t      D]w \ } } y7 t | t j  rL t |  |    } n | |   } Wn t k
 ry | j d  q X| j t |   q Wt t   |   S(   Ns   --(	   R   t
   isinstanceR   t   string_typest   getattrt	   Exceptiont   appendR   R   (   t   datt   outsR$   t   funcR'   (   t   funcst   names(    s6   lib/python2.7/site-packages/astropy/utils/data_info.pyR/      s    (    (   R1   R0   R/   (    (   R0   R1   s6   lib/python2.7/site-packages/astropy/utils/data_info.pyR   i   s    c         C` s   i  } xr | D]j } t  |  | d  } | d k	 r t j rj t |  } t | t j  rj t |  } qj n  | | | <q q W| S(   s[  
    Get the values for object ``attrs`` and return as a dict.  This
    ignores any attributes that are None and in Py2 converts any unicode
    attribute names or values to str.  In the context of serializing the
    supported core astropy classes this conversion will succeed and results
    in more succinct and less python-specific YAML.
    N(   R*   t   NoneR   R#   R   R(   t	   text_type(   t   objt   attrsR'   t   attrt   val(    (    s6   lib/python2.7/site-packages/astropy/utils/data_info.pyt   _get_obj_attrs_map   s    	c         C` s   | d k r t  |   j } na | d k r? t |  j j  } n@ | d k rm |  j d } | rd | n d } n t |  j |  } | d k r d } n  t |  S(   sP   
    Get a data object attribute for the ``attributes`` info summary method
    t   classR   t   shapei   t    N(	   t   typet   __name__R   t   infoR   R:   R*   R2   R   (   R-   R6   R7   t   datshape(    (    s6   lib/python2.7/site-packages/astropy/utils/data_info.pyt   _get_data_attribute   s    	c           B` sC  e  Z d  Z d d d d g Z e   Z e d d d d d	 d
 g  Z e   Z d Z d Z	 d  Z e d  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z e e d e d g  e D] Z e e d e ^ q   Z e e d e d g  e D] Z e e d e  ^ q  Z d d d  Z  d   Z! RS(!   s  
    Descriptor that data classes use to add an ``info`` attribute for storing
    data attributes in a uniform and portable way.  Note that it *must* be
    called ``info`` so that the DataInfo() object can be stored in the
    ``instance`` using the ``info`` key.  Because owner_cls.x is a descriptor,
    Python doesn't use __dict__['x'] normally, and the descriptor can safely
    store stuff there.  Thanks to http://nbviewer.ipython.org/urls/
    gist.github.com/ChrisBeaumont/5758381/raw/descriptor_writeup.ipynb for
    this trick that works for non-hashable classes.

    Parameters
    ----------
    bound : bool
        If True this is a descriptor attribute in a class definition, else it
        is a DataInfo() object that is bound to a data object instance. Default is False.
    t   meant   stdt   mint   maxR$   t   unitR   t   formatt   descriptiont   metaR:   R9   c         C` s)   | r% t  d   |  j D  |  _ n  d  S(   Nc         s` s   |  ] } | d  f Vq d  S(   N(   R2   (   t   .0R6   (    (    s6   lib/python2.7/site-packages/astropy/utils/data_info.pys	   <genexpr>   s    (   t   dictt
   attr_namest   _attrs(   t   selft   bound(    (    s6   lib/python2.7/site-packages/astropy/utils/data_info.pyt   __init__   s    c         C` si   | d  k r |  } | | _ nG | j j d  } | d  k r\ |  j d t  } | j d <n  | | _ | S(   NR>   RN   (   R2   t   _parent_clst   __dict__t   gett	   __class__t   Truet   _parent(   RM   t   instancet	   owner_clsR>   (    (    s6   lib/python2.7/site-packages/astropy/utils/data_info.pyt   __get__   s     	c         C` s   | d  k r t d   n  t | t  r |  j d t  } | j d <xJ | j | j | j	 D]" } t
 t | |   | j | <q_ Wn t d   d  S(   Ns   cannot set unbound descriptorRN   R>   s)   info must be set with a DataInfo instance(   R2   t
   ValueErrorR(   R   RS   RT   RQ   RK   t   attrs_from_parentt   _attrs_no_copyR   R*   RL   t	   TypeError(   RM   RV   t   valueR>   R6   (    (    s6   lib/python2.7/site-packages/astropy/utils/data_info.pyt   __set__   s    #c         C` s   |  j  S(   N(   RL   (   RM   (    (    s6   lib/python2.7/site-packages/astropy/utils/data_info.pyt   __getstate__   s    c         C` s   | |  _  d  S(   N(   RL   (   RM   t   state(    (    s6   lib/python2.7/site-packages/astropy/utils/data_info.pyt   __setstate__   s    c         C` s   | j  d  r% t t |   j |  S| |  j k rD t |  j |  Sy |  j | } Wn' t k
 r~ t t |   j |  n X| d k r t	 |  r |   } n  | d k r | d  k r t j d  } n  | S(   Nt   _t   parent_tableR   t   O(   t
   startswitht   superR   t   __getattribute__RZ   R*   RU   RL   t   KeyErrort   callableR2   R   R   (   RM   R6   R]   (    (    s6   lib/python2.7/site-packages/astropy/utils/data_info.pyt   __getattr__   s    c         C` s#  t  |  j | d   } | |  j k rK t | t  rK t |  j | |  d  St | t  r | j d  k rx t	 d   n  | j |  |  d  S| j
 d  r t t |   j | |  d  S| |  j k r t	 d j |  j    n  | d k r| d  k r d  n t j |  } n  | |  j | <d  S(   Ns   can't set attributeRb   s   attribute must be one of {0}Rc   (   R*   RS   R2   RZ   R(   t   propertyt   setattrRU   t   fsett   AttributeErrorRe   Rf   R   t   __setattr__RK   RF   t   weakreft   refRL   (   RM   R6   R]   t   propobj(    (    s6   lib/python2.7/site-packages/astropy/utils/data_info.pyRo     s"    $c         C` s   t  |  j |  j  S(   s=   Get the values for the parent ``attrs`` and return as a dict.(   R8   RU   t   _represent_as_dict_attrs(   RM   (    (    s6   lib/python2.7/site-packages/astropy/utils/data_info.pyt   _represent_as_dict3  s    c         C` s   |  j  |   S(   N(   RP   (   RM   t   map(    (    s6   lib/python2.7/site-packages/astropy/utils/data_info.pyt   _construct_from_dict7  s    R1   R0   R6   t   nant
   attributesR;   c      	   C` s  | d k r t  j } n  |  j } t   } | j j } | d k	 rO | | d <n  t | t t	 f  rj | n | g } x | D] } t | t
 j  r t |  d |  r t |  d |  } q t d j |    n  t j   9 x t D] } t j d |  q W| j | |   Wd QXqz Wt | d  rBt j | j  } nC y) t j t j |  t j |  B } Wn t k
 rd } n X| | d	 <y t |  | d
 <Wn t k
 rn X| d k r| SxI | j   D]; \ }	 }
 |
 d k r| j d j |	 |
  t j   qqWd S(   s  
        Write summary information about data object to the ``out`` filehandle.
        By default this prints to standard output via sys.stdout.

        The ``option`` argument specifies what type of information
        to include.  This can be a string, a function, or a list of
        strings or functions.  Built-in options are:

        - ``attributes``: data object attributes like ``dtype`` and ``format``
        - ``stats``: basic statistics: min, mean, and max

        If a function is specified then that function will be called with the
        data object as its single argument.  The function must return an
        OrderedDict containing the information attributes.

        If a list is provided then the information attributes will be
        appended for each of the options, in order.

        Examples
        --------

        >>> from astropy.table import Column
        >>> c = Column([1, 2], unit='m', dtype='int32')
        >>> c.info()
        dtype = int32
        unit = m
        class = Column
        n_bad = 0
        length = 2

        >>> c.info(['attributes', 'stats'])
        dtype = int32
        unit = m
        class = Column
        mean = 1.5
        std = 0.5
        min = 1
        max = 2
        n_bad = 0
        length = 2

        Parameters
        ----------
        option : str, function, list of (str or function)
            Info option, defaults to 'attributes'.
        out : file-like object, None
            Output destination, defaults to sys.stdout.  If None then the
            OrderedDict with information attributes is returned

        Returns
        -------
        info : OrderedDict if out==None else None
        R;   R$   t   info_summary_s-   option={0} is not an allowed information typet   ignoreNt   maski    t   n_badR%   s	   {0} = {1}(!   t   syst   stdoutRU   R   R>   R$   R2   R(   t   listt   tupleR   R)   t   hasattrR*   RY   RF   t   warningst   catch_warningst   IGNORE_WARNINGSt   filterwarningst   updateR   t   count_nonzeroR{   t   isinft   isnanR+   t   lenR\   t   itemst   writet   ost   linesep(   RM   t   optionR'   R-   R>   R$   t   optionst   ignore_kwargsR|   t   keyR7   (    (    s6   lib/python2.7/site-packages/astropy/utils/data_info.pyt   __call__E  sD    6		$	)

c         C` sE   |  j  d  k r" t t |   j   St   } |  j d |  | j   S(   NR'   (   RU   R2   Rf   R   t   __repr__t   StringIOR   t   getvalue(   RM   R'   (    (    s6   lib/python2.7/site-packages/astropy/utils/data_info.pyR     s
    	(   R   R:   RE   RF   RG   R9   (    N("   R=   t
   __module__t   __doc__t   _statst   setRZ   RK   R[   t   _info_summary_attrsRs   R2   RU   t   FalseRO   RX   R^   R_   Ra   Rj   Ro   Rt   Rv   t   staticmethodR   R6   R   R@   t   info_summary_attributest   statR*   R   t   info_summary_statsR   R   (    (    (    s6   lib/python2.7/site-packages/astropy/utils/data_info.pyR      s6   										()bc           B` sq   e  Z d  Z e j j d d g  Z e d g  Z d Z	 e
 d  Z d   Z d   Z d   Z e d    Z RS(	   sg  
    Base info class for anything that can be a column in an astropy
    Table.  There are at least two classes that inherit from this:

      ColumnInfo: for native astropy Column / MaskedColumn objects
      MixinInfo: for mixin column objects

    Note that this class is defined here so that mixins can use it
    without importing the table package.
    Rc   t   indicesc         C` s/   t  t |   j d |  | r+ i  |  _ n  d  S(   NRN   (   Rf   R   RO   t   _format_funcs(   RM   RN   (    (    s6   lib/python2.7/site-packages/astropy/utils/data_info.pyRO     s    c         c` sl   |  j  } |  j d k r+ d d l m } n |  j j } | j } x% | | d t t i   D] } | VqY Wd S(   sG   
        This is a mixin-safe version of Column.iter_str_vals.
        i   (   t	   FORMATTERiN(   RU   Rc   R2   t   table.columnR   t	   formattert   _pformat_col_iterR   (   RM   t   colR   R   t   str_val(    (    s6   lib/python2.7/site-packages/astropy/utils/data_info.pyt   iter_str_vals  s    		c   	      C` s  |  j  s d St | t  r@ | j  |  } t t |    } nC t | t j  rz | j j d k rz t j	 |  d } n	 | g } t j
 |  } | j d k r t |  t |  } n  xG t | |  D]6 \ } } x' |  j  D] } | j | |  j |  q Wq Wd S(   s  
        Adjust info indices after column modification.

        Parameters
        ----------
        index : slice, int, list, or ndarray
            Element(s) of column to modify. This parameter can
            be a single row number, a list of row numbers, an
            ndarray of row numbers, a boolean ndarray (a mask),
            or a column slice.
        value : int, list, or ndarray
            New value(s) to insert
        col_len : int
            Length of the column
        Nt   bi    i   (   R   R(   t   sliceR   t   rangeR   t   ndarrayR   R   t   wheret
   atleast_1dt   sizeR   R   t   replaceR$   (	   RM   t   indexR]   t   col_lent   tt   keysR   R7   t	   col_index(    (    s6   lib/python2.7/site-packages/astropy/utils/data_info.pyt   adjust_indices  s    	$	c   
      C` sF  d d l  m } t |  d t  s2 g  | j _ | St | t  rj g  |  j D] } | | ^ qK | j _ n |  j rBt | t j	  r | j
 j d k r t j |  d } n  d } t |  d | k } g  | j _ xj |  j D]\ } | s t | |  r| j | |  }	 n t |  }	 |	 j |  | j j j |	  q Wn  | S(   sX  
        Given a sliced object, modify its indices
        to correctly represent the slice.

        Parameters
        ----------
        col_slice : Column or mixin
            Sliced object
        item : slice, list, or ndarray
            Slice used to create col_slice
        col_len : int
            Length of original object
        i   (   t   SortedArrayt   _copy_indicesR   i    g333333?(   t   table.sorted_arrayR   R*   RT   R>   R   R(   R   R   R   R   R   R   R   t	   get_sliceR   t   replace_rowsR,   (
   RM   t	   col_slicet   itemR   R   t   xt	   thresholdt   smallR   t	   new_index(    (    s6   lib/python2.7/site-packages/astropy/utils/data_info.pyt   slice_indices  s&    )	$c   
      ` s   d d l  m }  f d   }   f d   } | |  d  } x6 |  d D]* } t j | | |  d | d | } qI Wt j |   | d	 <t d
   |  D  }	 t |	  d k r | d   n  |	 j   | d <| S(   s  
        Utility method to merge and validate the attributes ``attrs`` for the
        input table columns ``cols``.

        Note that ``dtype`` and ``shape`` attributes are handled specially.
        These should not be passed in ``attrs`` but will always be in the
        returned dict of merged attributes.

        Parameters
        ----------
        cols : list
            List of input Table column objects
        metadata_conflicts : str ('warn'|'error'|'silent')
            How to handle metadata conflicts
        name : str
            Output column name
        attrs : list
            List of attribute names to be merged

        Returns
        -------
        attrs : dict of merged attributes

        i   (   t   TableMergeErrorc         ` s   d j    |  | | |  } | S(   Ns_   In merged column '{}' the '{}' attribute does not match ({} != {}).  Using {} for merged output(   RF   (   R   t   leftt   rightR'   (   R$   (    s6   lib/python2.7/site-packages/astropy/utils/data_info.pyt   warn_str_funcH  s    c         ` s     f d    D S(   Nc         ` s@   i  |  ]6 } t    j | d   d  k	 r t    j |  |  q S(   N(   R*   R>   R2   (   RI   R6   (   R   (    s6   lib/python2.7/site-packages/astropy/utils/data_info.pys
   <dictcomp>O  s   	 (    (   R   (   R5   (   R   s6   lib/python2.7/site-packages/astropy/utils/data_info.pyt   getattrsN  s    i    i   t   metadata_conflictsR   R   c         s` s   |  ] } | j  d  Vq d S(   i   N(   R:   (   RI   R   (    (    s6   lib/python2.7/site-packages/astropy/utils/data_info.pys	   <genexpr>[  s    s   columns have different shapesR:   (   t   table.np_utilsR   R
   t   merget   common_dtypeR   R   t   pop(
   t   colsR   R$   R5   R   R   R   R'   R   t   uniq_shapes(    (   R5   R$   s6   lib/python2.7/site-packages/astropy/utils/data_info.pyt   merge_cols_attributes,  s    N(   R=   R   R   R   RK   t   unionR   R[   R2   R   R   RO   R   R   R   R   R   (    (    (    s6   lib/python2.7/site-packages/astropy/utils/data_info.pyR     s   
		&	*c           B` s   e  Z d    Z RS(   c         C` sp   | d k rS |  j  d  k	 rS d d l m } | |  } |  j  j j |  j |  n  t t |   j	 | |  d  S(   NR$   i   (   t   fix_column_name(
   Rc   R2   R   R   t   columnst   _rename_columnR$   Rf   R   Ro   (   RM   R6   R]   R   t   new_name(    (    s6   lib/python2.7/site-packages/astropy/utils/data_info.pyRo   e  s
    (   R=   R   Ro   (    (    (    s6   lib/python2.7/site-packages/astropy/utils/data_info.pyR   c  s   c           B` s   e  Z d  Z e d g  Z RS(   s&   Mixin that gets info.dtype from parentR   (   R=   R   R   R   RZ   (    (    (    s6   lib/python2.7/site-packages/astropy/utils/data_info.pyR   q  s   (.   R   t
   __future__R    R   R   R   R}   Rp   t   copyR   t   numpyR   t	   functoolsR   R   R   t   collectionsR   t
   contextlibR   t   externR   t   extern.six.movesR   R	   R   R;   R
   t   __all__RJ   t   RuntimeWarningR   R   RT   R"   R   R   R   R8   R2   R@   t   objectR   R   R   R   (    (    (    s6   lib/python2.7/site-packages/astropy/utils/data_info.pyt   <module>   s<   		&	0	