bg"dZddlZddlmZddlmZddlmZddlm Z dd lm Z dd l mZdd lmZgd Ze jZd ddd ddddd ddddddZe jZdZedGddZGdde jZGddeZdZed d%dZed d%dZed d&d Zd!Zed d&d"Z ed d'd$Z!dS)(a Record Arrays ============= Record arrays expose the fields of structured arrays as properties. Most commonly, ndarrays contain elements of a single type, e.g. floats, integers, bools etc. However, it is possible for elements to be combinations of these using structured types, such as:: >>> a = np.array([(1, 2.0), (1, 2.0)], dtype=[('x', np.int64), ('y', np.float64)]) >>> a array([(1, 2.), (1, 2.)], dtype=[('x', '>> a['x'] array([1, 1]) >>> a['y'] array([2., 2.]) Record arrays allow us to access fields as properties:: >>> ar = np.rec.array(a) >>> ar.x array([1, 1]) >>> ar.y array([2., 2.]) N)Counter) nullcontext) set_module)numeric) numerictypes) os_fspath)_get_legacy_print_mode)recordrecarray format_parser fromarrays fromrecords fromstringfromfilearray><=s|)blnBLNSrrrrrIicXdt|DS)z@Find duplication in a list, return a list of duplicated elementsc$g|] \}}|dk |Sr).0itemcountss I/opt/cloudlinux/venv/lib64/python3.11/site-packages/numpy/core/records.py z"find_duplicate..Ps-    D& A:: ::)ritems)lists r)find_duplicater.Ns5  #DMM//11   r+numpyc.eZdZdZddZd dZdZdZdS) ra Class to convert formats, names, titles description to a dtype. After constructing the format_parser object, the dtype attribute is the converted data-type: ``dtype = format_parser(formats, names, titles).dtype`` Attributes ---------- dtype : dtype The converted data-type. Parameters ---------- formats : str or list of str The format description, either specified as a string with comma-separated format descriptions in the form ``'f8, i4, a5'``, or a list of format description strings in the form ``['f8', 'i4', 'a5']``. names : str or list/tuple of str The field names, either specified as a comma-separated string in the form ``'col1, col2, col3'``, or as a list or tuple of strings in the form ``['col1', 'col2', 'col3']``. An empty list can be used, in that case default field names ('f0', 'f1', ...) are used. titles : sequence Sequence of title strings. An empty list can be used to leave titles out. aligned : bool, optional If True, align the fields by padding as the C-compiler would. Default is False. byteorder : str, optional If specified, all the fields will be changed to the provided byte-order. Otherwise, the default byte-order is used. For all available string specifiers, see `dtype.newbyteorder`. See Also -------- dtype, typename, sctype2char Examples -------- >>> np.format_parser(['>> np.format_parser(['f8', 'i4', 'a5'], ['col1', 'col2', 'col3'], ... []).dtype dtype([('col1', '>> np.format_parser(['.s+QQQ 7%,,q//7+QQQr+f1c,g|]}|dSrr%r&keyfieldss r)r*z/format_parser._parseFormats..s!:::c6#;q>:::r+c,g|]}|dSr$r%rDs r)r*z/format_parser._parseFormats..s!888CQ888r+) ValueError isinstancer-sbdtype enumeraterFr8 _f_formats_offsetslen_nfields)r6r7r:rKkeysrFs @r)r3zformat_parser._parseFormatss ?455 5 gt $ $ /HQQi>P>PQQQEE HWg..E >HtUm_g66E\F{::::T:::88884888 D  r+c|rt|ttfvrnJt|tr|d}nt dt|zd|d|jD|_ ng|_ |xj dtt|j |jDz c_ t|j }|rtd|z|rd|d|jD|_n g|_g}|jt|kr+|xjdg|jt|z zz c_dSdS)zQconvert input field names into a list and assign to the _names attribute ,zillegal input names %sc6g|]}|Sr%stripr&rs r)r*z0format_parser._setfieldnames..s DDD17799DDDr+Ncg|]}d|zS)zf%dr%)r&r!s r)r*z0format_parser._setfieldnames..s.AAAa AAAr+zDuplicate field names: %sc6g|]}|Sr%rUrWs r)r*z0format_parser._setfieldnames..s FFF!AGGIIFFFr+)typer-tuplerIstrsplit NameErrorreprrP_namesrangerOr.rH_titles)r6r8r9_dups r)r4zformat_parser._setfieldnamess  E{{tUm++E3'' H C(( 84;; FGGGDDeNT]N.CDDDDKKDK AA5T[1A1A15,@,@AAA A dk**  A84?@@ @  FFvnt}n/EFFFDLLDLF =3v;; & & LLTFdmc&kk&AB BLLLL ' &r+ctj|j|j|j|jd}|(t |d}||}||_dS)N)r8r7offsetsr9r)rJrKr`rMrNrb_byteorderconv newbyteorder)r6r;rKs r)r5zformat_parser._createdtypese[}l       &y|4I&&y11E r+)FN)F)__name__ __module__ __qualname____doc__r<r3r4r5r%r+r)rrWsi88t%%%% """"*"C"C"CH     r+rcLeZdZdZdZdZfdZfdZdZdZdZdZ xZ S) r zEA data-type scalar that allows field access as attribute lookup. r/ctdkr|StSNq)r __str__super__repr__r6 __class__s r)rrzrecord.__repr__s8 ! # #s * *<<>> !ww!!!r+ctdkr!t|StSrn)r r\r'rqrprss r)rpzrecord.__str__s> ! # #s * *tyy{{## #ww   r+c|dvr tj||S tj||S#t$rYnwxYwtj|dj}||d}|rW|j|dd} |j}n#t$r|cYSwxYw|j!| |j |jfS|Std|z)NsetfieldgetfieldrKrKr%'record' object has no attribute '%s') ntvoid__getattribute__AttributeErrorrFgetryrKr8viewrt)r6attr fielddictresobjdts r)r}zrecord.__getattribute__s) 4 4 47++D$77 7 7++D$77 7    D G,,T7;;B mmD$''  -$-RaR)C Y!     x#xx ;<<<J "%'+",-- -s#A AA&B.. B=<B=c\|dvrtd|ztj|dj}||d}|r|j|g|ddRSt||dr!tj|||Std|z)NrwzCannot set '%s' attributerKrrz) r~r{r|r}rFrrxgetattr __setattr__)r6rvalrrs r)rzrecord.__setattr__ s 4 4 4 !z record.pprint..'s(114SYY111111r+z %% %ds: %%sc:g|]}|t|fzSr%)r)r&rfmtr6s r)r*z!record.pprint..)s-DDDdtWT40011DDDr+ )rKr8maxjoin)r6r8maxlenrowsrs` @r)pprintz record.pprint#sb  11511111f$DDDDDeDDDyyr+) rhrirjrkrrrpr}rrr __classcell__rts@r)r r s HJ""""" !!!!! ---2 1 1 1r+r cXeZdZdZdZdZ ddZdZd Zd Zfd Zd Z dd Z xZ S)r a-Construct an ndarray that allows field access using attributes. Arrays may have a data-types containing fields, analogous to columns in a spread sheet. An example is ``[(x, int), (y, float)]``, where each entry in the array is a pair of ``(int, float)``. Normally, these attributes are accessed using dictionary lookups such as ``arr['x']`` and ``arr['y']``. Record arrays allow the fields to be accessed as members of the array, using ``arr.x`` and ``arr.y``. Parameters ---------- shape : tuple Shape of output array. dtype : data-type, optional The desired data-type. By default, the data-type is determined from `formats`, `names`, `titles`, `aligned` and `byteorder`. formats : list of data-types, optional A list containing the data-types for the different columns, e.g. ``['i4', 'f8', 'i4']``. `formats` does *not* support the new convention of using types directly, i.e. ``(int, float, int)``. Note that `formats` must be a list, not a tuple. Given that `formats` is somewhat limited, we recommend specifying `dtype` instead. names : tuple of str, optional The name of each column, e.g. ``('x', 'y', 'z')``. buf : buffer, optional By default, a new array is created of the given shape and data-type. If `buf` is specified and is an object exposing the buffer interface, the array will use the memory from the existing buffer. In this case, the `offset` and `strides` keywords are available. Other Parameters ---------------- titles : tuple of str, optional Aliases for column names. For example, if `names` were ``('x', 'y', 'z')`` and `titles` is ``('x_coordinate', 'y_coordinate', 'z_coordinate')``, then ``arr['x']`` is equivalent to both ``arr.x`` and ``arr.x_coordinate``. byteorder : {'<', '>', '='}, optional Byte-order for all fields. aligned : bool, optional Align the fields in memory as the C-compiler would. strides : tuple of ints, optional Buffer (`buf`) is interpreted according to these strides (strides define how many bytes each array element, row, column, etc. occupy in memory). offset : int, optional Start reading buffer (`buf`) from this offset onwards. order : {'C', 'F'}, optional Row-major (C-style) or column-major (Fortran-style) order. Returns ------- rec : recarray Empty array of the given shape and type. See Also -------- core.records.fromrecords : Construct a record array from data. record : fundamental data-type for `recarray`. format_parser : determine a data-type from formats, names, titles. Notes ----- This constructor can be compared to ``empty``: it creates a new record array but does not fill it with data. To create a record array from data, use one of the following methods: 1. Create a standard ndarray and convert it to a record array, using ``arr.view(np.recarray)`` 2. Use the `buf` keyword. 3. Use `np.rec.fromrecords`. Examples -------- Create an array with two fields, ``x`` and ``y``: >>> x = np.array([(1.0, 2), (3.0, 4)], dtype=[('x', '>> x array([(1., 2), (3., 4)], dtype=[('x', '>> x['x'] array([1., 3.]) View the array as a record array: >>> x = x.view(np.recarray) >>> x.x array([1., 3.]) >>> x.y array([2, 4]) Create a new, empty record array: >>> np.recarray((2,), ... dtype=[('x', int), ('y', float), ('z', int)]) #doctest: +SKIP rec.array([(-1073741821, 1.2249118382103472e-301, 24547520), (3471280, 1.2134086255804012e-316, 0)], dtype=[('x', '>> x1=np.array([1,2,3,4]) >>> x2=np.array(['a','dd','xyz','12']) >>> x3=np.array([1.1,2,3,4]) >>> r = np.core.records.fromarrays([x1,x2,x3],names='a,b,c') >>> print(r[1]) (2, 'dd', 2.0) # may vary >>> x1[1]=34 >>> r.a array([1, 2, 3, 4]) >>> x1 = np.array([1, 2, 3, 4]) >>> x2 = np.array(['a', 'dd', 'xyz', '12']) >>> x3 = np.array([1.1, 2, 3,4]) >>> r = np.core.records.fromarrays( ... [x1, x2, x3], ... dtype=np.dtype([('a', np.int32), ('b', 'S3'), ('c', np.float32)])) >>> r rec.array([(1, b'a', 1.1), (2, b'dd', 2. ), (3, b'xyz', 3. ), (4, b'12', 4. )], dtype=[('a', '.}s 2221A222r+Nrcg|] }|j Sr%r)r&rs r)r*zfromarrays..s22239222r+z>mismatch between the number of fields and the number of arrayszarray-shape mismatch in array z ("z")) rrrIrrJrKrr8rOrHr rLndim) arrayListrKrr7r8r9r:r;rr`d0nn_arraykr testshapers r)rrLsb32 222I 'u - -E }! " E3  5=32 222 gufgyIIO [F 5zzS^^##+,, , qB RB Avvdsd  eU # #FI&&3 1X]Insx"}n- ay   LaLLDLLLMM Mt Mr+c R|Y|Wtj|tfdtjdD}t |||||||S|tjt|f} nt|||||j} tj|| } || j|kr|| _n#ttf$rt|}|t|}t|tr|f}t|dkrtdt|| } t| jD]} t#|| | | <t%jdt(d | cYSwxYw| t} | S) a Create a recarray from a list of records in text form. Parameters ---------- recList : sequence data in the same field may be heterogeneous - they will be promoted to the highest data type. dtype : data-type, optional valid dtype for all arrays shape : int or tuple of ints, optional shape of each array. formats, names, titles, aligned, byteorder : If `dtype` is ``None``, these arguments are passed to `numpy.format_parser` to construct a dtype. See that function for detailed documentation. If both `formats` and `dtype` are None, then this will auto-detect formats. Use list of tuples rather than list of lists for faster processing. Returns ------- np.recarray record array consisting of given recList rows. Examples -------- >>> r=np.core.records.fromrecords([(456,'dbe',1.2),(2,'de',1.3)], ... names='col1,col2,col3') >>> print(r[0]) (456, 'dbe', 1.2) >>> r.col1 array([456, 2]) >>> r.col2 array(['dbe', 'de'], dtype='>> import pickle >>> pickle.loads(pickle.dumps(r)) rec.array([(456, 'dbe', 1.2), ( 2, 'de', 1.3)], dtype=[('col1', '.s7PPPa28CQK..0011PPPr+)r7rr8r9r:r;rzCan only deal with 1-d array.zxfromrecords expected a list of tuples, may have received a list of lists instead. In the future that will raise an errorrr)rJrrrarrrKr rrrHrrOrIrr rr[rrrr)recListrKrr7r8r9r:r;arrlistrretvalrrrrs @r)rrsX5=hwf---PPPP52;O;OPPP'7%u!'IOOO O &%))gufgyIIO!'///*  !6!6 FL+ z "*511 =LLE eS ! ! HE u::>><== =%''v{## * *Agaj))F1II   G a ) ) ) ) %. ++h  C JsB??CF  F c ||td|tj|} nt|||||j} | j} t |}|dvrt ||z | z}t|| ||} | S)aCreate a record array from binary data Note that despite the name of this function it does not accept `str` instances. Parameters ---------- datastring : bytes-like Buffer of binary data dtype : data-type, optional Valid dtype for all arrays shape : int or tuple of ints, optional Shape of each array. offset : int, optional Position in the buffer to start reading from. formats, names, titles, aligned, byteorder : If `dtype` is ``None``, these arguments are passed to `numpy.format_parser` to construct a dtype. See that function for detailed documentation. Returns ------- np.recarray Record array view into the data in datastring. This will be readonly if `datastring` is readonly. See Also -------- numpy.frombuffer Examples -------- >>> a = b'\x01\x02\x03abc' >>> np.core.records.fromstring(a, dtype='u1,u1,u1,S3') rec.array([(1, 2, 3, b'abc')], dtype=[('f0', 'u1'), ('f1', 'u1'), ('f2', 'u1'), ('f3', 'S3')]) >>> grades_dtype = [('Name', (np.str_, 10)), ('Marks', np.float64), ... ('GradeLevel', np.int32)] >>> grades_array = np.array([('Sam', 33.3, 3), ('Mike', 44.4, 5), ... ('Aadi', 66.6, 6)], dtype=grades_dtype) >>> np.core.records.fromstring(grades_array.tobytes(), dtype=grades_dtype) rec.array([('Sam', 33.3, 3), ('Mike', 44.4, 5), ('Aadi', 66.6, 6)], dtype=[('Name', '>> s = '\x01\x02\x03abc' >>> np.core.records.fromstring(s, dtype='u1,u1,u1,S3') Traceback (most recent call last) ... TypeError: a bytes-like object is required, not 'str' Nz2fromstring() needs a 'dtype' or 'formats' argument)Nr)rr)rrJrKritemsizerrOr ) datastringrKrrr7r8r9r:r;rrrs r)rrsp }LMMM gufgyIIO~H 'u - -E Z6)h6 eU 6 B B BF Mr+c|} |dd||z ||dS#||dwxYw)Nrr)tellseek)fdposs r)get_remaining_sizerKs] ''))C 1 wwyy3 QQs ,AA1c ||tdt|}|d}nt|tr|f}t |drt |} nt t|d} | 5}|dkr||dt|} |tj |} nt|||||j } | j } tj|t j} | | z}|dkrlt%|}| | z||d <t)|}tj|t j} | | z}|| krt+d t-|| }||j}||krt3d  dddn #1swxYwY|S) aCreate an array from binary file data Parameters ---------- fd : str or file type If file is a string or a path-like object then that file is opened, else it is assumed to be a file object. The file object must support random access (i.e. it must have tell and seek methods). dtype : data-type, optional valid dtype for all arrays shape : int or tuple of ints, optional shape of each array. offset : int, optional Position in the file to start reading from. formats, names, titles, aligned, byteorder : If `dtype` is ``None``, these arguments are passed to `numpy.format_parser` to construct a dtype. See that function for detailed documentation Returns ------- np.recarray record array consisting of data enclosed in file. Examples -------- >>> from tempfile import TemporaryFile >>> a = np.empty(10,dtype='f8,i4,a5') >>> a[5] = (0.5,10,'abcde') >>> >>> fd=TemporaryFile() >>> a = a.newbyteorder('<') >>> a.tofile(fd) >>> >>> _ = fd.seek(0) >>> r=np.core.records.fromfile(fd, formats='f8,i4,a5', shape=10, ... byteorder='<') >>> print(r[5]) (0.5, 10, 'abcde') >>> r.shape (10,) Nz0fromfile() needs a 'dtype' or 'formats' argument)rreadintorbrrrrz:Not enough bytes left in file for specified shape and typez%Didn't read as many bytes as expected)rrrIrhasattrropenr rrrJrKrrrprodr{intpr-indexr[rHr rdataOSError)rrKrrr7r8r9r:r;ctxrrr shapeprod shapesizenbytesr nbytesreads r)rrTsI\ }JKKK 'u - -E } E3  r:("oo9R==$'' C A:: GGFA   !"%%  HUOOEE!'5&'9MMSE>HUOO((rw(77 ( q==KKE%)iZ%7E%++b// "%LLE,,27,;;IX% D==PRR R%''[[--   ABB B ;CCCCCCCCCCCCCCC@ Ms?E$G00G47G4Tc *t|tdtfst|dr||t di} |t j|}n#|t||||| j}n||||| d} |%|t dt|||||St|trt||f||d| St|ttfrBt|dttfrt|f||d | St|f||d | St|tr=|!|j|kr||} n|} | r| } | St|drt#|||| St|t$rU|!|j|kr||} n|} | r| } | tSt'|d d} | t| t(st d t j|}| |j|kr||}|tS) a Construct a record array from a wide-variety of objects. A general-purpose record array constructor that dispatches to the appropriate `recarray` creation function based on the inputs (see Notes). Parameters ---------- obj : any Input object. See Notes for details on how various input types are treated. dtype : data-type, optional Valid dtype for array. shape : int or tuple of ints, optional Shape of each array. offset : int, optional Position in the file or buffer to start reading from. strides : tuple of ints, optional Buffer (`buf`) is interpreted according to these strides (strides define how many bytes each array element, row, column, etc. occupy in memory). formats, names, titles, aligned, byteorder : If `dtype` is ``None``, these arguments are passed to `numpy.format_parser` to construct a dtype. See that function for detailed documentation. copy : bool, optional Whether to copy the input object (True), or to use a reference instead. This option only applies when the input is an ndarray or recarray. Defaults to True. Returns ------- np.recarray Record array created from the specified object. Notes ----- If `obj` is ``None``, then call the `~numpy.recarray` constructor. If `obj` is a string, then call the `fromstring` constructor. If `obj` is a list or a tuple, then if the first object is an `~numpy.ndarray`, call `fromarrays`, otherwise call `fromrecords`. If `obj` is a `~numpy.recarray`, then make a copy of the data in the recarray (if ``copy=True``) and use the new formats, names, and titles. If `obj` is a file, then call `fromfile`. Finally, if obj is an `ndarray`, then return ``obj.view(recarray)``, making a copy of the data if ``copy=True``. Examples -------- >>> a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> np.core.records.array(a) rec.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=int32) >>> b = [(1, 1), (2, 4), (3, 9)] >>> c = np.core.records.array(b, formats = ['i2', 'f2'], names = ('x', 'y')) >>> c rec.array([(1, 1.0), (2, 4.0), (3, 9.0)], dtype=[('x', '>> c.x rec.array([1, 2, 3], dtype=int16) >>> c.y rec.array([ 1.0, 4.0, 9.0], dtype=float16) >>> r = np.rec.array(['abc','def'], names=['col1','col2']) >>> print(r.col1) abc >>> r.col1 array('abc', dtype='>> r.col2 array('def', dtype='r s""F"""""" """"""......    *  (  GDDDDDDDDNKKKKKRWKKKjI,I,I,I,I,wI,I,I,X    K:>AE[[[[| KEI6:QQQQh KEIAEHHHHT K;??CaaaaH KGKGKQ"Q"Q"Q"Q"Q"r+