Repository navigation
Fix negative axes reductions. - #118
Conversation
| else: | ||
| axis = tuple(axis) | ||
| neg_axis = tuple(ax for ax in range(self.ndim) if ax not in axis) | ||
| neg_axis = tuple(ax for ax in range(self.ndim) if ax not in set(axis)) |
There was a problem hiding this comment.
Why this change? I'm not entirely sure of Python semantics here but I suspect that you're creating a new set every time.
There was a problem hiding this comment.
Makes it O(ndim) rather than O(ndim^2). I ran a quick test to see if this was indeed the case:
>>> class A:
... def get_set(self):
... print("Called")
... return set(range(5))
...
>>> tuple(i for i in A().get_set())
Called
(0, 1, 2, 3, 4)There was a problem hiding this comment.
This doesn't really matter either way. Asymptotic arguments don't really carry significance when n is likely to be very small.
In [1]: axis = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
In [2]: ndim = 20
In [3]: %timeit tuple(ax for ax in range(ndim) if ax not in axis)
The slowest run took 6.44 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 4.03 µs per loopHappy either way though. I'm glad to learn that Python handles things in that order.
| if not isinstance(axis, tuple): | ||
| axis = (axis,) | ||
|
|
||
| axis = tuple(a if a >= 0 else a + self.ndim for a in axis) |
There was a problem hiding this comment.
Are there other uses of axis= where this should be done but is not yet currently done?
There was a problem hiding this comment.
With a short "find all" of axis, I'm inclined to say no. It does appear in tensordot, concatenate and stack but those already have this.
Codecov Report
@@ Coverage Diff @@
## master #118 +/- ##
==========================================
+ Coverage 96.04% 96.04% +<.01%
==========================================
Files 11 11
Lines 1896 1897 +1
==========================================
+ Hits 1821 1822 +1
Misses 75 75
Continue to review full report at Codecov.
|
`_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.
Fixes #117