File: test_1928_replace_simplify_method_with_classmethod_constructor.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 (148 lines) | stat: -rw-r--r-- 4,639 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# 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


def test_indexed_of_union():
    unionarray = ak.from_iter(
        [0.0, 1.1, "zero", 2.2, "one", "two", "three", 3.3, 4.4, 5.5, "four"],
        highlevel=False,
    )
    indexedarray = ak.contents.IndexedArray.simplified(
        ak.index.Index64(np.array([4, 3, 3, 8, 7, 6], np.int64)),
        unionarray,
    )
    assert indexedarray.to_list() == ["one", 2.2, 2.2, 4.4, 3.3, "three"]


def test_indexedoption_of_union():
    unionarray = ak.from_iter(
        [0.0, 1.1, "zero", 2.2, "one", "two", "three", 3.3, 4.4, 5.5, "four"],
        highlevel=False,
    )
    indexedoptionarray = ak.contents.IndexedOptionArray.simplified(
        ak.index.Index64(np.array([-1, 4, 3, -1, 3, 8, 7, 6, -1], np.int64)),
        unionarray,
    )
    assert indexedoptionarray.to_list() == [
        None,
        "one",
        2.2,
        None,
        2.2,
        4.4,
        3.3,
        "three",
        None,
    ]


def test_indexedoption_of_union_of_option_1():
    with pytest.raises(
        TypeError, match=r" must either be comprised of entirely optional contents"
    ):
        ak.contents.UnionArray(
            ak.index.Index8(np.array([0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1], dtype=np.int8)),
            ak.index.Index64(
                np.array([0, 1, 0, 2, 1, 2, 3, 3, 4, 5, 4], dtype=np.int64)
            ),
            [
                ak.from_iter([0.0, 1.1, 2.2, 3.3, None, 5.5], highlevel=False),
                ak.from_iter(["zero", "one", "two", "three", "four"], highlevel=False),
            ],
        )

    unionarray = ak.contents.UnionArray.simplified(
        ak.index.Index8(np.array([0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1], dtype=np.int8)),
        ak.index.Index64(np.array([0, 1, 0, 2, 1, 2, 3, 3, 4, 5, 4], dtype=np.int64)),
        [
            ak.from_iter([0.0, 1.1, 2.2, 3.3, None, 5.5], highlevel=False),
            ak.from_iter(["zero", "one", "two", "three", "four"], highlevel=False),
        ],
    )
    indexedoptionarray = ak.contents.IndexedOptionArray.simplified(
        ak.index.Index64(np.array([-1, 4, 3, -1, 3, 8, 7, 6, -1], np.int64)),
        unionarray,
    )
    assert indexedoptionarray.to_list() == [
        None,
        "one",
        2.2,
        None,
        2.2,
        None,
        3.3,
        "three",
        None,
    ]


def test_indexedoption_of_union_of_option_2():
    with pytest.raises(
        TypeError, match=r"must either be comprised of entirely optional contents"
    ):
        ak.contents.UnionArray(
            ak.index.Index8(np.array([0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1], dtype=np.int8)),
            ak.index.Index64(
                np.array([0, 1, 0, 2, 1, 2, 3, 3, 4, 5, 4], dtype=np.int64)
            ),
            [
                ak.from_iter([0.0, 1.1, 2.2, 3.3, 4.4, 5.5], highlevel=False),
                ak.from_iter(["zero", None, "two", "three", "four"], highlevel=False),
            ],
        )

    unionarray = ak.contents.UnionArray.simplified(
        ak.index.Index8(np.array([0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1], dtype=np.int8)),
        ak.index.Index64(np.array([0, 1, 0, 2, 1, 2, 3, 3, 4, 5, 4], dtype=np.int64)),
        [
            ak.from_iter([0.0, 1.1, 2.2, 3.3, 4.4, 5.5], highlevel=False),
            ak.from_iter(["zero", None, "two", "three", "four"], highlevel=False),
        ],
    )
    indexedoptionarray = ak.contents.IndexedOptionArray.simplified(
        ak.index.Index64(np.array([-1, 4, 3, -1, 3, 8, 7, 6, -1], np.int64)),
        unionarray,
    )
    assert indexedoptionarray.to_list() == [
        None,
        None,
        2.2,
        None,
        2.2,
        4.4,
        3.3,
        "three",
        None,
    ]


def test_indexedoption_of_union_of_option_1_2():
    unionarray = ak.contents.UnionArray(
        ak.index.Index8(np.array([0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1], dtype=np.int8)),
        ak.index.Index64(np.array([0, 1, 0, 2, 1, 2, 3, 3, 4, 5, 4], dtype=np.int64)),
        [
            ak.from_iter([0.0, 1.1, 2.2, 3.3, None, 5.5], highlevel=False),
            ak.from_iter(["zero", None, "two", "three", "four"], highlevel=False),
        ],
    )
    indexedoptionarray = ak.contents.IndexedOptionArray.simplified(
        ak.index.Index64(np.array([-1, 4, 3, -1, 3, 8, 7, 6, -1], np.int64)),
        unionarray,
    )
    assert indexedoptionarray.to_list() == [
        None,
        None,
        2.2,
        None,
        2.2,
        None,
        3.3,
        "three",
        None,
    ]