@@ -45,9 +45,8 @@ def issequence(seq):
45
45
Is seq a sequence (ndarray, list or tuple)?
46
46
47
47
"""
48
- if isinstance (seq , (ndarray , tuple , list )):
49
- return True
50
- return False
48
+ return isinstance (seq , (ndarray , tuple , list ))
49
+
51
50
52
51
def count_masked (arr , axis = None ):
53
52
"""
@@ -103,6 +102,7 @@ def count_masked(arr, axis=None):
103
102
m = getmaskarray (arr )
104
103
return m .sum (axis )
105
104
105
+
106
106
def masked_all (shape , dtype = float ):
107
107
"""
108
108
Empty masked array with all elements masked.
@@ -154,6 +154,7 @@ def masked_all(shape, dtype=float):
154
154
mask = np .ones (shape , make_mask_descr (dtype )))
155
155
return a
156
156
157
+
157
158
def masked_all_like (arr ):
158
159
"""
159
160
Empty masked array with the properties of an existing array.
@@ -221,6 +222,9 @@ class _fromnxfunction:
221
222
as the wrapped NumPy function. The docstring of `newfunc` is adapted from
222
223
the wrapped function as well, see `getdoc`.
223
224
225
+ This class should not be used directly. Instead, one of its extensions that
226
+ provides support for a specific type of input should be used.
227
+
224
228
Parameters
225
229
----------
226
230
funcname : str
@@ -261,48 +265,100 @@ def getdoc(self):
261
265
return
262
266
263
267
def __call__ (self , * args , ** params ):
268
+ pass
269
+
270
+
271
+ class _fromnxfunction_single (_fromnxfunction ):
272
+ """
273
+ A version of `_fromnxfunction` that is called with a single array
274
+ argument followed by auxiliary args that are passed verbatim for
275
+ both the data and mask calls.
276
+ """
277
+ def __call__ (self , x , * args , ** params ):
264
278
func = getattr (np , self .__name__ )
265
- if len (args ) == 1 :
266
- x = args [0 ]
267
- if isinstance (x , ndarray ):
268
- _d = func (x .__array__ (), ** params )
269
- _m = func (getmaskarray (x ), ** params )
270
- return masked_array (_d , mask = _m )
271
- elif isinstance (x , tuple ) or isinstance (x , list ):
272
- _d = func (tuple ([np .asarray (a ) for a in x ]), ** params )
273
- _m = func (tuple ([getmaskarray (a ) for a in x ]), ** params )
274
- return masked_array (_d , mask = _m )
275
- else :
276
- _d = func (np .asarray (x ), ** params )
277
- _m = func (getmaskarray (x ), ** params )
278
- return masked_array (_d , mask = _m )
279
+ if isinstance (x , ndarray ):
280
+ _d = func (x .__array__ (), * args , ** params )
281
+ _m = func (getmaskarray (x ), * args , ** params )
282
+ return masked_array (_d , mask = _m )
279
283
else :
280
- arrays = []
281
- args = list (args )
282
- while len (args ) > 0 and issequence (args [0 ]):
283
- arrays .append (args .pop (0 ))
284
- res = []
285
- for x in arrays :
286
- _d = func (np .asarray (x ), * args , ** params )
287
- _m = func (getmaskarray (x ), * args , ** params )
288
- res .append (masked_array (_d , mask = _m ))
289
- return res
290
-
291
- atleast_1d = _fromnxfunction ('atleast_1d' )
292
- atleast_2d = _fromnxfunction ('atleast_2d' )
293
- atleast_3d = _fromnxfunction ('atleast_3d' )
294
- #atleast_1d = np.atleast_1d
295
- #atleast_2d = np.atleast_2d
296
- #atleast_3d = np.atleast_3d
297
-
298
- vstack = row_stack = _fromnxfunction ('vstack' )
299
- hstack = _fromnxfunction ('hstack' )
300
- column_stack = _fromnxfunction ('column_stack' )
301
- dstack = _fromnxfunction ('dstack' )
302
-
303
- hsplit = _fromnxfunction ('hsplit' )
304
-
305
- diagflat = _fromnxfunction ('diagflat' )
284
+ _d = func (np .asarray (x ), * args , ** params )
285
+ _m = func (getmaskarray (x ), * args , ** params )
286
+ return masked_array (_d , mask = _m )
287
+
288
+
289
+ class _fromnxfunction_seq (_fromnxfunction ):
290
+ """
291
+ A version of `_fromnxfunction` that is called with a single sequence
292
+ of arrays followed by auxiliary args that are passed verbatim for
293
+ both the data and mask calls.
294
+ """
295
+ def __call__ (self , x , * args , ** params ):
296
+ func = getattr (np , self .__name__ )
297
+ _d = func (tuple ([np .asarray (a ) for a in x ]), * args , ** params )
298
+ _m = func (tuple ([getmaskarray (a ) for a in x ]), * args , ** params )
299
+ return masked_array (_d , mask = _m )
300
+
301
+
302
+ class _fromnxfunction_args (_fromnxfunction ):
303
+ """
304
+ A version of `_fromnxfunction` that is called with multiple array
305
+ arguments. The first non-array-like input marks the beginning of the
306
+ arguments that are passed verbatim for both the data and mask calls.
307
+ Array arguments are processed independently and the results are
308
+ returned in a list. If only one array is found, the return value is
309
+ just the processed array instead of a list.
310
+ """
311
+ def __call__ (self , * args , ** params ):
312
+ func = getattr (np , self .__name__ )
313
+ arrays = []
314
+ args = list (args )
315
+ while len (args ) > 0 and issequence (args [0 ]):
316
+ arrays .append (args .pop (0 ))
317
+ res = []
318
+ for x in arrays :
319
+ _d = func (np .asarray (x ), * args , ** params )
320
+ _m = func (getmaskarray (x ), * args , ** params )
321
+ res .append (masked_array (_d , mask = _m ))
322
+ if len (arrays ) == 1 :
323
+ return res [0 ]
324
+ return res
325
+
326
+
327
+ class _fromnxfunction_allargs (_fromnxfunction ):
328
+ """
329
+ A version of `_fromnxfunction` that is called with multiple array
330
+ arguments. Similar to `_fromnxfunction_args` except that all args
331
+ are converted to arrays even if they are not so already. This makes
332
+ it possible to process scalars as 1-D arrays. Only keyword arguments
333
+ are passed through verbatim for the data and mask calls. Arrays
334
+ arguments are processed independently and the results are returned
335
+ in a list. If only one arg is present, the return value is just the
336
+ processed array instead of a list.
337
+ """
338
+ def __call__ (self , * args , ** params ):
339
+ func = getattr (np , self .__name__ )
340
+ res = []
341
+ for x in args :
342
+ _d = func (np .asarray (x ), ** params )
343
+ _m = func (getmaskarray (x ), ** params )
344
+ res .append (masked_array (_d , mask = _m ))
345
+ if len (args ) == 1 :
346
+ return res [0 ]
347
+ return res
348
+
349
+
350
+ atleast_1d = _fromnxfunction_allargs ('atleast_1d' )
351
+ atleast_2d = _fromnxfunction_allargs ('atleast_2d' )
352
+ atleast_3d = _fromnxfunction_allargs ('atleast_3d' )
353
+
354
+ vstack = row_stack = _fromnxfunction_seq ('vstack' )
355
+ hstack = _fromnxfunction_seq ('hstack' )
356
+ column_stack = _fromnxfunction_seq ('column_stack' )
357
+ dstack = _fromnxfunction_seq ('dstack' )
358
+
359
+ hsplit = _fromnxfunction_single ('hsplit' )
360
+
361
+ diagflat = _fromnxfunction_single ('diagflat' )
306
362
307
363
308
364
#####--------------------------------------------------------------------------
0 commit comments