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
|
# 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
from awkward.types import ArrayType, ListType, NumpyType, OptionType, RegularType
def test_simple():
a = ak.from_numpy(np.array([[1, 2], [3, 4], [5, 6]]), regulararray=True)
b = ak.from_numpy(np.array([[7, 8], [9, 10]]), regulararray=True)
c = a.layout._mergemany([b.layout])
assert isinstance(c, ak.contents.RegularArray)
assert c.size == 2
assert ak.operations.to_list(c) == [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]
def test_regular_regular():
a1 = ak.from_json("[[0.0, 1.1], [2.2, 3.3]]")
a2 = ak.from_json("[[4.4, 5.5], [6.6, 7.7], [8.8, 9.9]]")
a1 = ak.to_regular(a1, axis=1)
a2 = ak.to_regular(a2, axis=1)
c = ak.concatenate([a1, a2])
assert c.to_list() == [[0.0, 1.1], [2.2, 3.3], [4.4, 5.5], [6.6, 7.7], [8.8, 9.9]]
assert c.type == ArrayType(RegularType(NumpyType("float64"), 2), 5)
def test_regular_option():
a1 = ak.from_json("[[0.0, 1.1], [2.2, 3.3]]")
a2 = ak.from_json("[[4.4, 5.5], [6.6, 7.7], null, [8.8, 9.9]]")
a1 = ak.to_regular(a1, axis=1)
a2 = ak.to_regular(a2, axis=1)
c = ak.concatenate([a1, a2])
assert c.to_list() == [
[0.0, 1.1],
[2.2, 3.3],
[4.4, 5.5],
[6.6, 7.7],
None,
[8.8, 9.9],
]
assert c.type == ArrayType(OptionType(RegularType(NumpyType("float64"), 2)), 6)
def test_option_regular():
a1 = ak.from_json("[[0.0, 1.1], null, [2.2, 3.3]]")
a2 = ak.from_json("[[4.4, 5.5], [6.6, 7.7], [8.8, 9.9]]")
a1 = ak.to_regular(a1, axis=1)
a2 = ak.to_regular(a2, axis=1)
c = ak.concatenate([a1, a2])
assert c.to_list() == [
[0.0, 1.1],
None,
[2.2, 3.3],
[4.4, 5.5],
[6.6, 7.7],
[8.8, 9.9],
]
assert c.type == ArrayType(OptionType(RegularType(NumpyType("float64"), 2)), 6)
def test_option_option():
a1 = ak.from_json("[[0.0, 1.1], null, [2.2, 3.3]]")
a2 = ak.from_json("[[4.4, 5.5], [6.6, 7.7], null, [8.8, 9.9]]")
a1 = ak.to_regular(a1, axis=1)
a2 = ak.to_regular(a2, axis=1)
c = ak.concatenate([a1, a2])
assert c.to_list() == [
[0.0, 1.1],
None,
[2.2, 3.3],
[4.4, 5.5],
[6.6, 7.7],
None,
[8.8, 9.9],
]
assert c.type == ArrayType(OptionType(RegularType(NumpyType("float64"), 2)), 7)
def test_regular_numpy():
a1 = ak.from_json("[[0.0, 1.1], [2.2, 3.3]]")
a2 = ak.Array(np.array([[4.4, 5.5], [6.6, 7.7], [8.8, 9.9]]))
a1 = ak.to_regular(a1, axis=1)
assert isinstance(a2.layout, ak.contents.NumpyArray)
c = ak.concatenate([a1, a2])
assert c.to_list() == [[0.0, 1.1], [2.2, 3.3], [4.4, 5.5], [6.6, 7.7], [8.8, 9.9]]
assert c.type == ArrayType(RegularType(NumpyType("float64"), 2), 5)
def test_numpy_regular():
a1 = ak.Array(np.array([[0.0, 1.1], [2.2, 3.3]]))
a2 = ak.from_json("[[4.4, 5.5], [6.6, 7.7], [8.8, 9.9]]")
assert isinstance(a1.layout, ak.contents.NumpyArray)
a2 = ak.to_regular(a2, axis=1)
c = ak.concatenate([a1, a2])
assert c.to_list() == [[0.0, 1.1], [2.2, 3.3], [4.4, 5.5], [6.6, 7.7], [8.8, 9.9]]
assert c.type == ArrayType(RegularType(NumpyType("float64"), 2), 5)
def test_regular_regular_axis1():
a1 = ak.from_json("[[0.0, 1.1], [2.2, 3.3]]")
a2 = ak.from_json("[[4.4, 5.5, 6.6], [7.7, 8.8, 9.9]]")
a1 = ak.to_regular(a1, axis=1)
a2 = ak.to_regular(a2, axis=1)
c = ak.concatenate([a1, a2], axis=1)
assert c.to_list() == [[0.0, 1.1, 4.4, 5.5, 6.6], [2.2, 3.3, 7.7, 8.8, 9.9]]
assert c.type == ArrayType(RegularType(NumpyType("float64"), 5), 2)
def test_option_regular_axis1():
a1 = ak.from_json("[[0.0, 1.1], null, [2.2, 3.3]]")
a2 = ak.from_json("[[4.4, 5.5, 6.6], [7, 8, 9], [7.7, 8.8, 9.9]]")
a1 = ak.to_regular(a1, axis=1)
a2 = ak.to_regular(a2, axis=1)
c = ak.concatenate([a1, a2], axis=1)
assert c.to_list() == [
[0.0, 1.1, 4.4, 5.5, 6.6],
[7, 8, 9],
[2.2, 3.3, 7.7, 8.8, 9.9],
]
assert c.type == ArrayType(ListType(NumpyType("float64")), 3)
def test_regular_option_axis1():
a1 = ak.from_json("[[0.0, 1.1], [7, 8], [2.2, 3.3]]")
a2 = ak.from_json("[[4.4, 5.5, 6.6], null, [7.7, 8.8, 9.9]]")
a1 = ak.to_regular(a1, axis=1)
a2 = ak.to_regular(a2, axis=1)
c = ak.concatenate([a1, a2], axis=1)
assert c.to_list() == [[0.0, 1.1, 4.4, 5.5, 6.6], [7, 8], [2.2, 3.3, 7.7, 8.8, 9.9]]
assert c.type == ArrayType(ListType(NumpyType("float64")), 3)
def test_option_option_axis1():
a1 = ak.from_json("[[0.0, 1.1], null, [2.2, 3.3]]")
a2 = ak.from_json("[[4.4, 5.5, 6.6], null, [7.7, 8.8, 9.9]]")
a1 = ak.to_regular(a1, axis=1)
a2 = ak.to_regular(a2, axis=1)
c = ak.concatenate([a1, a2], axis=1)
assert c.to_list() == [[0.0, 1.1, 4.4, 5.5, 6.6], [], [2.2, 3.3, 7.7, 8.8, 9.9]]
assert c.type == ArrayType(ListType(NumpyType("float64")), 3)
def test_regular_numpy_axis1():
a1 = ak.from_json("[[0.0, 1.1], [2.2, 3.3]]")
a2 = ak.Array(np.array([[4.4, 5.5, 6.6], [7.7, 8.8, 9.9]]))
a1 = ak.to_regular(a1, axis=1)
assert isinstance(a2.layout, ak.contents.NumpyArray)
c = ak.concatenate([a1, a2], axis=1)
assert c.to_list() == [[0.0, 1.1, 4.4, 5.5, 6.6], [2.2, 3.3, 7.7, 8.8, 9.9]]
assert c.type == ArrayType(RegularType(NumpyType("float64"), 5), 2)
def test_numpy_regular_axis1():
a1 = ak.Array(np.array([[0.0, 1.1], [2.2, 3.3]]))
a2 = ak.from_json("[[4.4, 5.5, 6.6], [7.7, 8.8, 9.9]]")
assert isinstance(a1.layout, ak.contents.NumpyArray)
a2 = ak.to_regular(a2, axis=1)
c = ak.concatenate([a1, a2], axis=1)
assert c.to_list() == [[0.0, 1.1, 4.4, 5.5, 6.6], [2.2, 3.3, 7.7, 8.8, 9.9]]
assert c.type == ArrayType(RegularType(NumpyType("float64"), 5), 2)
|