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
|
# 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
def test_reducers():
array = ak.Array([[{"rho": -1.1, "phi": -0.1}, {"rho": 1.1, "phi": 0.1}]])
with pytest.raises(TypeError):
assert ak.to_list(ak.operations.all(array, axis=1)) == [
{"phi": True, "rho": True}
]
with pytest.raises(TypeError):
assert ak.to_list(ak.operations.any(array, axis=1)) == [
{"phi": True, "rho": True}
]
with pytest.raises(TypeError):
assert ak.to_list(ak.operations.argmax(array, axis=1)) == [
{"phi": True, "rho": True}
]
with pytest.raises(TypeError):
assert ak.to_list(ak.operations.argmin(array, axis=1)) == [{"phi": 0, "rho": 0}]
with pytest.raises(TypeError):
assert ak.to_list(ak.operations.count_nonzero(array, axis=1)) == [
{"phi": 2, "rho": 2}
]
with pytest.raises(TypeError):
assert ak.to_list(ak.operations.count(array, axis=1)) == [{"phi": 2, "rho": 2}]
with pytest.raises(TypeError):
assert ak.to_list(ak.operations.max(array, axis=1)) == [
{"phi": 0.1, "rho": 1.1}
]
with pytest.raises(TypeError):
assert ak.to_list(ak.operations.min(array, axis=1)) == [
{"phi": -0.1, "rho": -1.1}
]
with pytest.raises(TypeError):
assert ak.to_list(ak.operations.prod(array, axis=1)) == [
{"phi": -0.010000000000000002, "rho": -1.2100000000000002}
]
with pytest.raises(TypeError):
assert ak.to_list(ak.operations.sum(array, axis=1)) == [
{"phi": 0.0, "rho": 0.0}
]
def test_overloaded_reducers():
def overload_add(array, mask):
return ak.contents.RecordArray(
[
ak.sum(array["rho"], axis=-1, mask_identity=False, highlevel=False),
ak.sum(array["phi"], axis=-1, mask_identity=False, highlevel=False),
],
["rho", "phi"],
)
behavior = {}
behavior[ak.sum, "VectorArray2D"] = overload_add
array = ak.Array(
[[{"rho": -1.1, "phi": -0.1}, {"rho": 1.1, "phi": 0.1}]],
with_name="VectorArray2D",
behavior=behavior,
)
assert ak.to_list(ak.sum(array, axis=1)) == [{"rho": 0, "phi": 0}]
with pytest.raises(
TypeError, match=r"overloads for custom types: VectorArray2D, int"
):
ak.to_list(array + 1)
def overload_add2(array, mask):
return ak.contents.RecordArray(
[ak.contents.NumpyArray(np.asarray([2.4, 3, 4.5, 6], dtype=np.float64))],
["rho"],
)
behavior = {}
behavior[ak.sum, "VectorArray2D"] = overload_add2
array = ak.Array(
[[{"rho": -1.1, "phi": -0.1}, {"rho": 1.1, "phi": 0.1}]],
with_name="VectorArray2D",
behavior=behavior,
)
assert ak.to_list(ak.sum(array, axis=1)) == [{"rho": 2.4}]
array = ak.highlevel.Array(
ak.contents.ByteMaskedArray(
ak.index.Index8(np.array([True, True])),
ak.contents.RecordArray(
[
ak.contents.NumpyArray(np.arange(2)),
ak.contents.IndexedOptionArray(
ak.index.Index64(np.array([0, -1])),
ak.contents.NumpyArray(np.array([1], dtype=np.int64)),
),
],
["a", "b"],
),
valid_when=True,
)
)
with pytest.raises(TypeError, match=r"overloads for custom types: a, b"):
ak.to_list(ak.sum(array, axis=0))
def overload_argmax(array, mask):
return ak.argmax(array["rho"], axis=-1, mask_identity=False, highlevel=False)
behavior = {}
behavior[ak.argmax, "VectorArray2D"] = overload_argmax
array = ak.Array(
[
[
{"rho": -1.1, "phi": -0.1},
{"rho": 1.1, "phi": 0.1},
{"rho": -1.1, "phi": 0.3},
],
[
{"rho": 1.1, "phi": 0.1},
{"rho": -1.1, "phi": 0.3},
{"rho": -1.1, "phi": -0.1},
],
],
with_name="VectorArray2D",
behavior=behavior,
)
assert ak.to_list(ak.argmax(array, axis=1)) == [1, 0]
|