Skip to content

Commit 5b16a66

Browse files
committed
First fixes for setitem, need to fix get_flatter_slices
1 parent 407aa21 commit 5b16a66

1 file changed

Lines changed: 16 additions & 10 deletions

File tree

src/blosc2/ndarray.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,8 @@ def get_flat_slices(
233233
A list of slices that correspond to the slice `s`.
234234
"""
235235
ndim = len(shape)
236+
if ndim == 0:
237+
return None #### something
236238
start = [s[i].start if s[i].start is not None else 0 for i in range(ndim)]
237239
stop = [builtins.min(s[i].stop if s[i].stop is not None else shape[i], shape[i]) for i in range(ndim)]
238240
# Steps are not used in the computation, so raise an error if they are not None or 1
@@ -1685,11 +1687,6 @@ def __getitem__( # noqa: C901
16851687
[3.3333, 3.3333, 3.3333, 3.3333, 3.3333],
16861688
[3.3333, 3.3333, 3.3333, 3.3333, 3.3333]])
16871689
"""
1688-
# First try some fast paths for common cases
1689-
# Check if there is a None = new axis
1690-
if key is None:
1691-
return expand_dims(self, axis=0)
1692-
16931690
# The more general case (this is quite slow)
16941691
# If the key is a LazyExpr, decorate with ``where`` and return it
16951692
if isinstance(key, blosc2.LazyExpr):
@@ -1749,7 +1746,11 @@ def __getitem__( # noqa: C901
17491746

17501747
return nparr
17511748

1752-
def __setitem__(self, key: int | slice | Sequence[slice], value: object):
1749+
def __setitem__(
1750+
self,
1751+
key: None | int | slice | Sequence[slice | int | np.bool_ | np.ndarray[int | np.bool_] | None],
1752+
value: object,
1753+
):
17531754
"""Set a slice of the array.
17541755
17551756
Parameters
@@ -1780,8 +1781,11 @@ def __setitem__(self, key: int | slice | Sequence[slice], value: object):
17801781
[3.3333, 3.3333, 3.3333, 3.3333, 3.3333, 3.3333, 3.3333, 3.3333]])
17811782
"""
17821783
blosc2_ext.check_access_mode(self.schunk.urlpath, self.schunk.mode)
1783-
key, _ = process_key(key, self.shape)
1784-
start, stop, step, _ = get_ndarray_start_stop(self.ndim, key, self.shape)
1784+
1785+
key_ = (key,) if not isinstance(key, tuple) else key
1786+
key_ = tuple(k[:] if isinstance(k, NDArray) else k for k in key_) # decompress NDArrays
1787+
key_, mask = process_key(key_, self.shape) # internally handles key an integer
1788+
start, stop, step, none_mask = get_ndarray_start_stop(self.ndim, key, self.shape)
17851789
if step != (1,) * self.ndim:
17861790
raise ValueError("Step parameter is not supported yet")
17871791
key = (start, stop)
@@ -4004,7 +4008,7 @@ def save(array: NDArray, urlpath: str, contiguous=True, **kwargs: Any) -> None:
40044008
array.save(urlpath, contiguous, **kwargs)
40054009

40064010

4007-
def asarray(array: Sequence | np.ndarray | blosc2.C2Array, **kwargs: Any) -> NDArray: # noqa: C901
4011+
def asarray(array: Sequence | np.ndarray | blosc2.C2Array, copy=True, **kwargs: Any) -> NDArray: # noqa: C901
40084012
"""Convert the `array` to an `NDArray`.
40094013
40104014
Parameters
@@ -4039,6 +4043,8 @@ def asarray(array: Sequence | np.ndarray | blosc2.C2Array, **kwargs: Any) -> NDA
40394043
>>> # Create a NDArray from a NumPy array
40404044
>>> nda = blosc2.asarray(a)
40414045
"""
4046+
if not copy:
4047+
raise ValueError("asarray which avoids copy not implemented yet.")
40424048
# Convert scalars to numpy array
40434049
dtype = kwargs.pop("dtype", None) # check if dtype provided
40444050
if not hasattr(array, "shape"):
@@ -4066,7 +4072,7 @@ def asarray(array: Sequence | np.ndarray | blosc2.C2Array, **kwargs: Any) -> NDA
40664072
if not isinstance(array, np.ndarray):
40674073
if hasattr(array, "chunks"):
40684074
# A getitem operation should be enough to get a numpy array
4069-
array = array[:]
4075+
array = array[()]
40704076
else:
40714077
if not array.flags.contiguous:
40724078
array = np.ascontiguousarray(array)

0 commit comments

Comments
 (0)