File: test_3311_form_with_key_path.py

package info (click to toggle)
python-awkward 2.8.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,932 kB
  • sloc: python: 178,875; cpp: 33,828; sh: 432; makefile: 21; javascript: 8
file content (257 lines) | stat: -rw-r--r-- 7,844 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
from __future__ import annotations

import numpy as np

import awkward as ak


def test_record_tuple():
    form = ak.forms.from_dict(
        {
            "class": "RecordArray",
            "fields": None,
            "contents": [
                {"class": "NumpyArray", "primitive": "int64", "form_key": "('0',)"},
                {"class": "NumpyArray", "primitive": "int64", "form_key": "('1',)"},
            ],
            "form_key": "()",
        }
    )
    array = ak.Array([(1, 2)])
    assert array.layout.form_with_key_path() == form


def test_record_dict():
    form = ak.forms.from_dict(
        {
            "class": "RecordArray",
            "fields": ["foo", "bar"],
            "contents": [
                {"class": "NumpyArray", "primitive": "int64", "form_key": "('foo',)"},
                {"class": "NumpyArray", "primitive": "int64", "form_key": "('bar',)"},
            ],
            "form_key": "()",
        }
    )
    array = ak.Array({"foo": [1], "bar": [2]})
    assert array.layout.form_with_key_path() == form


def test_numpy():
    form = ak.forms.from_dict(
        {"class": "NumpyArray", "primitive": "int64", "form_key": "()"}
    )
    array = ak.Array([1, 2, 3])
    assert array.layout.form_with_key_path() == form


def test_listoffset():
    form = ak.forms.from_dict(
        {
            "class": "ListOffsetArray",
            "offsets": "i64",
            "content": {
                "class": "NumpyArray",
                "primitive": "int64",
                "form_key": "(None,)",
            },
            "form_key": "()",
        }
    )
    array = ak.Array([[1, 2], [3]])
    assert array.layout.form_with_key_path() == form


def test_empty():
    form = ak.forms.from_dict({"class": "EmptyArray", "form_key": "()"})
    array = ak.Array([])
    assert array.layout.form_with_key_path() == form


def test_bitmasked():
    form = ak.forms.from_dict(
        {
            "class": "BitMaskedArray",
            "mask": "u8",
            "valid_when": True,
            "lsb_order": False,
            "content": {
                "class": "NumpyArray",
                "primitive": "float64",
                "form_key": "(None,)",
            },
            "form_key": "()",
        }
    )
    content = ak.Array([0.0, 1.1, 2.2, 3.3, 4.4]).layout
    mask = ak.index.IndexU8(
        np.packbits(np.array([False, True, True, False, False], dtype=np.int8))
    )
    bitmaskedarray = ak.contents.BitMaskedArray(mask, content, True, 5, False)
    assert bitmaskedarray.form_with_key_path() == form


def test_bytemasked():
    form = ak.forms.from_dict(
        {
            "class": "ByteMaskedArray",
            "mask": "i8",
            "valid_when": True,
            "content": {
                "class": "NumpyArray",
                "primitive": "float64",
                "form_key": "(None,)",
            },
            "form_key": "()",
        }
    )
    content = ak.Array([0.0, 1.1, 2.2, 3.3, 4.4]).layout
    mask = ak.index.Index8(np.array([False, True, True, False, False], dtype=np.int8))
    bytemaskedarray = ak.contents.ByteMaskedArray(mask, content, True)
    assert bytemaskedarray.form_with_key_path() == form


def test_indexedarray():
    form = ak.forms.from_dict(
        {
            "class": "IndexedArray",
            "index": "i64",
            "content": {
                "class": "NumpyArray",
                "primitive": "float64",
                "form_key": "(None,)",
            },
            "form_key": "()",
        }
    )

    content = ak.Array([0.0, 1.1, 2.2, 3.3, 4.4]).layout
    index = ak.index.Index64(np.array([3, 1, 1, 4, 2], dtype=np.int64))
    indexedarray = ak.contents.IndexedArray(index, content)
    assert indexedarray.form_with_key_path() == form


def test_indexedoptionarray():
    form = ak.forms.from_dict(
        {
            "class": "IndexedOptionArray",
            "index": "i64",
            "content": {
                "class": "NumpyArray",
                "primitive": "float64",
                "form_key": "(None,)",
            },
            "form_key": "()",
        }
    )
    content = ak.Array(
        np.array([0.0, 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9])
    ).layout
    index = ak.index.Index64(np.array([2, -1, 4, 0, 8], dtype=np.int64))
    layout = ak.Array(ak.contents.IndexedOptionArray(index, content)).layout
    assert layout.form_with_key_path() == form


def test_listarray():
    form = ak.forms.from_dict(
        {
            "class": "ListArray",
            "starts": "i64",
            "stops": "i64",
            "content": {
                "class": "NumpyArray",
                "primitive": "float64",
                "form_key": "(None,)",
            },
            "form_key": "()",
        }
    )
    content = ak.contents.NumpyArray(
        np.array([1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9])
    )
    starts = ak.index.Index64(np.array([0, 3, 3, 5, 6]))
    stops = ak.index.Index64(np.array([3, 3, 5, 6, 9]))
    layout = ak.contents.ListArray(starts, stops, content)
    assert layout.form_with_key_path() == form


def test_regulararray():
    form = ak.forms.from_dict(
        {
            "class": "RegularArray",
            "size": 3,
            "content": {
                "class": "NumpyArray",
                "primitive": "int64",
                "form_key": "(None,)",
            },
            "form_key": "()",
        }
    )
    content = ak.Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]).layout
    layout = ak.contents.RegularArray(content, 3, zeros_length=0)
    assert layout.form_with_key_path() == form


def test_unionarray():
    form = ak.forms.from_dict(
        {
            "class": "UnionArray",
            "tags": "i8",
            "index": "i64",
            "contents": [
                {
                    "class": "ListOffsetArray",
                    "offsets": "i64",
                    "content": {
                        "class": "NumpyArray",
                        "primitive": "float64",
                        "form_key": "(0, None)",
                    },
                    "form_key": "(0,)",
                },
                {
                    "class": "ListOffsetArray",
                    "offsets": "i64",
                    "content": {
                        "class": "ListOffsetArray",
                        "offsets": "i64",
                        "content": {
                            "class": "NumpyArray",
                            "primitive": "uint8",
                            "parameters": {"__array__": "char"},
                            "form_key": "(1, None, None)",
                        },
                        "parameters": {"__array__": "string"},
                        "form_key": "(1, None)",
                    },
                    "form_key": "(1,)",
                },
            ],
            "form_key": "()",
        }
    )
    content1 = ak.operations.from_iter([[], [1.1], [2.2, 2.2]], highlevel=False)
    content2 = ak.operations.from_iter([["two", "two"], ["one"], []], highlevel=False)
    tags = ak.index.Index8(np.array([0, 1, 0, 1, 0, 1], dtype=np.int8))
    index = ak.index.Index64(np.array([0, 0, 1, 1, 2, 2], dtype=np.int64))
    layout = ak.contents.UnionArray(tags, index, [content1, content2])
    assert layout.form_with_key_path() == form


def test_unmaskedarray():
    form = ak.forms.from_dict(
        {
            "class": "UnmaskedArray",
            "content": {
                "class": "NumpyArray",
                "primitive": "int64",
                "form_key": "(None,)",
            },
            "form_key": "()",
        }
    )

    content = ak.Array([1, 2, 3, 4, 5]).layout
    layout = ak.contents.UnmaskedArray(content)
    assert layout.form_with_key_path() == form