Skip to content

Commit 1ed28a8

Browse files
committed
Further bugfixes for NumPy 1.26 compatibility
1 parent 572ea0f commit 1ed28a8

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

src/blosc2/ndarray.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
from blosc2.info import InfoReporter
3535
from blosc2.schunk import SChunk
3636

37+
# NumPy version and a convenient boolean flag
38+
NUMPY_GE_2_0 = np.__version__ >= "2.0"
39+
3740

3841
def is_documented_by(original):
3942
def wrapper(target):
@@ -2896,19 +2899,20 @@ def chunkwise_logaddexp(inputs, output, offset):
28962899
output[:] = np.logaddexp(x1, x2)
28972900

28982901
dtype = blosc2.result_type(x1.dtype, x2.dtype)
2899-
if dtype == blosc2.bool:
2902+
if dtype == blosc2.bool_:
29002903
raise TypeError("logaddexp doesn't accept boolean arguments.")
29012904

29022905
if np.issubdtype(dtype, np.integer):
29032906
dtype = blosc2.float32
29042907
return blosc2.lazyudf(chunkwise_logaddexp, (x1, x2), dtype=dtype, shape=x1.shape)
29052908

29062909

2907-
try: # handle different numpy versions
2910+
# handle different numpy versions
2911+
if NUMPY_GE_2_0: # array-api compliant
29082912
nplshift = np.bitwise_left_shift
29092913
nprshift = np.bitwise_right_shift
29102914
npbinvert = np.bitwise_invert
2911-
except AttributeError:
2915+
else: # not array-api compliant
29122916
nplshift = np.left_shift
29132917
nprshift = np.right_shift
29142918
npbinvert = np.bitwise_not
@@ -2967,8 +2971,8 @@ def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
29672971
np.bitwise_or: "|",
29682972
np.bitwise_xor: "^",
29692973
np.arctan2: "arctan2",
2970-
nplshift: "<<",
2971-
nprshift: ">>",
2974+
nplshift: "<<", # nplshift selected above according to numpy version
2975+
nprshift: ">>", # nprshift selected above according to numpy version
29722976
np.remainder: "%",
29732977
np.nextafter: "nextafter",
29742978
np.copysign: "copysign",
@@ -3002,7 +3006,7 @@ def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
30023006
np.conj: "conj",
30033007
np.real: "real",
30043008
np.imag: "imag",
3005-
npbinvert: "~",
3009+
npbinvert: "~", # npbinvert selected above according to numpy version
30063010
np.isnan: "isnan",
30073011
np.isfinite: "isfinite",
30083012
np.isinf: "isinf",
@@ -3028,10 +3032,10 @@ def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
30283032
value = inputs[0] if inputs[1] is self else inputs[1]
30293033
_check_allowed_dtypes(value)
30303034
# catch special case of multiplying two bools (not implemented in numexpr)
3031-
if ufunc_map[ufunc] == "*" and blosc2.result_type(value, self) == blosc2.bool:
3035+
if ufunc_map[ufunc] == "*" and blosc2.result_type(value, self) == blosc2.bool_:
30323036
return blosc2.LazyExpr(new_op=(value, "&", self))
30333037
# catch special case of adding two bools (not implemented in numexpr)
3034-
if ufunc_map[ufunc] == "+" and blosc2.result_type(value, self) == blosc2.bool:
3038+
if ufunc_map[ufunc] == "+" and blosc2.result_type(value, self) == blosc2.bool_:
30353039
return blosc2.LazyExpr(new_op=(value, "|", self))
30363040
return blosc2.LazyExpr(new_op=(value, ufunc_map[ufunc], self))
30373041

@@ -3044,7 +3048,7 @@ def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
30443048

30453049
def __add__(self, value: int | float | NDArray | NDField | blosc2.C2Array, /) -> blosc2.LazyExpr:
30463050
_check_allowed_dtypes(value)
3047-
if blosc2.result_type(value, self) == blosc2.bool:
3051+
if blosc2.result_type(value, self) == blosc2.bool_:
30483052
return blosc2.LazyExpr(new_op=(value, "|", self))
30493053
return blosc2.LazyExpr(new_op=(self, "+", value))
30503054

@@ -3082,7 +3086,7 @@ def __rsub__(self, value: int | float | NDArray | NDField | blosc2.C2Array, /) -
30823086
def __mul__(self, value: int | float | NDArray | NDField | blosc2.C2Array, /) -> blosc2.LazyExpr:
30833087
_check_allowed_dtypes(value)
30843088
# catch special case of multiplying two bools (not implemented in numexpr)
3085-
if blosc2.result_type(value, self) == blosc2.bool:
3089+
if blosc2.result_type(value, self) == blosc2.bool_:
30863090
return blosc2.LazyExpr(new_op=(value, "&", self))
30873091
return blosc2.LazyExpr(new_op=(self, "*", value))
30883092

@@ -3801,7 +3805,8 @@ def get_fselection_numpy(self, key: list | np.ndarray) -> np.ndarray:
38013805
return_index=True,
38023806
return_inverse=True,
38033807
)
3804-
idx_inv = idx_inv if chunked_arr.shape != idx_inv.shape else idx_inv.squeeze(-1)
3808+
# In some versions of Numpy, output of np.unique has dummy dimension
3809+
idx_inv = idx_inv if len(idx_inv.shape) == 1 else idx_inv.squeeze(-1)
38053810
unique_chunks = chunked_arr[row_ids]
38063811
# sort by chunks (can't sort by index since larger index could belong to lower chunk)
38073812
# e.g. chunks of (100, 10) means (50, 15) has chunk idx (0,1) but (60,5) has (0, 0)

0 commit comments

Comments
 (0)