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
|
# 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
# taken from test_2719_typetracer_buffer_key.py
form = ak.forms.from_dict(
{
"class": "RecordArray",
"fields": ["x"],
"contents": [
{
"class": "ListOffsetArray",
"offsets": "i64",
"content": {
"class": "NumpyArray",
"primitive": "int64",
"inner_shape": [],
"parameters": {},
"form_key": "x.list.content",
},
"parameters": {},
"form_key": "x.list",
}
],
"parameters": {},
}
)
def test_buffer_keys_on_virtual_arrays():
buffers = {
"x.list-offsets": lambda: np.array([0, 2, 3, 5]),
"x.list.content-data": lambda: np.array([1, 2, 3, 4, 5]),
}
va = ak.from_buffers(form, 3, buffers, buffer_key="{form_key}-{attribute}")
assert va.layout.content("x").offsets.data.buffer_key == "x.list-offsets"
assert va.layout.content("x").content.data.buffer_key == "x.list.content-data"
def test_buffer_keys_on_placeholder_arrays():
buffers = {
"x.list-offsets": ak._nplikes.placeholder.PlaceholderArray(
shape=(4,),
dtype=np.int64,
buffer_key="x.list-offsets",
nplike=ak._nplikes.numpy.Numpy.instance(),
),
"x.list.content-data": ak._nplikes.placeholder.PlaceholderArray(
shape=(5,),
dtype=np.int64,
buffer_key="x.list.content-data",
nplike=ak._nplikes.numpy.Numpy.instance(),
),
}
pa = ak.from_buffers(form, 3, buffers, buffer_key="{form_key}-{attribute}")
assert pa.layout.content("x").offsets.data.buffer_key == "x.list-offsets"
assert pa.layout.content("x").content.data.buffer_key == "x.list.content-data"
with pytest.raises(
RuntimeError,
match=r"Awkward Array tried to access a buffer at 'x.list-offsets'",
):
pa.layout.content("x").offsets.data.materialize()
with pytest.raises(
RuntimeError,
match=r"Awkward Array tried to access a buffer at 'x.list.content-data'",
):
pa.layout.content("x").content.data.materialize()
|