a
    ߙfbw                     @   sJ  d dl Z d dlZd dlmZ d dlZd dlZd dlmZ d dl	m
Z
 d dlmZ d dlmZmZ d dlmZmZmZmZmZmZmZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dlm Z  d dl!m"Z" d dl#m$Z$ d dl%m&Z& d dl'm(Z( ddl)m*Z* dd Z+dd Z,dd Z-dd Z.dd Z/ej0j1ej02deefdd  Z3ej02deefd!d" Z4d#d$ Z5d%d& Z6d'd( Z7d)d* Z8d+d, Z9d-d. Z:d/d0 Z;d1d2 Z<d3d4 Z=d5d6 Z>d7d8 Z?d9d: Z@d;d< ZAd=d> ZBd?d@ ZCdAdB ZDdCdD ZEdEdF ZFdGdH ZGdIdJ ZHdKdL ZIdMdN ZJdOdP ZKej02dQeegdRdS ZLdTdU ZMdS )V    N)StringIO)represent_mixins_as_columns)ParentDtypeInfo)ArrayWrapper)EarthLocationSkyCoord)TableQTablejoinhstackvstackColumnNdarrayMixin)	serialize)time)coordinates)units)
BaseColumn)table_helpers)AstropyUserWarning)MergeConflictWarning)representation_equal)skycoord_equal   )
MIXIN_COLSc                 C   sL  | d }d|j _|j jdks J d|j _|j jdks8J t|tjtjtj	tj
tjfrtt tj|j _W d   q1 s0    Y  ntj|j _|j jtju sJ d|j _|j jdksJ ddi|j _|j jddiksJ tt d|j _W d   n1 s
0    Y  tt |j j W d   n1 s>0    Y  dS )z6
    Required attributes for a column can be set.
    maNr   )infonamedescription
isinstanceuQuantityr   r   r   Time	TimeDelta BaseRepresentationOrDifferentialpytestraisesAttributeErrorr   unitformatmetaZbad_attr)
mixin_colsr    r-   =lib/python3.9/site-packages/astropy/table/tests/test_mixin.pytest_attributes   s(    *
(r/   c                 C   s`   t |jtjrt| tus$t |tr8t|| ju sLJ nt|t|u sLJ |jjd u s\J d S N)	r    r   r!   ZQuantityInfotyper	   r   ColumnClassr   )tableZ	table_colZin_colr-   r-   r.   check_mixin_type@   s    r4   c                 C   sn   |  |}t||d |d  t| }| j |dd}t||d |d  |  |}t||d |d  dS )z
    Make a table with the columns in mixin_cols, which is an ordered dict of
    three cols: 'a' and 'b' are table_types.Column type, and 'm' is a mixin.
    r   )ir   br   namesZcol3N)r   r4   listvalues)table_typesr,   tZcolsr-   r-   r.   test_make_tableN   s    

r=   c                  C   sP   ddl m}  tt}|  D ]0}|d rd|d vrt }|j||d d qdS )z
    Test that table with mixin column can be written by io.ascii for
    every pure Python writer.  No validation of the output is done,
    this just confirms no exceptions.
    r   )_get_connectors_tableZWritez.fast_ZFormatr*   N)Zastropy.io.ascii.connectr>   r	   r   r   write)r>   r<   Zfmtoutr-   r-   r.   test_io_ascii_write^   s    
rB   c                 C   sr   t  }tjg ddd|d< t| d}|j|ddd t j|dd	}t|d tjs\J |d jdksnJ d
S )a  
    Test that table with Quantity mixin column can be round-tripped by
    io.votable.  Note that FITS and HDF5 mixin support are tested (much more
    thoroughly) in their respective subpackage tests
    (io/fits/tests/test_connect.py and io/misc/tests/test_hdf5.py).
    )r         Znmr)   r   	table-tmpvotableTr*   Z	overwriter?   N)	r	   r!   r"   strr
   r@   readr    r)   )tmpdirr<   filenameqtr-   r-   r.   test_votable_quantity_writel   s    rN   r;   c                 C   s~  |ddgddgg}t jD ]P}t jddgddggd|tdd	d
ddd|d| < t jddg|d|d| < qddg|d< t| d}tjtdd  |j	|ddd W d   n1 s0    Y  tjtdd  |j
|ddd}W d   n1 s0    Y  t jD ]}dD ]}|| }t|| t js2J || j|| jksLJ || jd ks`J || j|| jkszJ || || k sJ qqd!D ]4}t|| tsJ || || k sJ qt jD ]*}dD ]}|| }d"|| jjd< qq|j	|ddd |j
|dd#}t jD ]L}dD ]@}|| }t|| t jrVJ || || jk s4J q4q,dS )$a}  
    Test that table with Time mixin columns can be written by io.fits.
    Validation of the output is done. Test that io.fits writes a table
    containing Time mixin columns that can be partially round-tripped
    (metadata scale, location).

    Note that we postpone checking the "local" scale, since that cannot
    be done with format 'cxcsec', as it requires an epoch.
    r   rC   stringcolumn   rD   Zcxcsec隧@ 9> r   rE   r*   scalelocationr   1999-01-01T00:00:00.1234567892010-01-01T00:00:00rV   r6         @      @crF   zQTime Column "btai" has no specified location, but global Time Position is presentmatchfitsTrH   Nz8Time column reference position "TRPOSn" is not specifiedr*   Zastropy_nativer   r6   jdcol0Zcol1r]   formatted_valuer?   )r   ZSTANDARD_TIME_SCALESr#   r   rI   r
   r&   warnsr   r@   rJ   r    rV   r*   rW   allr   r   serialize_methodvalue)rK   r;   r<   rV   rL   tmabr   r-   r-   r.    test_io_time_write_fits_standard}   s\    
