ó
šßÈ[c           @` só   d  d l  m Z m Z m Z m Z d d l m Z d d l m Z d  d l	 Z	 d  d l
 Z
 d  d l Z d d l m Z d d l m Z d	 d
 g Z d „  Z d „  Z d „  Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   i    (   t   absolute_importt   divisiont   print_functiont   unicode_literalsi   (   t   six(   t   zipNi   (   t	   get_index(   t   AstropyUserWarningu   TableGroupsu   ColumnGroupsc         C` s'   |  j  d ƒ  t |  | ƒ SWd  QXd  S(   Nu   discard_on_copy(   t
   index_modet   _table_group_by(   t   tablet   keys(    (    s3   lib/python2.7/site-packages/astropy/table/groups.pyt   table_group_by   s    c         C` sÅ  d d l  m } t | t j ƒ r. | f } n  t | t t f ƒ rÑ xl | D]d } | |  j k rw t d j	 | ƒ ƒ ‚ n  |  j
 rJ t j |  | j ƒ rJ t d j	 | ƒ ƒ ‚ qJ qJ Wt | ƒ } |  | } t } n t | t j | f ƒ r7| } t | ƒ t |  ƒ k r.t d j	 t | ƒ t |  ƒ ƒ ƒ ‚ n  t } n t d j	 t | ƒ ƒ ƒ ‚ y[ t | | ƒ rst |  | ƒ n d } | d k	 r”| j ƒ  } n | j d d ƒ } t } Wn/ t k
 rÞ| j ƒ  } t j ƒ  d k } n X| | } t j t g | d | d  k t g f ƒ }	 t j |	 ƒ }
 | sfx9 t |
 d  |
 d ƒ D] \ } } | | | !j ƒ  qBWn  |  j |  | ƒ } | |
 d  } t | | ƒ r¦| | j d <n  t  | d |
 d | ƒ| _! | S(   u2  
    Get groups for ``table`` on specified ``keys``.

    Parameters
    ----------
    table : `Table`
        Table to group
    keys : str, list of str, `Table`, or Numpy array
        Grouping key specifier

    Returns
    -------
    grouped_table : Table object with groups attr set accordingly
    i   (   t   Tableu$   Table does not have key column {0!r}u2   Missing values in key column {0!r} are not allowedu;   Input keys array length {0} does not match table length {1}uB   Keys input must be string, list, tuple or numpy array, but got {0}t   kindu	   mergesortu   Darwinu   Windowsiÿÿÿÿu   grouped_by_table_colst   indicesR   N(   u   Darwinu   Windows("   R
   R   t
   isinstanceR   t   string_typest   listt   tuplet   colnamest
   ValueErrort   formatt   maskedt   npt   anyt   maskt   Truet   ndarrayt   lent   Falset	   TypeErrort   typeR   t   Nonet   sorted_datat   argsortt   platformt   systemt   concatenatet   flatnonzeroR   t   sortt	   __class__t   metat   TableGroupst   _groups(   R
   R   R   t   namet
   table_keyst   grouped_by_table_colst   table_indext   idx_sortt   stable_sortt   diffsR   t   i0t   i1t   outt   out_keys(    (    s3   lib/python2.7/site-packages/astropy/table/groups.pyR	      sR    
				$

,$c         C` s  d d l  m } t | | ƒ r. | j ƒ  } n  t | t j ƒ s^ t d j t | ƒ ƒ ƒ ‚ n  t	 | ƒ t	 |  ƒ k r t
 d j t	 | ƒ t	 |  ƒ ƒ ƒ ‚ n  d
 } t | | ƒ rÁ t | ƒ } n( t | d ƒ ré | j ré | j d } n  | d
 k	 r| j ƒ  } n | j ƒ  } | | } t j t g | d | d  k t g f ƒ } t j | ƒ } |  j |  | ƒ } t | d | d	 | | d  ƒ| _ | S(   u=  
    Get groups for ``column`` on specified ``keys``

    Parameters
    ----------
    column : Column object
        Column to group
    keys : Table or Numpy array of same length as col
        Grouping key specifier

    Returns
    -------
    grouped_column : Column object with groups attr set accordingly
    i   (   R   u+   Keys input must be numpy array, but got {0}u<   Input keys array length {0} does not match column length {1}u   indicesi    iÿÿÿÿR   R   N(   R
   R   R   t   as_arrayR   R   R   R   R    R   R   R!   R   t   hasattrR   R"   R#   R&   R   R'   R)   t   ColumnGroupsR,   (   t   columnR   R   t   indexR1   R3   R   R6   (    (    s3   lib/python2.7/site-packages/astropy/table/groups.pyt   column_group_byi   s.    		
,#t
   BaseGroupsc           B` sP   e  Z d  Z e d „  ƒ Z d „  Z d „  Z e Z d „  Z d „  Z	 d „  Z
 RS(   u4  
    A class to represent groups within a table of heterogeneous data.

      - ``keys``: key values corresponding to each group
      - ``indices``: index values in parent table or column corresponding to group boundaries
      - ``aggregate()``: method to create new table by aggregating within groups
    c         C` s   t  |  t ƒ r |  j S|  j S(   N(   R   R:   t   parent_columnt   parent_table(   t   self(    (    s3   lib/python2.7/site-packages/astropy/table/groups.pyt   parent¦   s    c         C` s   d |  _  |  S(   Ni    (   t   _iter_index(   RA   (    (    s3   lib/python2.7/site-packages/astropy/table/groups.pyt   __iter__ª   s    	c         C` sh   |  j  } | t |  j ƒ d k  r^ |  j | |  j | d } } |  j  d 7_  |  j | | !St ‚ d  S(   Ni   (   RC   R   R   RB   t   StopIteration(   RA   t   iiR4   R5   (    (    s3   lib/python2.7/site-packages/astropy/table/groups.pyt   next®   s    	c         C` sT  |  j  } t | t t j f ƒ rf |  j | |  j | d } } | | | !} | j j | | j _ nê |  j d  |  j d } } y | | | | } }	 Wn t	 k
 r¹ t
 d ƒ ‚ n Xt j t | ƒ d t j ƒ}
 x* t | |	 ƒ D] \ } } t |
 | | +qè W| |
 } | j j | | j _ t j d g t j |	 | ƒ g ƒ | j _ | S(   Ni   iÿÿÿÿuH   Index item for groups attribute must be a slice, numpy mask or int arrayt   dtypei    (   RB   R   t   intR   t   integerR   t   groupsR   t   _keyst	   ExceptionR   t   zerosR   t   boolR   R   R&   t   cumsumt   _indices(   RA   t   itemRB   R4   R5   R6   t   indices0t   indices1t   i0st   i1sR   (    (    s3   lib/python2.7/site-packages/astropy/table/groups.pyt   __getitem__¸   s"    	
+c         C` s   d j  |  j j |  j ƒ S(   Nu   <{0} indices={1}>(   R   R)   t   __name__R   (   RA   (    (    s3   lib/python2.7/site-packages/astropy/table/groups.pyt   __repr__Ð   s    c         C` s   t  |  j ƒ d S(   Ni   (   R   R   (   RA   (    (    s3   lib/python2.7/site-packages/astropy/table/groups.pyt   __len__Ó   s    (   RX   t
   __module__t   __doc__t   propertyRB   RD   RG   t   __next__RW   RY   RZ   (    (    (    s3   lib/python2.7/site-packages/astropy/table/groups.pyR>   ž   s   				R:   c           B` sG   e  Z d d d  „ Z e d „  ƒ Z e d „  ƒ Z d „  Z d „  Z RS(   c         C` s+   | |  _  | j |  _ | |  _ | |  _ d  S(   N(   R?   R@   RQ   RL   (   RA   R?   R   R   (    (    s3   lib/python2.7/site-packages/astropy/table/groups.pyt   __init__Ø   s    		c         C` sL   |  j  r |  j  j j S|  j d  k rA t j d t |  j ƒ g ƒ S|  j Sd  S(   Ni    (	   R@   RK   R   RQ   R!   R   t   arrayR   R?   (   RA   (    (    s3   lib/python2.7/site-packages/astropy/table/groups.pyR   Þ   s
    	c         C` s!   |  j  r |  j  j j S|  j Sd  S(   N(   R@   RK   R   RL   (   RA   (    (    s3   lib/python2.7/site-packages/astropy/table/groups.pyR   é   s    	c         C` sª  d d l  m } |  j d  |  j d } } |  j } t | | ƒ } t | d ƒ } | t j k } | t j k }	 y° | rá | sŒ | sŒ |	 rá |	 rº t j	 j
 | | ƒ t j |  j ƒ }
 q| rÌ t j	 } n  | j
 | | ƒ }
 n> t j g  t | | ƒ D] \ } } | | | | !ƒ ^ q÷ ƒ }
 Wn5 t k
 rWt d j | j j | j j ƒ ƒ ‚ n X| j d |
 d | j j d | j j d	 | j j d
 | j j d | j j ƒ } | S(   Ni   (   t   MaskedColumniÿÿÿÿu   reduceatu-   Cannot aggregate column '{0}' with type '{1}'t   dataR-   t   descriptiont   unitR   R*   (   R;   Ra   R   R?   R   R9   R   t   sumt   meant   addt   reduceatt   diffR`   R   RM   R   R   t   infoR-   RH   R)   Rc   Rd   R*   (   RA   t   funcRa   RU   RV   t   par_colR   Rh   t   sum_caset	   mean_caset   valsR4   R5   R6   (    (    s3   lib/python2.7/site-packages/astropy/table/groups.pyt	   aggregateñ   s4    	(B		c         C` sS   t  j t |  ƒ d t  j ƒ} x* t |  ƒ D] \ } } | | ƒ | | <q+ W|  | S(   uÇ  
        Filter groups in the Column based on evaluating function ``func`` on each
        group sub-table.

        The function which is passed to this method must accept one argument:

        - ``column`` : `Column` object

        It must then return either `True` or `False`.  As an example, the following
        will select all column groups with only positive values::

          def all_positive(column):
              if np.any(column < 0):
                  return False
              return True

        Parameters
        ----------
        func : function
            Filter function

        Returns
        -------
        out : Column
            New column with the aggregated rows.
        RH   (   R   t   emptyR   RO   t	   enumerate(   RA   Rk   R   t   it   group_column(    (    s3   lib/python2.7/site-packages/astropy/table/groups.pyt   filter  s    N(	   RX   R[   R!   R_   R]   R   R   Rp   Ru   (    (    (    s3   lib/python2.7/site-packages/astropy/table/groups.pyR:   ×   s
   	 R+   c           B` sV   e  Z d d d  „ Z e d „  ƒ Z e d „  ƒ Z d „  Z d „  Z e d „  ƒ Z	 RS(   c         C` s   | |  _  | |  _ | |  _ d  S(   N(   R@   RQ   RL   (   RA   R@   R   R   (    (    s3   lib/python2.7/site-packages/astropy/table/groups.pyR_   4  s    		c         C` s5   t  |  j d i  ƒ j d t ƒ } | r1 |  j j Sd S(   u^   
        Return the names of columns in the parent table that were used for grouping.
        u   metau   grouped_by_table_cols(    (   t   getattrR   t   getR   R   (   RA   R/   (    (    s3   lib/python2.7/site-packages/astropy/table/groups.pyt   key_colnames9  s    	!c         C` s6   |  j  d  k r+ t j d t |  j ƒ g ƒ S|  j  Sd  S(   Ni    (   RQ   R!   R   R`   R   R@   (   RA   (    (    s3   lib/python2.7/site-packages/astropy/table/groups.pyR   E  s    c   	      C` sÙ   |  j  d  |  j  d } } g  } |  j } x– t j | j ƒ D]‚ } | j j |  j k rj | j | ƒ } nH y | j	 j
 | ƒ } Wn/ t k
 r± } t j t j | ƒ t ƒ q= n X| j | ƒ q= W| j | d | j ƒS(   u„  
        Aggregate each group in the Table into a single row by applying the reduction
        function ``func`` to group values in each column.

        Parameters
        ----------
        func : function
            Function that reduces an array of values to a single value

        Returns
        -------
        out : Table
            New table with the aggregated rows.
        iÿÿÿÿi   R*   (   R   R@   R   t
   itervaluest   columnsRj   R-   Rx   t   takeRK   Rp   R   t   warningst   warnt	   text_typeR   t   appendR)   R*   (	   RA   Rk   RU   RV   t   out_colsR@   t   colt   new_colt   err(    (    s3   lib/python2.7/site-packages/astropy/table/groups.pyRp   L  s    	c         C` s_   t  j t |  ƒ d t  j ƒ} |  j } x- t |  ƒ D] \ } } | | | ƒ | | <q4 W|  | S(   uÎ  
        Filter groups in the Table based on evaluating function ``func`` on each
        group sub-table.

        The function which is passed to this method must accept two arguments:

        - ``table`` : `Table` object
        - ``key_colnames`` : tuple of column names in ``table`` used as keys for grouping

        It must then return either `True` or `False`.  As an example, the following
        will select all table groups with only positive values in the non-key columns::

          def all_positive(table, key_colnames):
              colnames = [name for name in table.colnames if name not in key_colnames]
              for colname in colnames:
                  if np.any(table[colname] < 0):
                      return False
              return True

        Parameters
        ----------
        func : function
            Filter function

        Returns
        -------
        out : Table
            New table with the aggregated rows.
        RH   (   R   Rq   R   RO   Rx   Rr   (   RA   Rk   R   Rx   Rs   t   group_table(    (    s3   lib/python2.7/site-packages/astropy/table/groups.pyRu   o  s
    	c         C` s   |  j  S(   N(   RL   (   RA   (    (    s3   lib/python2.7/site-packages/astropy/table/groups.pyR   ”  s    N(
   RX   R[   R!   R_   R]   Rx   R   Rp   Ru   R   (    (    (    s3   lib/python2.7/site-packages/astropy/table/groups.pyR+   3  s   	#	%(   t
   __future__R    R   R   R   t   externR   t   extern.six.movesR   R$   R|   t   numpyR   R<   R   t   utils.exceptionsR   t   __all__R   R	   R=   t   objectR>   R:   R+   (    (    (    s3   lib/python2.7/site-packages/astropy/table/groups.pyt   <module>   s   "		P	59\