i$8dZddlZddlZddlmZddlmcmZddl m Z ddl m Z gdZ dZe edZdZd Zd Zd Zd>d Zd>dZd?dZdZdZd@dZdAdZdBdZ dCdZe e dDdZd?dZe edAdZdAdZ dZ!e e!dZ"dZ#e e#dZ$ dCd Z%e e% dEd!Z&dBd"Z'e e'dBd#Z(d?d$Z)e e)dFd%Z*dGd&Z+d'Z, dHd(Z-e e-dId*Z. dJd+Z/e e/ dKd,Z0d-Z1e e1d.Z2dBd/Z3e e3dLd0Z4d1Z5e e5d2Z6 dCd3Z7e e7 dMd4Z8 dHd5Z9e e9dNd6Z: dOd7Z;e e; dPd;Z< dCd<Z=e e= dQd=Z>[ dS)Rz 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)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 DC:\PYTHON\_runtimes\venv\Lib\site-packages\numpy/lib/recfunctions.py!_recursive_fill_fields_dispatcherr s 6?c|j}|jD]Y} ||}n#t$rYwxYw|jjt|||?|||dt |<Z|S)a 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 -------- >>> import numpy as np >>> 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', '>> import numpy as np >>> 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.._s/EE4d+,EEEEEEr!cbg|],\}}t|dkr|n |d|f|df-S)rr&)r/r0fs r z"_get_fieldspec..asO   aVVq[[TTqtTlAaD 9   r!r$)r#r-s` r_get_fieldspecr8GsU( {U }EEEEEEE  !    r!c g}|j}|D]X}||}|j2||tt|fC||Yt|S)ab 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 -------- >>> import numpy as np >>> 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$appendtupler adtype listnamesr$r0r)s rr r gs,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 -------- >>> import numpy as np >>> 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:extendr r;r<s rr r so.I LE66, = $   ^G44 5 5 5   r!c|j}|d|ffSg}|D]S}|j|\}}|j#|t|<|||fTt |S)a_ Flatten a structured data-type description. Examples -------- >>> import numpy as np >>> 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$r-r@r r:r;)ndtyper$descrr(typ_s rr r s 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+)r@r r#r$r&r8r:np) seqarraysflattenr'ar)s r _zip_dtyperLsH / 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. rJ)rLrC)rIrJs r _zip_descrrOs i 1 1 1 77r!c<|i}|j}|D]}||}|j3|r|g||<ng||<|t|||Dt||gpg}|r||n|r|g}|pg||<|S)a} 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 internally during recursion). Examples -------- >>> import numpy as np >>> 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']} )r$updater listgetr:)r=lastnameparentsr$r0r) lastparents rr r s6 LE--, = $ #!)  " NN-gtWEE F F F Fgkk(B77=2>>J *!!(++++ *&\ &,"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) isinstancerHvoid_izip_fields_flatr;iterableelements rrZrZsi  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__NrG)hasattrrXstr _izip_fieldsrHrYr&r;r[s rrbrbs  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)rZrb itertools zip_longestr;)rI fill_valuerJzipfunctups r _izip_recordsrj/sd#$iF:FF""GGCLL!!!!!!""r!ct|tjsd}|r"|r|tj}n5tj|}|r|tj}|S)z Private function: return a recarray, a ndarray, a MaskedArray or a MaskedRecords depending on the input parameters F) rXma MaskedArrayviewmrec MaskedRecordsfilledrHrecarray)rusemask asrecarrays r _fix_outputruGsq fbn - -.  5[[!344F6""  .[[--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$datamaskrgitems)rdefaultsr$rwrxrgkvs r _fix_defaultsr}Xso L E & V[&:K4T>r((**!!A ::JqM DGDG  Mr!c|Srr)rIrgrJrsrts r_merge_arrays_dispatcherrfs r!c 0 t|dkrtj|d}t|tjtjfr|j}|jtjd|fg}|rt|fd|krd| }|r|r tj }n(tj }n|r tj}n tj}|||S|f}n d|D}t!d |D}t#|}t||} g} g} |rt%||D]b\} } || z }| }tj|  }|rtj|| j}t|tjtjfrlt|jdkr|d}d}n>> import numpy as np >>> 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)sizer/rKs rr1zmerge_arrays..s$,,Q!&,,,,,,r!)r#ndmin)rGr#)r#count)rx)r&rHrrXndarrayrYr#r$rLravelrorprlrmrrrnr;maxzip __array__ getmaskarray_check_fill_valueitemarrayonesr:rechainrjfromiterrR)rIrgrJrsrtseqdtypeseqtypesizes maxlengthr'seqdataseqmaskrKn nbmissingrwrxfvalfmskrs rrrksKd I!M)A,// )bj"'233<? > !x"h 011H %*i\4@@@HLL!))I %-"0GG nGG %+*>>w>?? ?" II<;;;; ,,),,, , ,EE I)W555HGG..)U++ F FFQ"QI7799&&((D?1%%++--D -j!'BBdRZ$9::?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  5[[!344F)U++ F FFQ"QI7799&&((D -j!'BBdRZ$9::F4:!++#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. 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 -------- >>> import numpy as np >>> 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', '._drop_descr s  1 1DTlGz!!}(# GZ883OOT5M222w0000r!rrsrt)rsetr#rHemptyshaperru)rrrsrtr'rrs @rrrsFz""% \ __      {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|fSrr)r/rrs rr6z _keep_fields..Is$777qDJqM"777r!rr)rHrrrru)r keep_namesrsrtr'rs` r _keep_fieldsr6sX&8777J777H Xdj 1 1 1F "4 0 0F vw: F F FFr!c|fSrrrrs r_rec_drop_fields_dispatcherrOrr!c(t||ddS)zK Returns a new numpy.recarray with fields in `drop_names` dropped. FTr)rrs rrrSs 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 -------- >>> import numpy as np >>> 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', '._recursive_rename_fieldsxsL 4 4D nnT400GTlG}(66w KKL' 23333r!)r#rn)rrr'rs @rrr_sD2     (' J??H 99X  r!c#"K|V|Ed{VdSrr)rr$rwdtypesrgrsrts 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)NT)copysubok)rHrrs rr6z!append_fields..s)AAAqT222AAAr!cNg|]"\}}|||jfg#Sr)rnr#)r/r0rKs rr6z!append_fields..s1LLLitQqw())LLLr!rGz5The dtypes argument must be None, a dtype, or a list.crg|]4\}}}tj|dd|||fg5S)NT)rrr#)rHrrn)r/rKrds rr6z!append_fields..sU;;;Q1T;;;@@1a&JJ;;;r!)rsrgT)rJrsrgrr)rXr;rRr&r%rarrpoprl masked_allrr8r#rru) rr$rwrrgrsrtmsgrs rrrs @%%'' u::T " "KCS// ! # E3   x ~AADAAALL3ud;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{VdSrrrr$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)rrs rrrs(> u4$(% 9 9 99r!c|fSrr)rKalignrecurses 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 -------- >>> import numpy as np >>> 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)rNrTr3r) rXrHr#rastyper$r-r&r:r)rKrrdt fieldinfor0rifmts rrrsz 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)NrGr)rr)rrrs r count_elemz+_get_fields_and_offsets..count_elem^sNh"nn   Bh"nn5yr!rrGNc.g|]\}}}|||zzfSrr)r/rcoirs rr6z+_get_fields_and_offsets..ws."Q"Q"QGAq!Aq!a$h,#7"Q"Q"Qr!) r$r-r:rHr#_get_fields_and_offsetsitemsizeranger@) roffsetrr-r0r(f_dtf_offsetr subfieldsrrs @@rrrUsFSS $q58h*T""a :  MM28TA4L111h6GH I I I I/h6GHHI=D1XX S S66MM),,,,MM"Q"Q"Q"Q"Qy"Q"Q"QRRRR  S 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. rGrN)r&rreversed) offsetscountsrnegativeit prev_offsetstriderr end_offset new_strides r_common_striderzs 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)a{ 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 `numpy.ndarray`, `numpy.recarray` or `numpy.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 -------- >>> import numpy as np >>> 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 supportedcg|]}d|Sr5rr/rs rr6z.structured_to_unstructured..s . . .WWW . . .r!cg|] }|j Srr)r/rs rr6z.structured_to_unstructured..s$;$;$;RW$;$;$;r!r$formatsrrc3.K|]}|jkVdSrrr/r out_dtypes rr1z-structured_to_unstructured..s*&J&Jrw)';&J&J&J&J&J&Jr!rG.T)r.rrc"g|] }|jf Sr)rrs rr6z.structured_to_unstructured..'s )N)N)NB9bh*?)N)N)Nr!r$rrr)r#r$r%rr&NotImplementedErrorrrrH result_typerrnrrrrmemmapallr__array_wrap__rsumstridesabsnewaxisuint8minlib stride_tricks as_strided__self__r)rr#rrr-n_fieldsdtsrrr$flattened_fieldscan_view common_stridewrap new_shape new_strides packed_fieldsrs @rrrsx y9::: $SY / /F6{{H1}}CDDD Q!"GHHH<C . .eHoo . . .E }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 -------- >>> import numpy as np >>> 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', '.ts444WWW444r!c"g|] }|jf Srr)r/rrs rr6z.unstructured_to_structured..us<<<q#)n<<.s 1 1 1WWW 1 1 1r!c,g|]}j|jfSr)r#r)r/rrs rr6z.unstructured_to_structured..s")N)N)NB39bh*?)N)N)Nr!rrrr)rr%rrrHr#rrr&r isalignedstructascontiguousarrayrnrr)rr#r$rrrn_elemrr-rrrrrs` rrr2s=r yB?@@@ Yr]F {{!"JKKK } =44eFmm444EH<<<>> import numpy as np >>> 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 rrrs@H y9::: %c * *D 42   r!c ||fSrr)dstsrczero_unassigneds r!_assign_fields_by_name_dispatcherr4s 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)r1r2r3r0s rrrs4 yC 33 sy & & D !#d)SY"1 3 3 3 3 33r!c|fSrr)rrequired_dtypes r_require_fields_dispatcherr8s 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 -------- >>> import numpy as np >>> 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', '&s Mr!c t|tjr|St|dkr|dSd|D}d|D}d|D}d|D}|d} t | } d| D} |ddD]} t | D]\} }| | vr-| | |f| | 6| | }| |\}}|r| t||f| |<n||krtd |d |d t| dkrtj |}ntj tj |f| }tj tjd|f}g}t|||dd |ddD]d\}}}}|jj} | ||d t|||<3|D].}||||||<||vr||/et%t'||||S)a Superposes arrays fields by fields Parameters ---------- arrays : array or sequence Sequence of input arrays. 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. autoconvert : {False, True}, optional Whether automatically cast the type of the field to the maximum. Examples -------- >>> import numpy as np >>> 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', 1e+20, 1e+20), dtype=[('A', 'S3'), ('B', '.Ys.:::aq!!'')):::r!c,g|]}t|Srr4rs rr6z stack_arrays..Zs***1A***r!cg|] }|j Srrrs rr6z stack_arrays..[s ) ) )!ag ) ) )r!cg|] }|j Srr7)r/rs rr6z stack_arrays..\s(((A(((r!cg|]\}}|Srr)r/rrs rr6z stack_arrays..`s $ $ $41aQ $ $ $r!NzIncompatible type 'z' <> ''rr5r)rXrHrr&r8r:indexr TypeErrorrl concatenaterr cumsumr_rr#r$rur})r<rzrsrtr=rInrecordsrBfldnamesdtype_lnewdescrr$dtype_nfnamefdtypenameidxrEcdtyperrseenrKrrjr0s rrr+sT&"*%% V  ay::6:::I** ***H ) )y ) ) )F(((((HQiGg&&H $ $8 $ $ $E!"": S S+G44 S SME6E!!000 U####++e,,$W- 6S).FF0C0C(DHW%%v%%#$Q&$Q$Q$Q$Q$QRRR& S 8}} **x 0 02H==25H-.. 8VCRC[&*MM * *LQ1aGME}/03t99'!,,**D()$F4L1%4'' D)))* }VX66&: ? ? ??r!c|fSrr)rKkey ignoremask return_indexs r_find_duplicates_dispatcherrZs 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 -------- >>> import numpy as np >>> 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) rWr1r2jointype r1postfix r2postfixrzrsrts r_join_by_dispatcherrj  8Or!inner12c  |dvrtd|zttrftt tkr:t fdt D} td| D]B} | |jjvrtd| | |jjvrtd| C| }| }t|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]\}"}#d|!D}$ |$|"}%|!|%\}&}'|"vr|"t%|#|'f|!|%<L|"|z|'f|"|z|#fg|!|%|%d z<c#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|) ||d}-t+t-|)|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... )rlouter leftouterzWThe 'jointype' argument should be in 'inner', 'outer' or 'leftouter' (got '%s' instead)c3@K|]\}}||dzdv|VdS)rGNr)r/rxrWs rr1zjoin_by..s=DDA1AEFF 3C3C13C3C3C3CDDr!zduplicate join key zr1 does not have key field zr2 does not have key field z8r1 and r2 contain common names, r1postfix and r2postfix zcan't both be emptycg|]}|v| Srr)r/rrWs rr6zjoin_by..s + + +!!s((A(((r!)orderFrGNrrl)rrrprqrcg|]\}}|Srrr.s rr6zjoin_by..Js000+$000r!r)rprqr)r%rXrar&rnext enumerater#r$rrrlrHr\rHr8r:rFrrsortrur}).rWrerfrgrhrirzrsrtdupr0nb1nb2r1namesr2names collisionsrkey1r1kr2kauxidx_sortflag_inidx_inidx_1idx_2r1cmnr2cmnr1spcr2spcidx_outs1s2rBrPrQr$rRrErScmnrr5selectedr)kwargss.` rrrsWh666<>FG  #sf 3s88}}C  DDDD3DDDDD6s66777EE rx~ % %C4CCDD D rx~ % %C4CCDD D & B Bb''3r77#S(."(.gWg,,W-S9J9 H $$oo , + + +w + + +D r4 C r4 C .#s $ $C{{{%%H h-Cnugs122w#crc(':;<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 ; ;F }VX66 A A& A AAs2P!P'&P'c ||fSrr)rWrerfrgrhrirzs r_rec_join_dispatcherr{rkr!c2||||ddd}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)rgrhrirzrsrt)r)rWrerfrgrhrirzrs rrrs:#&5NNF 3B ) )& ) ))r!)F)NN)NT)TFr)NNNN)rFFF)NrTF)FF)r)NNN)NFr)NNNNN)NNFFr)T)NTFF)NTF)NNNNNN)rlrmrnNTF)rlrmrnN)?__doc__renumpyrHnumpy.marlnumpy.ma.mrecordsmrecordsronumpy._core.overridesrnumpy.lib._iotoolsr__all__r rr8r r r rLrOr rZrbrjrur}rrrrrrrrrrrrrrrrrrrr!rr,rr4rr8rr>rrZr rjrrrrr!rrsn 999999......   :;;$$<;$N   @BB6     8 8 8 8----`       """"0"    BF6: 12238+0EEE32EP011:G:G:G21:GzGGGG2455II65I233%%43%P9=HL 233,0:?CGCGCG43CGL 677999879D233Q"Q"Q"43Q"f####J***ZBF37?@@111A@1DCGJN?@@BG3;i'i'i'A@i'V788''98'V:;;#3#3#3<;#3J344))54)X=A:> 122AF"T?T?T?32T?p48 455777657v?C04 ,--DG49nBnBnB.-nBd?C -..EH * * */. *r!