Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Determine if negative axis argument is equivalent to zero #215

Open
douglasdavis opened this issue Apr 5, 2023 · 0 comments
Open

Determine if negative axis argument is equivalent to zero #215

douglasdavis opened this issue Apr 5, 2023 · 0 comments

Comments

@douglasdavis
Copy link
Collaborator

Determining if axis is equivalent to zero if it's negative...

Given this array,

>>> array = ak.Array([[{"x": 1.1, "y": [1]}], [], [{"x": 2.2, "y": [1, 2]}]])
>>> array.type.show()
3 * var * {
    x: float64,
    y: var * int64
}

For x, axis=-2 is equivalent to axis=0:

>>> ak.sum(array.x, axis=0), ak.sum(array.x, axis=-2)
(<Array [3.3] type='1 * float64'>, <Array [3.3] type='1 * float64'>)

For y, axis=-3 is equivalent to axis=0:

>>> ak.sum(array.y, axis=0), ak.sum(array.y, axis=-3)
(<Array [[2, 2]] type='1 * var * int64'>, <Array [[2, 2]] type='1 * var * int64'>)

We could derive that from the minmax_depth:

>>> array.layout.minmax_depth
(2, 3)

which doesn't say which has depth 2 and which has depth 3, just that something does. If a given axis is negative and

  • -axis > min(array.layout.minmax_depth), then it will be an index out of range error for some part of the array
  • min(array.layout.minmax_depth) <= -axis <= max(array.layout.minmax_depth), then it will be axis == 0 for some part of the array
  • -axis < min(array.layout.minmax_depth), then it will be axis != 0 for all parts of the array.

This last case is one that you can always offload to Awkward. If

  • -axis == min(array.layout.minmax_depth) == max(array.layout.minmax_depth), then it's a valid request, equivalent to axis == 0, but not handled by dask-awkward (NotImplementedError).

The only other case, -axis > min(array.layout.minmax_depth) would even be an Awkward axis out of range error (np.AxisError, but maybe ValueError?).

>>> ak.sum(array.y, axis=-4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/jpivarski/irishep/awkward/src/awkward/operations/ak_sum.py", line 218, in sum
    return _impl(array, axis, keepdims, mask_identity, highlevel, behavior)
  File "/home/jpivarski/irishep/awkward/src/awkward/operations/ak_sum.py", line 292, in _impl
    out = ak._do.reduce(
  File "/home/jpivarski/irishep/awkward/src/awkward/_do.py", line 383, in reduce
    raise ak._errors.wrap_error(
ValueError: while calling

    ak.sum(
        array = <Array [[[1]], [], [[1, 2]]] type='3 * var * var * int64'>
        axis = -4
        keepdims = False
        mask_identity = False
        highlevel = True
        behavior = None
    )

Error details: axis=-4 exceeds the depth of the nested list structure (which is 3)

I expected that to be an np.AxisError, not a ValueError...

Originally posted by @jpivarski in #151 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant