Repository navigation
Fix negative axis indices raising error, fixes #117 - #119
nils-werner wants to merge 5 commits into
Conversation
|
Actually, digging through the code I've found a couple more axis issues. Trying to fix them too now. |
|
Any fixes are welcome, as well as a |
|
Needs a rebase. Also, are you done here? |
| axes = _normalize_axis(axes, self.ndim) | ||
|
|
||
| if np.any(axes >= self.ndim) or np.any(axes < 0): | ||
| if any(a >= self.ndim for a in axes) or any(a < 0 for a in axes): |
There was a problem hiding this comment.
Can we put this into _normalize_axis?
|
Needs tests for exceptions... |
|
Checking in on this. It looks like this has fallen behind a bit from master. @nils-werner are you still interested in this? If so then do you know how to proceed or are you blocked on anything? |
4d5fb0e to
d6c5f5c
Compare
|
Sorry for being silent for a month, I was quite busy with other work. I've rebased the branch to sit ontop of A few pointers as to what is still missing from this PR would be appreciated. Alternatively, if anybody wants to pick up the work and finish it, I'd be happy to hand it over. |
| axes = _normalize_axis(axes, self.ndim) | ||
|
|
||
| if np.any(axes >= self.ndim) or np.any(axes < 0): | ||
| if any(a >= self.ndim for a in axes) or any(a < 0 for a in axes): |
There was a problem hiding this comment.
This is better placed in _normalize_axis.
There was a problem hiding this comment.
Actually, it seems the function already has this check, so this can be safely removed.
Codecov Report
@@ Coverage Diff @@
## master #119 +/- ##
=========================================
+ Coverage 95.57% 95.6% +0.02%
=========================================
Files 10 10
Lines 1220 1228 +8
=========================================
+ Hits 1166 1174 +8
Misses 54 54
Continue to review full report at Codecov.
|
| return starts, stops | ||
|
|
||
|
|
||
| def _normalize_axis(axis, ndim): |
There was a problem hiding this comment.
Best to move this function into utils. indexing is COO specific and I'm pretty sure this will be needed elsewhere down the road.
|
Some minor comments. You can see in the CodeCov page what's missing coverage-wise, it'd be nice to have a test or remove dead code. Overall, this looks pretty good and complete. Thanks! |
| ) | ||
| return axis | ||
| elif isinstance(axis, int): | ||
| return _normalize_axis([axis], ndim)[0] |
There was a problem hiding this comment.
Can we flip the logic here? Make int the default and call that for Iterable?
There was a problem hiding this comment.
Also, please use numbers.Integral. Otherwise Numpy int64 etc. won't work.
|
Superseded by #136, which is now merged. |
`_arg_minmax_common` never normalized the axis, and its bounds check only
covered one side (`axis >= x.ndim`), so a negative axis reached the
transpose and reshape logic untouched. The initial `list.pop(axis)`
happens to work for `-1`, but the final un-transpose does
`new_transpose.insert(axis, new_transpose.pop(0))`, which for `-1`
inserts before the last element instead of at the end. The reduced axis
ends up in front.
Measured against numpy on a (2, 3) array:
axis=1 numpy [[1], [0]] (2, 1) sparse [[1], [0]] (2, 1)
axis=-1 numpy [[1], [0]] (2, 1) sparse [[1, 0]] (1, 2)
A 1-D array with `axis=-1` raised `ValueError: shape must be an
non-negative integer...` where numpy returns a value, and `axis=-5` on a
2-D array leaked `IndexError: pop index out of range` instead of a
message naming the bounds.
`normalize_axis` is already imported in this module and every other
axis-taking function in it uses it: `concatenate`, `stack`, `roll`,
`diagonal`, `expand_dims` and `flip`. This was the only one left out,
which also matches the history: the reductions got this treatment in
#118/#119 and `diagonal` in #955, while `argmax`/`argmin` arrived later
in #614 and were never covered. The existing tests parametrize `axis`
over `[None, 0, 1, 2]`, so no negative axis was ever exercised.
The wording of the out-of-bounds `ValueError` is kept so that
`test_argmax_argmin_constraint` still passes, including the 0-D case.
Giving negative axis indices for reductions raises errors, see #117. This PR fixes that.
Fixes #117