U
    #cM                     @   s   d Z ddlZddlmZ ddlmZ ddlmZ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 ddlmZmZmZ dd	lmZ G d
d dZG dd dZG dd dZdd ZdS )z^Tests suite for mrecords.

:author: Pierre Gerard-Marchant
:contact: pierregm_at_uga_dot_edu

    N)recarray)maskednomask)temppath)fromrecords
fromarrays)MaskedRecords	mrecarrayr   fromtextfiler   addfield)assert_assert_equalassert_equal_records)picklec                   @   s   e Zd ZdddddgZdddd	d
gZdddddgZdefdefdgZdddddgZ	e
jeeeeee	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/d0 Zd1d2 Zd3S )4TestMRecords               皙?皙@ffffff
@皙@      @   one   two   three   four   fiveabc|S8r   maskdtypec                 C   s   | j }|t}t|j|j t|j|j tt|j	t
 t|j	|j	t
 dD ]}t|| ||  qTt|t| d S )Nr    r!   r#   )baseviewr	   r   
recordmaskr   _maskr   
isinstance_datar   )selfr)   mbasefield r2   @/tmp/pip-unpacked-wheel-b2rbor69/numpy/ma/tests/test_mrecords.pytest_byview$   s    
zTestMRecords.test_byviewc                 C   s  | j  }|t}dD ]*}tt||||  t|| ||  q|d }tt|t t|j|j t|	 d t|j
t t|j d t|d |d d  |d }tt|t t|j|j t|	 d t|j
d t|j d	 t|d |d d  t|d tk |d d
 }tt|t t|j|j t|j
ddg t|jtjdd	g|jjd t||d d
 t dD ]"}tt|||d d
 |  qd S )Nr(   r   )r   r   r   )FFFr    )NNNT)TTTr   r   r'   )r)   copyr*   r	   r   getattrr   r-   r'   tolistr+   r   r,   itemr   r   r&   nparray)r/   r)   r0   r1   Zmbase_firstZ
mbase_lastZmbase_slr2   r2   r3   test_get0   sD    

zTestMRecords.test_getc                 C   sH  | j  }|t}| }d|_d|jjd d < t|d jdddddg t|d jdddddg d|_t|d jdgd  tt	
|d dgd  t|jdgd  t|j tjdddddgtd	 t|_t|jjdgd  t|jjdgd  tt	
|d
 dgd  tt	|d
 dgd  t|j tjdddddgtd	 |t }d|jdd < t|jdddddg t|jjdddddg t|jdd < t|j|d  t|jjdddddg ddtfg}t	jdddg|d	}|t}t	jdddgdddgd}||d< t|j| t|jjdddg d S )N)i?B g@xDN/Ar   r    r   r   Fr   r   r   )r   r   r   r6   r#      N/A)r   r   r   r   r   r!   )alphaz|S1num)r    r   )r!   r   )r#   r   
         r&   )r)   r7   r*   r	   
fill_valuer    r.   r   r,   magetmaskarrayr+   r9   r;   r<   boolr   r#   r&   Zgetdatar!   intr   rB   )r/   r)   r0   ndtypedataZrdatavalr2   r2   r3   test_set_fieldsX   sd    




zTestMRecords.test_set_fieldsc                 C   s   | j  }|t}t|d d< t|jdddddg t|jjdddddg tt	
dt	jdgdtfd	tfgd
}t|d d< t|jdddddg t|jjdddddg d S )Nr    r   r   r   r   r   r   r!   r6   )r)   r7   r*   r	   r   r   r    r,   r   r;   arangerandomrandrK   floatr/   r)   r0   r2   r2   r3   test_set_fields_mask   s    

z!TestMRecords.test_set_fields_maskc                 C   s   | j  }|t}t|_tt|d dgd  t|d j	|d j	 t|d j	|d j	 t|j	
 tjdgd td t|_tt|d dgd  t|j	
 tjd	gd td d S )
