a
    ߙfb                     @   st   d Z ddlZddlmZmZ ddlmZ ddlZdgZG dd de	Z
ddd	Zd
d Zdd Zdd Zdd ZdS )z
High-level operations for numpy structured arrays.

Some code and inspiration taken from numpy.lib.recfunctions.join_by().
Redistribution license restrictions apply.
    N)OrderedDictCounter)SequenceTableMergeErrorc                   @   s   e Zd ZdS )r   N)__name__
__module____qualname__ r	   r	   5lib/python3.9/site-packages/astropy/table/np_utils.pyr      s   {col_name}_{table_name}c                    s  t  fddg }|du r4dd tt D }t D ]\}}|| }|jjD ]p}|v rx|vr| n@t }	|		| t
fdd|	D r|j|d}|| | |< qTq<t|}
d	d |
 D }|rtd
|tfdd|D S )a  
    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)lenr	   )arraysr	   r
   <lambda>$       z"get_col_name_map.<locals>.<lambda>Nc                 S   s   g | ]}t |d  qS    )str).0Ziir	   r	   r
   
<listcomp>(   r   z$get_col_name_map.<locals>.<listcomp>c                 3   s   | ]} |j jv V  qd S r   )dtypenames)r   other)namer	   r
   	<genexpr>9   r   z#get_col_name_map.<locals>.<genexpr>)
table_nameZcol_namec                 S   s   g | ]\}}|d kr|qS r   r	   )r   r   countr	   r	   r
   r   A   r   zgMerging column names resulted in duplicates: {}.  Change uniq_col_name or table_names args to fix this.c                 3   s   | ]}| | fV  qd S r   r	   r   r   )col_name_mapr	   r
   r   H   r   )collectionsdefaultdictranger   	enumerater   r   appendlistpopanyformatr   itemsr   r   )r   Zcommon_namesZuniq_col_nameZtable_namesZcol_name_listidxarrayr   out_nameZothersZcol_name_countZrepeated_namesr	   )r   r   r   r
   get_col_name_map   s2    

r,   c                 C   s   g }|  D ]\}}dd t| |D }dd |D }zt|}W n< ty~ } z$td|d |j|W Y d}~n
d}~0 0 tdd |D }	t|	d	krtd
|	 }
|	t
|||
f q|S )z
    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.
    c                 S   s    g | ]\}}|d ur|| qS r   r	   )r   arrr   r	   r	   r
   r   Y   r   zget_descrs.<locals>.<listcomp>c                 S   s   g | ]}|d ur|qS r   r	   r   r	   r	   r
   r   \   r   z,The '{}' columns have incompatible types: {}r   Nc                 s   s   | ]}|j d d V  qdS )r   N)shaper   colr	   r	   r
   r   h   r   zget_descrs.<locals>.<genexpr>r   z Key columns have different shape)r(   zipcommon_dtyper   r'   _incompat_typessetr   r%   r#   fix_column_name)r   r   Z
out_descrsr+   Zin_namesZin_colsr   r   tmeZuniq_shapesr.   r	   r	   r
   
get_descrsM   s$    
r7   c                    s   t jt jt jt jt jf t fdd| D }t|dkr`dd | D }td| }||_	|dd | D }|D ]}|j
jdv rrd	|j |d
< qrt dd |D }|j
jS )z
    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D V  qdS )c                 3   s   | ]}t  jj|V  qd S r   )
issubclassr   type)r   Znp_typer0   r	   r
   r   z   r   z)common_dtype.<locals>.<genexpr>.<genexpr>N)tuple)r   Znp_typesr:   r
   r   z   s   zcommon_dtype.<locals>.<genexpr>r   c                 S   s   g | ]}|j jqS r	   )r   r   r/   r	   r	   r
   r   ~   r   z common_dtype.<locals>.<listcomp>z Columns have incompatible types c                 S   s   g | ]}t jd |jdqS )r   )r   )npemptyr   r/   r	   r	   r
   r      r   )SU0r   c                 S   s   g | ]}|d  qS )r   r	   )r   r-   r	   r	   r
   r      r   )r=   Zbool_Zobject_Znumber	characterZvoidr4   r   r   r3   r   kinditemsizer*   r   )ZcolsZ
uniq_typesZincompat_typesr6   Zarrsr-   Z
arr_commonr	   r<   r
   r2   r   s    r2   c                 C   sX   d}t | tst|| D ]$}t |tjr6|jjd u rt|qt| dkrTtdd S )Nz@`arrays` arg must be a sequence (e.g. list) of structured arraysr   z,`arrays` arg must include at least one array)	
isinstancer   	TypeErrorr=   Zndarrayr   r   r   
ValueError)r   errr*   r	   r	   r
   (_check_for_sequence_of_structured_arrays   s    

rI   c                 C   s.   | dur*zt | } W n ty(    Y n0 | S )z
    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.
    N)r   UnicodeEncodeError)valr	   r	   r
   r5      s    r5   )r   N)__doc__r   r   r   collections.abcr   Znumpyr=   __all__rG   r   r,   r7   r2   rI   r5   r	   r	   r	   r
   <module>   s     
8%