a
    ߙfb?                     @   s  d dl m Z  d dlZd dlmZmZ d dlmZmZ d dl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mZ d dlmZ e	g dZeg dg dg dgg ddZedZdd Zdd Zdd Zdd Zdd Z dd Z!dd  Z"d!d" Z#d#d$ Z$d%d& Z%d'd( Z&d)d* Z'd+d, Z(d-d. Z)d/d0 Z*d1d2 Z+d3d4 Z,d5d6 Z-d7d8 Z.d9d: Z/ej0j1d;d<d=d> Z2ej0j1d;d<d?d@ Z3dAdB Z4ej05dCeegdDdE Z6dS )F    )datetimeN)assert_equalassert_allclose)TableColumn)Time	TimeDelta)units)Quantity)get_pkg_data_filename)assert_quantity_allclose)BoxLeastSquaresLombScargle)
TimeSeries)z2016-03-22T12:30:31z2015-01-21T12:30:32z2016-03-22T12:30:40)         )      r   )r   r   r   )abc)nameszdata/sampled.csvc                  C   s   t  } tg ddd| d< d S )N)iQ  iR  iS  Zmjdformattime)r   r   ts r   Dlib/python3.9/site-packages/astropy/timeseries/tests/test_sampled.pytest_empty_initialization   s    r    c                  C   sT   t  } tt}g d| d< W d    n1 s20    Y  |jjd dksPJ d S )Nr   r   r   fluxr   zSTimeSeries object is invalid - expected 'time' as the first column but found 'flux')r   pytestraises
ValueErrorvalueargsr   excr   r   r   !test_empty_initialization_invalid    s    *r*   c                  C   s0   t td} | d | ju sJ t| jjtj d S )Nr   r   )r   
INPUT_TIMEr   r   isotr   r   r   r   test_initialize_only_time)   s    
r.   c                  C   sV   t tg dg dgddgd} t| jjtj t| d g d t| d g d d S )N
   r   r   r         r   r   r   datar   )r   r,   r   r   r-   r   r   r   r   test_initialization_with_data0   s    r6   c                  C   s^   t t,} tg dg dgddgd W d    n1 s<0    Y  | jjd dksZJ d S )Nr/   r1   r   r   r5   r   r   z1Either 'time' or 'time_start' should be specified)r#   r$   	TypeErrorr   r&   r'   r)   r   r   r   test_initialize_only_data7   s    :r:   c                  C   s"   t ttd} | jg dksJ d S )N)r   r5   r   r   r   r   )r   r,   PLAIN_TABLEcolnamesr   r   r   r   test_initialization_with_table=   s    r>   c                  C   sL   t tddddddtdddg dg d	gd
dgd} t| jjg d d S )N     r   r0   r   secr   r/   r1   r   r   
time_start
time_deltar5   r   )z2018-07-01T10:10:10.000z2018-07-01T10:10:13.000z2018-07-01T10:10:16.000)r   r   r   r   r   r-   r   r   r   r   #test_initialization_with_time_deltaB   s
    
rE   c               	   C   sn   t t<} ttddddddg dg dgddgd	 W d    n1 sL0    Y  | jjd
 dksjJ d S )Nr?   r@   r   r0   r/   r1   r   r   )rC   r5   r   r   z-'time' is scalar, so 'time_delta' is requiredr#   r$   r8   r   r   r&   r'   r9   r   r   r   &test_initialization_missing_time_deltaK   s
    $rG   c               
   C   sp   t t>} tttddddddg dg dgddgd	 W d    n1 sN0    Y  | jjd
 dkslJ d S )Nr?   r@   r   r0   r/   r1   r   r   )r   rC   r5   r   r   z+Cannot specify both 'time' and 'time_start')r#   r$   r8   r   r,   r   r&   r'   r9   r   r   r   /test_initialization_invalid_time_and_time_startR   s
    $rH   c               	   C   st   t tB} ttddddddg dg dg dgdd	gd
 W d    n1 sR0    Y  | jjd dkspJ d S )Nr?   r@   r   r0   )r   r   r   r/   r1   r   r   rB   r   z0'time_delta' should be a Quantity or a TimeDeltarF   r9   r   r   r   &test_initialization_invalid_time_deltaY   s    $rI   c                  C   s4  t  } t| d< t| d}t|jtg dks4J t|jtksFJ tg dtgddgd}t|jtddgksxJ t|jtksJ t	t
}t| td W d    n1 s0    Y  |jjd d	ksJ t	t
*}ttg dtgddgd
 W d    n1 s0    Y  |jjd d	ks0J d S )Nr   )r5   r;   r/   r   r7   )r5   r   r   zA'time' has been given both in the table and as a keyword argumentr4   )r<   copyr,   r   setr=   allr   r#   r$   r8   r&   r'   )r5   Zts1Zts2r)   r   r   r   %test_initialization_with_time_in_dataa   s    