Nr!   r   r   r    r#   r   r   r   r6   r   r?   )r)   r7   r*   r	   r   r&   r   rH   rI   r,   r9   r;   r<   rJ   r   rU   r2   r2   r3   test_set_mask   s    



zTestMRecords.test_set_maskc                 C   s   | j  }|t}dddddg|_t|jjdddddg t|jjdddddg t|jjdddddg dddddg|_t|jjdddddg t|jjdddddg t|jjdddddg d S )Nr   r   )	r)   r7   r*   r	   r&   r   r    r!   r#   rU   r2   r2   r3   test_set_mask_fromarray   s    

z$TestMRecords.test_set_mask_fromarrayc                 C   s   | j  t}tjdddddgdtfdtfdtfgd}||_t|j	jddd	d	dg t|j
jd	d	dddg t|jjddd	d	dg d
|_||_t|j	jddd	d	dg t|j
jd	d	dddg t|jjddd	d	dg d S )N)r   r   r   )r   r   r   r?   r    r!   r#   r6   r   r   F)r)   r7   r*   r	   r;   r<   rJ   r&   r   r    r!   r#   Z	fieldmask)r/   r0   Znmaskr2   r2   r3   test_set_mask_fromfields   s    z%TestMRecords.test_set_mask_fromfieldsc                 C   s  | j  }|t }t|d< t|j tj	dddddgt
d t|jdddddg |t }d|d d< t|jjd	d	d
dd	g t|jjdddddg t|jjdddddg t|jjdddddg t|jjdddddg t|jjdddddg |t }t|d d< t|jjddd
dd	g t|jjdddddg t|jjdddddg t|jjdddddg t|jjdddddg t|jjdddddg d S )NrP   r?   rW   r6   r   r   r   r   r   r   r   r   r   g      @r   r   r      5r   r   r   r   r   r   r   )r)   r7   r*   r	   r   r   r,   r9   r;   r<   rJ   r+   r    r.   r!   r#   rU   r2   r2   r3   test_set_elements   s<    
zTestMRecords.test_set_elementsc              	   C   s  | j  }|t}|  zd|dd < t|jjdddddg t|jjddd	dd
g t|j	jdddddg t|jj
dddddg t|jj
|jj
 t|jj
|j	j
 W n( tk
r   Y n tk
r    Y n
X tdzd|dd < W n ttfk
r   Y n
X tdd S )Nr[   rP   r   r   r   r   r   r   r   r   r   r   r   r\   r   r   z)Flexible hard masks should be supported !z.Should have expected a readable buffer object!)r)   r7   r*   r	   harden_maskr   r    r.   r!   r#   r,   NotImplementedErrorAssertionError	Exception	TypeErrorrU   r2   r2   r3   test_setslices_hardmask   s.    

z$TestMRecords.test_setslices_hardmaskc                 C   s   | j  }|t}|  t|j t|_t	|j
|j
 |  t|j  t|_t	|j
t|j|j tt|d j
tk t|d j
|d j
 d S )Nr!   r    )r)   r7   r*   r	   r^   r   Z	_hardmaskr   r&   r   r,   Zsoften_maskrH   Zmake_mask_noneshaper'   Z	make_maskr   rU   r2   r2   r3   test_hardmask
  s    


zTestMRecords.test_hardmaskc                 C   s~   | j  }|t}tdtjd D ]T}tj||d}t|}t	|j
|j
 t|j|j t	|j|j t|j|j q$d S )Nr   r   )protocol)r)   r7   r*   r	   ranger   HIGHEST_PROTOCOLdumpsloadsr   r'   r   r.   r,   )r/   r)   mrecproto_Zmrec_r2   r2   r3   test_pickling  s    


