1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
U
P±domã    @s&dZddlmZmZmZmZmZmZmZm    Z    ddlm
Z
ddl Z ddl Z ddl mZmZmZmZmZddlmZmZe jjjZe
jjZdddd    d
d gZd d ddgZd#dd„Zdd„Z Gdd„deƒZ!dd„Z"e!Z#d$dd„Zddddddddef    dd    „Zdd„Z$dd„Z%d%e j&d œd!d
„Z'd&d"d „Z(dS)'a,: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
 
é)ÚMAErrorÚ MaskedArrayÚmaskedÚnomaskÚ masked_arrayÚgetdataÚ getmaskarrayÚfilledN)Úbool_ÚdtypeÚndarrayÚrecarrayÚarray)Ú
fromarraysÚ fromrecordsÚ MaskedRecordsÚ    mrecarrayrrÚ fromtextfileÚaddfieldÚ_dataÚ_maskÚ
_fieldmaskr c    Csît|ƒ}dd„t|ƒDƒ}|dkr(|}nXt|ttfƒr<|}n$t|tƒrR| d¡}ntd|›ƒ‚t|ƒ}||kr€|||d…7}g}t|||j    ƒD]P\}}}|t
krÐ|dt
krÄ|  ||df¡qâ|  |¡q’|  ||df¡q’t   |¡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)úf%i©©Ú.0ÚirrúHd:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\numpy/ma/mrecords.pyÚ
<listcomp>6sz_checknames.<locals>.<listcomp>Nú,zillegal input names ré)ÚlenÚrangeÚ
isinstanceÚtupleÚlistÚstrÚsplitÚ    NameErrorÚzipÚdescrÚreserved_fieldsÚappendÚnpr )    r*ÚnamesZndescrÚ default_namesZ    new_namesZnnamesÚnÚdÚtrrrÚ _checknames-s(
   r3cCs:dd„|jjDƒ}tj|j|d}tdgt|ƒƒ|_|S)NcSsg|] }|df‘qS)z|b1r©rr0rrrrPsz"_get_fieldmask.<locals>.<listcomp>©r F)r r.r-ÚemptyÚshaper$r!Úflat)ÚselfÚmdescrZfdmaskrrrÚ_get_fieldmaskOsr;c@sÒeZdZdZdddddddddeddddfdd„Zdd    „Zed
d „ƒZed d „ƒZ    dd„Z
dd„Z dd„Z dd„Z dd„Zdd„Zdd„Zd,dd„Zdd„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    ˆƒƒ|_
n´tj | |d} | j |j krÄ|j| j}}|dkr˜t  | |j ¡} n,||kr°t | |j ¡} nd}t|||fƒ‚|sÚ| | ¡d|_n2| jˆkrê| }ntj ‡fdd„| Dƒˆd    }||_
|S)
N)    r ÚbufÚoffsetÚstridesÚformatsr.ÚtitlesÚ    byteorderÚalignedF©Úcopyr z?Mask and data not compatible: data size is %i, mask size is %i.Tcsg|]}t|gtˆƒƒ‘qSr©r$r!©rÚm©Úmdtyperrr‹sz)MaskedRecords.__new__.<locals>.<listcomp>r5)r Ú__new__ÚmaÚmake_mask_descrr rr-Úsizer$r!rrr7ÚresizeZreshaperÚ __setmask__Ú _sharedmask)Úclsr7r r<r=r>r?r.r@rArBÚmaskZ    hard_maskÚ
fill_valueZ    keep_maskrDÚoptionsr9ZndÚnmÚmsgrrrHrrJhs>ý  
 
ÿzMaskedRecords.__new__cs¤t|ddƒ}|dkrpt|dtƒ}t |d¡}|tkrFtj|j|d}n*t |¡‰t‡fdd„|Dƒˆd     t
¡}|j }|j |d|  |¡|dtkr t
|d<dS)Nrr r5csg|]}t|gtˆƒƒ‘qSrrErF©r:rrršsz4MaskedRecords.__array_finalize__.<locals>.<listcomp>©rÚ
_baseclass)Úgetattrrr Ú__getattribute__rKZmake_mask_noner7rLÚnarrayÚviewr Ú__dict__ÚupdateÚ _update_from)r9ÚobjrZobjmaskZ_dtypeÚ_dictrrWrÚ__array_finalize__s$   
ÿÿ 
 z MaskedRecords.__array_finalize__cCs t |t¡S)z2
        Returns the data as a recarray.
 
        )r r]r ©r9rrrr¤szMaskedRecords._datacCs|jS)z!
        Alias to mask.
 
        rXrdrrrr¬szMaskedRecords._fieldmaskcCs|jrt|jƒSt|jƒS)z%
        Returns the length
 
        )Úndimr!rr rdrrrÚ__len__´s
