diff options
author | Nathaniel J. Smith <njs@pobox.com> | 2012-05-11 14:31:50 +0100 |
---|---|---|
committer | Nathaniel J. Smith <njs@pobox.com> | 2012-06-16 10:45:38 +0100 |
commit | b272bc605ce7784be5b3edb13ad7afe22b04e71f (patch) | |
tree | 40fc10c60fd1b48d94be48a80e7cfc98525bd6e7 /numpy/core/_methods.py | |
parent | 1b6582d98c58afd977a69ac49f7e8e0d08a800b8 (diff) | |
download | numpy-b272bc605ce7784be5b3edb13ad7afe22b04e71f.tar.gz |
Remove maskna API from ndarray, and all (and only) the code supporting it
The original masked-NA-NEP branch contained a large number of changes
in addition to the core NA support. For example:
- ufunc.__call__ support for where= argument
- nditer support for arbitrary masks (in support of where=)
- ufunc.reduce support for simultaneous reduction over multiple axes
- a new "array assignment API"
- ndarray.diagonal() returning a view in all cases
- bug-fixes in __array_priority__ handling
- datetime test changes
etc. There's no consensus yet on what should be done with the
maskna-related part of this branch, but the rest is generally useful
and uncontroversial, so the goal of this branch is to identify exactly
which code changes are involved in maskna support.
The basic strategy used to create this patch was:
- Remove the new masking-related fields from ndarray, so no arrays
are masked
- Go through and remove all the code that this makes
dead/inaccessible/irrelevant, in a largely mechanical fashion. So
for example, if I saw 'if (PyArray_HASMASK(a)) { ... }' then that
whole block was obviously just dead code if no arrays have masks,
and I removed it. Likewise for function arguments like skipna that
are useless if there aren't any NAs to skip.
This changed the signature of a number of functions that were newly
exposed in the numpy public API. I've removed all such functions from
the public API, since releasing them with the NA-less signature in 1.7
would create pointless compatibility hassles later if and when we add
back the NA-related functionality. Most such functions are removed by
this commit; the exception is PyArray_ReduceWrapper, which requires
more extensive surgery, and will be handled in followup commits.
I also removed the new ndarray.setasflat method. Reason: a comment
noted that the only reason this was added was to allow easier testing
of one branch of PyArray_CopyAsFlat. That branch is now the main
branch, so that isn't an issue. Nonetheless this function is arguably
useful, so perhaps it should have remained, but I judged that since
numpy's API is already hairier than we would like, it's not a good
idea to add extra hair "just in case". (Also AFAICT the test for this
method in test_maskna was actually incorrect, as noted here:
https://github.com/njsmith/numpyNEP/blob/master/numpyNEP.py
so I'm not confident that it ever worked in master, though I haven't
had a chance to follow-up on this.)
I also removed numpy.count_reduce_items, since without skipna it
became trivial.
I believe that these are the only exceptions to the "remove dead code"
strategy.
Diffstat (limited to 'numpy/core/_methods.py')
-rw-r--r-- | numpy/core/_methods.py | 64 |
1 files changed, 38 insertions, 26 deletions
diff --git a/numpy/core/_methods.py b/numpy/core/_methods.py index 80e12298c..d3c150ac8 100644 --- a/numpy/core/_methods.py +++ b/numpy/core/_methods.py @@ -5,34 +5,51 @@ from numpy.core import multiarray as mu from numpy.core import umath as um from numpy.core.numeric import asanyarray -def _amax(a, axis=None, out=None, skipna=False, keepdims=False): +def _amax(a, axis=None, out=None, keepdims=False): return um.maximum.reduce(a, axis=axis, - out=out, skipna=skipna, keepdims=keepdims) + out=out, keepdims=keepdims) -def _amin(a, axis=None, out=None, skipna=False, keepdims=False): +def _amin(a, axis=None, out=None, keepdims=False): return um.minimum.reduce(a, axis=axis, - out=out, skipna=skipna, keepdims=keepdims) + out=out, keepdims=keepdims) -def _sum(a, axis=None, dtype=None, out=None, skipna=False, keepdims=False): +def _sum(a, axis=None, dtype=None, out=None, keepdims=False): return um.add.reduce(a, axis=axis, dtype=dtype, - out=out, skipna=skipna, keepdims=keepdims) + out=out, keepdims=keepdims) -def _prod(a, axis=None, dtype=None, out=None, skipna=False, keepdims=False): +def _prod(a, axis=None, dtype=None, out=None, keepdims=False): return um.multiply.reduce(a, axis=axis, dtype=dtype, - out=out, skipna=skipna, keepdims=keepdims) - -def _mean(a, axis=None, dtype=None, out=None, skipna=False, keepdims=False): + out=out, keepdims=keepdims) + +def _any(a, axis=None, dtype=None, out=None, keepdims=False): + return um.logical_or.reduce(a, axis=axis, dtype=dtype, out=out, + keepdims=keepdims) + +def _all(a, axis=None, dtype=None, out=None, keepdims=False): + return um.logical_and.reduce(a, axis=axis, dtype=dtype, out=out, + keepdims=keepdims) + +def _count_reduce_items(arr, axis): + if axis is None: + axis = tuple(xrange(arr.ndim)) + if not isinstance(axis, tuple): + axis = (axis,) + items = 1 + for ax in axis: + items *= arr.shape[ax] + return items + +def _mean(a, axis=None, dtype=None, out=None, keepdims=False): arr = asanyarray(a) # Upgrade bool, unsigned int, and int to float64 if dtype is None and arr.dtype.kind in ['b','u','i']: ret = um.add.reduce(arr, axis=axis, dtype='f8', - out=out, skipna=skipna, keepdims=keepdims) + out=out, keepdims=keepdims) else: ret = um.add.reduce(arr, axis=axis, dtype=dtype, - out=out, skipna=skipna, keepdims=keepdims) - rcount = mu.count_reduce_items(arr, axis=axis, - skipna=skipna, keepdims=keepdims) + out=out, keepdims=keepdims) + rcount = _count_reduce_items(arr, axis) if isinstance(ret, mu.ndarray): ret = um.true_divide(ret, rcount, out=ret, casting='unsafe', subok=False) @@ -41,18 +58,15 @@ def _mean(a, axis=None, dtype=None, out=None, skipna=False, keepdims=False): return ret def _var(a, axis=None, dtype=None, out=None, ddof=0, - skipna=False, keepdims=False): + keepdims=False): arr = asanyarray(a) # First compute the mean, saving 'rcount' for reuse later if dtype is None and arr.dtype.kind in ['b','u','i']: - arrmean = um.add.reduce(arr, axis=axis, dtype='f8', - skipna=skipna, keepdims=True) + arrmean = um.add.reduce(arr, axis=axis, dtype='f8', keepdims=True) else: - arrmean = um.add.reduce(arr, axis=axis, dtype=dtype, - skipna=skipna, keepdims=True) - rcount = mu.count_reduce_items(arr, axis=axis, - skipna=skipna, keepdims=True) + arrmean = um.add.reduce(arr, axis=axis, dtype=dtype, keepdims=True) + rcount = _count_reduce_items(arr, axis) if isinstance(arrmean, mu.ndarray): arrmean = um.true_divide(arrmean, rcount, out=arrmean, casting='unsafe', subok=False) @@ -69,8 +83,7 @@ def _var(a, axis=None, dtype=None, out=None, ddof=0, x = um.multiply(x, x, out=x) # add.reduce((arr - arrmean) ** 2, axis) - ret = um.add.reduce(x, axis=axis, dtype=dtype, out=out, - skipna=skipna, keepdims=keepdims) + ret = um.add.reduce(x, axis=axis, dtype=dtype, out=out, keepdims=keepdims) # add.reduce((arr - arrmean) ** 2, axis) / (n - ddof) if not keepdims and isinstance(rcount, mu.ndarray): @@ -84,10 +97,9 @@ def _var(a, axis=None, dtype=None, out=None, ddof=0, return ret -def _std(a, axis=None, dtype=None, out=None, ddof=0, - skipna=False, keepdims=False): +def _std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False): ret = _var(a, axis=axis, dtype=dtype, out=out, ddof=ddof, - skipna=skipna, keepdims=keepdims) + keepdims=keepdims) if isinstance(ret, mu.ndarray): ret = um.sqrt(ret, out=ret) |