File: test_0549_numba_array_asarray.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 (47 lines) | stat: -rw-r--r-- 1,134 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
# 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

numba = pytest.importorskip("numba")

ak.numba.register_and_check()


def test_array():
    @numba.njit
    def f1(x):
        return np.array(x)

    assert isinstance(
        f1(ak.highlevel.Array([[[1], [2], [3]], [[4], [5], [6]]])), np.ndarray
    )
    assert f1(ak.highlevel.Array([[[1], [2], [3]], [[4], [5], [6]]])).tolist() == [
        [[1], [2], [3]],
        [[4], [5], [6]],
    ]
    assert f1(ak.highlevel.Array([[1, 2, 3], [4, 5, 6]])).tolist() == [
        [1, 2, 3],
        [4, 5, 6],
    ]
    assert f1(ak.highlevel.Array([1, 2, 3, 4, 5, 6])).tolist() == [1, 2, 3, 4, 5, 6]

    with pytest.raises(ValueError):
        f1(ak.highlevel.Array([[1, 2, 3, 4], [5, 6]]))


def test_asarray():
    @numba.njit
    def f1(x):
        return np.asarray(x[-1][1:])

    akarray = ak.highlevel.Array(
        [[1.1, 2.2, 3.3], [], [4.4, 5.5], [6.6, 7.7, 8.8, 9.9]]
    )
    nparray = f1(akarray)

    assert nparray.tolist() == [7.7, 8.8, 9.9]