Skip to content

Commit ab05fdb

Browse files
authored
Merge pull request #428 from Blosc/correctStack
Improve mode handling for concatenating to disk
2 parents 8998c37 + 41667b5 commit ab05fdb

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/blosc2/ndarray.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3580,8 +3580,13 @@ def concatenate(arrays: list[NDArray], /, axis=0, **kwargs: Any) -> NDArray: #
35803580
kwargs = _check_ndarray_kwargs(**kwargs)
35813581
# Proceed with the actual concatenation
35823582
copy = True
3583+
# When provided urlpath coincides with an array
3584+
mode = kwargs.pop("mode", "a") # default mode for blosc2 is "a"
35833585
for arr2 in arrays[1:]:
3584-
arr1 = blosc2_ext.concatenate(arr1, arr2, axis, copy=copy, **kwargs)
3586+
arr1 = blosc2_ext.concatenate(arr1, arr2, axis, copy=copy, mode=mode, **kwargs)
3587+
# Have now overwritten existing file (if mode ='w'), need to change mode
3588+
# for concatenating to the same file
3589+
mode = "r" if mode == "r" else "a"
35853590
# arr1 is now the result of the concatenation, so we can now just enlarge it
35863591
copy = False
35873592

tests/ndarray/test_concatenate.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,17 @@ def test_stack(shape, dtype, axis):
8585
ndarr2 = blosc2.arange(0, int(np.prod(shape)), 1, dtype=dtype, shape=shape)
8686
ndarr3 = blosc2.arange(0, int(np.prod(shape)), 1, dtype=dtype, shape=shape)
8787
cparams = blosc2.CParams(codec=blosc2.Codec.BLOSCLZ)
88-
result = blosc2.stack([ndarr1, ndarr2, ndarr3], axis=axis, cparams=cparams)
88+
result = blosc2.stack(
89+
[ndarr1, ndarr2, ndarr3], axis=axis, cparams=cparams, urlpath="localfile.b2nd", mode="w"
90+
)
8991
nparray = np.stack([ndarr1[:], ndarr2[:], ndarr3[:]], axis=axis)
9092
np.testing.assert_almost_equal(result[:], nparray)
93+
94+
newres = blosc2.open("localfile.b2nd", mode="r")
95+
np.testing.assert_almost_equal(newres[:], nparray)
96+
97+
# Test overwriting existing file
98+
result = blosc2.stack(
99+
[ndarr1, ndarr2, ndarr3], axis=axis, cparams=cparams, urlpath="localfile.b2nd", mode="w"
100+
)
101+
np.testing.assert_almost_equal(result[:], nparray)

0 commit comments

Comments
 (0)