We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 5c933ec commit a3a81f4Copy full SHA for a3a81f4
7 files changed
CMakeLists.txt
@@ -50,7 +50,7 @@ else()
50
include(FetchContent)
51
FetchContent_Declare(blosc2
52
GIT_REPOSITORY https://github.com/Blosc/c-blosc2
53
- GIT_TAG 1c2f8bb0c914c43e23b751fbcf6642cd7aec09db # v2.18.0 (concatenate added)
+ GIT_TAG 22b828b412391a8cbeeb97415777c5beecef73ff # v2.18.1 (expand_dims added)
54
)
55
FetchContent_MakeAvailable(blosc2)
56
include_directories("${blosc2_SOURCE_DIR}/include")
doc/reference/ndarray.rst
@@ -74,6 +74,7 @@ Constructors
74
copy
75
concatenate
76
empty
77
+ expand_dims
78
frombuffer
79
fromiter
80
nans
src/blosc2/__init__.py
@@ -239,6 +239,7 @@ class Tuner(Enum):
239
reshape,
240
copy,
241
concatenate,
242
+ expand_dims,
243
empty,
244
frombuffer,
245
fromiter,
src/blosc2/blosc2_ext.pyx
@@ -509,6 +509,7 @@ cdef extern from "b2nd.h":
509
int b2nd_copy(b2nd_context_t *ctx, b2nd_array_t *src, b2nd_array_t **array)
510
int b2nd_concatenate(b2nd_context_t *ctx, b2nd_array_t *src1, b2nd_array_t *src2,
511
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)
513
int b2nd_from_schunk(blosc2_schunk *schunk, b2nd_array_t **array)
514
515
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):
2892
2893
else:
2894
# Return the first array, which now contains the concatenated data
2895
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
@@ -3588,8 +3588,12 @@ def concatenate(arrays: list[NDArray], /, axis=0, **kwargs: Any) -> NDArray: #
3588
3589
3590
3591
+def expand_dims(array: NDArray, axis=0) -> NDArray:
3592
+ return blosc2_ext.expand_dims(array, axis=axis)
3593
3594
3595
def stack(arrays: list[NDArray], axis=0, **kwargs: Any) -> NDArray:
- """Stack two arrays, creating a new axis.
3596
+ """Stack multiple arrays, creating a new axis.
3597
3598
Parameters
3599
----------
@@ -3621,8 +3625,8 @@ def stack(arrays: list[NDArray], axis=0, **kwargs: Any) -> NDArray:
3621
3625
if axis < 0:
3622
3626
axis += arrays[0].ndim + 1 # Adjust axis to be within the new stacked array's dimensions
3623
3627
newarrays = []
3624
- for i, arr in enumerate(arrays):
- newarrays[i] = blosc2_ext.expand_dims(arr, axis=axis)
3628
+ for arr in arrays:
3629
+ newarrays += [blosc2.expand_dims(arr, axis=axis)]
3630
return blosc2.concatenate(newarrays, axis, **kwargs)
3631
3632
tests/ndarray/test_concatenate.py
@@ -64,6 +64,22 @@ def test_concat3(shape1, shape2, shape3, dtype, axis):
64
np.testing.assert_almost_equal(result[:], nparray)
65
66
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),
+ ([121, 521, 10], "f8", 1),
+ ([121, 121, 101], "i4", 2),
+ # 4-dimensional arrays
+ ([21, 121, 101, 10], "f4", 0),
+ ([121, 21, 101, 10], "i8", 1),
+ ([121, 121, 10, 10], "i8", 2),
+ ([121, 121, 101, 2], "i8", -1),
81
+ ],
82
+)
83
def test_stack(shape, dtype, axis):
84
ndarr1 = blosc2.arange(0, int(np.prod(shape)), 1, dtype=dtype, shape=shape)
85
ndarr2 = blosc2.arange(0, int(np.prod(shape)), 1, dtype=dtype, shape=shape)
tests/ndarray/test_resize.py
@@ -29,3 +29,20 @@ def test_resize(shape, new_shape, chunks, blocks, fill_value):
29
slices = tuple(slice(s) for s in shape)
30
for i in np.nditer(a[slices]):
31
assert i == fill_value
32
33
34
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