zMaskedRecords.__len__c
Cs–zt ||¡WStk
r"YnXt |d¡j}z||dd…}Wn6ttfk
r|}ztd|›ƒ|‚W5d}~XYnXt |d¡}t ||d¡}|j|Ž}|j    j
dk    r¸t dƒ‚d}|  dd¡}    |    dk    rz |    |}    Wnt k
ròYnXt|    j    ƒ}
|     t|
r|
fnd    f¡ ¡}|js*|rŠ| t¡}t|_d
|_|    |_|  d d¡} | dk    r’z| ||_Wntk
r†d|_YnXn| ¡}|S) Nr éúrecord array has no attribute r^rYz4MaskedRecords is currently limited tosimple records.FrrTÚ _fill_value)Úobjectr[ÚAttributeErrorr ÚfieldsÚ    TypeErrorÚKeyErrorr]Úgetfieldr r.ÚNotImplementedErrorÚgetÚ
IndexErrorr!ÚboolÚanyr7rrYZ_isfieldrriÚ
ValueErrorÚitem) r9ÚattrÚ    fielddictÚresÚeÚ
_localdictrraZ    hasmaskedrZtp_lenrirrrr[¿sPÿÿ 
 
 
 
 
zMaskedRecords.__getattribute__c
Cs|dkr| |¡dSt |d¡}||k}zt |||¡}WnHtk
r„t |d¡jp\i}t |d¡pli}||ks€||ks€‚YnNXt |d¡jp–i}||kr¤|S|rÒzt ||¡Wntk
rÐ|YSXz||dd…}Wn8tt    fk
r}    zt
d|›ƒ|    ‚W5d}    ~    XYnX|t krT|d}
|
dk    rJ|d|} n|} d    } nt |ƒ} t |ƒ} t |d
¡j| f|žŽ} |d  || ¡| S) z<
        Sets the attribute attr to the value val.
 
        )rRZ    fieldmaskNr^r Z_optinforgrhriTrr)rOrjr[Ú __setattr__Ú    Exceptionr rlÚ __delattr__rmrnrkrr    rÚsetfieldÚ __setitem__)r9rwÚvalr{ZnewattrÚretrxZoptinforyrzriÚdvalZmvalrarrrr|îsN
 
ÿÿ
 
zMaskedRecords.__setattr__cCs®|j}t |d¡}t ||d¡}t|tƒrx|| t¡}|||_d|_|d}|dk    rd|||_    |j
st|jrtt S|St j ||dd t¡}t j ||dd t¡|_|S)z„
        Returns all the fields sharing the same fieldname base.
 
        The fieldname base is either `_data` or `_mask`.
 
        rrYTriNFrC)r^r r[r]r#r&rrrPrirerr-rrr )r9Úindxr{rrraZfvalrrrÚ __getitem__&s  
 
 
 zMaskedRecords.__getitem__cCs,t |||¡t|tƒr(t |¡|j|<dS)z2
        Sets the given record to value.
 
        N)rr€r#r&rKrr)r9r„Úvaluerrrr€Fs
zMaskedRecords.__setitem__cszˆjdkr@dd„t‡fdd„ˆjjDƒŽDƒ}dd |¡›dSdd„t‡fd    d„ˆjjDƒƒDƒ}d
d |¡›d Sd S) z8
        Calculates the string representation.
 
        r cSs(g|] }dd dd„|Dƒ¡›d‘qS)ú(rcSsg|] }t|ƒ‘qSr©r&rrrrrUsú4MaskedRecords.__str__.<locals>.<listcomp>.<listcomp>ú)©Újoin©rÚsrrrrUsÿz)MaskedRecords.__str__.<locals>.<listcomp>csg|]}tˆ|ƒ‘qSr©rZ©rÚfrdrrrVsú[z, ú]cSs"g|]}d dd„|Dƒ¡›‘qS)rcSsg|] }t|ƒ‘qSrrˆrrrrrYsr‰r‹rrrrrYsÿcsg|]}tˆ|ƒ‘qSrrrrdrrrZsr‡rŠN)rMr)r r.rŒ)r9ZmstrrrdrÚ__str__Os
