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)
| 5508 | |
| 5509 | |
| 5510 | def stack(seq, axis=0, allow_unknown_chunksizes=False): |
| 5511 | """ |
| 5512 | Stack arrays along a new axis |
| 5513 | |
| 5514 | Given a sequence of dask arrays, form a new dask array by stacking them |
| 5515 | along a new dimension (axis=0 by default) |
| 5516 | |
| 5517 | Parameters |
| 5518 | ---------- |
| 5519 | seq: list of dask.arrays |
| 5520 | axis: int |
| 5521 | Dimension along which to align all of the arrays |
| 5522 | allow_unknown_chunksizes: bool |
| 5523 | Allow unknown chunksizes, such as come from converting from dask |
| 5524 | dataframes. Dask.array is unable to verify that chunks line up. If |
| 5525 | data comes from differently aligned sources then this can cause |
| 5526 | unexpected results. |
| 5527 | |
| 5528 | Examples |
| 5529 | -------- |
| 5530 | |
| 5531 | Create slices |
| 5532 | |
| 5533 | >>> import dask.array as da |
| 5534 | >>> import numpy as np |
| 5535 | |
| 5536 | >>> data = [da.from_array(np.ones((4, 4)), chunks=(2, 2)) |
| 5537 | ... for i in range(3)] |
| 5538 | |
| 5539 | >>> x = da.stack(data, axis=0) |
| 5540 | >>> x.shape |
| 5541 | (3, 4, 4) |
| 5542 | |
| 5543 | >>> da.stack(data, axis=1).shape |
| 5544 | (4, 3, 4) |
| 5545 | |
| 5546 | >>> da.stack(data, axis=-1).shape |
| 5547 | (4, 4, 3) |
| 5548 | |
| 5549 | Result is a new dask Array |
| 5550 | |
| 5551 | See Also |
| 5552 | -------- |
| 5553 | concatenate |
| 5554 | """ |
| 5555 | from dask.array import wrap |
| 5556 | |
| 5557 | seq = [asarray(a, allow_unknown_chunksizes=allow_unknown_chunksizes) for a in seq] |
| 5558 | |
| 5559 | if not seq: |
| 5560 | raise ValueError("Need array(s) to stack") |
| 5561 | if not allow_unknown_chunksizes and not all(x.shape == seq[0].shape for x in seq): |
| 5562 | idx = first(i for i in enumerate(seq) if i[1].shape != seq[0].shape) |
| 5563 | raise ValueError( |
| 5564 | "Stacked arrays must have the same shape. The first array had shape " |
| 5565 | f"{seq[0].shape}, while array {idx[0] + 1} has shape {idx[1].shape}." |
| 5566 | ) |
| 5567 |
searching dependent graphs…