zTestMRecords.test_picklingc                 C   s   t jdddgdddgtd}t jdddgdddgtd}t jd	d
dgdddgdd}dtfdtfdg}t|||g|dd}| }t|d tjdtd t|d tjdtd t|d tjddd d S )Nr   r   r   r   r%   r   r   r   onetwothreer$   r    r!   r"   韆     i@r>   r'   rG   )r   r   rs   r6   )r   r   rt   r#   )ro   rp   r>   )rH   r<   rK   rT   r   filledr   r;   )r/   _a_b_cddtyperk   Z
mrecfilledr2   r2   r3   test_filled'  s    zTestMRecords.test_filledc                 C   s   t jdddgdddgtd}t jdddgdddgtd}t jd	d
dgdddgdd}dtfdtfdg}t|||g|dd}t| dddg d S )Nr   r   r   r   r%   r   r   r   ro   rp   rq   r$   r    r!   r"   rr   ru   )r   r   N)r   r   r   )NNr   )rH   r<   rK   rT   r   r   r9   )r/   rw   rx   ry   rz   rk   r2   r2   r3   test_tolist6  s    zTestMRecords.test_tolistc                 C   s0   t dtdd}d|d d< t|d d d d S )Nr   r)   )formatsnamesrC   r   )r	   rT   r   )r/   xr2   r2   r3   test_withnamesC  s    zTestMRecords.test_withnamesc                 C   s   t ddtfddtfgd}t|d< t|d d t ddgd}t|d< t|d tjd	|j	d  t d
dd}t|d< d|d< |d t
|dtjddg|j	d d S )Nr   i)sr$   fr6   r   )r      1g      ?)Zf0z<f8)r   r   )r   r   zi4, (2,3)float, floatrW   r?   )r	   rK   rT   r   r   rv   r:   r;   r<   r'   r   )r/   ZeasyZsoloZmultr2   r2   r3   test_exotic_formatsI  s"    


z TestMRecords.test_exotic_formatsN)__name__
__module____qualname__ZilistflistslistrK   rT   rz   r&   rH   r<   listzipr)   r4   r=   rO   rV   rX   rY   rZ   r]   rc   re   rn   r{   r|   r   r   r2   r2   r2   r3   r      s*   (7 r   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
TestViewc                 C   sn   t dt jd }}dtfdtfg}t jtt|||d}t||g|dd}d|j	d< ||||f| _
d S )	NrC   r    r!   r6   )g      "g     Xru   )FTr   )r;   rQ   rR   rS   rT   r<   r   r   r   r&   rM   )r/   r    r!   rL   arrrk   r2   r2   r3   setup_method_  s    
zTestView.setup_methodc                 C   s@   | j \}}}}| }tt|t t|| t|j|j d S )N)rM   r*   r   r-   r   r   r,   )r/   rk   r    r!   r   testr2   r2   r3   test_view_by_itselfh  s
    
zTestView.test_view_by_itselfc                 C   sd   | j \}}}}tdf}||}tt|tj t|tj	t
t||td t|d tjk d S )Nr   r6   )r   r   )rM   rT   r*   r   r-   rH   ZMaskedArrayr   r;   r<   r   r   r   )r/   rk   r    r!   r   ntyper   r2   r2   r3   test_view_simple_dtypeo  s    
zTestView.test_view_simple_dtypec                 C   s~   | j \}}}}dtfdtfg}||}tt|t t||| t|d d tk t|j	t
	| t|jd k d S )NABr   )rM   rT   r*   r   r-   r   r   r   r   r'   r;   Z_fill_value)r/   rk   r    r!   r   Zalttyper   r2   r2   r3   test_view_flexible_typew  s    
z TestView.test_view_flexible_typeN)r   r   r   r   r   r   r   r2   r2   r2   r3   r   ]  s   	r   c                   @   s   e Zd ZejdddgdddgedZejdddgdddgedZejd	d
dgdddgddZ	defdefdgZ
eeee	ge
ddZeejeje	jfe
dZeee
fZdd Zdd Zdd Zdd Zdd ZdS )TestMRecordsImportr   r   r   r   r%   r   r   r   r   r   r   r$   r    r!   r"   )s   99999s   99999.r@   ru   r6   c           
      C   s   t jdddgdddgtd}t jdddgdddgtd}t jd	d
dgdddgdd}| j\}}}td|||fD ]\}}tt||j|j qpt jddd	gdddgt	d}	t
t|	|jd|d  d S )Nr   r   r   r   r%   r   r   r   ro   rp   rq   r$   r(   r6   )rH   r<   rK   rT   rM   r   r   r8   r,   objectr   r   r'   )
r/   rw   rx   ry   rk   nrecrm   r   l_xr2   r2   r3   test_fromarrays  s    z"TestMRecordsImport.test_fromarraysc                 C   s  | j \}}}dddg}t|dd}t|dd}t|| t|}t|j|j |jjD ]}tt||t|j| qVt|	 dd}t|jdt
fdtfd	g td
dD ] \}	}
tt||	t|j|
 qt|}t|j|j t|j|  t|j|j d S )N)r   abcg   @r   )r   Zxyg   @r   )r    g   ?r   zc1, c2, c3, c4)r~   zc1,c2,c3c1c2)c3z|S5)r   r   r   r(   )rM   recfromrecordsr   r   r   r'   r~   r8   r.   r9   rK   rT   r   rv   r,   )r/   rk   r   rz   Zpalistpampa_mrecr1   r   nr2   r2   r3   test_fromrecords  s(    
z#TestMRecordsImport.test_fromrecordsc                 C   s   | j \}}}t| |dddgd}t|j|j t|j dddg t| |dd}t|j|j t|j dddg t| ||jd}t|j|j t|j |j  t| ||j d}t|j|j t|j |j  d S )Nr   r   )r'   r&   r?   rW   T)rM   r   r9   r   r.   r   r,   )r/   rk   r   rz   r   r2   r2   r3   test_fromrecords_wmask  s    
z)TestMRecordsImport.test_fromrecords_wmaskc              
   C   s   d}t  4}t|d}|| W 5 Q R X t|ddd}W 5 Q R X tt|t t|jddddg t|j	j
ddddg t|jdddd	g d S )
Nz#
'One (S)','Two (I)','Three (F)','Four (M)','Five (-)','Six (C)'
'strings',1,1.0,'mixed column',,1
'with embedded "double quotes"',2,2.0,1.0,,1
'strings',3,3.0E5,3,,1
'strings',4,-1e-10,,,1
w,ZABCDEFG)	delimitervarnamesr   r   g    OAg|۽)r   openwriter
   r   r-   r   r   FEr,   C)r/   Zfcontentpathr   Zmrectxtr2   r2   r3   test_fromtextfile  s    z$TestMRecordsImport.test_fromtextfilec                 C   sT   | j \}}}dddgdddg }}t|tj||d}t|j| t|jj| d S )Nd      i,  r   r   rF   )rM   r   rH   r<   r   Zf3r,   )r/   rk   r   rz   dmr2   r2   r3   test_addfield  s
    z TestMRecordsImport.test_addfieldN)r   r   r   rH   r<   rK   rw   rT   rx   ry   rz   r   rk   recfromarraysr.   r   rM   r   r   r   r   r   r2   r2   r2   r3   r     s"    
r   c                  C   s0   t jddgddgdtfdtfgd} | d  d S )	N)r   2)r   4)r   r   )r   r   r    r!   r%   r   )rH   Zmasked_arrayrK   r   )yr2   r2   r3   #test_record_array_with_object_field  s    r   )__doc__Znumpyr;   Znumpy.marH   r   r   r   Znumpy.testingr   Znumpy.core.recordsr   r   r   r   Znumpy.ma.mrecordsr   r	   r
   r   Znumpy.ma.testutilsr   r   r   Znumpy.compatr   r   r   r   r   r2   r2   r2   r3   <module>   s      D&c