File: test_0002_minimal_listarray.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 (103 lines) | stat: -rw-r--r-- 3,427 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
# 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():
    data = np.array([0, 2, 2, 3], dtype="i8")
    offsets = ak.index.Index64(data)

    assert np.asarray(offsets.data).tolist() == [0, 2, 2, 3]
    assert offsets[0] == 0
    assert offsets[1] == 2
    assert offsets[2] == 2
    assert offsets[3] == 3
    data[2] = 999
    assert offsets[2] == 999

    data = np.array([0, 2, 2, 3], dtype="i4")
    offsets = ak.index.Index32(data)

    assert np.asarray(offsets.data).tolist() == [0, 2, 2, 3]
    assert offsets[0] == 0
    assert offsets[1] == 2
    assert offsets[2] == 2
    assert offsets[3] == 3
    data[2] = 999
    assert offsets[2] == 999

    content = ak.contents.NumpyArray(np.arange(12).reshape(3, 4))
    data = np.array([0, 2, 2, 3], dtype="i4")
    offsets = ak.index.Index32(data)
    array = ak.contents.ListOffsetArray(offsets, content)

    assert ak.to_numpy(content).tolist() == [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]]
    assert ak.to_numpy(content[0]).tolist() == [0, 1, 2, 3]
    assert content.to_typetracer()[0].form == content[0].form
    assert ak.to_numpy(content[1]).tolist() == [4, 5, 6, 7]
    assert content.to_typetracer()[1].form == content[1].form
    assert ak.to_numpy(content[2]).tolist() == [8, 9, 10, 11]
    assert content.to_typetracer()[2].form == content[2].form
    assert [content[i][j] for i in range(3) for j in range(4)] == [
        0,
        1,
        2,
        3,
        4,
        5,
        6,
        7,
        8,
        9,
        10,
        11,
    ]

    assert ak.to_numpy(array[0]).tolist() == [[0, 1, 2, 3], [4, 5, 6, 7]]
    assert array.to_typetracer()[0].form == array[0].form
    assert ak.to_numpy(array[1]).tolist() == []
    assert array.to_typetracer()[1].form == array[1].form
    assert ak.to_numpy(array[2]).tolist() == [[8, 9, 10, 11]]
    assert array.to_typetracer()[2].form == array[2].form
    assert ak.to_numpy(array[1:3][0]).tolist() == []
    assert array.to_typetracer()[1:3][0].form == array[1:3][0].form
    assert ak.to_numpy(array[1:3][1]).tolist() == [[8, 9, 10, 11]]
    assert array.to_typetracer()[1:3][1].form == array[1:3][1].form
    assert ak.to_numpy(array[2:3][0]).tolist() == [[8, 9, 10, 11]]
    assert array.to_typetracer()[2:3][0].form == array[2:3][0].form


def test_len():
    offsets = ak.index.Index32(np.array([0, 2, 2, 3], dtype="i4"))
    content = ak.contents.NumpyArray(np.arange(12).reshape(4, 3))
    array = ak.contents.ListOffsetArray(offsets, content)

    assert len(content) == 4
    assert len(array) == 3


def test_members():
    offsets = ak.index.Index32(np.array([0, 2, 2, 3], dtype="i4"))
    content = ak.contents.NumpyArray(np.arange(12).reshape(3, 4))
    array = ak.contents.ListOffsetArray(offsets, content)
    new = ak.contents.ListOffsetArray(offsets, array)

    assert np.asarray(array.offsets.data).tolist() == [0, 2, 2, 3]
    assert ak.to_numpy(array.content).tolist() == [
        [0, 1, 2, 3],
        [4, 5, 6, 7],
        [8, 9, 10, 11],
    ]

    assert np.asarray(new.offsets.data).tolist() == [0, 2, 2, 3]
    assert np.asarray(new.content.offsets.data).tolist() == [0, 2, 2, 3]
    assert ak.to_numpy(new.content.content).tolist() == [
        [0, 1, 2, 3],
        [4, 5, 6, 7],
        [8, 9, 10, 11],
    ]