o 6aM@sdZddlZddlmZddlmZddlmZmZddlm Z ddl m Z m ZddlmZmZm Z mZm Z mZddlmZmZmZdd lmZGd d d ZGd d d ZGdddZddZdS)z^Tests suite for mrecords. :author: Pierre Gerard-Marchant :contact: pierregm_at_uga_dot_edu N)recarray)maskednomask)temppath) fromrecords fromarrays) MaskedRecords mrecarrayr fromtextfileraddfield)assert_ assert_equalassert_equal_records)picklec@seZdZgdZgdZgdZdefdefdgZgdZ e j e e eeee edZd d Zd d Zd dZddZddZddZddZddZddZddZddZdd Zd!d"Zd#d$Zd%d&Zd'S)( TestMRecords皙?皙@ffffff @皙@@onetwothreefourfiveabc|S8rrrrrmaskdtypecCs|j}|t}t|j|jt|j|jtt|j t t|j |j t dD] }t||||q*t|t|dS)Nr#r$r&) baseviewr r recordmaskr_maskr isinstance_datar)selfr-mbasefieldr6>/usr/lib/python3/dist-packages/numpy/ma/tests/test_mrecords.py test_byview$s zTestMRecords.test_byviewcCs|j}|t}dD]}tt||||t||||q |d}tt|tt|j|jt| dt|j t t|j dt|d|dd|d}tt|tt|j|jt| dt|j dt|j d t|d|ddt|dtu|dd }tt|tt|j|jt|j dd gt|jtjdd g|j jd t||dd tdD]}tt|||dd |qdS) Nr,r)rrr)FFFr#)NNNT)TTTrrr+)r-copyr.r r getattrr r1r+tolistr/rr0itemrrr*nparray)r3r-r4r5 mbase_first mbase_lastmbase_slr6r6r7test_get0sF     zTestMRecords.test_getcCs|j}|t}|}d|_d|jjdd<t|djgdt|djgdd|_t|djdgdtt |ddgdt|j dgdt|j t jgd td t|_t|jjdgdt|jj dgdtt |d dgdtt |d d gdt|j t jgd td |t}d|jdd<t|jgdt|jjgdt|jdd<t|j|dt|jjgdddtfg}t jgd|d }|t}t jgdgdd}||d<t|j|t|jjgddS)N)i?Bg@xDN/Arr#)rrrrrr(rrF)rrrrrrrFrFrGr:r&N/A)rrrrGrIrIrGrrrrrr)rrrrrr$rrrrr)alphaz|S1num))r#r)r$r)r&r) rrrr*)r-r;r.r fill_valuer#r2r r0ma getmaskarrayr/r=r?r@boolrr&r*getdatar$intrrM)r3r-r4ndtypedatardatavalr6r6r7test_set_fieldsXsP         zTestMRecords.test_set_fieldscCs|j}|t}t|dd<t|jgdt|jjgdtt dt j dgdt fdtfgd}t|dd<t|jgdt|jjgd dS) Nr#rrKrr$r:)rrrrr)rrrrr)r-r;r.r rr r#r0rr?arangerandomrandrXfloatr3r-r4r6r6r7test_set_fields_masks    z!TestMRecords.test_set_fields_maskcCs|j}|t}t|_tt|ddgdt|dj |dj t|dj |dj t|j t j dgdt dt|_tt|ddgdt|j t j d gdt ddS) Nr$rrr#r&rrrr:rrF)r-r;r.r rr*r rTrUr0r=r?r@rVrrcr6r6r7 test_set_masks    zTestMRecords.test_set_maskcCs|j}|t}gd|_t|jjgdt|jjgdt|jjgdgd|_t|jjgdt|jjgdt|jjgddS)N)rrrrrrrrrr) r-r;r.r r*r r#r$r&rcr6r6r7test_set_mask_fromarrays    z$TestMRecords.test_set_mask_fromarraycCs|jt}tjgddtfdtfdtfgd}||_t|j jgdt|j jgdt|j jgdd|_||_ t|j jgdt|j jgdt|j jgddS) N)rrrrirrrrjrFr#r$r&r:)rrrrr)rrrrrF) r-r;r.r r?r@rVr*r r#r$r& fieldmask)r3r4nmaskr6r6r7test_set_mask_fromfieldssz%TestMRecords.test_set_mask_fromfieldscCs^|j}|t}t|d<t|jtj gdt dt|j gd|t}d|dd<t|j j gdt|j jgdt|jj gd t|jjgdt|jj gd t|jjgd|t}t|dd<t|j j gd t|j jgd t|jj gd t|jjgd t|jj gdt|jjgd dS)Nr^)rFrerFrerer:rKrrrr)rrrrrrg)@rorrr)5rpr r!r"r)rrrrrrr)r-r;r.r rr r0r=r?r@rVr/r#r2r$r&rcr6r6r7test_set_elementss<    zTestMRecords.test_set_elementsc Cs|j}|t}|zAd|dd<t|jjgdt|jjgdt|j jgdt|jj gdt|jj |jj t|jj |j j Wt dt yXYnt y_wz d|dd<Wtd t tfywYdSw) Nrnr^rJ)rrrrr)rrr rpr"r(z)Flexible hard masks should be supported !rz.Should have expected a readable buffer object!)r-r;r.r harden_maskr r#r2r$r&r0NotImplementedErrorAssertionError Exception TypeErrorrcr6r6r7test_setslices_hardmasks2     z$TestMRecords.test_setslices_hardmaskcCs|j}|t}|t|jt|_t |j |j | t|j t|_t |j t |j|jtt |dj tut|dj |dj dS)Nr$r#)r-r;r.r rrr _hardmaskrr*rr0 soften_maskrTmake_mask_noneshaper+ make_maskr rcr6r6r7 test_hardmask s    zTestMRecords.test_hardmaskcCs~|j}|t}tdtjdD]*}tj||d}t|}t |j |j t |j |j t |j |j t |j |j qdS)Nrr)protocol)r-r;r.r rangerHIGHEST_PROTOCOLdumpsloadsr r+rr2r0)r3r-mrecproto_mrec_r6r6r7 test_picklings   zTestMRecords.test_picklingcCstjgdgdtd}tjgdgdtd}tjgdgddd}dtfdtfd g}t|||g|d d }|}t|dtjd td t|dtjdtd t|dtjddd dS)NrrrrIr)rrronetwothreer'r#r$r%韆i@rEr+rS)rrrr:)rrrr&)rrrE)rTr@rXrbrfilledr r?)r3_a_b_cddtyper mrecfilledr6r6r7 test_filled's  zTestMRecords.test_filledcCstjgdgdtd}tjgdgdtd}tjgdgddd}dtfd tfd g}t|||g|d d }t|gd dS)NrrIr)rrrQr'r#r$r%rr))rrN)rrr)NNr )rTr@rXrbrr r=)r3rrrrrr6r6r7 test_tolist6s zTestMRecords.test_tolistcCs0tdtdd}d|dd<t|ddddS)Nrr-)formatsnamesrNr)r rbr )r3xr6r6r7test_withnamesCs zTestMRecords.test_withnamescCstddtfddtfgd}t|d<t|ddtddgd}t|d<t|dtjd |j dtd d d}t|d<d |d<|dt |dtjd d g|j ddS)Nri)sr'fr:r)r1g?)f0zr?r@r+r)r3easysolomultr6r6r7test_exotic_formatsIs"    z TestMRecords.test_exotic_formatsN)__name__ __module__ __qualname__ilistflistslistrXrbrr*rTr@listzipr-r8rDr]rdrfrhrmrqrwr}rrrrrr6r6r6r7rs, (7   rc@s,eZdZddZddZddZddZd S) TestViewcCsntdtjd}}dtfdtfg}tjtt|||d}t||g|dd}d|j d<||||f|_ dS) NrNr#r$r:)g"gXr)FTr) r?r_r`rarbr@rrrr*rZ)r3r#r$rYarrrr6r6r7setup_s  zTestView.setupcCs@|j\}}}}|}tt|tt||t|j|jdS)N)rZr.r r1rrr0)r3rr#r$rtestr6r6r7test_view_by_itselfhs  zTestView.test_view_by_itselfcCsd|j\}}}}tdf}||}tt|tjt|tj t t ||tdt|dtj udS)Nrr:)rr) rZrbr.r r1rT MaskedArrayr r?r@rrr)r3rr#r$rntyperr6r6r7test_view_simple_dtypeos  zTestView.test_view_simple_dtypecCs~|j\}}}}dtfdtfg}||}tt|tt|||t|ddtut|j t |t|j dudS)NABr) rZrbr.r r1rrrr r+r? _fill_value)r3rr#r$ralttyperr6r6r7test_view_flexible_typews z TestView.test_view_flexible_typeN)rrrrrrrr6r6r6r7r]s   rc@seZdZejgdgdedZejgdgdedZejgdgdddZ defdefd gZ e eee ge d d Z e ejeje jfe d Ze ee fZd dZddZddZddZddZdS)TestMRecordsImportrrIr)r)rrr r'r#r$r%)s99999s99999.rHrr:c Cstjgdgdtd}tjgdgdtd}tjgdgddd}|j\}}}td|||fD]\}}tt||j|jq2tjgdgd t d} t t | |j d |d dS) NrrIr)rrr'r,)rrrrQr:r) rTr@rXrbrZrr r<r0objectrrr+) r3rrrrnrecrrl_xr6r6r7test_fromarrayss z"TestMRecordsImport.test_fromarraysc Cs|j\}}}gd}t|dd}t|dd}t||t|}t|j|j|jjD]}tt||t|j|q*t| dd}t|jdt fdt fdgt dd D]\} } tt|| t|j| qSt|}t|j|jt|j| t|j|jdS) N))rabcg @r)rxyg@r)r g?rzc1, c2, c3, c4)rzc1,c2,c3c1c2)c3z|S5)rrrr,)rZrecfromrecordsrrr r+rr<r2r=rXrbrrr0) r3rrrpalistpampa_mrecr5rnr6r6r7test_fromrecordss"     z#TestMRecordsImport.test_fromrecordscCs|j\}}}t||gdd}t|j|jt|jgdt||dd}t|j|jt|jgdt|||jd}t|j|jt|j|jt|||jd}t|j|jt|j|jdS)Nri)r+r*)rFrerFT)rerere)rZrr=rr2r r0)r3rrrrr6r6r7test_fromrecords_wmasks  z)TestMRecordsImport.test_fromrecords_wmaskc Csd}t)}t|d }||Wdn1swYt|ddd}Wdn1s1wYtt|tt|jgdt|j j gdt|j gddS)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) delimitorvarnames)rrrr)rrgOAg|۽) ropenwriter r r1rr FEr0C)r3fcontentpathrmrectxtr6r6r7test_fromtextfiles  z$TestMRecordsImport.test_fromtextfilecCsP|j\}}}gdgd}}t|tj||d}t|j|t|jj|dS)N)di,rQrR)rZr rTr@r f3r0)r3rrrdmr6r6r7 test_addfields  z TestMRecordsImport.test_addfieldN)rrrrTr@rXrrbrrrrr recfromarraysr2rrZrrrrrr6r6r6r7rs"     rcCs0tjddgddgdtfdtfgd}|ddS) N)r2)r4)rr)rrr#r$r)r)rT masked_arrayrXr)yr6r6r7#test_record_array_with_object_fields  r)__doc__numpyr?numpy.marTrrr numpy.testingrnumpy.core.recordsrrrrnumpy.ma.mrecordsrr r r numpy.ma.testutilsr r r numpy.compatrrrrrr6r6r6r7s      D& c