*:rM   c                  C   sP   t t} tttdd W d    n1 s.0    Y  | jjd dksLJ d S )Ni  )r   r5   Z	n_samplesr   zP'n_samples' has been given both and it is not the same length as the input data.)r#   r$   r8   r   r,   r<   r&   r'   r9   r   r   r   test_initialization_n_samplesz   s    ,rN   c                  C   s`   t t.} ttddgddggddgd W d    n1 s>0    Y  | jjd d	ks\J d S )
Nr0   r   r   r2   r   r   r4   r   z1Length of 'time' (3) should match data length (2))r#   r$   r%   r   r,   r&   r'   r9   r   r   r   #test_initialization_length_mismatch   s    <rO   c                  C   sV   t t$} tttdddd W d    n1 s40    Y  | jjd dksRJ d S )Nr   rA   r   )r   rD   r   z='time_delta' should not be specified since 'time' is an array)r#   r$   r8   r   r,   r   r&   r'   r9   r   r   r   4test_initialization_invalid_both_time_and_time_delta   s    2rP   c                  C   s  t g ddd} t| d}g d|d< |jdtj d}t|jtsHJ t|jj	g d	d
d |jdtj t dddd}t|jtsJ t|jj	g dd
d
d |jdtj dtj d}t|jtsJ t|jj	g dd
d |jdtj dtj d}t|jtsJ t|jj	g dd
d |jdtj dtj dtj d}t|jtsXJ t|jj	g dd
d |jdtj dd}t|jt
sJ t|jtjg dd
d |jdtj t ddddd}t|jt
sJ t|jtjg dd
d
d |jdtj ddd}t|jt
s&J t|jtjg dd
d |jdtj ddd }t|jt
sjJ t|jtjg d!d
d |jdtj dddd"}t|jt
sJ t|jtjg d#d
d d S )$Nr   r   r      	      unixr   r+   r   r   r   r   r   r   r"   皙	@period)r   r   g333333333333?gffffff?gư>)rtol皙?)rY   
epoch_time)g333333g?r[           g      ?皙?)r\   ZatolrY   
wrap_phase)r   r   r   rZ   r]   r[   r`   rY   epoch_phase)r`   gffffffgٿr[   gg      )rY   rd   rb   )r`   g?gffffff@r[   g333333@皙@T)rY   normalize_phase)r         ?g׿?g      ?)rY   r^   rf   )gǿg      ?ri   r_   rg         ?r   rY   rb   rf   )r   rg   g      ?rh   g      ?ri   rj   rY   rd   rf   )rj   gۿg      ri   g      пg      Կ)rY   rd   rb   rf   )rj   g      ?g?ri   g?g      ?)r   r   foldus
isinstancer   r   r   rA   r
   Zto_valueZone)timesr   Ztsfr   r   r   	test_fold   sh    
"rr   c                  C   s  t g ddd} t| d}g d|d< tjtjdd |jd	d
 W d    n1 sX0    Y  tjtjdd" |jd	tj d
 W d    n1 s0    Y  tjtjdd$ |jd	tj dd W d    n1 s0    Y  tjtjdd, |jd	tj dtj dd W d    n1 s,0    Y  tjtjdd$ |jd	tj dd W d    n1 sr0    Y  tjtjdd, |jd	tj dtj dd W d    n1 s0    Y  tjt	dd* |jd	tj dtj d W d    n1 s
0    Y  tjt	dd* |jd	tj dtj d W d    n1 sT0    Y  tjt	dd& |jd	tj ddd W d    n1 s0    Y  tjt	dd& |jd	tj ddd W d    n1 s0    Y  d S )NrQ   rU   r   r+   rV   r"   z,period should be a Quantity in units of timematchrW   rX   zLepoch_phase should be a Quantity in units of time when normalize_phase=Falseg?rc   zSepoch_phase should be a dimensionless Quantity or a float when normalize_phase=TrueTrl   zKwrap_phase should be a Quantity in units of time when normalize_phase=Falsera   z<wrap_phase should be dimensionless when normalize_phase=Truerk   z-wrap_phase should be between 0 and the periodggz$wrap_phase should be between 0 and 1re   )
r   r   r#   r$   rn   Z
UnitsErrorrm   mro   r%   )rq   r   r   r   r   test_fold_invalid_options   sV    
*02<4<::6rv   c                  C   s  t d} |  }g d|d< |j| tjdd t|}t	|j
jtj |jddgks`J t|jdksrJ |jd jd tk sJ tj|d	d
}|j
jd	ksJ | }|jj| tjjk sJ |j| dgksJ |d |d k sJ t t}td  W d    n1 s.0    Y  |jjd dksNJ |  }g d|d< t t}t| W d    n1 s0    Y  |jjd dksJ d S )Npandasr!   r   T)Zinplacer   r   r   Ztcb)Z
time_scalez"Input should be a pandas DataFramez'DataFrame does not have a DatetimeIndex)r#   ZimportorskipZ	DataFrameZ	set_indexZDatetimeIndexr,   Z
datetime64r   Zfrom_pandasr   r   r-   r=   lenindicescolumnsrL   scaleZ	to_pandasindexvaluesZIndexr$   r8   r&   r'   )rw   Zdf1r   Zts_tcbZdf2r)   Zdf4r   r   r   test_pandas  s.    