..
"

rm   c              
   C   s<  |ddgddgg}t jddgddggd	d
tdddddd|d< t jddgd
d|d< ddg|d< t| d}tjtdd  |j|ddd W d   n1 s0    Y  tjtd d  |j	|ddd!}W d   n1 s0    Y  d"D ]}|d# }t
|| t jsJ || j|| jks.J || jd$ksBJ || j|| jks\J || || k sJ qd%D ]4}t
|| tsJ || || k szJ qzd"D ]}|d# }d&|| jjd< q|j|ddd |j	|dd'}d"D ]@}|d# }t
|| t jrJ || || jk sJ qdS )(a  
    Test that table with a Time mixin with scale local can also be written
    by io.fits. Like ``test_io_time_write_fits_standard`` above, but avoiding
    ``cxcsec`` format, which requires an epoch and thus cannot be used for a
    local time scale.
    r   rC   rO   rP   iQ  iR  iS  iT  ZmjdlocalrR   rS   rT   r   rE   rU   Za_localrX   rY   rZ   Zb_localr[   r\   r]   rF   z/Time Column "b_local" has no specified locationr^   r`   TrH   Nz9Time column reference position "TRPOSn" is not specified.ra   rb   _localrc   rd   rf   r?   )r   r#   r   rI   r
   r&   rg   r   r@   rJ   r    rV   r*   rW   rh   r   r   ri   rj   )rK   r;   r<   rL   rk   rl   r   r-   r-   r.   test_io_time_write_fits_local   sR    

..rp   c                 C   s|   t | }|jttjf}|s(td t }t	t
}|j|dd W d   n1 s\0    Y  dt|jv sxJ dS )zf
    Test that table with mixin columns (excluding Quantity) cannot be written by
    io.votable.
    zno unsupported column typesrG   r?   Nz'cannot write table with mixin column(s))r	   columnsZnot_isinstancer   r!   r"   r&   skipr   r'   
ValueErrorr@   rI   rj   )r,   r<   Zunsupported_colsrA   errr-   r-   r.   test_votable_mixin_write_fail  s    
,ru   c              	   C   s2  |   }| g d|d< | g d|d< t D ]\}}|||< q4|  |}g d|d< t D ]$\}}||| j_|d || j_qddD ]}t||d|d}|d	 }|d
 }t D ]f\}}|d }	|d }
t||	||  t||
||  ||	 jj|ksJ ||
 jj|d ksJ qqdD ]V}t	t
 }t||d|d}W d   n1 s^0    Y  dt|jv s(J q(t	t"}t||ddgd}W d   n1 s0    Y  dt|jv sJ tjtdd  t||dgd}W d   n1 s0    Y  t|d |d ks.J dS )zq
    Join tables with mixin cols.  Use column "i" as proxy for what the
    result should be for each mixin.
    )r   r6   r6   r]   r   r   r   rC   rQ   r5   )r6   r]   r   d2)innerleft)keys	join_typei_1i_2_1_2)outerrightNzjoin requires masking columnZskycoord)r{   z(one or more key columns are not sortablezFIn merged column 'quantity' the 'description' attribute does not matchr^   quantityZa_1)r   r   r   itemsr   r   r
   assert_table_name_col_equalr&   r'   NotImplementedErrorrI   rj   	TypeErrorrg   r   nprh   )r;   t1r   colt2r|   t12idx1idx2name1name2excr-   r-   r.   	test_join"  sB    

020r   c              
   C   s  |   }| g d|d< t D ],\}}|||< ||| j_ddi|| j_q"dD ]:}dD ].}|  |}|r|dd }|d	krtt	 }t
||g|d
}W d   n1 s0    Y  dt|jv s^J q^t
||g|d
}|d }	|d }
t D ]\}}|d }|d }t||||	  t||||
  dD ]R}t|| j|t|| j|ksbJ t|| j|t|| j|ks8J q8qq^qTdS )zs
    Hstack tables with mixin cols.  Use column "i" as proxy for what the
    result should be for each mixin.
    rv   r5   r   r   )ry   r   )TFNr   )r|   zhstack requires masking columnr}   r~   r   r   )r   r+   )r   r   r   r   r   r   r+   r&   r'   r   r   rI   rj   r   getattr)r;   r   r   r   r|   Zchopr   r   r   r   r   r   r   attrr-   r-   r.   test_hstackQ  s6    


.&r   c                 C   s   t |tjrBt| | j|jks&J t| | j|jksJ nt |tjrhtt| | |sJ nnt |t	j
rt| tu rt| | |ksJ n>t |tjrt| | j|jksJ nt| | |ksJ dS )zQ
    Assert all(t[name] == col), with special handling for known mixin cols.
    N)r    r   r   r   rh   radecr%   r   r!   r"   r1   r	   r   r   data)r<   r   r   r-   r-   r.   r   u  s    r   c                 C   s   d}| d }d|j _d|j _d|j _ddi|j _t|g}ddgtdd	gtddfD ]j}|| }|| }t	|d||  |D ]@}t
|d j |t
|j |ksJ t
|j |t
|j |ksJ qqXd
S )zU
    Test that slicing / indexing table gives right values and col attrs inherit
    r   r)   dtyper*   r   r+   r   {0}rw   r   r   rQ   r   rC   N)r   r   r*   r   r+   r	   r   arrayslicer   r   )r,   attrsr   r<   itemr   m2r   r-   r-   r.   test_get_items  s    
" r   c           
      C   s   dd }dd }d}dD ]}| | }||j _d|j _d|j _d	d
i|j _tjtj||fD ]~}||}|D ]l}|dkst|j jdds|j jj	ds|tjtjfv rt|j |}	n|j j
d}	t|j ||	kshJ qhqXqdS )z
    Test copy, pickle, and init from class roundtrip preserve info.  This
    tests not only the mixin classes but a regular column as well.
    c                 S   s   t t | S r0   )pickleloadsdumpsr]   r-   r-   r.   pickle_roundtrip  s    z>test_info_preserved_pickle_copy_init.<locals>.pickle_roundtripc                 S   s
   |  | S r0   )	__class__r   r-   r-   r.   init_from_class  s    z=test_info_preserved_pickle_copy_init.<locals>.init_from_classr   )r5   r   r   rw   r   r   r   ZisnativeTZvoid=N)r   r   r*   r   r+   copydeepcopyr   r   
startswithZnewbyteorder)
r,   r   r   r   Zcolnamer   funcr   r   Zoriginalr-   r-   r.   $test_info_preserved_pickle_copy_init  s,    r   c                 C   s  d}| d }|j jdu sJ t|gdgd}|j jdu s<J ||d< |j jdu sTJ d|j _d|j _d|j _dd	i|j _t|g}||d
< d|j _|j|gdd d|j _|j|gdd dD ]F}t||| |D ]0}|dkrt|d j |t|| j |ksJ qq|d }t	|t	|u rd|j
v r|d |d< t|d|d  |D ]6}|dkrLt|d j |t|d j |ksLJ qLt|d	d gdgd}t	|t	|u rd|j
v r|d |d< t|d|d  |D ]6}|dkrt|d j |t|d j |ksJ qdS )zC
    Test that adding a column preserves values and attributes
    r   r   Nr   r7   newr   rw   r   r   m3T)r   m4F)r   r   r   r   r   r   srC   )r   r   r	   r*   r   r+   Zadd_columnsr   r   r1   __dict__)r,   r   r   r<   r   r   r   r-   r-   r.   test_add_column  sH    
(
*
r   c                  C   sJ   t t} t t}tt t| |g W d   n1 s<0    Y  dS )z(
    Vstack tables with mixin cols.
    N)r	   r   r&   r'   r   r   )r   r   r-   r-   r.   test_vstack  s    r   c                 C   s  t | }| }d|d j_g d}t|d tjttj	tj
tjfr|d|d  |jD ]J}|| }t|tjrt||| | sJ qZt||| | ksZJ qZ|d jjdksJ nNtt }|d|d  W d   n1 s0    Y  dt|jv s
J dS )zT
    Test inserting a row, which works for Column, Quantity, Time and SkyCoord.
    rw   r   )r   r   r   rC   rQ   r   r   NzUnable to insert row)r	   r   r   r   r    r!   r"   r   r   r#   r$   r   r   
insert_rowcolnamesr   r   rh   r&   r'   rs   rI   rj   )r,   r<   t0Zidxsr   r   r   r-   r-   r.   test_insert_row  s     
.r   c                  C   sl   t dgtj g} tt*}| ddtj tj f W d   n1 sL0    Y  dt|j	v shJ dS )z8
    Insert a row into a QTable with the wrong unit
    r   r   rC   Nz='m / s' (speed/velocity) and 'm' (length) are not convertible)
r	   r!   r   r&   r'   rs   r   r   rI   rj   )r<   r   r-   r-   r.   test_insert_row_bad_unit  s    8r   c                 C   sF   t | }| }| d }t|dr*|jjnd}|d jj|ksBJ dS )z
    Test that converting to numpy array creates an object dtype and that
    each instance in the array has the expected type.
    r   r   ON)r	   Zas_arrayhasattrr   kind)r,   r<   Ztar   Z
dtype_kindr-   r-   r.   test_convert_np_array  s
    r   c                  C   sR  dD ]F} t |  }t|gdgd}dtddtddftddgtddgffD ]\}}| }|| |d |< | d	krt|d j| |j| ksJ t|d j| |j| ksJ t|d j| |d j| ksJ qTt|d | || ksJ t|d | || ks(J t|d | |d | ksTJ qTqd
S )zx
    Test that assignment of an int, slice, and fancy index works.
    Along the way test that copying table works.
    )r   	arraywrapr   r7   )r   rC   r   rC   r   rQ   r   N)r   r	   r   r   r   r   rh   r   )r   r   r   Zi0Zi1r<   r-   r-   r.   test_assignment_and_copy*  s     
""(  r   c                  C   s   t t} | j}|D ]}|| | j_qt| }|D ]}|| jj|ksHJ |dkrt|d | d jkslJ t|d j	| d j	u sJ t
|d |jsJ q0t||| |  q0t | }|D ](}|| jj|ksJ t||| |  qdS )zF
    Test that a table round trips from QTable => Table => QTable
    r   N)r	   r   r   r   r   r   r   rh   rj   r)   r    r2   r   )rM   r8   r   r<   Zqt2r-   r-   r.   test_conversion_qtable_tableA  s     r   c                  C   sR   t  } ddg| d< d| d< t| d ddgks4J t| d ddgksNJ dS )z?
    Test for mixin-related regression described in #3321.
    xyr   r6   N)r   r   rh   r<   r-   r-   r.   test_setitem_as_column_nameZ  s
    r   c                  C   s,   t ddgtj g} |  g dks(J dS )zI
    Test that table representation of quantities does not have unit
    r   rC   )re   z m  z----z 1.0z 2.0N)r	   r!   r   pformatr   r-   r-   r.   test_quantity_representatione  s    r   c                  C   sH  t jdgdgdgtjd} t| g}| g dks8J t jdgdgdgdd} t| g}| g dksnJ t dgtj dgtj dgtj } t| g}| g d	ksJ t 	dgtj dgtj } t| g}| g d
ksJ t 
dgtj tj dgtj tj dgtj tj } t| g}| g dksDJ dS )z>
    Test that Representations are represented correctly.
    r   r   rE   )    col0    ------------(0., 1., 0.)r   )r   z     m      r   r   
      )     col0     z deg, deg, pc --------------z(10., 20., 1.))z
   col0   z
   deg    z
----------z
(10., 20.)rC   )z           col0           zmas / yr, mas / yr, km / sz--------------------------z            (10., 2., 10.)N)r   ZCartesianRepresentationr!   Zoner   r   ZSphericalRepresentationdegZpcZUnitSphericalRepresentationZSphericalCosLatDifferentialZmasZyrZkmr   r]   r<   r-   r-   r.   "test_representation_representationq  s"    

&

0
r   c                  C   s   t jdgdgdgdd} t| g}| g dks6J t jdgdgdgddd} t| g}| g dksnJ d	|d
 _| g dksJ d|d
 _| g dksJ dS )z
    Test that skycoord representation works, both in the way that the
    values are output and in changing the frame representation.
    r   r   Z	cartesian)representation_type)r   zNone,None,Noner   z   0.0,1.0,0.0r   )r)   r   )z    col0   z   m,m,m   z-----------z0.0,1.0,0.0Zunitsphericalre   )  col0  zdeg,deg --------z90.0,0.0Zcylindrical)r   z  m,deg,m   r   z1.0,90.0,0.0N)r   r   r   r   r   r   r-   r-   r.   test_skycoord_representation  s    



r   c                  C   s  t jg ddd} t jg dddgd}t jjg ddd	gd
}t dddt}t| gdgd
}||d< ||d< ||d< t	|d tsJ |d d d | d d ksJ |d d d | d d ksJ |d d d | d d ksJ |d d d | d d ksJ t	|d ts(J |d d d |d d ksJJ |d d d |d d kslJ |d d d |d d ksJ |d d d |d d ksJ t	|d tsJ |d d d |d d ksJ |d d d	 |d d	 ksJ |d d d |d d ks*J |d d d	 |d d	 ksLJ t	|d ts`J |d d d |d d ksJ |d d d |d d ksJ |d d d |d d ksJ |d d d |d d ksJ |
 g dksJ dS )z
    Test directly adding a plain structured array into a table instead of the
    view as an NdarrayMixin.  Once added as an NdarrayMixin then all the previous
    tests apply.
    ))r   r   )rC   r6   )rQ   r]   )rD   rw   z<i4,|U1)r   ))r   Zaa)r   Zbb)   Zcc)(   Zdd)r   Zi4)r   ZU2))d   Zraa)   Zrbb)i,  Zrcc)i  ZrddZrxZryr7      rD   rC   r   r6   r]   rw   r   r   r   r   )z'   a         b           c       d [2] z'-------- ---------- ------------ ------z'(1, 'a') (10, 'aa') (100, 'raa') 0 .. 1z'(2, 'b') (20, 'bb') (200, 'rbb') 2 .. 3z'(3, 'c') (30, 'cc') (300, 'rcc') 4 .. 5z'(4, 'd') (40, 'dd') (400, 'rdd') 6 .. 7N)r   r   ZrecZfromrecordsZarangeZreshapeZviewr   r   r    r   )r   r6   r]   rw   r<   r-   r-   r.   test_ndarray_mixin  sF    

   """""""""""""r   c                  C   sx   t ddgtj g} d| d j_|  g dks4J d| d j_|  g dksTJ d| d j_|  g d	kstJ d
