U
    e»{j¬  ã                   @   sl   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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)ÚCounterÚOrderedDict)ÚSequenceÚTableMergeErrorc                   @   s   e Zd ZdS )r   N)Ú__name__Ú
__module__Ú__qualname__© r	   r	   úU/var/www/html/bwave/python/venv/lib/python3.8/site-packages/astropy/table/np_utils.pyr      s   ú{col_name}_{table_name}c                    s  t  ‡ fdd„¡‰g }|dkr4dd„ ttˆ ƒƒD ƒ}tˆ ƒD ]Š\}}|| }|jjD ]p‰ˆ}ˆ|krxˆ|kr¸| ˆ¡ n@tˆ ƒ}	|	 	|¡ t
‡fdd„|	D ƒƒr®|j|ˆd�}| |¡ ˆˆ| |< qTq<t|ƒ}
d	d„ |
 ¡ D ƒ}|rôt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)Ú.0Úiir	   r	   r
   Ú
<listcomp>)   s     z$get_col_name_map.<locals>.<listcomp>c                 3   s   | ]}ˆ |j jkV  qd S r   )ÚdtypeÚnames)r   Úother)Únamer	   r
   Ú	<genexpr>:   s     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   D   s      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   N   s     )Ú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   s<    
 ÿ
þÿr-   c                 C   sÎ   g }|  ¡ D ]¼\}}dd„ t| |ƒD ƒ}dd„ |D ƒ}zt|ƒ}W n: tk
r| } ztd |d |j¡ƒ|‚W 5 d}~X Y nX dd„ |D ƒ}	t|	ƒd	kr td
ƒ‚|	 ¡ }
|dk	r¸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 k	r|| ‘qS r   r	   )r   Úarrr   r	   r	   r
   r   _   s      zget_descrs.<locals>.<listcomp>c                 S   s   g | ]}|d k	r|‘qS r   r	   r   r	   r	   r
   r   b   s      z,The '{}' columns have incompatible types: {}r   Nc                 S   s   h | ]}|j d d… ’qS )r   N)Úshape©r   Úcolr	   r	   r
   Ú	<setcomp>q   s     zget_descrs.<locals>.<setcomp>r   z Key columns have different shape)
r)   ÚzipÚcommon_dtyper   r(   Ú_incompat_typesr   r&   r   r$   )r   r   Z
out_descrsr,   Zin_namesZin_colsr   r   ÚtmeZuniq_shapesr/   r	   r	   r
   Ú
get_descrsS   s.     ÿÿür7   c                    sª   t jt jt jt jt jf‰ ‡ fdd„| D ƒ}t|ƒdkr\dd„ | D ƒ}td|› �ƒ}||_|‚dd„ | D ƒ}|D ]}|j	j
dkrnd	|j |d
< qnt  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                    s"   h | ]‰ t ‡ fd d„ˆD ƒƒ’qS )c                 3   s   | ]}t ˆ jj|ƒV  qd S r   )Ú
issubclassr   Útype)r   Znp_type©r1   r	   r
   r   †   s     z)common_dtype.<locals>.<setcomp>.<genexpr>)Útuple)r   ©Znp_typesr:   r
   r2   …   s   ÿzcommon_dtype.<locals>.<setcomp>r   c                 S   s   g | ]}|j j‘qS r	   )r   r   r0   r	   r	   r
   r   ‹   s     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   r0   r	   r	   r
   r   �   s     )ÚSÚUÚ0r   c                 S   s   g | ]}|d  ‘qS )r   r	   )r   r.   r	   r	   r
   r   ˜   s     )r=   Zbool_Zobject_ÚnumberÚ	characterÚvoidr   r   r5   r   ÚkindÚitemsizer+   r   )ÚcolsZ
uniq_typesZincompat_typesr6   Zarrsr.   Z
arr_commonr	   r<   r
   r4   }   s    
þr4   c                 C   sX   d}t | tƒst|ƒ‚| D ]$}t |tjƒr6|jjd k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    

rL   )r   N)Ú__doc__r    r   r   Úcollections.abcr   Únumpyr=   Ú__all__rJ   r   r-   r7   r4   rL   r	   r	   r	   r
   Ú<module>   s      ÿ
>*