Skip to content

Commit a81215e

Browse files
committed
Add aliases and where method to Operand
1 parent 6c84cee commit a81215e

2 files changed

Lines changed: 86 additions & 2 deletions

File tree

src/blosc2/__init__.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ class Tuner(Enum):
178178
object_ = np.object_
179179

180180
from numpy import (
181+
bool,
181182
bool_,
182183
complex64,
183184
complex128,
@@ -420,6 +421,9 @@ def _raise(exc):
420421
# Delayed imports for avoiding overwriting of python builtins
421422
from .ndarray import (
422423
abs,
424+
acos,
425+
acosh,
426+
add,
423427
all,
424428
any,
425429
arccos,
@@ -430,6 +434,11 @@ def _raise(exc):
430434
arctan2,
431435
arctanh,
432436
array_from_ffi_ptr,
437+
asin,
438+
asinh,
439+
atan,
440+
atan2,
441+
atanh,
433442
conj,
434443
contains,
435444
cos,
@@ -511,6 +520,9 @@ def _raise(exc):
511520
"__version__",
512521
# Functions
513522
"abs",
523+
"acos",
524+
"acosh",
525+
"add",
514526
"all",
515527
"any",
516528
"arange",
@@ -524,7 +536,12 @@ def _raise(exc):
524536
"are_partitions_aligned",
525537
"are_partitions_behaved",
526538
"asarray",
539+
"asin",
540+
"asinh",
527541
"astypeclib_info",
542+
"atan",
543+
"atan2",
544+
"atanh",
528545
"broadcast_to",
529546
"compress",
530547
"compress2",

src/blosc2/ndarray.py

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,25 @@ def item(self) -> float | bool | complex | int:
10051005
"""
10061006
return self[()].item()
10071007

1008+
def where(self, value1=None, value2=None):
1009+
"""
1010+
Select ``value1`` or ``value2`` values based on ``True``/``False`` for ``self``.
1011+
1012+
Parameters
1013+
----------
1014+
value1: array_like, optional
1015+
The value to select when element of ``self`` is True.
1016+
value2: array_like, optional
1017+
The value to select when element of ``self`` is False.
1018+
1019+
Returns
1020+
-------
1021+
out: LazyExpr
1022+
A new expression with the where condition applied.
1023+
"""
1024+
expr = blosc2.LazyExpr._new_expr("o0", {"o0": self}, guess=False).where(value1, value2)
1025+
return expr.compute()
1026+
10081027
@is_documented_by(sum)
10091028
def sum(self, axis=None, dtype=None, keepdims=False, **kwargs):
10101029
expr = blosc2.LazyExpr(new_op=(self, None, None))
@@ -2673,6 +2692,9 @@ def arcsin(ndarr: NDArray | NDField | blosc2.C2Array | blosc2.LazyExpr, /) -> bl
26732692
return blosc2.LazyExpr(new_op=(ndarr, "arcsin", None))
26742693

26752694

2695+
asin = arcsin # alias
2696+
2697+
26762698
def arccos(ndarr: NDArray | NDField | blosc2.C2Array | blosc2.LazyExpr, /) -> blosc2.LazyExpr:
26772699
"""
26782700
Compute the inverse cosine, element-wise.
@@ -2708,6 +2730,9 @@ def arccos(ndarr: NDArray | NDField | blosc2.C2Array | blosc2.LazyExpr, /) -> bl
27082730
return blosc2.LazyExpr(new_op=(ndarr, "arccos", None))
27092731

27102732

2733+
acos = arccos # alias
2734+
2735+
27112736
def arctan(ndarr: NDArray | NDField | blosc2.C2Array | blosc2.LazyExpr, /) -> blosc2.LazyExpr:
27122737
"""
27132738
Compute the inverse tangent, element-wise.
@@ -2743,6 +2768,9 @@ def arctan(ndarr: NDArray | NDField | blosc2.C2Array | blosc2.LazyExpr, /) -> bl
27432768
return blosc2.LazyExpr(new_op=(ndarr, "arctan", None))
27442769

27452770

2771+
atan = arctan # alias
2772+
2773+
27462774
def arctan2(
27472775
ndarr1: NDArray | NDField | blosc2.C2Array, ndarr2: NDArray | NDField | blosc2.C2Array, /
27482776
) -> blosc2.LazyExpr:
@@ -2786,6 +2814,9 @@ def arctan2(
27862814
return blosc2.LazyExpr(new_op=(ndarr1, "arctan2", ndarr2))
27872815

27882816

2817+
atan2 = arctan2 # alias
2818+
2819+
27892820
def arcsinh(ndarr: NDArray | NDField | blosc2.C2Array | blosc2.LazyExpr, /) -> blosc2.LazyExpr:
27902821
"""
27912822
Compute the inverse hyperbolic sine, element-wise.
@@ -2821,6 +2852,9 @@ def arcsinh(ndarr: NDArray | NDField | blosc2.C2Array | blosc2.LazyExpr, /) -> b
28212852
return blosc2.LazyExpr(new_op=(ndarr, "arcsinh", None))
28222853

28232854

2855+
asinh = arcsinh # alias
2856+
2857+
28242858
def arccosh(ndarr: NDArray | NDField | blosc2.C2Array | blosc2.LazyExpr, /) -> blosc2.LazyExpr:
28252859
"""
28262860
Compute the inverse hyperbolic cosine, element-wise.
@@ -2856,6 +2890,9 @@ def arccosh(ndarr: NDArray | NDField | blosc2.C2Array | blosc2.LazyExpr, /) -> b
28562890
return blosc2.LazyExpr(new_op=(ndarr, "arccosh", None))
28572891

28582892

2893+
acosh = arccosh # alias
2894+
2895+
28592896
def arctanh(ndarr: NDArray | NDField | blosc2.C2Array | blosc2.LazyExpr, /) -> blosc2.LazyExpr:
28602897
"""
28612898
Compute the inverse hyperbolic tangent, element-wise.
@@ -2891,6 +2928,9 @@ def arctanh(ndarr: NDArray | NDField | blosc2.C2Array | blosc2.LazyExpr, /) -> b
28912928
return blosc2.LazyExpr(new_op=(ndarr, "arctanh", None))
28922929

28932930

2931+
atanh = arctanh # alias
2932+
2933+
28942934
def exp(ndarr: NDArray | NDField | blosc2.C2Array | blosc2.LazyExpr, /) -> blosc2.LazyExpr:
28952935
"""
28962936
Calculate the exponential of all elements in the input array.
@@ -3387,8 +3427,36 @@ def multiply(
33873427
return x1 * x2
33883428

33893429

3430+
def add(
3431+
x1: NDArray | NDField | blosc2.C2Array | blosc2.LazyExpr,
3432+
x2: NDArray | NDField | blosc2.C2Array | blosc2.LazyExpr,
3433+
) -> blosc2.LazyExpr:
3434+
"""
3435+
Computes the value of x1_i + x2_i for each element x1_i of the input array x1
3436+
with the respective element x2_i of the input array x2.
3437+
3438+
Parameters
3439+
-----------
3440+
x1: NDArray | NDField | blosc2.C2Array | blosc2.LazyExpr
3441+
First input array. May have any data type.
3442+
3443+
x2:NDArray | NDField | blosc2.C2Array | blosc2.LazyExpr
3444+
Second input array. Must be compatible with x1. May have any data type.
3445+
3446+
Returns
3447+
-------
3448+
out LazyExpr
3449+
A LazyArray containing the element-wise results.
3450+
3451+
References
3452+
----------
3453+
`np.add <https://numpy.org/doc/stable/reference/generated/numpy.add.html#numpy.add>`_
3454+
"""
3455+
return x1 + x2
3456+
3457+
33903458
def where(
3391-
condition: blosc2.LazyExpr,
3459+
condition: blosc2.LazyExpr | NDArray,
33923460
x: NDArray | NDField | np.ndarray | int | float | complex | bool | str | bytes | None = None,
33933461
y: NDArray | NDField | np.ndarray | int | float | complex | bool | str | bytes | None = None,
33943462
) -> blosc2.LazyExpr:
@@ -4301,7 +4369,6 @@ def asarray(
43014369
>>> # Create a NDArray from a NumPy array
43024370
>>> nda = blosc2.asarray(a)
43034371
"""
4304-
43054372
# Convert scalars to numpy array
43064373
casting = kwargs.pop("casting", "unsafe")
43074374
if casting != "unsafe":

0 commit comments

Comments
 (0)