Skip to content

Commit

Permalink
bn.nansum(np.ones((2, 2))[..., np.newaxis]) fix
Browse files Browse the repository at this point in the history
issue #183
  • Loading branch information
kwgoodman committed Jan 2, 2018
1 parent 71b04e8 commit e3b2746
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
2 changes: 2 additions & 0 deletions RELEASE.rst
Expand Up @@ -22,6 +22,8 @@ Bottleneck 1.3.0
- #170 Documentation fails to build on Python 3
- #175 bn.bench() crashes on python 3.6.3, numpy 1.13.3
- #178 bn.push(a, n=None) raises when None is explicitly passed
- #183 bn.nansum(a) wrong output when a = np.ones((2, 2))[..., np.newaxis]
same issue of other reduce functions

Bottleneck 1.2.1
----------------
Expand Down
25 changes: 23 additions & 2 deletions bottleneck/src/iterators.h
Expand Up @@ -78,6 +78,9 @@ init_iter_all(iter *it, PyArrayObject *a, int ravel, int anyorder)
it->nits = 1;
it->a_ravel = NULL;

/* The fix for relaxed strides checking in numpy and the fix for
* issue #183 has left this if..else tree in need of a refactor from the
* the ground up */
if (ndim == 1) {
it->ndim_m2 = -1;
it->length = shape[0];
Expand All @@ -94,13 +97,31 @@ init_iter_all(iter *it, PyArrayObject *a, int ravel, int anyorder)
it->ndim_m2 = -1;
it->axis = ndim - 1;
it->length = PyArray_SIZE(a);
it->astride = strides[ndim - 1];
it->astride = 0;
for (i=ndim-1; i > -1; i--) {
/* protect against length zero strides such as in
* np.ones((2, 2))[..., np.newaxis] */
if (strides[i] == 0) {
continue;
}
it->astride = strides[i];
break;
}
}
else if (F_CONTIGUOUS(a) && !C_CONTIGUOUS(a)) {
if (anyorder || !ravel) {
it->ndim_m2 = -1;
it->length = PyArray_SIZE(a);
it->astride = strides[0];
it->astride = 0;
for (i=0; i < ndim; i++) {
/* protect against length zero strides such as in
* np.ones((2, 2), order='F')[np.newaxis, ...] */
if (strides[i] == 0) {
continue;
}
it->astride = strides[i];
break;
}
} else {
it->ndim_m2 = -1;
if (anyorder) {
Expand Down
1 change: 1 addition & 0 deletions bottleneck/tests/util.py
Expand Up @@ -110,6 +110,7 @@ def array_generator(func_name, dtypes):
yield np.array([0, 0, 0]) # nanargmax/nanargmin
yield np.array([1, nan, nan, 2]) # nanmedian
yield np.array([2**31], dtype=np.int64) # overflows on windows
yield np.array([[1.0, 2], [3, 4]])[..., np.newaxis] # issue #183

# ties
yield np.array([0, 0, 0])
Expand Down

0 comments on commit e3b2746

Please sign in to comment.