o 6aP@s8dZddlZddlZddlmZddlmZddlmZ ddlm Z ddl m Z dd lmZdd lmZgd Ze jZd d dd d dddd d dddddZe jZddZedGdddZGddde jZGdddeZddZ  d+ddZ  d+d d!Z  d,d"d#Zd$d%Z  d,d&d'Z!  (d-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)numeric) numerictypes) os_fspath) set_module)get_printoptions)recordrecarray format_parser fromarrays fromrecords fromstringfromfilearray><=s|)blnBLNSrrrrrIicCsddt|DS)z@Find duplication in a list, return a list of duplicated elementscSsg|] \}}|dkr|qSr).0itemcountsr!r!4/usr/lib/python3/dist-packages/numpy/core/records.py Qs z"find_duplicate..)ritems)listr!r!r%find_duplicateOs r)numpyc@s4eZdZdZd ddZd ddZdd Zd d ZdS)r a 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(['.f1cg|]}|dqSrr!r"keyfieldsr!r%r&cr:r r!r<r>r!r%r&r@) ValueError isinstancer(sbdtype enumerater?r1 _f_formats_offsetslen_nfields)r/r0r3rDkeysr!r>r%r,s   zformat_parser._parseFormatscCs|r,t|ttfvr nt|tr|d}ntdt|dd|d|jD|_ ng|_ |j ddt t |j |jD7_ t |j }|rOt d||r_dd|d|jD|_ng|_g}|jt |kr||jdg|jt |7_dSdS) zQconvert input field names into a list and assign to the _names attribute ,zillegal input names %scSg|]}|qSr!stripr"rr!r!r%r&z0format_parser._setfieldnames..NcSsg|]}d|qS)zf%dr!r"rr!r!r%r&rPzDuplicate field names: %scSrLr!rMrOr!r!r%r&rP)typer(tuplerBstrsplit NameErrorreprrI_namesrangerHr)rA_titles)r/r1r2_dupr!r!r%r-s*    "zformat_parser._setfieldnamescCsDt|j|j|j|jd}|durt|d}||}||_dS)N)r1r0offsetsr2r)rCrDrXrFrGrZ_byteorderconv newbyteorder)r/r4rDr!r!r%r.s   zformat_parser._createdtype)FN)F)__name__ __module__ __qualname____doc__r5r,r-r.r!r!r!r%r Xs  :  $r csTeZdZdZdZdZfddZfddZddZd d Zd d Zd dZ Z S)r zEA data-type scalar that allows field access as attribute lookup. r*cs tddkr |StSNlegacy1.13)r __str__super__repr__r/ __class__r!r%rhs zrecord.__repr__cs$tddkr t|StSrc)r rTr#rgrfrirjr!r%rfs  zrecord.__str__cCs|dvr tj||Sztj||WStyYnwtj|dj}||d}|rW|j|dd}z|j}Wn tyF|YSw|jdurU| |j |jfS|Std|)NsetfieldgetfieldrDrD%'record' object has no attribute '%s') ntvoid__getattribute__AttributeErrorr?getrnrDr1viewrk)r/attr fielddictresobjdtr!r!r%rss,     zrecord.__getattribute__cCsv|dvr td|tj|dj}||d}|r'|j|g|ddRSt||dr5tj|||Std|)NrlzCannot set '%s' attributerDrorp) rtrqrrrsr?rurmgetattr __setattr__)r/rwvalrxryr!r!r%r} s   zrecord.__setattr__cCs<tj||}t|tjr|jjdur||j|jfS|Sr+)rqrr __getitem__rBrDr1rvrkr/indxrzr!r!r%rszrecord.__getitem__cs@jj}tdd|D}d|fdd|D}d|S)zPretty-print all fields.css|]}t|VqdSr+)rHr"namer!r!r% (sz record.pprint..z %% %ds: %%scsg|] }|t|fqSr!)r|rfmtr/r!r%r&*r8z!record.pprint.. )rDr1maxjoin)r/r1maxlenrowsr!rr%pprint$s  z record.pprint) r_r`rarbrhrfrsr}rr __classcell__r!r!rjr%r s   r csbeZdZdZdZdZ   dddZd d Zd d Zd dZfddZddZ dddZ Z 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', '    zrecarray.__setattr__csjt|}t|tr3|jjdur-|t|}t|jjt j r+|j|jj|jfdS|S|jtdS|S)Nr)rR) rgrrBrrDr1rvrRrrqrrrrjr!r%rs    zrecarray.__getitem__cCs|j}|jjtust|jjtjs#|jturttj|f}d}d}nd}d}|jdks1|jdkr;tj |d|dd }nd t |jf}d d t |}t d dkrVd |}||||fS)Nz rec.array(zrec.array(%s,%sdtype=%s)zarray(z)array(%s,%sdtype=%s).view(numpy.recarray)rr;z, rK) separatorprefixsuffixz [], shape=%sr rdre) rDrRr rrqrrrCsizer array2stringrWrHr )r/ repr_dtyperrlstlfr!r!r%rhs" zrecarray.__repr__cCs|t|trt|dj}||}t|dj}||dd}|dur5|j|}|jjdur0|S|tS|j |g|RS)NrDro) rBintrrsr1r?rnrDrvrm)r/rwr~r1rxryrzr!r!r%field/s    zrecarray.field) NNrNNNNNFrr+) r_r`rarbrrrsr}rrhrrr!r!rjr%r 5sj " !!r cCs |dkrtjdtdddS|S)NrzPassing `shape=0` to have the shape be inferred is deprecated, and in future will be equivalent to `shape=(0,)`. To infer the shape and suppress this warning, pass `shape=None` instead. stacklevel)warningswarn FutureWarning)rr!r!r%_deprecate_shape_0_as_NoneAsrFcCs8dd|D}t|}|dur|dj}nt|tr|f}|dur,|dur,dd|D}|dur6t|}n t|||||j}|j} t|t|krNt d|dj} t| } | dkrb|d| }t |D]\} } || j } | jd| j | }||krt d| qft ||}t t|D] }|||| |<q|S)aCreate a record array from a (flat) list of arrays Parameters ---------- arrayList : list or tuple List of array-like objects (such as lists, tuples, and ndarrays). dtype : data-type, optional valid dtype for all arrays shape : int or tuple of ints, optional Shape of the resulting array. If not provided, inferred from ``arrayList[0]``. 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 given arrayList columns. Examples -------- >>> 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', '.NrcSsg|]}|jqSr!r)r"rzr!r!r%r&sz>mismatch between the number of fields and the number of arraysz array-shape mismatch in array %d)rrrBrrCrDr r1rHrArEndimr rY) arrayListrDrr0r1r2r3r4rrXd0nnkrz testshape_arrayrr!r!r%r Ms80       r c sD|dur(|dur(tj|tdfddtjdD}t|||||||dS|dur4tt|f} n t|||||j} z tj|| d} WnHt t fyt |}|dur[t |}t |trc|f}t |dkrmt dt|| } t| jD] } t|| | | <qwtjd td d | YSw|dur| j|kr|| _| 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', '.)r0rr1r2r3r4rzCan 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 errorror)rCrrrYrr rDr r rrArrHrBrr rrSrrrrv)recListrDrr0r1r2r3r4arrlistrretvalrrryr!rr%rs@+     rc Csx|dur |dur td|durt|} n t|||||j} | j} t|}|dvr2t||| }t|| ||d} | 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)rrCrDr itemsizerrHr ) datastringrDrrr0r1r2r3r4rrrr!r!r%rs7 rc Cs>|}z|dd||W||dS||dw)Nrro)tellseek)fdposr!r!r%get_remaining_sizeHs   rc Csl|dur |dur tdt|}|durd}nt|tr|f}t|dr)t|} ntt|d} | z}|dkr=||dt |} |durKt |} n t |||||j } | j } t |jtjd} | | }|dkrt|}| | ||d <t|}t |jtjd} | | }|| krtd t|| }||j}||krtd Wd|S1swY|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)rrrBrhasattrropenrrrrCrDr rrprodrqintpr(indexrSrAr rdataIOError)rrDrrr0r1r2r3r4ctxrrr shapeprod shapesizenbytesr nbytesreadr!r!r%rPsN-          rTc Cst|tdtfst|dr|dur|durtdi} |dur&t|}n|dur4t||||| j}n||||| d} |durQ|durHtdt|||||dSt|t rbt ||f||d| St|t t frt|dt t fr}t |f||d | St|f||d | St|tr|dur|j|kr||} n|} | r| } | St|drt||||d St|tr|dur|j|kr||} n|} | r| } | tSt|d d} | dust| tstd t|}|dur|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='sj#        U  ] T J c