File: test_1508_awkward_from_rdataframe.py

package info (click to toggle)
python-awkward 2.6.5-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 23,088 kB
  • sloc: python: 148,689; cpp: 33,562; sh: 432; makefile: 21; javascript: 8
file content (184 lines) | stat: -rw-r--r-- 4,945 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
# BSD 3-Clause License; see https://github.com/scikit-hep/awkward/blob/main/LICENSE

from __future__ import annotations

import sys

import numpy as np
import pytest

import awkward as ak
import awkward._connect.cling
import awkward._lookup

ROOT = pytest.importorskip("ROOT")

ROOT.ROOT.EnableImplicitMT(1)

compiler = ROOT.gInterpreter.Declare


def test_refcount():
    array = ak.Array([[[1], [2]], [[3], [4, 5]]])

    assert [sys.getrefcount(x) == 2 for x in (array)]

    data_frame = ak.to_rdataframe({"x": array})

    assert data_frame.GetColumnType("x").startswith("awkward::ListArray_")

    column_type = data_frame.GetColumnType("x")
    result_ptrs = data_frame.Take[column_type]("x")
    view = result_ptrs.begin()
    lookup = result_ptrs.begin().lookup()
    generator = lookup["x"].generator

    column_type = data_frame.GetColumnType("x")
    result_ptrs = data_frame.Take[column_type]("x")
    view = result_ptrs.begin()
    lookup = result_ptrs.begin().lookup()
    generator = lookup["x"].generator

    array_out = ak.from_rdataframe(
        data_frame,
        columns=("x",),
    )
    assert array.to_list() == array_out["x"].to_list()

    assert [
        sys.getrefcount(x) == 2
        for x in (
            array,
            array_out,
            lookup,
            generator,
            view,
            result_ptrs.begin().lookup(),
        )
    ]

    for _ in range(3):

        def f1(x):
            return 3.14

        for _ in range(10):
            f1(result_ptrs.begin().lookup())
            assert [
                sys.getrefcount(x) == 2
                for x in (
                    array,
                    array_out,
                    lookup,
                    generator,
                    view,
                    result_ptrs.begin().lookup(),
                )
            ]

    for _ in range(3):

        def f2(x):
            return x

        for _ in range(10):
            y = f2(result_ptrs.begin().lookup())
            assert [
                sys.getrefcount(x) == 2
                for x in (
                    array,
                    array_out,
                    lookup,
                    generator,
                    view,
                    result_ptrs.begin().lookup(),
                )
            ]

    for _ in range(3):

        def f3(x):
            return x, x

        for _ in range(10):
            y = f3(  # noqa: F841 (checking reference count)
                result_ptrs.begin().lookup()
            )
            assert [
                sys.getrefcount(x) == 2
                for x in (
                    array,
                    array_out,
                    lookup,
                    generator,
                    view,
                    result_ptrs.begin().lookup(),
                )
            ]


def test_data_frame_vec_of_vec_of_integers():
    ak_array_in = ak.Array([[[1], [2]], [[3], [4, 5]]])

    data_frame = ak.to_rdataframe({"x": ak_array_in})

    assert data_frame.GetColumnType("x").startswith("awkward::ListArray_")

    ak_array_out = ak.from_rdataframe(
        data_frame,
        columns=("x",),
    )
    assert ak_array_in.to_list() == ak_array_out["x"].to_list()


def test_data_frame_NumpyArray():
    array = ak.contents.numpyarray.NumpyArray(
        np.array([0.0, 1.1, 2.2, 3.3]),
        parameters={"some": "stuff", "other": [1, 2, "three"]},
    )

    layout = array
    generator = ak._connect.cling.togenerator(layout.form, flatlist_as_rvec=False)
    lookup = ak._lookup.Lookup(layout)
    generator.generate(compiler)

    array_out = generator.tolayout(lookup, 0, ())

    assert array.to_list() == array_out.to_list()


def test_data_frame_ListOffsetArray_NumpyArray():
    array = ak.contents.listoffsetarray.ListOffsetArray(
        ak.index.Index(np.array([1, 4, 4, 6, 7], np.int64)),
        ak.contents.numpyarray.NumpyArray(
            np.array([6.6, 1.1, 2.2, 3.3, 4.4, 5.5, 7.7])
        ),
    )

    layout = array
    generator = ak._connect.cling.togenerator(layout.form, flatlist_as_rvec=False)
    lookup = ak._lookup.Lookup(layout)
    generator.generate(compiler)

    array_out = generator.tolayout(lookup, 0, ())
    assert array.to_list() == array_out.to_list()


def test_nested_ListOffsetArray_NumpyArray():
    array = ak.contents.ListOffsetArray(
        ak.index.Index64(np.array([0, 1, 5], dtype=np.int64)),
        ak.contents.listoffsetarray.ListOffsetArray(
            ak.index.Index(np.array([1, 1, 4, 4, 6, 7], np.int64)),
            ak.contents.numpyarray.NumpyArray(
                np.array([6.6, 1.1, 2.2, 3.3, 4.4, 5.5, 7.7])
            ),
        ),
    )

    layout = array
    generator = ak._connect.cling.togenerator(layout.form, flatlist_as_rvec=False)
    lookup = ak._lookup.Lookup(layout)
    generator.generate(compiler)

    array_out = generator.tolayout(lookup, 0, ())
    assert array.to_list() == array_out.to_list()