Skip to content

Commit 095c1bd

Browse files
author
Luke Shaw
committed
Improve parethesis handling to pass all tests
1 parent 51cba77 commit 095c1bd

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

src/blosc2/lazyexpr.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2641,6 +2641,10 @@ def save(self, urlpath=None, **kwargs):
26412641
"urlbase": value.urlbase,
26422642
}
26432643
continue
2644+
# if isinstance(value, blosc2.LazyExpr) and hasattr(value, "array"):
2645+
# # If operand is a persistent lazy expression (i.e. on disk)
2646+
# operands[key] = value.array.urlpath
2647+
# continue
26442648
if isinstance(value, blosc2.Proxy):
26452649
# Take the required info from the Proxy._cache container
26462650
value = value._cache
@@ -2678,7 +2682,7 @@ def _new_expr(cls, expression, operands, guess, out=None, where=None, ne_args=No
26782682
if isinstance(new_expr, blosc2.LazyExpr):
26792683
# Restore the original expression and operands
26802684
new_expr.expression = f"({_expression})" # forcibly add parenthesis
2681-
new_expr.expression_tosave = new_expr.expression
2685+
new_expr.expression_tosave = _expression
26822686
new_expr.operands = _operands
26832687
new_expr.operands_tosave = operands
26842688
else:

tests/ndarray/test_lazyexpr.py

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,7 +1329,7 @@ def test_missing_operator():
13291329
# Check that the expression is still there, and can be introspected
13301330
# Note the added parentheses. The parser automatically adds these,
13311331
# mainly because of possible operator precedence issues in nested expressions.
1332-
assert expr2.expression == "(a + b)"
1332+
assert expr2.expression == "a + b"
13331333
# Check that dtype and shape are None
13341334
assert expr2.dtype is None
13351335
assert expr2.shape is None
@@ -1349,8 +1349,50 @@ def test_chain_expressions():
13491349
le1 = a**3 + blosc2.sin(a**2)
13501350
le2 = le1 < c
13511351
le3 = le2 & (b < 0)
1352-
13531352
le1_ = blosc2.lazyexpr("a ** 3 + sin(a ** 2)", {"a": a})
13541353
le2_ = blosc2.lazyexpr("(le1 < c)", {"le1": le1_, "c": c})
13551354
le3_ = blosc2.lazyexpr("(le2 & (b < 0))", {"le2": le2_, "b": b})
13561355
assert (le3_[:] == le3[:]).all()
1356+
1357+
# TODO: This test should pass eventually
1358+
# le1 = a ** 3 + blosc2.sin(a ** 2)
1359+
# le2 = le1 < c
1360+
# le3 = (b < 0)
1361+
# le4 = le2 & le3
1362+
# le1_ = blosc2.lazyexpr("a ** 3 + sin(a ** 2)", {"a": a})
1363+
# le2_ = blosc2.lazyexpr("(le1 < c)", {"le1": le1_, "c": c})
1364+
# le3_ = blosc2.lazyexpr("(b < 0)", {"b": b})
1365+
# le4_ = blosc2.lazyexpr("(le2 & le3)", {"le2": le2_, "le3": le3_})
1366+
# assert (le4_[:] == le4[:]).all()
1367+
1368+
1369+
# TODO: Test the chaining of multiple persistent lazy expressions
1370+
# def test_chain_persistentexpressions():
1371+
# N = 1_000
1372+
# dtype = "float64"
1373+
# a = blosc2.linspace(0, 1, N * N, dtype=dtype, shape=(N, N), urlpath="a.b2nd", mode="w")
1374+
# b = blosc2.linspace(1, 2, N * N, dtype=dtype, shape=(N, N), urlpath="b.b2nd", mode="w")
1375+
# c = blosc2.linspace(0, 1, N, dtype=dtype, shape=(N,), urlpath="c.b2nd", mode="w")
1376+
#
1377+
# le1 = a ** 3 + blosc2.sin(a ** 2)
1378+
# le2 = le1 < c
1379+
# le3 = (b < 0)
1380+
# le4 = le2 & le3
1381+
#
1382+
# le1_ = blosc2.lazyexpr("a ** 3 + sin(a ** 2)", {"a": a})
1383+
# le1_.save("expr1.b2nd", mode="w")
1384+
# myle1 = blosc2.open("expr1.b2nd")
1385+
#
1386+
# le2_ = blosc2.lazyexpr("(le1 < c)", {"le1": myle1, "c": c})
1387+
# le2_.save("expr2.b2nd", mode="w")
1388+
# myle2 = blosc2.open("expr2.b2nd")
1389+
#
1390+
# le3_ = blosc2.lazyexpr("(b < 0)", {"b": b})
1391+
# le3_.save("expr3.b2nd", mode="w")
1392+
# myle3 = blosc2.open("expr3.b2nd")
1393+
#
1394+
# le4_ = blosc2.lazyexpr("(le2 & le3)", {"le2": myle2, "le3": myle3})
1395+
# le4_.save("expr4.b2nd", mode="w")
1396+
# myle4 = blosc2.open("expr4.b2nd")
1397+
# print((myle4[:] == le4[:]).all())
1398+
#

0 commit comments

Comments
 (0)