S )z
    The QuantityInfo info class for Quantity implements a
    possible_string_format_functions() method that overrides the
    standard pprint._possible_string_format_functions() function.
    Test this.
    r   rC   z%.3fre   )z col0z  m  z-----z1.000z2.000z	hi {:.3f})r   z   m    r   zhi 1.000zhi 2.000z.4f)z col0 z  m   z------z1.0000z2.0000N)r	   r!   r   r   r*   r   r   r-   r-   r.   %test_possible_string_format_functions  s    r   c                 C   s   t | }| }|dd |jg dks.J t|d tjr^t|d j	|d j	ksJ nt|d t
jrt|d j|d jksJ t|d j|d jksJ nHt|d t
jrtt|d |d sJ nt|d |d ksJ dS )z 
    Rename a mixin column.
    r   mm)r5   r   r6   r   N)r	   r   Zrename_columnr   r    r   r   r   rh   r   r   r   r   r   r%   r   )r,   r<   Ztcr-   r-   r.   test_rename_mixin_columns  s      r   c                  C   s:   t dddgidd} d| d _d| d jd< t|  dS )zv
    If the unit is invalid for a column that gets serialized this would
    cause an exception.  Fixed in #7481.
    r   r   rC   T)Zmaskedznot a valid unitN)r   r)   maskr   r   r   r-   r-   r.   )test_represent_mixins_as_columns_unit_fix)  s    
