
    IR-e                     t    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e	          Z
	 ddZd	 Zd
 Z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                       e Zd ZdS )r   N)__name__
__module____qualname__     6lib/python3.11/site-packages/astropy/table/np_utils.pyr   r      s        Dr   {col_name}_{table_name}c                     t          j         fd          g }|&d t          t                               D             }t	                     D ]\  }}||         }|j        j        D ]}|v r|vr|                               nkt                     }	|		                    |           t          fd|	D                       r|                    |          }|                    |           |         |<   t          |          }
d |
                                D             }|r"t          d                    |                    t          f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                  *    d gt                     z  S N)len)arrayss   r   <lambda>z"get_col_name_map.<locals>.<lambda>$   s    D6CKK3G r   Nc                 2    g | ]}t          |d z             S    )str).0iis     r   
<listcomp>z$get_col_name_map.<locals>.<listcomp>(   s"    @@@rs26{{@@@r   c              3   4   K   | ]}|j         j        v V  d S r   )dtypenames)r   othernames     r   	<genexpr>z#get_col_name_map.<locals>.<genexpr>9   s-      EEUtu{00EEEEEEr   )
table_namecol_namec                 $    g | ]\  }}|d k    |S r   r   )r   r    counts      r   r   z$get_col_name_map.<locals>.<listcomp>C   s"    RRR{tUPQ		d			r   zgMerging column names resulted in duplicates: {}.  Change uniq_col_name or table_names args to fix this.c              3   ,   K   | ]}||         fV  d S r   r   )r   r    col_name_maps     r   r!   z#get_col_name_map.<locals>.<genexpr>M   s-      TTdl4&89TTTTTTr   )collectionsdefaultdictranger   	enumerater   r   appendlistpopanyformatr   itemsr   r   )r   common_namesuniq_col_nametable_namescol_name_listidxarrayr"   out_nameotherscol_name_countrepeated_namesr'   r    s   `           @@r   get_col_name_mapr<      s    *+G+G+G+GHHLM@@U3v;;-?-?@@@'' / /
U %
K% 	/ 	/DH|## },,!((... f

3EEEEfEEEEE ,33#-  4    H $$X...*.L"3'''	/, ]++NRRn.B.B.D.DRRRN 
DDJFE E
 
 	
 TTTTmTTTTTLr   c                    g }|                                 D ]\  }}d t          | |          D             }d |D             }	 t          |          }nA# t          $ r4}t          d                    |d         |j                            |d}~ww xY wd |D             }	t          |	          dk    rt          d          |	                                }
|t          |          }|	                    |||
f           |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                 (    g | ]\  }}|||         S r   r   )r   arrr    s      r   r   zget_descrs.<locals>.<listcomp>]   s%    XXXdtGW3t9GWGWGWr   c                     g | ]}||S r   r   )r   r    s     r   r   zget_descrs.<locals>.<listcomp>`   s    ???$d.>.>.>.>r   z,The '{}' columns have incompatible types: {}r   Nc                 ,    h | ]}|j         d d         S )r   N)shaper   cols     r   	<setcomp>zget_descrs.<locals>.<setcomp>o   s"    888sy}888r   r   z Key columns have different shape)
r1   zipcommon_dtyper   r0   _incompat_typesr   r.   r   r,   )r   r'   
out_descrsr8   in_namesin_colsr   r   tmeuniq_shapesrB   s              r   
get_descrsrN   R   sB    J*0022 4 4(XXc&(.C.CXXX @?(???		 ))EE 	 	 	 ">EE!Hc1   		 98888{q  !"DEEE!!8}}H8UE23333s   A
B/BBc                    t           j        t           j        t           j        t           j        t           j        ffd| D             }t          |          dk    r'd | D             }t          d|           }||_        |d | D             }|D ]}|j	        j
        dv rd|j        z  |d<   t          j        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                 H    h | ]t          fd D                       S )c              3   L   K   | ]}t          j        j        |          V  d S r   )
issubclassr   type)r   np_typerD   s     r   r!   z)common_dtype.<locals>.<setcomp>.<genexpr>   s1      JJgj11JJJJJJr   )tuple)r   rD   np_typess    @r   rE   zcommon_dtype.<locals>.<setcomp>   sG        	JJJJJJJJJ  r   r   c                 &    g | ]}|j         j        S r   )r   r    rC   s     r   r   z common_dtype.<locals>.<listcomp>   s    999S#).999r   z Columns have incompatible types c                 D    g | ]}t          j        d |j                  S )r   )r   )npemptyr   rC   s     r   r   z common_dtype.<locals>.<listcomp>   s)    999SBHQci(((999r   )SU0r   c                     g | ]
}|d          S )r   r   )r   r?   s     r   r   z common_dtype.<locals>.<listcomp>   s    222c3q6222r   )rY   bool_object_number	charactervoidr   r   rH   r   kinditemsizer7   r   )cols
uniq_typesincompat_typesrL   arrsr?   
arr_commonrV   s          @r   rG   rG   {   s     "*birwGH     J :99D999QQQRR,	99D999D  ( (9>Z''3<'CF22T22233Jr   c                 
   d}t          | t                    st          |          | D ]7}t          |t          j                  r|j        j        t          |          8t          |           dk    rt          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   	TypeErrorrY   ndarrayr   r   r   
ValueError)r   errr7   s      r   (_check_for_sequence_of_structured_arraysrq      s    
LCfh'' nn ! !%,, 	!0A0IC..  1J
6{{aGHHH r   )r   N)__doc__r(   r   r   collections.abcr   numpyrY   __all__ro   r   r<   rN   rG   rq   r   r   r   <module>rv      s         , , , , , , , , $ $ $ $ $ $    
	 	 	 	 	j 	 	 	
 PT: : : :z& & &R     >	I 	I 	I 	I 	Ir   