File: test_1568_fix_lengths_empty_regular_slices.py

package info (click to toggle)
python-awkward 2.6.5-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 23,088 kB
  • sloc: python: 148,689; cpp: 33,562; sh: 432; makefile: 21; javascript: 8
file content (107 lines) | stat: -rw-r--r-- 3,054 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# BSD 3-Clause License; see https://github.com/scikit-hep/awkward/blob/main/LICENSE

from __future__ import annotations

import numpy as np
import pytest

import awkward as ak

to_list = ak.operations.to_list


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

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

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

    f = ak.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.contents.NumpyArray(d)
    f = ak.operations.to_regular(e, axis=1)

    assert (
        ak.to_list(d[-4:, -4:, []])
        == ak.to_list(e[-4:, -4:, []])
        == ak.to_list(f[-4:, -4:, []])
        == [[[], [], [], []], [[], [], [], []], [[], [], [], []], [[], [], [], []]]
    )
    assert (
        ak.to_list(d[:, [], []])
        == ak.to_list(e[:, [], []])
        == ak.to_list(f[:, [], []])
        == [[], [], [], [], []]
    )
    assert (
        ak.to_list(d[1:4, [], []])
        == ak.to_list(e[1:4, [], []])
        == ak.to_list(f[1:4, [], []])
        == [[], [], []]
    )
    assert (
        ak.to_list(d[:, [], [], []])
        == ak.to_list(e[:, [], [], []])
        == ak.to_list(f[:, [], [], []])
        == [[], [], [], [], []]
    )
    assert (
        ak.to_list(d[1:4, [], [], []])
        == ak.to_list(e[1:4, [], [], []])
        == ak.to_list(f[1:4, [], [], []])
        == [[], [], []]
    )
    with pytest.raises(ValueError):
        _ = e[:, [], :, 0, []]

    with pytest.raises(ValueError):
        _ = e[:, [], :, []]

    with pytest.raises(ValueError):
        _ = f[:, [], :, []]

    with pytest.raises(ValueError):
        _ = e[1:4, [], -3:, []]

    with pytest.raises(ValueError):
        _ = f[1:4, [], -3:, []]

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

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

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