o
    1ή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g dZg dZg dZdefdefdgZg d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#d$ Zd%d& Zd'S )(TestMRecords               皙?皙@ffffff
@皙@      @   one   two   three   four   fiveabc|S8r   r   r   r   r   maskdtypec                 C   s   | j }|t}t|j|j t|j|j tt|j	t
 t|j	|j	t
 dD ]}t|| ||  q*t|t| d S )Nr#   r$   r&   )baseviewr	   r   
recordmaskr   _maskr   
isinstance_datar   )selfr-   mbasefield r6   C/tmp/pip-target-vg8gfxp4/lib/python/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u  |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   r1   r+   tolistr/   r   r0   itemr   r   r*   nparray)r3   r-   r4   r5   mbase_first
mbase_lastmbase_slr6   r6   r7   test_get0   sF   

zTestMRecords.test_getc                 C   s  | j  }|t}| }d|_d|jjd d < t|d jg d t|d jg d d|_t|d jdgd  tt	
|d dgd  t|jdgd  t|j tjg d	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g dtd
 |t }d|jdd < t|jg d t|jjg d t|jdd < t|j|d  t|jjg d ddtfg}t	jg d|d
}|t}t	jg dg dd}||d< t|j| t|jjg d d S )N)i?B g@xDN/Ar   r#   )r   r   r   r   r   r(   r   r   F)r   r   r   r   r   r   rF   rF   rG   r:   r&      N/A)r   r   r   rG   rI   rI   rG   r   r   r   r   r   r   )r   r   r   r   r   r$   r   r   r   r   r   )alphaz|S1num))r#   r   )r$   r   )r&   r   )
         r   r   r   r*   )r-   r;   r.   r	   
fill_valuer#   r2   r   r0   magetmaskarrayr/   r=   r?   r@   boolr   r&   r*   getdatar$   intr   rM   )r3   r-   r4   ndtypedatardatavalr6   r6   r7   test_set_fieldsX   sP   






zTestMRecords.test_set_fieldsc                 C   s   | j  }|t}t|d d< t|jg d t|jjg d tt	
dt	jdgdtfdtfgd}t|d d< t|jg d t|jjg d	 d S )
Nr#   r   rK   r   r$   r:   )r   r   r   r   r   )r   r   r   r   r   )r-   r;   r.   r	   r   r   r#   r0   r   r?   arangerandomrandrX   floatr3   r-   r4   r6   r6   r7   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   r:   r   rF   )r-   r;   r.   r	   r   r*   r   rT   rU   r0   r=   r?   r@   rV   r   rc   r6   r6   r7   test_set_mask   s   



zTestMRecords.test_set_maskc                 C   s   | j  }|t}g d|_t|jjg d t|jjg d t|jjg d g d|_t|jjg d t|jjg d t|jjg d d S )N)r   r   r   r   r   r   r   r   r   r   )	r-   r;   r.   r	   r*   r   r#   r$   r&   rc   r6   r6   r7   test_set_mask_fromarray   s   



z$TestMRecords.test_set_mask_fromarrayc                 C   s   | j  t}tjg ddtfdtfdtfgd}||_t|j	jg d t|j
jg d t|jjg d d|_||_t|j	jg d t|j
jg d t|jjg d d S )	N)r   r   r   ri   r   r   r   rj   rF   r#   r$   r&   r:   )r   r   r   r   r   )r   r   r   r   r   F)r-   r;   r.   r	   r?   r@   rV   r*   r   r#   r$   r&   	fieldmask)r3   r4   nmaskr6   r6   r7   test_set_mask_fromfields   s   z%TestMRecords.test_set_mask_fromfieldsc                 C   s^  | j  }|t }t|d< t|j tj	g dt
d t|jg d |t }d|d d< t|jjg d t|jjg d t|jjg d	 t|jjg d t|jjg d
 t|jjg d |t }t|d d< t|jjg d t|jjg d t|jjg d t|jjg d t|jjg d t|jjg d d S )Nr^   )rF   re   rF   re   re   r:   rK   r   r   r   r   )r   r   r   r   r   rg   )      @ro   r   r   r   )   5rp   r    r!   r"   r   )r   r   r   r   r   r   r   )r-   r;   r.   r	   r   r   r0   r=   r?   r@   rV   r/   r#   r2   r$   r&   rc   r6   r6   r7   test_set_elements   s<   

zTestMRecords.test_set_elementsc              	   C   s   | j  }|t}|  zAd|dd < t|jjg d t|jjg d t|j	jg d t|jj
g d t|jj
|jj
 t|jj
|j	j
 W td tyX   Y n ty_    w zd|dd < W td	 ttfyw   Y d S w )
Nrn   r^   rJ   )r   r   r   r   r   )r   r   r    rp   r"   r(   z)Flexible hard masks should be supported !r   z.Should have expected a readable buffer object!)r-   r;   r.   r	   harden_maskr   r#   r2   r$   r&   r0   NotImplementedErrorAssertionError	Exception	TypeErrorrc   r6   r6   r7   test_setslices_hardmask   s2   

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u  t|d j
|d j
 d S )Nr$   r#   )r-   r;   r.   r	   rr   r   	_hardmaskr   r*   r   r0   soften_maskrT   make_mask_noneshaper+   	make_maskr   rc   r6   r6   r7   test_hardmask
  s   


zTestMRecords.test_hardmaskc                 C   s~   | j  }|t}tdtjd D ]*}tj||d}t|}t	|j
|j
 t|j|j t	|j|j t|j|j qd S )Nr   r   )protocol)r-   r;   r.   r	   ranger   HIGHEST_PROTOCOLdumpsloadsr   r+   r   r2   r0   )r3   r-   mrecproto_mrec_r6   r6   r7   test_pickling  s   


zTestMRecords.test_picklingc                 C   s   t jg dg dtd}t jg dg dtd}t jg dg d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 )Nr   r   r   rI   r)   r   r   r   onetwothreer'   r#   r$   r%   韆     i@rE   r+   rS   )r   r   r   r:   )r   r   r   r&   )r   r   rE   )rT   r@   rX   rb   r   filledr   r?   )r3   _a_b_cddtyper   
mrecfilledr6   r6   r7   test_filled'  s   zTestMRecords.test_filledc                 C   s   t jg dg dtd}t jg dg dtd}t jg dg ddd}dtfd	tfd
g}t|||g|dd}t| g d d S )Nr   rI   r)   r   r   rQ   r'   r#   r$   r%   r   r   ))r   r   N)r   r   r   )NNr    )rT   r@   rX   rb   r   r   r=   )r3   r   r   r   r   r   r6   r6   r7   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namesrN   r   )r	   rb   r   )r3   xr6   r6   r7   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'   fr:   r   )r      1g      ?)f0z<f8)r   r   )r   r   zi4, (2,3)float, floatre   rF   )r	   rX   rb   r   r   r   r>   r?   r@   r+   r   )r3   easysolomultr6   r6   r7   test_exotic_formatsI  s"   


z TestMRecords.test_exotic_formatsN)__name__
__module____qualname__ilistflistslistrX   rb   r   r*   rT   r@   listzipr-   r8   rD   r]   rd   rf   rh   rm   rq   rw   r}   r   r   r   r   r   r6   r6   r6   r7   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 )	NrN   r#   r$   r:   )g      "g     Xr   )FTr   )r?   r_   r`   ra   rb   r@   r   r   r   r*   rZ   )r3   r#   r$   rY   arrr   r6   r6   r7   setup_method_  s   
zTestView.setup_methodc                 C   s@   | j \}}}}| }tt|t t|| t|j|j d S )N)rZ   r.   r   r1   r   r   r0   )r3   r   r#   r$   r   testr6   r6   r7   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u  d S )Nr   r:   )r   r   )rZ   rb   r.   r   r1   rT   MaskedArrayr   r?   r@   r   r   r   )r3   r   r#   r$   r   ntyper   r6   r6   r7   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u  t|j	t
	| t|jd u  d S )NABr   )rZ   rb   r.   r   r1   r   r   r   r   r+   r?   _fill_value)r3   r   r#   r$   r   alttyper   r6   r6   r7   test_view_flexible_typew  s   
