Skip to content

Commit 8a1c202

Browse files
committed
New blosc2.concat that overrides blosc2.concatenate (now deprecated)
1 parent 2c2b1c8 commit 8a1c202

File tree

6 files changed

+30
-15
lines changed

6 files changed

+30
-15
lines changed

bench/ndarray/concatenate.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,15 @@ def run_benchmark(num_arrays=10, size=500, aligned_chunks=False, axis=0,
7373

7474
# Time the concatenation
7575
start_time = time.time()
76-
result = blosc2.concatenate(arrays, axis=axis, cparams=blosc2.CParams(codec=codec))
76+
result = blosc2.concat(arrays, axis=axis, cparams=blosc2.CParams(codec=codec))
7777
duration = time.time() - start_time
7878

7979
return duration, result.shape, data_size_gb
8080

8181

8282
def run_numpy_benchmark(num_arrays=10, size=500, axis=0, dtype=np.float64, datadist="linspace"):
8383
"""
84-
Benchmark numpy.concatenate performance for comparison.
84+
Benchmark numpy.concat performance for comparison.
8585
8686
Parameters:
8787
- num_arrays: Number of arrays to concatenate
@@ -120,7 +120,7 @@ def run_numpy_benchmark(num_arrays=10, size=500, axis=0, dtype=np.float64, datad
120120

121121
# Time the concatenation
122122
start_time = time.time()
123-
result = np.concatenate(numpy_arrays, axis=axis)
123+
result = np.concat(numpy_arrays, axis=axis)
124124
duration = time.time() - start_time
125125

126126
return duration, result.shape, data_size_gb
@@ -200,11 +200,11 @@ def autolabel(rects, ax):
200200

201201
# Save the plot
202202
plt.tight_layout()
203-
plt.savefig(os.path.join(output_dir, 'concatenate_benchmark_combined.png'), dpi=100)
203+
plt.savefig(os.path.join(output_dir, 'concat_benchmark_combined.png'), dpi=100)
204204
plt.show()
205205
plt.close()
206206

207-
print(f"Combined plot saved to {os.path.join(output_dir, 'concatenate_benchmark_combined.png')}")
207+
print(f"Combined plot saved to {os.path.join(output_dir, 'concat_benchmark_combined.png')}")
208208

209209

210210
def main():

doc/reference/ndarray.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ Constructors
7272

7373
asarray
7474
copy
75-
concatenate
75+
concat
7676
empty
7777
expand_dims
7878
frombuffer

src/blosc2/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ class Tuner(Enum):
243243
sort,
244244
reshape,
245245
copy,
246+
concat,
246247
concatenate,
247248
expand_dims,
248249
empty,

src/blosc2/blosc2_ext.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2872,7 +2872,7 @@ def schunk_get_slice_nchunks(schunk: SChunk, key):
28722872
return res
28732873

28742874

2875-
def concatenate(arr1: NDArray, arr2: NDArray, axis: int, **kwargs):
2875+
def concat(arr1: NDArray, arr2: NDArray, axis: int, **kwargs):
28762876
"""
28772877
Concatenate two NDArray objects along a specified axis.
28782878
"""

src/blosc2/ndarray.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3523,7 +3523,7 @@ def copy(array: NDArray, dtype: np.dtype | str = None, **kwargs: Any) -> NDArray
35233523
return array.copy(dtype, **kwargs)
35243524

35253525

3526-
def concatenate(arrays: list[NDArray], /, axis=0, **kwargs: Any) -> NDArray: # noqa: C901
3526+
def concat(arrays: list[NDArray], /, axis=0, **kwargs: Any) -> NDArray: # noqa: C901
35273527
"""Concatenate a list of arrays along a specified axis.
35283528
35293529
Parameters
@@ -3549,7 +3549,7 @@ def concatenate(arrays: list[NDArray], /, axis=0, **kwargs: Any) -> NDArray: #
35493549
>>> import numpy as np
35503550
>>> arr1 = blosc2.arange(0, 5, dtype=np.int32)
35513551
>>> arr2 = blosc2.arange(5, 10, dtype=np.int32)
3552-
>>> result = blosc2.concatenate([arr1, arr2])
3552+
>>> result = blosc2.concat([arr1, arr2])
35533553
>>> print(result[:])
35543554
[0 1 2 3 4 5 6 7 8 9]
35553555
"""
@@ -3583,7 +3583,7 @@ def concatenate(arrays: list[NDArray], /, axis=0, **kwargs: Any) -> NDArray: #
35833583
# When provided urlpath coincides with an array
35843584
mode = kwargs.pop("mode", "a") # default mode for blosc2 is "a"
35853585
for arr2 in arrays[1:]:
3586-
arr1 = blosc2_ext.concatenate(arr1, arr2, axis, copy=copy, mode=mode, **kwargs)
3586+
arr1 = blosc2_ext.concat(arr1, arr2, axis, copy=copy, mode=mode, **kwargs)
35873587
# Have now overwritten existing file (if mode ='w'), need to change mode
35883588
# for concatenating to the same file
35893589
mode = "r" if mode == "r" else "a"
@@ -3593,6 +3593,20 @@ def concatenate(arrays: list[NDArray], /, axis=0, **kwargs: Any) -> NDArray: #
35933593
return arr1
35943594

35953595

3596+
# Previous concatenate function was renamed to concat. Keep it with a DeprecationWarning
3597+
def concatenate(arrays: list[NDArray], /, axis=0, **kwargs: Any) -> NDArray:
3598+
"""Concatenate a list of arrays along a specified axis.
3599+
3600+
This is an alias for :func:`concat`. It is kept for backward compatibility.
3601+
"""
3602+
warnings.warn(
3603+
"blosc2.concatenate is deprecated, use blosc2.concat instead.",
3604+
DeprecationWarning,
3605+
stacklevel=2,
3606+
)
3607+
return concat(arrays, axis, **kwargs)
3608+
3609+
35963610
def expand_dims(array: NDArray, axis=0) -> NDArray:
35973611
if not isinstance(array, blosc2.NDArray):
35983612
raise TypeError("Argument array must be instance of blosc2.NDArray")
@@ -3638,7 +3652,7 @@ def stack(arrays: list[NDArray], axis=0, **kwargs: Any) -> NDArray:
36383652
newarrays = []
36393653
for arr in arrays:
36403654
newarrays += [blosc2.expand_dims(arr, axis=axis)]
3641-
return blosc2.concatenate(newarrays, axis, **kwargs)
3655+
return blosc2.concat(newarrays, axis, **kwargs)
36423656

36433657

36443658
def save(array: NDArray, urlpath: str, contiguous=True, **kwargs: Any) -> None:

tests/ndarray/test_concatenate.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ def test_concat2(shape1, shape2, dtype, axis):
3333
ndarr1 = blosc2.arange(0, int(np.prod(shape1)), 1, dtype=dtype, shape=shape1)
3434
ndarr2 = blosc2.arange(0, int(np.prod(shape2)), 1, dtype=dtype, shape=shape2)
3535
cparams = blosc2.CParams(clevel=1)
36-
result = blosc2.concatenate([ndarr1, ndarr2], axis=axis, cparams=cparams)
37-
nparray = np.concatenate([ndarr1[:], ndarr2[:]], axis=axis)
36+
result = blosc2.concat([ndarr1, ndarr2], axis=axis, cparams=cparams)
37+
nparray = np.concat([ndarr1[:], ndarr2[:]], axis=axis)
3838
np.testing.assert_almost_equal(result[:], nparray)
3939

4040

@@ -59,8 +59,8 @@ def test_concat3(shape1, shape2, shape3, dtype, axis):
5959
ndarr2 = blosc2.arange(0, int(np.prod(shape2)), 1, dtype=dtype, shape=shape2)
6060
ndarr3 = blosc2.arange(0, int(np.prod(shape3)), 1, dtype=dtype, shape=shape3)
6161
cparams = blosc2.CParams(codec=blosc2.Codec.BLOSCLZ)
62-
result = blosc2.concatenate([ndarr1, ndarr2, ndarr3], axis=axis, cparams=cparams)
63-
nparray = np.concatenate([ndarr1[:], ndarr2[:], ndarr3[:]], axis=axis)
62+
result = blosc2.concat([ndarr1, ndarr2, ndarr3], axis=axis, cparams=cparams)
63+
nparray = np.concat([ndarr1[:], ndarr2[:], ndarr3[:]], axis=axis)
6464
np.testing.assert_almost_equal(result[:], nparray)
6565

6666

0 commit comments

Comments
 (0)