2[c @`sdZddlmZmZmZddlZddlZddlZddl j j Z ddl mZddlmZmZmZmZmZddlmZmZej jjZe jZddljZddlmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&ej j'Z'dd d d d d gZ(ddddgZ)dZ*e+dZ,dZ-de e.fdYZ/dZ0e/Z1e+e+e+e+e+e2e+e+dZe+e+e+e+e+e2e+e+e"d ZdZ3dZ4e+dde+e+dZ5e+dZ6dS(s,:mod:`numpy.ma..mrecords` Defines the equivalent of :class:`numpy.recarrays` for masked arrays, where fields can be accessed as attributes. Note that :class:`numpy.ma.MaskedArray` already supports structured datatypes and the masking of individual fields. .. moduleauthor:: Pierre Gerard-Marchant i(tdivisiontabsolute_importtprint_functionN(t basestring(tbool_tdtypetndarraytrecarraytarray(t fromarrayst fromrecords(tMAErrort MaskedArraytmaskedtnomaskt masked_arraytgetdatat getmaskarraytfilledt MaskedRecordst mrecarrayR R t fromtextfiletaddfieldt_datat_maskt _fieldmaskRcC`st|dr9djg|jjD]}|d^q"Sd}xi|D]a}tj|}|t|jj7}t|jjt j r|t |j 7}n|d7}qFW|d S(sR Returns the formats of arrays in arraylist as a comma-separated string. Rt,iti( thasattrtjoinRtdescrtnptasarrayt_typestrttypet issubclasstntypestflexibletreprtitemsize(tdatatdesctformatstobj((s0/tmp/pip-build-fiC0ax/numpy/numpy/ma/mrecords.pyt _getformats3s* c C`s[t|}gt|D]}d|^q}|dkrD|}n~t|ttfrb|}n7t|tr|jd}ntdt |t|}||kr|||7}ng}xt |||j D]l\}}} |t kr3| dt kr#|j || dfqJ|j | q|j || dfqWtj|S(s 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. sf%iRsillegal input names %siiN(tlentrangetNonet isinstancettupletlisttstrtsplitt NameErrorR&tzipRtreserved_fieldstappendRR( Rtnamestndescrtit default_namest new_namestnnamestntdtt((s0/tmp/pip-build-fiC0ax/numpy/numpy/ma/mrecords.pyt _checknamesEs( #     % cC`s]g|jjD]}|df^q }tj|jd|}ttgt||_|S(Ns|b1R( RR9RtemptytshapeR1tFalseR-tflat(tselfR?tmdescrtfdmask((s0/tmp/pip-build-fiC0ax/numpy/numpy/ma/mrecords.pyt_get_fieldmaskgs%cB`s eZdZddddddddeeedeedZdZdZ e de Z dZ e de Z dZdZd Zd Zd Zd Zd ZdddZdZdZdZddZdZdZdZRS(s 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. icK`stj||d|d|d|d|d|d|d|d| d |  }tj|j}| tksvtj|  r|stt gt ||_ qn&tj | d |} | j |j krI|j| j}}|d krtj| |j } nD||kr tj| |j } n d d }t|||ft}n|sh|j| t|_n\| j|kr| }n;tj g| D]}t|gt |^qd|}||_ |S(NRtbuftoffsettstridesR*R9ttitlest byteordertalignedtcopyis/Mask and data not compatible: data size is %i, smask size is %i.(Rt__new__tmatmake_mask_descrRRRtsizeR1RER-RRRDtresizetreshapeR tTruet __setmask__t _sharedmask(tclsRDRRKRLRMR*R9RNRORPtmaskt hard_maskt fill_valuet keep_maskRQtoptionsRGtmdtypetndtnmtmsgRtm((s0/tmp/pip-build-fiC0ax/numpy/numpy/ma/mrecords.pyRRs8!  "      2  cC`st|dd}|dkrt|dt}tj|d}|tkritj|jd|}qtj|}t g|D]}t |gt |^qd|j t }n|j}|jd||j||dtkrt |dy||}Wntk rnX|jtt|j p,dfj}n|jsM|r|jt}t|_t|_||_|j d d} | dk ry| ||_Wqtk rd|_qXqn |j}|S( NRis record array has no attribute %sRlRfs4MaskedRecords is currently limited tosimple records.Rit _fill_value(tobjectRhtAttributeErrorRtfieldst TypeErrortKeyErrorRktgetfieldRtNotImplementedErrorREtgetR/t IndexErrortboolR-tanyRDR RfRXt_isfieldRRxt ValueErrortitem( RGtattrt fielddicttrest _localdictRR+t hasmaskedRRx((s0/tmp/pip-build-fiC0ax/numpy/numpy/ma/mrecords.pyRhsF    -      cC`s|d kr|j|dStj|d}||k}ytj|||}Wntk rtj|djp|i}tj|dpi}||kp||ks5tjd \}} || q5n^Xtj|djpi}||kr|S|r5ytj ||Wq5tk r1|SXny||d } Wn't t fk rpt d|nX|t kr|d } | dk r|d |} n|} t} nt|} t|} tj|d j| | }|d j|| |S( s< Sets the attribute attr to the value val. R\t fieldmaskNRlRt_optinfois record array has no attribute %sRxRR(R\R(RYRyRht __setattr__t ExceptionRR{tsystexc_infot __delattr__R|R}RzR R/RXRRtsetfieldt __setitem__(RGRtvalRtnewattrtretRtoptinfotexctypetvalueRRxtdvaltmvalR+((s0/tmp/pip-build-fiC0ax/numpy/numpy/ma/mrecords.pyRsH            cC`s|j}tj|d}tj||d}t|tr||jt}|||_t|_ |d}|dk r|||_ n|j r|jrt S|Stj||dtjt}tj||dtjt|_|S(s Returns all the fields sharing the same fieldname base. The fieldname base is either `_data` or `_mask`. RRfRxRQN(RlRRhRkR0RR RRXRZR/RxRvR RRRERR(RGtindxRRRR+tfval((s0/tmp/pip-build-fiC0ax/numpy/numpy/ma/mrecords.pyt __getitem__?s      "%cC`s?tj|||t|tr;tj||j|}ddj|Sgtg|jjD]}t||^qD]2}ddjg|D]}t|^q^q}ddj|SdS(s8 Calculates the string representation. is(%s)Rs[%s]s, s%sN(RUR6RR9RgRR3(RGtftsR;tmstr((s0/tmp/pip-build-fiC0ax/numpy/numpy/ma/mrecords.pyt__str__hsddcC`s|jj}dtg|D]}t|^qdf}g|jjD]}||t||f^qI}|jdd|j|d|jfdgtdj |S(s6 Calculates the repr representation. s %%%is : %%siismasked_records(s fill_values )s ( RR9tmaxR-RgtinserttextendR^R3R(RGt_namesR?tfmtRtreprstr((s0/tmp/pip-build-fiC0ax/numpy/numpy/ma/mrecords.pyt__repr__vs 02 cC`s|dkr?|dkr*tj|}q(tj||}n|dkry@t|trxtj||}d}ntj||}Wq(tk rtj|}|jdkr|jj d}|j j||}|j |ntj||}d|_ q(Xntj|||}t |dttk r|tj|j}|jj|t|_|j|j_n|S(s3 Returns a view of the mrecarray. iRN(R/RRkR#R|RRR{t __class__t __bases__t __array__RnRxRgRRSRTRRD(RGRR"toutputtbasetypeRa((s0/tmp/pip-build-fiC0ax/numpy/numpy/ma/mrecords.pyRks0     cC`s t|_dS(s+ Forces the mask to hard. N(RXt _hardmask(RG((s0/tmp/pip-build-fiC0ax/numpy/numpy/ma/mrecords.pyt harden_maskscC`s t|_dS(s* Forces the mask to soft N(RER(RG((s0/tmp/pip-build-fiC0ax/numpy/numpy/ma/mrecords.pyt soften_maskscC`s4|jjjt|}|jj|_|S(s7 Returns a copy of the masked record. (RRQRkR"R(RGtcopied((s0/tmp/pip-build-fiC0ax/numpy/numpy/ma/mrecords.pyRQscC`sf|dk r|j|jSt|jjdt}t|jj}d||<|jS(s 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``. RN(R/RttolistRjRyR(RGR^tresultR\((s0/tmp/pip-build-fiC0ax/numpy/numpy/ma/mrecords.pyRs  cC`s@d|j|j|jj|jj|jj|jf}|S(sWReturn the internal state of the masked array. This is for pickling. i(RDRtflagstfncRttobytesRRx(RGtstate((s0/tmp/pip-build-fiC0ax/numpy/numpy/ma/mrecords.pyt __getstate__s    c C`s|\}}}}}}}tj|||||ftg|jjD]\} } | tf^qG} |jdj|| ||f||_dS(st 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. RN(Rt __setstate__RRRRlR^( RGRtvertshpttyptisftrawtmsktflvtkt_Ra((s0/tmp/pip-build-fiC0ax/numpy/numpy/ma/mrecords.pyRs 1 cC`s%t|j|jddf|jfS(s? Return a 3-tuple for pickling a MaskedArray. itb(i(t_mrreconstructRRfR(RG((s0/tmp/pip-build-fiC0ax/numpy/numpy/ma/mrecords.pyt __reduce__sN(t__name__t __module__t__doc__R/RERRXRRRrRstpropertyRRuRRwRhRRRRRRkRRRQRRRR(((s0/tmp/pip-build-fiC0ax/numpy/numpy/ma/mrecords.pyRns4    %    - 8  )     cC`sOtj|||j|}tjt|d}|j||d|d|S(sK Build a new MaskedArray from the information stored in a pickle. tb1R\R(RRRRk(tsubtypet baseclasst baseshapeRRR((s0/tmp/pip-build-fiC0ax/numpy/numpy/ma/mrecords.pyRsc C`sg|D]} t| ^q} g|D]} tjt| ^q&} t| d|d|d|d|d|d|d|jt} tt| | j _ |dk r|| _ n| S( s  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. RRDR*R9RNRPRON( RRt atleast_1dRt recfromarraysRkRR2R6RRFR/R^( t arraylistRRDR*R9RNRPROR^txtdatalisttmasklistt_array((s0/tmp/pip-build-fiC0ax/numpy/numpy/ma/mrecords.pyR s(   c C`sot|dd } t|trot|trH|jjt}n|d kr`|j}n|j}nt |d|d|d|d|d|d|d|jt } |d k r|| _ n| t k rRt j| d t} t| j} | r | | j_qR| jd krBg| D]} t| ^q!| j_qR| j| n| d k rk| | j(n| S( sN 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. RRRDR*R9RNRPRORQiN(RgR/R0RR RRkRRtrecfromrecordsRR^RRRRER-RRFRvR1RY(treclistRRDR*R9RNRPROR^R\RtmrectmaskrecordlengthRe((s0/tmp/pip-build-fiC0ax/numpy/numpy/ma/mrecords.pyR <s."      (  cC`s.g}tj|}|jdkr1|d}n|jdkrOtdnx|D]}yt|Wnttfk ryt|Wnettfk ryt|Wn'ttfk r|j|j q X|jtj tq&X|jtj tqVX|jtj tqVW|S(s. 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. iisThe array should be 2D at most!( RR RvRtintR|tfloattcomplexR8R(tarrtvartypesR((s0/tmp/pip-build-fiC0ax/numpy/numpy/ma/mrecords.pyt_guessvartypes}s*   cC`st|dr|Syt|}Wn!tk rFtd|nX|jd dkrq|jdd|S|jtddS(s1 Opens the file handle of file `fname`. treadlinesNo such file: '%s'is\xisWow, binary fileN(RtopentIOErrorRtseektcloseR(tfnameR((s0/tmp/pip-build-fiC0ax/numpy/numpy/ma/mrecords.pytopenfiles  t#Rc C`s8t|}xTtrb|j}||j| j}|j|} t| dkrPqqW|d krx| }ntg|D]=}|d|krt|dkr|jj|^q} | j \} } |j |d krt | d}nzg|D]} t j | ^q}t|| krzd}|d7}tj|t|| fddt | d}ngt||D]\}}||f^q}g|D]}tj|^q}| j|k}gt| j|||D]0\}}}}t|d|d|d |^q}t|d|S( s 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 lineiis&Attempting to %i dtypes for %i fields!s Reverting to default.t stackleveliR\RR^N(RRXRtfindtstripR4R-R/RRDRRRRtwarningstwarnR6RStdefault_fill_valuetTR (Rt delimitort commentchart missingchartvarnamesRtftexttlinet firstlinet _varnamest _variablesRtnfieldstvRdR?RRHtmfillvRtaReRAt _datalist((s0/tmp/pip-build-fiC0ax/numpy/numpy/ma/mrecords.pyRs6     @  " #."Lc C`s|j}|j}|dks*|tkr@dt|j}ntj|}tj|jj ||jfg}t |j |}g|jj j D]!}|j|j||^q|j|j|jj ||jt}tjg|jD]}|tf^q} t |j | } g|jj j D]!}| j|j||^q:| jt|| jj || |_|S(sAdds 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. sf%iN(RRR/R7R-RRSRRRRRDR{tvaluesRR~RkRR9RR( tmrecordtnewfieldt newfieldnameRRtnewdtypetnewdataRR?t newmdtypetnewmask((s0/tmp/pip-build-fiC0ax/numpy/numpy/ma/mrecords.pyRs&  %5+5 (7Rt __future__RRRRRtnumpyRtnumpy.core.numerictypestcoret numerictypesR$t numpy.compatRRRRRRRjtnumpy.core.recordsR RR Rtrecordst_byteorderconvR!tnumpy.maRSR R R RRRRRt_check_fill_valuet__all__R7R,R/RBRJRyRRRRERRRR(((s0/tmp/pip-build-fiC0ax/numpy/numpy/ma/mrecords.pyt sD   ( :     "     )  ? %  C