ÿÿzMaskedRecords.__str__csnˆjj}dtdd„|Dƒƒdf‰‡‡fdd„ˆjjDƒ}| dd¡| ˆdˆjfd    g¡td
 |¡ƒS) z6
        Calculates the repr representation.
 
        z %%%is : %%scSsg|] }t|ƒ‘qSr)r!r4rrrrcsz*MaskedRecords.__repr__.<locals>.<listcomp>écsg|]}ˆ|tˆ|ƒf‘qSrrr©Úfmtr9rrrdsrzmasked_records(z    fill_valuez              )Ú
)r r.ÚmaxÚinsertÚextendrSr&rŒ)r9Z_namesZreprstrrr–rÚ__repr__]s ÿzMaskedRecords.__repr__cCs
|dkr*|dkrt |¡}qÎt ||¡}n¤|dkrÀz(t|tƒrLt ||¡}n t ||¡}WqÎtk
r¼t |¡}|jdkr¦|jjd}|     ¡ ||¡}| 
|¡n t ||¡}d|_ YqÎXnt |||¡}t |dt ƒt k    rt |j¡}|j |t¡|_|j|j_|S)z3
        Returns a view of the mrecarray.
 
        Nrr)r r]Ú
issubclassrmr-r rlÚ    __class__Ú    __bases__Z    __array__r`rirZrrKrLrr7)r9r ÚtypeÚoutputÚbasetyperIrrrr]js. 
 
 
 
zMaskedRecords.viewcCs
d|_dS)z+
        Forces the mask to hard.
 
        TN©Z    _hardmaskrdrrrÚ harden_mask’szMaskedRecords.harden_maskcCs
d|_dS)z*
        Forces the mask to soft
 
        FNr£rdrrrÚ soften_mask™szMaskedRecords.soften_maskcCs$|j ¡ t|ƒ¡}|j ¡|_|S)z7
        Returns a copy of the masked record.
 
        )rrDr]r r)r9ÚcopiedrrrrD s 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``.
 
        Nr5)r    Útolistr\rjr)r9rSÚresultrRrrrr§©s     zMaskedRecords.tolistcCs,d|j|j|jj|j ¡|j ¡|jf}|S)zWReturn the internal state of the masked array.
 
        This is for pickling.
 
        r )r7r ÚflagsZfncrÚtobytesrri)r9ÚstaterrrÚ __getstate__¹súzMaskedRecords.__getstate__c
Cs^|\}}}}}}}t |||||f¡tdd„|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|]\}}|tf‘qSr©r
)rÚkÚ_rrrrÙsz.MaskedRecords.__setstate__.<locals>.<listcomp>rN)r Ú __setstate__r r*r^rS)
r9r«ÚverZshpÚtypZisfÚrawZmskZflvrIrrrr°És
zMaskedRecords.__setstate__cCst|j|jddf| ¡fS)z?
        Return a 3-tuple for pickling a MaskedArray.
 
        )rÚb)Ú_mrreconstructržrYr¬rdrrrÚ
__reduce__ÝsþzMaskedRecords.__reduce__)NN)N)Ú__name__Ú
__module__Ú __qualname__Ú__doc__rrJrcÚpropertyrrrfr[r|r…r€r”rœr]r¤r¥rDr§r¬r°r¶rrrrrVsBü
(
 
 /8     
(    
cCs4t |||¡ |¡}t t|d¡}|j||||dS)zK
    Build a new MaskedArray from the information stored in a pickle.
 
    Zb1)rRr )r rJr])ÚsubtypeZ    baseclassZ    baseshaper¢rrrrrrµçsrµFc    
Cs\dd„|Dƒ}    dd„|Dƒ}
t|    |||||||d t¡} 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©rÚxrrrrszfromarrays.<locals>.<listcomp>cSsg|]}t t|ƒ¡‘qSr)r-Z
atleast_1drr½rrrrs©r r7r?r.r@rBrAN)Ú recfromarraysr]rr%r)rr8rS) Z    arraylistr r7r?r.r@rBrArSZdatalistZmasklistZ_arrayrrrrøs"ýýc
 
Csàt|ddƒ}
t|tƒrDt|tƒr.| ¡ t¡}|dkr<|j}| ¡}t||||||||d t    ¡} |dk    rp|| _
|    t k    rÆt j |    dd}    t|    jƒ} | rž|    | j_n(|    jdkr¼dd„|    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.
 
    rNr¿FrCrgcSsg|] }t|ƒ‘qSr)r$rFrrrr\szfromrecords.<locals>.<listcomp>)rZr#r rr    r]r r§ÚrecfromrecordsrrSrr-rr!rr8rerO) Zreclistr r7r?r.r@rBrArSrRrZmrecZmaskrecordlengthrrrr#s8" 
 
 
þþ
 
 
 
cCsðg}t |¡}|jdkr"|d}n|jdkr4tdƒ‚|D]²}z t|ƒWnttfk
rØz t|ƒWnZttfk
rÂz t|ƒWn$ttfk
r¬| |j    ¡YnX| t     t¡¡YnX| t     t¡¡Yq8X| t     t¡¡q8|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.
 
    rgrzThe array should be 2D at most!)
r-ZasarrayreruÚintrmÚfloatÚcomplexr,r )ZarrÚvartypesr‘rrrÚ_guessvartypesds*
 
 
 
 
   rÆc
Csˆt|dƒr|Sz t|ƒ}Wn4tk
rN}ztd|›dƒ|‚W5d}~XYnX| ¡dd…dkrt| dd¡|S| ¡tdƒ‚dS)    z1
    Opens the file handle of file `fname`.
 
    ÚreadlinezNo such file: 'ú'Nrgz\xrzWow, binary file)ÚhasattrÚopenÚFileNotFoundErrorrÇÚseekÚcloserp)Úfnamer‘rzrrrÚopenfile‰s
 $ rÏú#Ú)Ú    delimitorcsZ|tjk    r.ˆdk    rtdƒ‚tjdtdd|‰t|ƒ}| ¡}|d| ˆ¡…     ¡}    |     
ˆ¡}
t |
ƒdkr6qnq6|dkrz|
}t ‡‡fdd„|Dƒƒ} | j \} } | ¡|dkrºt| d    ƒ}nNd
d„|Dƒ}t |ƒ| krd }|d 7}tj|t |ƒ| fddt| d    ƒ}d d„t||ƒDƒ}dd„|Dƒ}| j|k}dd„t| j|||ƒDƒ}t||dS)a®
    Creates a mrecarray from data stored in the file `filename`.
 
    Parameters
    ----------
    fname : {file name/handle}
        Handle of an opened file.
    delimiter : {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 lineNz;fromtextfile() got multiple values for argument 'delimiter'zThe 'delimitor' keyword argument of numpy.ma.mrecords.fromtextfile() is deprecated since NumPy 1.22.0, use 'delimiter' instead.rg)Ú
stacklevelr cs2g|]*}|dˆkrt|ƒdkr| ¡ ˆ¡‘qS)rr )r!Ústripr')rÚline©Ú commentcharÚ    delimiterrrrÐs ÿz fromtextfile.<locals>.<listcomp>rcSsg|]}t |¡‘qSr)r-r )rÚvrrrrÙsz&Attempting to %i dtypes for %i fields!z Reverting to default.cSsg|]\}}||f‘qSrr)rr0r‘rrrráscSsg|]}t |¡‘qSr)rKZdefault_fill_valuerrrrrâscSs$g|]\}}}}t||||d‘qS))rRr rS)r)rÚarGr2r‘rrrrçs
ÿr5)r-Ú_NoValuermÚwarningsÚwarnÚDeprecationWarningrÏrÇÚfindrÔr'r!rr7rÍrÆr)ÚTr)rÎrØr×Z missingcharÚvarnamesrÅrÒZftextrÕÚ    firstlineZ    _varnamesZ
_variablesr¯ZnfieldsrVr:ZmfillvrZ    _datalistrrÖrrsD
ý
 
 
ÿcsþ|j‰|j‰|dks|tkr*dtˆjƒ}t |¡}t ˆjj||jfg¡}t    ˆj
|ƒ‰‡‡fdd„ˆjj   ¡Dƒˆj |jfˆjj |žŽˆ t¡‰t dd„|jDƒ¡}t    ˆj
|ƒ‰‡‡fdd„ˆjj   ¡Dƒˆj t|ƒfˆjj |žŽˆˆ_ˆS)zÜAdds 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.
 
    Nrcs"g|]}ˆjˆj|Žf|žŽ‘qSr©rror)rÚnewdatarrrÿsÿzaddfield.<locals>.<listcomp>cSsg|] }|tf‘qSrr­r4rrrrscs"g|]}ˆjˆj|Žf|žŽ‘qSrrãr)rÚnewmaskrrr    sÿ)rrr+r!r rKrr-r*r r7rlÚvaluesrr]rr.r)ZmrecordZnewfieldZ newfieldnameZnewdtypeZ    newmdtyper)rrrärårrís,
 
ÿ
 
ÿ 
ÿ)N)NNNNNFNN)NrÐrÑNN)N))rºZnumpy.marrrrrrrr    rKrÜÚnumpyr-r
r r r rr\Znumpy.core.recordsrrÀrrÁÚcoreÚrecordsZ_byteorderconvZ_check_fill_valueÚ__all__r+r3r;rrµrrÆrÏrÛrrrrrrÚ<module>s^( 
þ 
"    þ
+þ
A%ÿþ P