bgndZddlZddlZddlmZddlmZmZddlmZddl m Z ddl m Z ddl mZejjjZgdZd Ze ed Zd Zd Zd ZdZdAdZdAdZdBdZdZdZdCdZdDdZdEdZ dFdZ e e  dGdZ!dBdZ"e e"dDdZ#dDdZ$dZ%e e%d Z&d!Z'e e'd"Z( dFd#Z)e e) dHd$Z*dEd%Z+e e+dEd&Z,dBd'Z-e e-dId(Z.dJd)Z/d*Z0 dKd+Z1e e1dLd-Z2 dMd.Z3e e3 dNd/Z4d0Z5e e5d1Z6dEd2Z7e e7dOd3Z8d4Z9e e9d5Z: dFd6Z;e e; dPd7Z< dKd8Z=e e=dQd9Z> dRd:Z?e e? dSd>Z@ dFd?ZAe eA dTd@ZBdS)Uz 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. N)ndarrayrecarray) MaskedArray) MaskedRecords)array_function_dispatch)_is_string_like) append_fieldsapply_along_fieldsassign_fields_by_name drop_fieldsfind_duplicates flatten_descrget_fieldstructure get_namesget_names_flatjoin_by merge_arraysrec_append_fieldsrec_drop_fieldsrec_joinrecursive_fill_fields rename_fields repack_fieldsrequire_fields stack_arraysstructured_to_unstructuredunstructured_to_structuredc ||fSN)inputoutputs i/builddir/build/BUILD/cloudlinux-venv-1.0.7/venv/lib64/python3.11/site-packages/numpy/lib/recfunctions.py!_recursive_fill_fields_dispatcherr$s 6?c|j}|jD]Y} ||}n#t$rYwxYw|jjt|||?|||dt |<Z|S)aj 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', np.int64), ('B', np.float64)]) >>> b = np.zeros((3,), dtype=a.dtype) >>> rfn.recursive_fill_fields(a, b) array([(1, 10.), (2, 20.), (0, 0.)], dtype=[('A', '>> dt = np.dtype([(('a', 'A'), np.int64), ('b', np.double, 3)]) >>> dt.descr [(('a', 'A'), '>> _get_fieldspec(dt) [(('a', 'A'), dtype('int64')), ('b', dtype(('z!_get_fieldspec..as/EE4d+,EEEEEEr%cbg|],\}}t|dkr|n |d|f|df-S)rr*)r3r4fs r# z"_get_fieldspec..csO   aVVq[[TTqtTlAaD 9   r%r()r'r1s` r#_get_fieldspecr<JsU& {U }EEEEEEE  !    r%c g}|j}|D]X}||}|j2||tt|fC||Yt|S)aG Returns the field names of the input datatype as a tuple. Input datatype must have fields otherwise error is raised. Parameters ---------- adtype : dtype Input datatype Examples -------- >>> from numpy.lib import recfunctions as rfn >>> rfn.get_names(np.empty((1,), dtype=[('A', int)]).dtype) ('A',) >>> rfn.get_names(np.empty((1,), dtype=[('A',int), ('B', float)]).dtype) ('A', 'B') >>> adtype = np.dtype([('a', int), ('b', [('ba', int), ('bb', int)])]) >>> rfn.get_names(adtype) ('a', ('b', ('ba', 'bb'))) )r(appendtupleradtype listnamesr(r4r-s r#rris*I LE##, = $   dE)G*<*<$=$=> ? ? ? ?   T " " " "   r%cg}|j}|D]H}||||}|j"|t|It |S)a Returns the field names of the input datatype as a tuple. Input datatype must have fields otherwise error is raised. Nested structure are flattened beforehand. Parameters ---------- adtype : dtype Input datatype Examples -------- >>> from numpy.lib import recfunctions as rfn >>> rfn.get_names_flat(np.empty((1,), dtype=[('A', int)]).dtype) is None False >>> rfn.get_names_flat(np.empty((1,), dtype=[('A',int), ('B', str)]).dtype) ('A', 'B') >>> adtype = np.dtype([('a', int), ('b', [('ba', int), ('bb', int)])]) >>> rfn.get_names_flat(adtype) ('a', 'b', 'ba', 'bb') )r(r>extendrr?r@s r#rrso,I LE66, = $   ^G44 5 5 5   r%c|j}|d|ffSg}|D]S}|j|\}}|j#|t|<|||fTt |S)aD 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'))) Nr/)r(r1rDrr>r?)ndtyper(descrr,typ_s r#rrs LE }V  + +E}U+HS!y$ ]3//0000 eS\****U||r%FcVg}|r-|D])}|t|j*ne|D]b}|j}|j;t |jdkr#|t |K|d|fctj|S)Nr/)rDrr'r(r*r<r>np) seqarraysflattenr+ar-s r# _zip_dtyperPsH / 4 4A OOM!'22 3 3 3 3 4 / /AgG}(S-?-?1-D-Dw 7 78888W .... 8H  r%c.t||jS)z 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. rN)rPrG)rMrNs r# _zip_descrrSs i 1 1 1 77r%c6|i}|j}|D]}||}|j3|r|g||<ng||<|t|||Dd||gpgD}|r||n|r|g}|pg||<|S)ab Returns a dictionary with fields indexing lists of their parent fields. This function is used to simplify access to fields nested in other fields. Parameters ---------- adtype : np.dtype Input datatype lastname : optional Last processed field name (used internally during recursion). parents : dictionary Dictionary of parent fields (used interbally during recursion). Examples -------- >>> 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']} Ncg|]}|Sr r )r3rIs r#r:z&get_fieldstructure..sGGG!GGGr%)r(updatergetr>)rAlastnameparentsr(r4r- lastparents r#rrs4 LE--, = $ #!)  " NN-gtWEE F F F FGGgkk(B&?&?&E2GGGJ *!!(++++ *&\ &,"GDMM Nr%c#K|D]C}t|tjr#tt |Ed{V?|VDdS)zu Returns an iterator of concatenated fields from a sequence of arrays, collapsing any nested structure. N) isinstancerLvoid_izip_fields_flatr?iterableelements r#r^r^si  grw ' ' (w88 8 8 8 8 8 8 8 8MMMM r%c#2K|D]}t|dr+t|tst|Ed{V=t|tjr6t t|dkrt|Ed{V|VdS)zP Returns an iterator of concatenated fields from a sequence of arrays. __iter__NrK)hasattrr\str _izip_fieldsrLr]r*r?r_s r#rfrfs  GZ ( ( w,, #G,, , , , , , , , ,  ) ) c%...A.AQ.F.F#G,, , , , , , , , ,MMMMr%Tc#K|rt}nt}tj|d|iD]}t ||VdS)a* 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 fillvalueN)r^rf itertools zip_longestr?)rM fill_valuerNzipfunctups r# _izip_recordsrn-sd#$iF:FF""GGCLL!!!!!!""r%ct|tsd}|r|r|t}n0t j|}|r|t }|S)z Private function: return a recarray, a ndarray, a MaskedArray or a MaskedRecords depending on the input parameters F)r\rviewrmafilledr)r"usemask asrecarrays r# _fix_outputruEsj fk * *+  0[[//F6""  +[[**F Mr%c|jj}|j|j|j}}}|piD]\}}||vr|||<|||||< |S)zp Update the fill_value and masked data of `output` from the default given in a dictionary defaults. )r'r(datamaskrkitems)r"defaultsr(rwrxrkkvs r# _fix_defaultsr}Vso L E & V[&:K4T>r((**!!A ::JqM DGDG  Mr%c|Srr )rMrkrNrsrts r#_merge_arrays_dispatcherrds r%c t|dkrtj|d}t|ttjfr|j}|jtjd|fg}|rt|fd|krP| }|r|rt}nt}n|rt}nt}| ||S|f}n d|D}td |D}t|}t||} g} g} |rt!||D]X\} } || z }| }t%j|  }|rt)|| j}t|ttjfrlt|jdkr|d}d}n>> from numpy.lib import recfunctions as rfn >>> rfn.merge_arrays((np.array([1, 2]), np.array([10., 20., 30.]))) array([( 1, 10.), ( 2, 20.), (-1, 30.)], dtype=[('f0', '>> rfn.merge_arrays((np.array([1, 2], dtype=np.int64), ... 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', np.int64)]), ... np.array([10., 20., 30.])), ... usemask=False, asrecarray=True) rec.array([( 1, 10.), ( 2, 20.), (-1, 30.)], dtype=[('a', '.s";;;2R]2&&;;;r%c3$K|] }|jV dSr)sizer3rOs r#r5zmerge_arrays..s$,,Q!&,,,,,,r%)r'ndmin)rKr')r'count)rx)r*rLrr\rr]r'r(rPravelrrrrpr?maxzip __array__rq getmaskarray_check_fill_valueitemarrayonesr>richainrnfromiterlist)rMrkrNrsrtseqdtypeseqtypesizes maxlengthr+seqdataseqmaskrOn nbmissingrwrxfvalfmskr"s r#rris1b I!M)A,// )grw/00<? > !x"h 011H %*i\4@@@HLL!))I "*+GG)GG ""!>>w>?? ?" II<;;;; ,,),,, , ,EE I)W555HGG.+)U++ F FFQ"QI7799&&((D?1%%++--D (QW==dWbg$677?4:!++#yy{{1~#!xAG1EEE!wt4:>>> NN9?4$)1CDD E E E NN9?4$)1CDD E E E E]7G<<<=="+d()LLL#M'7$K$K$KLLNNN  0[[//F)U++ F FFQ"QI7799&&((D (QW==dWbg$677F4:!++#yy{{1~!xAG1EEE NN9?4$)1CDD E E E EU='#J#J#JKK#+9>>>  +[[**F Mr%c|fSrr )base drop_namesrsrts r#_drop_fields_dispatcherr 7Nr%ct|r|g}nt|}fd|j|}tj|j|}t ||}t|||S)a Return a new array with fields in `drop_names` dropped. Nested fields are supported. .. versionchanged:: 1.18.0 `drop_fields` returns an array with 0 fields if all fields are dropped, rather than returning ``None`` as it did previously. 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', np.int64), ('b', [('ba', np.double), ('bb', np.int64)])]) >>> rfn.drop_fields(a, 'a') array([((2., 3),), ((5., 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', ')rFrr(r+r4r-rG _drop_descrs r#rz drop_fields.._drop_descr s  1 1DTlGz!!}(# GZ883OOT5M222w0000r%rrsrt)rsetr'rLemptyshaperru)rrrsrtr+r"rs @r#r r sLz""% \ __      {4:z22H Xdj 1 1 1F "4 0 0F vw: F F FFr%cfd|D}tjj|}t|}t |||S)a Return a new array keeping only the fields in `keep_names`, and preserving the order of those fields. Parameters ---------- base : array Input array keep_names : string or sequence String or sequence of strings corresponding to the names of the fields to keep. Order of the names will be preserved. 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. c.g|]}|j|fSr r)r3rrs r#r:z _keep_fields..Is$777qDJqM"777r%rr)rLrrrru)r keep_namesrsrtr+r"s` r# _keep_fieldsr6sX&8777J777H Xdj 1 1 1F "4 0 0F vw: F F FFr%c|fSrr rrs r#_rec_drop_fields_dispatcherrOrr%c(t||ddS)zK Returns a new numpy.recarray with fields in `drop_names` dropped. FTr)r rs r#rrSs tZ4 H H HHr%c|fSrr )r namemappers r#_rename_fields_dispatcherr[rr%cZfd|j|}||S)a Rename the fields from a flexible-datatype ndarray or recarray. Nested fields are supported. Parameters ---------- base : ndarray Input array whose fields must be modified. namemapper : dictionary Dictionary mapping old field names to their new version. Examples -------- >>> 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., [ 3., 30.])), (4, (5., [ 6., 60.]))], dtype=[('A', ')rFrr+r4newnamer-_recursive_rename_fieldss r#rz/rename_fields.._recursive_rename_fieldswsL 4 4D nnT400GTlG}(66w KKL' 23333r%)r'rp)rrr+rs @r#rr_sD0     (' J??H 99X  r%c#"K|V|Ed{VdSrr )rr(rwdtypesrkrsrts r#_append_fields_dispatcherrs$ JJJOOOOOOOOOr%ct|ttfr2t|t|krd}t |nt|t r|g}|g}|'d|D}dt ||D}nt|ttfs|g}t|t|kr7t|dkr|t|z}nd}t |dt |||D}t|||}t|dkrt|d || }n|}tj tt|t|t|j t|j z }t||}t||}t||| S) a  Add new fields to an existing array. The names of the fields are given with the `names` arguments, the corresponding values with the `data` arguments. If a single field is appended, `names`, `data` and `dtypes` do not have to be lists but just values. Parameters ---------- base : array Input array to extend. names : string, sequence String or sequence of strings corresponding to the names of the new fields. data : array or sequence of arrays Array or sequence of arrays storing the fields to add to the base. dtypes : sequence of datatypes, optional Datatype or sequence of datatypes. If None, the datatypes are estimated from the `data`. fill_value : {float}, optional Filling value used to pad missing data on the shorter arrays. usemask : {False, True}, optional Whether to return a masked array or not. asrecarray : {False, True}, optional Whether to return a recarray (MaskedRecords) or not. z7The number of arrays does not match the number of namesNc<g|]}tj|ddS)FT)copysubok)rLrrs r#r:z!append_fields..s)BBBd333BBBr%cNg|]"\}}|||jfg#Sr )rpr')r3r4rOs r#r:z!append_fields..s1LLLitQqw())LLLr%rKz5The dtypes argument must be None, a dtype, or a list.crg|]4\}}}tj|dd|||fg5S)FT)rrr')rLrrp)r3rOrds r#r:z!append_fields..sU;;;Q1d!<<<AAAq6(KK;;;r%)rsrkT)rNrsrkrr)r\r?rr*r)rerrpoprq masked_allrr<r'rru) rr(rwrrkrsrtmsgr"s r#r r s @%%'' u::T " "KCS// ! # E3   x ~BBTBBBLL3ud;K;KLLL&5$-00 ZF t99F # #6{{a#d))+M oo%;;!$T5&!9!9;;; g* E E ED 4yy1}}D$'1333xxzz ] CIIs4yy!!TZ((>$*+E+EEGGGF#4 0 0F "4 0 0F vw: F F FFr%c#"K|V|Ed{VdSrr rr(rwrs r#_rec_append_fields_dispatcherrs$ JJJOOOOOOOOOr%c,t||||ddS)aM Add new fields to an existing array. The names of the fields are given with the `names` arguments, the corresponding values with the `data` arguments. If a single field is appended, `names`, `data` and `dtypes` do not have to be lists but just values. Parameters ---------- base : array Input array to extend. names : string, sequence String or sequence of strings corresponding to the names of the new fields. data : array or sequence of arrays Array or sequence of arrays storing the fields to add to the base. dtypes : sequence of datatypes, optional Datatype or sequence of datatypes. If None, the datatypes are estimated from the `data`. See Also -------- append_fields Returns ------- appended_array : np.recarray TF)rwrrtrs)r rs r#rrs(> u4$(% 9 9 99r%c|fSrr )rOalignrecurses r#_repack_fields_dispatcherrs 4Kr%ct|tjs.t|j||}||dS|j|Sg}|jD]f}|j|}|rt|d|d}n|d}t|dkr |d|f}|||fgtj|| }tj|j |fS) a Re-pack the fields of a structured array or dtype in memory. The memory layout of structured datatypes allows fields at arbitrary byte offsets. This means the fields can be separated by padding bytes, their offsets can be non-monotonically increasing, and they can overlap. This method removes any overlaps and reorders the fields in memory so they have increasing byte offsets, and adds or removes padding bytes depending on the `align` option, which behaves like the `align` option to `numpy.dtype`. If `align=False`, this method produces a "packed" memory layout in which each field starts at the byte the previous field ended, and any padding bytes are removed. If `align=True`, this methods produces an "aligned" memory layout in which each field's offset is a multiple of its alignment, and the total itemsize is a multiple of the largest alignment, by adding padding bytes as needed. Parameters ---------- a : ndarray or dtype array or dtype for which to repack the fields. align : boolean If true, use an "aligned" memory layout, otherwise use a "packed" layout. recurse : boolean If True, also repack nested structures. Returns ------- repacked : ndarray or dtype Copy of `a` with fields repacked, or `a` itself if no repacking was needed. Examples -------- >>> from numpy.lib import recfunctions as rfn >>> def print_offsets(d): ... print("offsets:", [d.fields[name][1] for name in d.names]) ... print("itemsize:", d.itemsize) ... >>> dt = np.dtype('u1, >> dt dtype({'names': ['f0', 'f1', 'f2'], 'formats': ['u1', '>> print_offsets(dt) offsets: [0, 8, 16] itemsize: 24 >>> packed_dt = rfn.repack_fields(dt) >>> packed_dt dtype([('f0', 'u1'), ('f1', '>> print_offsets(packed_dt) offsets: [0, 1, 9] itemsize: 17 )rrF)rNrTr7r) r\rLr'rastyper(r1r*r>r)rOrrdt fieldinfor4rmfmts r#rrsx a " "( 17% A A Axxx'''wI & &htn  AeTBBBCCa&C s88q==FD>D$%%%% )5 ) ) )B 8QVRL ! !!r%c d}g}|jD]}|j|}|d|d}}||\}}|j1|tj||ff|||zfet |||z} |j t|D]@ dkr|| | fd| DA|S)z Returns a flat list of (dtype, count, offset) tuples of all the scalar fields in the dtype "dt", including nested fields, in left to right order. cfd}|jdkr!|jD]}||z}|j}|jdk!||fS)NrKr )rr)rrrs r# count_elemz+_get_fields_and_offsets..count_elem\sNh"nn   Bh"nn5yr%rrKNc.g|]\}}}|||zzfSr r )r3rcoirs r#r:z+_get_fields_and_offsets..us."O"O"O'!QAq!af*#5"O"O"Or%) r(r1r>rLr'_get_fields_and_offsetsitemsizerangerD) roffsetrr1r4r,f_dtf_offsetr subfieldsrrs @@r#rrSsFQQ $q58h*T""a :  MM28TA4L111h6GH I I I I/h6GHHI=D1XX Q Q66MM),,,,MM"O"O"O"O"OY"O"O"OPPPP  Q Mr%cht|dkr|S|d|dk}|r+tt|t|}nt||}d}d}|D]A\}}|dkr|rdS||}||krdS||dz |zz} n|} |||z } || }|| krdS| }B|r| S|S)z Returns the stride between the fields, or None if the stride is not constant. The values in "counts" designate the lengths of subarrays. Subarrays are treated as many contiguous fields, with always positive stride. rKrN)r*rreversed) offsetscountsrnegativeit prev_offsetstriderr end_offset new_strides r#_common_striderxs 7||qqzGAJ&H" '""HV$4$4 5 5 & ! !K F!!  A:: tt~!!!tt519"88JJJ  "+-J~###tt w Mr%c|fSrr )arrr'rcastings r#&_structured_to_unstructured_dispatcherr 6Mr%unsafec|jjtdt|j}t |}|dkr|td|dkrt dt |\}}}dt|D} |tj d|Dntj|tj| |||jj d} | | }t|tj tjtjfv} |sF| rCt!fd |Dr't#||j } | |j} |jt)|j fz}|jt-| d fz}|d tjf tj}|d t3|df}tjj|||d }| d}| dkr |d dddf}t|t| jur | |}|Stj| fd|Dd}||||}| t)|ffS)aW Converts an n-D structured array into an (n+1)-D unstructured array. The new array will have a new last dimension equal in size to the number of field-elements of the input array. If not supplied, the output datatype is determined from the numpy type promotion rules applied to all the field datatypes. Nested fields, as well as each element of any subarray fields, all count as a single field-elements. Parameters ---------- arr : ndarray Structured array or dtype to convert. Cannot contain object datatype. dtype : dtype, optional The dtype of the output unstructured array. copy : bool, optional If true, always return a copy. If false, a view is returned if possible, such as when the `dtype` and strides of the fields are suitable and the array subtype is one of `np.ndarray`, `np.recarray` or `np.memmap`. .. versionchanged:: 1.25.0 A view can now be returned if the fields are separated by a uniform stride. casting : {'no', 'equiv', 'safe', 'same_kind', 'unsafe'}, optional See casting argument of `numpy.ndarray.astype`. Controls what kind of data casting may occur. Returns ------- unstructured : ndarray Unstructured array with one more dimension. Examples -------- >>> from numpy.lib import recfunctions as rfn >>> a = np.zeros(4, dtype=[('a', 'i4'), ('b', 'f4,u2'), ('c', 'f4', 2)]) >>> a array([(0, (0., 0), [0., 0.]), (0, (0., 0), [0., 0.]), (0, (0., 0), [0., 0.]), (0, (0., 0), [0., 0.])], dtype=[('a', '>> rfn.structured_to_unstructured(a) array([[0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.]]) >>> b = np.array([(1, 2, 5), (4, 5, 7), (7, 8 ,11), (10, 11, 12)], ... dtype=[('x', 'i4'), ('y', 'f4'), ('z', 'f8')]) >>> np.mean(rfn.structured_to_unstructured(b[['x', 'z']]), axis=-1) array([ 3. , 5.5, 9. , 11. ]) Narr must be a structured arrayrz(arr has no fields. Unable to guess dtypez#arr with no fields is not supportedc8g|]}d|Szf{}formatr3rs r#r:z.structured_to_unstructured..s" 6 6 6U\\!__ 6 6 6r%cg|] }|j Sr r)r3rs r#r:z.structured_to_unstructured..s$;$;$;RW$;$;$;r%r(formatsrrc3.K|]}|jkVdSrrr3r out_dtypes r#r5z-structured_to_unstructured..s*&J&Jrw)';&J&J&J&J&J&Jr%rK.T)r.rrc"g|] }|jf Sr )rrs r#r:z.structured_to_unstructured..$s )N)N)NB9bh*?)N)N)Nr%r(rrr)r'r(r)rr*NotImplementedErrorrrrL result_typerrprrrmemmapallr__array_wrap__rsumstridesabsnewaxisuint8minlib stride_tricks as_strided__self__r)rr'rrr1n_fieldsdtsrrr(flattened_fieldscan_view common_stridewrap new_shape new_strides packed_fieldsrs @r#rrsv y9::: $SY / /F6{{H1}}CDDD Q!"GHHH<C 6 6eHoo 6 6 6E }N$;$;s$;$;$;< HUOO x%,/,3-0Y-?!A!ABB ((# $ $C CyyRZbi@@H h3&J&J&J&Jc&J&J&J#J#J'w 8JKK  $%D S[[)2D$EEI+]););Q(??Kc2:o&++BH55Cc3w<<==()C&&11#2;2=8<2>>C ((9%%f-Cq  #ttt)nCyyT] 3 333d3iiJHu)N)N)N)N#)N)N)NPPQQM **]w* ? ?C 88YV/ 0 00r%c|fSrr )rr'r(rrrs r#&_unstructured_to_structured_dispatcherr#+rr%cjdkrtdjd}|dkrtd|Z|dt|D}t jfd|D| }t |}t|\} } } n|td t j|}t |}t|dkrggg} } } n t|\} } } |t| krtd |}|r|j std d tt|D}t j|fd| Dd} t j  | t j|| | |j d} | || |dS)a Converts an n-D unstructured array into an (n-1)-D structured array. The last dimension of the input array is converted into a structure, with number of field-elements equal to the size of the last dimension of the input array. By default all output fields have the input array's dtype, but an output structured dtype with an equal number of fields-elements can be supplied instead. Nested fields, as well as each element of any subarray fields, all count towards the number of field-elements. Parameters ---------- arr : ndarray Unstructured array or dtype to convert. dtype : dtype, optional The structured dtype of the output array names : list of strings, optional If dtype is not supplied, this specifies the field names for the output dtype, in order. The field dtypes will be the same as the input array. align : boolean, optional Whether to create an aligned memory layout. copy : bool, optional See copy argument to `numpy.ndarray.astype`. If true, always return a copy. If false, and `dtype` requirements are satisfied, a view is returned. casting : {'no', 'equiv', 'safe', 'same_kind', 'unsafe'}, optional See casting argument of `numpy.ndarray.astype`. Controls what kind of data casting may occur. Returns ------- structured : ndarray Structured array with fewer dimensions. Examples -------- >>> from numpy.lib import recfunctions as rfn >>> dt = np.dtype([('a', 'i4'), ('b', 'f4,u2'), ('c', 'f4', 2)]) >>> a = np.arange(20).reshape((4,5)) >>> a array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19]]) >>> rfn.unstructured_to_structured(a, dt) array([( 0, ( 1., 2), [ 3., 4.]), ( 5, ( 6., 7), [ 8., 9.]), (10, (11., 12), [13., 14.]), (15, (16., 17), [18., 19.])], dtype=[('a', '.ps"<<<U\\!__<<.qs<<<q#)n<<.s" 9 9 9U\\!__ 9 9 9r%c,g|]}j|jfSr )r'r)r3rrs r#r:z.unstructured_to_structured..s")N)N)NB39bh*?)N)N)Nr%rrr r)rr)r rrLr'rrr*risalignedstructascontiguousarrayrprr)rr'r(rrrn_elemrr1rrrr!rs` r#rr/s=p yB?@@@ Yr]F {{!"JKKK } =<>> from numpy.lib import recfunctions as rfn >>> b = np.array([(1, 2, 5), (4, 5, 7), (7, 8 ,11), (10, 11, 12)], ... dtype=[('x', 'i4'), ('y', 'f4'), ('z', 'f8')]) >>> rfn.apply_along_fields(np.mean, b) array([ 2.66666667, 5.33333333, 8.66666667, 11. ]) >>> rfn.apply_along_fields(np.mean, b[['x', 'z']]) array([ 3. , 5.5, 9. , 11. ]) Nrr)axis)r'r(r)r)r-ruarrs r#r r s@F y9::: %c * *D 42   r%c ||fSrr )dstsrczero_unassigneds r#!_assign_fields_by_name_dispatcherr6s 8Or%c|jj||d<dS|jjD]5}||jjvr|rd||<t|||||6dS)a Assigns values from one structured array to another by field name. Normally in numpy >= 1.14, assignment of one structured array to another copies fields "by position", meaning that the first field from the src is copied to the first field of the dst, and so on, regardless of field name. This function instead copies "by field name", such that fields in the dst are assigned from the identically named field in the src. This applies recursively for nested structures. This is how structure assignment worked in numpy >= 1.6 to <= 1.13. Parameters ---------- dst : ndarray src : ndarray The source and destination arrays during assignment. zero_unassigned : bool, optional If True, fields in the dst for which there was no matching field in the src are filled with the value 0 (zero). This was the behavior of numpy <= 1.13. If False, those fields are not modified. N.r)r'r(r )r3r4r5r4s r#r r s4 yC 33 sy & & D !#d)SY"1 3 3 3 3 33r%c|fSrr )rrequired_dtypes r#_require_fields_dispatcherr:s 8Or%c\tj|j|}t|||S)a Casts a structured array to a new dtype using assignment by field-name. This function assigns from the old to the new array by name, so the value of a field in the output array is the value of the field with the same name in the source array. This has the effect of creating a new ndarray containing only the fields "required" by the required_dtype. If a field name in the required_dtype does not exist in the input array, that field is created and set to 0 in the output array. Parameters ---------- a : ndarray array to cast required_dtype : dtype datatype for output array Returns ------- out : ndarray array with the new dtype, with field values copied from the fields in the input array with the same name Examples -------- >>> from numpy.lib import recfunctions as rfn >>> a = np.ones(4, dtype=[('a', 'i4'), ('b', 'f8'), ('c', 'u1')]) >>> rfn.require_fields(a, [('b', 'f4'), ('c', 'u1')]) array([(1., 1), (1., 1), (1., 1), (1., 1)], dtype=[('b', '>> rfn.require_fields(a, [('b', 'f4'), ('newf', 'u1')]) array([(1., 0), (1., 0), (1., 0), (1., 0)], dtype=[('b', '>> 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', np.double), ('C', np.double)]) >>> test = rfn.stack_arrays((z,zz)) >>> test masked_array(data=[(b'A', 1.0, --), (b'B', 2.0, --), (b'a', 10.0, 100.0), (b'b', 20.0, 200.0), (b'c', 30.0, 300.0)], mask=[(False, False, True), (False, False, True), (False, False, False), (False, False, False), (False, False, False)], fill_value=(b'N/A', 1.e+20, 1.e+20), dtype=[('A', 'S3'), ('B', '.Rs.:::aq!!'')):::r%c,g|]}t|Sr r8rs r#r:z stack_arrays..Ss***1A***r%cg|] }|j Sr rrs r#r:z stack_arrays..Ts ) ) )!ag ) ) )r%cg|] }|j Sr r;)r3rs r#r:z stack_arrays..Us(((A(((r%cg|]\}}|Sr r )r3rrs r#r:z stack_arrays..Ys $ $ $41aQ $ $ $r%NzIncompatible type 'z' <> ''rzf%ir)r\rr*r<r>indexr TypeErrorrq concatenaterrLrcumsumr_rr'r(rur})r>rzrsrtr?rMnrecordsrFfldnamesdtype_lnewdescrr(dtype_nfnamefdtypenameidxrIcdtyper"rseenrOrrjr4s r#rr%sR&'"" V  ay::6:::I** ***H ) )y ) ) )F(((((HQiGg&&H $ $8 $ $ $E!"": 6 6+G44 6 6ME6E!!000 U####++e,,$W- 66).FF0C0C(DHW%%v%%#)%+VVVVV%5666& 6 8}} **x 0 02H==25H-.. 8VCRC[&*MM * *LQ1aGME}12us4yy()!A#..**D()$F4L1%4'' D)))* }VX66&: ? ? ??r%c|fSrr )rOkey ignoremask return_indexs r#_find_duplicates_dispatcherr\}s 4Kr%ctj|}t|j}|}|r||D] }||} ||}|}||}|} | dd| ddk} |r|j} d| | dd<tjdg| f} | dd| ddz| dd<||| } |r | || fS| S)a Find the duplicates in a structured array along a given key Parameters ---------- a : array-like Input array key : {string, None}, optional Name of the fields along which to check the duplicates. If None, the search is performed by records ignoremask : {True, False}, optional Whether masked data should be discarded or considered as duplicates. return_index : {False, True}, optional Whether to return the indices of the duplicated values. Examples -------- >>> 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) (masked_array(data=[(1,), (1,), (2,), (2,)], mask=[(False,), (False,), (False,), (False,)], fill_value=(999999,), dtype=[('a', 'E7D/ * *DSbS DH$D"I7D!JGDM**r%c  ||fSrr ) rYr1r2jointype r1postfix r2postfixrzrsrts r#_join_by_dispatcherrl  8Or%inner12c  |dvrtd|zttrftt tkr:t fdt D} td| zD]B} | |jjvrtd| z| |jjvrtd| zC| }| }t|} |jj|jj} } t | t | zt z }|r|s|sd}|dz }t|fd | D}t||}t||}tj ||f}| }||}tj d g|d d |d dkf}|d d |d dz|d d<||}||| k}||| k| z }t|t|}}|dkrd\}}n|dkrq||}tj |||| kf}tj |||| k| z f}t||z t||z }}nC|dkr=||}tj |||| kf}t||z d}}||||}}t|j} t|jD] \}!}"|!vr| |!|"f!t|jD]\}!}"t#d| D}# |#|!}$| |$\}%}&|!vr|!t'|"|&f| |$<Y|!|z|&f|!|z|"fg| |$|$d z<p#t$r| |!|"fYwxYwtj| } t'||}'tj|'|z|zf| }(|(jj}#| D]J})||)}*|)|#vs |)| vr |s |)vr|)|z })|(|)}+|*d ||+d |<|dvr|*|d |+|'|'|z<K| D]L})||)}*|)|#vs |)| vr |s |)vr|)|z })|(|)}+|*d ||+d |<|dkr|r|*|d |+| d <M|( t-||},t/t1|(|fi|,S)a< Join arrays `r1` and `r2` on key `key`. The key should be either a string or a sequence of string corresponding to the fields used to join the array. An exception is raised if the `key` field cannot be found in the two input arrays. Neither `r1` nor `r2` should have any duplicates along `key`: the presence of duplicates will make the output quite unreliable. Note that duplicates are not looked for by the algorithm. Parameters ---------- key : {string, sequence} A string or a sequence of strings corresponding to the fields used for comparison. r1, r2 : arrays Structured arrays. jointype : {'inner', 'outer', 'leftouter'}, optional If 'inner', returns the elements common to both r1 and r2. If 'outer', returns the common elements as well as the elements of r1 not in r2 and the elements of not in r2. If 'leftouter', returns the common elements and the elements of r1 not in r2. r1postfix : string, optional String appended to the names of the fields of r1 that are present in r2 but absent of the key. r2postfix : string, optional String appended to the names of the fields of r2 that are present in r1 but absent of the key. defaults : {dictionary}, optional Dictionary mapping field names to the corresponding default values. usemask : {True, False}, optional Whether to return a MaskedArray (or MaskedRecords is `asrecarray==True`) or a ndarray. asrecarray : {False, True}, optional Whether to return a recarray (or MaskedRecords if `usemask==True`) or just a flexible-type ndarray. Notes ----- * The output is sorted along the key. * A temporary array is formed by dropping the fields not in the key for the two arrays and concatenating the result. This array is then sorted, and the common entries selected. The output is constructed by filling the fields with the selected entries. Matching is not preserved if there are some duplicates... )rnouter leftouterzWThe 'jointype' argument should be in 'inner', 'outer' or 'leftouter' (got '%s' instead)c3@K|]\}}||dzdv|VdS)rKNr )r3rxrYs r#r5zjoin_by..s7AA1!s1Q344y..1....AAr%zduplicate join key %rzr1 does not have key field %rzr2 does not have key field %rz8r1 and r2 contain common names, r1postfix and r2postfix zcan't both be emptycg|]}|v| Sr r )r3rrYs r#r:zjoin_by..s - - -1188Q888r%)orderFrKNrrn)rrrrrsrc3 K|] \}}|V dSrr r2s r#r5zjoin_by..Es&44kdET444444r%r)rrrsr)r)r\rer*rnext enumerater'r(rrrqrJr^rLr<r>rrHrrsortdictrur})-rYrgrhrirjrkrzrsrtdupr4nb1r1namesr2names collisionsrkey1r1kr2kauxidx_sortflag_inidx_inidx_1idx_2r1cmnr2cmnr1spcr2spcidx_outs1s2rFrRrSr(rTrIrUcmnr"r9selectedr-kwargss-` r#rrsTh666<>FG  #sf 3s88}}C  AAAA #AAAAA036777EE rx~ % %w Y&/Y&//ww{*++ + + + MM5&/ * * * * * +"Xf  F eU  C ]C%K%/1 @ @ @F L E 88a5 E>>a7ll9l# NA)"6E6* - - -'/'7GCe O $ 00a5 E>>a7ll9l# NA)"6E6*   U '/GUFGG  KKcK 'j 9 9 9F }VX66 A A& A AAs0P!P%$P%c ||fSrr )rYrgrhrirjrkrzs r#_rec_join_dispatcherrvrmr%cJt||||dd}t|||fi|S)z Join arrays `r1` and `r2` on keys. Alternative to join_by, that always returns a np.recarray. See Also -------- join_by : equivalent function FT)rirjrkrzrsrt)r|r)rYrgrhrirjrkrzrs r#rr|sB8yI#UtEEEF 3B ) )& ) ))r%)F)NN)NT)TFr)NNNN)rFFF)NrTF)FF)r)NNN)NFr)NNNNN)NNFFr)T)NTFF)NTF)NNNNNN)rnrorpNTF)rnrorpN)C__doc__rinumpyrLnumpy.marqrrrnumpy.ma.mrecordsrnumpy.core.overridesrnumpy.lib._iotoolsrcorer__all__r$rr<rrrrPrSrr^rfrnrur}rrrr rrrrrrr rrrrrrrrr#rr.r r6r r:rr@rr\r rlrrrr r%r#rs######## ++++++888888......EJ0   :;;##<;#L   >@@4     8 8 8 8,,,,^       """"0"    BF6: 12238+0DDD32DN011=G=G=G21=G@GGGG2455II65I233$$43$N9=HL 233,0:?CGCGCG43CGL 677999879D233P"P"P"43P"d####J***ZBF37?@@~1~1~1A@~1BCGJN?@@BG3;h'h'h'A@h'T788&&98&T:;;#3#3#3<;#3J344((54(V=A:> 122AF"T?T?T?32T?p48 455666656t?C04 ,--DG49pBpBpB.-pBh?C -..EH * * */. * * *r%