File: what-is-an-awkward-array.md

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 (300 lines) | stat: -rw-r--r-- 5,730 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
---
jupytext:
  text_representation:
    extension: .md
    format_name: myst
    format_version: 0.13
    jupytext_version: 1.16.1
kernelspec:
  display_name: Python 3 (ipykernel)
  language: python
  name: python3
---

# What is an "Awkward" Array?


```{code-cell} ipython3
import numpy as np
import awkward as ak
```

## Versatile Arrays
Awkward Arrays are general tree-like data structures, like JSON, but contiguous in memory and operated upon with compiled, vectorized code like NumPy.

They look like NumPy arrays:


```{code-cell} ipython3
ak.Array([1, 2, 3])
```

Like NumPy, they can have multiple dimensions:


```{code-cell} ipython3
ak.Array([
    [1, 2, 3],
    [4, 5, 6]
])
```

These dimensions can have varying lengths; arrays can be [ragged](https://en.wikipedia.org/wiki/Jagged_array):


```{code-cell} ipython3
ak.Array([
    [1, 2, 3],
    [4],
    [5, 6]
])
```

Each dimension can contain missing values:


```{code-cell} ipython3
ak.Array([
    [1, 2, 3],
    [4],
    [5, 6, None]
])
```

Awkward Arrays can store _numbers_:


```{code-cell} ipython3
ak.Array([
    [3, 141], 
    [59, 26, 535], 
    [8]
])
```

They can also work with _dates_:


```{code-cell} ipython3
ak.Array(
    [
        [np.datetime64("1815-12-10"), np.datetime64("1969-07-16")],
        [np.datetime64("1564-04-26")],
    ]
)
```

They can even work with _strings_:


```{code-cell} ipython3
ak.Array(
    [
        [
            "Benjamin List",
            "David MacMillan",
        ],
        [
            "Emmanuelle Charpentier",
            "Jennifer A. Doudna",
        ],
    ]
)
```

Awkward Arrays can have structure through _records_:


```{code-cell} ipython3
ak.Array(
    [
        [
            {"name": "Benjamin List", "age": 53},
            {"name": "David MacMillan", "age": 53},
        ],
        [
            {"name": "Emmanuelle Charpentier", "age": 52},
            {"name": "Jennifer A. Doudna", "age": 57},
        ],
        [
            {"name": "Akira Yoshino", "age": 73},
            {"name": "M. Stanley Whittingham", "age": 79},
            {"name": "John B. Goodenough", "age": 98},
        ],
    ]
)
```

In fact, Awkward Arrays can represent many kinds of jagged data. They can possess complex structures that mix records, and primitive types.


```{code-cell} ipython3
ak.Array(
    [
        [
            {
                "name": "Benjamin List",
                "age": 53,
                "institutions": [
                    "University of Cologne",
                    "Max Planck Institute for Coal Research",
                    "Hokkaido University",
                ],
            },
            {
                "name": "David MacMillan",
                "age": 53,
                "institutions": None,
            },
        ]
    ]
)
```

They can even contain unions!


```{code-cell} ipython3
ak.Array(
    [
        [np.datetime64("1815-12-10"), "Cassini"],
        [np.datetime64("1564-04-26")],
    ]
)
```

## NumPy-like interface

Awkward Array _looks like_ NumPy. It behaves identically to NumPy for regular arrays


```{code-cell} ipython3
x = ak.Array([
    [1, 2, 3],
    [4, 5, 6]
]);
```


```{code-cell} ipython3
ak.sum(x, axis=-1)
```

providing a similar high-level API, and implementing the [ufunc](https://numpy.org/doc/stable/reference/ufuncs.html) mechanism:


```{code-cell} ipython3
powers_of_two = ak.Array(
    [
        [1, 2, 4],
        [None, 8],
        [16],
    ]
);
```


```{code-cell} ipython3
ak.sum(powers_of_two)
```

But generalises to the tricky kinds of data that NumPy struggles to work with. It can perform reductions through varying length lists:

![](example-reduction-sum.svg)


```{code-cell} ipython3
ak.sum(powers_of_two, axis=0)
```

## Lightweight structures
Awkward makes it east to pull apart record structures:


```{code-cell} ipython3
nobel_prize_winner = ak.Array(
    [
        [
            {"name": "Benjamin List", "age": 53},
            {"name": "David MacMillan", "age": 53},
        ],
        [
            {"name": "Emmanuelle Charpentier", "age": 52},
            {"name": "Jennifer A. Doudna", "age": 57},
        ],
        [
            {"name": "Akira Yoshino", "age": 73},
            {"name": "M. Stanley Whittingham", "age": 79},
            {"name": "John B. Goodenough", "age": 98},
        ],
    ]
);
```


```{code-cell} ipython3
nobel_prize_winner.name
```


```{code-cell} ipython3
nobel_prize_winner.age
```

These records are lightweight, and simple to compose:


```{code-cell} ipython3
nobel_prize_winner_with_birth_year = ak.zip({
    "name": nobel_prize_winner.name,
    "age": nobel_prize_winner.age,
    "birth_year": 2021 - nobel_prize_winner.age
});
```


```{code-cell} ipython3
nobel_prize_winner_with_birth_year.show()
```

## High performance
Like NumPy, Awkward Array performs computations in fast, optimised kernels.


```{code-cell} ipython3
large_array = ak.Array([[1, 2, 3], [], [4, 5]] * 1_000_000)
```

We can compute the sum in `3.37 ms ± 107 µs` on a reference CPU:


```{code-cell} ipython3
ak.sum(large_array)
```

The same sum can be computed with pure-Python over the flattened array in `369 ms ± 8.07 ms`:


```{code-cell} ipython3
large_flat_array = ak.ravel(large_array)

sum(large_flat_array)
```

These performance values are not benchmarks; they are only an indication of the speed of Awkward Array.

Some problems are hard to solve with array-oriented programming. Awkward Array supports [Numba](https://numba.pydata.org/) out of the box:

```{code-cell} ipython3
import numba as nb

@nb.njit
def cumulative_sum(arr):
    result = 0
    for x in arr:
        for y in x:
            result += y
    return result
    
cumulative_sum(large_array)
```