σ
ίΘ[c           @` s
  d  Z  d d l m Z m Z m Z m Z d d l m Z d d l m	 Z	 m
 Z
 d d l m Z d d l Z d d l m Z m Z d d l Z d d l j Z d	 d
 l m Z d g Z d e f d     YZ d d d  Z d   Z d   Z d   Z d   Z d   Z d S(   u§   
High-level operations for numpy structured arrays.

Some code and inspiration taken from numpy.lib.recfunctions.join_by().
Redistribution license restrictions apply.
i    (   t   absolute_importt   divisiont   print_functiont   unicode_literalsi   (   t   six(   t   zipt   range(   t   chainN(   t   OrderedDictt   Counteri   (   t	   _np_utilsu   TableMergeErrort   TableMergeErrorc           B` s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s5   lib/python2.7/site-packages/astropy/table/np_utils.pyR      s   u   {col_name}_{table_name}c         ` s·  t  j   f d     g  } | d k r_ g  t t     D] } t j | d  ^ q= } n  xΧ t    D]Ι \ } } | | } x° | j j	 D]’   }	  | k rΖ  | k r#| j
   q#n] t    }
 |
 j |  t  f d   |
 D  r| j d | d   }	 n  | j
 |	    |	 | <q Wql Wt |  } g  t j |  D] \  } | d k rU ^ qU} | rt d j |    n  t  f d   | D    S(	   u  
    Find the column names mapping when merging the list of structured ndarrays
    ``arrays``.  It is assumed that col names in ``common_names`` are to be
    merged into a single column while the rest will be uniquely represented
    in the output.  The args ``uniq_col_name`` and ``table_names`` specify
    how to rename columns in case of conflicts.

    Returns a dict mapping each output column name to the input(s).  This takes the form
    {outname : (col_name_0, col_name_1, ...), ... }.  For key columns all of input names
    will be present, while for the other non-key columns the value will be (col_name_0,
    None, ..) or (None, col_name_1, ..) etc.
    c           ` s   d  g t    S(   N(   t   Nonet   len(    (   t   arrays(    s5   lib/python2.7/site-packages/astropy/table/np_utils.pyt   <lambda>+   s    i   c         3` s!   |  ] }   | j  j k Vq d  S(   N(   t   dtypet   names(   t   .0t   other(   t   name(    s5   lib/python2.7/site-packages/astropy/table/np_utils.pys	   <genexpr>@   s    t
   table_namet   col_nameuh   Merging column names resulted in duplicates: {0}.  Change uniq_col_name or table_names args to fix this.c         3` s   |  ] } |   | f Vq d  S(   N(    (   R   R   (   t   col_name_map(    s5   lib/python2.7/site-packages/astropy/table/np_utils.pys	   <genexpr>O   s    N(   t   collectionst   defaultdictR   R   R   R   t	   text_typet	   enumerateR   R   t   appendt   listt   popt   anyt   formatR	   t	   iteritemsR   R   (   R   t   common_namest   uniq_col_namet   table_namest   col_name_listt   iit   idxt   arrayR   t   out_namet   otherst   col_name_countt   countt   repeated_names(    (   R   R   R   s5   lib/python2.7/site-packages/astropy/table/np_utils.pyt   get_col_name_map   s0    5
4	c         C` s4  g  } x't  j |  D]\ } } g  t |  |  D]" \ } } | d k	 r2 | | ^ q2 } g  | D] } | d k	 ra | ^ qa } y t |  }	 Wn2 t k
 rΓ }
 t d j | d |
 j    n Xt d   | D  } t	 |  d k rt d j |    n  | j
   } | j t |  |	 | f  q W| S(   u΄   
    Find the dtypes descrs resulting from merging the list of arrays' dtypes,
    using the column name mapping ``col_name_map``.

    Return a list of descrs for the output.
    u.   The '{0}' columns have incompatible types: {1}i    c         s` s   |  ] } | j  d  Vq d S(   i   N(   t   shape(   R   t   col(    (    s5   lib/python2.7/site-packages/astropy/table/np_utils.pys	   <genexpr>o   s    i   u&   Key columns {0!r} have different shapeN(   R   R#   R   R   t   common_dtypeR   R"   t   _incompat_typest   setR   R    R   t   fix_column_name(   R   R   t
   out_descrsR+   t   in_namest   arrR   t   in_colsR   R   t   tmet   uniq_shapesR1   (    (    s5   lib/python2.7/site-packages/astropy/table/np_utils.pyt
   get_descrsT   s    8%	 c         ` s*  t  j t  j t  j t  j t  j f   t   f d   |  D  } t |  d k r g  |  D] } | j j	 ^ qY } t
 d j |   } | | _ |  n  g  |  D] } t  j d d | j ^ q } x4 | D], } | j j d	 k rΚ d | j | d <qΚ qΚ Wt  j g  | D] } | d ^ q } | j j S(
   uά   
    Use numpy to find the common dtype for a list of structured ndarray columns.

    Only allow columns within the following fundamental numpy data types:
    np.bool_, np.object_, np.number, np.character, np.void
    c         3` s+   |  ]!   t    f d     D  Vq d S(   c         3` s$   |  ] } t    j j |  Vq d  S(   N(   t
   issubclassR   t   type(   R   t   np_type(   R2   (    s5   lib/python2.7/site-packages/astropy/table/np_utils.pys	   <genexpr>   s    N(   t   tuple(   R   (   t   np_types(   R2   s5   lib/python2.7/site-packages/astropy/table/np_utils.pys	   <genexpr>   s   i   u#   Columns have incompatible types {0}R   u   Su   Uu   0i    (   u   Su   U(   t   npt   bool_t   object_t   numbert	   charactert   voidR5   R   R   R   R   R"   R4   t   emptyt   kindt   itemsizeR*   t   str(   t   colst
   uniq_typesR2   t   incompat_typesR;   t   arrsR9   t
   arr_common(    (   RB   s5   lib/python2.7/site-packages/astropy/table/np_utils.pyR3   y   s    $			+&c         C` s   d } t  |  t j  s' t |   n  xB |  D]: } t  | t j  sY | j j d  k r. t |   q. q. Wt	 |   d k r t
 d   n  d  S(   Nu@   `arrays` arg must be a sequence (e.g. list) of structured arraysi    u,   `arrays` arg must include at least one array(   t
   isinstanceR   t   Sequencet	   TypeErrorRC   t   ndarrayR   R   R   R   t
   ValueError(   R   t   errR*   (    (    s5   lib/python2.7/site-packages/astropy/table/np_utils.pyt(   _check_for_sequence_of_structured_arrays   s    %c         C` sR   |  d k	 rN y t |   }  WqN t k
 rJ t j rD t d   n    qN Xn  |  S(   uή   
    Fixes column names so that they are compatible with Numpy on
    Python 2.  Raises a ValueError exception if the column name
    contains Unicode characters, which can not reasonably be used as a
    column name.
    u<   Column names must not contain Unicode characters on Python 2N(   R   RL   t   UnicodeEncodeErrorR   t   PY2RV   (   t   val(    (    s5   lib/python2.7/site-packages/astropy/table/np_utils.pyR6   £   s    	
c         C` s¬   t  |  d  } t j |  d t } g  t |  D]% } t j | d | f j    ^ q2 } g  } x! | D] } | j | j j  qj Wd j	 |  } t j
 j | d | S(   uT  
    Partial replacement for `~numpy.core.records.fromrecords` which includes
    a workaround for the bug with unicode arrays described at:
    https://github.com/astropy/astropy/issues/3052

    This should not serve as a full replacement for the original function;
    this only does enough to fulfill the needs of the table module.
    i    R   .u   ,t   formats(   R   RC   R*   t   objectR   t   tolistR   R   RL   t   joint   rect
   fromarrays(   t   rec_listt   nfieldst   objt   it
   array_listR\   (    (    s5   lib/python2.7/site-packages/astropy/table/np_utils.pyt   recarray_fromrecords·   s    8(    t   __doc__t
   __future__R    R   R   R   t   externR   t   extern.six.movesR   R   t	   itertoolsR   R   R   R	   t   numpyRC   t   numpy.mat   mat    R
   t   __all__RV   R   R   R0   R=   R3   RX   R6   Rg   (    (    (    s5   lib/python2.7/site-packages/astropy/table/np_utils.pyt   <module>   s$   "	7	%			