Skip to content

Commit bf6fa61

Browse files
author
Luke Shaw
committed
Fix lazyexpr o0 bug to pass test
1 parent fec1012 commit bf6fa61

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/blosc2/lazyexpr.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -719,11 +719,11 @@ def visit_Name(self, node):
719719
opname,
720720
v,
721721
) in localop.operands.items(): # expression operands already in terms of basic operands
722-
newopname = self.update_func(v)
722+
newopname = ";" + self.update_func(v) # add illegal character ;
723723
newexpr = re.sub(
724724
rf"(?<=\s){opname}|(?<=\(){opname}", newopname, newexpr
725725
) # replace with newopname
726-
node.id = newexpr
726+
node.id = newexpr.replace(";", "") # remove all illegal characters
727727
else:
728728
node.id = self.update_func(localop)
729729
else:

tests/ndarray/test_reductions.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,3 +468,23 @@ def test_slicebrackets_lazy():
468468
arr = blosc2.lazyexpr("anarr[10, 1] + 1", {"anarr": a})
469469
newarr = arr[:]
470470
np.testing.assert_allclose(newarr, a[10, 1] + 1)
471+
472+
473+
def test_reduce_string():
474+
shape = (10, 10, 2)
475+
476+
# Create a NDArray from a NumPy array
477+
npa = np.linspace(0, 1, np.prod(shape), dtype=np.float32).reshape(shape)
478+
npb = np.linspace(1, 2, np.prod(shape), dtype=np.float64).reshape(shape)
479+
npc = npa**2 + npb**2 + 2 * npa * npb + 1
480+
481+
a = blosc2.asarray(npa)
482+
b = blosc2.asarray(npb)
483+
484+
# Get a LazyExpr instance
485+
c = a**2 + b**2 + 2 * a * b + 1
486+
# Evaluate: output is a NDArray
487+
d = blosc2.lazyexpr("sl + c.sum() + a.std()", operands={"a": a, "c": c, "sl": a.slice((1, 1))})
488+
sum = d.compute()[()]
489+
npsum = npa[1, 1] + np.sum(npc) + np.std(npa)
490+
assert np.allclose(sum, npsum)

0 commit comments

Comments
 (0)