**r~   c                  C   sP   t t} tjtdd W d    n1 s.0    Y  | jjd dksLJ d S )Ncsvr   r   zR``time_column`` should be provided since the default Table readers are being used.r#   r$   r%   r   readCSV_FILEr&   r'   r9   r   r   r   test_read_time_missing0  s    ,r   c                  C   sR   t t } tjtddd W d    n1 s00    Y  | jjd dksNJ d S )Nabcr   Ztime_columnr   r   z.Time column 'abc' not found in the input data.r   r9   r   r   r   test_read_time_wrong6  s    .r   c                  C   s\   t jtddd} | jg dks"J t| dks2J | d jdksDJ | d  d	ksXJ d S )
NZDater   r   )r   ABCDEFGr   r   Zisor   g     p@)r   r   r   r=   rx   r   sum)
timeseriesr   r   r   	test_read<  s
    r   astropy)sourcec                  C   sv   t d} tj| dd}|d jdks(J |d jdks:J |d j dksPJ t|d	ks`J t|jd
ksrJ d S )Nz/timeseries/kplr010666592-2009131110544_slc.fitszkepler.fitsr   r   r-   tdbsap_fluxelectron / si7     )	r   r   r   r   r{   unit	to_stringrx   rz   filenamer   r   r   r   test_kepler_astropyD  s    r   c                  C   s   t d} tjtdd tj| dd}W d    n1 s:0    Y  |d jdksVJ |d jdkshJ |d	 j	 d
ks~J t
|dksJ t
|jdksJ d S )NzJtimeseries/hlsp_tess-data-alerts_tess_phot_00025155310-s01_tess_v1_lc.fitsz Ignoring 815 rows with NaN timesrs   z	tess.fitsr   r   r-   r   r   r   i=K  r   )r   r#   ZwarnsUserWarningr   r   r   r{   r   r   rx   rz   r   r   r   r   test_tess_astropyO  s    ,r   c                  C   s@  t tg dg dgddgd} |   tt.}|  jtg dddd	d
 W d    n1 sd0    Y  |jj	d	 dksJ ttB}|  j
tg dddtg dddgd	dgd W d    n1 s0    Y  |jj	d	 dksJ tt"}|  ddg W d    n1 s&0    Y  |jj	d	 dksFJ tt}|  d W d    n1 sv0    Y  |jj	d	 dksJ tt"}|  ddg W d    n1 s0    Y  |jj	d	 dksJ tt }|  dd W d    n1 s0    Y  |jj	d	 dks<J d S )Nr/   r1   r   r   r4   )r   r   r2   r   )namer   )r|   zPTimeSeries object is invalid - expected 'time' as the first column but found 'c'der   )ZindexeszPTimeSeries object is invalid - expected 'time' as the first column but found 'd'zPTimeSeries object is invalid - expected 'time' as the first column but found 'a'r   zPTimeSeries object is invalid - expected 'time' as the first column but found 'b'ZbananazUTimeSeries object is invalid - expected 'time' as the first column but found 'banana')r   r,   rJ   r#   r$   r%   Z
add_columnr   r&   r'   Zadd_columnsZkeep_columnsZremove_columnZremove_columnsZrename_columnr(   r   r   r   test_required_columns[  s6    	<$2.20r   clsc                 C   s   t tg dg dgddgd}| |d}t|| s8J t|jj|jj t|j	|d  |j
d u shJ | j|ddd}t|j
|d  | j|ddd}t|j
d d S )Nr/   r1   r   r   r4   )Zuncertaintyg?)r   r,   Zfrom_timeseriesrp   r   tZjdr   r   yZdyr   )r   r   Zp1Zp2Zp3r   r   r   test_periodogram  s    r   )7r   r#   Znumpy.testingr   r   Zastropy.tabler   r   Zastropy.timer   r   r   r	   rn   Zastropy.unitsr
   Zastropy.utils.datar   Zastropy.tests.helperr   Zastropy.timeseries.periodogramsr   r   Zastropy.timeseries.sampledr   r,   r<   r   r    r*   r.   r6   r:   r>   rE   rG   rH   rI   rM   rN   rO   rP   rr   rv   r~   r   r   r   ZmarkZremote_datar   r   r   Zparametrizer   r   r   r   r   <module>   sP   "		
J4!


/