Stack arrays along a new axis Given a sequence of dask arrays, form a new dask array by stacking them along a new dimension (axis=0 by default) Parameters ---------- seq: list of dask.arrays axis: int Dimension along which to align all of the arrays allow_u
(seq, axis=0, allow_unknown_chunksizes=False)
| 1361 | |
| 1362 | |
| 1363 | def stack(seq, axis=0, allow_unknown_chunksizes=False): |
| 1364 | """ |
| 1365 | Stack arrays along a new axis |
| 1366 | |
| 1367 | Given a sequence of dask arrays, form a new dask array by stacking them |
| 1368 | along a new dimension (axis=0 by default) |
| 1369 | |
| 1370 | Parameters |
| 1371 | ---------- |
| 1372 | seq: list of dask.arrays |
| 1373 | axis: int |
| 1374 | Dimension along which to align all of the arrays |
| 1375 | allow_unknown_chunksizes: bool |
| 1376 | Allow unknown chunksizes, such as come from converting from dask |
| 1377 | dataframes. Dask.array is unable to verify that chunks line up. If |
| 1378 | data comes from differently aligned sources then this can cause |
| 1379 | unexpected results. |
| 1380 | |
| 1381 | Examples |
| 1382 | -------- |
| 1383 | |
| 1384 | Create slices |
| 1385 | |
| 1386 | >>> import dask.array as da |
| 1387 | >>> import numpy as np |
| 1388 | |
| 1389 | >>> data = [da.from_array(np.ones((4, 4)), chunks=(2, 2)) |
| 1390 | ... for i in range(3)] |
| 1391 | |
| 1392 | >>> x = da.stack(data, axis=0) |
| 1393 | >>> x.shape |
| 1394 | (3, 4, 4) |
| 1395 | |
| 1396 | >>> da.stack(data, axis=1).shape |
| 1397 | (4, 3, 4) |
| 1398 | |
| 1399 | >>> da.stack(data, axis=-1).shape |
| 1400 | (4, 4, 3) |
| 1401 | |
| 1402 | Result is a new dask Array |
| 1403 | |
| 1404 | See Also |
| 1405 | -------- |
| 1406 | concatenate |
| 1407 | """ |
| 1408 | from dask.array import wrap |
| 1409 | |
| 1410 | seq = [asarray(a, allow_unknown_chunksizes=allow_unknown_chunksizes) for a in seq] |
| 1411 | |
| 1412 | if not seq: |
| 1413 | raise ValueError("Need array(s) to stack") |
| 1414 | if not allow_unknown_chunksizes and not all(x.shape == seq[0].shape for x in seq): |
| 1415 | idx = first(i for i in enumerate(seq) if i[1].shape != seq[0].shape) |
| 1416 | raise ValueError( |
| 1417 | "Stacked arrays must have the same shape. The first array had shape " |
| 1418 | f"{seq[0].shape}, while array {idx[0] + 1} has shape {idx[1].shape}." |
| 1419 | ) |
| 1420 |
no test coverage detected
searching dependent graphs…