Skip to content

Commit f43ac50

Browse files
Merge pull request #417 from Blosc/enableSliceLazy
Enable slice lazy
2 parents 0d74554 + ebab79e commit f43ac50

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/blosc2/lazyexpr.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2718,6 +2718,9 @@ def __getitem__(self, item):
27182718
kwargs = {"_getitem": True}
27192719
return self.compute(item, **kwargs)
27202720

2721+
def slice(self, item):
2722+
return self.compute(item) # should do a slice since _getitem = False
2723+
27212724
def __str__(self):
27222725
return f"{self.expression}"
27232726

tests/ndarray/test_lazyexpr.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,6 +1066,28 @@ def test_eval_item(array_fixture):
10661066
np.testing.assert_allclose(res[()], nres[0:10:2])
10671067

10681068

1069+
# Test lazyexpr's slice method
1070+
def test_eval_slice(array_fixture):
1071+
a1, a2, a3, a4, na1, na2, na3, na4 = array_fixture
1072+
expr = blosc2.lazyexpr("a1 + a2 - (a3 * a4)", operands={"a1": a1, "a2": a2, "a3": a3, "a4": a4})
1073+
nres = ne_evaluate("na1 + na2 - (na3 * na4)")[:2]
1074+
res = expr.slice(slice(0, 2))
1075+
assert isinstance(res, blosc2.ndarray.NDArray)
1076+
np.testing.assert_allclose(res[:], nres)
1077+
res = expr[:2]
1078+
assert isinstance(res, np.ndarray)
1079+
np.testing.assert_allclose(res, nres)
1080+
1081+
# string lazy expressions automatically use .slice internally
1082+
expr1 = blosc2.lazyexpr("a1 * a2", operands={"a1": a1, "a2": a2})
1083+
expr2 = blosc2.lazyexpr("expr1[:2] + a3[:2]")
1084+
nres = ne_evaluate("(na1 * na2) + na3")[:2]
1085+
assert isinstance(expr2, blosc2.LazyExpr)
1086+
res = expr2.compute()
1087+
assert isinstance(res, blosc2.ndarray.NDArray)
1088+
np.testing.assert_allclose(res[()], nres)
1089+
1090+
10691091
# Test get_chunk method
10701092
@pytest.mark.heavy
10711093
def test_get_chunk(array_fixture):

0 commit comments

Comments
 (0)