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

Fix: Lengths of empty regular slices #1568

Merged
merged 24 commits into from
Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
9c75e25
Fix: Lengths of empty regular slices
ioanaif Jul 26, 2022
0996cd7
Renamed test to match PR nr
ioanaif Jul 26, 2022
0c477f4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 26, 2022
ce32cb4
Only build RegularArray if slice has None
ioanaif Jul 27, 2022
8fcbe09
Fixed conflicts
ioanaif Jul 27, 2022
2df4998
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 27, 2022
39bb268
Fixed precommit issue
ioanaif Jul 27, 2022
ab4bd5e
Merge branch 'ioanaif/fix-lengths-of-empty-regular-slices-1557' of ht…
ioanaif Jul 27, 2022
837e8a0
Improved condition for None slice
ioanaif Jul 27, 2022
7b1833d
Merge branch 'main' into ioanaif/fix-lengths-of-empty-regular-slices-…
jpivarski Aug 9, 2022
a31add8
Merge branch 'main' into ioanaif/fix-lengths-of-empty-regular-slices-…
jpivarski Aug 9, 2022
6c06dad
Slice debugs
ioanaif Aug 11, 2022
3abdc68
Slice debugs
ioanaif Aug 11, 2022
8430558
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 11, 2022
9762a6a
Clean-up
ioanaif Aug 11, 2022
afa38fa
Clean-up
ioanaif Aug 11, 2022
e61ee6b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 11, 2022
cdf5999
Cleanup
ioanaif Aug 11, 2022
5040bf0
Merge branch 'ioanaif/fix-lengths-of-empty-regular-slices-1557' of ht…
ioanaif Aug 11, 2022
59508f7
Removing empty lines
ioanaif Aug 11, 2022
a1f6965
Re-pass on dealing with empty regular slices and more testing on outp…
ioanaif Aug 12, 2022
42e6018
Update src/awkward/_v2/contents/content.py
ioanaif Aug 12, 2022
32b4bcc
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 12, 2022
d47b7e8
Added numpy edge case
ioanaif Aug 15, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/awkward/_v2/contents/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,13 @@ def _getitem(self, where):
self._nplike,
)

if any(isinstance(x, list) and len(x) == 0 for x in items) and any(
isinstance(x, slice) for x in items
):
attempt = next.maybe_toNumpyArray()
if attempt is not None:
next = attempt

out = next._getitem_next(nextwhere[0], nextwhere[1:], None)

if out.length == 0:
Expand Down
114 changes: 114 additions & 0 deletions tests/v2/test_1568-fix-lengths-empty-regular-slices.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# BSD 3-Clause License; see https://github.com/scikit-hep/awkward-1.0/blob/main/LICENSE

import pytest # noqa: F401
import awkward as ak # noqa: F401
import numpy as np # noqa: F401

to_list = ak._v2.operations.to_list


def test_lengths_empty_regular_slices():
d = np.arange(2 * 3).reshape(2, 3)
e = ak._v2.contents.NumpyArray(d)

assert ak._v2.to_list(d[:, []]) == ak._v2.to_list(e[:, []]) == [[], []]
assert ak._v2.to_list(d[1:, []]) == ak._v2.to_list(e[1:, []]) == [[]]

assert d[:, []].shape == ak._v2.to_numpy(e[:, []]).shape
assert d[1:, []].shape == ak._v2.to_numpy(e[1:, []]).shape

f = ak._v2.operations.to_regular(e, axis=1)
assert to_list(f[:, []]) == [[], []]

d = np.arange(5 * 7 * 11 * 13 * 17).reshape(5, 7, 11, 13, 17)
e = ak._v2.contents.NumpyArray(d)
f = ak._v2.operations.to_regular(e, axis=1)

assert (
ak._v2.to_list(d[-4:, -4:, []])
== ak._v2.to_list(e[-4:, -4:, []])
== ak._v2.to_list(f[-4:, -4:, []])
== [[[], [], [], []], [[], [], [], []], [[], [], [], []], [[], [], [], []]]
)
assert (
ak._v2.to_list(d[:, [], []])
== ak._v2.to_list(e[:, [], []])
== ak._v2.to_list(f[:, [], []])
== [[], [], [], [], []]
)
assert (
ak._v2.to_list(d[1:4, [], []])
== ak._v2.to_list(e[1:4, [], []])
== ak._v2.to_list(f[1:4, [], []])
== [[], [], []]
)
assert (
ak._v2.to_list(d[:, [], [], []])
== ak._v2.to_list(e[:, [], [], []])
== ak._v2.to_list(f[:, [], [], []])
== [[], [], [], [], []]
)
assert (
ak._v2.to_list(d[1:4, [], [], []])
== ak._v2.to_list(e[1:4, [], [], []])
== ak._v2.to_list(f[1:4, [], [], []])
== [[], [], []]
)
assert (
ak._v2.to_list(d[:, [], :, []])
== ak._v2.to_list(e[:, [], :, []])
== ak._v2.to_list(f[:, [], :, []])
== []
)
assert (
ak._v2.to_list(d[1:4, [], -3:, []])
== ak._v2.to_list(e[1:4, [], -3:, []])
== ak._v2.to_list(f[1:4, [], -3:, []])
== []
)

assert (
d[:, :, []].shape
== ak._v2.to_numpy(e[:, :, []]).shape
== ak._v2.to_numpy(f[:, :, []]).shape
)
assert (
d[:, [], []].shape
== ak._v2.to_numpy(e[:, [], []]).shape
== ak._v2.to_numpy(f[:, [], []]).shape
)
assert (
d[1:4, :, [], []].shape
== ak._v2.to_numpy(e[1:4, :, [], []]).shape
== ak._v2.to_numpy(f[1:4, :, [], []]).shape
)
assert (
d[:, [], [], []].shape
== ak._v2.to_numpy(e[:, [], [], []]).shape
== ak._v2.to_numpy(f[:, [], [], []]).shape
)
assert (
d[1:4, [], [], []].shape
== ak._v2.to_numpy(e[1:4, [], [], []]).shape
== ak._v2.to_numpy(f[1:4, [], [], []]).shape
)
assert (
d[:, [], :, []].shape
== ak._v2.to_numpy(e[:, [], :, []]).shape
== ak._v2.to_numpy(f[:, [], :, []]).shape
)
assert (
d[1:4, [], -3:, []].shape
== ak._v2.to_numpy(e[1:4, [], -3:, []]).shape
== ak._v2.to_numpy(f[1:4, [], -3:, []]).shape
)

np1 = np.ones((5, 7))
a = ak._v2.Array(np.ones((5, 7)))
assert (
ak._v2.to_list(np1[:, []]) == ak._v2.to_list(a[:, []]) == [[], [], [], [], []]
)
assert ak._v2.to_list(np1[[], :]) == ak._v2.to_list(a[[], :]) == []

assert np1[:, []].shape == ak._v2.to_numpy(a[:, []]).shape == (5, 0)
assert np1[[], :].shape == ak._v2.to_numpy(a[[], :]).shape == (0, 7)