z TestView.test_view_flexible_typeN)r   r   r   r   r   r   r   r6   r6   r6   r7   r   ]  s
    	r   c                   @   s   e Zd Zejg dg dedZejg dg dedZejg dg d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   rI   r)   r   )r   r   r    r'   r#   r$   r%   )s   99999s   99999.rH   r   r:   c           
      C   s   t jg dg dtd}t jg dg dtd}t jg dg ddd}| j\}}}td|||fD ]\}}tt||j|j q2t jg dg d	t	d}	t
t|	|jd
|d  d S )Nr   rI   r)   r   r   r'   r,   )r   r   r   rQ   r:   r   )rT   r@   rX   rb   rZ   r   r   r<   r0   objectr   r   r+   )
r3   r   r   r   r   nrecr   r   l_xr6   r6   r7   test_fromarrays  s   z"TestMRecordsImport.test_fromarraysc                 C   s   | j \}}}g d}t|dd}t|dd}t|| t|}t|j|j |jjD ]}tt||t|j| q*t|	 dd}t|jdt
fdtfdg tdd	D ]\}	}
tt||	t|j|
 qSt|}t|j|j t|j|  t|j|j d S )
N))r   abcg   @r   )r   xyg   @r   )r    g   ?r   zc1, c2, c3, c4)r   zc1,c2,c3c1c2)c3z|S5)r   r   r   r,   )rZ   recfromrecordsr   r   r   r+   r   r<   r2   r=   rX   rb   r   r   r0   )r3   r   r   r   palistpampa_mrecr5   r   nr6   r6   r7   test_fromrecords  s"   
z#TestMRecordsImport.test_fromrecordsc                 C   s   | j \}}}t| |g dd}t|j|j t|j g d t| |dd}t|j|j t|j g d t| ||jd}t|j|j t|j |j  t| ||j d}t|j|j t|j |j  d S )Nri   )r+   r*   )rF   re   rF   T)re   re   re   )rZ   r   r=   r   r2   r   r0   )r3   r   r   r   r   r6   r6   r7   test_fromrecords_wmask  s   
z)TestMRecordsImport.test_fromrecords_wmaskc              	   C   s   d}t  )}t|d}|| W d    n1 sw   Y  t|ddd}W d    n1 s1w   Y  tt|t t|jg d t|j	j
g d t|jg d 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,ABCDEFG)	delimitervarnames)r   r   r   r   )r   r   g    OAg|۽)r   openwriter
   r   r1   r   r   FEr0   C)r3   fcontentpathr   mrectxtr6   r6   r7   test_fromtextfile  s   z$TestMRecordsImport.test_fromtextfilec                 C   sP   | j \}}}g dg d}}t|tj||d}t|j| t|jj| d S )N)d      i,  rQ   rR   )rZ   r   rT   r@   r   f3r0   )r3   r   r   r   dmr6   r6   r7   test_addfield  s
   z TestMRecordsImport.test_addfieldN)r   r   r   rT   r@   rX   r   rb   r   r   r   r   r   recfromarraysr2   r   rZ   r   r   r   r   r   r6   r6   r6   r7   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   )rT   masked_arrayrX   r   )yr6   r6   r7   #test_record_array_with_object_field  s   r   )__doc__numpyr?   numpy.marT   r   r   r   numpy.testingr   numpy.core.recordsr   r   r   r   numpy.ma.mrecordsr   r	   r
   r   numpy.ma.testutilsr   r   r   numpy.compatr   r   r   r   r   r6   r6   r6   r7   <module>   s       D&c