File: test_2297_common_backend.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 (79 lines) | stat: -rw-r--r-- 2,294 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
# BSD 3-Clause License; see https://github.com/scikit-hep/awkward/blob/main/LICENSE

from __future__ import annotations

import pytest

import awkward as ak

jax = pytest.importorskip("jax")
ak.jax.register_and_check()

left = ak.Array([1, 2, 3], backend="jax")
right = ak.Array([100, 200, 300.0], backend="cpu")
typetracer = ak.Array([44, 55, 66], backend="typetracer")


def test_concatenate():
    with pytest.raises(
        ValueError, match="cannot operate on arrays with incompatible backends"
    ):
        ak.concatenate((left, right))

    result = ak.concatenate((left, typetracer))
    assert ak.backend(result) == "typetracer"


def test_broadcast_arrays():
    with pytest.raises(
        ValueError, match="cannot operate on arrays with incompatible backends"
    ):
        ak.broadcast_arrays(left, right)

    result = ak.broadcast_arrays(left, typetracer)
    assert all(ak.backend(x) == "typetracer" for x in result)


def test_broadcast_fields():
    with pytest.raises(
        ValueError, match="cannot operate on arrays with incompatible backends"
    ):
        ak.broadcast_fields(left, right)

    result = ak.broadcast_fields(left, typetracer)
    assert all(ak.backend(x) == "typetracer" for x in result)


def test_cartesian():
    with pytest.raises(
        ValueError, match="cannot operate on arrays with incompatible backends"
    ):
        ak.cartesian((left, right), axis=0)

    result = ak.cartesian((left, typetracer), axis=0)
    assert ak.backend(result) == "typetracer"


def test_to_rdataframe():
    pytest.importorskip("ROOT")
    array = ak.Array([100, 200, 300.0], backend="typetracer")
    with pytest.raises(
        TypeError,
        match="from an nplike without known data to an nplike with known data",
    ):
        ak.to_rdataframe({"array": array})


def test_transform():
    def apply(layouts, backend, **kwargs):
        if not all(x.is_numpy for x in layouts):
            return
        return tuple(x.copy(data=backend.nplike.asarray(x) * 2) for x in layouts)

    with pytest.raises(
        ValueError, match="cannot operate on arrays with incompatible backends"
    ):
        ak.transform(apply, left, right)

    result = ak.transform(apply, left, typetracer)
    assert all(ak.backend(x) == "typetracer" for x in result)