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
|
# BSD 3-Clause License; see https://github.com/scikit-hep/awkward/blob/main/LICENSE
from __future__ import annotations
import numpy as np
import pytest # noqa: F401
import awkward as ak
def test_union_simplification():
array = ak.Array(
ak.contents.UnionArray(
ak.index.Index8(np.arange(64, dtype=np.int8) % 2),
ak.index.Index64(np.arange(64, dtype=np.int64) // 2),
[
ak.contents.RecordArray(
[ak.contents.NumpyArray(np.arange(64, dtype=np.int64))], ["x"]
),
ak.contents.RecordArray(
[
ak.contents.NumpyArray(np.arange(64, dtype=np.int64)),
ak.contents.NumpyArray(np.arange(64, dtype=np.int8)),
],
["x", "y"],
),
],
)
)
form, length, container = ak.to_buffers(array)
assert form.to_dict() == {
"class": "UnionArray",
"tags": "i8",
"index": "i64",
"contents": [
{
"class": "RecordArray",
"fields": ["x"],
"contents": [
{
"class": "NumpyArray",
"primitive": "int64",
"inner_shape": [],
"parameters": {},
"form_key": "node2",
}
],
"parameters": {},
"form_key": "node1",
},
{
"class": "RecordArray",
"fields": ["x", "y"],
"contents": [
{
"class": "NumpyArray",
"primitive": "int64",
"inner_shape": [],
"parameters": {},
"form_key": "node4",
},
{
"class": "NumpyArray",
"primitive": "int8",
"inner_shape": [],
"parameters": {},
"form_key": "node5",
},
],
"parameters": {},
"form_key": "node3",
},
],
"parameters": {},
"form_key": "node0",
}
projected_form = {
"class": "UnionArray",
"tags": "i8",
"index": "i64",
"contents": [
{
"class": "RecordArray",
"fields": ["x"],
"contents": [
{
"class": "NumpyArray",
"primitive": "int64",
"inner_shape": [],
"parameters": {},
"form_key": "node2",
}
],
"parameters": {},
"form_key": "node1",
},
{
"class": "RecordArray",
"fields": ["x"],
"contents": [
{
"class": "NumpyArray",
"primitive": "int64",
"inner_shape": [],
"parameters": {},
"form_key": "node4",
}
],
"parameters": {},
"form_key": "node3",
},
],
"parameters": {},
"form_key": "node0",
}
container.pop("node5-data")
projected = ak.from_buffers(
projected_form, length, container, allow_noncanonical_form=True
)
assert projected.layout.form.to_dict(verbose=False) == {
"class": "IndexedArray",
"index": "i64",
"content": {"class": "RecordArray", "fields": ["x"], "contents": ["int64"]},
}
assert ak.almost_equal(array[["x"]], projected)
|