ó 2ÄÈ[c @`sjdZddlmZmZmZddlZddlZddlZddl j Z ddlm Z m Z ddl m Z ddlmZddlmZddlmZejdd krÑdd lmZnej jjZd d d dddddddddg Zd„Zd„Zd„Zd„Zd„Zed„Z ed„Z!ddd„Z#d„Z$d „Z%de&d!„Z'e&ed"„Z(dd#„Z)d$eeed%„Z*e&ed&„Z+e&ed'„Z,d(„Z-d)„Z.dd$e&ed*„Z/dd+„Z0eed,„Z1de&eed-„Z2de&ed.„Z3d/d0d1de&ed2„Z4d/d0d1dd3„Z5dS(4sÆ Collection of utilities to manipulate structured arrays. Most of these functions were initially implemented by John Hunter for matplotlib. They have been rewritten and extended for convenience. i(tdivisiontabsolute_importtprint_functionN(tndarraytrecarray(t MaskedArray(t MaskedRecords(t_is_string_like(t basestringi(tzipt append_fieldst drop_fieldstfind_duplicatestget_fieldstructuretjoin_byt merge_arraystrec_append_fieldstrec_drop_fieldstrec_jointrecursive_fill_fieldst rename_fieldst stack_arrayscC`sz|j}xj|jD]_}y||}Wntk r=qnX|jjr^t|||ƒq|||t|ƒ*qW|S(sl Fills fields from output with fields from input, with support for nested structures. Parameters ---------- input : ndarray Input array. output : ndarray Output array. Notes ----- * `output` should be at least the same size as `input` Examples -------- >>> from numpy.lib import recfunctions as rfn >>> a = np.array([(1, 10.), (2, 20.)], dtype=[('A', int), ('B', float)]) >>> b = np.zeros((3,), dtype=a.dtype) >>> rfn.recursive_fill_fields(a, b) array([(1, 10.0), (2, 20.0), (0, 0.0)], dtype=[('A', '\}}t|ƒdkr`|n |d|f|df^q<SdS(sH Produce a list of name/dtype pairs corresponding to the dtype fields Similar to dtype.descr, but the second item of each tuple is a dtype, not a string. As a result, this handles subarray dtypes Can be passed to the dtype constructor to reconstruct the dtype, noting that this (deliberately) discards field offsets. Examples -------- >>> dt = np.dtype([(('a', 'A'), int), ('b', float, 3)]) >>> dt.descr [(('a', 'A'), '>> get_fieldspec(dt) [(('a', 'A'), dtype('int32')), ('b', dtype(('`siiN(RtNoneR(RR R"tf((Rs5/tmp/pip-build-fiC0ax/numpy/numpy/lib/recfunctions.pyt get_fieldspecIs  cC`srg}|j}xP|D]H}||}|jrQ|j|tt|ƒƒfƒq|j|ƒqWt|ƒpqdS(sù Returns the field names of the input datatype as a tuple. Parameters ---------- adtype : dtype Input datatype Examples -------- >>> from numpy.lib import recfunctions as rfn >>> rfn.get_names(np.empty((1,), dtype=int)) is None True >>> rfn.get_names(np.empty((1,), dtype=[('A',int), ('B', float)])) ('A', 'B') >>> adtype = np.dtype([('a', int), ('b', [('ba', int), ('bb', int)])]) >>> rfn.get_names(adtype) ('a', ('b', ('ba', 'bb'))) N(Rtappendttuplet get_namesR#(tadtypet listnamesRR"R((s5/tmp/pip-build-fiC0ax/numpy/numpy/lib/recfunctions.pyR(hs    "cC`sfg}|j}xD|D]<}|j|ƒ||}|jr|jt|ƒƒqqWt|ƒpedS(s2 Returns the field names of the input datatype as a tuple. Nested structure are flattend beforehand. Parameters ---------- adtype : dtype Input datatype Examples -------- >>> from numpy.lib import recfunctions as rfn >>> rfn.get_names_flat(np.empty((1,), dtype=int)) is None True >>> rfn.get_names_flat(np.empty((1,), dtype=[('A',int), ('B', float)])) ('A', 'B') >>> adtype = np.dtype([('a', int), ('b', [('ba', int), ('bb', int)])]) >>> rfn.get_names_flat(adtype) ('a', 'b', 'ba', 'bb') N(RR&textendtget_names_flatR'R#(R)R*RR"R((s5/tmp/pip-build-fiC0ax/numpy/numpy/lib/recfunctions.pyR,‡s     cC`sŒ|j}|dkr"d|ffSg}xS|D]K}|j|\}}|jrg|jt|ƒƒq/|j||fƒq/Wt|ƒSdS(sD Flatten a structured data-type description. Examples -------- >>> from numpy.lib import recfunctions as rfn >>> ndtype = np.dtype([('a', '>> rfn.flatten_descr(ndtype) (('a', dtype('int32')), ('ba', dtype('float64')), ('bb', dtype('int32'))) RN(RR#R R+t flatten_descrR&R'(tndtypeRtdescrRttypt_((s5/tmp/pip-build-fiC0ax/numpy/numpy/lib/recfunctions.pyR-¦s     cC`s¤g}|r6xˆ|D]}|jt|jƒƒqWnax^|D]V}|j}|jr€t|jƒdkr€|jt|ƒƒq=|jd|fƒq=Wtj|ƒS(NiR(R+R-RRRR%R&tnp(t seqarraystflattenRtaR((s5/tmp/pip-build-fiC0ax/numpy/numpy/lib/recfunctions.pyt zip_dtypeÀs   cC`st|d|ƒjS(sî Combine the dtype description of a series of arrays. Parameters ---------- seqarrays : sequence of arrays Sequence of arrays flatten : {boolean}, optional Whether to collapse nested descriptions. R4(R6R/(R3R4((s5/tmp/pip-build-fiC0ax/numpy/numpy/lib/recfunctions.pyt zip_descrÐs cC`së|dkri}n|j}xÀ|D]¸}||}|jrz|rT|g||>> from numpy.lib import recfunctions as rfn >>> ndtype = np.dtype([('A', int), ... ('B', [('BA', int), ... ('BB', [('BBA', int), ('BBB', int)])])]) >>> rfn.get_fieldstructure(ndtype) ... # XXX: possible regression, order of BBA and BBB is swapped {'A': [], 'B': [], 'BA': ['B'], 'BB': ['B'], 'BBA': ['B', 'BB'], 'BBB': ['B', 'BB']} N(R#RtupdateR tgetR&(R)tlastnametparentsRR"RR1t lastparent((s5/tmp/pip-build-fiC0ax/numpy/numpy/lib/recfunctions.pyR Þs"       + cc`sQxJ|D]B}t|tjƒrDx'tt|ƒƒD] }|Vq2Wq|VqWdS(su Returns an iterator of concatenated fields from a sequence of arrays, collapsing any nested structure. N(t isinstanceR2tvoidt_izip_fields_flatR'(titerabletelementR$((s5/tmp/pip-build-fiC0ax/numpy/numpy/lib/recfunctions.pyR? s   cc`s¡xš|D]’}t|dƒrKt|tƒ rKxjt|ƒD] }|Vq9Wqt|tjƒr”tt|ƒƒdkr”x!t|ƒD] }|Vq‚Wq|VqWdS(sP Returns an iterator of concatenated fields from a sequence of arrays. t__iter__iN(thasattrR=Rt _izip_fieldsR2R>RR'(R@RAR$((s5/tmp/pip-build-fiC0ax/numpy/numpy/lib/recfunctions.pyRDs  * cc`so|rt}nt}tjddkr4tj}n tj}x+|d||ŒD]}t||ƒƒVqPWdS(s* Returns an iterator of concatenated items from a sequence of arrays. Parameters ---------- seqarrays : sequence of arrays Sequence of arrays. fill_value : {None, integer} Value used to pad shorter iterables. flatten : {True, False}, Whether to iit fillvalueN(R?RDtsyst version_infot itertoolst zip_longestt izip_longestR'(R3t fill_valueR4tzipfuncRIttup((s5/tmp/pip-build-fiC0ax/numpy/numpy/lib/recfunctions.pyt izip_records,s   cC`sdt|tƒst}n|r9|r`|jtƒ}q`n'tj|ƒ}|r`|jtƒ}n|S(s€ Private function: return a recarray, a ndarray, a MaskedArray or a MaskedRecords depending on the input parameters (R=RtFalsetviewRtmatfilledR(Rtusemaskt asrecarray((s5/tmp/pip-build-fiC0ax/numpy/numpy/lib/recfunctions.pyt _fix_outputIs cC`s{|jj}|j|j|j}}}xK|p5ijƒD]7\}}||kr<|||<|||||>> from numpy.lib import recfunctions as rfn >>> rfn.merge_arrays((np.array([1, 2]), np.array([10., 20., 30.]))) masked_array(data = [(1, 10.0) (2, 20.0) (--, 30.0)], mask = [(False, False) (False, False) (True, False)], fill_value = (999999, 1e+20), dtype = [('f0', '>> rfn.merge_arrays((np.array([1, 2]), np.array([10., 20., 30.])), ... usemask=False) array([(1, 10.0), (2, 20.0), (-1, 30.0)], dtype=[('f0', '>> rfn.merge_arrays((np.array([1, 2]).view([('a', int)]), ... np.array([10., 20., 30.])), ... usemask=False, asrecarray=True) rec.array([(1, 10.0), (2, 20.0), (-1, 30.0)], dtype=[('a', '¶stndmintcountRW(iN( RR2t asanyarrayR=RR>RRR6tTruetravelRRRRPR'tmaxR t __array__RQt getmaskarrayt_check_fill_valuetitemtarraytonesR#R&RHtchainRNtfromitertlist(R3RKR4RSRTtseqdtypetseqtypet_mtsizest maxlengthRtseqdatatseqmaskR5tnt nbmissingRVRWtfvaltfmskR((s5/tmp/pip-build-fiC0ax/numpy/numpy/lib/recfunctions.pyRhsz2  "     "    $! $$c`sŒt|ƒr|g}n t|ƒ}‡fd†‰ˆ|j|ƒ}|sOdStj|jd|ƒ}t||ƒ}t|d|d|ƒS(sv Return a new array with fields in `drop_names` dropped. Nested fields are supported. Parameters ---------- base : array Input array drop_names : string or sequence String or sequence of strings corresponding to the names of the fields to drop. usemask : {False, True}, optional Whether to return a masked array or not. asrecarray : string or sequence, optional Whether to return a recarray or a mrecarray (`asrecarray=True`) or a plain ndarray or masked array with flexible dtype. The default is False. Examples -------- >>> from numpy.lib import recfunctions as rfn >>> a = np.array([(1, (2, 3.0)), (4, (5, 6.0))], ... dtype=[('a', int), ('b', [('ba', float), ('bb', int)])]) >>> rfn.drop_fields(a, 'a') array([((2.0, 3),), ((5.0, 6),)], dtype=[('b', [('ba', '>> rfn.drop_fields(a, 'ba') array([(1, (3,)), (4, (6,))], dtype=[('a', '>> rfn.drop_fields(a, ['ba', 'bb']) array([(1,), (4,)], dtype=[('a', '>> from numpy.lib import recfunctions as rfn >>> a = np.array([(1, (2, [3.0, 30.])), (4, (5, [6.0, 60.]))], ... dtype=[('a', int),('b', [('ba', float), ('bb', (float, 2))])]) >>> rfn.rename_fields(a, {'a':'A', 'bb':'BB'}) array([(1, (2.0, [3.0, 30.0])), (4, (5.0, [6.0, 60.0]))], dtype=[('A', '>> def print_offsets(d): ... print("offsets:", [d.fields[name][1] for name in d.names]) ... print("itemsize:", d.itemsize) ... >>> dt = np.dtype('u1,i4,f4', align=True) >>> dt dtype({'names':['f0','f1','f2'], 'formats':['u1','>> print_offsets(dt) offsets: [0, 4, 8] itemsize: 16 >>> packed_dt = repack_fields(dt) >>> packed_dt dtype([('f0', 'u1'), ('f1', '>> print_offsets(packed_dt) offsets: [0, 1, 5] itemsize: 13 taligntrecurseR„iiiN( R=R2Rt repack_fieldstastypeRORR#R RbRR&R](R5R‹RŒtdtt fieldinfoR"RMtfmt((s5/tmp/pip-build-fiC0ax/numpy/numpy/lib/recfunctions.pyRßs 8  cC`s×t|tƒr|St|ƒdkr-|dSg|D]}tj|ƒjƒ^q4}g|D]}t|ƒ^q\}g|D]}|j^q{}g|D]} | j^q—} |d} t| ƒ} g| D]\} } | ^qÉ}xÄ|dD]¸}x¯t|ƒD]¡\}}||kr:| j ||fƒ|j |ƒqÿ|j |ƒ}| |\}}|r{|t ||ƒf| |>> from numpy.lib import recfunctions as rfn >>> x = np.array([1, 2,]) >>> rfn.stack_arrays(x) is x True >>> z = np.array([('A', 1), ('B', 2)], dtype=[('A', '|S3'), ('B', float)]) >>> zz = np.array([('a', 10., 100.), ('b', 20., 200.), ('c', 30., 300.)], ... dtype=[('A', '|S3'), ('B', float), ('C', float)]) >>> test = rfn.stack_arrays((z,zz)) >>> test masked_array(data = [('A', 1.0, --) ('B', 2.0, --) ('a', 10.0, 100.0) ('b', 20.0, 200.0) ('c', 30.0, 300.0)], mask = [(False, False, True) (False, False, True) (False, False, False) (False, False, False) (False, False, False)], fill_value = ('N/A', 1e+20, 1e+20), dtype = [('A', '|S3'), ('B', ' '%s'iÿÿÿÿsf%iRSRTN(R=RRR2RaRcRRR%R&tindexRdt TypeErrorRQt concatenateR‡tsumtcumsumtr_R R#RUR\(tarraysRYRSRTt autoconvertR5R3tnrecordsR.RŠtfldnamestdtype_ltnewdescrRuRtdtype_ntfnametfdtypetnameidxR1tcdtypeRtoffsettseentitjR"((s5/tmp/pip-build-fiC0ax/numpy/numpy/lib/recfunctions.pyR.sL'(    0    c C`s tj|ƒjƒ}t|jƒ}|}|r\x||D]}||}q;W||}n|jƒ}||}|jƒ} | d | dk} |r²|j} t| | d>> from numpy.lib import recfunctions as rfn >>> ndtype = [('a', int)] >>> a = np.ma.array([1, 1, 1, 2, 2, 3, 3], ... mask=[0, 0, 1, 0, 0, 0, 1]).view(ndtype) >>> rfn.find_duplicates(a, ignoremask=True, return_index=True) ... # XXX: judging by the output, the ignoremask flag has no effect iÿÿÿÿiN( R2RaRcR RtargsortRRt recordmaskROR”( R5tkeyt ignoremaskt return_indexR R~R$tsortidxt sortedbaset sorteddatatflagt sortedmaskt duplicates((s5/tmp/pip-build-fiC0ax/numpy/numpy/lib/recfunctions.pyR „s(     tinnert1t2c .`s2|dkrtd|ƒ‚ntˆtƒr:ˆf‰nttˆƒƒtˆƒkrt‡fd†tˆƒDƒƒ} td| ƒ‚nxXˆD]P} | |jjkr¿td| ƒ‚n| |jjkr”td| ƒ‚q”q”W|j ƒ}|j ƒ}t|ƒ} |jj|jj} } t| ƒt| ƒ@tˆƒ}|rw|pT| rwd }|d 7}t|ƒ‚ng| D]}|ˆkr~|^q~}t ||ƒ}t ||ƒ}t j ||fƒ}|j d ˆƒ}||}t j tg|d |d kfƒ}|d |d |d *||}||| k}||| k| }t|ƒt|ƒ}}|dkr‰d\}}nÍ|dkr||}tj |||| kfƒ}tj |||| k| fƒ}t|ƒ|t|ƒ|}}nP|dkrV||}tj |||| kfƒ}t|ƒ|d}}n||||}} t|jƒ}!x?t|jƒD].\}"}#|"ˆkrŠ|!j|"|#fƒqŠqŠWxÐt|jƒD]¿\}"}#td„|!Dƒƒ}$y|$j|"ƒ}%Wn$tk r'|!j|"|#fƒqÌX|!|%\}&}'|"ˆkr`|"t|#|'ƒf|!|%øssduplicate join key %rsr1 does not have key field %rsr2 does not have key field %rs8r1 and r2 contain common names, r1postfix and r2postfix scan't both be emptytorderiiÿÿÿÿics`s|]\}}|VqdS(N((R!R"R((s5/tmp/pip-build-fiC0ax/numpy/numpy/lib/recfunctions.pys <sRRSRT(R²RµR¶(ii(RµR¶(RR=RRR{tnextt enumerateRRRcR€RQR”R§ROR2R%R&RmR’RdR‡tsorttdictRUR\(.R©tr1tr2tjointypet r1postfixt r2postfixRYRSRTtdupR"tnb1tr1namestr2namest collisionsR‰Rutkey1tr1ktr2ktauxtidx_sorttflag_intidx_intidx_1tidx_2tr1cmntr2cmntr1spctr2spctidx_outts1ts2R.RŸR RR¡R1R¢tcmnRR$tselectedRtkwargs((R©s5/tmp/pip-build-fiC0ax/numpy/numpy/lib/recfunctions.pyRºsª3   "      % &    #$      "    +     +  c C`s@td|d|d|d|dtdtƒ}t||||S(s¨ Join arrays `r1` and `r2` on keys. Alternative to join_by, that always returns a np.recarray. See Also -------- join_by : equivalent function R¿RÀRÁRYRSRT(R¼RORbR(R©R½R¾R¿RÀRÁRYRÙ((s5/tmp/pip-build-fiC0ax/numpy/numpy/lib/recfunctions.pyRms (6t__doc__t __future__RRRRFRHtnumpyR2tnumpy.maRQRRRtnumpy.ma.mrecordsRtnumpy.lib._iotoolsRt numpy.compatRRGtfuture_builtinsR tcoreRgt__all__RR%R(R,R-ROR6R7R#R R?RDRbRNRUR\RR R€RRR RRRR RR(((s5/tmp/pip-build-fiC0ax/numpy/numpy/lib/recfunctions.pyts^       '      /   ˆ@  'E !O  U6 ²