Skip to content

Commit 89c0882

Browse files
author
Luke Shaw
committed
Starting to implement stack function
1 parent be86f0e commit 89c0882

3 files changed

Lines changed: 49 additions & 0 deletions

File tree

src/blosc2/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ class Tuner(Enum):
253253
permute_dims,
254254
transpose,
255255
matrix_transpose,
256+
stack,
256257
)
257258

258259
from .c2array import c2context, C2Array, URLPath

src/blosc2/ndarray.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3588,6 +3588,44 @@ def concatenate(arrays: list[NDArray], /, axis=0, **kwargs: Any) -> NDArray: #
35883588
return arr1
35893589

35903590

3591+
def stack(arrays: list[NDArray], axis=0, **kwargs: Any) -> NDArray:
3592+
"""Stack two arrays, creating a new axis.
3593+
3594+
Parameters
3595+
----------
3596+
arrays: list of :ref:`NDArray`
3597+
A list containing two or more NDArray instances to be stacked.
3598+
axis: int, optional
3599+
The new axis along which the arrays will be stacked. Default is 0.
3600+
3601+
Other Parameters
3602+
----------------
3603+
kwargs: dict, optional
3604+
Keyword arguments that are supported by the :func:`empty` constructor.
3605+
3606+
Returns
3607+
-------
3608+
out: :ref:`NDArray`
3609+
A new NDArray containing the stacked data.
3610+
3611+
Examples
3612+
--------
3613+
>>> import blosc2
3614+
>>> import numpy as np
3615+
>>> arr1 = blosc2.arange(0, 6, dtype=np.int32, shape=(2,3))
3616+
>>> arr2 = blosc2.arange(6, 12, dtype=np.int32, shape=(2,3))
3617+
>>> result = blosc2.stack([arr1, arr2])
3618+
>>> print(result.shape)
3619+
(2, 2, 3)
3620+
"""
3621+
if axis < 0:
3622+
axis += arrays[0].ndim + 1 # Adjust axis to be within the new stacked array's dimensions
3623+
newarrays = []
3624+
for i, arr in enumerate(arrays):
3625+
newarrays[i] = blosc2_ext.expand_dims(arr, axis=axis)
3626+
return blosc2.concatenate(newarrays, axis, **kwargs)
3627+
3628+
35913629
def save(array: NDArray, urlpath: str, contiguous=True, **kwargs: Any) -> None:
35923630
"""Save an array to a file.
35933631

tests/ndarray/test_concatenate.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,13 @@ def test_concat3(shape1, shape2, shape3, dtype, axis):
6262
result = blosc2.concatenate([ndarr1, ndarr2, ndarr3], axis=axis, cparams=cparams)
6363
nparray = np.concatenate([ndarr1[:], ndarr2[:], ndarr3[:]], axis=axis)
6464
np.testing.assert_almost_equal(result[:], nparray)
65+
66+
67+
def test_stack(shape, dtype, axis):
68+
ndarr1 = blosc2.arange(0, int(np.prod(shape)), 1, dtype=dtype, shape=shape)
69+
ndarr2 = blosc2.arange(0, int(np.prod(shape)), 1, dtype=dtype, shape=shape)
70+
ndarr3 = blosc2.arange(0, int(np.prod(shape)), 1, dtype=dtype, shape=shape)
71+
cparams = blosc2.CParams(codec=blosc2.Codec.BLOSCLZ)
72+
result = blosc2.stack([ndarr1, ndarr2, ndarr3], axis=axis, cparams=cparams)
73+
nparray = np.stack([ndarr1[:], ndarr2[:], ndarr3[:]], axis=axis)
74+
np.testing.assert_almost_equal(result[:], nparray)

0 commit comments

Comments
 (0)