Skip to content

Commit a3a81f4

Browse files
author
Luke Shaw
committed
Incorporate expand_dims and add blosc2.stack
1 parent 5c933ec commit a3a81f4

7 files changed

Lines changed: 55 additions & 4 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ else()
5050
include(FetchContent)
5151
FetchContent_Declare(blosc2
5252
GIT_REPOSITORY https://github.com/Blosc/c-blosc2
53-
GIT_TAG 1c2f8bb0c914c43e23b751fbcf6642cd7aec09db # v2.18.0 (concatenate added)
53+
GIT_TAG 22b828b412391a8cbeeb97415777c5beecef73ff # v2.18.1 (expand_dims added)
5454
)
5555
FetchContent_MakeAvailable(blosc2)
5656
include_directories("${blosc2_SOURCE_DIR}/include")

doc/reference/ndarray.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ Constructors
7474
copy
7575
concatenate
7676
empty
77+
expand_dims
7778
frombuffer
7879
fromiter
7980
nans

src/blosc2/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ class Tuner(Enum):
239239
reshape,
240240
copy,
241241
concatenate,
242+
expand_dims,
242243
empty,
243244
frombuffer,
244245
fromiter,

src/blosc2/blosc2_ext.pyx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,7 @@ cdef extern from "b2nd.h":
509509
int b2nd_copy(b2nd_context_t *ctx, b2nd_array_t *src, b2nd_array_t **array)
510510
int b2nd_concatenate(b2nd_context_t *ctx, b2nd_array_t *src1, b2nd_array_t *src2,
511511
int8_t axis, c_bool copy, b2nd_array_t **array)
512+
int b2nd_expand_dims(const b2nd_array_t *array, b2nd_array_t ** view, const int8_t axis)
512513
int b2nd_from_schunk(blosc2_schunk *schunk, b2nd_array_t **array)
513514

514515
void blosc2_unidim_to_multidim(uint8_t ndim, int64_t *shape, int64_t i, int64_t *index)
@@ -2892,3 +2893,14 @@ def concatenate(arr1: NDArray, arr2: NDArray, axis: int, **kwargs):
28922893
else:
28932894
# Return the first array, which now contains the concatenated data
28942895
return arr1
2896+
2897+
def expand_dims(arr1: NDArray, axis: int):
2898+
"""
2899+
Add new dummy axis to NDArray object at specified dimension.
2900+
"""
2901+
cdef b2nd_array_t *view
2902+
_check_rc(b2nd_expand_dims(arr1.array, &view, axis),
2903+
"Error while concatenating the arrays")
2904+
2905+
return blosc2.NDArray(_schunk=PyCapsule_New(view.sc, <char *> "blosc2_schunk*", NULL),
2906+
_array=PyCapsule_New(view, <char *> "b2nd_array_t*", NULL))

src/blosc2/ndarray.py

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

35903590

3591+
def expand_dims(array: NDArray, axis=0) -> NDArray:
3592+
return blosc2_ext.expand_dims(array, axis=axis)
3593+
3594+
35913595
def stack(arrays: list[NDArray], axis=0, **kwargs: Any) -> NDArray:
3592-
"""Stack two arrays, creating a new axis.
3596+
"""Stack multiple arrays, creating a new axis.
35933597
35943598
Parameters
35953599
----------
@@ -3621,8 +3625,8 @@ def stack(arrays: list[NDArray], axis=0, **kwargs: Any) -> NDArray:
36213625
if axis < 0:
36223626
axis += arrays[0].ndim + 1 # Adjust axis to be within the new stacked array's dimensions
36233627
newarrays = []
3624-
for i, arr in enumerate(arrays):
3625-
newarrays[i] = blosc2_ext.expand_dims(arr, axis=axis)
3628+
for arr in arrays:
3629+
newarrays += [blosc2.expand_dims(arr, axis=axis)]
36263630
return blosc2.concatenate(newarrays, axis, **kwargs)
36273631

36283632

tests/ndarray/test_concatenate.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,22 @@ def test_concat3(shape1, shape2, shape3, dtype, axis):
6464
np.testing.assert_almost_equal(result[:], nparray)
6565

6666

67+
@pytest.mark.parametrize(
68+
("shape", "dtype", "axis"),
69+
[
70+
([521], "i2", 0),
71+
([521, 121], "u4", 0),
72+
([52, 21], "i8", 1),
73+
([521, 121, 10], "f4", 0),
74+
([121, 521, 10], "f8", 1),
75+
([121, 121, 101], "i4", 2),
76+
# 4-dimensional arrays
77+
([21, 121, 101, 10], "f4", 0),
78+
([121, 21, 101, 10], "i8", 1),
79+
([121, 121, 10, 10], "i8", 2),
80+
([121, 121, 101, 2], "i8", -1),
81+
],
82+
)
6783
def test_stack(shape, dtype, axis):
6884
ndarr1 = blosc2.arange(0, int(np.prod(shape)), 1, dtype=dtype, shape=shape)
6985
ndarr2 = blosc2.arange(0, int(np.prod(shape)), 1, dtype=dtype, shape=shape)

tests/ndarray/test_resize.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,20 @@ def test_resize(shape, new_shape, chunks, blocks, fill_value):
2929
slices = tuple(slice(s) for s in shape)
3030
for i in np.nditer(a[slices]):
3131
assert i == fill_value
32+
33+
34+
@pytest.mark.parametrize(
35+
("shape", "axis", "chunks", "blocks", "fill_value"),
36+
[
37+
((100, 1230), 1, (200, 100), (55, 3), b"0123"),
38+
((23, 34), 0, (20, 20), (10, 10), 1234),
39+
((80, 51, 60), -1, (20, 10, 33), (6, 6, 26), 3.333),
40+
],
41+
)
42+
def test_expand_dims(shape, axis, chunks, blocks, fill_value):
43+
a = blosc2.full(shape, fill_value=fill_value, chunks=chunks, blocks=blocks)
44+
45+
b = blosc2.expand_dims(a, axis=axis)
46+
npa = np.expand_dims(a, axis)
47+
assert npa.shape == b.shape
48+
np.testing.assert_array_equal(npa, b[:])

0 commit comments

Comments
 (0)