r   c                  C   sv   t dddgtj i} d| d j_d| d j_t| }d|jd d vsNJ |d jdks`J |d jdksrJ dS )	z
    If the mixin defines a primary data column, that should get the
    description, format, etc., so no __info__ should be needed.
    r   r   rC   Zparrotz7.2fZ__info__Z__serialized_columns__N)	r	   r!   r   r   r   r*   r   r   r+   )r<   Ztserr-   r-   r.   )test_primary_data_column_gets_description4  s    
r   c                  C   sf   t dgdgdd d} t| g}t }|j|ddd |d tj| dd	}t|d
 | sbJ d S )Nr   rC   r   )r)   Zgalcen_v_sunz
ascii.ecsvTrH   r   r?   re   )r   r   r   r@   seekrJ   r   )scr<   r   r   r-   r-   r.   test_skycoord_with_velocityB  s    

r   	table_clsc                 C   s   ddgt j }d|jvsJ | |gdgd}d|jvs:J | |g}d|jvsRJ | d|i}d|jvslJ ||d< d|jvsJ tddgddgdd	}||d
< d|jvsJ dS )zIf a mixin input to a table has no info, it should stay that way.

    This since having 'info' slows down slicing, etc.
    See gh-11066.
    r   rC   r   qr7   Zq2rQ   r   rE   r   N)r!   r   r   r   )r   r   r<   r   r-   r-   r.   #test_ensure_input_info_is_unchangedM  s    
r   c                  C   sj   G dd dt } t }| g d|d< t }d}tjt|d t| W d   n1 s\0    Y  dS )zjMake a mixin column class that does not trigger the machinery to generate
    a pure column representationc                   @   s   e Zd Ze ZdS )z+test_bad_info_class.<locals>.MyArrayWrapperN)__name__
__module____qualname__r   r   r-   r-   r-   r.   MyArrayWrapperf  s   r   )r   r   rC   rk   zSfailed to represent column 'tm' \(MyArrayWrapper\) as one or more Column subclassesr^   N)r   r   r   r&   r'   r   r   )r   r<   rA   r_   r-   r-   r.   test_bad_info_classc  s    r   )Nr   r   ior   r&   Znumpyr   Zastropy.table.serializer   Zastropy.utils.data_infor   Zastropy.table.table_helpersr   Zastropy.coordinatesr   r   Zastropy.tabler   r	   r
   r   r   r   r   r   Zastropyr   r   r   r!   Zastropy.table.columnr   r   Zastropy.utils.exceptionsr   Zastropy.utils.metadatar   Z-astropy.coordinates.tests.test_representationr   Z!astropy.io.misc.asdf.tags.helpersr   Zconftestr   r/   r4   r=   rB   rN   ZmarkZremote_dataZparametrizerm   rp   ru   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r-   r-   r-   r.   <module>   sp   $!K
D/$!5

)"<
