File: test_3667_virtual_array_shape_check_in_from_buffers.py

package info (click to toggle)
python-awkward 2.8.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 25,140 kB
  • sloc: python: 182,845; cpp: 33,828; sh: 432; makefile: 21; javascript: 8
file content (63 lines) | stat: -rw-r--r-- 2,107 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
# 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
from awkward._nplikes.numpy import Numpy
from awkward._nplikes.shape import unknown_length
from awkward._nplikes.virtual import VirtualNDArray


@pytest.mark.parametrize("offsets_length", [5, unknown_length])
@pytest.mark.parametrize("content_length", [9, unknown_length])
def test(offsets_length, content_length):
    offset_generator = lambda: np.array([0, 2, 4, 5, 6], dtype=np.int64)  # noqa: E731
    data_generator = lambda: np.array([1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=np.int64)  # noqa: E731

    offsets = ak.index.Index(
        VirtualNDArray(
            Numpy.instance(),
            shape=(offsets_length,),
            dtype=np.int64,
            generator=offset_generator,
        )
    )
    data = ak.contents.NumpyArray(
        VirtualNDArray(
            Numpy.instance(),
            shape=(content_length,),
            dtype=np.int64,
            generator=data_generator,
        )
    )
    array = ak.Array(ak.contents.ListOffsetArray(offsets, data))
    assert array.to_list() == [[1, 2], [3, 4], [5], [6]]

    buffers = {"node0-offsets": offset_generator, "node1-data": data_generator}
    form = ak.forms.ListOffsetForm(
        "i64", ak.forms.NumpyForm("int64", form_key="node1"), form_key="node0"
    )
    array = ak.from_buffers(form, 4, buffers)
    assert array.to_list() == [[1, 2], [3, 4], [5], [6]]

    offsets = VirtualNDArray(
        Numpy.instance(),
        shape=(offsets_length,),
        dtype=np.int64,
        generator=offset_generator,
    )
    data = VirtualNDArray(
        Numpy.instance(),
        shape=(content_length,),
        dtype=np.int64,
        generator=data_generator,
    )
    buffers = {"node0-offsets": offsets, "node1-data": data}
    form = ak.forms.ListOffsetForm(
        "i64", ak.forms.NumpyForm("int64", form_key="node1"), form_key="node0"
    )
    array = ak.from_buffers(form, 4, buffers)
    assert array.to_list() == [[1, 2], [3, 4], [5], [6]]