forked from scikit-hep/awkward
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_0046-start-indexedarray.py
157 lines (130 loc) · 10.5 KB
/
test_0046-start-indexedarray.py
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
# BSD 3-Clause License; see https://github.com/jpivarski/awkward-1.0/blob/master/LICENSE
from __future__ import absolute_import
import sys
import pytest
import numpy
import awkward1
def test_basic():
content = awkward1.layout.NumpyArray(numpy.array([0.0, 1.1, 2.2, 3.3, 4.4]))
ind = numpy.array([2, 2, 0, 3, 4], dtype=numpy.int32)
index = awkward1.layout.Index32(ind)
array = awkward1.layout.IndexedArray32(index, content)
assert awkward1.tolist(array) == [2.2, 2.2, 0.0, 3.3, 4.4]
ind[3] = 1
assert awkward1.tolist(array) == [2.2, 2.2, 0.0, 1.1, 4.4]
ind = numpy.array([2, 2, 0, 3, 4], dtype=numpy.uint32)
index = awkward1.layout.IndexU32(ind)
array = awkward1.layout.IndexedArrayU32(index, content)
assert awkward1.tolist(array) == [2.2, 2.2, 0.0, 3.3, 4.4]
ind[3] = 1
assert awkward1.tolist(array) == [2.2, 2.2, 0.0, 1.1, 4.4]
ind = numpy.array([2, 2, 0, 3, 4], dtype=numpy.int64)
index = awkward1.layout.Index64(ind)
array = awkward1.layout.IndexedArray64(index, content)
assert awkward1.tolist(array) == [2.2, 2.2, 0.0, 3.3, 4.4]
ind[3] = 1
assert awkward1.tolist(array) == [2.2, 2.2, 0.0, 1.1, 4.4]
ind = numpy.array([2, 2, 0, 3, 4], dtype=numpy.int32)
index = awkward1.layout.Index32(ind)
array = awkward1.layout.IndexedOptionArray32(index, content)
assert awkward1.tolist(array) == [2.2, 2.2, 0.0, 3.3, 4.4]
ind[3] = 1
assert awkward1.tolist(array) == [2.2, 2.2, 0.0, 1.1, 4.4]
ind = numpy.array([2, 2, 0, 3, 4], dtype=numpy.int64)
index = awkward1.layout.Index64(ind)
array = awkward1.layout.IndexedOptionArray64(index, content)
assert awkward1.tolist(array) == [2.2, 2.2, 0.0, 3.3, 4.4]
ind[3] = 1
assert awkward1.tolist(array) == [2.2, 2.2, 0.0, 1.1, 4.4]
def test_type():
content = awkward1.layout.NumpyArray(numpy.array([0.0, 1.1, 2.2, 3.3, 4.4]))
index = awkward1.layout.Index32(numpy.array([2, 2, 0, 3, 4], dtype=numpy.int32))
array = awkward1.layout.IndexedArray32(index, content)
assert awkward1.typeof(array) == awkward1.types.PrimitiveType("float64")
array = awkward1.layout.IndexedOptionArray32(index, content)
assert awkward1.typeof(array) == awkward1.types.OptionType(awkward1.types.PrimitiveType("float64"))
def test_null():
content = awkward1.layout.NumpyArray(numpy.array([0.0, 1.1, 2.2, 3.3, 4.4]))
index = awkward1.layout.Index64(numpy.array([2, 2, 0, -1, 4], dtype=numpy.int64))
array = awkward1.layout.IndexedOptionArray64(index, content)
assert awkward1.tolist(array) == [2.2, 2.2, 0.0, None, 4.4]
def test_carry():
content = awkward1.layout.NumpyArray(numpy.array([0.0, 1.1, 2.2, 3.3, 4.4]))
index = awkward1.layout.Index64(numpy.array([2, 2, 0, 3, 4], dtype=numpy.int64))
indexedarray = awkward1.layout.IndexedArray64(index, content)
offsets = awkward1.layout.Index64(numpy.array([0, 3, 3, 5], dtype=numpy.int64))
listoffsetarray = awkward1.layout.ListOffsetArray64(offsets, indexedarray)
assert awkward1.tolist(listoffsetarray) == [[2.2, 2.2, 0.0], [], [3.3, 4.4]]
assert awkward1.tolist(listoffsetarray[::-1]) == [[3.3, 4.4], [], [2.2, 2.2, 0.0]]
assert awkward1.tolist(listoffsetarray[[2, 0]]) == [[3.3, 4.4], [2.2, 2.2, 0.0]]
assert awkward1.tolist(listoffsetarray[[2, 0], 1]) == [4.4, 2.2] # invokes carry
assert awkward1.tolist(listoffsetarray[2:, 1]) == [4.4] # invokes carry
index = awkward1.layout.Index64(numpy.array([2, 2, 0, 3, -1], dtype=numpy.int64))
indexedarray = awkward1.layout.IndexedOptionArray64(index, content)
listoffsetarray = awkward1.layout.ListOffsetArray64(offsets, indexedarray)
assert awkward1.tolist(listoffsetarray) == [[2.2, 2.2, 0.0], [], [3.3, None]]
assert awkward1.tolist(listoffsetarray[::-1]) == [[3.3, None], [], [2.2, 2.2, 0.0]]
assert awkward1.tolist(listoffsetarray[[2, 0]]) == [[3.3, None], [2.2, 2.2, 0.0]]
assert awkward1.tolist(listoffsetarray[[2, 0], 1]) == [None, 2.2] # invokes carry
assert awkward1.tolist(listoffsetarray[2:, 1]) == [None] # invokes carry
def test_others():
content = awkward1.layout.NumpyArray(numpy.array([[0.0, 0.0], [0.1, 1.0], [0.2, 2.0], [0.3, 3.0], [0.4, 4.0], [0.5, 5.0]]))
index = awkward1.layout.Index64(numpy.array([4, 0, 3, 1, 3], dtype=numpy.int64))
indexedarray = awkward1.layout.IndexedArray64(index, content)
assert indexedarray[3, 0] == 0.1
assert indexedarray[3, 1] == 1.0
assert awkward1.tolist(indexedarray[3, ::-1]) == [1.0, 0.1]
assert awkward1.tolist(indexedarray[3, [1, 1, 0]]) == [1.0, 1.0, 0.1]
assert awkward1.tolist(indexedarray[3:, 0]) == [0.1, 0.3]
assert awkward1.tolist(indexedarray[3:, 1]) == [1.0, 3.0]
assert awkward1.tolist(indexedarray[3:, ::-1]) == [[1.0, 0.1], [3.0, 0.3]]
assert awkward1.tolist(indexedarray[3:, [1, 1, 0]]) == [[1.0, 1.0, 0.1], [3.0, 3.0, 0.3]]
def test_missing():
content = awkward1.layout.NumpyArray(numpy.array([[0.0, 0.0], [0.1, 1.0], [0.2, 2.0], [0.3, 3.0], [0.4, 4.0], [0.5, 5.0]]))
index = awkward1.layout.Index64(numpy.array([4, 0, 3, -1, 3], dtype=numpy.int64))
indexedarray = awkward1.layout.IndexedOptionArray64(index, content)
assert awkward1.tolist(indexedarray[3:, 0]) == [None, 0.3]
assert awkward1.tolist(indexedarray[3:, 1]) == [None, 3.0]
assert awkward1.tolist(indexedarray[3:, ::-1]) == [None, [3.0, 0.3]]
assert awkward1.tolist(indexedarray[3:, [1, 1, 0]]) == [None, [3.0, 3.0, 0.3]]
def test_highlevel():
content = awkward1.layout.NumpyArray(numpy.array([0.0, 1.1, 2.2, 3.3, 4.4]))
index = awkward1.layout.Index64(numpy.array([2, 2, 0, -1, 4], dtype=numpy.int64))
array = awkward1.Array(awkward1.layout.IndexedOptionArray64(index, content), checkvalid=True)
assert awkward1.tolist(array) == [2.2, 2.2, 0.0, None, 4.4]
assert str(array) == "[2.2, 2.2, 0, None, 4.4]"
assert repr(array) == "<Array [2.2, 2.2, 0, None, 4.4] type='5 * ?float64'>"
def test_builder():
assert awkward1.tolist(awkward1.Array([1.1, 2.2, 3.3, None, 4.4], checkvalid=True)) == [1.1, 2.2, 3.3, None, 4.4]
assert awkward1.tolist(awkward1.Array([None, 2.2, 3.3, None, 4.4], checkvalid=True)) == [None, 2.2, 3.3, None, 4.4]
assert awkward1.tolist(awkward1.Array([[1.1, 2.2, 3.3], [], [None, 4.4]], checkvalid=True)) == [[1.1, 2.2, 3.3], [], [None, 4.4]]
assert awkward1.tolist(awkward1.Array([[1.1, 2.2, 3.3], [], None, [None, 4.4]], checkvalid=True)) == [[1.1, 2.2, 3.3], [], None, [None, 4.4]]
assert awkward1.tolist(awkward1.Array([[1.1, 2.2, 3.3], None, [], [None, 4.4]], checkvalid=True)) == [[1.1, 2.2, 3.3], None, [], [None, 4.4]]
assert awkward1.tolist(awkward1.Array([[1.1, 2.2, 3.3], None, [], [None, 4.4]], checkvalid=True)) != [[1.1, 2.2, 3.3], [], None, [None, 4.4]]
assert awkward1.tolist(awkward1.Array([[None, 1.1, 2.2, 3.3], [], [None, 4.4]], checkvalid=True)) == [[None, 1.1, 2.2, 3.3], [], [None, 4.4]]
assert awkward1.tolist(awkward1.Array([[None, 1.1, 2.2, 3.3], [], None, [None, 4.4]], checkvalid=True)) == [[None, 1.1, 2.2, 3.3], [], None, [None, 4.4]]
assert awkward1.tolist(awkward1.Array([[None, 1.1, 2.2, 3.3], None, [], [None, 4.4]], checkvalid=True)) == [[None, 1.1, 2.2, 3.3], None, [], [None, 4.4]]
assert awkward1.tolist(awkward1.Array([[None, 1.1, 2.2, 3.3], None, [], [None, 4.4]], checkvalid=True)) != [[None, 1.1, 2.2, 3.3], [], None, [None, 4.4]]
assert awkward1.tolist(awkward1.Array([None, [1.1, 2.2, 3.3], [], [None, 4.4]], checkvalid=True)) == [None, [1.1, 2.2, 3.3], [], [None, 4.4]]
assert awkward1.tolist(awkward1.Array([None, [1.1, 2.2, 3.3], [], None, [None, 4.4]], checkvalid=True)) == [None, [1.1, 2.2, 3.3], [], None, [None, 4.4]]
assert awkward1.tolist(awkward1.Array([None, [1.1, 2.2, 3.3], None, [], [None, 4.4]], checkvalid=True)) == [None, [1.1, 2.2, 3.3], None, [], [None, 4.4]]
assert awkward1.tolist(awkward1.Array([None, [1.1, 2.2, 3.3], None, [], [None, 4.4]], checkvalid=True)) != [None, [1.1, 2.2, 3.3], [], None, [None, 4.4]]
assert awkward1.tolist(awkward1.Array([None, None, None, None, None], checkvalid=True)) == [None, None, None, None, None]
assert awkward1.tolist(awkward1.Array([[None, None, None], [], [None, None]], checkvalid=True)) == [[None, None, None], [], [None, None]]
def test_json():
assert awkward1.tolist(awkward1.Array("[1.1, 2.2, 3.3, null, 4.4]", checkvalid=True)) == [1.1, 2.2, 3.3, None, 4.4]
assert awkward1.tolist(awkward1.Array("[null, 2.2, 3.3, null, 4.4]", checkvalid=True)) == [None, 2.2, 3.3, None, 4.4]
assert awkward1.tolist(awkward1.Array("[[1.1, 2.2, 3.3], [], [null, 4.4]]", checkvalid=True)) == [[1.1, 2.2, 3.3], [], [None, 4.4]]
assert awkward1.tolist(awkward1.Array("[[1.1, 2.2, 3.3], [], null, [null, 4.4]]", checkvalid=True)) == [[1.1, 2.2, 3.3], [], None, [None, 4.4]]
assert awkward1.tolist(awkward1.Array("[[1.1, 2.2, 3.3], null, [], [null, 4.4]]", checkvalid=True)) == [[1.1, 2.2, 3.3], None, [], [None, 4.4]]
assert awkward1.tolist(awkward1.Array("[[1.1, 2.2, 3.3], null, [], [null, 4.4]]", checkvalid=True)) != [[1.1, 2.2, 3.3], [], None, [None, 4.4]]
assert awkward1.tolist(awkward1.Array("[[null, 1.1, 2.2, 3.3], [], [null, 4.4]]", checkvalid=True)) == [[None, 1.1, 2.2, 3.3], [], [None, 4.4]]
assert awkward1.tolist(awkward1.Array("[[null, 1.1, 2.2, 3.3], [], null, [null, 4.4]]", checkvalid=True)) == [[None, 1.1, 2.2, 3.3], [], None, [None, 4.4]]
assert awkward1.tolist(awkward1.Array("[[null, 1.1, 2.2, 3.3], null, [], [null, 4.4]]", checkvalid=True)) == [[None, 1.1, 2.2, 3.3], None, [], [None, 4.4]]
assert awkward1.tolist(awkward1.Array("[[null, 1.1, 2.2, 3.3], null, [], [null, 4.4]]", checkvalid=True)) != [[None, 1.1, 2.2, 3.3], [], None, [None, 4.4]]
assert awkward1.tolist(awkward1.Array("[null, [1.1, 2.2, 3.3], [], [null, 4.4]]", checkvalid=True)) == [None, [1.1, 2.2, 3.3], [], [None, 4.4]]
assert awkward1.tolist(awkward1.Array("[null, [1.1, 2.2, 3.3], [], null, [null, 4.4]]", checkvalid=True)) == [None, [1.1, 2.2, 3.3], [], None, [None, 4.4]]
assert awkward1.tolist(awkward1.Array("[null, [1.1, 2.2, 3.3], null, [], [null, 4.4]]", checkvalid=True)) == [None, [1.1, 2.2, 3.3], None, [], [None, 4.4]]
assert awkward1.tolist(awkward1.Array("[null, [1.1, 2.2, 3.3], null, [], [null, 4.4]]", checkvalid=True)) != [None, [1.1, 2.2, 3.3], [], None, [None, 4.4]]
assert awkward1.tolist(awkward1.Array("[null, null, null, null, null]", checkvalid=True)) == [None, None, None, None, None]
assert awkward1.tolist(awkward1.Array("[[null, null, null], [], [null, null]]", checkvalid=True)) == [[None, None, None], [], [None, None]]