@@ -857,6 +857,7 @@ def __str__ (self):
857
857
#####--------------------------------------------------------------------------
858
858
#---- --- Mask creation functions ---
859
859
#####--------------------------------------------------------------------------
860
+
860
861
def _recursive_make_descr (datatype , newtype = bool_ ):
861
862
"Private function allowing recursion in make_descr."
862
863
# Do we have some name fields ?
@@ -1134,6 +1135,7 @@ def masked_where(condition, a, copy=True):
1134
1135
result ._mask = cond
1135
1136
return result
1136
1137
1138
+
1137
1139
def masked_greater (x , value , copy = True ):
1138
1140
"""
1139
1141
Return the array `x` masked where (x > value).
@@ -1142,22 +1144,27 @@ def masked_greater(x, value, copy=True):
1142
1144
"""
1143
1145
return masked_where (greater (x , value ), x , copy = copy )
1144
1146
1147
+
1145
1148
def masked_greater_equal (x , value , copy = True ):
1146
1149
"Shortcut to masked_where, with condition = (x >= value)."
1147
1150
return masked_where (greater_equal (x , value ), x , copy = copy )
1148
1151
1152
+
1149
1153
def masked_less (x , value , copy = True ):
1150
1154
"Shortcut to masked_where, with condition = (x < value)."
1151
1155
return masked_where (less (x , value ), x , copy = copy )
1152
1156
1157
+
1153
1158
def masked_less_equal (x , value , copy = True ):
1154
1159
"Shortcut to masked_where, with condition = (x <= value)."
1155
1160
return masked_where (less_equal (x , value ), x , copy = copy )
1156
1161
1162
+
1157
1163
def masked_not_equal (x , value , copy = True ):
1158
1164
"Shortcut to masked_where, with condition = (x != value)."
1159
1165
return masked_where (not_equal (x , value ), x , copy = copy )
1160
1166
1167
+
1161
1168
def masked_equal (x , value , copy = True ):
1162
1169
"""
1163
1170
Shortcut to masked_where, with condition = (x == value). For
@@ -1171,6 +1178,7 @@ def masked_equal(x, value, copy=True):
1171
1178
# return array(d, mask=m, copy=copy)
1172
1179
return masked_where (equal (x , value ), x , copy = copy )
1173
1180
1181
+
1174
1182
def masked_inside (x , v1 , v2 , copy = True ):
1175
1183
"""
1176
1184
Shortcut to masked_where, where ``condition`` is True for x inside
@@ -1188,6 +1196,7 @@ def masked_inside(x, v1, v2, copy=True):
1188
1196
condition = (xf >= v1 ) & (xf <= v2 )
1189
1197
return masked_where (condition , x , copy = copy )
1190
1198
1199
+
1191
1200
def masked_outside (x , v1 , v2 , copy = True ):
1192
1201
"""
1193
1202
Shortcut to ``masked_where``, where ``condition`` is True for x outside
@@ -1205,7 +1214,7 @@ def masked_outside(x, v1, v2, copy=True):
1205
1214
condition = (xf < v1 ) | (xf > v2 )
1206
1215
return masked_where (condition , x , copy = copy )
1207
1216
1208
- #
1217
+
1209
1218
def masked_object (x , value , copy = True , shrink = True ):
1210
1219
"""
1211
1220
Mask the array `x` where the data are exactly equal to value.
@@ -1234,6 +1243,7 @@ def masked_object(x, value, copy=True, shrink=True):
1234
1243
mask = mask_or (mask , make_mask (condition , shrink = shrink ))
1235
1244
return masked_array (x , mask = mask , copy = copy , fill_value = value )
1236
1245
1246
+
1237
1247
def masked_values (x , value , rtol = 1.e-5 , atol = 1.e-8 , copy = True , shrink = True ):
1238
1248
"""
1239
1249
Mask the array x where the data are approximately equal in
@@ -1271,6 +1281,7 @@ def masked_values(x, value, rtol=1.e-5, atol=1.e-8, copy=True, shrink=True):
1271
1281
mask = mask_or (mask , make_mask (condition , shrink = shrink ))
1272
1282
return masked_array (xnew , mask = mask , copy = copy , fill_value = value )
1273
1283
1284
+
1274
1285
def masked_invalid (a , copy = True ):
1275
1286
"""
1276
1287
Mask the array for invalid values (NaNs or infs).
@@ -1292,6 +1303,7 @@ def masked_invalid(a, copy=True):
1292
1303
#####--------------------------------------------------------------------------
1293
1304
#---- --- Printing options ---
1294
1305
#####--------------------------------------------------------------------------
1306
+
1295
1307
class _MaskedPrintOption :
1296
1308
"""
1297
1309
Handle the string used to represent missing data in a masked array.
@@ -1372,6 +1384,20 @@ def _recursive_printoption(result, mask, printopt):
1372
1384
#---- --- MaskedArray class ---
1373
1385
#####--------------------------------------------------------------------------
1374
1386
1387
+ def _recursive_filled (a , mask , fill_value ):
1388
+ """
1389
+ Recursively fill `a` with `fill_value`.
1390
+ Private function
1391
+ """
1392
+ names = a .dtype .names
1393
+ for name in names :
1394
+ current = a [name ]
1395
+ print "Name: %s : %s" % (name , current )
1396
+ if current .dtype .names :
1397
+ _recursive_filled (current , mask [name ], fill_value [name ])
1398
+ else :
1399
+ np .putmask (current , mask [name ], fill_value [name ])
1400
+
1375
1401
#...............................................................................
1376
1402
class _arraymethod (object ):
1377
1403
"""
@@ -2013,6 +2039,7 @@ def _getrecordmask(self):
2013
2039
try :
2014
2040
return _mask .view ((bool_ , len (self .dtype ))).all (axis )
2015
2041
except ValueError :
2042
+ # In case we have nested fields...
2016
2043
return np .all ([[f [n ].all () for n in _mask .dtype .names ]
2017
2044
for f in _mask ], axis = axis )
2018
2045
@@ -2106,6 +2133,7 @@ def set_fill_value(self, value=None):
2106
2133
fill_value = property (fget = get_fill_value , fset = set_fill_value ,
2107
2134
doc = "Filling value." )
2108
2135
2136
+
2109
2137
def filled (self , fill_value = None ):
2110
2138
"""Return a copy of self._data, where masked values are filled
2111
2139
with fill_value.
@@ -2140,9 +2168,10 @@ def filled(self, fill_value=None):
2140
2168
#
2141
2169
if m .dtype .names :
2142
2170
result = self ._data .copy ()
2143
- for n in result .dtype .names :
2144
- field = result [n ]
2145
- np .putmask (field , self ._mask [n ], fill_value [n ])
2171
+ _recursive_filled (result , self ._mask , fill_value )
2172
+ # for n in result.dtype.names:
2173
+ # field = result[n]
2174
+ # np.putmask(field, self._mask[n], fill_value[n])
2146
2175
elif not m .any ():
2147
2176
return self ._data
2148
2177
else :
@@ -2287,6 +2316,58 @@ def __repr__(self):
2287
2316
return _print_templates ['short' ] % parameters
2288
2317
return _print_templates ['long' ] % parameters
2289
2318
#............................................
2319
+ def __eq__ (self , other ):
2320
+ "Check whether other equals self elementwise"
2321
+ omask = getattr (other , '_mask' , nomask )
2322
+ if omask is nomask :
2323
+ check = ndarray .__eq__ (self .filled (0 ), other ).view (type (self ))
2324
+ check ._mask = self ._mask
2325
+ else :
2326
+ odata = filled (other , 0 )
2327
+ check = ndarray .__eq__ (self .filled (0 ), odata ).view (type (self ))
2328
+ if self ._mask is nomask :
2329
+ check ._mask = omask
2330
+ else :
2331
+ mask = mask_or (self ._mask , omask )
2332
+ if mask .dtype .names :
2333
+ if mask .size > 1 :
2334
+ axis = 1
2335
+ else :
2336
+ axis = None
2337
+ try :
2338
+ mask = mask .view ((bool_ , len (self .dtype ))).all (axis )
2339
+ except ValueError :
2340
+ mask = np .all ([[f [n ].all () for n in mask .dtype .names ]
2341
+ for f in mask ], axis = axis )
2342
+ check ._mask = mask
2343
+ return check
2344
+ #
2345
+ def __ne__ (self , other ):
2346
+ "Check whether other doesn't equal self elementwise"
2347
+ omask = getattr (other , '_mask' , nomask )
2348
+ if omask is nomask :
2349
+ check = ndarray .__ne__ (self .filled (0 ), other ).view (type (self ))
2350
+ check ._mask = self ._mask
2351
+ else :
2352
+ odata = filled (other , 0 )
2353
+ check = ndarray .__ne__ (self .filled (0 ), odata ).view (type (self ))
2354
+ if self ._mask is nomask :
2355
+ check ._mask = omask
2356
+ else :
2357
+ mask = mask_or (self ._mask , omask )
2358
+ if mask .dtype .names :
2359
+ if mask .size > 1 :
2360
+ axis = 1
2361
+ else :
2362
+ axis = None
2363
+ try :
2364
+ mask = mask .view ((bool_ , len (self .dtype ))).all (axis )
2365
+ except ValueError :
2366
+ mask = np .all ([[f [n ].all () for n in mask .dtype .names ]
2367
+ for f in mask ], axis = axis )
2368
+ check ._mask = mask
2369
+ return check
2370
+ #
2290
2371
def __add__ (self , other ):
2291
2372
"Add other to self, and return a new masked array."
2292
2373
return add (self , other )
0 commit comments