B 9sz_getformats..N) hasattrjoinrdescrnpasarray_typestrtype issubclassntypesZflexiblerepritemsize)dataformatsobjrrr _getformats3s    r2c Cst|}ddt|D}|dkr(|}nZt|ttfr<|}n&t|trR|d}ntdt|t|}||kr|||d7}g}xbt |||j D]P\}}}|t kr|dt kr| ||dfq| |q| ||dfqWt |S)z Checks that field names ``descr`` are not reserved keywords. If this is the case, a default 'f%i' is substituted. If the argument `names` is not None, updates the field names to valid names. cSsg|] }d|qS)zf%ir)rirrr r!Nsz_checknames..Nrzillegal input names %srr)lenrange isinstancetupleliststrsplit NameErrorr-zipr&reserved_fieldsappendr'r) r&namesZndescrZ default_namesZ new_namesZnnamesndtrrr _checknamesEs(    rCcCs:dd|jjD}tj|j|d}tdgt||_|S)NcSsg|] }|dfqS)z|b1r)rr@rrr r!hsz"_get_fieldmask..)rF)rr?r'emptyshaper7r4flat)selfmdescrZfdmaskrrr _get_fieldmaskgsrIc@seZdZdZdddddddddeddddfddZdd Zd d Zeed Z d dZ ee d Z ddZ ddZ ddZddZddZddZddZd-ddZdd Zd!d"Zd#d$Zd.d%d&Zd'd(Zd)d*Zd+d,ZdS)/ra Attributes ---------- _data : recarray Underlying data, as a record array. _mask : boolean array Mask of the records. A record is masked when all its fields are masked. _fieldmask : boolean recarray Record array of booleans, setting the mask of each individual field of each record. _fill_value : record Filling values for each field. NrFTc stj|||||||||| | d }t|j| tks>t| sX|sVtdgt |_ ntj | |d} | j |j kr|j| j}}|dkrt | |j } n,||krt| |j } nd}t|||fd}|s|| d|_n2| jkr| }ntj fdd| Dd }||_ |S) N) rbufoffsetstridesr0r?titles byteorderalignedF)copyrz?Mask and data not compatible: data size is %i, mask size is %i.Tcsg|]}t|gtqSr)r7r4)rm)mdtyperr r!sz)MaskedRecords.__new__..)r)r __new__mamake_mask_descrrrr'sizer7r4rr rEZresizeZreshaper __setmask__ _sharedmask)clsrErrJrKrLr0r?rMrNrOmaskZ hard_mask fill_valueZ keep_maskrPZoptionsrGZndZnmmsgrr)rRr rSs6    zMaskedRecords.__new__cst|dd}|dkrpt|dt}t|d}|tkrFtj|j|d}n*t|tfdd|Dd t }|j }|j |d| ||dtkrt |d<dS)Nrr)rcsg|]}t|gtqSr)r7r4)rrQ)rHrr r!sz4MaskedRecords.__array_finalize__..)r _baseclass)getattrrr__getattribute__rTZmake_mask_nonerErUnarrayviewr __dict__update _update_from)rGr1rZobjmaskZ_dtypeZ_dictr)rHr __array_finalize__s       z MaskedRecords.__array_finalize__cCs t|tS)z2 Returns the data as a recarray. )rrar )rGrrr _getdataszMaskedRecords._getdata)fgetcCs|jS)z! Alias to mask. )r)rGrrr _getfieldmaskszMaskedRecords._getfieldmaskcCs|jrt|jSt|jS)z% Returns the length )ndimr4rr)rGrrr __len__s zMaskedRecords.__len__c Csry t||Stk r YnXt|dj}y||dd}Wn$ttfk rhtd|YnXt|d}t||d}|j|}|j jrt dd}| dd}|dk ry ||}Wnt k rYnX|t jt|j pd f}|js|rf|t}t|_d |_||_| d d} | dk rny| ||_Wntk rbd|_YnXn|}|S) Nrz record array has no attribute %srbr]z4MaskedRecords is currently limited tosimple records.FrrT _fill_value)objectr_AttributeErrorrfields TypeErrorKeyErrorragetfieldrNotImplementedErrorget IndexErrorr'boolr4anyrErr]Z_isfieldrrl ValueErroritem) rGattr fielddictres _localdictrr1Z hasmaskedrrlrrr r_sF        zMaskedRecords.__getattribute__c Cs|dkr||dSt|d}||k}yt|||}WnZt|djpTi}t|dpdi}||ks||kstdd\}} || Yn@Xt|djpi}||kr|S|ryt||Wn |Sy||dd} Wn&t t fk rt d|YnX|t krF|d} | dk r<|d|} n|} d } nt |} t|} t|d j| f| }|d || |S) z< Sets the attribute attr to the value val. )rZZ fieldmaskNrbrZ_optinforkz record array has no attribute %srlTrr)rWrmr_ __setattr__rrosysexc_info __delattr__rprqrnrrrsetfield __setitem__)rGrzvalr}ZnewattrZretr{Zoptinfoexctypevaluer|rlZdvalZmvalr1rrr r~sH    zMaskedRecords.__setattr__cCs|j}t|d}t||d}t|trx||t}|||_d|_|d}|dk rd|||_ |j st|jrtt S|St j ||ddt}t j ||ddt|_|S)z Returns all the fields sharing the same fieldname base. The fieldname base is either `_data` or `_mask`. rr]TrlNF)rP)rbrr_rar6rrrrXrlrirr'r rr )rGindxr}rrr1Zfvalrrr __getitem__?s      zMaskedRecords.__getitem__cCs,t|||t|tr(t||j|<dS)z2 Sets the given record to value. N)rrr6rrTrr)rGrrrrr r_s zMaskedRecords.__setitem__csrjdkr..)r%)rsrrr r!nsz)MaskedRecords.__str__..csg|]}t|qSr)r^)rf)rGrr r!osz[%s]z, cSs$g|]}dddd|DqS)z%srcSsg|] }t|qSr)r9)rr3rrr r!rsz4MaskedRecords.__str__...)r%)rrrrr r!rscsg|]}t|qSr)r^)rr)rGrr r!ssz(%s)N)rVr<rr?r%)rGZmstrr)rGr __str__hs zMaskedRecords.__str__csnjj}dtdd|DdffddjjD}|dd|djfd gtd |S) z6 Calculates the repr representation. z %%%is : %%scSsg|] }t|qSr)r4)rr@rrr r!|sz*MaskedRecords.__repr__..csg|]}|t|fqSr)r^)rr)fmtrGrr r!}srzmasked_records(z fill_valuez ) )rr?maxinsertextendr[r9r%)rGZ_namesZreprstrr)rrGr __repr__vs zMaskedRecords.__repr__cCs|dkr*|dkrt|}qt||}n|dkry,t|trPt||}d}n t||}Wqtk rt|}|jdkr|jjd}| ||}| |n t||}d|_ YqXnt|||}t |dt t k r t|j}|j|t|_|j|j_|S)z3 Returns a view of the mrecarray. Nrr)rrar+rpr'rro __class__ __bases__Z __array__rdrlr^rrTrUrrE)rGrr*outputbasetyperRrrr ras0          zMaskedRecords.viewcCs d|_dS)z+ Forces the mask to hard. TN) _hardmask)rGrrr harden_maskszMaskedRecords.harden_maskcCs d|_dS)z* Forces the mask to soft FN)r)rGrrr soften_maskszMaskedRecords.soften_maskcCs$|jt|}|j|_|S)z7 Returns a copy of the masked record. )rrPrar*r)rGZcopiedrrr rPs zMaskedRecords.copycCsH|dk r||St|td}t|j}d||<|S)a Return the data portion of the array as a list. Data items are converted to the nearest compatible Python type. Masked values are converted to fill_value. If fill_value is None, the corresponding entries in the output list will be ``None``. N)r)rtolistr`rmr)rGr[resultrZrrr rs zMaskedRecords.tolistcCs,d|j|j|jj|j|j|jf}|S)zWReturn the internal state of the masked array. This is for pickling. r)rErflagsZfncrtobytesrrl)rGstaterrr __getstate__szMaskedRecords.__getstate__c Cs^|\}}}}}}}t|||||ftdd|jjD} |jd|| ||f||_dS)at Restore the internal state of the masked array. This is for pickling. ``state`` is typically the output of the ``__getstate__`` output, and is a 5-tuple: - class name - a tuple giving the shape of the data - a typecode for the data - a binary string for the data - a binary string for the mask. cSsg|]\}}|tfqSr)r)rk_rrr r!sz.MaskedRecords.__setstate__..rN)r __setstate__rr&rbr[) rGrZverZshptypZisfrawZmskZflvrRrrr rs zMaskedRecords.__setstate__cCst|j|jddf|fS)z? Return a 3-tuple for pickling a MaskedArray. )rb)_mrreconstructrr]r)rGrrr __reduce__szMaskedRecords.__reduce__)NN)N)__name__ __module__ __qualname____doc__rrSrerfpropertyrrhrrjr_r~rrrrrarrrPrrrrrrrr rns4 %   -8   ) cCs4t||||}tt|d}|j||||dS)zK Build a new MaskedArray from the information stored in a pickle. Zb1)rZr)rrSra)ZsubtypeZ baseclassZ baseshaperrrrrr rsrFc Cs\dd|D} dd|D} t| |||||||dt} tt| | j_|dk rX|| _| S)a  Creates a mrecarray from a (flat) list of masked arrays. Parameters ---------- arraylist : sequence A list of (masked) arrays. Each element of the sequence is first converted to a masked array if needed. If a 2D array is passed as argument, it is processed line by line dtype : {None, dtype}, optional Data type descriptor. shape : {None, integer}, optional Number of records. If None, shape is defined from the shape of the first array in the list. formats : {None, sequence}, optional Sequence of formats for each individual field. If None, the formats will be autodetected by inspecting the fields and selecting the highest dtype possible. names : {None, sequence}, optional Sequence of the names of each field. fill_value : {None, sequence}, optional Sequence of data to be used as filling values. Notes ----- Lists of tuples should be preferred over lists of lists for faster processing. cSsg|] }t|qSr)r)rxrrr r!0szfromarrays..cSsg|]}tt|qSr)r'Z atleast_1dr)rrrrr r!1s)rrEr0r?rMrOrNN) recfromarraysrarr8r<rrFr[) Z arraylistrrEr0r?rMrOrNr[ZdatalistZmasklistZ_arrayrrr r sc Cst|dd} t|trDt|tr.|t}|dkr<|j}|}t||||||||dt } |dk rp|| _ | t k rt j | dd} t| j} | r| | j_n(| jdkrdd| D| j_n | | | dk r| | jdd<| S) aN Creates a MaskedRecords from a list of records. Parameters ---------- reclist : sequence A list of records. Each element of the sequence is first converted to a masked array if needed. If a 2D array is passed as argument, it is processed line by line dtype : {None, dtype}, optional Data type descriptor. shape : {None,int}, optional Number of records. If None, ``shape`` is defined from the shape of the first array in the list. formats : {None, sequence}, optional Sequence of formats for each individual field. If None, the formats will be autodetected by inspecting the fields and selecting the highest dtype possible. names : {None, sequence}, optional Sequence of the names of each field. fill_value : {None, sequence}, optional Sequence of data to be used as filling values. mask : {nomask, sequence}, optional. External mask to apply on the data. Notes ----- Lists of tuples should be preferred over lists of lists for faster processing. rN)rrEr0r?rMrOrNF)rPrkcSsg|] }t|qSr)r7)rrQrrr r!uszfromrecords..)r^r6rrrrarrrecfromrecordsrr[rr'r r4rrFrirW) ZreclistrrEr0r?rMrOrNr[rZrZmrecZmaskrecordlengthrrr r <s."        cCsg}t|}|jdkr"|d}n|jdkr4tdx|D]}y t|Wnttfk ry t|WnZttfk ry t|Wn$ttfk r||j YnX|t tYnX|t tYq:X|t tq:W|S)a. Tries to guess the dtypes of the str_ ndarray `arr`. Guesses by testing element-wise conversion. Returns a list of dtypes. The array is first converted to ndarray. If the array is 2D, the test is performed on the first line. An exception is raised if the file is 3D or more. rkrzThe array should be 2D at most!) r'r(rirxintrpfloatcomplexr>r)Zarrvartypesrrrr _guessvartypes}s*         rcCstt|dr|Sy t|}Wn tk r:td|YnX|dddkr`|dd|S|tddS)z1 Opens the file handle of file `fname`. readlinezNo such file: '%s'Nrkz\xrzWow, binary file)r$openIOErrorrseekclosers)fnamerrrr openfiles   r#r"cs.t|}x:|}|d|}|} t| dkr Pq W|dkrP| }tfdd|D} | j\} } ||dkrt | d}nLdd|D}t|| krd} | d7} t j | t|| fd d t | d}d dt ||D}d d|D}| j |k}d dt | j |||D}t||dS)a Creates a mrecarray from data stored in the file `filename`. Parameters ---------- fname : {file name/handle} Handle of an opened file. delimitor : {None, string}, optional Alphanumeric character used to separate columns in the file. If None, any (group of) white spacestring(s) will be used. commentchar : {'#', string}, optional Alphanumeric character used to mark the start of a comment. missingchar : {'', string}, optional String indicating missing data, and used to create the masks. varnames : {None, sequence}, optional Sequence of the variable names. If None, a list will be created from the first non empty line of the file. vartypes : {None, sequence}, optional Sequence of the variables dtypes. If None, it will be estimated from the first non-commented line. Ultra simple: the varnames are in the header, one lineNrcs2g|]*}|dkrt|dkr|qS)rr)r4stripr:)rline) commentchar delimitorrr r!sz fromtextfile..rcSsg|]}t|qSr)r'r)rvrrr r!sz&Attempting to %i dtypes for %i fields!z Reverting to default.rk) stacklevelcSsg|]\}}||fqSrr)rr@rrrr r!scSsg|]}t|qSr)rTZdefault_fill_value)rrrrr r!scSs$g|]\}}}}t||||dqS))rZrr[)r)rarQrBrrrr r!s)r)rrfindrr:r4rrErrwarningswarnr<Tr )rrrZ missingcharZvarnamesrZftextr firstlineZ _varnamesZ _variablesrZnfieldsr\rHZmfillvrZ _datalistr)rrr rs4      cs|j|j|dks|tkr*dtj}t|}tjj||jfg}t j |fddjj Dj |jfjj |ttdd|jD}t j |fddjj Dj t|fjj |_S)zAdds a new field to the masked record array Uses `newfield` as data and `newfieldname` as name. If `newfieldname` is None, the new field name is set to 'fi', where `i` is the number of existing fields. Nzf%ics"g|]}jj|f|qSr)rrr)rr)rnewdatarr r! szaddfield..cSsg|] }|tfqSr)r)rr@rrr r!scs"g|]}jj|f|qSr)rrr)rr)rnewmaskrr r!s)rrr=r4rrTr r'r&r rErovaluesrrarr?r)ZmrecordZnewfieldZ newfieldnameZnewdtypeZ newmdtyper)rrrrr rs&       )N)NNNNNFNN)Nrr"NN)N)5rZ __future__rrrrrZnumpyr'Znumpy.core.numerictypesZcoreZ numerictypesr,Z numpy.compatrrrrr r r`Znumpy.core.recordsr rr rZrecordsZ_byteorderconvr)Znumpy.marTr rrrrrrrZ_check_fill_value__all__r=r2rCrIrmrrrrrrrrrrr  sH   (  "  )?% C